org.mapdb
Class Bind
- java.lang.Object
-
- org.mapdb.Bind
-
public final class Bind extends java.lang.ObjectBinding is simple yet powerful way to keep secondary collection synchronized with primary collection. Primary collection provides notification on updates and secondary collection is modified accordingly. This way MapDB provides secondary indexes, values and keys. It also supports less usual scenarios such as histograms, inverse lookup index (on maps), group counters and so on. There are two things to keep on mind when using binding: * Binding is not persistent, so it needs to be restored every time store is reopened. If you modify primary collection before binding is restored, secondary collection does not get updated and becomes inconsistent. * If secondary collection is empty, binding will recreate its content based on primary collection. If there is even single item on secondary collection, binding assumes it is consistent and leaves it as its. Any thread-safe collection can be used as secondary (not just collections provided by MapDB). This gives great flexibility for modeling and scaling your data. For example primary data can be stored in durable DB with transactions and large secondary indexes may be stored in other faster non-durable DB. Or primary collection may be stored on disk and smaller secondary index (such as category counters) can be stored in memory for faster lookups. Also you may use ordinaryjava.util.*collections (if they are thread safe) to get additional speed. There are many [code examples](https://github.com/jankotek/MapDB/tree/master/src/test/java/examples) how Collection Binding can be used. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static interfaceBind.MapListener<K,V>Listener called whenMapis modified.static interfaceBind.MapWithModificationListener<K,V>Primary Maps must provide notifications when it is modified.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static <K,V,C> voidhistogram(Bind.MapWithModificationListener<K,V> primary, java.util.concurrent.ConcurrentMap<C,java.lang.Long> histogram, Fun.Function2<C,K,V> entryToCategory)Binds Secondary Map so it it creates [histogram](http://en.wikipedia.org/wiki/Histogram) from data in Primary Map.static <K,V> voidmapInverse(Bind.MapWithModificationListener<K,V> primary, java.util.Map<V,K> inverse)Binds Secondary Set so it contains inverse mapping to Primary Map: Primary Value will become Secondary Key.static <K,V> voidmapInverse(Bind.MapWithModificationListener<K,V> primary, java.util.Set<java.lang.Object[]> inverse)Binds Secondary Set so it contains inverse mapping to Primary Map: Primary Value will become Secondary Key.static <K,V> voidmapPutAfterDelete(Bind.MapWithModificationListener<K,V> primary, Bind.MapWithModificationListener<K,V> secondary, boolean overwriteSecondary)After key is removed from primary for some reason (map.remove, or expiration inHTreeMap), it gets moved into secondary collection.static <K,V,K2> voidsecondaryKey(Bind.MapWithModificationListener<K,V> map, java.util.Map<K2,K> secondary, Fun.Function2<K2,K,V> fun)Binds Secondary Set so it contains Secondary Key (Index).static <K,V,K2> voidsecondaryKey(Bind.MapWithModificationListener<K,V> map, java.util.Set<java.lang.Object[]> secondary, Fun.Function2<K2,K,V> fun)Binds Secondary Set so it contains Secondary Key (Index).static <K,V,K2> voidsecondaryKeys(Bind.MapWithModificationListener<K,V> map, java.util.Set<java.lang.Object[]> secondary, Fun.Function2<K2[],K,V> fun)Binds Secondary Set so it contains Secondary Key (Index).static <K,V,V2> voidsecondaryValue(Bind.MapWithModificationListener<K,V> map, java.util.Map<K,V2> secondary, Fun.Function2<V2,K,V> fun)Binds Secondary Map so that it contains Key from Primary Map and custom Value.static <K,V,V2> voidsecondaryValues(Bind.MapWithModificationListener<K,V> map, java.util.Set<java.lang.Object[]> secondary, Fun.Function2<V2[],K,V> fun)Binds Secondary Map so that it contains Key from Primary Map and custom Value.static <K,V> voidsize(Bind.MapWithModificationListener<K,V> map, Atomic.Long sizeCounter)BindsAtomic.Longto Primary Map so the Atomic.Long contains size of Map.
-
-
-
Method Detail
-
size
public static <K,V> void size(Bind.MapWithModificationListener<K,V> map, Atomic.Long sizeCounter)
BindsAtomic.Longto Primary Map so the Atomic.Long contains size of Map.Atomic.Longis incremented on each insert and decremented on each entry removal. MapDB collections usually do not keep their size, but require complete traversal to count items. IfAtomic.Longhas zero value, it will be updated with value frommap.size()and than bind to map. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. NOTE:BTreeMapandHTreeMapalready supports this directly as optional parameter namedcounter. In that case all calls toMap.size()are forwarded to underlying counter. Check parameters atDB.hashMapCreate(String)andDB.treeMapCreate(String)- Type Parameters:
K- type of key in mapV- type of value in map- Parameters:
map- primary map whose size needs to be trackedsizeCounter- number updated when Map Entry is added or removed.
-
secondaryValue
public static <K,V,V2> void secondaryValue(Bind.MapWithModificationListener<K,V> map, java.util.Map<K,V2> secondary, Fun.Function2<V2,K,V> fun)
Binds Secondary Map so that it contains Key from Primary Map and custom Value. Secondary Value is updated every time Primary Map is modified. If Secondary Map is empty its content will be recreated from Primary Map. This binding is not persistent. You need to restore it every time store is reopened. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. Type params:- Type Parameters:
K- - key type in primary and Secondary MapV- - value type in Primary MapV2- - value type in Secondary Map- Parameters:
map- Primary Mapsecondary- Secondary Map with customfun- function which calculates secondary value from primary key and value
-
secondaryValues
public static <K,V,V2> void secondaryValues(Bind.MapWithModificationListener<K,V> map, java.util.Set<java.lang.Object[]> secondary, Fun.Function2<V2[],K,V> fun)
Binds Secondary Map so that it contains Key from Primary Map and custom Value. Secondary Value is updated every time Primary Map is modified. If Secondary Map is empty its content will be recreated from Primary Map. This binding is not persistent. You need to restore it every time store is reopened. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. Type params:- Type Parameters:
K- - key type in primary and Secondary MapV- - value type in Primary MapV2- - value type in Secondary Map .- Parameters:
map- Primary Mapsecondary- Secondary Map with customfun- function which calculates secondary values from primary key and value
-
secondaryKey
public static <K,V,K2> void secondaryKey(Bind.MapWithModificationListener<K,V> map, java.util.Set<java.lang.Object[]> secondary, Fun.Function2<K2,K,V> fun)
Binds Secondary Set so it contains Secondary Key (Index). Usefull if you need to lookup Keys from Primary Map by custom criteria. Other use is for reverse lookup To lookup keys in Secondary Set useFun.filter(java.util.NavigableSet, Object[])If Secondary Set is empty its content will be recreated from Primary Map. This binding is not persistent. You need to restore it every time store is reopened. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. Type params:- Type Parameters:
K- - Key in Primary MapV- - Value in Primary MapK2- - Secondary- Parameters:
map- primary mapsecondary- secondary setfun- function which calculates Secondary Key from Primary Key and Value
-
secondaryKey
public static <K,V,K2> void secondaryKey(Bind.MapWithModificationListener<K,V> map, java.util.Map<K2,K> secondary, Fun.Function2<K2,K,V> fun)
Binds Secondary Set so it contains Secondary Key (Index). Usefull if you need to lookup Keys from Primary Map by custom criteria. Other use is for reverse lookup If Secondary Set is empty its content will be recreated from Primary Map. This binding is not persistent. You need to restore it every time store is reopened. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. Type params:- Type Parameters:
K- - Key in Primary MapV- - Value in Primary MapK2- - Secondary- Parameters:
map- primary mapsecondary- secondary setfun- function which calculates Secondary Key from Primary Key and Value
-
secondaryKeys
public static <K,V,K2> void secondaryKeys(Bind.MapWithModificationListener<K,V> map, java.util.Set<java.lang.Object[]> secondary, Fun.Function2<K2[],K,V> fun)
Binds Secondary Set so it contains Secondary Key (Index). Useful if you need to lookup Keys from Primary Map by custom criteria. Other use is for reverse lookup To lookup keys in Secondary Set useFun.filter(java.util.NavigableSet, Object[])} If Secondary Set is empty its content will be recreated from Primary Map. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. Type params:- Type Parameters:
K- - Key in Primary MapV- - Value in Primary MapK2- - Secondary- Parameters:
map- primary mapsecondary- secondary setfun- function which calculates Secondary Keys from Primary Key and Value
-
mapInverse
public static <K,V> void mapInverse(Bind.MapWithModificationListener<K,V> primary, java.util.Set<java.lang.Object[]> inverse)
Binds Secondary Set so it contains inverse mapping to Primary Map: Primary Value will become Secondary Key. This is useful for creating bi-directional Maps. To lookup keys in Secondary Set useFun.filter(java.util.NavigableSet, Object[])If Secondary Set is empty its content will be recreated from Primary Map. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. Type params:- Type Parameters:
K- - Key in Primary Map and Second Value in Secondary SetV- - Value in Primary Map and Primary Value in Secondary Set- Parameters:
primary- Primary Map for which inverse mapping will be createdinverse- Secondary Set which will contain inverse mapping
-
mapInverse
public static <K,V> void mapInverse(Bind.MapWithModificationListener<K,V> primary, java.util.Map<V,K> inverse)
Binds Secondary Set so it contains inverse mapping to Primary Map: Primary Value will become Secondary Key. This is useful for creating bi-directional Maps. In this case some data may be lost, if there are duplicated primary values. It is recommended to use multimap:NavigableSet<Fun.Tuple2<V,K>>which handles value duplicities. UsemapInverse(org.mapdb.Bind.MapWithModificationListener, java.util.Map)If Secondary Set is empty its content will be recreated from Primary Map. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. Type params:- Type Parameters:
K- - Key in Primary Map and Second Value in Secondary SetV- - Value in Primary Map and Primary Value in Secondary Set- Parameters:
primary- Primary Map for which inverse mapping will be createdinverse- Secondary Set which will contain inverse mapping
-
histogram
public static <K,V,C> void histogram(Bind.MapWithModificationListener<K,V> primary, java.util.concurrent.ConcurrentMap<C,java.lang.Long> histogram, Fun.Function2<C,K,V> entryToCategory)
Binds Secondary Map so it it creates [histogram](http://en.wikipedia.org/wiki/Histogram) from data in Primary Map. Histogram keeps count how many items are in each category. This method takes function which defines in what category each Primary Map entry is in. If Secondary Map is empty its content will be recreated from Primary Map. NOTE: Binding just installs Modification Listener on primary collection. Binding itself is not persistent and has to be restored after primary collection is loaded. Data contained in secondary collection are persistent. Type params:- Type Parameters:
K- - Key type in primary mapV- - Value type in primary mapC- - Category type- Parameters:
primary- Primary Map to create histogram forhistogram- Secondary Map to create histogram for, key is Category, value is number of items in categoryentryToCategory- returns Category in which entry from Primary Map belongs to.
-
mapPutAfterDelete
public static <K,V> void mapPutAfterDelete(Bind.MapWithModificationListener<K,V> primary, Bind.MapWithModificationListener<K,V> secondary, boolean overwriteSecondary)
After key is removed from primary for some reason (map.remove, or expiration inHTreeMap), it gets moved into secondary collection. This does not apply to updated values where key remains unchanged (put(), replace()..)- Type Parameters:
K- keyV- value- Parameters:
primary- map from which data are removed by usersecondary- map which gets automatically updated with data removed from primaryoverwriteSecondary- if true any data in secondary will be overwritten. If false only non-existing keys will be inserted (put() versus putIfAbsent();
-
-
DataMelt 3.0 © DataMelt by jWork.ORG