コード例 #1
0
ファイル: TestMVStore.java プロジェクト: yudis/h2database
  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();
  }
コード例 #2
0
ファイル: TestMVStore.java プロジェクト: yudis/h2database
  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();
  }