Skip to content

Commit

Permalink
GH-5016 introduce cache for common vocabulary in LMDB Store
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed Jun 5, 2024
1 parent a7bd86a commit e66a4e1
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.StampedLock;
Expand Down Expand Up @@ -845,6 +846,8 @@ public long getId(Value value) throws IOException {
return getId(value, false);
}

private final ConcurrentHashMap<Value, Long> commonVocabulary = new ConcurrentHashMap<>();

/**
* Gets the ID for the specified value.
*
Expand Down Expand Up @@ -872,6 +875,9 @@ public long getId(Value value, boolean create) throws IOException {
try {
// Check cache
Long cachedID = valueIDCache.get(value);
if (cachedID == null) {
cachedID = commonVocabulary.get(value);
}

if (cachedID != null) {
long id = cachedID;
Expand Down Expand Up @@ -903,6 +909,11 @@ public long getId(Value value, boolean create) throws IOException {
// Store id in cache
LmdbValue nv = getLmdbValue(value);
nv.setInternalID(id, revision);

if (nv.isIRI() && isCommonVocabulary(((IRI) nv))) {
commonVocabulary.put(value, id);
}

valueIDCache.put(nv, id);
}
}
Expand All @@ -916,6 +927,14 @@ public long getId(Value value, boolean create) throws IOException {
return LmdbValue.UNKNOWN_ID;
}

private static boolean isCommonVocabulary(IRI nv) {
String string = nv.toString();
return string.startsWith("http://www.w3.org/") ||
string.startsWith("http://purl.org/") ||
string.startsWith("http://publications.europa.eu/resource/authority") ||
string.startsWith("http://xmlns.com/");
}

public void gcIds(Collection<Long> ids, Collection<Long> nextIds) throws IOException {
if (!ids.isEmpty()) {
// wrap into read txn as resizeMap expects an active surrounding read txn
Expand Down

0 comments on commit e66a4e1

Please sign in to comment.