Skip to content

Commit

Permalink
refactoring keyvaluestore creation from class hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorrit Poelen committed Jan 9, 2025
1 parent c8a4d54 commit 2bb725d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package bio.guoda.preston.cmd;

import bio.guoda.preston.store.KeyToPath;

public interface KeyToPathFactory {
KeyToPath getKeyToPath();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package bio.guoda.preston.cmd;

import bio.guoda.preston.store.KeyTo1LevelPath;
import bio.guoda.preston.store.KeyTo3LevelPath;
import bio.guoda.preston.store.KeyToPath;

import java.net.URI;

public class KeyToPathFactoryDepth implements KeyToPathFactory {

private final URI baseURI;
private final int depth;
private KeyToPath keyToPathLocal;

public KeyToPathFactoryDepth(URI baseURI, int depth) {
if (depth != 0 && depth != 2) {
throw new IllegalArgumentException("only directory depths in {0,2} are supported, but found [" + depth + "]");
}

this.baseURI = baseURI;
this.depth = depth;
this.keyToPathLocal = null;
}

@Override
public KeyToPath getKeyToPath() {
if (this.keyToPathLocal == null) {
if (this.depth == 2) {
this.keyToPathLocal = new KeyTo3LevelPath(baseURI);
} else if (this.depth == 0) {
this.keyToPathLocal = new KeyTo1LevelPath(baseURI);
}
}
return this.keyToPathLocal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bio.guoda.preston.cmd;

import bio.guoda.preston.store.KeyValueStore;
import bio.guoda.preston.store.ValidatingKeyValueStreamFactory;

public interface KeyValueStoreFactory {
KeyValueStore getKeyValueStore(ValidatingKeyValueStreamFactory validatingKeyValueStreamFactory);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package bio.guoda.preston.cmd;

import bio.guoda.preston.store.KeyTo5LevelPath;
import bio.guoda.preston.store.KeyToPath;
import bio.guoda.preston.store.KeyValueStore;
import bio.guoda.preston.store.KeyValueStoreLocalFileSystem;
import bio.guoda.preston.store.KeyValueStoreReadOnly;
import bio.guoda.preston.store.KeyValueStoreWithFallback;
import bio.guoda.preston.store.ValidatingKeyValueStreamFactory;

import java.io.File;

public class KeyValueStoreFactoryFallBack implements KeyValueStoreFactory {

private final File dataDir;
private final File tmpDir;
private KeyToPath keyToPath;

public KeyValueStoreFactoryFallBack(File dataDir, File tmpDir, KeyToPath keyToPath) {
this.dataDir = dataDir;
this.tmpDir = tmpDir;
this.keyToPath = keyToPath;
}

@Override
public KeyValueStore getKeyValueStore(ValidatingKeyValueStreamFactory validatingKeyValueStreamFactory) {
KeyValueStore primary = new KeyValueStoreLocalFileSystem(
this.tmpDir,
this.keyToPath,
validatingKeyValueStreamFactory
);

// for backwards compatibility
KeyValueStoreReadOnly fallback = new KeyValueStoreLocalFileSystem(
this.tmpDir,
new KeyTo5LevelPath(this.dataDir.toURI()),
validatingKeyValueStreamFactory
);

return new KeyValueStoreWithFallback(primary, fallback);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
import bio.guoda.preston.store.HexaStoreImpl;
import bio.guoda.preston.store.KeyTo1LevelPath;
import bio.guoda.preston.store.KeyTo3LevelPath;
import bio.guoda.preston.store.KeyTo5LevelPath;
import bio.guoda.preston.store.KeyToPath;
import bio.guoda.preston.store.KeyValueStore;
import bio.guoda.preston.store.KeyValueStoreLocalFileSystem;
import bio.guoda.preston.store.KeyValueStoreReadOnly;
import bio.guoda.preston.store.KeyValueStoreWithFallback;
import bio.guoda.preston.store.ProvenanceTracer;
import bio.guoda.preston.store.ProvenanceTracerByIndex;
import bio.guoda.preston.store.ProvenanceTracerImpl;
Expand Down Expand Up @@ -67,19 +63,26 @@ static File mkdir(String data1) {
}

protected KeyValueStore getKeyValueStore(ValidatingKeyValueStreamFactory validatingKeyValueStreamFactory) {
KeyValueStore primary = new KeyValueStoreLocalFileSystem(
return getKeyValueStore(validatingKeyValueStreamFactory,
new File(getDataDir()),
new File(getTmpDir()),
getKeyToPathLocal(new File(getDataDir()).toURI()),
validatingKeyValueStreamFactory
);
this.depth);
}

KeyValueStoreReadOnly fallback = new KeyValueStoreLocalFileSystem(
new File(getTmpDir()),
new KeyTo5LevelPath(new File(getDataDir()).toURI()),
validatingKeyValueStreamFactory
);
public static KeyValueStore getKeyValueStore(
ValidatingKeyValueStreamFactory validatingKeyValueStreamFactory,
File dataDir,
File tmpDir,
int directoryDepth) {

final KeyToPathFactory keyToPathFactory
= new KeyToPathFactoryDepth(dataDir.toURI(), directoryDepth);

return new KeyValueStoreWithFallback(primary, fallback);
return new KeyValueStoreFactoryFallBack(
dataDir,
tmpDir,
keyToPathFactory.getKeyToPath()
).getKeyValueStore(validatingKeyValueStreamFactory);
}


Expand All @@ -94,8 +97,8 @@ public ProvenanceTracer getProvenanceTracer() {

private Factory<KeyValueStore> getKeyValueStoreFactoryForOrigins() {
return () -> getKeyValueStore(
new ValidatingKeyValueStreamContentAddressedFactory()
);
new ValidatingKeyValueStreamContentAddressedFactory()
);
}

public boolean isAnchored() {
Expand All @@ -109,7 +112,7 @@ protected ProvenanceTracer getTracerOfDescendants() {
return getTracerOfDescendants(factory);
}

protected ProvenanceTracer getTracerOfDescendants(Factory<KeyValueStore> keyValueStoreFactory) {
protected ProvenanceTracer getTracerOfDescendants(Factory<KeyValueStore> keyValueStoreFactory) {
HexaStoreImpl hexastore = new HexaStoreImpl(
keyValueStoreFactory.create(),
getHashType()
Expand Down

0 comments on commit 2bb725d

Please sign in to comment.