Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    *  Copyright (c) 2012 Jan Kotek
    *
    *  Licensed under the Apache License, Version 2.0 (the "License");
    *  you may not use this file except in compliance with the License.
    *  You may obtain a copy of the License at
    *
    *    http://www.apache.org/licenses/LICENSE-2.0
    *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  
  package org.mapdb;
  
  import java.util.*;
A database with easy access to named maps and other collections.

Author(s):
Jan Kotek
  
  @SuppressWarnings("unchecked")
  public class DB {
  
      protected final boolean strictDBGet;

    
Engine which provides persistence for this D
  
      protected Engine engine;
    
already loaded named collections. It is important to keep collections as singletons, because of 'in-memory' lockin
  
      protected Map<StringWeakReference<?>> collections = new HashMap<StringWeakReference<?>>();

    
view over named records
  
      protected SortedMap<StringObjectcatalog;

    
default serializer used for persistence. Handles POJO and other stuff which requires write-able access to Engine
  
      protected Serializer<?> defaultSerializer;

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

Parameters:
engine
  
      public DB(final Engine engine){
          this(engine,false);
      }
  
      public DB(Engine engineboolean strictDBGet) {
          this. = engine;
          this. = strictDBGet;
          reinit();
      }
  
      protected void reinit() {
          // load serializer
          this. = new SerializerPojo(classInfos){
              @Override
              protected void saveClassInfo() {
                  //hook to save classes if they are updated
                  //I did not want to create direct dependency between SerialierPojo and Engine
                  .update(..);
              }
          };
  
          //open name dir
           = BTreeMap.preinitCatalog(this);
      }
  
      protected <A> A catGet(String name, A init){
          A ret = (A) .get(name);
          return ret!=nullret : init;
      }
  
  
      protected <A> A catGet(String name){
          return (A) .get(name);
      }
  
      protected <A> A catPut(String name, A value){
          .put(namevalue);
          return value;
      }
  
      protected <A> A catPut(String name, A value, A retValueIfNull){
          if(value==nullreturn retValueIfNull;
          .put(namevalue);
          return value;
      }
  
  
  
      public class HTreeMapMaker{
          protected final String name;
 
         public HTreeMapMaker(String name) {
             this. = name;
         }
 
 
         protected boolean keepCounter = false;
         protected Serializer keySerializer = null;
         protected Serializer valueSerializer = null;
         protected long expireMaxSize = 0L;
         protected long expire = 0L;
         protected long expireAccess = 0L;
 
         protected Fun.Function1 valueCreator = null;




        
keepCounter if counter should be kept, without counter updates are faster, but entire collection needs to be traversed to count items
 
         public HTreeMapMaker keepCounter(boolean keepCounter){
             this. = keepCounter;
             return this;
         }



        
keySerializer used to convert keys into/from binary form.
 
         public HTreeMapMaker keySerializer(Serializer keySerializer){
             this. = keySerializer;
             return this;
         }

        
valueSerializer used to convert values into/from binary form.
 
         public HTreeMapMaker valueSerializer(Serializer valueSerializer){
             this. = valueSerializer;
             return this;
         }

        
maximal number of entries in this map. Less used entries will be expired and removed to make collection smaller
 
         public HTreeMapMaker expireMaxSize(long maxSize){
             this. = maxSize;
             return this;
         }

        
Specifies that each entry should be automatically removed from the map once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.
 
         public HTreeMapMaker expireAfterWrite(long intervalTimeUnit timeUnit){
             this. = timeUnit.toMillis(interval);
             return this;
         }

        
Specifies that each entry should be automatically removed from the map once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.
 
         public HTreeMapMaker expireAfterWrite(long interval){
             this. = interval;
             return this;
         }

        
Specifies that each entry should be automatically removed from the map once a fixed duration has elapsed after the entry's creation, the most recent replacement of its value, or its last access. Access time is reset by all map read and write operations
 
         public HTreeMapMaker expireAfterAccess(long intervalTimeUnit timeUnit){
             this. = timeUnit.toMillis(interval);
             return this;
         }

        
Specifies that each entry should be automatically removed from the map once a fixed duration has elapsed after the entry's creation, the most recent replacement of its value, or its last access. Access time is reset by all map read and write operations
 
         public HTreeMapMaker expireAfterAccess(long interval){
             this. = interval;
             return this;
         }

        
If value is not found, HTreeMap can fetch and insert default value. `valueCreator` is used to return new value. This way `HTreeMap.get()` never returns null
 
         public HTreeMapMaker valueCreator(Fun.Function1 valueCreator){
             this. = valueCreator;
             return this;
         }
 
 
         public <K,V> HTreeMap<K,V> make(){
             if(!=0) =true;
             return DB.this.createHashMap(HTreeMapMaker.this);
         }
 
 
     }


    
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
<K> key
<V> value
Returns:
map
 
 
     synchronized public <K,V> HTreeMap<K,V> getHashMap(String name){
         return getHashMap(namenull);
     }

    
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.
<K> key
<V> value
Returns:
map
 
     synchronized public <K,V> HTreeMap<K,V> getHashMap(String nameFun.Function1<V,K> valueCreator){
         checkNotClosed();
         HTreeMap<K,V> ret = (HTreeMap<K, V>) getFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createHashMap(name).make();
         }
 
 
         //check type
         checkType(type"HashMap");
         //open existing map
         ret = new HTreeMap<K,V>(,
                 (Long)catGet(name+".counterRecid"),
                 (Integer)catGet(name+".hashSalt"),
                 (long[])catGet(name+".segmentRecids"),
                 catGet(name+".keySerializer",getDefaultSerializer()),
                 catGet(name+".valueSerializer",getDefaultSerializer()),
                 catGet(name+".expireTimeStart",0L),
                 catGet(name+".expire",0L),
                 catGet(name+".expireAccess",0L),
                 catGet(name+".expireMaxSize",0L),
                 (long[])catGet(name+".expireHeads",null),
                 (long[])catGet(name+".expireTails",null),
                 valueCreator
         );
 
 
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }


    
Returns new builder for HashMap with given name

Parameters:
name of map to create
Returns:
maker, call `.make()` to create map
Throws:
java.lang.IllegalArgumentException if name is already used
 
     public HTreeMapMaker createHashMap(String name){
         return new HTreeMapMaker(name);
     }



    
Creates new HashMap with more specific arguments

Parameters:
<K> key type
<V> value type
Returns:
newly created map
Throws:
java.lang.IllegalArgumentException if name is already used
 
     synchronized protected <K,V> HTreeMap<K,V> createHashMap(HTreeMapMaker m){
         String name = m.name;
         checkNameNotExists(name);
 
         long expireTimeStart=0, expire=0, expireAccess=0, expireMaxSize = 0;
         long[] expireHeads=nullexpireTails=null;
 
         if(m.expire!=0 || m.expireAccess!=0 || m.expireMaxSize !=0){
             expireTimeStart = catPut(name+".expireTimeStart",System.currentTimeMillis());
             expire = catPut(name+".expire",m.expire);
             expireAccess = catPut(name+".expireAccess",m.expireAccess);
             expireMaxSize = catPut(name+".expireMaxSize",m.expireMaxSize);
             expireHeads = new long[16];
             expireTails = new long[16];
             for(int i=0;i<16;i++){
                 expireHeads[i] = .put(0L,.);
                 expireTails[i] = .put(0L,.);
             }
             catPut(name+".expireHeads",expireHeads);
             catPut(name+".expireTails",expireHeads);
 
         }
 
 
         HTreeMap<K,V> ret = new HTreeMap<K,V>(,
                 catPut(name+".counterRecid",!m.keepCounter?0L:.put(0L, .)),
                 catPut(name+".hashSalt",..nextInt()),
                 catPut(name+".segmentRecids",HTreeMap.preallocateSegments()),
                 catPut(name+".keySerializer",m.keySerializer,getDefaultSerializer()),
                 catPut(name+".valueSerializer",m.valueSerializer,getDefaultSerializer()),
                 expireTimeStart,expire,expireAccess,expireMaxSizeexpireHeads ,expireTails,
                 m.valueCreator
 
         );
 
         .put(name + ".type""HashMap");
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }

    
Opens existing or creates new Hash Tree Set.

Parameters:
name of Set
<K> values in set
Returns:
set
 
     synchronized public <K> Set<K> getHashSet(String name){
         checkNotClosed();
         Set<K> ret = (Set<K>) getFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createHashSet(name,false,null);
         }
 
 
         //check type
         checkType(type"HashSet");
         //open existing map
         ret = new HTreeMap(,
                 (Long)catGet(name+".counterRecid"),
                 (Integer)catGet(name+".hashSalt"),
                 (long[])catGet(name+".segmentRecids"),
                 catGet(name+".serializer",getDefaultSerializer()),
                 null, 0L,0L,0L,0L,null,null,null
         ).keySet();
 
 
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }


    
Creates new HashSet

Parameters:
name of set to create
keepCounter if counter should be kept, without counter updates are faster, but entire collection needs to be traversed to count items.
serializer used to convert keys into/from binary form. Use null for default value.
<K> item type
Throws:
java.lang.IllegalArgumentException if name is already used
 
 
     synchronized public <K> Set<K> createHashSet(String nameboolean keepCounterSerializer<K> serializer){
         checkNameNotExists(name);
 
 
 
         Set<K> ret = new HTreeMap<K,Object>(,
                 catPut(name+".counterRecid",!keepCounter?0L:.put(0L, .)),
                 catPut(name+".hashSalt",..nextInt()),
                 catPut(name+".segmentRecids",HTreeMap.preallocateSegments()),
                 catPut(name+".serializer",serializer,getDefaultSerializer()),
                 null, 0L,0L,0L,0L,null,null,null
         ).keySet();
 
         .put(name + ".type""HashSet");
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
 
 
     public class BTreeMapMaker{
         protected final String name;
 
         public BTreeMapMaker(String name) {
             this. = name;
         }
 
         protected int nodeSize = 32;
         protected boolean valuesStoredOutsideNodes = false;
         protected boolean keepCounter = false;
         protected BTreeKeySerializer keySerializer;
         protected Serializer valueSerializer;
         protected Comparator comparator;
 
         protected Iterator pumpSource;
         protected Fun.Function1 pumpKeyExtractor;
         protected Fun.Function1 pumpValueExtractor;
         protected int pumpPresortBatchSize = -1;


        
nodeSize maximal size of node, larger node causes overflow and creation of new BTree node. Use large number for small keys, use small number for large keys
 
         public BTreeMapMaker nodeSize(int nodeSize){
             this. = nodeSize;
             return this;
         }

        
valuesStoredOutsideNodes if true, values are stored outside of BTree nodes. Use 'true' if your values are large
 
         public BTreeMapMaker valuesStoredOutsideNodes(boolean outside){
             this. = outside;
             return this;
         }

        
keepCounter if counter should be kept, without counter updates are faster, but entire collection needs to be traversed to count items
 
         public BTreeMapMaker keepCounter(boolean keepCounter){
             this. = keepCounter;
             return this;
         }

        
keySerializer used to convert keys into/from binary form.
 
         public BTreeMapMaker keySerializer(BTreeKeySerializer keySerializer){
             this. = keySerializer;
             return this;
         }

        
valueSerializer used to convert values into/from binary form.
 
         public BTreeMapMaker valueSerializer(Serializer valueSerializer){
             this. = valueSerializer;
             return this;
         }

        
comparator used to sort keys.
 
         public BTreeMapMaker comparator(Comparator comparator){
             this. = comparator;
             return this;
         }
 
         public <K,V> BTreeMapMaker pumpSource(Iterator<K> keysSource,  Fun.Function1<V,K> valueExtractor){
             this. = keysSource;
             this. = Fun.noTransformExtractor();
             this. = valueExtractor;
             return this;
         }
 
 
         public <K,V> BTreeMapMaker pumpSource(Iterator<Fun.Tuple2<K,V>> entriesSource){
             this. = entriesSource;
             this. = Fun.keyExtractor();
             this. = Fun.valueExtractor();
             return this;
         }
 
         public BTreeMapMaker pumpPresort(int batchSize){
             this. = batchSize;
             return this;
         }
 
 
         public <K,V> BTreeMap<K,V> make(){
             return DB.this.createTreeMap(BTreeMapMaker.this);
         }

        
creates map optimized for using `String` keys
 
         public <V> Map<String, V> makeStringMap() {
              = .;
             return make();
         }

        
creates map optimized for using zero or positive `Long` keys
 
         public <V> Map<Long, V> makeLongMap() {
             return make();
         }
 
     }
 
     public class BTreeSetMaker{
         protected final String name;
 
         public BTreeSetMaker(String name) {
             this. = name;
         }
 
         protected int nodeSize = 32;
         protected boolean keepCounter = false;
         protected BTreeKeySerializer serializer;
         protected Comparator comparator;
 
         protected Iterator pumpSource;
         protected int pumpPresortBatchSize = -1;


        
nodeSize maximal size of node, larger node causes overflow and creation of new BTree node. Use large number for small keys, use small number for large keys
 
         public BTreeSetMaker nodeSize(int nodeSize){
             this. = nodeSize;
             return this;
         }


        
keepCounter if counter should be kept, without counter updates are faster, but entire collection needs to be traversed to count items
 
         public BTreeSetMaker keepCounter(boolean keepCounter){
             this. = keepCounter;
             return this;
         }

        
keySerializer used to convert keys into/from binary form.
 
         public BTreeSetMaker serializer(BTreeKeySerializer serializer){
             this. = serializer;
             return this;
         }

        
comparator used to sort keys.
 
         public BTreeSetMaker comparator(Comparator comparator){
             this. = comparator;
             return this;
         }
 
         public <K> BTreeSetMaker pumpSource(Iterator<K> source){
             this. = source;
             return this;
         }
 
 
         public BTreeSetMaker pumpPresort(int batchSize){
             this. = batchSize;
             return this;
         }
 
 
         public <K> NavigableSet<K> make(){
             return DB.this.createTreeSet(BTreeSetMaker.this);
         }

        
creates set optimized for using `String`
 
         public NavigableSet<StringmakeStringSet() {
              = .;
             return make();
         }

        
creates set optimized for using zero or positive `Long`
 
         public NavigableSet<LongmakeLongSet() {
             return make();
         }
 
     }


    
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
<K> key
<V> value
Returns:
map
 
     synchronized public <K,V> BTreeMap<K,V> getTreeMap(String name){
         checkNotClosed();
         BTreeMap<K,V> ret = (BTreeMap<K,V>) getFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createTreeMap(name).make();
 
         }
         checkType(type"TreeMap");
 
         ret = new BTreeMap<K, V>(,
                 (LongcatGet(name + ".rootRecidRef"),
                 catGet(name+".maxNodeSize",32),
                 catGet(name+".valuesOutsideNodes",false),
                 catGet(name+".counterRecid",0L),
                 (BTreeKeySerializer)catGet(name+".keySerializer",new BTreeKeySerializer.BasicKeySerializer(getDefaultSerializer())),
                 catGet(name+".valueSerializer",getDefaultSerializer()),
                 (Comparator)catGet(name+".comparator",.)
                 );
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }

    
Returns new builder for TreeMap with given name

Parameters:
name of map to create
Returns:
maker, call `.make()` to create map
Throws:
java.lang.IllegalArgumentException if name is already used
 
     public BTreeMapMaker createTreeMap(String name){
         return new BTreeMapMaker(name);
     }
 
 
     synchronized protected <K,V> BTreeMap<K,V> createTreeMap(BTreeMapMaker m){
         String name = m.name;
         checkNameNotExists(name);
         m.keySerializer = fillNulls(m.keySerializer);
         m.keySerializer = catPut(name+".keySerializer",m.keySerializer,new BTreeKeySerializer.BasicKeySerializer(getDefaultSerializer()));
         m.valueSerializer = catPut(name+".valueSerializer",m.valueSerializer,getDefaultSerializer());
         m.comparator = catPut(name+".comparator",m.comparator,.);
 
         long counterRecid = !m.keepCounter?0L:.put(0L, .);
 
         long rootRecidRef;
         if(m.pumpSource==null){
             rootRecidRef = BTreeMap.createRootRef(,m.keySerializer,m.valueSerializer,m.comparator);
         }else{
             rootRecidRef = Pump.buildTreeMap(m.pumpSource,,m.pumpKeyExtractor,m.pumpValueExtractor,m.nodeSize,
                     m.valuesStoredOutsideNodes,counterRecid,m.keySerializer,m.valueSerializer,m.comparator);
         }
 
         BTreeMap<K,V> ret = new BTreeMap<K,V>(,
                 catPut(name+".rootRecidRef"rootRecidRef),
                 catPut(name+".maxNodeSize",m.nodeSize),
                 catPut(name+".valuesOutsideNodes",m.valuesStoredOutsideNodes),
                 catPut(name+".counterRecid",counterRecid),
                 m.keySerializer,
                 m.valueSerializer,
                 m.comparator
         );
         .put(name + ".type""TreeMap");
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
     protected <K> BTreeKeySerializer<K> fillNulls(BTreeKeySerializer<K> keySerializer) {
         if(keySerializer instanceof BTreeKeySerializer.Tuple2KeySerializer){
             BTreeKeySerializer.Tuple2KeySerializer s = (BTreeKeySerializer.Tuple2KeySerializerkeySerializer;
             return new BTreeKeySerializer.Tuple2KeySerializer(
                     s.aComparator!=null?s.aComparator:.,
                     s.aSerializer!=null?s.aSerializer:,
                     s.bSerializer!=null?s.bSerializer:
             );
         }
         if(keySerializer instanceof BTreeKeySerializer.Tuple3KeySerializer){
             BTreeKeySerializer.Tuple3KeySerializer s = (BTreeKeySerializer.Tuple3KeySerializerkeySerializer;
             return new BTreeKeySerializer.Tuple3KeySerializer(
                     s.aComparator!=null?s.aComparator:.,
                     s.bComparator!=null?s.bComparator:.,
                     s.aSerializer!=null?s.aSerializer:,
                     s.bSerializer!=null?s.bSerializer:,
                     s.cSerializer!=null?s.cSerializer:
             );
         }
         if(keySerializer instanceof BTreeKeySerializer.Tuple4KeySerializer){
             BTreeKeySerializer.Tuple4KeySerializer s = (BTreeKeySerializer.Tuple4KeySerializerkeySerializer;
             return new BTreeKeySerializer.Tuple4KeySerializer(
                     s.aComparator!=null?s.aComparator:.,
                     s.bComparator!=null?s.bComparator:.,
                     s.cComparator!=null?s.cComparator:.,
                     s.aSerializer!=null?s.aSerializer:,
                     s.bSerializer!=null?s.bSerializer:,
                     s.cSerializer!=null?s.cSerializer:,
                     s.dSerializer!=null?s.dSerializer:
             );
         }
 
         return keySerializer;
     }


    
Get Named directory. Key is name, value is recid under which named record is stored

Returns:
 
     public SortedMap<StringObjectgetCatalog(){
         return ;
     }


    
Opens existing or creates new B-linked-tree Set.

Parameters:
name of set
<K> values in set
Returns:
set
 
     synchronized public <K> NavigableSet<K> getTreeSet(String name){
         checkNotClosed();
         NavigableSet<K> ret = (NavigableSet<K>) getFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createTreeSet(name).make();
 
         }
         checkType(type"TreeSet");
 
         ret = new BTreeMap<K, Object>(,
                 (LongcatGet(name+".rootRecidRef"),
                 catGet(name+".maxNodeSize",32),
                 false,
                 catGet(name+".counterRecid",0L),
                 (BTreeKeySerializercatGet(name+".keySerializer",new BTreeKeySerializer.BasicKeySerializer(getDefaultSerializer())),
                 null,
                 (ComparatorcatGet(name+".comparator",.)
         ).keySet();
 
         .put(namenew WeakReference<Object>(ret));
         return ret;
 
     }

    
Creates new TreeSet.

Parameters:
name of set to create
Returns:
maker used to construct set
Throws:
java.lang.IllegalArgumentException if name is already used
 
     synchronized public BTreeSetMaker createTreeSet(String name){
          return new BTreeSetMaker(name);
     }
 
     synchronized public <K> NavigableSet<K> createTreeSet(BTreeSetMaker m){
         checkNameNotExists(m.name);
         m.serializer = fillNulls(m.serializer);
         m.serializer = catPut(m.name+".keySerializer",m.serializer,new BTreeKeySerializer.BasicKeySerializer(getDefaultSerializer()));
         m.comparator = catPut(m.name+".comparator",m.comparator,.);
 
         if(m.pumpPresortBatchSize!=-1){
             m.pumpSource = Pump.sort(m.pumpSource,m.pumpPresortBatchSize,Collections.reverseOrder(m.comparator),getDefaultSerializer());
         }
 
         long counterRecid = !m.keepCounter?0L:.put(0L, .);
         long rootRecidRef;
 
         if(m.pumpSource==null){
             rootRecidRef = BTreeMap.createRootRef(,m.serializer,null,m.comparator);
         }else{
             rootRecidRef = Pump.buildTreeMap(m.pumpSource,,Fun.noTransformExtractor(),null,m.nodeSize,
                     false,counterRecid,m.serializer,null,m.comparator);
         }
 
         NavigableSet<K> ret = new BTreeMap<K,Object>(,
                 catPut(m.name+".rootRecidRef"rootRecidRef),
                 catPut(m.name+".maxNodeSize",m.nodeSize),
                 false,
                 catPut(m.name+".counterRecid",counterRecid),
                 m.serializer,
                 null,
                 m.comparator
         ).keySet();
         .put(m.name + ".type""TreeSet");
         .put(m.namenew WeakReference<Object>(ret));
         return ret;
     }
 
 //    synchronized public <E> Queue<E> getQueue(String name){
 //        Long recid = catalog.get(name);
 //        if(recid==null){
 //
 //        }else{
 //            return new Queues.Lifo<E>(engine, getDefaultSerializer(),  recid, true);
 //        }
 //    }
 
 
     synchronized public <E> Queue<E> getQueue(String name) {
         checkNotClosed();
         Queues.Queue<E> ret = (Queues.Queue<E>) getFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createQueue(name,null);
         }
         checkType(type"Queue");
 
         ret = new Queues.Queue<E>(,
                 (Serializer<E>) catGet(name+".serializer",getDefaultSerializer()),
                 (Long)catGet(name+".headRecid"),
                 (Long)catGet(name+".nextTailRecid"),
                 (Long)catGet(name+".sizeRecid")
                 );
 
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
     synchronized public <E> Queue<E> createQueue(String nameSerializer<E> serializer) {
         checkNameNotExists(name);
 
         long headerRecid = .put(0L, .);
         long nextTail = .put(...new Queues.SimpleQueue.NodeSerializer(null));
         long nextTailRecid = .put(nextTail.);
         long sizeRecid = .put(0L, .);
 
         Queues.Queue<E> ret = new Queues.Queue<E>(,
                 catPut(name+".serializer",serializer,getDefaultSerializer()),
                 catPut(name+".headRecid",headerRecid),
                 catPut(name+".nextTailRecid",nextTailRecid),
                 catPut(name+".sizeRecid",sizeRecid)
                 );
         .put(name + ".type""Queue");
         .put(namenew WeakReference<Object>(ret));
         return ret;
 
     }
 
 
     synchronized public <E> Queue<E> getStack(String name) {
         checkNotClosed();
         Queues.Stack<E> ret = (Queues.Stack<E>) getFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createStack(name,null,true);
         }
 
         checkType(type"Stack");
 
         ret = new Queues.Stack<E>(,
                 (Serializer<E>) catGet(name+".serializer",getDefaultSerializer()),
                 (Long)catGet(name+".headRecid"),
                 (Boolean)catGet(name+".useLocks")
         );
 
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
 
 
     synchronized public <E> Queue<E> createStack(String nameSerializer<E> serializerboolean useLocks) {
         checkNameNotExists(name);
 
         long headerRecid = .put(0L, .);
 
 
         Queues.Stack<E> ret = new Queues.Stack<E>(,
                 catPut(name+".serializer",serializer,getDefaultSerializer()),
                 catPut(name+".headRecid",headerRecid),
                 catPut(name+".useLocks",useLocks)
         );
         .put(name + ".type""Stack");
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
 
     synchronized public <E> Queue<E> getCircularQueue(String name) {
         checkNotClosed();
         Queue<E> ret = (Queue<E>) getFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createCircularQueue(name,null, 1024);
         }
 
         checkType(type"CircularQueue");
 
         ret = new Queues.CircularQueue<E>(,
                 (Serializer<E>) catGet(name+".serializer",getDefaultSerializer()),
                 (Long)catGet(name+".headRecid"),
                 (Long)catGet(name+".headInsertRecid"),
                 (Long)catGet(name+".size")
         );
 
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
 
 
     synchronized public <E> Queue<E> createCircularQueue(String nameSerializer<E> serializerlong size) {
         checkNameNotExists(name);
         if(serializer==nullserializer = getDefaultSerializer();
 
 //        long headerRecid = engine.put(0L, Serializer.LONG);
         //insert N Nodes empty nodes into a circle
         long prevRecid = 0;
         long firstRecid = 0;
         Serializer<Queues.SimpleQueue.NodenodeSer = new Queues.SimpleQueue.NodeSerializer(serializer);
         for(long i=0;i<size;i++){
             Queues.SimpleQueue.Node n = new Queues.SimpleQueue.Node(prevRecidnull);
             prevRecid = .put(nnodeSer);
             if(firstRecid==0) firstRecid = prevRecid;
         }
         //update first node to point to last recid
         .update(firstRecidnew Queues.SimpleQueue.Node(prevRecidnull), nodeSer );
 
         long headRecid = .put(prevRecid.);
         long headInsertRecid = .put(prevRecid.);
 
 
 
         Queues.CircularQueue<E> ret = new Queues.CircularQueue<E>(,
                 catPut(name+".serializer",serializer),
                 catPut(name+".headRecid",headRecid),
                 catPut(name+".headInsertRecid",headInsertRecid),
                 catPut(name+".size",size)
         );
         .put(name + ".type""CircularQueue");
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
     synchronized public Atomic.Long createAtomicLong(String namelong initValue){
         checkNameNotExists(name);
         long recid = .put(initValue,.);
         Atomic.Long ret = new Atomic.Long(,
                 catPut(name+".recid",recid)
         );
         .put(name + ".type""AtomicLong");
         .put(namenew WeakReference<Object>(ret));
         return ret;
 
     }
 
 
     synchronized public Atomic.Long getAtomicLong(String name){
         checkNotClosed();
         Atomic.Long ret = (Atomic.LonggetFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createAtomicLong(name,0L);
         }
         checkType(type"AtomicLong");
 
         ret = new Atomic.Long(, (LongcatGet(name+".recid"));
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
 
 
     synchronized public Atomic.Integer createAtomicInteger(String nameint initValue){
         checkNameNotExists(name);
         long recid = .put(initValue,.);
         Atomic.Integer ret = new Atomic.Integer(,
                 catPut(name+".recid",recid)
         );
         .put(name + ".type""AtomicInteger");
         .put(namenew WeakReference<Object>(ret));
         return ret;
 
     }
 
 
     synchronized public Atomic.Integer getAtomicInteger(String name){
         checkNotClosed();
         Atomic.Integer ret = (Atomic.IntegergetFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createAtomicInteger(name, 0);
         }
         checkType(type"AtomicInteger");
 
         ret = new Atomic.Integer(, (LongcatGet(name+".recid"));
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
 
 
     synchronized public Atomic.Boolean createAtomicBoolean(String nameboolean initValue){
         checkNameNotExists(name);
         long recid = .put(initValue,.);
         Atomic.Boolean ret = new Atomic.Boolean(,
                 catPut(name+".recid",recid)
         );
         .put(name + ".type""AtomicBoolean");
         .put(namenew WeakReference<Object>(ret));
         return ret;
 
     }
 
 
     synchronized public Atomic.Boolean getAtomicBoolean(String name){
         checkNotClosed();
         Atomic.Boolean ret = (Atomic.BooleangetFromWeakCollection(name);
         if(ret!=nullreturn ret;
         String type = catGet(name + ".type"null);
         if(type==null){
             checkShouldCreate(name);
             return createAtomicBoolean(namefalse);
         }
         checkType(type"AtomicBoolean");
 
         ret = new Atomic.Boolean(, (LongcatGet(name+".recid"));
         .put(namenew WeakReference<Object>(ret));
         return ret;
     }
 
     protected void checkShouldCreate(String name) {
         if(throw new NoSuchElementException("No record with this name was found: "+name);
     }
 
 
     synchronized public Atomic.String createAtomicString(String nameString initValue){
         checkNameNotExists(name);
         if(initValue==nullthrow new IllegalArgumentException("initValue may not be null");
         long recid = .put(initValue,.);
         Atomic.String ret = new Atomic.String(,
                 catPut(name+".recid",recid)
         );
         .put(name + ".type""AtomicString");
         .put(namenew WeakReference<Object>(ret));
         return ret;
 
    }
    synchronized public Atomic.String getAtomicString(String name){
        checkNotClosed();
        Atomic.String ret = (Atomic.StringgetFromWeakCollection(name);
        if(ret!=nullreturn ret;
        String type = catGet(name + ".type"null);
        if(type==null){
            checkShouldCreate(name);
            return createAtomicString(name"");
        }
        checkType(type"AtomicString");
        ret = new Atomic.String(, (LongcatGet(name+".recid"));
        .put(namenew WeakReference<Object>(ret));
        return ret;
    }
    synchronized public <E> Atomic.Var<E> createAtomicVar(String name, E initValueSerializer<E> serializer){
        checkNameNotExists(name);
        if(serializer==nullserializer=getDefaultSerializer();
        long recid = .put(initValue,serializer);
        Atomic.Var ret = new Atomic.Var(,
                catPut(name+".recid",recid),
                catPut(name+".serializer",serializer)
        );
        .put(name + ".type""AtomicVar");
        .put(namenew WeakReference<Object>(ret));
        return ret;
    }
    synchronized public <E> Atomic.Var<E> getAtomicVar(String name){
        checkNotClosed();
        Atomic.Var ret = (Atomic.VargetFromWeakCollection(name);
        if(ret!=nullreturn ret;
        String type = catGet(name + ".type"null);
        if(type==null){
            checkShouldCreate(name);
            return createAtomicVar(namenullgetDefaultSerializer());
        }
        checkType(type"AtomicString");
        ret = new Atomic.Var(, (LongcatGet(name+".recid"), (SerializercatGet(name+".serializer"));
        .put(namenew WeakReference<Object>(ret));
        return ret;
    }

    
return record with given name or null if name does not exis
    synchronized public <E> E get(String name){
        String type = catGet(name+".type");
        if(type==nullreturn null;
        if("HashMap".equals(type)) return (E) getHashMap(name);
        if("HashSet".equals(type)) return (E) getHashSet(name);
        if("TreeMap".equals(type)) return (E) getTreeMap(name);
        if("TreeSet".equals(type)) return (E) getTreeSet(name);
        if("AtomicBoolean".equals(type)) return (E) getAtomicBoolean(name);
        if("AtomicInteger".equals(type)) return (E) getAtomicInteger(name);
        if("AtomicLong".equals(type)) return (E) getAtomicLong(name);
        if("AtomicString".equals(type)) return (E) getAtomicString(name);
        if("AtomicVar".equals(type)) return (E) getAtomicVar(name);
        if("Queue".equals(type)) return (E) getQueue(name);
        if("Stack".equals(type)) return (E) getStack(name);
        if("CircularQueue".equals(type)) return (E) getCircularQueue(name);
        throw new InternalError("Unknown type: "+name);
    }

    
delete record/collection with given nam
    synchronized public void delete(String name){
        Object r = get(name);
        if(r instanceof Atomic.Boolean){
            .delete(((Atomic.Boolean)r)..);
        }else if(r instanceof Atomic.Integer){
            .delete(((Atomic.Integer)r)..);
        }else if(r instanceof Atomic.Long){
            .delete(((Atomic.Long)r)..);
        }else if(r instanceof Atomic.String){
            .delete(((Atomic.String)r)..);
        }else if(r instanceof Atomic.Var){
            .delete(((Atomic.Var)r)., ((Atomic.Var)r).);
        }else if(r instanceof Queue){
            //drain queue
            Queue q = (Queuer;
            while(q.poll()!=null){
                //do nothing
            }
        }else if(r instanceof HTreeMap || r instanceof HTreeMap.KeySet){
            HTreeMap m = (r instanceof HTreeMap)? (HTreeMapr : ((HTreeMap.KeySet)r).parent();
            m.clear();
            //delete segments
            for(long segmentRecid:m.segmentRecids){
                .delete(segmentRecid.);
            }
        }else if(r instanceof BTreeMap || r instanceof BTreeMap.KeySet){
            BTreeMap m = (r instanceof BTreeMap)? (BTreeMapr : (BTreeMap) ((BTreeMap.KeySetr).;
            //TODO on BTreeMap recursively delete all nodes
            m.clear();
            if(m.counter!=null)
                .delete(m.counter.recid,.);
        }
        for(String n:.keySet()){
            if(!n.startsWith(name)) continue;
            .remove(n);
        }
        .remove(name);
    }


    
return map of all named collections/records
    synchronized public Map<String,ObjectgetAll(){
        TreeMap<String,Objectretnew TreeMap<StringObject>();
        for(String name:.keySet()){
            if(!name.endsWith(".type")) continue;