public long getQuotaUsage(String username) { if (!db.exists(username)) db.createAtomicLong(username, 0); return db.getAtomicLong(username).get(); }
public class AtomicLongTest extends TestCase { DB db = DBMaker.newMemoryDB().transactionDisable().make(); Atomic.Long ai = db.createAtomicLong("test", 1); /** constructor initializes to given value */ public void testConstructor() { assertEquals(1, ai.get()); } /** default constructed initializes to zero */ public void testConstructor2() { Atomic.Long ai = db.getAtomicLong("test2"); assertEquals(0, ai.get()); } /** get returns the last value set */ public void testGetSet() { assertEquals(1, ai.get()); ai.set(2); assertEquals(2, ai.get()); ai.set(-3); assertEquals(-3, ai.get()); } /** compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet() { assertTrue(ai.compareAndSet(1, 2)); assertTrue(ai.compareAndSet(2, -4)); assertEquals(-4, ai.get()); assertFalse(ai.compareAndSet(-5, 7)); assertFalse((7 == ai.get())); assertTrue(ai.compareAndSet(-4, 7)); assertEquals(7, ai.get()); } /** compareAndSet in one thread enables another waiting for value to succeed */ public void testCompareAndSetInMultipleThreads() throws InterruptedException { Thread t = new Thread( new Runnable() { public void run() { while (!ai.compareAndSet(2, 3)) Thread.yield(); } }); t.start(); assertTrue(ai.compareAndSet(1, 2)); t.join(0); assertFalse(t.isAlive()); assertEquals(ai.get(), 3); } /** getAndSet returns previous value and sets to given value */ public void testGetAndSet() { assertEquals(1, ai.getAndSet(0)); assertEquals(0, ai.getAndSet(-10)); assertEquals(-10, ai.getAndSet(1)); } /** getAndAdd returns previous value and adds given value */ public void testGetAndAdd() { assertEquals(1, ai.getAndAdd(2)); assertEquals(3, ai.get()); assertEquals(3, ai.getAndAdd(-4)); assertEquals(-1, ai.get()); } /** getAndDecrement returns previous value and decrements */ public void testGetAndDecrement() { assertEquals(1, ai.getAndDecrement()); assertEquals(0, ai.getAndDecrement()); assertEquals(-1, ai.getAndDecrement()); } /** getAndIncrement returns previous value and increments */ public void testGetAndIncrement() { assertEquals(1, ai.getAndIncrement()); assertEquals(2, ai.get()); ai.set(-2); assertEquals(-2, ai.getAndIncrement()); assertEquals(-1, ai.getAndIncrement()); assertEquals(0, ai.getAndIncrement()); assertEquals(1, ai.get()); } /** addAndGet adds given value to current, and returns current value */ public void testAddAndGet() { assertEquals(3, ai.addAndGet(2)); assertEquals(3, ai.get()); assertEquals(-1, ai.addAndGet(-4)); assertEquals(-1, ai.get()); } /** decrementAndGet decrements and returns current value */ public void testDecrementAndGet() { assertEquals(0, ai.decrementAndGet()); assertEquals(-1, ai.decrementAndGet()); assertEquals(-2, ai.decrementAndGet()); assertEquals(-2, ai.get()); } /** incrementAndGet increments and returns current value */ public void testIncrementAndGet() { assertEquals(2, ai.incrementAndGet()); assertEquals(2, ai.get()); ai.set(-2); assertEquals(-1, ai.incrementAndGet()); assertEquals(0, ai.incrementAndGet()); assertEquals(1, ai.incrementAndGet()); assertEquals(1, ai.get()); } /** toString returns current value. */ public void testToString() { for (long i = -12; i < 6; ++i) { ai.set(i); assertEquals(ai.toString(), Long.toString(i)); } } /** longValue returns current value. */ public void testLongValue() { for (int i = -12; i < 6; ++i) { ai.set(i); assertEquals((long) i, ai.longValue()); } } /** floatValue returns current value. */ public void testFloatValue() { for (int i = -12; i < 6; ++i) { ai.set(i); assertEquals((float) i, ai.floatValue()); } } /** doubleValue returns current value. */ public void testDoubleValue() { for (int i = -12; i < 6; ++i) { ai.set(i); assertEquals((double) i, ai.doubleValue()); } } public void testTX() { TxMaker txMaker = DBMaker.newMemoryDB().makeTxMaker(); DB db = txMaker.makeTx(); System.out.println(db.getAtomicLong("counter").incrementAndGet()); db.commit(); db = txMaker.makeTx(); System.out.println(db.getAtomicLong("counter").incrementAndGet()); db.commit(); db = txMaker.makeTx(); System.out.println(db.getAtomicLong("counter").incrementAndGet()); db.commit(); } }