Esempio n. 1
0
def test_readonly_column_family():
    db = pyrocksdb.DB()
    opts = pyrocksdb.Options()
    opts.create_if_missing = True
    tmp = tempfile.TemporaryDirectory()
    s = db.open(opts, tmp.name)
    assert s.ok()

    copts = pyrocksdb.ColumnFamilyOptions()
    s, cf = db.create_column_family(copts, "new_cf")
    del cf
    db.close()

    cfd1 = pyrocksdb.ColumnFamilyDescriptor(pyrocksdb.DefaultColumnFamilyName,
                                            pyrocksdb.ColumnFamilyOptions())
    cfd2 = pyrocksdb.ColumnFamilyDescriptor("new_cf",
                                            pyrocksdb.ColumnFamilyOptions())
    cfds = pyrocksdb.VectorColumnFamilyDescriptor()
    cfds.append(cfd1)
    cfds.append(cfd2)
    db_opts = pyrocksdb.DBOptions()
    #  a = [1,2]
    s, cfhs = db.open_for_readonly(db_opts, tmp.name, cfds)
    assert (s.ok())
    assert (len(cfhs) == 2)
    assert (cfhs[0].get_name() == pyrocksdb.DefaultColumnFamilyName)
    assert (cfhs[1].get_name() == 'new_cf')

    wopts = pyrocksdb.WriteOptions()
    s = db.put(wopts, cfhs[1], b'key1', b'value1')
    assert (not s.ok())
Esempio n. 2
0
def transaction_db():
    opts = pyrocksdb.Options()
    opts.create_if_missing = True
    txn_db_opts = pyrocksdb.TransactionDBOptions()
    tmp = tempfile.TemporaryDirectory()
    db = pyrocksdb.transaction_db()
    s = db.open(opts, txn_db_opts, tmp.name)
    assert (s.ok())
    yield db
    db.close()
Esempio n. 3
0
def db():
    db = pyrocksdb.DB()
    opts = pyrocksdb.Options()
    opts.IncreaseParallelism()
    opts.OptimizeLevelStyleCompaction()
    opts.create_if_missing = True
    tmp = tempfile.TemporaryDirectory()
    s = db.open(opts, tmp.name)
    assert s.ok()
    yield db
    db.close()
Esempio n. 4
0
def test_readonly():
    db = pyrocksdb.DB()
    opts = pyrocksdb.Options()
    opts.IncreaseParallelism()
    opts.OptimizeLevelStyleCompaction()
    opts.create_if_missing = True
    tmp = tempfile.TemporaryDirectory()
    s = db.open(opts, tmp.name)
    assert s.ok()
    db.close()

    s = db.open_for_readonly(opts, tmp.name)
    assert (s.ok())

    wopts = pyrocksdb.WriteOptions()
    s = db.put(wopts, b'key1', b'value1')
    assert (not s.ok())
Esempio n. 5
0
def test_compaction_column_family():
    db = pyrocksdb.DB()
    opts = pyrocksdb.Options()
    opts.create_if_missing = True
    tmp = tempfile.TemporaryDirectory()
    s = db.open(opts, tmp.name)
    assert s.ok()

    copts = pyrocksdb.ColumnFamilyOptions()
    s, cf = db.create_column_family(copts, "new_cf")
    assert s.ok()

    wopts = pyrocksdb.WriteOptions()
    for i in range(100):
        db.put(wopts, cf, b'key1', b'value1')
    options = pyrocksdb.CompactRangeOptions()
    db.compact_range(options, None, None)
    db.close()
Esempio n. 6
0
    def __init__(self, db_name: str):
        self._logger = getLogger('tonga')
        self._db = pyrocksdb.DB()

        self._opts = pyrocksdb.Options()

        self._opts.create_if_missing = True

        s: pyrocksdb.Status = self._db.open(self._opts, db_name)

        if not s.ok():
            self._logger.error('RockDB open fail -> %s', s.to_string())
            raise RocksDBErrors

        self._wopts = pyrocksdb.WriteOptions()
        self._ropts = pyrocksdb.ReadOptions()

        self._initialize = False
Esempio n. 7
0
def test_transaction_db():
    opts = pyrocksdb.Options()
    opts.create_if_missing = True
    txn_db_opts = pyrocksdb.TransactionDBOptions()
    tmp = tempfile.TemporaryDirectory()
    db = pyrocksdb.transaction_db()
    s = db.open(opts, txn_db_opts, tmp.name)
    assert (s.ok())
    wopts = pyrocksdb.WriteOptions()
    txn = db.begin_transaction(wopts)
    assert (txn)
    ropts = pyrocksdb.ReadOptions()
    blob = txn.get(ropts, b'key1')
    assert (blob.status.is_not_found())
    del txn

    txn = db.begin_transaction(wopts)
    s = txn.put(b'key1', b'value1')
    assert (s.ok())
    s = txn.put(b'key2', b'value2')
    assert (s.ok())
    blob = txn.get(ropts, b'key1')
    assert (blob.status.ok())
    assert (blob.data == b'value1')

    # the data won't be written unitl the commit
    blob = db.get(ropts, b'key1')
    assert (blob.status.is_not_found())

    s = db.put(wopts, b'key3', b'value3')
    assert (s.ok())

    s = db.put(wopts, b'key1', b'value1')
    assert (not s.ok())

    s = txn.commit()
    assert (s.ok())
    del txn

    txn_opts = pyrocksdb.TransactionOptions()
    txn_opts.set_snapshot = True
    txn = db.begin_transaction(wopts, txn_opts)
    snapshot = txn.get_snapshot()
    del txn
Esempio n. 8
0
 def __init__(self, create_if_missing=True, flush_size=10):
     """
     Initialise a new RocksDBClient
     Args:
         create_if_missing: Create the key and value pair if they
         aren't already in the database.
         flush_size: the size of the internal cache to keep data for before
         writing to disk.
     """
     self.flush_size = flush_size
     self.path = RocksDBClient.data_path
     self.db = pyrocksdb.DB()
     self.opts = pyrocksdb.Options()
     self.create_if_missing = create_if_missing
     self.opts.IncreaseParallelism()
     self.opts.OptimizeLevelStyleCompaction()
     self.opts.create_if_missing = self.create_if_missing
     self.connection = self.db.open(self.opts, self.path)
     if self.connection.ok():
         logger.debug("DB connection established")
     else:
         raise self.ConnectionError("DB connection failed")
Esempio n. 9
0
def test_options():
    opts = pyrocksdb.Options()
    assert (opts.target_file_size_base == 64 * 1048576)
    assert (opts.target_file_size_multiplier == 1)
Esempio n. 10
0
def test_column_family():
    db = pyrocksdb.DB()
    opts = pyrocksdb.Options()
    opts.create_if_missing = True
    tmp = tempfile.TemporaryDirectory()
    s = db.open(opts, tmp.name)
    assert s.ok()

    copts = pyrocksdb.ColumnFamilyOptions()
    s, cf = db.create_column_family(copts, "new_cf")
    del cf
    db.close()

    cfd1 = pyrocksdb.ColumnFamilyDescriptor(pyrocksdb.DefaultColumnFamilyName,
                                            pyrocksdb.ColumnFamilyOptions())
    cfd2 = pyrocksdb.ColumnFamilyDescriptor("new_cf",
                                            pyrocksdb.ColumnFamilyOptions())
    cfds = pyrocksdb.VectorColumnFamilyDescriptor()
    cfds.append(cfd1)
    cfds.append(cfd2)
    db_opts = pyrocksdb.DBOptions()
    #  a = [1,2]
    s, cfhs = db.open(db_opts, tmp.name, cfds)
    assert (s.ok())
    assert (len(cfhs) == 2)
    assert (cfhs[0].get_name() == pyrocksdb.DefaultColumnFamilyName)
    assert (cfhs[1].get_name() == 'new_cf')
    wopts = pyrocksdb.WriteOptions()
    ropts = pyrocksdb.ReadOptions()
    s = db.put(wopts, cfhs[0], b'key', b'value')
    assert (s.ok())
    b = db.get(ropts, cfhs[0], b'key')
    assert (b.status.ok())
    assert (b.data == b'value')
    b = db.get(ropts, cfhs[1], b'key')
    assert (b.status.is_not_found())

    s = db.put(wopts, cfhs[1], b'key', b'value2')
    assert (s.ok())
    b = db.get(ropts, cfhs[1], b'key')
    assert (b.status.ok())
    assert (b.data == b'value2')

    b = db.get(ropts, cfhs[0], b'key')
    assert (b.status.ok())
    assert (b.data == b'value')

    s = db.delete(wopts, cfhs[0], b'key')
    assert (s.ok())
    b = db.get(ropts, cfhs[0], b'key')
    assert (b.status.is_not_found())

    s = db.delete(wopts, cfhs[1], b'key')
    assert (s.ok())
    b = db.get(ropts, cfhs[1], b'key')
    assert (b.status.is_not_found())

    # write batch
    update = pyrocksdb.WriteBatch()
    update.put(cfhs[0], b'key1', b'value1')
    update.put(cfhs[0], b'key2', b'value2')
    update.delete(cfhs[0], 'key1')
    s = db.write(wopts, update)
    assert (s.ok())
    b = db.get(ropts, cfhs[0], b'key1')
    assert (b.status.is_not_found())
    b = db.get(ropts, cfhs[0], b'key2')
    assert (b.status.ok())
    assert (b.data == b'value2')

    for cfh in cfhs:
        del cfh