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

use regular jdk String.intern() #350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 6 additions & 8 deletions core-tests/src/test/java/overflowdb/storage/OdbStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import overflowdb.Graph;
import overflowdb.testdomains.gratefuldead.GratefulDead;
import overflowdb.testdomains.gratefuldead.Song;
import overflowdb.util.StringInterner;

import java.io.File;
import java.io.IOException;
Expand All @@ -18,7 +17,6 @@
import static org.junit.Assert.assertFalse;

public class OdbStorageTest {
private StringInterner stringInterner = new StringInterner();

@Test
public void persistToFileIfStorageConfigured() throws IOException {
Expand Down Expand Up @@ -68,7 +66,7 @@ public void shouldDeleteTmpStorageIfNoStorageLocationConfigured() {
public void shouldErrorWhenTryingToOpenWithoutStorageFormatVersion() throws IOException {
File storageFile = Files.createTempFile("overflowdb", "bin").toFile();
storageFile.deleteOnExit();
OdbStorage storage = OdbStorage.createWithSpecificLocation(storageFile, stringInterner);
OdbStorage storage = OdbStorage.createWithSpecificLocation(storageFile);
storage.close();

// modify storage: drop storage version
Expand All @@ -78,14 +76,14 @@ public void shouldErrorWhenTryingToOpenWithoutStorageFormatVersion() throws IOEx
store.close();

// should throw a BackwardsCompatibilityError
OdbStorage.createWithSpecificLocation(storageFile, stringInterner);
OdbStorage.createWithSpecificLocation(storageFile);
}

@Test(expected = BackwardsCompatibilityError.class)
public void shouldErrorWhenTryingToOpenDifferentStorageFormatVersion() throws IOException {
File storageFile = Files.createTempFile("overflowdb", "bin").toFile();
storageFile.deleteOnExit();
OdbStorage storage = OdbStorage.createWithSpecificLocation(storageFile, stringInterner);
OdbStorage storage = OdbStorage.createWithSpecificLocation(storageFile);
storage.close();

// modify storage: change storage version
Expand All @@ -95,14 +93,14 @@ public void shouldErrorWhenTryingToOpenDifferentStorageFormatVersion() throws IO
store.close();

// should throw a BackwardsCompatibilityError
OdbStorage.createWithSpecificLocation(storageFile, stringInterner);
OdbStorage.createWithSpecificLocation(storageFile);
}

@Test
public void shouldProvideStringToIntGlossary() throws IOException {
File storageFile = Files.createTempFile("overflowdb", "bin").toFile();
storageFile.deleteOnExit();
OdbStorage storage = OdbStorage.createWithSpecificLocation(storageFile, stringInterner);
OdbStorage storage = OdbStorage.createWithSpecificLocation(storageFile);

String a = "a";
String b = "b";
Expand All @@ -119,7 +117,7 @@ public void shouldProvideStringToIntGlossary() throws IOException {

// should survive restarts
storage.close();
storage = OdbStorage.createWithSpecificLocation(storageFile, stringInterner);
storage = OdbStorage.createWithSpecificLocation(storageFile);
assertEquals(stringIdA, storage.lookupOrCreateStringToIntMapping(a));
assertEquals(stringIdB, storage.lookupOrCreateStringToIntMapping(b));

Expand Down
10 changes: 2 additions & 8 deletions core/src/main/java/overflowdb/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public final class Graph implements AutoCloseable {
protected final OdbStorage storage;
public final NodeSerializer nodeSerializer;
protected final NodeDeserializer nodeDeserializer;
protected final StringInterner stringInterner;
protected final Optional<HeapUsageMonitor> heapUsageMonitor;
protected final boolean overflowEnabled;
protected final ReferenceManager referenceManager;
Expand Down Expand Up @@ -66,11 +65,10 @@ private Graph(Config config,
this.config = config;
this.nodeFactoryByLabel = nodeFactoryByLabel;
this.edgeFactoryByLabel = edgeFactoryByLabel;
this.stringInterner = new StringInterner();

this.storage = config.getStorageLocation().isPresent()
? OdbStorage.createWithSpecificLocation(config.getStorageLocation().get().toFile(), stringInterner)
: OdbStorage.createWithTempFile(stringInterner);
? OdbStorage.createWithSpecificLocation(config.getStorageLocation().get().toFile())
: OdbStorage.createWithTempFile();
this.nodeDeserializer = new NodeDeserializer(this, nodeFactoryByLabel, config.isSerializationStatsEnabled(), storage);
this.nodeSerializer = new NodeSerializer(config.isSerializationStatsEnabled(), storage, convertPropertyForPersistence);
this.nodesWriter = new NodesWriter(nodeSerializer, storage);
Expand Down Expand Up @@ -216,7 +214,6 @@ public synchronized void close() {
} else {
this.closed = true;
shutdownNow();
stringInterner.clear();
}
}

Expand Down Expand Up @@ -432,7 +429,4 @@ public ArrayList<Map<String, String>> getAllLibraryVersions() {
return storage.getAllLibraryVersions();
}

public StringInterner getStringInterner() {
return this.stringInterner;
}
}
5 changes: 1 addition & 4 deletions core/src/main/java/overflowdb/storage/NodeDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import overflowdb.NodeFactory;
import overflowdb.NodeRef;
import overflowdb.util.PropertyHelper;
import overflowdb.util.StringInterner;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -23,12 +22,10 @@ public class NodeDeserializer extends BookKeeper {
protected final Graph graph;
private final Map<String, NodeFactory> nodeFactoryByLabel;
private final OdbStorage storage;
private final StringInterner stringInterner;

public NodeDeserializer(Graph graph, Map<String, NodeFactory> nodeFactoryByLabel, boolean statsEnabled, OdbStorage storage) {
super(statsEnabled);
this.graph = graph;
this.stringInterner = graph.getStringInterner();
this.nodeFactoryByLabel = nodeFactoryByLabel;
this.storage = storage;
}
Expand Down Expand Up @@ -113,7 +110,7 @@ private final Object unpackValue(final ArrayValue packedValueAndType) {
case BOOLEAN:
return value.asBooleanValue().getBoolean();
case STRING:
return stringInterner.intern(value.asStringValue().asString());
return value.asStringValue().asString().intern();
case BYTE:
return value.asIntegerValue().asByte();
case SHORT:
Expand Down
15 changes: 6 additions & 9 deletions core/src/main/java/overflowdb/storage/OdbStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.h2.mvstore.MVStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import overflowdb.util.StringInterner;

import java.io.File;
import java.io.IOException;
Expand All @@ -31,7 +30,6 @@ public class OdbStorage implements AutoCloseable {
private final Logger logger = LoggerFactory.getLogger(getClass());

private final File mvstoreFile;
private final StringInterner stringInterner;
private FileStore mvstoreFileStore;
protected MVStore mvstore;
private MVMap<Long, byte[]> nodesMVMap;
Expand All @@ -42,20 +40,19 @@ public class OdbStorage implements AutoCloseable {
private ArrayList<String> stringToIntReverseMappings;
private int libraryVersionsIdCurrentRun;

public static OdbStorage createWithTempFile(StringInterner stringInterner) {
return new OdbStorage(Optional.empty(), stringInterner);
public static OdbStorage createWithTempFile() {
return new OdbStorage(Optional.empty());
}

/**
* create with specific mvstore file - which may or may not yet exist.
* mvstoreFile won't be deleted at the end (unlike temp file constructors above)
*/
public static OdbStorage createWithSpecificLocation(final File mvstoreFile, StringInterner stringInterner) {
return new OdbStorage(Optional.ofNullable(mvstoreFile), stringInterner);
public static OdbStorage createWithSpecificLocation(final File mvstoreFile) {
return new OdbStorage(Optional.ofNullable(mvstoreFile));
}

private OdbStorage(final Optional<File> mvstoreFileMaybe, StringInterner stringInterner) {
this.stringInterner = stringInterner;
private OdbStorage(final Optional<File> mvstoreFileMaybe) {
if (mvstoreFileMaybe.isPresent()) {
mvstoreFile = mvstoreFileMaybe.get();
if (mvstoreFile.exists() && mvstoreFile.length() > 0) {
Expand Down Expand Up @@ -206,7 +203,7 @@ private void ensureCapacity(ArrayList array, int requiredMinSize) {
public String reverseLookupStringToIntMapping(int stringId) {
getStringToIntMappings(); //ensure everything is initialized
String string = stringToIntReverseMappings.get(stringId);
return stringInterner.intern(string);
return string.intern();
}

private void ensureMVStoreAvailable() {
Expand Down
16 changes: 0 additions & 16 deletions core/src/main/java/overflowdb/util/StringInterner.java

This file was deleted.