database: fix get_batch for older versions of lmdb

Some distros have older versions of py-lmdb installed
that do not implement "Cursor.getmulti()".  Add a  workaround for "get_batch()".

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-12-28 06:02:09 -05:00
parent 5c5e91cb3d
commit f7d5f11cf8
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 15 additions and 6 deletions

View File

@ -98,8 +98,9 @@ class MoonrakerDatabase:
if not db_path.is_dir():
db_path.mkdir()
self.database_path = str(db_path)
self.lmdb_env = lmdb.open(self.database_path, map_size=MAX_DB_SIZE,
max_dbs=MAX_NAMESPACES)
self.lmdb_env: lmdb.Environment = lmdb.open(
self.database_path, map_size=MAX_DB_SIZE, max_dbs=MAX_NAMESPACES
)
with self.lmdb_env.begin(write=True, buffers=True) as txn:
# lookup existing namespaces
with txn.cursor() as cursor:
@ -454,12 +455,20 @@ class MoonrakerDatabase:
) -> Dict[str, Any]:
db = self._get_db(namespace)
result: Dict[str, Any] = {}
encoded_keys: List[bytes] = [k.encode() for k in keys]
with self.lmdb_env.begin(buffers=True, db=db) as txn:
with txn.cursor() as cursor:
if hasattr(cursor, "getmulti"):
encoded_keys: List[bytes] = [k.encode() for k in keys]
vals = cursor.getmulti(encoded_keys)
result = {bytes(k).decode(): self._decode_value(v)
for k, v in vals}
result = {
bytes(k).decode(): self._decode_value(v) for k, v in vals
}
else:
for key in keys:
value = txn.get(key.encode())
if value is None:
continue
result[key] = self._decode_value(value)
return result
# *** Namespace level operations***