Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-advanced-user-tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-advanced-user-tools
Commits
c53e35d3
Commit
c53e35d3
authored
4 years ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
ENH: refactored cache classes to inherit from an abstract base class
parent
e14f1784
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!59
FIX: if multiple updates for one entity exist, the retrieve would result in an...
,
!46
F cache version
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/caosadvancedtools/cache.py
+46
-18
46 additions, 18 deletions
src/caosadvancedtools/cache.py
with
46 additions
and
18 deletions
src/caosadvancedtools/cache.py
+
46
−
18
View file @
c53e35d3
...
...
@@ -30,6 +30,7 @@ import sqlite3
from
hashlib
import
sha256
from
lxml
import
etree
from
abc
import
ABC
,
abstractmethod
import
caosdb
as
db
...
...
@@ -50,16 +51,25 @@ def get_pretty_xml(cont):
return
etree
.
tounicode
(
cont
.
to_xml
(
local_serialization
=
True
),
pretty_print
=
True
)
# Increase this, when changes to the cache tables are made:
CACHE_SCHEMA_VERSION
=
2
class
Cache
(
object
):
"""
stores identifiables (as a hash of xml) and their respective ID.
class
AbstractCache
(
ABC
):
@abstractmethod
def
get_cache_schema_version
(
self
):
"""
A method that has to be overloaded that sets the version of the
SQLITE database schema. The schema is saved in table version column schema.
This allows to retrieve the Record corresponding to an indentifiable
without querying.
"""
Increase this variable, when changes to the cache tables are made.
"""
pass
@abstractmethod
def
create_cache
(
self
):
"""
Provide an overloaded function here that creates the cache in
the most recent version.
"""
pass
def
__init__
(
self
,
db_file
=
None
,
force_creation
=
False
):
"""
...
...
@@ -96,9 +106,9 @@ class Cache(object):
except
sqlite3
.
OperationalError
:
current_schema
=
1
if
current_schema
>
CACHE_SCHEMA_VERSION
:
if
current_schema
>
self
.
get_cache_schema_version
()
:
raise
RuntimeError
(
"
Cache is corrupt or was created with a future version of this program.
"
)
elif
current_schema
<
CACHE_SCHEMA_VERSION
:
elif
current_schema
<
self
.
get_cache_schema_version
()
:
raise
RuntimeError
(
"
Cache version too old.
"
)
def
get_cache_version
(
self
):
...
...
@@ -116,6 +126,22 @@ class Cache(object):
return
version_row
[
0
][
0
]
finally
:
conn
.
close
()
# TODO: A better name would be IdentifiablesCache
class
Cache
(
AbstractCache
):
"""
stores identifiables (as a hash of xml) and their respective ID.
This allows to retrieve the Record corresponding to an indentifiable
without querying.
"""
def
get_cache_schema_version
(
self
):
return
2
def
__init__
(
self
,
db_file
=
None
,
force_creation
=
False
):
super
().
__init__
(
db_file
,
force_creation
)
def
create_cache
(
self
):
"""
...
...
@@ -131,7 +157,7 @@ class Cache(object):
'''
CREATE TABLE identifiables (digest TEXT PRIMARY KEY, caosdb_id INTEGER, caosdb_version TEXT)
'''
)
c
.
execute
(
'''
CREATE TABLE version (schema INTEGER)
'''
)
c
.
execute
(
"
INSERT INTO version VALUES (?)
"
,
(
CACHE_SCHEMA_VERSION
,))
c
.
execute
(
"
INSERT INTO version VALUES (?)
"
,
(
self
.
get_cache_schema_version
()
,))
conn
.
commit
()
conn
.
close
()
...
...
@@ -218,7 +244,7 @@ class Cache(object):
self
.
insert
(
ehash
,
ent
.
id
,
ent
.
version
.
id
)
class
UpdateCache
(
Cache
):
class
UpdateCache
(
Abstract
Cache
):
"""
stores unauthorized updates
...
...
@@ -226,12 +252,14 @@ class UpdateCache(Cache):
be stored in this cache such that it can be authorized and done later.
"""
def
__init__
(
self
,
db_file
=
None
):
if
db_file
is
None
:
# TODO: check whether a hardcoded temp file is really wanted
# Why not crawler_update_cache.db in current working directory?
db_file
=
"
/tmp/crawler_update_cache.db
"
super
().
__init__
(
db_file
=
db_file
)
def
get_cache_schema_version
(
self
):
return
1
def
get_default_file_name
():
return
"
/tmp/crawler_update_cache.db
"
def
__init__
(
self
,
db_file
=
None
,
force_creation
=
False
):
super
().
__init__
(
db_file
,
force_creation
)
@staticmethod
def
get_previous_version
(
cont
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment