Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-2061 mapdb upgrade #2063

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*******************************************************************************/
package org.eclipse.rdf4j.query.algebra.evaluation.iterator;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -51,6 +49,7 @@
import org.eclipse.rdf4j.query.impl.EmptyBindingSet;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.Serializer;

/**
* @author David Huynh
Expand All @@ -76,8 +75,6 @@ public class GroupIterator extends CloseableIteratorIteration<BindingSet, QueryE

private final Object lock = new Object();

private final File tempFile;

private final DB db;

/**
Expand All @@ -103,14 +100,12 @@ public GroupIterator(EvaluationStrategy strategy, Group group, BindingSet parent
this.iterationCacheSyncThreshold = iterationCacheSyncThreshold;

if (this.iterationCacheSyncThreshold > 0) {
try {
this.tempFile = File.createTempFile("group-eval", null);
} catch (IOException e) {
throw new QueryEvaluationException("could not initialize temp db", e);
}
this.db = DBMaker.newFileDB(tempFile).deleteFilesAfterClose().closeOnJvmShutdown().make();
this.db = DBMaker.tempFileDB()
.fileDeleteAfterClose()
.fileChannelEnable()
.closeOnJvmShutdown()
.make();
} else {
this.tempFile = null;
this.db = null;
}
}
Expand Down Expand Up @@ -158,7 +153,7 @@ protected void handleClose() throws QueryEvaluationException {

private <T> Set<T> createSet(String setName) {
if (db != null) {
return db.getHashSet(setName);
return (Set<T>) db.hashSet(setName).serializer(Serializer.JAVA).create();
} else {
return new HashSet<>();
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@
<dependency>
<groupId>org.mapdb</groupId>
<artifactId>mapdb</artifactId>
<version>1.0.8</version>
<version>3.0.8</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's quite a jump!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but mapdb documentation is still a bit sparse though :-/ Neither v1 nor v2 are supported anymore...

v3 allows for an overflow: one can create an in-memory mapdb, set an "expire" on either size or access time, and the expired entries can be stored in e.g. another on-disk mapdb

See also jankotek/mapdb#708

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does look nice. If I remember correctly we sort of rolled our own for this by using a MapDB backed set but only calling commit (which writes to disk) every N items. If we can now configure mapdb to handle that kind of overflow for us, it simplify the code a lot (and it will probably also be more reliable).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation is here: https://jankotek.gitbooks.io/mapdb/content/

There seems to be a long-standing issue with mmap and JDK, which is a bit unfortunate since the performance gains are substantial https://jankotek.gitbooks.io/mapdb/content/performance/

There is also bug in JVM. Mmaped file handles are not released until DirectByteBuffer is GCed. That means that mmap file remains open even after db.close() is called. On Windows it prevents file to be reopened or deleted. On Linux it consumes file descriptors, and could lead to errors once all descriptors are used.
There is a workaround for this bug using undocumented API. But it was linked to JVM crashes in rare cases and is disabled by default. Use DBMaker.cleanerHackEnable() to enable it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeenbroekstra apparently the overflow in MapDB is not (yet) supported for Maps, not for Sets :-(

Otherwise it would indeed save a few lines in GroupIterator

</dependency>
</dependencies>
</dependencyManagement>
Expand Down