V
- The type of the Serializable
data stored in the underlying storage.
Encapsulates the concept of a Storage. It can abstract away a key-value storage as well as other ways of storing
data. It uses three main concepts (see below) - namespaces, partitions and criteria to abstract away most storages.
It lets you store and retrieve raw byte[] or strings, or objects of the type parameter.
It exposes these concepts:
getPartition(String, int)
,
clear(String, int)
, numberOfPartitions(String)
and repartition(String, int)
, as well as
actually partitioning the data when implementing the byte[] methods. For convenience, a hash(String, int)
is provided. By default, unless the specific storage manager says so, the storage is assumed to be unpartitioned.
StorageManager
can provide
storage-specific Criteria
for storage-specific querying needs. Note that the Criteria
methods,
retrieveAll(Criteria)
and getAll(Criteria)
do not take a namespace since those are handled by
the Criteria
implementations. The apply(Criteria, Object)
can be used as a general method to
apply arbitrary changes to the storage.
convert(Serializable)
and convert(byte[])
methods.
For convenience,
toString(byte[])
and toBytes(String)
. The map flavors are available as
toByteArrayMap(Map)
and toStringMap(Map)
methods.
DEFAULT_NAMESPACE
when invoking the corresponding methods that do require a namespace. If you wish
to control the default namespace used, override the getDefaultNamespace()
method.
public abstract class StorageManager<V extends Serializable> extends Object implements Serializable
Modifier and Type | Field and Description |
---|---|
protected BulletConfig |
config |
static String |
DEFAULT_NAMESPACE |
Constructor and Description |
---|
StorageManager(BulletConfig config)
Constructor that takes a
BulletConfig . |
Modifier and Type | Method and Description |
---|---|
<T,R> CompletableFuture<R> |
apply(Criteria<T,R> criteria,
T query)
Applies the given
Criteria to this storage. |
CompletableFuture<Boolean> |
clear()
Clears the default namespace.
|
CompletableFuture<Boolean> |
clear(int partition)
Clears the specified partition under the default namespace.
|
CompletableFuture<Boolean> |
clear(Set<String> ids)
Removes a given set of IDs from the storage under the default namespace.
|
abstract CompletableFuture<Boolean> |
clear(String namespace)
Clears the specified namespace.
|
CompletableFuture<Boolean> |
clear(String namespace,
int partition)
Clears the specified partition under the given namespace.
|
abstract CompletableFuture<Boolean> |
clear(String namespace,
Set<String> ids)
Removes a given set of IDs from the storage under the given namespace.
|
void |
close()
.
|
protected V |
convert(byte[] bytes)
Converts a @
byte[] to a type of the given object. |
protected byte[] |
convert(V object)
Converts an object of the given type to a @
byte[] . |
static StorageManager |
from(BulletConfig config)
Create a
StorageManager instance using the class specified in the config file. |
static <S> Map<String,byte[]> |
fromObjectMap(Map<String,S> input,
Function<S,byte[]> converter)
|
CompletableFuture<V> |
get(String id)
Retrieves data stored for a given String identifier in the default namespace as a
Serializable object. |
CompletableFuture<V> |
get(String namespace,
String id)
Retrieves data stored for a given String identifier in the given namespace as a
Serializable object. |
CompletableFuture<Map<String,V>> |
getAll()
Retrieves all the IDs from the default namespace in the storage as a
Map of IDs to their
Serializable values. |
<T,R> CompletableFuture<Map<String,V>> |
getAll(Criteria<T,R> criteria)
Retrieves all the IDs matching the specified
Criteria from the storage as the type of the storage. |
CompletableFuture<Map<String,V>> |
getAll(Set<String> ids)
Retrieves the values for the provided IDs from the default namespace in the storage as a
Map of IDs to
their Serializable values. |
CompletableFuture<Map<String,V>> |
getAll(String namespace)
Retrieves all the IDs from the given namespace in the storage as a
Map of IDs to their
Serializable values. |
CompletableFuture<Map<String,V>> |
getAll(String namespace,
Set<String> ids)
Retrieves the values for the provided IDs from the given namespace in the storage as a
Map of IDs to
their Serializable values. |
protected abstract CompletableFuture<Map<String,byte[]>> |
getAllRaw(String namespace)
Retrieves all the IDs from the given namespace in the storage as byte[].
|
protected CompletableFuture<Map<String,byte[]>> |
getAllRaw(String namespace,
Set<String> ids)
Retrieves the values for the provided IDs from the given namespace in the storage as byte[].
|
CompletableFuture<Map<String,String>> |
getAllString()
Retrieves all the IDs from the default namespace in the storage as a
Map of IDs to their String values. |
CompletableFuture<Map<String,String>> |
getAllString(Set<String> ids)
Retrieves the values for the provided IDs from the default namespace in the storage as a
Map of IDs to
their String values. |
CompletableFuture<Map<String,String>> |
getAllString(String namespace)
Retrieves all the IDs from the given namespace in the storage as a
Map of IDs to their String values. |
CompletableFuture<Map<String,String>> |
getAllString(String namespace,
Set<String> ids)
Retrieves the values for the provided IDs from the given namespace in the storage as a
Map of IDs to
their String values. |
protected String |
getDefaultNamespace()
Gets the default namespace to store data under.
|
CompletableFuture<Map<String,V>> |
getPartition(int partition)
Retrieves the IDs stored in the provided partition for the default namespace.
|
CompletableFuture<Map<String,V>> |
getPartition(String namespace,
int partition)
Retrieves the IDs stored in the provided partition for the given namespace.
|
protected CompletableFuture<Map<String,byte[]>> |
getPartitionRaw(String namespace,
int partition)
Retrieves the IDs stored in the provided partition for the given namespace.
|
CompletableFuture<Map<String,String>> |
getPartitionString(String namespace,
int partition)
Retrieves the IDs stored in the provided partition for the given namespace.
|
protected abstract CompletableFuture<byte[]> |
getRaw(String namespace,
String id)
Retrieves a given ID from the given namespace in the storage.
|
CompletableFuture<String> |
getString(String id)
Retrieves data stored for a given String identifier in the default namespace as a String.
|
CompletableFuture<String> |
getString(String namespace,
String id)
Retrieves data stored for a given String identifier in the given namespace as a String.
|
static int |
hash(String key,
int numberOfPartitions)
A default hash function for a given String that places the String into 0 to numberOfPartitions exclusive.
|
int |
numberOfPartitions()
Returns the number of partitions stored in this storage manager for the default namespace.
|
int |
numberOfPartitions(String namespace)
Returns the number of partitions stored in this storage manager for the given namespace.
|
CompletableFuture<Boolean> |
put(String namespace,
String id,
V value)
Stores any
Serializable object for a given String identifier in the given namespace. |
CompletableFuture<Boolean> |
put(String id,
V data)
Stores any
Serializable object for a given String identifier in the default namespace. |
CompletableFuture<Boolean> |
putAll(Map<String,V> data)
Stores a map of IDs and values into the storage in the default namespace.
|
CompletableFuture<Boolean> |
putAll(String namespace,
Map<String,V> data)
Stores a map of IDs and values into the storage in the given namespace.
|
protected CompletableFuture<Boolean> |
putAllRaw(String namespace,
Map<String,byte[]> data)
Stores a map of IDs and values into the storage in the given namespace.
|
CompletableFuture<Boolean> |
putAllString(Map<String,String> data)
Stores a map of IDs and String values into the storage in the default namespace.
|
CompletableFuture<Boolean> |
putAllString(String namespace,
Map<String,String> data)
Stores a map of IDs and String values into the storage in the given namespace.
|
protected abstract CompletableFuture<Boolean> |
putRaw(String namespace,
String id,
byte[] value)
Store a given ID and value for that ID into the given namespace in the storage.
|
CompletableFuture<Boolean> |
putString(String id,
String value)
Stores a String for a given String identifier in the default namespace.
|
CompletableFuture<Boolean> |
putString(String namespace,
String id,
String value)
Stores a String for a given String identifier in the given namespace.
|
CompletableFuture<V> |
remove(String id)
Retrieves and removes data stored for a given String identifier as a
Serializable object in the default
namespace. |
CompletableFuture<V> |
remove(String namespace,
String id)
Retrieves and removes data stored for a given String identifier as a
Serializable object in the given
namespace. |
protected abstract CompletableFuture<byte[]> |
removeRaw(String namespace,
String id)
Removes a given ID from the given namespace in the storage.
|
CompletableFuture<String> |
removeString(String id)
Retrieves and removes data stored for a given String identifier as a String in the default namespace.
|
CompletableFuture<String> |
removeString(String namespace,
String id)
Retrieves and removes data stored for a given String identifier as a String in the given namespace.
|
CompletableFuture<Boolean> |
repartition(int newPartitionCount)
Repartitions the data into the given new number of partitions in the default namespace.
|
CompletableFuture<Boolean> |
repartition(String namespace,
int newPartitionCount)
Repartitions the data into the given new number of partitions for the given namespace.
|
<T,R> CompletableFuture<R> |
retrieveAll(Criteria<T,R> criteria)
|
static Map<String,byte[]> |
toByteArrayMap(Map<String,String> input)
|
static byte[] |
toBytes(String input)
Converts a
String to a byte[]. |
static <S> Map<String,S> |
toObjectMap(Map<String,byte[]> input,
Function<byte[],S> converter)
|
static String |
toString(byte[] input)
Converts a byte[] input to a String.
|
static Map<String,String> |
toStringMap(Map<String,byte[]> input)
|
abstract CompletableFuture<Boolean> |
wipe()
Removes all the IDs from the storage across all namespaces.
|
public static final String DEFAULT_NAMESPACE
protected BulletConfig config
public StorageManager(BulletConfig config)
BulletConfig
.config
- The config to use.public <T,R> CompletableFuture<Map<String,V>> getAll(Criteria<T,R> criteria)
Criteria
from the storage as the type of the storage.T
- The type of query taken by the Criteria
.R
- The type of result returned by the Criteria
.criteria
- The Criteria
understood by this storage.CompletableFuture
that resolves to a Map
of IDs to their stored values.public <T,R> CompletableFuture<R> retrieveAll(Criteria<T,R> criteria)
T
- The type of query taken by the Criteria
.R
- The type of result returned by the Criteria
.criteria
- The Criteria
understood by this storage.CompletableFuture
that resolves to the type returned by the Criteria
.public <T,R> CompletableFuture<R> apply(Criteria<T,R> criteria, T query)
protected String getDefaultNamespace()
public CompletableFuture<Boolean> clear()
CompletableFuture
that resolves to true if the wipe was successful.public CompletableFuture<Boolean> clear(int partition)
partition
- The partition to clear.CompletableFuture
that resolves to true if the wipe was successful.public CompletableFuture<Boolean> clear(Set<String> ids)
ids
- The set of IDs to remove from the storage for the default namespace.CompletableFuture
that resolves to true if the wipe was successful.public CompletableFuture<Boolean> put(String id, V data)
Serializable
object for a given String identifier in the default namespace.id
- The ID to store this data under.data
- The object to store as the data.CompletableFuture
that resolves to true if the store succeeded.public CompletableFuture<V> get(String id)
Serializable
object.id
- The ID of the data.CompletableFuture
that resolves to the data.public CompletableFuture<Boolean> putAll(Map<String,V> data)
data
- The map of IDs and values to store.CompletableFuture
that resolves to true if the storage was completely successful.public CompletableFuture<Map<String,V>> getAll()
Map
of IDs to their
Serializable
values.CompletableFuture
that resolves to a Map
of IDs to their stored values.public CompletableFuture<Map<String,V>> getAll(Set<String> ids)
Map
of IDs to
their Serializable
values.ids
- The Set
of IDs to retrieve.CompletableFuture
that resolves to a Map
of IDs to their stored values.public CompletableFuture<V> remove(String id)
Serializable
object in the default
namespace.id
- The ID of the data.CompletableFuture
that resolves to the data.public int numberOfPartitions()
public CompletableFuture<Map<String,V>> getPartition(int partition)
partition
- The partition number to return.CompletableFuture
that resolves to a Map
of IDs to their stored values as byte
arrays or null if no data is present.public CompletableFuture<Boolean> repartition(int newPartitionCount)
newPartitionCount
- The new number of partitions to use.CompletableFuture
that resolves to true if the repartitioning was successful.public CompletableFuture<Boolean> putString(String id, String value)
id
- The ID to store this value under.value
- The object to store as the value.CompletableFuture
that resolves to true if the store succeeded.public CompletableFuture<String> getString(String id)
id
- The ID of the data.CompletableFuture
that resolves to the data.public CompletableFuture<Boolean> putAllString(Map<String,String> data)
data
- The map of IDs and values to store.CompletableFuture
that resolves to true if the storage was completely successful.public CompletableFuture<Map<String,String>> getAllString()
Map
of IDs to their String values.CompletableFuture
that resolves to a Map
of IDs to their String values.public CompletableFuture<Map<String,String>> getAllString(Set<String> ids)
Map
of IDs to
their String values.ids
- The Set
of IDs to retrieve.CompletableFuture
that resolves to a Map
of IDs to their String values.public CompletableFuture<String> removeString(String id)
id
- The ID of the data.CompletableFuture
that resolves to the data.public static int hash(String key, int numberOfPartitions)
key
- The String key to hash.numberOfPartitions
- The number of partitions. Must be zero or positive.public static StorageManager from(BulletConfig config)
StorageManager
instance using the class specified in the config file.config
- The non-null BulletConfig
containing the class name and StorageManager settings.public CompletableFuture<Boolean> putString(String namespace, String id, String value)
namespace
- The namespace to store this value under.id
- The ID to store this value under.value
- The object to store as the value.CompletableFuture
that resolves to true if the store succeeded.public CompletableFuture<String> getString(String namespace, String id)
namespace
- The namespace of the data.id
- The ID of the data.CompletableFuture
that resolves to the data.public CompletableFuture<Boolean> putAllString(String namespace, Map<String,String> data)
namespace
- The namespace to store the entries in.data
- The map of IDs and values to store.CompletableFuture
that resolves to true if the storage was completely successful.public CompletableFuture<Map<String,String>> getAllString(String namespace)
Map
of IDs to their String values.namespace
- The namespace to retrieve from.CompletableFuture
that resolves to a Map
of IDs to their String values.public CompletableFuture<Map<String,String>> getAllString(String namespace, Set<String> ids)
Map
of IDs to
their String values.namespace
- The namespace of the IDs.ids
- The Set
of IDs to retrieve.CompletableFuture
that resolves to a Map
of IDs to their String values.public CompletableFuture<String> removeString(String namespace, String id)
namespace
- The namespace of the data.id
- The ID of the data.CompletableFuture
that resolves to the data.public CompletableFuture<Map<String,String>> getPartitionString(String namespace, int partition)
getAll(String)
namespace
- The namespace to retrieve the IDs from.partition
- The partition number to return.CompletableFuture
that resolves to a Map
of IDs to their stored values as Strings
or null if no data is present.public static byte[] toBytes(String input)
String
to a byte[].input
- The String input.public static String toString(byte[] input)
input
- The byte[] input.public static Map<String,String> toStringMap(Map<String,byte[]> input)
input
- The String to byte[] map.public static Map<String,byte[]> toByteArrayMap(Map<String,String> input)
input
- The String to byte[] map.public void close()
close
in interface AutoCloseable
protected abstract CompletableFuture<Boolean> putRaw(String namespace, String id, byte[] value)
namespace
- The namespace to store the entry in.id
- The unique ID to represent this entry.value
- The value to store for this entry.CompletableFuture
that resolves to true if the storage was successful.protected abstract CompletableFuture<byte[]> getRaw(String namespace, String id)
namespace
- The namespace to retrieve from.id
- The unique ID to retrieve from the storage.CompletableFuture
that resolves to the byte[] value for this ID or null if it does not exist.protected CompletableFuture<Boolean> putAllRaw(String namespace, Map<String,byte[]> data)
putRaw(String, String, byte[])
in parallel.namespace
- The namespace to store the entries in.data
- The map of IDs and values to store.CompletableFuture
that resolves to true if the storage was completely successful.protected abstract CompletableFuture<Map<String,byte[]>> getAllRaw(String namespace)
namespace
- The namespace to retrieve from.CompletableFuture
that resolves to a Map
of IDs to their stored values as byte[].protected CompletableFuture<Map<String,byte[]>> getAllRaw(String namespace, Set<String> ids)
getRaw(String, String)
repeatedly on each of the given IDs in parallel.namespace
- The namespace of the IDs.ids
- The Set
of IDs to retrieve.CompletableFuture
that resolves to a Map
of IDs to their stored values as byte[].protected abstract CompletableFuture<byte[]> removeRaw(String namespace, String id)
namespace
- The namespace of the ID.id
- The unique ID to remove from the storage.CompletableFuture
that resolves to the byte[] value for this ID or null if it does not exist.public abstract CompletableFuture<Boolean> wipe()
CompletableFuture
that resolves to true if the wipe was successful.public abstract CompletableFuture<Boolean> clear(String namespace)
namespace
- The namespace to clear.CompletableFuture
that resolves to true if the wipe was successful.public abstract CompletableFuture<Boolean> clear(String namespace, Set<String> ids)
namespace
- The namespace that has these IDs.ids
- The set of IDs to remove from the storage for the given namespace.CompletableFuture
that resolves to true if the wipe was successful and throws otherwise.public CompletableFuture<Boolean> put(String namespace, String id, V value)
Serializable
object for a given String identifier in the given namespace.namespace
- The namespace to store this value under.id
- The ID to store this value under.value
- The object to store as the value.CompletableFuture
that resolves to true if the store succeeded.public CompletableFuture<V> get(String namespace, String id)
Serializable
object.namespace
- The namespace of the data.id
- The ID of the data.CompletableFuture
that resolves to the data.public CompletableFuture<Boolean> putAll(String namespace, Map<String,V> data)
namespace
- The namespace to store the entries in.data
- The map of IDs and values to store.CompletableFuture
that resolves to true if the storage was completely successful.public CompletableFuture<Map<String,V>> getAll(String namespace)
Map
of IDs to their
Serializable
values.namespace
- The namespace to retrieve from.CompletableFuture
that resolves to a Map
of IDs to their stored values.public CompletableFuture<Map<String,V>> getAll(String namespace, Set<String> ids)
Map
of IDs to
their Serializable
values.namespace
- The namespace of the IDs.ids
- The Set
of IDs to retrieve.CompletableFuture
that resolves to a Map
of IDs to their stored values.public CompletableFuture<V> remove(String namespace, String id)
Serializable
object in the given
namespace.namespace
- The namespace of the data.id
- The ID of the data.CompletableFuture
that resolves to the data.public int numberOfPartitions(String namespace)
namespace
- The namespace whose partitions are being asked for.protected CompletableFuture<Map<String,byte[]>> getPartitionRaw(String namespace, int partition)
getAllRaw(String)
namespace
- The namespace to retrieve the IDs from.partition
- The partition number to return.CompletableFuture
that resolves to a Map
of IDs to their stored values as byte
arrays or null if no data is present.public CompletableFuture<Map<String,V>> getPartition(String namespace, int partition)
getAll(String)
namespace
- The namespace to retrieve the IDs from.partition
- The partition number to return.CompletableFuture
that resolves to a Map
of IDs to their stored values as Serializable
objects or null if no data is present.public CompletableFuture<Boolean> clear(String namespace, int partition)
namespace
- The namespace for the partition.partition
- The partition to clear.CompletableFuture
that resolves to true if the wipe was successful.public CompletableFuture<Boolean> repartition(String namespace, int newPartitionCount)
namespace
- The namespace to repartition.newPartitionCount
- The new number of partitions to use.CompletableFuture
that resolves to true if the repartitioning was successful.protected V convert(byte[] bytes)
byte[]
to a type of the given object. By default, uses SerializerDeserializer
to
deserialize using Java deserialization.bytes
- The byte[] to convert.protected byte[] convert(V object)
byte[]
. By default, uses SerializerDeserializer
to
serialize using Java serialization.object
- The object to convert.public static <S> Map<String,byte[]> fromObjectMap(Map<String,S> input, Function<S,byte[]> converter)
S
- The type to convert from.input
- The String to the given type map.converter
- The Function
that converts from the given type to byte[].public static <S> Map<String,S> toObjectMap(Map<String,byte[]> input, Function<byte[],S> converter)
S
- The type to convert to.input
- The String to byte[] map.converter
- The Function
that converts from byte[] to the given type.Copyright © 2021. All rights reserved.