From 5a7aa6c11dcb4bf1a7a91e7bc25bb161f9a8db19 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Mon, 23 Jun 2025 15:00:11 +0300 Subject: Add function for queueing edits to sql and lmdb. Signed-off-by: Munyoki Kilyungi --- gn3/case_attributes.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'gn3/case_attributes.py') diff --git a/gn3/case_attributes.py b/gn3/case_attributes.py index 099b9cc..91d8b7f 100644 --- a/gn3/case_attributes.py +++ b/gn3/case_attributes.py @@ -4,6 +4,8 @@ import csv import json import uuid import tempfile +import lmdb +import pickle from dataclasses import dataclass from typing import Union from enum import Enum, auto @@ -111,6 +113,47 @@ def required_access( raise AuthorisationError( f"User does not have the privileges {access_levels}") + +def __queue_edits__(cursor, directory: Path, edit: CaseAttributeEdit) -> set: + """Queues a case attribute edit for review by inserting it into + the audit table and storing its review ID in an LMDB database. + + Args: + cursor: A database cursor for executing SQL queries. + directory (Path): The base directory path for the LMDB database. + edit (CaseAttributeEdit): A dataclass containing the edit details, including + inbredset_id, user_id, and diff (the changes to be reviewed). + + Returns: + set: A set of review IDs, including the newly added edit's ID, stored in the + LMDB database for the specified inbredset_id. + + Notes: + - Inserts the edit into the `caseattributes_audit` table with status set to + `EditStatus.review`. + - Uses LMDB to store review IDs under the key b"review" for the given + inbredset_id. + - The LMDB map_size is set to 8 MB. + - The JSON diff data is serialized using a custom `CAJSONEncoder`. + """ + cursor.execute( + "INSERT INTO " + "caseattributes_audit(status, editor, json_diff_data) " + "VALUES (%s, %s, %s) " + "ON DUPLICATE KEY UPDATE status=%s", + (str(EditStatus.review), + edit.user_id, json.dumps(edit.diff, cls=CAJSONEncoder), str(EditStatus.review),)) + env = lmdb.open(f"{directory}/case-attributes/{edit.inbredset_id}", + map_size=8_000_000) # 1 MB + with env.begin() as txn: + review_ids = set() + if reviews := txn.get(b"review"): + review_ids = pickle.loads(reviews) + review_ids.add(cursor.lastrowid) + txn.put(b"review", pickle.dumps(review_ids)) + return review_ids + + def __inbredset_group__(conn, inbredset_id): """Return InbredSet group's top-level details.""" with conn.cursor(cursorclass=DictCursor) as cursor: -- cgit 1.4.1