Skip to content

Commit

Permalink
Add record cache to base record model
Browse files Browse the repository at this point in the history
  • Loading branch information
bennybp committed Jan 19, 2024
1 parent edc07fb commit ab3e05c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
6 changes: 4 additions & 2 deletions qcportal/qcportal/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ def get_dataset_record(self, entry_name: str, specification_name: str):
record = decompress_from_cache(record_data[1], self._record_type)

if not self.read_only:
record._del_tasks.append(functools.partial(self.writeback_record, record_data[0])) # give it the uid
record._record_cache = self
record._record_cache_uid = record_data[0]

return record

Expand All @@ -395,7 +396,8 @@ def get_dataset_records(self, entry_names: Iterable[str], specification_names: I
record = decompress_from_cache(compressed_record, self._record_type)

if not self.read_only:
record._del_tasks.append(functools.partial(self.writeback_record, uid)) # give it the uid
record._record_cache = self
record._record_cache_uid = uid

all_records.append((ename, sname, record))

Expand Down
15 changes: 8 additions & 7 deletions qcportal/qcportal/record_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
QueryModelBase,
QueryIteratorBase,
)

from qcportal.cache import RecordCache
from qcportal.compression import CompressionEnum, decompress, get_compressed_ext


Expand Down Expand Up @@ -392,8 +394,10 @@ class Config:
# A dictionary of all subclasses (calculation types) to actual class type
_all_subclasses: ClassVar[Dict[str, Type[BaseRecord]]] = {}

# Stuff to run when being deleted
_del_tasks: List[Callable] = PrivateAttr([])
# Local record cache we can use for child records
# This record may also be part of the cache
_record_cache: Optional[RecordCache] = PrivateAttr(None)
_record_cache_uid: Optional[int] = PrivateAttr(None)

def __init__(self, client=None, **kwargs):
BaseModel.__init__(self, **kwargs)
Expand All @@ -416,11 +420,8 @@ def __init_subclass__(cls):
cls._all_subclasses[record_type] = cls

def __del__(self):
# _del_tasks may not exist if this is being called after
# a validation error or something
if hasattr(self, "_del_tasks"):
for f in reversed(self._del_tasks):
f(self)
if self._record_cache is not None and self._record_cache_uid is not None and not self._record_cache.read_only:
self._record_cache.writeback_record(self._record_cache_uid, self)

s = super()
if hasattr(s, "__del__"):
Expand Down

0 comments on commit ab3e05c

Please sign in to comment.