forked from Consensys/orion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the key/value library provided by cava
- Loading branch information
Showing
18 changed files
with
152 additions
and
216 deletions.
There are no files selected for viewing
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
21 changes: 14 additions & 7 deletions
21
src/main/java/net/consensys/orion/api/storage/Storage.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 |
---|---|---|
@@ -1,25 +1,32 @@ | ||
package net.consensys.orion.api.storage; | ||
|
||
import net.consensys.cava.concurrent.AsyncResult; | ||
|
||
import java.util.Optional; | ||
|
||
public interface Storage<T> { | ||
|
||
/** | ||
* Stores data in the store. | ||
* | ||
* @param data The data to store. | ||
* @return the base64 encoded key, as an UTF-8 String | ||
*/ | ||
String put(T data); | ||
AsyncResult<String> put(T data); | ||
|
||
/** | ||
* @param key should be base64 encoded UTF-8 string | ||
* @return The retrieved data. | ||
* Generates digest for data without storing it. | ||
* | ||
* @param data the data to generate a digest for | ||
* @return the digest of the data | ||
*/ | ||
Optional<T> get(String key); | ||
String generateDigest(T data); | ||
|
||
/** | ||
* Remove stored data. | ||
* Gets data from the store. | ||
* | ||
* @param key The base64 encoded key. | ||
* @param key should be base64 encoded UTF-8 string | ||
* @return The retrieved data. | ||
*/ | ||
void remove(String key); | ||
AsyncResult<Optional<T>> get(String key); | ||
} |
15 changes: 8 additions & 7 deletions
15
src/main/java/net/consensys/orion/api/storage/StorageEngine.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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
package net.consensys.orion.api.storage; | ||
|
||
import java.util.Optional; | ||
|
||
public interface StorageEngine<T> { | ||
import net.consensys.cava.concurrent.AsyncCompletion; | ||
import net.consensys.cava.concurrent.AsyncResult; | ||
|
||
void put(String key, T data); | ||
import java.io.Closeable; | ||
import java.util.Optional; | ||
|
||
Optional<T> get(String key); | ||
public interface StorageEngine<T> extends Closeable { | ||
|
||
void remove(String key); | ||
AsyncCompletion put(String key, T data); | ||
|
||
boolean isOpen(); | ||
AsyncResult<Optional<T>> get(String key); | ||
|
||
@Override | ||
void close(); | ||
} |
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
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
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
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
48 changes: 16 additions & 32 deletions
48
src/main/java/net/consensys/orion/impl/storage/file/MapDbStorage.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 |
---|---|---|
@@ -1,63 +1,47 @@ | ||
package net.consensys.orion.impl.storage.file; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static net.consensys.orion.impl.http.server.HttpContentType.CBOR; | ||
import static org.mapdb.Serializer.BYTE_ARRAY; | ||
|
||
import net.consensys.cava.bytes.Bytes; | ||
import net.consensys.cava.concurrent.AsyncCompletion; | ||
import net.consensys.cava.concurrent.AsyncResult; | ||
import net.consensys.cava.kv.MapDBKeyValueStore; | ||
import net.consensys.orion.api.storage.StorageEngine; | ||
import net.consensys.orion.impl.http.server.HttpContentType; | ||
import net.consensys.orion.impl.utils.Serializer; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
import org.mapdb.DB; | ||
import org.mapdb.DBMaker; | ||
import org.mapdb.HTreeMap; | ||
|
||
public class MapDbStorage<T> implements StorageEngine<T> { | ||
|
||
private final Class<? extends T> typeParameterClass; | ||
private final DB db; | ||
private final HTreeMap<byte[], byte[]> storageData; | ||
private MapDBKeyValueStore store; | ||
|
||
public MapDbStorage(Class<? extends T> typeParameterClass, Path dbDir) { | ||
this.typeParameterClass = typeParameterClass; | ||
Path dbFile = dbDir.resolve("mapdb"); | ||
db = DBMaker.fileDB(dbFile.toFile()).transactionEnable().make(); | ||
storageData = db.hashMap("storageData", BYTE_ARRAY, BYTE_ARRAY).createOrOpen(); | ||
store = new MapDBKeyValueStore(dbDir.resolve("mapdb")); | ||
} | ||
|
||
@Override | ||
public void put(String key, T data) { | ||
// store data | ||
storageData.put(key.getBytes(StandardCharsets.UTF_8), Serializer.serialize(CBOR, data)); | ||
db.commit(); | ||
} | ||
|
||
@Override | ||
public Optional<T> get(String key) { | ||
byte[] bytes = storageData.get(key.getBytes(StandardCharsets.UTF_8)); | ||
if (bytes == null) { | ||
return Optional.empty(); | ||
} | ||
public AsyncCompletion put(String key, T data) { | ||
return store | ||
.putAsync(Bytes.wrap(key.getBytes(StandardCharsets.UTF_8)), Bytes.wrap(Serializer.serialize(CBOR, data))); | ||
|
||
return Optional.of(Serializer.deserialize(CBOR, typeParameterClass, bytes)); | ||
} | ||
|
||
@Override | ||
public void remove(String key) { | ||
if (storageData.remove(key.getBytes(StandardCharsets.UTF_8)) != null) { | ||
db.commit(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return !db.isClosed(); | ||
public AsyncResult<Optional<T>> get(String key) { | ||
return store.getAsync(Bytes.wrap(key.getBytes(UTF_8))).thenApply( | ||
optionalBytes -> optionalBytes | ||
.map(bytes -> Serializer.deserialize(HttpContentType.CBOR, typeParameterClass, bytes.toArrayUnsafe()))); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
db.close(); | ||
store.close(); | ||
} | ||
} |
Oops, something went wrong.