private void testRemoveAndFetch(final boolean inTransaction) throws Exception { final Exchange ex = _persistit.getExchange("persistit", "gogo", true); final Transaction txn = ex.getTransaction(); ex.getValue().put(RED_FOX); for (int i = 1; i < 10000; i++) { if (inTransaction) { txn.begin(); } ex.to(i).store(); if (inTransaction) { txn.commit(); txn.end(); } } for (int i = 1; i < 10000; i++) { if (inTransaction) { txn.begin(); } ex.getValue().clear(); ex.to(i).fetchAndRemove(); assertTrue("A value was fetched", ex.getValue().isDefined()); if (inTransaction) { txn.commit(); txn.end(); } } }
@Test public void testStringAppend() throws PersistitException { int initialLength = 256; final String randomString = createString(initialLength); final Exchange ex = _persistit.getExchange("persistit", randomString, true); ex.clear().append(randomString); ex.getValue().put(randomString); ex.store(); ex.clear().append(randomString); ex.fetch(); assertEquals(randomString, ex.getValue().getString()); /* lets double key length but keep value the same */ initialLength *= 2; String randomKey = createString(initialLength); ex.clear().append(randomKey); ex.getValue().put(randomString); ex.store(); ex.clear().append(randomKey); ex.fetch(); assertEquals(randomString, ex.getValue().getString()); /* now lets keep doubling value length for kicks */ for (int i = 0; i < 12; i++) { initialLength *= 2; final String randomValue = createString(initialLength); ex.clear().append(randomKey); ex.getValue().put(randomValue); ex.store(); ex.clear().append(randomKey); ex.fetch(); assertEquals(randomValue, ex.getValue().getString()); } /* now double the key length */ initialLength = 256; for (int i = 0; i < 2; i++) { initialLength *= 2; randomKey = createString(initialLength); ex.clear().append(randomKey); ex.getValue().put(randomString); ex.store(); ex.clear().append(randomKey); ex.fetch(); assertEquals(randomString, ex.getValue().getString()); } /* * set key length to value larger than max and make sure exception is * thrown */ initialLength = 2048; // 2047 is max key length randomKey = createString(initialLength); try { ex.clear().append(randomKey); fail("ConversionException should have been thrown"); } catch (final ConversionException expected) { } }
@Test public void testTraversal() throws PersistitException { final Exchange ex = _persistit.getExchange("persistit", "gogo", true); final String mockValue = createString(256); /* insert 1000 records */ for (int i = 0; i < 1000; i++) { final String key = createString(32); ex.clear().append(key); ex.getValue().put(mockValue); ex.store(); } /* traverse forwards through those values */ ex.clear().to(Key.BEFORE); while (ex.next()) { ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); } ex.clear().to(Key.BEFORE); while (ex.hasNext()) { ex.next(); ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); } /* now traverse backwards through those values */ ex.clear().to(Key.AFTER); while (ex.previous()) { ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); } ex.clear().to(Key.AFTER); while (ex.hasPrevious()) { ex.previous(); ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); } /* now use the traverse method with various directions */ ex.clear().to(Key.BEFORE); final Key key = ex.getKey(); final KeyFilter kf = new KeyFilter(key); /* this is mostly to test if we can trigger bad things */ assertEquals(false, ex.traverse(Key.EQ, kf, 4)); assertEquals(false, ex.traverse(Key.GT, kf, 4)); assertEquals(false, ex.traverse(Key.GTEQ, kf, 4)); assertEquals(false, ex.traverse(Key.LT, kf, 4)); assertEquals(false, ex.traverse(Key.LTEQ, kf, 4)); }
@Test public void testAppend() throws PersistitException { final Exchange ex = _persistit.getExchange("persistit", "tree", true); final String mockValue = "PADRAIG"; /* test boolean append */ ex.clear().append(true); ex.getValue().put(mockValue); ex.store(); ex.clear().append(true); ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); /* test float append */ final float floatKey = 5.545454f; ex.clear().append(floatKey); ex.getValue().put(mockValue); ex.store(); ex.clear().append(floatKey); ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); /* test double append */ final double doubleKey = 6.66; ex.clear().append(doubleKey); ex.getValue().put(mockValue); ex.store(); ex.clear().append(doubleKey); ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); /* test int append */ final int intKey = 6; ex.clear().append(intKey); ex.getValue().put(mockValue); ex.store(); ex.clear().append(intKey); ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); /* test byte append */ final byte oneByte = 1; ex.clear().append(oneByte); ex.getValue().put(mockValue); ex.store(); ex.clear().append(oneByte); ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); /* test short append */ final short smallShort = 1234; ex.clear().append(smallShort); ex.getValue().put(mockValue); ex.store(); ex.clear().append(smallShort); ex.fetch(); assertEquals(mockValue, ex.getValue().getString()); }
@Test public void testKeyValues() throws PersistitException { final Exchange ex = _persistit.getExchange("persistit", "gogo", true); final String firstValue = new String("PADRAIG"); final String secondValue = new String("SARAH"); final String thirdValue = new String("TEENY"); final String fourthValue = new String("NIMBUS"); ex.clear().append(-2); ex.getValue().put(firstValue); ex.store(); ex.clear().append(-1); ex.getValue().put(secondValue); ex.store(); ex.clear().append(0); ex.getValue().put(thirdValue); ex.store(); ex.clear().append(1); ex.getValue().put(fourthValue); ex.store(); ex.clear().to(-2); ex.fetch(); assertEquals(firstValue, ex.getValue().getString()); ex.clear().to(-1); ex.fetch(); assertEquals(secondValue, ex.getValue().getString()); ex.clear().to(0); ex.fetch(); assertEquals(thirdValue, ex.getValue().getString()); ex.clear().to(1); ex.fetch(); assertEquals(fourthValue, ex.getValue().getString()); ex.clear().append(-2); ex.remove(); ex.getValue().put(-2); ex.store(); ex.clear().append(-1); ex.remove(); ex.getValue().put(-1); ex.store(); ex.clear().append(0); ex.remove(); ex.getValue().put(0); ex.store(); ex.clear().append(1); ex.remove(); ex.getValue().put(1); ex.store(); ex.clear().to(-2); ex.fetch(); assertEquals(-2, ex.getValue().getInt()); ex.clear().to(-1); ex.fetch(); assertEquals(-1, ex.getValue().getInt()); ex.clear().to(0); ex.fetch(); assertEquals(0, ex.getValue().getInt()); ex.clear().to(1); ex.fetch(); assertEquals(1, ex.getValue().getInt()); }