-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring keyvaluestore creation from class hierarchy
- Loading branch information
Jorrit Poelen
committed
Jan 9, 2025
1 parent
c8a4d54
commit 2bb725d
Showing
5 changed files
with
114 additions
and
17 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
preston-cmd/src/main/java/bio/guoda/preston/cmd/KeyToPathFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
36 changes: 36 additions & 0 deletions
36
preston-cmd/src/main/java/bio/guoda/preston/cmd/KeyToPathFactoryDepth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
preston-cmd/src/main/java/bio/guoda/preston/cmd/KeyValueStoreFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
43 changes: 43 additions & 0 deletions
43
preston-cmd/src/main/java/bio/guoda/preston/cmd/KeyValueStoreFactoryFallBack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters