Ejemplo n.º 1
0
 private void testSetting() {
   String fileName = getBaseDir() + "/testSetting.h3";
   FileUtils.delete(fileName);
   MVStore s = MVStore.open(fileName);
   assertEquals(0, s.getCurrentVersion());
   assertEquals(0, s.getStoreVersion());
   s.setStoreVersion(1);
   assertEquals(null, s.getSetting("hello"));
   s.setSetting("test", "Hello");
   assertEquals("Hello", s.getSetting("test"));
   s.close();
   s = MVStore.open(fileName);
   assertEquals(0, s.getCurrentVersion());
   assertEquals(0, s.getStoreVersion());
   s.setStoreVersion(1);
   assertEquals(null, s.getSetting("hello"));
   s.setSetting("test", "Hello");
   assertEquals("Hello", s.getSetting("test"));
   s.store();
   s.close();
   s = MVStore.open(fileName);
   assertEquals(1, s.getCurrentVersion());
   assertEquals(1, s.getStoreVersion());
   assertEquals("Hello", s.getSetting("test"));
   s.close();
 }
Ejemplo n.º 2
0
  private void testVersion() {
    String fileName = getBaseDir() + "/testVersion.h3";
    FileUtils.delete(fileName);
    MVStore s;
    s = openStore(fileName);
    MVMap<String, String> m;
    s = openStore(fileName);
    m = s.openMap("data", String.class, String.class);
    long first = s.getCurrentVersion();
    s.incrementVersion();
    m.put("1", "Hello");
    m.put("2", "World");
    for (int i = 10; i < 20; i++) {
      m.put("" + i, "data");
    }
    long old = s.getCurrentVersion();
    s.incrementVersion();
    m.put("1", "Hallo");
    m.put("2", "Welt");
    MVMap<String, String> mFirst;
    mFirst = m.openVersion(first);
    assertEquals(0, mFirst.size());
    MVMap<String, String> mOld;
    assertEquals("Hallo", m.get("1"));
    assertEquals("Welt", m.get("2"));
    mOld = m.openVersion(old);
    assertEquals("Hello", mOld.get("1"));
    assertEquals("World", mOld.get("2"));
    assertTrue(mOld.isReadOnly());
    s.getCurrentVersion();
    s.setRetainChunk(0);
    long old2 = s.store();

    // the old version is still available
    assertEquals("Hello", mOld.get("1"));
    assertEquals("World", mOld.get("2"));

    m.put("1", "Hi");
    assertEquals("Welt", m.remove("2"));
    s.store();
    s.close();

    s = openStore(fileName);
    m = s.openMap("data", String.class, String.class);
    assertEquals("Hi", m.get("1"));
    assertEquals(null, m.get("2"));
    mOld = m.openVersion(old2);
    assertEquals("Hallo", mOld.get("1"));
    assertEquals("Welt", mOld.get("2"));
    s.close();
  }
Ejemplo n.º 3
0
 private void testIterateOverChanges() {
   String fileName = getBaseDir() + "/testIterate.h3";
   FileUtils.delete(fileName);
   MVStore s = openStore(fileName);
   s.setMaxPageSize(6);
   MVMap<Integer, String> m = s.openMap("data", Integer.class, String.class);
   for (int i = 0; i < 100; i++) {
     m.put(i, "Hi");
   }
   s.incrementVersion();
   s.store();
   for (int i = 20; i < 40; i++) {
     assertEquals("Hi", m.put(i, "Hello"));
   }
   long old = s.getCurrentVersion();
   s.incrementVersion();
   for (int i = 10; i < 15; i++) {
     m.put(i, "Hallo");
   }
   m.put(50, "Hallo");
   for (int i = 90; i < 93; i++) {
     assertEquals("Hi", m.remove(i));
   }
   assertEquals(null, m.put(100, "Hallo"));
   Iterator<Integer> it = m.changeIterator(old);
   ArrayList<Integer> list = New.arrayList();
   while (it.hasNext()) {
     list.add(it.next());
   }
   assertEquals("[10, 11, 12, 13, 14, 50, 100, 90, 91, 92]", list.toString());
   s.close();
 }
Ejemplo n.º 4
0
  private void testRollbackInMemory() {
    String fileName = getBaseDir() + "/testRollback.h3";
    FileUtils.delete(fileName);
    MVStore s = openStore(fileName);
    assertEquals(0, s.getCurrentVersion());
    s.setMaxPageSize(5);
    MVMap<String, String> m = s.openMap("data", String.class, String.class);
    s.rollbackTo(0);
    assertTrue(m.isClosed());
    assertEquals(0, s.getCurrentVersion());
    m = s.openMap("data", String.class, String.class);

    MVMap<String, String> m0 = s.openMap("data0", String.class, String.class);
    MVMap<String, String> m2 = s.openMap("data2", String.class, String.class);
    m.put("1", "Hello");
    for (int i = 0; i < 10; i++) {
      m2.put("" + i, "Test");
    }
    long v1 = s.incrementVersion();
    assertEquals(1, v1);
    assertEquals(1, s.getCurrentVersion());
    MVMap<String, String> m1 = s.openMap("data1", String.class, String.class);
    assertEquals("Test", m2.get("1"));
    m.put("1", "Hallo");
    m0.put("1", "Hallo");
    m1.put("1", "Hallo");
    m2.clear();
    assertEquals("Hallo", m.get("1"));
    assertEquals("Hallo", m1.get("1"));
    s.rollbackTo(v1);
    assertEquals(1, s.getCurrentVersion());
    for (int i = 0; i < 10; i++) {
      assertEquals("Test", m2.get("" + i));
    }
    assertEquals("Hello", m.get("1"));
    assertNull(m0.get("1"));
    assertTrue(m1.isClosed());
    assertFalse(m0.isReadOnly());
    assertTrue(m1.isReadOnly());
    s.close();
  }
Ejemplo n.º 5
0
  private void testExample() {
    String fileName = getBaseDir() + "/testExample.h3";
    FileUtils.delete(fileName);

    // open the store (in-memory if fileName is null)
    MVStore s = MVStore.open(fileName);

    // create/get the map "data"
    // the String.class, String.class will be optional later
    MVMap<String, String> map = s.openMap("data", String.class, String.class);

    // add some data
    map.put("1", "Hello");
    map.put("2", "World");

    // get the current version, for later use
    long oldVersion = s.getCurrentVersion();

    // from now on, the old version is read-only
    s.incrementVersion();

    // more changes, in the new version
    // changes can be rolled back if required
    // changes always go into 'head' (the newest version)
    map.put("1", "Hi");
    map.remove("2");

    // access the old data (before incrementVersion)
    MVMap<String, String> oldMap = map.openVersion(oldVersion);

    // store the newest data to disk
    s.store();

    // print the old version (can be done
    // concurrently with further modifications)
    // this will print Hello World
    // System.out.println(oldMap.get("1"));
    // System.out.println(oldMap.get("2"));
    oldMap.close();

    // print the newest version ("Hi")
    // System.out.println(map.get("1"));

    // close the store - this doesn't write to disk
    s.close();
  }
Ejemplo n.º 6
0
  private void testRollbackStored() {
    String fileName = getBaseDir() + "/testRollback.h3";
    FileUtils.delete(fileName);
    MVMap<String, String> meta;
    MVStore s = openStore(fileName);
    assertEquals(-1, s.getRetainChunk());
    s.setRetainChunk(0);
    assertEquals(0, s.getRetainChunk());
    assertEquals(0, s.getCurrentVersion());
    assertFalse(s.hasUnsavedChanges());
    MVMap<String, String> m = s.openMap("data", String.class, String.class);
    assertTrue(s.hasUnsavedChanges());
    MVMap<String, String> m0 = s.openMap("data0", String.class, String.class);
    m.put("1", "Hello");
    assertEquals(1, s.incrementVersion());
    s.rollbackTo(1);
    assertEquals("Hello", m.get("1"));
    long v2 = s.store();
    assertEquals(2, v2);
    assertEquals(2, s.getCurrentVersion());
    assertFalse(s.hasUnsavedChanges());
    s.close();

    s = openStore(fileName);
    assertEquals(2, s.getCurrentVersion());
    s.setRetainChunk(0);
    meta = s.getMetaMap();
    m = s.openMap("data", String.class, String.class);
    m0 = s.openMap("data0", String.class, String.class);
    MVMap<String, String> m1 = s.openMap("data1", String.class, String.class);
    m.put("1", "Hallo");
    m0.put("1", "Hallo");
    m1.put("1", "Hallo");
    assertEquals("Hallo", m.get("1"));
    assertEquals("Hallo", m1.get("1"));
    assertTrue(s.hasUnsavedChanges());
    s.rollbackTo(v2);
    assertFalse(s.hasUnsavedChanges());
    assertNull(meta.get("map.data1"));
    assertNull(m0.get("1"));
    assertEquals("Hello", m.get("1"));
    assertEquals(2, s.store());
    s.close();

    s = openStore(fileName);
    s.setRetainChunk(0);
    assertEquals(2, s.getCurrentVersion());
    meta = s.getMetaMap();
    assertTrue(meta.get("map.data") != null);
    assertTrue(meta.get("map.data0") != null);
    assertNull(meta.get("map.data1"));
    m = s.openMap("data", String.class, String.class);
    m0 = s.openMap("data0", String.class, String.class);
    assertNull(m0.get("1"));
    assertEquals("Hello", m.get("1"));
    assertFalse(m0.isReadOnly());
    m.put("1", "Hallo");
    s.incrementVersion();
    assertEquals(3, s.getCurrentVersion());
    long v4 = s.store();
    assertEquals(4, v4);
    assertEquals(4, s.getCurrentVersion());
    s.close();

    s = openStore(fileName);
    s.setRetainChunk(0);
    m = s.openMap("data", String.class, String.class);
    m.put("1", "Hello");
    s.store();
    s.close();

    s = openStore(fileName);
    s.setRetainChunk(0);
    m = s.openMap("data", String.class, String.class);
    assertEquals("Hello", m.get("1"));
    s.rollbackTo(v4);
    assertEquals("Hallo", m.get("1"));
    s.close();

    s = openStore(fileName);
    s.setRetainChunk(0);
    m = s.openMap("data", String.class, String.class);
    assertEquals("Hallo", m.get("1"));
    s.close();
  }