From faf223207015077d0afc36c7cc7cf1fc29976c7f Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Mon, 6 Feb 2023 14:40:04 +0300 Subject: Get total number of versions that a database has * gn3/db/matrix.py (get_total_versions): New function. Signed-off-by: Munyoki Kilyungi --- gn3/db/matrix.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'gn3/db') diff --git a/gn3/db/matrix.py b/gn3/db/matrix.py index d40f3c3..71e7ca0 100644 --- a/gn3/db/matrix.py +++ b/gn3/db/matrix.py @@ -17,6 +17,15 @@ class Matrix: metadata: dict +def get_total_versions(db_path: str) -> int: + """Get the total number of versions in the matrix""" + env = lmdb.open(db_path) + with env.begin(write=False) as txn: + versions_hash = txn.get(b"versions") + if not versions_hash: + return 0 + return int(len(versions_hash) / BLOB_HASH_DIGEST) + def get_current_matrix(db_path: str) -> Optional[Matrix]: """Get the most recent matrix from DB_PATH. This is functionally equivalent to get_nth_matrix(0, db_path)""" @@ -28,7 +37,6 @@ def get_current_matrix(db_path: str) -> Optional[Matrix]: nrows = 0 if matrix_hash: (nrows,) = struct.unpack("