Skip to content

Commit 835cff1

Browse files
author
Ralf Grubenmann
committed
Patches pyld to prevent contexts from overwhelming its cache
1 parent 901c67c commit 835cff1

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

renku/core/commands/checks/validate_shacl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
# limitations under the License.
1818
"""Check KG structure using SHACL."""
1919
import yaml
20-
from pyld import jsonld as ld
2120
from rdflib.namespace import Namespace
2221
from rdflib.term import BNode
2322

2423
from renku.core.commands.echo import WARNING
24+
from renku.core.compat import pyld
2525
from renku.core.models.jsonld import NoDatesSafeLoader
2626
from renku.core.utils.shacl import validate_graph
2727

@@ -100,7 +100,7 @@ def check_shacl_structure(path):
100100
with path.open(mode='r') as fp:
101101
source = yaml.load(fp, Loader=NoDatesSafeLoader) or {}
102102

103-
rdf = ld.to_rdf(
103+
rdf = pyld.jsonld.to_rdf(
104104
source,
105105
options={
106106
'format': 'application/n-quads',

renku/core/commands/format/graph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def _jsonld(graph, format, *args, **kwargs):
3939
"""Return formatted graph in JSON-LD ``format`` function."""
4040
import json
4141

42-
from pyld import jsonld
42+
from renku.core.compat import pyld
4343
from renku.core.models.jsonld import asjsonld
4444

45-
output = getattr(jsonld, format)([
45+
output = getattr(pyld.jsonld, format)([
4646
asjsonld(action) for action in graph.activities.values()
4747
])
4848
return json.dumps(output, indent=2)

renku/core/compat.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
"""Compatibility layer for different Python versions."""
1919

2020
import contextlib
21+
import json
2122
import os
2223
import sys
24+
from collections import deque
2325
from pathlib import Path
2426

27+
import pyld
28+
2529
if sys.version_info < (3, 6):
2630
original_resolve = Path.resolve
2731

@@ -63,4 +67,36 @@ def __exit__(self, *excinfo):
6367
except NameError: # pragma: no cover
6468
FileNotFoundError = IOError
6569

66-
__all__ = ('FileNotFoundError', 'Path', 'contextlib')
70+
71+
class ActiveContextCache(object):
72+
"""Pyld context cache without issue of missing contexts."""
73+
74+
def __init__(self, size=100):
75+
self.order = deque()
76+
self.cache = {}
77+
self.size = size
78+
79+
def get(self, active_ctx, local_ctx):
80+
key1 = json.dumps(active_ctx)
81+
key2 = json.dumps(local_ctx)
82+
return self.cache.get(key1, {}).get(key2)
83+
84+
def set(self, active_ctx, local_ctx, result):
85+
if len(self.order) == self.size:
86+
entry = self.order.popleft()
87+
if sum(
88+
e['activeCtx'] == entry['activeCtx'] and
89+
e['localCtx'] == entry['localCtx'] for e in self.order
90+
) == 0:
91+
# only delete from cache if it doesn't exist in context deque
92+
print("this totally works")
93+
del self.cache[entry['activeCtx']][entry['localCtx']]
94+
key1 = json.dumps(active_ctx)
95+
key2 = json.dumps(local_ctx)
96+
self.order.append({'activeCtx': key1, 'localCtx': key2})
97+
self.cache.setdefault(key1, {})[key2] = json.loads(json.dumps(result))
98+
99+
100+
pyld._cache = {'activeCtx': ActiveContextCache()}
101+
102+
__all__ = ('FileNotFoundError', 'Path', 'contextlib', 'pyld')

renku/core/models/jsonld.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
from attr._compat import iteritems
3131
from attr._funcs import has
3232
from attr._make import Factory, fields
33-
from pyld import jsonld as ld
3433

34+
from renku.core.compat import pyld
3535
from renku.core.models.locals import ReferenceMixin, with_reference
3636
from renku.core.models.migrations import JSONLD_MIGRATIONS
3737

@@ -149,7 +149,7 @@ def wrap(cls):
149149

150150
# Register class for given JSON-LD @type
151151
try:
152-
type_ = ld.expand({
152+
type_ = pyld.jsonld.expand({
153153
'@type': jsonld_cls._jsonld_type,
154154
'@context': context
155155
})[0]['@type']
@@ -473,10 +473,10 @@ def from_jsonld(
473473

474474
if cls._jsonld_translate:
475475
# perform the translation
476-
data = ld.compact(data, cls._jsonld_translate)
476+
data = pyld.jsonld.compact(data, cls._jsonld_translate)
477477
# compact using the class json-ld context
478478
data.pop('@context', None)
479-
data = ld.compact(data, cls._jsonld_context)
479+
data = pyld.jsonld.compact(data, cls._jsonld_context)
480480

481481
data.setdefault('@context', cls._jsonld_context)
482482

@@ -504,7 +504,7 @@ def from_jsonld(
504504
data['@context'] = {'@base': data['@context']}
505505
data['@context'].update(cls._jsonld_context)
506506
try:
507-
compacted = ld.compact(data, cls._jsonld_context)
507+
compacted = pyld.jsonld.compact(data, cls._jsonld_context)
508508
except Exception:
509509
compacted = data
510510
else:

tests/cli/test_log.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ def test_dataset_log_strict(tmpdir, runner, project, client, format):
6262
result = runner.invoke(
6363
cli, ['log', '--strict', '--format={}'.format(format)]
6464
)
65+
6566
assert 0 == result.exit_code, result.output
6667
assert all(p in result.output for p in paths)

tests/core/models/test_shacl_schema.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
# limitations under the License.
1818
"""test KG against SHACL shape."""
1919

20-
from pyld import jsonld as ld
21-
2220
from renku.cli import cli
23-
from renku.core.compat import Path
21+
from renku.core.compat import Path, pyld
2422
from renku.core.utils.shacl import validate_graph
2523

2624

@@ -61,7 +59,7 @@ def test_dataset_shacl(tmpdir, runner, project, client):
6159

6260
with client.with_dataset('dataset') as dataset:
6361
g = dataset.asjsonld()
64-
rdf = ld.to_rdf(
62+
rdf = pyld.jsonld.to_rdf(
6563
g,
6664
options={
6765
'format': 'application/n-quads',
@@ -94,7 +92,7 @@ def test_project_shacl(project, client):
9492
project.creator = Creator(email='johndoe@example.com', name='Johnny Doe')
9593

9694
g = project.asjsonld()
97-
rdf = ld.to_rdf(
95+
rdf = pyld.jsonld.to_rdf(
9896
g,
9997
options={
10098
'format': 'application/n-quads',

0 commit comments

Comments
 (0)