@Test public void concurrent_first_key() { DB db = DBMaker.memoryDB().transactionDisable().make(); final BTreeMap m = db.treeMap("name"); // fill final int c = 1000000 * TT.scale(); for (int i = 0; i <= c; i++) { m.put(i, i); } Thread t = new Thread() { @Override public void run() { for (int i = 0; i <= c; i++) { m.remove(c); } } }; t.run(); while (t.isAlive()) { assertNotNull(m.firstKey()); } }
@Test public void par_update_get_compact() throws InterruptedException { int scale = TT.scale(); if (scale == 0) return; int threadNum = Math.min(4, scale * 4); final long end = TT.nowPlusMinutes(10); e = openEngine(); final BlockingQueue<Fun.Pair<Long, byte[]>> q = new ArrayBlockingQueue(threadNum * 10); for (int i = 0; i < threadNum; i++) { byte[] b = TT.randomByteArray(new Random().nextInt(10000)); long recid = e.put(b, BYTE_ARRAY_NOSIZE); q.put(new Fun.Pair(recid, b)); } final CountDownLatch l = new CountDownLatch(2); Thread tt = new Thread() { @Override public void run() { try { while (l.getCount() > 1) e.compact(); } finally { l.countDown(); } } }; tt.setDaemon(true); tt.run(); Exec.execNTimes( threadNum, new Callable() { @Override public Object call() throws Exception { Random r = new Random(); while (System.currentTimeMillis() < end) { Fun.Pair<Long, byte[]> t = q.take(); assertTrue( Serializer.BYTE_ARRAY.equals(t.b, e.get(t.a, Serializer.BYTE_ARRAY_NOSIZE))); int size = r.nextInt(1000); if (r.nextInt(10) == 1) size = size * 100; byte[] b = TT.randomByteArray(size); e.update(t.a, b, Serializer.BYTE_ARRAY_NOSIZE); q.put(new Fun.Pair<Long, byte[]>(t.a, b)); } return null; } }); l.countDown(); l.await(); for (Fun.Pair<Long, byte[]> t : q) { assertTrue(Serializer.BYTE_ARRAY.equals(t.b, e.get(t.a, Serializer.BYTE_ARRAY_NOSIZE))); } e.close(); }
@Test public void issue_38() { int max = 50000 * TT.scale(); Map<Integer, String[]> map = DBMaker.memoryDB().transactionDisable().make().treeMap("test"); for (int i = 0; i < max; i++) { map.put(i, new String[5]); } for (int i = 0; i < max; i = i + 1000) { assertTrue(Arrays.equals(new String[5], map.get(i))); assertTrue(map.get(i).toString().contains("[Ljava.lang.String")); } }
@Test public void empty_update_commit() { if (TT.scale() == 0) return; e = openEngine(); long recid = e.put("", Serializer.STRING_NOSIZE); assertEquals("", e.get(recid, Serializer.STRING_NOSIZE)); for (int i = 0; i < 10000; i++) { String s = TT.randomString(80000); e.update(recid, s, Serializer.STRING_NOSIZE); assertEquals(s, e.get(recid, Serializer.STRING_NOSIZE)); e.commit(); assertEquals(s, e.get(recid, Serializer.STRING_NOSIZE)); } e.close(); }
@Test public void randomStructuralCheck() { Random r = new Random(); BTreeMap map = DBMaker.memoryDB() .transactionDisable() .make() .treeMapCreate("aa") .keySerializer(BTreeKeySerializer.INTEGER) .valueSerializer(Serializer.INTEGER) .make(); int max = 100000 * TT.scale(); for (int i = 0; i < max * 10; i++) { map.put(r.nextInt(max), r.nextInt()); } map.checkStructure(); }
@Test public void compact2() { long max = TT.scale() * 10000; e = openEngine(); Map<Long, Long> recids = new HashMap<Long, Long>(); for (Long l = 0L; l < max; l++) { recids.put(l, e.put(l, Serializer.LONG)); } e.commit(); e.compact(); for (Long l = max; l < max * 2; l++) { recids.put(l, e.put(l, Serializer.LONG)); } for (Map.Entry<Long, Long> m : recids.entrySet()) { Long recid = m.getValue(); Long value = m.getKey(); assertEquals(value, e.get(recid, Serializer.LONG)); } e.close(); }
@Test public void par_cas() throws InterruptedException { int scale = TT.scale(); if (scale == 0) return; int threadNum = 8 * scale; final long end = TT.nowPlusMinutes(10); e = openEngine(); final BlockingQueue<Fun.Pair<Long, byte[]>> q = new ArrayBlockingQueue(threadNum * 10); for (int i = 0; i < threadNum; i++) { byte[] b = TT.randomByteArray(new Random().nextInt(10000)); long recid = e.put(b, BYTE_ARRAY_NOSIZE); q.put(new Fun.Pair(recid, b)); } Exec.execNTimes( threadNum, new Callable() { @Override public Object call() throws Exception { Random r = new Random(); while (System.currentTimeMillis() < end) { Fun.Pair<Long, byte[]> t = q.take(); int size = r.nextInt(10000); if (r.nextInt(10) == 1) size = size * 100; byte[] b = TT.randomByteArray(size); assertTrue(e.compareAndSwap(t.a, t.b, b, Serializer.BYTE_ARRAY_NOSIZE)); q.put(new Fun.Pair<Long, byte[]>(t.a, b)); } return null; } }); for (Fun.Pair<Long, byte[]> t : q) { assertTrue(Serializer.BYTE_ARRAY.equals(t.b, e.get(t.a, Serializer.BYTE_ARRAY_NOSIZE))); } e.close(); }
@Test public void recover_with_interrupt() throws InterruptedException { int scale = TT.scale(); if (scale == 0) return; e = openEngine(); if (!e.canRollback() || e instanceof StoreHeap) // TODO engine might have crash recovery, but no rollbacks return; // fill recids final int max = scale * 1000; final ArrayList<Long> recids = new ArrayList<Long>(); final AtomicLong a = new AtomicLong(10); final long counterRecid = e.put(a.get(), Serializer.LONG); Random r = new Random(a.get()); for (int j = 0; j < max; j++) { byte[] b = new byte[r.nextInt(100000)]; r.nextBytes(b); long recid = e.put(b, Serializer.BYTE_ARRAY_NOSIZE); recids.add(recid); } e.commit(); long endTime = TT.nowPlusMinutes(10); while (endTime > System.currentTimeMillis()) { final CountDownLatch latch = new CountDownLatch(1); Thread t = new Thread() { @Override public void run() { try { for (; ; ) { long A = a.incrementAndGet(); Random r = new Random(A); e.update(counterRecid, A, Serializer.LONG); for (long recid : recids) { byte[] b = new byte[r.nextInt(100000)]; r.nextBytes(b); e.update(recid, b, Serializer.BYTE_ARRAY_NOSIZE); } e.commit(); } } finally { latch.countDown(); } } }; t.start(); Thread.sleep(5000); t.stop(); latch.await(); if (!e.isClosed()) { close(); } // reopen and check the content e = openEngine(); // check if A-1 was commited long A = e.get(counterRecid, Serializer.LONG); assertTrue("" + A + " - " + a.get(), A == a.get() || A == a.get() - 1); r = new Random(A); for (long recid : recids) { byte[] b = new byte[r.nextInt(100000)]; r.nextBytes(b); byte[] b2 = e.get(recid, Serializer.BYTE_ARRAY_NOSIZE); assertTrue("Data were not commited recid=" + recid, Arrays.equals(b, b2)); } a.set(A); } e.close(); }