def db_with_no_comparator(request, tempdir) -> KeyValueStorageLeveldb:
    global db_no
    if request.param == 'leveldb':
        db = KeyValueStorageLeveldb(tempdir, 'kv{}'.format(db_no))
    else:
        db = KeyValueStorageRocksdb(tempdir, 'kv{}'.format(db_no))
    db_no += 1
    yield db
    db.close()
def db_with_no_comparator(request, tempdir) -> KeyValueStorageLeveldb:
    global db_no
    if request.param == 'leveldb':
        db = KeyValueStorageLeveldb(tempdir, 'kv{}'.format(db_no))
    else:
        db = KeyValueStorageRocksdb(tempdir, 'kv{}'.format(db_no))
    db_no += 1
    yield db
    db.close()
예제 #3
0
def storage(request, tdir) -> KeyValueStorage:
    global db_no
    if request.param == 'leveldb':
        db = KeyValueStorageLeveldb(tdir, 'metrics_ldb_{}'.format(db_no))
    else:
        db = KeyValueStorageRocksdb(tdir, 'metrics_rdb_{}'.format(db_no))
    db_no += 1
    yield db
    db.close()
예제 #4
0
def kv(request, tempdir) -> KeyValueStorage:
    global i

    if request.param == 'leveldb':
        kv = KeyValueStorageLeveldb(tempdir, 'kv{}'.format(i))
    elif request.param == 'rocksdb':
        kv = KeyValueStorageRocksdb(tempdir, 'kv{}'.format(i))
    else:
        kv = KeyValueStorageInMemory()

    i += 1
    yield kv
    kv.close()
예제 #5
0
class ClientTxnLog(HasFileStorage):
    """
    An immutable log of transactions made by the client.
    """

    def __init__(self, name, baseDir=None):
        self.dataDir = "data/clients"
        self.name = name
        HasFileStorage.__init__(self, name,
                                baseDir=baseDir,
                                dataDir=self.dataDir)
        self.clientDataLocation = self.dataLocation
        if not os.path.exists(self.clientDataLocation):
            os.makedirs(self.clientDataLocation)
        # self.transactionLog = TextFileStore(self.clientDataLocation,
        #                                     "transactions")
        self.transactionLog = KeyValueStorageLeveldb(
            self.clientDataLocation, "transactions")
        self.serializer = ledger_txn_serializer

    def close(self):
        self.transactionLog.close()

    @property
    def txnFieldOrdering(self):
        fields = getTxnOrderedFields()
        return updateFieldsWithSeqNo(fields)

    def append(self, identifier: str, reqId, txn):
        key = '{}{}'.format(identifier, reqId)
        self.transactionLog.put(
            key=key, value=self.serializer.serialize(
                txn, fields=self.txnFieldOrdering, toBytes=False))

    def hasTxn(self, identifier, reqId) -> bool:
        key = '{}{}'.format(identifier, reqId)
        return key in self.transactionLog

    def reset(self):
        self.transactionLog.reset()
def kv(request, tempdir) -> KeyValueStorage:
    global i

    if request.param == 'leveldb':
        kv = KeyValueStorageLeveldb(tempdir, 'kv{}'.format(i))
    else:
        kv = KeyValueStorageRocksdb(tempdir, 'kv{}'.format(i))

    assert kv.read_only is False

    kv.put('k1', 'v1')
    kv.put('k2', 'v2')
    kv.put('k3', 'v3')
    kv.close()

    if request.param == 'leveldb':
        kv = KeyValueStorageLeveldb(tempdir, 'kv{}'.format(i), read_only=True)
    else:
        kv = KeyValueStorageRocksdb(tempdir, 'kv{}'.format(i), read_only=True)

    i += 1
    yield kv
    kv.close()
def kv(request, tempdir) -> KeyValueStorage:
    global i

    if request.param == 'leveldb':
        kv = KeyValueStorageLeveldb(tempdir, 'kv{}'.format(i))
    else:
        kv = KeyValueStorageRocksdb(tempdir, 'kv{}'.format(i))

    assert kv.read_only is False

    kv.put('k1', 'v1')
    kv.put('k2', 'v2')
    kv.put('k3', 'v3')
    kv.close()

    if request.param == 'leveldb':
        kv = KeyValueStorageLeveldb(tempdir, 'kv{}'.format(i), read_only=True)
    else:
        kv = KeyValueStorageRocksdb(tempdir, 'kv{}'.format(i), read_only=True)

    i += 1
    yield kv
    kv.close()
예제 #8
0
class LevelDbHashStore(HashStore):
    def __init__(self, dataDir, fileNamePrefix=""):
        self.dataDir = dataDir
        self.nodesDb = None
        self.leavesDb = None
        self._leafCount = 0
        self.nodes_db_name = fileNamePrefix + '_merkleNodes'
        self.leaves_db_name = fileNamePrefix + '_merkleLeaves'
        self.open()

    @property
    def is_persistent(self) -> bool:
        return True

    def writeLeaf(self, leafHash):
        self.leavesDb.put(str(self.leafCount + 1), leafHash)
        self.leafCount += 1

    def writeNode(self, node):
        start, height, nodeHash = node
        seqNo = self.getNodePosition(start, height)
        self.nodesDb.put(str(seqNo), nodeHash)

    def readLeaf(self, seqNo):
        return self._readOne(seqNo, self.leavesDb)

    def readNode(self, seqNo):
        return self._readOne(seqNo, self.nodesDb)

    def _readOne(self, pos, db):
        self._validatePos(pos)
        try:
            # Converting any bytearray to bytes
            return bytes(db.get(str(pos)))
        except KeyError:
            logger.error("{} does not have position {}".format(db, pos))

    def readLeafs(self, start, end):
        return self._readMultiple(start, end, self.leavesDb)

    def readNodes(self, start, end):
        return self._readMultiple(start, end, self.nodesDb)

    def _readMultiple(self, start, end, db):
        """
        Returns a list of hashes with serial numbers between start
         and end, both inclusive.
         """
        self._validatePos(start, end)
        # Converting any bytearray to bytes
        return [bytes(db.get(str(pos))) for pos in range(start, end + 1)]

    @property
    def leafCount(self) -> int:
        return self._leafCount

    @property
    def nodeCount(self) -> int:
        return self.nodesDb.size

    @leafCount.setter
    def leafCount(self, count: int) -> None:
        self._leafCount = count

    @property
    def closed(self):
        return (self.nodesDb is None and self.leavesDb is None) \
            or \
               (self.nodesDb.closed and self.leavesDb.closed)

    def open(self):
        self.nodesDb = KeyValueStorageLeveldb(self.dataDir, self.nodes_db_name)
        self.leavesDb = KeyValueStorageLeveldb(
            self.dataDir, self.leaves_db_name)
        self._leafCount = self.leavesDb.size

    def close(self):
        self.nodesDb.close()
        self.leavesDb.close()

    def reset(self) -> bool:
        self.nodesDb.reset()
        self.leavesDb.reset()
        self.leafCount = 0
        return True
예제 #9
0
def kv(tempdir) -> KeyValueStorageLeveldb:
    global i
    kv = KeyValueStorageLeveldb(tempdir, 'kv{}'.format(i))
    i += 1
    yield kv
    kv.close()