Class NitriteStateRepository<V>
- java.lang.Object
-
- de.otto.synapse.state.NitriteStateRepository<V>
-
- Type Parameters:
V-
- All Implemented Interfaces:
StateRepository<V>,java.io.Closeable,java.lang.AutoCloseable
public class NitriteStateRepository<V> extends java.lang.Object implements StateRepository<V>, java.io.Closeable
A StateRepository with extra functionality for secondary indexes, queries, sorting and paging of results.This implementation is using a Nitrite Database to store the entities either on heap, off heap or in a file system.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNitriteStateRepository.Builder<V>
-
Constructor Summary
Constructors Constructor Description NitriteStateRepository(java.lang.String name, java.lang.Class<V> valueType, java.util.Set<java.lang.String> indexedFields)NitriteStateRepository(java.lang.String name, java.lang.Class<V> valueType, java.util.Set<java.lang.String> indexedFields, org.dizitart.no2.NitriteBuilder builder)
-
Method Summary
Modifier and Type Method Description static <V> NitriteStateRepository.Builder<V>builder(java.lang.Class<V> clazz)voidclear()Removes all of the mappings from this repository (optional operation).voidclose()Closes theStateRepository.java.util.Optional<V>compute(java.lang.String key, java.util.function.BiFunction<? super java.lang.String,? super java.util.Optional<V>,? extends V> remappingFunction)Attempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).voidconsumeAll(java.util.function.BiConsumer<? super java.lang.String,? super V> consumer)Computes each entry within the repository.java.util.Collection<V>findBy(java.lang.String key, java.lang.Object value)java.util.Collection<V>findBy(org.dizitart.no2.Filter filter)java.util.Collection<V>findBy(org.dizitart.no2.Filter filter, org.dizitart.no2.FindOptions findOptions)java.util.Collection<V>findBy(org.dizitart.no2.FindOptions findOptions)java.util.Optional<V>get(java.lang.String key)Returns the optional value to which the specified key is mapped, orOptional.empty()if this repository contains no mapping for the key.java.lang.StringgetName()The unique name of theStateRepository.java.util.Set<java.lang.String>keySet()Returns an immutable set of the keys in this repository.java.util.Optional<V>put(java.lang.String key, V value)Associates the specified value with the specified key in this repository.java.util.Optional<V>remove(java.lang.String key)Removes the mapping for a key from this repository if it is present.longsize()Returns the number of key-value mappings in this repository.
-
-
-
Constructor Detail
-
NitriteStateRepository
public NitriteStateRepository(java.lang.String name, java.lang.Class<V> valueType, java.util.Set<java.lang.String> indexedFields, org.dizitart.no2.NitriteBuilder builder)
-
NitriteStateRepository
public NitriteStateRepository(java.lang.String name, java.lang.Class<V> valueType, java.util.Set<java.lang.String> indexedFields)
-
-
Method Detail
-
getName
public java.lang.String getName()
Description copied from interface:StateRepositoryThe unique name of theStateRepository.Using the same name for multiple repositories will lead to
- Specified by:
getNamein interfaceStateRepository<V>- Returns:
- name
-
keySet
public java.util.Set<java.lang.String> keySet()
Description copied from interface:StateRepositoryReturns an immutable set of the keys in this repository.- Specified by:
keySetin interfaceStateRepository<V>- Returns:
- a set view of the keys contained in this repository
-
findBy
public java.util.Collection<V> findBy(org.dizitart.no2.Filter filter)
-
findBy
public java.util.Collection<V> findBy(org.dizitart.no2.FindOptions findOptions)
-
findBy
public java.util.Collection<V> findBy(org.dizitart.no2.Filter filter, org.dizitart.no2.FindOptions findOptions)
-
findBy
public java.util.Collection<V> findBy(java.lang.String key, java.lang.Object value)
-
get
public java.util.Optional<V> get(java.lang.String key)
Description copied from interface:StateRepositoryReturns the optional value to which the specified key is mapped, orOptional.empty()if this repository contains no mapping for the key.More formally, if this repository contains a mapping from a key
kto a valuevsuch that(key==null ? k==null : key.equals(k)), then this method returnsOptional.of(v); otherwise it returnsOptional.empty(). (There can be at most one such mapping.)- Specified by:
getin interfaceStateRepository<V>- Parameters:
key- the key whose associated value is to be returned- Returns:
- the Optional containing the value to which the specified key is mapped, or
Optional.empty()if this repository contains no mapping for the key
-
consumeAll
public void consumeAll(java.util.function.BiConsumer<? super java.lang.String,? super V> consumer)
Description copied from interface:StateRepositoryComputes each entry within the repository.- Specified by:
consumeAllin interfaceStateRepository<V>- Parameters:
consumer- the consumer that will be applied on each repository entry
-
put
public java.util.Optional<V> put(java.lang.String key, V value)
Description copied from interface:StateRepositoryAssociates the specified value with the specified key in this repository. If the repository previously contained a mapping for the key, the old value is replaced by the specified value. (A repository m is said to contain a mapping for a key k if and only ifm.get(k)would return a non-empty value.)- Specified by:
putin interfaceStateRepository<V>- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Returns:
- the previous value associated with 'key', or 'Optional.empty()' if there was no mapping for 'key'
-
compute
public java.util.Optional<V> compute(java.lang.String key, java.util.function.BiFunction<? super java.lang.String,? super java.util.Optional<V>,? extends V> remappingFunction)
Description copied from interface:StateRepositoryAttempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping). For example, to either create or append aStringmsg to a value mapping:repository.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))If the function returns
null, the mapping is removed (or remains absent if initially absent). If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.The default implementation is equivalent to performing the following steps for this
repository, then returning the current value ornullif absent:OptionalV> oldValue = repository.get(key); V newValue = remappingFunction.apply(key, oldValue); if (!oldValue.isEmpty()) { if (newValue != null) repository.put(key, newValue); else repository.remove(key); } else { if (newValue != null) repository.put(key, newValue); else return Optional.empty(); }- Specified by:
computein interfaceStateRepository<V>- Parameters:
key- key with which the specified value is to be associatedremappingFunction- the function to compute a value- Returns:
- the Optional containing the new value associated with the specified key, or Optional.empty() if none
-
remove
public java.util.Optional<V> remove(java.lang.String key)
Description copied from interface:StateRepositoryRemoves the mapping for a key from this repository if it is present. More formally, if this repository contains a mapping from key 'k' to value 'v' such that(key==null ? k==null : key.equals(k)), that mapping is removed. (The repository can contain at most one such mapping.)Returns the optional value to which this repository previously associated the key, or 'Optional.empty()' if the repository contained no mapping for the key.
The repository will not contain a mapping for the specified key once the call returns.
- Specified by:
removein interfaceStateRepository<V>- Parameters:
key- key whose mapping is to be removed from the repository- Returns:
- the optional previous value associated with 'key', or 'Optional.empty()' if there was no mapping for 'key'.
-
clear
public void clear()
Description copied from interface:StateRepositoryRemoves all of the mappings from this repository (optional operation). The repository will be empty after this call returns.- Specified by:
clearin interfaceStateRepository<V>
-
size
public long size()
Description copied from interface:StateRepositoryReturns the number of key-value mappings in this repository. If the repository contains more than 'Integer.MAX_VALUE' elements, returns 'Integer.MAX_VALUE'.- Specified by:
sizein interfaceStateRepository<V>- Returns:
- the number of key-value mappings in this repository
-
close
public void close()
Description copied from interface:StateRepositoryCloses theStateRepository.Depending on the implementation of the interface, it might be required to close() the repository on shutdown in order to prevent data loss.
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceStateRepository<V>
-
builder
public static <V> NitriteStateRepository.Builder<V> builder(java.lang.Class<V> clazz)
-
-