|
| 1 | +from cffi import FFI |
| 2 | +from contextlib import contextmanager |
| 3 | + |
| 4 | +ffi = FFI() |
| 5 | + |
| 6 | +ffi.cdef(""" |
| 7 | +typedef struct rocksdb_t rocksdb_t; |
| 8 | +typedef struct rocksdb_options_t rocksdb_options_t; |
| 9 | +typedef struct rocksdb_readoptions_t rocksdb_readoptions_t; |
| 10 | +typedef struct rocksdb_iterator_t rocksdb_iterator_t; |
| 11 | +
|
| 12 | +rocksdb_options_t* rocksdb_options_create(void); |
| 13 | +void rocksdb_options_destroy(rocksdb_options_t*); |
| 14 | +void rocksdb_options_set_create_if_missing(rocksdb_options_t*, unsigned char); |
| 15 | +
|
| 16 | +rocksdb_t* rocksdb_open(const rocksdb_options_t* options, const char* name, char** errptr); |
| 17 | +void rocksdb_close(rocksdb_t* db); |
| 18 | +
|
| 19 | +rocksdb_readoptions_t* rocksdb_readoptions_create(void); |
| 20 | +void rocksdb_readoptions_destroy(rocksdb_readoptions_t*); |
| 21 | +
|
| 22 | +rocksdb_iterator_t* rocksdb_create_iterator(rocksdb_t* db, const rocksdb_readoptions_t* options); |
| 23 | +void rocksdb_iter_destroy(rocksdb_iterator_t* iter); |
| 24 | +void rocksdb_iter_seek_to_first(rocksdb_iterator_t* iter); |
| 25 | +unsigned char rocksdb_iter_valid(const rocksdb_iterator_t* iter); |
| 26 | +void rocksdb_iter_next(rocksdb_iterator_t* iter); |
| 27 | +const char* rocksdb_iter_key(const rocksdb_iterator_t* iter, size_t* klen); |
| 28 | +const char* rocksdb_iter_value(const rocksdb_iterator_t* iter, size_t* vlen); |
| 29 | +""") |
| 30 | + |
| 31 | +# Load the library |
| 32 | +rocksdb = ffi.dlopen("librocksdb.so") |
| 33 | + |
| 34 | +@contextmanager |
| 35 | +def rocksdb_options(create_if_missing=False): |
| 36 | + opts = rocksdb.rocksdb_options_create() |
| 37 | + rocksdb.rocksdb_options_set_create_if_missing(opts, int(create_if_missing)) |
| 38 | + try: |
| 39 | + yield opts |
| 40 | + finally: |
| 41 | + rocksdb.rocksdb_options_destroy(opts) |
| 42 | + |
| 43 | +@contextmanager |
| 44 | +def open_db(path, options): |
| 45 | + err_ptr = ffi.new("char**") |
| 46 | + db = rocksdb.rocksdb_open(options, path.encode('utf-8'), err_ptr) |
| 47 | + if err_ptr[0] != ffi.NULL: |
| 48 | + raise RuntimeError("Open error: " + ffi.string(err_ptr[0]).decode()) |
| 49 | + try: |
| 50 | + yield db |
| 51 | + finally: |
| 52 | + rocksdb.rocksdb_close(db) |
| 53 | + |
| 54 | +def read_iter(db): |
| 55 | + """ |
| 56 | + Generator that yields (key, value) pairs from a RocksDB database. |
| 57 | +
|
| 58 | + Args: |
| 59 | + db (rocksdb_t*): A RocksDB database handle. |
| 60 | +
|
| 61 | + Yields: |
| 62 | + tuple[bytes, bytes]: The (key, value) pairs from the database. |
| 63 | + """ |
| 64 | + ropts = rocksdb.rocksdb_readoptions_create() |
| 65 | + it = rocksdb.rocksdb_create_iterator(db, ropts) |
| 66 | + try: |
| 67 | + rocksdb.rocksdb_iter_seek_to_first(it) |
| 68 | + while rocksdb.rocksdb_iter_valid(it): |
| 69 | + klen = ffi.new("size_t*") |
| 70 | + vlen = ffi.new("size_t*") |
| 71 | + key_ptr = rocksdb.rocksdb_iter_key(it, klen) |
| 72 | + val_ptr = rocksdb.rocksdb_iter_value(it, vlen) |
| 73 | + yield ( |
| 74 | + bytes(ffi.buffer(key_ptr, klen[0])), |
| 75 | + bytes(ffi.buffer(val_ptr, vlen[0])), |
| 76 | + ) |
| 77 | + rocksdb.rocksdb_iter_next(it) |
| 78 | + finally: |
| 79 | + rocksdb.rocksdb_iter_destroy(it) |
| 80 | + rocksdb.rocksdb_readoptions_destroy(ropts) |
| 81 | + |
| 82 | +def test(path, rounds): |
| 83 | + """ |
| 84 | + Iterate over a RocksDB database and print key-value pairs in hexadecimal. |
| 85 | +
|
| 86 | + Args: |
| 87 | + path (str): Path to the RocksDB database. |
| 88 | + rounds (int): Number of key-value pairs to read from the start of the database. |
| 89 | +
|
| 90 | + Behavior: |
| 91 | + - Opens the database in read-only mode (does not create a new DB). |
| 92 | + - Uses a RocksDB iterator to traverse from the first key. |
| 93 | + - Prints each key-value pair as hexadecimal strings. |
| 94 | + - Stops early if the iterator reaches the end of the DB before 'rounds' entries. |
| 95 | + """ |
| 96 | + with rocksdb_options(create_if_missing=False) as opts, open_db(path, opts) as db: |
| 97 | + for i, (key, val) in enumerate(read_iter(db)): |
| 98 | + print(f"Found KV-pair: {key.hex()} -> {val.hex()}") |
| 99 | + if i + 1 >= rounds: |
| 100 | + break |
0 commit comments