Пример #1
0
 private void testInMemory() {
   for (int j = 0; j < 1; j++) {
     MVStore s = openStore(null);
     // s.setMaxPageSize(10);
     // long t;
     int len = 100;
     // TreeMap<Integer, String> m = new TreeMap<Integer, String>();
     // HashMap<Integer, String> m = New.hashMap();
     MVMap<Integer, String> m = s.openMap("data", Integer.class, String.class);
     // t = System.currentTimeMillis();
     for (int i = 0; i < len; i++) {
       assertNull(m.put(i, "Hello World"));
     }
     // System.out.println("put: " + (System.currentTimeMillis() - t));
     // t = System.currentTimeMillis();
     for (int i = 0; i < len; i++) {
       assertEquals("Hello World", m.get(i));
     }
     // System.out.println("get: " + (System.currentTimeMillis() - t));
     // t = System.currentTimeMillis();
     for (int i = 0; i < len; i++) {
       assertEquals("Hello World", m.remove(i));
     }
     // System.out.println("remove: " + (System.currentTimeMillis() - t));
     // System.out.println();
     assertEquals(null, m.get(0));
     assertEquals(0, m.size());
     s.close();
   }
 }
Пример #2
0
  private void testSimple() {
    String fileName = getBaseDir() + "/testSimple.h3";
    FileUtils.delete(fileName);
    MVStore s = openStore(fileName);
    MVMap<Integer, String> m = s.openMap("data", Integer.class, String.class);
    for (int i = 0; i < 3; i++) {
      m.put(i, "hello " + i);
    }
    s.store();
    assertEquals("hello 0", m.remove(0));

    assertNull(m.get(0));
    for (int i = 1; i < 3; i++) {
      assertEquals("hello " + i, m.get(i));
    }
    s.store();
    s.close();

    s = openStore(fileName);
    m = s.openMap("data", Integer.class, String.class);
    assertNull(m.get(0));
    for (int i = 1; i < 3; i++) {
      assertEquals("hello " + i, m.get(i));
    }
    s.close();
  }
Пример #3
0
 private void testReuseSpace() {
   String fileName = getBaseDir() + "/testReuseSpace.h3";
   FileUtils.delete(fileName);
   long initialLength = 0;
   for (int j = 0; j < 20; j++) {
     MVStore s = openStore(fileName);
     MVMap<Integer, String> m = s.openMap("data", Integer.class, String.class);
     for (int i = 0; i < 10; i++) {
       m.put(i, "Hello");
     }
     s.store();
     for (int i = 0; i < 10; i++) {
       assertEquals("Hello", m.get(i));
       assertEquals("Hello", m.remove(i));
     }
     s.store();
     s.close();
     long len = FileUtils.size(fileName);
     if (initialLength == 0) {
       initialLength = len;
     } else {
       assertTrue(len <= initialLength * 2);
     }
   }
 }
Пример #4
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();
 }
Пример #5
0
 private void testBtreeStore() {
   String fileName = getBaseDir() + "/testBtreeStore.h3";
   FileUtils.delete(fileName);
   MVStore s = openStore(fileName);
   s.close();
   s = openStore(fileName);
   MVMap<Integer, String> m = s.openMap("data", Integer.class, String.class);
   int count = 2000;
   // Profiler p = new Profiler();
   // p.startCollecting();
   // long t = System.currentTimeMillis();
   for (int i = 0; i < count; i++) {
     assertNull(m.put(i, "hello " + i));
     assertEquals("hello " + i, m.get(i));
   }
   // System.out.println("put: " + (System.currentTimeMillis() - t));
   // System.out.println(p.getTop(5));
   // p = new Profiler();
   // p.startCollecting();
   // t = System.currentTimeMillis();
   s.store();
   // System.out.println("store: " + (System.currentTimeMillis() - t));
   // System.out.println(p.getTop(5));
   assertEquals("hello 0", m.remove(0));
   assertNull(m.get(0));
   for (int i = 1; i < count; i++) {
     assertEquals("hello " + i, m.get(i));
   }
   s.store();
   s.close();
   s = openStore(fileName);
   m = s.openMap("data", Integer.class, String.class);
   assertNull(m.get(0));
   for (int i = 1; i < count; i++) {
     assertEquals("hello " + i, m.get(i));
   }
   for (int i = 1; i < count; i++) {
     m.remove(i);
   }
   s.store();
   assertNull(m.get(0));
   for (int i = 0; i < count; i++) {
     assertNull(m.get(i));
   }
   s.close();
 }
Пример #6
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();
  }
Пример #7
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();
  }
Пример #8
0
 private void testDefragment() {
   String fileName = getBaseDir() + "/testDefragment.h3";
   FileUtils.delete(fileName);
   long initialLength = 0;
   for (int j = 0; j < 20; j++) {
     MVStore s = openStore(fileName);
     MVMap<Integer, String> m = s.openMap("data", Integer.class, String.class);
     for (int i = 0; i < 10; i++) {
       m.put(j + i, "Hello " + j);
     }
     s.store();
     s.compact(80);
     s.close();
     long len = FileUtils.size(fileName);
     // System.out.println("   len:" + len);
     if (initialLength == 0) {
       initialLength = len;
     } else {
       assertTrue("initial: " + initialLength + " len: " + len, len <= initialLength * 3);
     }
   }
   // long len = FileUtils.size(fileName);
   // System.out.println("len0: " + len);
   MVStore s = openStore(fileName);
   MVMap<Integer, String> m = s.openMap("data", Integer.class, String.class);
   for (int i = 0; i < 100; i++) {
     m.remove(i);
   }
   s.store();
   s.compact(80);
   s.close();
   // len = FileUtils.size(fileName);
   // System.out.println("len1: " + len);
   s = openStore(fileName);
   m = s.openMap("data", Integer.class, String.class);
   s.compact(80);
   s.close();
   // len = FileUtils.size(fileName);
   // System.out.println("len2: " + len);
 }
Пример #9
0
 private void testRandom() {
   String fileName = getBaseDir() + "/testRandom.h3";
   FileUtils.delete(fileName);
   MVStore s = openStore(fileName);
   MVMap<Integer, Integer> m = s.openMap("data", Integer.class, Integer.class);
   TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
   Random r = new Random(1);
   int operationCount = 1000;
   int maxValue = 30;
   Integer expected, got;
   for (int i = 0; i < operationCount; i++) {
     int k = r.nextInt(maxValue);
     int v = r.nextInt();
     boolean compareAll;
     switch (r.nextInt(3)) {
       case 0:
         log(i + ": put " + k + " = " + v);
         expected = map.put(k, v);
         got = m.put(k, v);
         if (expected == null) {
           assertNull(got);
         } else {
           assertEquals(expected, got);
         }
         compareAll = true;
         break;
       case 1:
         log(i + ": remove " + k);
         expected = map.remove(k);
         got = m.remove(k);
         if (expected == null) {
           assertNull(got);
         } else {
           assertEquals(expected, got);
         }
         compareAll = true;
         break;
       default:
         Integer a = map.get(k);
         Integer b = m.get(k);
         if (a == null || b == null) {
           assertTrue(a == b);
         } else {
           assertEquals(a.intValue(), b.intValue());
         }
         compareAll = false;
         break;
     }
     if (compareAll) {
       Iterator<Integer> it = m.keyIterator(null);
       Iterator<Integer> itExpected = map.keySet().iterator();
       while (itExpected.hasNext()) {
         assertTrue(it.hasNext());
         expected = itExpected.next();
         got = it.next();
         assertEquals(expected, got);
       }
       assertFalse(it.hasNext());
     }
   }
   s.close();
 }