Skip to content

Commit 8f8f017

Browse files
committed
GH-4950 LMDB: support inlined values in value store
1 parent 65611d0 commit 8f8f017

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/ValueStore.java

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.eclipse.rdf4j.model.util.Literals;
7777
import org.eclipse.rdf4j.sail.lmdb.LmdbUtil.Transaction;
7878
import org.eclipse.rdf4j.sail.lmdb.config.LmdbStoreConfig;
79+
import org.eclipse.rdf4j.sail.lmdb.inlined.Values;
7980
import org.eclipse.rdf4j.sail.lmdb.model.LmdbBNode;
8081
import org.eclipse.rdf4j.sail.lmdb.model.LmdbIRI;
8182
import org.eclipse.rdf4j.sail.lmdb.model.LmdbLiteral;
@@ -513,6 +514,10 @@ public LmdbValue getLazyValue(long id) throws IOException {
513514
resultValue = new LmdbBNode(lazyRevision, id);
514515
break;
515516
default:
517+
if (ValueIds.isInlined(id)) {
518+
resultValue = new LmdbLiteral(lazyRevision, id);
519+
break;
520+
}
516521
throw new IOException("Unsupported value with id type: " + idType);
517522
}
518523
// Store value in cache
@@ -540,6 +545,12 @@ public LmdbValue getValue(long id) throws IOException {
540545
LmdbValue resultValue = cachedValue(cacheID);
541546

542547
if (resultValue == null) {
548+
// unpack inlined values if possible
549+
if (ValueIds.isInlined(id)) {
550+
Literal unpacked = Values.unpackLiteral(id, this);
551+
return new LmdbLiteral(revision, unpacked.getLabel(), unpacked.getDatatype(), id);
552+
}
553+
543554
// Value not in cache, fetch it from file
544555
byte[] data = getData(id);
545556

@@ -564,6 +575,13 @@ public LmdbValue getValue(long id) throws IOException {
564575
* @return <code>true</code> if value could be successfully resolved, else <code>false</code>
565576
*/
566577
public boolean resolveValue(long id, LmdbValue value) {
578+
// unpack inlined values if possible
579+
if (ValueIds.isInlined(id)) {
580+
Literal unpacked = Values.unpackLiteral(id, this);
581+
((LmdbLiteral) value).setLabel(unpacked.getLabel());
582+
((LmdbLiteral) value).setDatatype(unpacked.getDatatype());
583+
return true;
584+
}
567585
try {
568586
byte[] data = getData(id);
569587
if (data != null) {
@@ -935,34 +953,46 @@ public long getId(Value value, boolean create) throws IOException {
935953
return id;
936954
}
937955

938-
// ID not cached, search in file
939-
byte[] data = value2data(value, create);
940-
if (data == null && value instanceof Literal) {
941-
data = literal2legacy((Literal) value);
956+
long id = LmdbValue.UNKNOWN_ID;
957+
if (value instanceof Literal) {
958+
// inline value into id if possible
959+
long packedId = Values.packLiteral((Literal) value);
960+
if (packedId != 0L) {
961+
Literal unpacked = Values.unpackLiteral(id, this);
962+
if (unpacked.equals(value)) {
963+
id = packedId;
964+
}
965+
}
942966
}
943967

944-
if (data != null) {
945-
long id = findId(data, create);
946-
if (id != LmdbValue.UNKNOWN_ID) {
947-
if (isOwnValue) {
948-
// Store id in value for fast access in any consecutive calls
949-
((LmdbValue) value).setInternalID(id, revision);
950-
// Store id in cache
951-
valueIDCache.put((LmdbValue) value, id);
952-
} else {
953-
// Store id in cache
954-
LmdbValue nv = getLmdbValue(value);
955-
nv.setInternalID(id, revision);
968+
if (id == LmdbValue.UNKNOWN_ID) {
969+
// not inlined or ID not cached, search in index
970+
byte[] data = value2data(value, create);
971+
if (data == null && value instanceof Literal) {
972+
data = literal2legacy((Literal) value);
973+
}
956974

957-
if (nv.isIRI() && isCommonVocabulary(((IRI) nv))) {
958-
commonVocabulary.put(value, id);
959-
}
975+
if (data != null) {
976+
id = findId(data, create);
977+
}
978+
}
979+
980+
if (id != LmdbValue.UNKNOWN_ID) {
981+
if (isOwnValue) {
982+
// Store id in value for fast access in any consecutive calls
983+
((LmdbValue) value).setInternalID(id, revision);
984+
// Store id in cache
985+
valueIDCache.put((LmdbValue) value, id);
986+
} else {
987+
// Store id in cache
988+
LmdbValue nv = getLmdbValue(value);
989+
nv.setInternalID(id, revision);
960990

961-
valueIDCache.put(nv, id);
991+
if (nv.isIRI() && isCommonVocabulary(((IRI) nv))) {
992+
commonVocabulary.put(value, id);
962993
}
994+
valueIDCache.put(nv, id);
963995
}
964-
965-
return id;
966996
}
967997
} finally {
968998
revisionLock.unlockRead(stamp);

0 commit comments

Comments
 (0)