Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.mapdb;
  
  import org.junit.Ignore;
  import org.junit.Test;
  
  import java.util.*;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
 public class TxMakerTest{
 
     TxMaker tx =
             //new TxMaker(new TxEngine(new DB(new StoreHeap()).getEngine(),true));
             DBMaker.newMemoryDB().makeTxMaker();
 
     @Test public void simple_commit(){
         DB db =.makeTx();
         db.getHashMap("test").put("aa""bb");
         db.commit();
         assertEquals("bb".makeTx().getHashMap("test").get("aa"));
     }
 
     @Test public void simple_rollback(){
         DB db =.makeTx();
         db.getHashMap("test").put("aa""bb");
         db.rollback();
         assertEquals(null.makeTx().getHashMap("test").get("aa"));
     }
 
     @Test public void commit_conflict(){
         DB db0 = .makeTx();
         long recid = db0.getEngine().put(111, .);
         db0.commit();
         DB db1 = .makeTx();
         db1.getEngine().update(recid, 222, .);
         DB db2.makeTx();
         db2.getEngine().update(recid, 333, .);
         db2.commit();
 
         try{
             //will fail, record was already modified
             db1.commit();
 
             fail("should throw exception");
         }catch(TxRollbackException e){
             //expected
         }
 
 
         assertEquals(Integer.valueOf(333), .makeTx().getEngine().get(recid.));
 
     }
 
     @Test(timeout = 60000)
     public void concurrent_tx() throws Throwable {
         final int threads = 10;
         final int items = 1000;
         final CountDownLatch l = new CountDownLatch(threads);
         final List<Throwableex = new CopyOnWriteArrayList<Throwable>();
         final Collection s = Collections.synchronizedCollection(new HashSet());
         for(int i=0;i<threads;i++){
             final int t=i*items*10000;
             new Thread(){
                 @Override
                 public void run() {
                     try{
                     for (int index = tindex < t+itemsindex++) {
                         final int temp = index;
                         s.add(temp);
                           .execute(new TxBlock() {
 
                             @Override
                             public void tx(DB dbthrows TxRollbackException {
 //							Queue<String> queue = db.getQueue(index + "");
 //							queue.offer(temp + "");
                                 Map map = db.getHashMap("ha");
                                 if(temp!=t)
                                     assertEquals(temp-1,map.get(temp-1));
                                 map.put(temptemp );
                             }
                         });
                     }
                     }catch(Throwable e){
                         e.printStackTrace();
                         ex.add(e);
                     }finally{
                         l.countDown();
                     }
                 }
             }.start();
         }
         while(!l.await(100, .) && ex.isEmpty()){}
 
         if(!ex.isEmpty())
             throw ex.get(0);
        Map m = .makeTx().getHashMap("ha");
        assertEquals(s.size(),m.size());
        for(Object i:s){
            assertEquals(im.get(i));
        }
    }
    @Test(timeout = 60000)
    public void increment() throws Throwable {
        final int threads = 10;
        final int items = 1000;
        DB db = .makeTx();
        final long recid = db.getEngine().put(1L,.);
        db.commit();
        final List<Throwableex = new CopyOnWriteArrayList<Throwable>();
        final CountDownLatch l = new CountDownLatch(threads);
        for(int i=0;i<threads;i++){
            new Thread(){
                @Override
                public void run() {
                    try{
                        for (int j = 0;j<items;j++) {
                            .execute(new TxBlock() {
                                @Override
                                public void tx(DB dbthrows TxRollbackException {
                                    long old = db.getEngine().get(recid.);
                                    db.getEngine().update(recid,old+1,.);
                                }
                            });
                        }
                    }catch(Throwable e){
                        e.printStackTrace();
                        ex.add(e);
                    }finally{
                        l.countDown();
                    }
                }
            }.start();
        }
        while(!l.await(100, .) && ex.isEmpty()){}
        if(!ex.isEmpty())
            throw ex.get(0);
        assertEquals(Long.valueOf(threads*items+1), .makeTx().getEngine().get(recid,.));
    }
    @Test(timeout = 60000)
    public void cas() throws Throwable {
        final int threads = 10;
        final int items = 1000;
        DB db = .makeTx();
        final long recid = db.getEngine().put(1L,.);
        db.commit();
        final List<Throwableex = new CopyOnWriteArrayList<Throwable>();
        final CountDownLatch l = new CountDownLatch(threads);
        for(int i=0;i<threads;i++){
            new Thread(){
                @Override
                public void run() {
                    try{
                        for (int j = 0;j<items;j++) {
                            .execute(new TxBlock() {
                                @Override
                                public void tx(DB dbthrows TxRollbackException {
                                    long old = db.getEngine().get(recid.);
                                    while(!db.getEngine().compareAndSwap(recid,old,old+1,.)){
                                    }
                                }
                            });
                        }
                    }catch(Throwable e){
                        e.printStackTrace();
                        ex.add(e);
                    }finally{
                        l.countDown();
                    }
                }
            }.start();
        }
        while(!l.await(100, .) && ex.isEmpty()){}
        if(!ex.isEmpty())
            throw ex.get(0);
        assertEquals(Long.valueOf(threads*items+1), .makeTx().getEngine().get(recid,.));
    }
    @Test @Ignore
    public void txSnapshot(){
        TxMaker txMaker = DBMaker
                .newMemoryDB()
                .snapshotEnable()
                .makeTxMaker();
        DB db = txMaker.makeTx();
        long recid = db.getEngine().put("aa",.);
        DB snapshot = db.snapshot();
        db.getEngine().update(recid,"bb",.);
        assertEquals("aa",snapshot.getEngine().get(recid,.));
        assertEquals("bb",db.getEngine().get(recid,.));
    }
    @Test
    public void txSnapshot2(){
        TxMaker txMaker = DBMaker
                .newMemoryDB()
                .snapshotEnable()
                .makeTxMaker();
        DB db = txMaker.makeTx();
        long recid = db.getEngine().put("aa",.);
        db.commit();
        db = txMaker.makeTx();
        DB snapshot = db.snapshot();
        db.getEngine().update(recid,"bb",.);
        assertEquals("aa",snapshot.getEngine().get(recid,.));
        assertEquals("bb",db.getEngine().get(recid,.));
    }
New to GrepCode? Check out our FAQ X