``class KeyValueStore`` maintains access to the raw data stored in Snapshots and various other Database-related structures.
| 28 | |
| 29 | |
| 30 | class KeyValueStore: |
| 31 | """ |
| 32 | ``class KeyValueStore`` maintains access to the raw data stored in Snapshots and various |
| 33 | other Database-related structures. |
| 34 | """ |
| 35 | def __init__(self, buffer: Optional[databuffer.DataBuffer] = None, handle=None): |
| 36 | if handle is not None: |
| 37 | self.handle = core.handle_of_type(handle, core.BNKeyValueStore) |
| 38 | else: |
| 39 | if buffer is None: |
| 40 | _handle = core.BNCreateKeyValueStore() |
| 41 | else: |
| 42 | _handle = core.BNCreateKeyValueStoreFromDataBuffer(buffer.handle) |
| 43 | assert _handle is not None |
| 44 | self.handle = _handle |
| 45 | |
| 46 | def __del__(self): |
| 47 | core.BNFreeKeyValueStore(self.handle) |
| 48 | |
| 49 | def __getitem__(self, item: str) -> databuffer.DataBuffer: |
| 50 | return self.get_value(item) |
| 51 | |
| 52 | def __setitem__(self, key: str, value: databuffer.DataBuffer): |
| 53 | return self.set_value(key, value) |
| 54 | |
| 55 | @property |
| 56 | def keys(self): |
| 57 | """Get a list of all keys stored in the kvs (read-only)""" |
| 58 | count = ctypes.c_ulonglong(0) |
| 59 | value = core.BNGetKeyValueStoreKeys(self.handle, count) |
| 60 | assert value is not None |
| 61 | |
| 62 | result = [] |
| 63 | try: |
| 64 | for i in range(0, count.value): |
| 65 | result.append(value[i]) |
| 66 | return result |
| 67 | finally: |
| 68 | core.BNFreeStringList(value, count) |
| 69 | |
| 70 | def get_value(self, key: str) -> databuffer.DataBuffer: |
| 71 | """Get the value for a single key""" |
| 72 | handle = core.BNGetKeyValueStoreBuffer(self.handle, key) |
| 73 | assert handle is not None |
| 74 | return databuffer.DataBuffer(handle=handle) |
| 75 | |
| 76 | def set_value(self, key: str, value: databuffer.DataBuffer): |
| 77 | """Set the value for a single key""" |
| 78 | core.BNSetKeyValueStoreBuffer(self.handle, key, value.handle) |
| 79 | |
| 80 | @property |
| 81 | def serialized_data(self) -> databuffer.DataBuffer: |
| 82 | """Get the stored representation of the kvs (read-only)""" |
| 83 | handle = core.BNGetKeyValueStoreSerializedData(self.handle) |
| 84 | assert handle is not None |
| 85 | return databuffer.DataBuffer(handle=handle) |
| 86 | |
| 87 | def begin_namespace(self, name: str): |
no outgoing calls
no test coverage detected