org.mapdb
Class DB

java.lang.Object
  extended by org.mapdb.DB

public class DB
extends Object

A database with easy access to named maps and other collections.

Author:
Jan Kotek

Nested Class Summary
 class DB.BTreeMapMaker
           
 class DB.BTreeSetMaker
           
 class DB.HTreeMapMaker
           
 class DB.HTreeSetMaker
           
protected static class DB.IdentityWrapper
           
 
Field Summary
protected  SortedMap<String,Object> catalog
          view over named records
protected  Engine engine
          Engine which provides persistence for this DB
protected  Map<String,WeakReference<?>> namesInstanciated
          already loaded named collections.
protected  Map<DB.IdentityWrapper,String> namesLookup
           
protected  boolean strictDBGet
           
 
Constructor Summary
DB(Engine engine)
          Construct new DB.
DB(Engine engine, boolean strictDBGet, boolean disableLocks)
           
 
Method Summary
<A> A
catGet(String name)
           
<A> A
catGet(String name, A init)
           
<A> A
catPut(String name, A value)
           
<A> A
catPut(String name, A value, A retValueIfNull)
           
 void checkNameNotExists(String name)
          Checks that object with given name does not exist yet.
 void checkNotClosed()
           
 void checkShouldCreate(String name)
           
 void checkType(String type, String expected)
           
 void close()
          Closes database.
 void commit()
          Commit changes made on collections loaded by this DB
 void compact()
          Perform storage maintenance.
 Atomic.Boolean createAtomicBoolean(String name, boolean initValue)
           
 Atomic.Integer createAtomicInteger(String name, int initValue)
           
 Atomic.Long createAtomicLong(String name, long initValue)
           
 Atomic.String createAtomicString(String name, String initValue)
           
<E> Atomic.Var<E>
createAtomicVar(String name, E initValue, Serializer<E> serializer)
           
<E> BlockingQueue<E>
createCircularQueue(String name, Serializer<E> serializer, long size)
           
protected
<K,V> HTreeMap<K,V>
createHashMap(DB.HTreeMapMaker m)
          Creates new HashMap with more specific arguments
 DB.HTreeMapMaker createHashMap(String name)
          Returns new builder for HashMap with given name
protected
<K> Set<K>
createHashSet(DB.HTreeSetMaker m)
           
 DB.HTreeSetMaker createHashSet(String name)
          Creates new HashSet
<E> BlockingQueue<E>
createQueue(String name, Serializer<E> serializer, boolean useLocks)
           
<E> BlockingQueue<E>
createStack(String name, Serializer<E> serializer, boolean useLocks)
           
protected
<K,V> BTreeMap<K,V>
createTreeMap(DB.BTreeMapMaker m)
           
 DB.BTreeMapMaker createTreeMap(String name)
          Returns new builder for TreeMap with given name
<K> NavigableSet<K>
createTreeSet(DB.BTreeSetMaker m)
           
 DB.BTreeSetMaker createTreeSet(String name)
          Creates new TreeSet.
 void delete(String name)
          delete record/collection with given name
 boolean exists(String name)
           
protected
<K> BTreeKeySerializer<K>
fillNulls(BTreeKeySerializer<K> keySerializer)
          Replace nulls in tuple serializers with default (Comparable) values
<E> E
get(String name)
          return record with given name or null if name does not exist
 Map<String,Object> getAll()
          return map of all named collections/records
 Atomic.Boolean getAtomicBoolean(String name)
           
 Atomic.Integer getAtomicInteger(String name)
           
 Atomic.Long getAtomicLong(String name)
           
 Atomic.String getAtomicString(String name)
           
<E> Atomic.Var<E>
getAtomicVar(String name)
           
 SortedMap<String,Object> getCatalog()
          Get Name Catalog.
<E> BlockingQueue<E>
getCircularQueue(String name)
           
 Serializer getDefaultSerializer()
           
 Engine getEngine()
           
 Object getFromWeakCollection(String name)
          All collections are weakly referenced to prevent two instances of the same collection in memory.
<K,V> HTreeMap<K,V>
getHashMap(String name)
          Opens existing or creates new Hash Tree Map.
<K,V> HTreeMap<K,V>
getHashMap(String name, Fun.Function1<V,K> valueCreator)
          Opens existing or creates new Hash Tree Map.
<K> Set<K>
getHashSet(String name)
          Opens existing or creates new Hash Tree Set.
 String getNameForObject(Object obj)
          returns name for this object, if it has name and was instanciated by this DB
<E> BlockingQueue<E>
getQueue(String name)
           
<E> BlockingQueue<E>
getStack(String name)
           
<K,V> BTreeMap<K,V>
getTreeMap(String name)
          Opens existing or creates new B-linked-tree Map.
<K> NavigableSet<K>
getTreeSet(String name)
          Opens existing or creates new B-linked-tree Set.
 boolean isClosed()
           
<V> V
namedPut(String name, Object ret)
           
protected  void reinit()
           
 void rename(String oldName, String newName)
          rename named record into newName
 void rollback()
          Rollback changes made on collections loaded by this DB
 DB snapshot()
          Make readonly snapshot view of DB and all of its collection Collections loaded by this instance are not affected (are still mutable).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

strictDBGet

protected final boolean strictDBGet

engine

protected Engine engine
Engine which provides persistence for this DB


namesInstanciated

protected Map<String,WeakReference<?>> namesInstanciated
already loaded named collections. It is important to keep collections as singletons, because of 'in-memory' locking


namesLookup

protected Map<DB.IdentityWrapper,String> namesLookup

catalog

protected SortedMap<String,Object> catalog
view over named records

Constructor Detail

DB

public DB(Engine engine)
Construct new DB. It is just thin layer over Engine which does the real work.

Parameters:
engine -

DB

public DB(Engine engine,
          boolean strictDBGet,
          boolean disableLocks)
Method Detail

reinit

protected void reinit()

catGet

public <A> A catGet(String name,
                    A init)

catGet

public <A> A catGet(String name)

catPut

public <A> A catPut(String name,
                    A value)

catPut

public <A> A catPut(String name,
                    A value,
                    A retValueIfNull)

getNameForObject

public String getNameForObject(Object obj)
returns name for this object, if it has name and was instanciated by this DB


getHashMap

public <K,V> HTreeMap<K,V> getHashMap(String name)
Opens existing or creates new Hash Tree Map. This collection perform well under concurrent access. Is best for large keys and large values.

Parameters:
name - of the map
Returns:
map

getHashMap

public <K,V> HTreeMap<K,V> getHashMap(String name,
                                      Fun.Function1<V,K> valueCreator)
Opens existing or creates new Hash Tree Map. This collection perform well under concurrent access. Is best for large keys and large values.

Parameters:
name - of map
valueCreator - if value is not found, new is created and placed into map.
Returns:
map

namedPut

public <V> V namedPut(String name,
                      Object ret)

createHashMap

public DB.HTreeMapMaker createHashMap(String name)
Returns new builder for HashMap with given name

Parameters:
name - of map to create
Returns:
maker, call `.make()` to create map
Throws:
IllegalArgumentException - if name is already used

createHashMap

protected <K,V> HTreeMap<K,V> createHashMap(DB.HTreeMapMaker m)
Creates new HashMap with more specific arguments

Returns:
newly created map
Throws:
IllegalArgumentException - if name is already used

getHashSet

public <K> Set<K> getHashSet(String name)
Opens existing or creates new Hash Tree Set.

Parameters:
name - of the Set
Returns:
set

createHashSet

public DB.HTreeSetMaker createHashSet(String name)
Creates new HashSet

Parameters:
name - of set to create

createHashSet

protected <K> Set<K> createHashSet(DB.HTreeSetMaker m)

getTreeMap

public <K,V> BTreeMap<K,V> getTreeMap(String name)
Opens existing or creates new B-linked-tree Map. This collection performs well under concurrent access. Only trade-off are deletes, which causes tree fragmentation. It is ordered and best suited for small keys and values.

Parameters:
name - of map
Returns:
map

createTreeMap

public DB.BTreeMapMaker createTreeMap(String name)
Returns new builder for TreeMap with given name

Parameters:
name - of map to create
Returns:
maker, call `.make()` to create map
Throws:
IllegalArgumentException - if name is already used

createTreeMap

protected <K,V> BTreeMap<K,V> createTreeMap(DB.BTreeMapMaker m)

fillNulls

protected <K> BTreeKeySerializer<K> fillNulls(BTreeKeySerializer<K> keySerializer)
Replace nulls in tuple serializers with default (Comparable) values

Parameters:
keySerializer - with nulls
Returns:
keySerializers which does not contain any nulls

getCatalog

public SortedMap<String,Object> getCatalog()
Get Name Catalog. It is metatable which contains information about named collections and records. Each collection constructor takes number of parameters, this map contains those parameters. _Note:_ Do not modify this map, unless you know what you are doing!

Returns:
Name Catalog

getTreeSet

public <K> NavigableSet<K> getTreeSet(String name)
Opens existing or creates new B-linked-tree Set.

Parameters:
name - of set
Returns:
set

createTreeSet

public DB.BTreeSetMaker createTreeSet(String name)
Creates new TreeSet.

Parameters:
name - of set to create
Returns:
maker used to construct set
Throws:
IllegalArgumentException - if name is already used

createTreeSet

public <K> NavigableSet<K> createTreeSet(DB.BTreeSetMaker m)

getQueue

public <E> BlockingQueue<E> getQueue(String name)

createQueue

public <E> BlockingQueue<E> createQueue(String name,
                                        Serializer<E> serializer,
                                        boolean useLocks)

getStack

public <E> BlockingQueue<E> getStack(String name)

createStack

public <E> BlockingQueue<E> createStack(String name,
                                        Serializer<E> serializer,
                                        boolean useLocks)

getCircularQueue

public <E> BlockingQueue<E> getCircularQueue(String name)

createCircularQueue

public <E> BlockingQueue<E> createCircularQueue(String name,
                                                Serializer<E> serializer,
                                                long size)

createAtomicLong

public Atomic.Long createAtomicLong(String name,
                                    long initValue)

getAtomicLong

public Atomic.Long getAtomicLong(String name)

createAtomicInteger

public Atomic.Integer createAtomicInteger(String name,
                                          int initValue)

getAtomicInteger

public Atomic.Integer getAtomicInteger(String name)

createAtomicBoolean

public Atomic.Boolean createAtomicBoolean(String name,
                                          boolean initValue)

getAtomicBoolean

public Atomic.Boolean getAtomicBoolean(String name)

checkShouldCreate

public void checkShouldCreate(String name)

createAtomicString

public Atomic.String createAtomicString(String name,
                                        String initValue)

getAtomicString

public Atomic.String getAtomicString(String name)

createAtomicVar

public <E> Atomic.Var<E> createAtomicVar(String name,
                                         E initValue,
                                         Serializer<E> serializer)

getAtomicVar

public <E> Atomic.Var<E> getAtomicVar(String name)

get

public <E> E get(String name)
return record with given name or null if name does not exist


exists

public boolean exists(String name)

delete

public void delete(String name)
delete record/collection with given name


getAll

public Map<String,Object> getAll()
return map of all named collections/records


rename

public void rename(String oldName,
                   String newName)
rename named record into newName

Parameters:
oldName - current name of record/collection
newName - new name of record/collection
Throws:
NoSuchElementException - if oldName does not exist

checkNameNotExists

public void checkNameNotExists(String name)
Checks that object with given name does not exist yet.

Parameters:
name - to check
Throws:
IllegalArgumentException - if name is already used

close

public void close()
Closes database. All other methods will throw 'IllegalAccessError' after this method was called.

!! it is necessary to call this method before JVM exits!!


getFromWeakCollection

public Object getFromWeakCollection(String name)
All collections are weakly referenced to prevent two instances of the same collection in memory. This is mainly for locking, two instances of the same lock would not simply work.


checkNotClosed

public void checkNotClosed()

isClosed

public boolean isClosed()
Returns:
true if DB is closed and can no longer be used

commit

public void commit()
Commit changes made on collections loaded by this DB

See Also:
Engine.commit()

rollback

public void rollback()
Rollback changes made on collections loaded by this DB

See Also:
Engine.rollback()

compact

public void compact()
Perform storage maintenance. Typically compact underlying storage and reclaim unused space.

NOTE: MapDB does not have smart defragmentation algorithms. So compaction usually recreates entire store from scratch. This may require additional disk space.


snapshot

public DB snapshot()
Make readonly snapshot view of DB and all of its collection Collections loaded by this instance are not affected (are still mutable). You have to load new collections from DB returned by this method

Returns:
readonly snapshot view

getDefaultSerializer

public Serializer getDefaultSerializer()
Returns:
default serializer used in this DB, it handles POJO and other stuff.

getEngine

public Engine getEngine()
Returns:
underlying engine which takes care of persistence for this DB.

checkType

public void checkType(String type,
                      String expected)


Copyright © 2014. All Rights Reserved.