Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
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 Crawler
Commits
7b3a8e77
Commit
7b3a8e77
authored
2 years ago
by
Henrik tom Wörden
Browse files
Options
Downloads
Plain Diff
Merge branch 'f-hashable' into 'dev'
Fix hash function See merge request
!59
parents
c50938ad
17844119
No related branches found
No related tags found
2 merge requests
!71
REL: RElease v0.2.0
,
!59
Fix hash function
Pipeline
#29835
passed
2 years ago
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
3
Pipelines
10
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+1
-1
1 addition, 1 deletion
CHANGELOG.md
src/caoscrawler/identified_cache.py
+44
-22
44 additions, 22 deletions
src/caoscrawler/identified_cache.py
unittests/test_identified_cache.py
+4
-0
4 additions, 0 deletions
unittests/test_identified_cache.py
with
49 additions
and
23 deletions
CHANGELOG.md
+
1
−
1
View file @
7b3a8e77
...
@@ -16,9 +16,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
...
@@ -16,9 +16,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed ###
### Removed ###
### Fixed ###
### Fixed ###
*
FIX: https://gitlab.com/caosdb/caosdb-crawler/-/issues/31 Identified cache: Hash is the same for Records without IDs
*
FIX: https://gitlab.com/caosdb/caosdb-crawler/-/issues/30
*
FIX: https://gitlab.com/caosdb/caosdb-crawler/-/issues/30
### Security ###
### Security ###
## [0.1.0] - 2022-10-11
## [0.1.0] - 2022-10-11
...
...
This diff is collapsed.
Click to expand it.
src/caoscrawler/identified_cache.py
+
44
−
22
View file @
7b3a8e77
...
@@ -23,13 +23,52 @@
...
@@ -23,13 +23,52 @@
# ** end header
# ** end header
#
#
"""
"""
stores identified records and is able to detect duplicates
This module is a cache for Records where we checked the existence in a remote server using
identifiables. If the Record was found, this means that we identified the corresponding Record
in the remote server and the ID of the local object can be set.
To prevent querying the server again and again for the same objects, this cache allows storing
Records that were found on a remote server and those that were not (typically in separate caches).
The look up in the cache is done using a hash of a string representation.
TODO: We need a general review:
- How are entities identified with each other?
- What happens if the identification fails?
Checkout how this was done in the old crawler.
"""
"""
import
caosdb
as
db
import
caosdb
as
db
from
hashlib
import
sha256
from
hashlib
import
sha256
from
datetime
import
datetime
def
_value_representation
(
value
):
"""
returns the string representation of property values to be used in the hash function
"""
# TODO: (for review)
# This expansion of the hash function was introduced recently
# to allow the special case of Files as values of properties.
# We need to review the completeness of all the cases here, as the cache
# is crucial for correct identification of insertion and updates.
if
value
is
None
:
return
"
None
"
elif
isinstance
(
value
,
db
.
File
):
return
str
(
value
.
path
)
elif
isinstance
(
value
,
db
.
Entity
):
if
value
.
id
is
not
None
:
return
str
(
value
.
id
)
else
:
return
"
PyID=
"
+
str
(
id
(
value
))
elif
isinstance
(
value
,
list
):
return
"
[
"
+
"
,
"
.
join
([
_value_representation
(
el
)
for
el
in
value
])
+
"
]
"
elif
(
isinstance
(
value
,
str
)
or
isinstance
(
value
,
int
)
or
isinstance
(
value
,
float
)
or
isinstance
(
value
,
datetime
)):
return
str
(
value
)
else
:
raise
ValueError
(
f
"
Unknown datatype of the value:
{
value
}
"
)
def
_create_hashable_string
(
identifiable
:
db
.
Record
):
def
_create_hashable_string
(
identifiable
:
db
.
Record
):
...
@@ -46,28 +85,11 @@ def _create_hashable_string(identifiable: db.Record):
...
@@ -46,28 +85,11 @@ def _create_hashable_string(identifiable: db.Record):
# sorted([p.name for p in identifiable.parents])
# sorted([p.name for p in identifiable.parents])
raise
RuntimeError
(
"
Cache entry can only be generated for entities with 1 parent.
"
)
raise
RuntimeError
(
"
Cache entry can only be generated for entities with 1 parent.
"
)
rec_string
=
"
P<{}>N<{}>
"
.
format
(
identifiable
.
parents
[
0
].
name
,
identifiable
.
name
)
rec_string
=
"
P<{}>N<{}>
"
.
format
(
identifiable
.
parents
[
0
].
name
,
identifiable
.
name
)
# TODO this structure neglects Properties if multiple exist for the same name
for
pname
in
sorted
([
p
.
name
for
p
in
identifiable
.
properties
]):
for
pname
in
sorted
([
p
.
name
for
p
in
identifiable
.
properties
]):
value
=
str
(
identifiable
.
get_property
(
pname
).
value
)
rec_string
+=
(
"
{}:
"
.
format
(
pname
)
+
# TODO: (for review)
_value_representation
(
identifiable
.
get_property
(
pname
).
value
))
# This expansion of the hash function was introduced recently
# to allow the special case of Files as values of properties.
# We need to review the completeness of all the cases here, as the cache
# is crucial for correct identification of insertion and updates.
if
isinstance
(
identifiable
.
get_property
(
pname
).
value
,
db
.
File
):
value
=
str
(
identifiable
.
get_property
(
pname
).
value
.
path
)
elif
isinstance
(
identifiable
.
get_property
(
pname
).
value
,
db
.
Entity
):
value
=
str
(
identifiable
.
get_property
(
pname
).
value
.
id
)
elif
isinstance
(
identifiable
.
get_property
(
pname
).
value
,
list
):
tmplist
=
[]
for
val
in
identifiable
.
get_property
(
pname
).
value
:
if
isinstance
(
val
,
db
.
Entity
):
tmplist
.
append
(
val
.
id
)
else
:
tmplist
.
append
(
val
)
value
=
str
(
tmplist
)
rec_string
+=
"
{}:
"
.
format
(
pname
)
+
value
return
rec_string
return
rec_string
...
...
This diff is collapsed.
Click to expand it.
unittests/test_identified_cache.py
+
4
−
0
View file @
7b3a8e77
...
@@ -53,6 +53,10 @@ def test_create_hash():
...
@@ -53,6 +53,10 @@ def test_create_hash():
db
.
Record
(
"
A
"
)
db
.
Record
(
"
A
"
)
.
add_parent
(
"
B
"
)
.
add_parent
(
"
B
"
)
.
add_property
(
'
a
'
,
[
db
.
Record
(
id
=
12
),
11
]))
==
"
P<B>N<A>a:[12, 11]
"
)
.
add_property
(
'
a
'
,
[
db
.
Record
(
id
=
12
),
11
]))
==
"
P<B>N<A>a:[12, 11]
"
)
assert
(
_create_hashable_string
(
db
.
Record
().
add_parent
(
"
B
"
).
add_property
(
'
a
'
,
[
db
.
Record
()]))
!=
_create_hashable_string
(
db
.
Record
().
add_parent
(
"
B
"
).
add_property
(
'
a
'
,
[
db
.
Record
()])))
def
test_IdentifiedCache
():
def
test_IdentifiedCache
():
...
...
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