Skip to content
Snippets Groups Projects
Commit 19ecd7b9 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

Merge branch 'f-cache-version-review' into 'f-cache-version'

Suggestions for merge

See merge request caosdb/caosdb-advanced-user-tools!71
parents 3daaa1ce 93106b9e
Branches
Tags
2 merge requests!59FIX: if multiple updates for one entity exist, the retrieve would result in an...,!46F cache version
...@@ -27,12 +27,11 @@ ...@@ -27,12 +27,11 @@
# server side? # server side?
import os import os
import sqlite3 import sqlite3
from hashlib import sha256
from lxml import etree
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from hashlib import sha256
import caosdb as db import caosdb as db
from lxml import etree
def put_in_container(stuff): def put_in_container(stuff):
...@@ -85,12 +84,13 @@ class AbstractCache(ABC): ...@@ -85,12 +84,13 @@ class AbstractCache(ABC):
if force_creation is set to True, the file will be created if force_creation is set to True, the file will be created
regardless of a file at the same path already exists. regardless of a file at the same path already exists.
""" """
if db_file is None: if db_file is None:
self.db_file = self.get_default_file_name() self.db_file = self.get_default_file_name()
else: else:
self.db_file = db_file self.db_file = db_file
if force_creation: if not os.path.exists(self.db_file) or force_creation:
self.create_cache() self.create_cache()
else: else:
self.check_cache() self.check_cache()
...@@ -104,10 +104,6 @@ class AbstractCache(ABC): ...@@ -104,10 +104,6 @@ class AbstractCache(ABC):
If it exists, but the schema is outdated, an exception will be raised. If it exists, but the schema is outdated, an exception will be raised.
""" """
print(self.db_file)
if not os.path.exists(self.db_file):
self.create_cache()
else:
try: try:
current_schema = self.get_cache_version() current_schema = self.get_cache_version()
except sqlite3.OperationalError: except sqlite3.OperationalError:
...@@ -128,8 +124,10 @@ class AbstractCache(ABC): ...@@ -128,8 +124,10 @@ class AbstractCache(ABC):
c = conn.cursor() c = conn.cursor()
c.execute("SELECT schema FROM version") c.execute("SELECT schema FROM version")
version_row = c.fetchall() version_row = c.fetchall()
if len(version_row) != 1: if len(version_row) != 1:
raise RuntimeError("Cache version table broken.") raise RuntimeError("Cache version table broken.")
return version_row[0][0] return version_row[0][0]
finally: finally:
conn.close() conn.close()
...@@ -144,12 +142,15 @@ class AbstractCache(ABC): ...@@ -144,12 +142,15 @@ class AbstractCache(ABC):
""" """
conn = sqlite3.connect(self.db_file) conn = sqlite3.connect(self.db_file)
c = conn.cursor() c = conn.cursor()
for sql in commands: for sql in commands:
c.execute(*sql) c.execute(*sql)
if fetchall: if fetchall:
results = c.fetchall() results = c.fetchall()
conn.commit() conn.commit()
conn.close() conn.close()
if fetchall: if fetchall:
return results return results
...@@ -250,9 +251,11 @@ class Cache(AbstractCache): ...@@ -250,9 +251,11 @@ class Cache(AbstractCache):
""" """
# Check whether all entities have IDs and versions: # Check whether all entities have IDs and versions:
for ent in entities: for ent in entities:
if ent.id is None: if ent.id is None:
raise RuntimeError("Entity has no ID.") raise RuntimeError("Entity has no ID.")
if ent.version is None or ent.version.id is None: if ent.version is None or ent.version.id is None:
raise RuntimeError("Entity has no version ID.") raise RuntimeError("Entity has no version ID.")
...@@ -264,8 +267,7 @@ class Cache(AbstractCache): ...@@ -264,8 +267,7 @@ class Cache(AbstractCache):
""" """
Runs through all entities stored in the cache and checks Runs through all entities stored in the cache and checks
whether the version still matches the most recent version. whether the version still matches the most recent version.
Non-matching entities will be removed Non-matching entities will be removed from the cache.
from the cache.
entities: When set to a db.Container or a list of Entities entities: When set to a db.Container or a list of Entities
the IDs from the cache will not be retrieved from the CaosDB database, the IDs from the cache will not be retrieved from the CaosDB database,
...@@ -281,20 +283,17 @@ class Cache(AbstractCache): ...@@ -281,20 +283,17 @@ class Cache(AbstractCache):
"SELECT caosdb_id, caosdb_version FROM identifiables", ())], True) "SELECT caosdb_id, caosdb_version FROM identifiables", ())], True)
if entities is None: if entities is None:
c = db.Container() # TODO this might become a problem. If many entities are cached,
else: # then all of them are retrieved here...
c = entities entities = db.Container()
v = dict() entities.extend([db.Entity(id=c_id) for c_id, _ in res])
for c_id, c_version in res: entities.retrieve()
if entities is None:
c.append(db.Entity(id=c_id))
v[c_id] = c_version
if entities is None:
c.retrieve()
v = {c_id: c_version for c_id, c_version in res}
invalidate_list = [] invalidate_list = []
for ent in c:
for ent in entities:
if ent.version.id != v[ent.id]: if ent.version.id != v[ent.id]:
invalidate_list.append(ent.id) invalidate_list.append(ent.id)
......
...@@ -190,8 +190,6 @@ class InvalidationTest(unittest.TestCase): ...@@ -190,8 +190,6 @@ class InvalidationTest(unittest.TestCase):
def test_invalid(self): def test_invalid(self):
assert len(self.cache.validate_cache()) == 0
ent = db.Record() ent = db.Record()
ent2 = db.Record() ent2 = db.Record()
ent2.add_parent(name="Experiment") ent2.add_parent(name="Experiment")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment