Skip to content

Commit 2fb2c30

Browse files
committed
GH-4950 LMDB: Add config option to disable inlining of literals
1 parent a53a4ef commit 2fb2c30

5 files changed

Lines changed: 89 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* SPDX-License-Identifier: BSD-3-Clause
1010
*******************************************************************************/
1111
// Some portions generated by Codex
12+
// Some portions generated by Co-Pilot
1213
package org.eclipse.rdf4j.sail.lmdb;
1314

1415
import static org.eclipse.rdf4j.sail.lmdb.LmdbUtil.E;
@@ -232,6 +233,7 @@ class ValueStore extends AbstractValueFactory {
232233

233234
private final long valueEvictionInterval;
234235
private final boolean valueHashCacheEnabled;
236+
private final boolean inlineLiterals;
235237

236238
ValueStore(File dir, LmdbStoreConfig config) throws IOException {
237239
this(dir, new StoreProperties(dir), config);
@@ -246,6 +248,7 @@ class ValueStore extends AbstractValueFactory {
246248
this.mapSize = config.getValueDBSize();
247249
this.valueEvictionInterval = config.getValueEvictionInterval();
248250
this.valueHashCacheEnabled = config.getValueHashCacheEnabled();
251+
this.inlineLiterals = config.getInlineLiterals();
249252
open();
250253

251254
int cacheSize = nextPowerOfTwo(config.getValueCacheSize());
@@ -1083,7 +1086,7 @@ public long getId(Value value, boolean create) throws IOException {
10831086
}
10841087

10851088
long id = LmdbValue.UNKNOWN_ID;
1086-
if (value instanceof Literal) {
1089+
if (inlineLiterals && value instanceof Literal) {
10871090
// inline value into id if possible
10881091
try {
10891092
long packedId = Values.packLiteral((Literal) value);

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/config/LmdbStoreConfig.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* SPDX-License-Identifier: BSD-3-Clause
1010
*******************************************************************************/
11-
// Some portions generated by Codex
11+
// Some portions generated by Co-Pilot
1212
package org.eclipse.rdf4j.sail.lmdb.config;
1313

1414
import java.time.Duration;
@@ -89,6 +89,8 @@ public class LmdbStoreConfig extends BaseSailConfig {
8989

9090
private boolean valueHashCacheEnabled = false;
9191

92+
private boolean inlineLiterals = true;
93+
9294
/*--------------*
9395
* Constructors *
9496
*--------------*/
@@ -240,6 +242,15 @@ public LmdbStoreConfig setValueHashCacheEnabled(boolean valueHashCacheEnabled) {
240242
return this;
241243
}
242244

245+
public boolean getInlineLiterals() {
246+
return inlineLiterals;
247+
}
248+
249+
public LmdbStoreConfig setInlineLiterals(boolean inlineLiterals) {
250+
this.inlineLiterals = inlineLiterals;
251+
return this;
252+
}
253+
243254
@Override
244255
public Resource export(Model m) {
245256
Resource implNode = super.export(m);
@@ -288,6 +299,9 @@ public Resource export(Model m) {
288299
if (valueHashCacheEnabled) {
289300
m.add(implNode, LmdbStoreSchema.VALUE_HASH_CACHE_ENABLED, vf.createLiteral(true));
290301
}
302+
if (!inlineLiterals) {
303+
m.add(implNode, LmdbStoreSchema.INLINE_LITERALS, vf.createLiteral(false));
304+
}
291305
return implNode;
292306
}
293307

@@ -434,6 +448,17 @@ public void parse(Model m, Resource implNode) throws SailConfigException {
434448
+ " property, found " + lit);
435449
}
436450
});
451+
452+
Models.objectLiteral(m.getStatements(implNode, LmdbStoreSchema.INLINE_LITERALS, null))
453+
.ifPresent(lit -> {
454+
try {
455+
setInlineLiterals(lit.booleanValue());
456+
} catch (IllegalArgumentException e) {
457+
throw new SailConfigException(
458+
"Boolean value required for " + LmdbStoreSchema.INLINE_LITERALS
459+
+ " property, found " + lit);
460+
}
461+
});
437462
} catch (ModelException e) {
438463
throw new SailConfigException(e.getMessage(), e);
439464
}

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/config/LmdbStoreSchema.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* SPDX-License-Identifier: BSD-3-Clause
1010
*******************************************************************************/
11-
// Some portions generated by Codex
11+
// Some portions generated by Co-Pilot
1212
package org.eclipse.rdf4j.sail.lmdb.config;
1313

1414
import org.eclipse.rdf4j.model.IRI;
@@ -97,6 +97,11 @@ public class LmdbStoreSchema {
9797
*/
9898
public final static IRI VALUE_HASH_CACHE_ENABLED;
9999

100+
/**
101+
* <tt>http://rdf4j.org/config/sail/lmdb#inlineLiterals</tt>
102+
*/
103+
public final static IRI INLINE_LITERALS;
104+
100105
static {
101106
ValueFactory factory = SimpleValueFactory.getInstance();
102107
TRIPLE_INDEXES = factory.createIRI(NAMESPACE, "tripleIndexes");
@@ -113,5 +118,6 @@ public class LmdbStoreSchema {
113118
PAGE_CARDINALITY_ESTIMATOR = factory.createIRI(NAMESPACE, "pageCardinalityEstimator");
114119
VALUE_EVICTION_INTERVAL = factory.createIRI(NAMESPACE, "valueEvictionInterval");
115120
VALUE_HASH_CACHE_ENABLED = factory.createIRI(NAMESPACE, "valueHashCacheEnabled");
121+
INLINE_LITERALS = factory.createIRI(NAMESPACE, "inlineLiterals");
116122
}
117123
}

core/sail/lmdb/src/test/java/org/eclipse/rdf4j/sail/lmdb/ValueStoreTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* SPDX-License-Identifier: BSD-3-Clause
1010
*******************************************************************************/
11-
// Some portions generated by Codex
11+
// Some portions generated by Co-Pilot
1212
package org.eclipse.rdf4j.sail.lmdb;
1313

1414
import static org.junit.Assert.assertEquals;
@@ -32,12 +32,16 @@
3232

3333
import org.eclipse.rdf4j.model.IRI;
3434
import org.eclipse.rdf4j.model.Literal;
35+
import org.eclipse.rdf4j.model.Model;
36+
import org.eclipse.rdf4j.model.Resource;
3537
import org.eclipse.rdf4j.model.Value;
38+
import org.eclipse.rdf4j.model.util.ModelBuilder;
3639
import org.eclipse.rdf4j.model.util.Values;
3740
import org.eclipse.rdf4j.model.vocabulary.RDF;
3841
import org.eclipse.rdf4j.model.vocabulary.RDFS;
3942
import org.eclipse.rdf4j.model.vocabulary.XSD;
4043
import org.eclipse.rdf4j.sail.lmdb.config.LmdbStoreConfig;
44+
import org.eclipse.rdf4j.sail.lmdb.config.LmdbStoreSchema;
4145
import org.eclipse.rdf4j.sail.lmdb.model.LmdbBNode;
4246
import org.eclipse.rdf4j.sail.lmdb.model.LmdbIRI;
4347
import org.eclipse.rdf4j.sail.lmdb.model.LmdbLiteral;
@@ -53,6 +57,8 @@
5357
*/
5458
public class ValueStoreTest {
5559

60+
private static final IRI INLINE_LITERALS = Values.iri(LmdbStoreSchema.NAMESPACE + "inlineLiterals");
61+
5662
private ValueStore valueStore;
5763
private File dataDir;
5864

@@ -74,6 +80,31 @@ private LmdbStoreConfig hashCacheEnabledConfig() {
7480
return new LmdbStoreConfig().setValueHashCacheEnabled(true);
7581
}
7682

83+
@Test
84+
public void testDisableInlineLiteralsUsesStoredIds() throws Exception {
85+
valueStore.close();
86+
87+
LmdbStoreConfig config = new LmdbStoreConfig();
88+
Resource implNode = Values.bnode();
89+
Model configModel = new ModelBuilder()
90+
.add(implNode, INLINE_LITERALS, Values.literal(false))
91+
.build();
92+
93+
config.parse(configModel, implNode);
94+
assertFalse("inline literals should be disabled after parsing config",
95+
config.getInlineLiterals());
96+
97+
valueStore = createValueStore(config);
98+
99+
Literal literal = Values.literal("inline");
100+
valueStore.startTransaction(true);
101+
long id = valueStore.storeValue(literal);
102+
valueStore.commit();
103+
104+
assertFalse("small literals should not be inlined when disabled", ValueIds.isInlined(id));
105+
assertEquals(literal, valueStore.getValue(id));
106+
}
107+
77108
@Test
78109
public void testGcValues() throws Exception {
79110
Value values[] = new Value[] {

core/sail/lmdb/src/test/java/org/eclipse/rdf4j/sail/lmdb/config/LmdbStoreConfigTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* SPDX-License-Identifier: BSD-3-Clause
1010
*******************************************************************************/
11-
// Some portions generated by Codex
11+
// Some portions generated by Co-Pilot
1212
package org.eclipse.rdf4j.sail.lmdb.config;
1313

1414
import static org.assertj.core.api.Assertions.assertThat;
@@ -38,6 +38,8 @@ class LmdbStoreConfigTest {
3838

3939
private static final IRI NO_READAHEAD = Values.iri(LmdbStoreSchema.NAMESPACE + "noReadahead");
4040

41+
private static final IRI INLINE_LITERALS = Values.iri(LmdbStoreSchema.NAMESPACE + "inlineLiterals");
42+
4143
@Test
4244
void pageCardinalityEstimatorDefaultsToEnabled() {
4345
assertThat(new LmdbStoreConfig().getPageCardinalityEstimator()).isTrue();
@@ -58,6 +60,11 @@ void valueHashCacheDefaultsToDisabled() {
5860
assertThat(new LmdbStoreConfig().getValueHashCacheEnabled()).isFalse();
5961
}
6062

63+
@Test
64+
void inlineLiteralsDefaultsToEnabled() {
65+
assertThat(new LmdbStoreConfig().getInlineLiterals()).isTrue();
66+
}
67+
6168
@ParameterizedTest
6269
@ValueSource(booleans = { true, false })
6370
void testThatLmdbStoreConfigParseAndExportNoReadahead(final boolean noReadahead) {
@@ -106,6 +113,18 @@ void testThatLmdbStoreConfigParseAndExportValueHashCacheEnabled(final boolean va
106113
);
107114
}
108115

116+
@ParameterizedTest
117+
@ValueSource(booleans = { true, false })
118+
void testThatLmdbStoreConfigParseAndExportInlineLiterals(final boolean inlineLiterals) {
119+
testParseAndExport(
120+
INLINE_LITERALS,
121+
Values.literal(inlineLiterals),
122+
LmdbStoreConfig::getInlineLiterals,
123+
inlineLiterals,
124+
!inlineLiterals
125+
);
126+
}
127+
109128
@ParameterizedTest
110129
@ValueSource(booleans = { true, false })
111130
void testThatLmdbStoreConfigParseAndExportAutoGrow(final boolean autoGrow) {

0 commit comments

Comments
 (0)