diff options
author | Munyoki Kilyungi | 2023-02-06 15:05:56 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-02-06 18:14:19 +0300 |
commit | 3cc5e7b57b0ca9bd5143d746022cb69349023a68 (patch) | |
tree | 8bf620c1f7e7baf631a6c64462c873fd36b8bf1a | |
parent | faf223207015077d0afc36c7cc7cf1fc29976c7f (diff) | |
download | genenetwork3-3cc5e7b57b0ca9bd5143d746022cb69349023a68.tar.gz |
Get the N-th matrix given N, an index value
* gn3/db/matrix.py (get_nth_matrix): New function.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r-- | gn3/db/matrix.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/gn3/db/matrix.py b/gn3/db/matrix.py index 71e7ca0..0fc4bf4 100644 --- a/gn3/db/matrix.py +++ b/gn3/db/matrix.py @@ -26,6 +26,31 @@ def get_total_versions(db_path: str) -> int: return 0 return int(len(versions_hash) / BLOB_HASH_DIGEST) + +def get_nth_matrix(index: int, db_path: str) -> Optional[Matrix]: + """Get the NTH matrix from the DB_PATH. The most recent matrix is 0.""" + env = lmdb.open(db_path) + with env.begin(write=False) as txn: + versions_hash = txn.get(b"versions") + if (index * 32) + 32 > len(versions_hash): + return None + _hash, nrows = versions_hash[index * 32: (index * 32) + 32], 0 + row_pointers = txn.get(_hash + b":row-pointers") + if _hash: + (nrows,) = struct.unpack("<Q", txn.get(_hash + b":nrows")) + if row_pointers: + return Matrix( + data=[ + json.loads(txn.get(row_pointers[i: i + 32]).decode()) + for i in range(0, nrows * 32, 32) + ], + metadata=json.loads( + txn.get(_hash + b":metadata").rstrip(b"\x00").decode() + ), + ) + return None + + 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)""" |