Skip to content

Commit 27b6fd0

Browse files
committed
wip
1 parent 61239cf commit 27b6fd0

4 files changed

Lines changed: 122 additions & 21 deletions

File tree

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/optimizer/MinusOptimizer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ public void meet(Var node) {
236236
names.add(node.getName());
237237
}
238238
}
239+
240+
@Override
241+
public void meet(Extension node) {
242+
for (ExtensionElem elem : node.getElements()) {
243+
names.add(elem.getName());
244+
}
245+
super.meet(node);
246+
}
239247
});
240248
return names;
241249
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,31 @@ static <T> T readTransaction(long env, long writeTxn, Transaction<T> transaction
9999
return ret;
100100
}
101101

102+
103+
static <T> T readTransaction(long env, long writeTxn, long id, TransactionWithId<T> transaction) throws IOException {
104+
try (MemoryStack stack = stackPush()) {
105+
long txn;
106+
if (writeTxn == 0) {
107+
PointerBuffer pp = stack.mallocPointer(1);
108+
E(mdb_txn_begin(env, NULL, MDB_RDONLY, pp));
109+
txn = pp.get(0);
110+
} else {
111+
txn = writeTxn;
112+
}
113+
114+
try {
115+
return transaction.exec(stack, txn, id);
116+
} finally {
117+
if (writeTxn == 0) {
118+
mdb_txn_abort(txn);
119+
}
120+
}
121+
}
122+
123+
}
124+
125+
126+
102127
static <T> T transaction(long env, Transaction<T> transaction) throws IOException {
103128
T ret;
104129
try (MemoryStack stack = stackPush()) {
@@ -202,4 +227,9 @@ interface Transaction<T> {
202227
T exec(MemoryStack stack, long txn) throws IOException;
203228
}
204229

230+
@FunctionalInterface
231+
interface TransactionWithId<T> {
232+
T exec(MemoryStack stack, long txn, long id) throws IOException;
233+
}
234+
205235
}

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

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,20 +454,45 @@ ValueStoreRevision getRevision() {
454454
return revision;
455455
}
456456

457+
458+
459+
460+
private final LmdbUtil.TransactionWithId<byte[]> GET_DATA_FN = this::getDataInTxn;
461+
457462
protected byte[] getData(long id) throws IOException {
458-
return readTransaction(env, (stack, txn) -> {
459-
MDBVal keyData = MDBVal.calloc(stack);
460-
keyData.mv_data(id2data(idBuffer(stack), id).flip());
461-
MDBVal valueData = MDBVal.calloc(stack);
462-
if (mdb_get(txn, dbi, keyData, valueData) == MDB_SUCCESS) {
463-
byte[] valueBytes = new byte[valueData.mv_data().remaining()];
464-
valueData.mv_data().get(valueBytes);
465-
return valueBytes;
463+
return readTransaction(env, id, GET_DATA_FN);
464+
}
465+
466+
<T> T readTransaction(long env, long id, LmdbUtil.TransactionWithId<T> getDataFn) throws IOException {
467+
txnLock.readLock().lock();
468+
try {
469+
if (writeTxn != 0) {
470+
return LmdbUtil.readTransaction(env, writeTxn, id, getDataFn);
466471
}
472+
return threadLocalReadTxn.get().execute(getDataFn, env, id);
473+
} finally {
474+
txnLock.readLock().unlock();
475+
}
476+
}
477+
478+
private byte[] getDataInTxn(MemoryStack stack, long txn, long id) {
479+
MDBVal keyData = MDBVal.malloc(stack);
480+
ByteBuffer keyBuf = id2data(idBuffer(stack), id);
481+
keyBuf.flip();
482+
keyData.mv_data(keyBuf);
483+
484+
MDBVal valueData = MDBVal.malloc(stack);
485+
if (mdb_get(txn, dbi, keyData, valueData) != MDB_SUCCESS) {
467486
return null;
468-
});
487+
}
488+
489+
ByteBuffer src = valueData.mv_data(); // don’t call twice
490+
byte[] out = new byte[src.remaining()];
491+
src.get(out);
492+
return out;
469493
}
470494

495+
471496
/**
472497
* Get value from cache by ID.
473498
* <p>
@@ -1412,6 +1437,38 @@ synchronized <T> T execute(Transaction<T> transaction, long env) throws IOExcept
14121437
}
14131438
}
14141439

1440+
synchronized <T> T execute(LmdbUtil.TransactionWithId<T> transaction, long env, long id) throws IOException {
1441+
try (MemoryStack stack = MemoryStack.stackPush()) {
1442+
try {
1443+
ensureTxn(env);
1444+
state.depth++;
1445+
try {
1446+
return transaction.exec(stack, state.txn, id);
1447+
} finally {
1448+
releaseTxn();
1449+
}
1450+
} catch (Exception e) {
1451+
// Retry once
1452+
try {
1453+
System.gc();
1454+
Thread.sleep(1);
1455+
System.gc();
1456+
Thread.sleep(1);
1457+
} catch (InterruptedException ie) {
1458+
Thread.currentThread().interrupt();
1459+
}
1460+
1461+
ensureTxn(env);
1462+
state.depth++;
1463+
try {
1464+
return transaction.exec(stack, state.txn, id);
1465+
} finally {
1466+
releaseTxn();
1467+
}
1468+
}
1469+
}
1470+
}
1471+
14151472
private void ensureTxn(long env) throws IOException {
14161473
registerIfNeeded();
14171474

core/sail/lmdb/src/test/java/org/eclipse/rdf4j/sail/lmdb/benchmark/ThemeQueryBenchmark.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.io.File;
1717
import java.io.IOException;
18+
import java.util.List;
1819
import java.util.concurrent.TimeUnit;
1920

2021
import org.apache.commons.io.FileUtils;
@@ -197,19 +198,24 @@ public void testQueryExplanation() throws IOException {
197198
String[] themeNames = paramValues("themeName");
198199
for (String themeNameValue : themeNames) {
199200
for (String queryIndexValue : queryIndexes) {
200-
themeName = themeNameValue;
201-
x_queryIndex = Integer.parseInt(queryIndexValue);
202-
setup();
203-
try (SailRepositoryConnection connection = repository.getConnection()) {
204-
String explanation = connection
205-
.prepareTupleQuery(query)
206-
.explain(Explanation.Level.Executed)
207-
.toString();
208-
System.out.println("Query Explanation for theme " + themeName + " and query index " + x_queryIndex
209-
+ ":\n" + explanation);
210-
} finally {
211-
tearDown();
201+
for (Boolean b : List.of(false, true)) {
202+
this.z_useSparqlUo = b;
203+
themeName = themeNameValue;
204+
x_queryIndex = Integer.parseInt(queryIndexValue);
205+
setup();
206+
try (SailRepositoryConnection connection = repository.getConnection()) {
207+
String explanation = connection
208+
.prepareTupleQuery(query)
209+
.explain(Explanation.Level.Executed)
210+
.toString();
211+
System.out
212+
.println("Query Explanation for theme " + themeName + " and query index " + x_queryIndex
213+
+ " and z_useSparqlUo=" + b + " :\n" + explanation);
214+
} finally {
215+
tearDown();
216+
}
212217
}
218+
System.out.println("----------------------------------------\n");
213219
}
214220
}
215221
}

0 commit comments

Comments
 (0)