Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.mapdb;
  
  import java.util.*;
Store which keeps all instances on heap. It does not use serialization.
 
 public class StoreHeap extends Store implements Serializable{
 
     protected final static Fun.Tuple2 TOMBSTONE = Fun.t2(null,null);


    
All commited records in store
 
     protected final ConcurrentNavigableMap<Long,Fun.Tuple2records
             = new ConcurrentSkipListMap<LongFun.Tuple2>();

    
All not-yet commited records in store
 
     protected final ConcurrentNavigableMap<Long,Fun.Tuple2rollback
             = new ConcurrentSkipListMap<LongFun.Tuple2>();


    
Queue of deleted recids, those are reused for new records
 
     protected final Queue<LongfreeRecids = new ConcurrentLinkedQueue<Long>();

    
Maximal returned recid, incremented if there are no free recid
 
     protected final AtomicLong maxRecid = new AtomicLong();
 
     public StoreHeap(){
         super(false,false,null);
         for(long recid=1;recid<=;recid++){
             .put(recid, Fun.t2(null, (Serializer)null));
         }
     }
 
     @Override
     public long preallocate() {
         final Lock lock  = [new Random().nextInt(.)].writeLock();
         lock.lock();
         try{
             Long recid = .poll();
             if(recid==nullrecid = .incrementAndGet();
             return recid;
         }finally{
             lock.unlock();
         }
     }
 
     @Override
     public void preallocate(long[] recids) {
         final Lock lock  = [new Random().nextInt(.)].writeLock();
         lock.lock();
         try{
             for(int i=0;i<recids.length;i++){
                 Long recid = .poll();
                 if(recid==nullrecid = .incrementAndGet();
                 recids[i] = recid;
             }
         }finally{
             lock.unlock();
         }
     }
     @Override
     public <A> long put(A valueSerializer<A> serializer) {
         assert(value!=null);
         final Lock lock  = [new Random().nextInt(.)].writeLock();
         lock.lock();
         try{
             Long recid = .poll();
             if(recid==nullrecid = .incrementAndGet();
             .put(recid, Fun.<ObjectSerializer>t2(value,serializer));
             .put(recid, Fun.t2(,serializer ));
             assert(recid>0);
             return recid;
         }finally{
             lock.unlock();
         }
     }
 
     @Override
     public <A> A get(long recidSerializer<A> serializer) {
         assert(recid>0);
         final Lock lock  = [Utils.longHash(recid)&.].readLock();
         lock.lock();
         try{
             //get from commited records
             Fun.Tuple2 t = .get(recid);
             return t!=null? (A) t.a : null;
         }finally{
             lock.unlock();
         }
    }
    @Override
    public <A> void update(long recid, A valueSerializer<A> serializer) {
        assert(recid>0);
        assert(value!=null);
        assert(serializer!=null);
        assert(recid>0);
        final Lock lock  = [Utils.longHash(recid)&.].writeLock();
        lock.lock();
        try{
            Fun.Tuple2 old = .put(recid, Fun.<ObjectSerializer>t2(value,serializer));
            if(old!=null//TODO null if record was preallocated
                .putIfAbsent(recid,old);
        }finally{
            lock.unlock();
        }
    }
    @Override
    public <A> boolean compareAndSwap(long recid, A expectedOldValue, A newValueSerializer<A> serializer) {
        assert(recid>0);
        assert(expectedOldValue!=null && newValue!=null);
        final Lock lock  = [Utils.longHash(recid)&.].writeLock();
        lock.lock();
        try{
            Fun.Tuple2 old = Fun.t2(expectedOldValueserializer);
            boolean ret =  .replace(recidold, Fun.t2(newValueserializer));
            if(ret.putIfAbsent(recid,old);
            return ret;
        }finally{
            lock.unlock();
        }
    }
    @Override
    public <A> void delete(long recidSerializer<A> serializer) {
        assert(recid>0);
        final Lock lock  = [Utils.longHash(recid)&.].writeLock();
        lock.lock();
        try{
            Fun.Tuple2 t2 = .remove(recid);
            if(t2!=null.putIfAbsent(recid,t2);
            .add(recid);
        }finally{
            lock.unlock();
        }
    }
    @Override
    public void close() {
        lockAllWrite();
        try{
            .clear();
            .clear();
            .clear();
        }finally{
            unlockAllWrite();
        }
    }
    @Override
    public boolean isClosed() {
        return false;
    }
    @Override
    public void commit() {
        lockAllWrite();
        try{
            .clear();
        }finally{
            unlockAllWrite();
        }
    }
    @Override
    public void rollback() throws UnsupportedOperationException {
        lockAllWrite();
        try{
            //put all stuff from `rollback` into `records`
            for(Map.Entry<Long,Fun.Tuple2e:.entrySet()){
                Long recid = e.getKey();
                Fun.Tuple2 val = e.getValue();
                if(val == .remove(recid);
                else .put(recidval);
            }
            .clear();
        }finally{
            unlockAllWrite();
        }
    }
    @Override
    public boolean isReadOnly() {
        return false;
    }
    @Override
    public void clearCache() {
    }
    @Override
    public void compact() {
    }
    @Override
    public boolean canRollback(){
        return true;
    }
    @Override
    public long getMaxRecid() {
        return .get();
    }
    @Override
    public ByteBuffer getRaw(long recid) {
        Fun.Tuple2 t = .get(recid);
        if(t==null||t.a == nullreturn null;
        return ByteBuffer.wrap(serialize(t.a, (Serializer<Object>) t.b).copyBytes());
    }
    @Override
    public Iterator<LonggetFreeRecids() {
        return Collections.unmodifiableCollection().iterator();
    }
    @Override
    public void updateRaw(long recidByteBuffer data) {
        throw new UnsupportedOperationException("can not put raw data into StoreHeap");
    }
    @Override
    public long getSizeLimit() {
        return 0;
    }
    @Override
    public long getCurrSize() {
        return .size();
    }
    @Override
    public long getFreeSize() {
        return 0;
    }
    @Override
    public String calculateStatistics() {
        return null;
    }
New to GrepCode? Check out our FAQ X