Ejemplo n.º 1
1
  @Test
  public void submapToString() {

    for (int i = 0; i < 20; i++) {
      m.put(i, "aa" + i);
    }

    Map submap = m.subMap(10, true, 13, true);
    assertEquals("{10=aa10, 11=aa11, 12=aa12, 13=aa13}", submap.toString());
  }
Ejemplo n.º 2
0
  @Test
  public void mod_listener_lock() {
    DB db = DBMaker.memoryDB().transactionDisable().make();
    final BTreeMap m = db.treeMap("name");

    final long rootRecid = db.getEngine().get(m.rootRecidRef, Serializer.RECID);
    final AtomicInteger counter = new AtomicInteger();

    m.modificationListenerAdd(
        new Bind.MapListener() {
          @Override
          public void update(Object key, Object oldVal, Object newVal) {
            assertTrue(m.nodeLocks.get(rootRecid) == Thread.currentThread());
            assertEquals(1, m.nodeLocks.size());
            counter.incrementAndGet();
          }
        });

    m.put("aa", "aa");
    m.put("aa", "bb");
    m.remove("aa");

    m.put("aa", "aa");
    m.remove("aa", "aa");
    m.putIfAbsent("aa", "bb");
    m.replace("aa", "bb", "cc");
    m.replace("aa", "cc");

    assertEquals(8, counter.get());
  }
Ejemplo n.º 3
0
  @Test
  public void floorTestFill() {

    m.put(1, "val1");
    m.put(2, "val2");
    m.put(5, "val3");

    assertEquals(5, m.floorKey(5));
    assertEquals(1, m.floorKey(1));
    assertEquals(2, m.floorKey(2));
    assertEquals(2, m.floorKey(3));
    assertEquals(2, m.floorKey(4));
    assertEquals(5, m.floorKey(5));
    assertEquals(5, m.floorKey(6));
  }
Ejemplo n.º 4
0
  @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());
    }
  }
Ejemplo n.º 5
0
 @Test
 public void root_leaf_insert() {
   BTreeMap m = new BTreeMap(engine, 6, true, false, null, null, null, null);
   m.put(11, 12);
   BTreeMap.LeafNode n = (BTreeMap.LeafNode) engine.recordGet(m.rootRecid, m.nodeSerializer);
   assertArrayEquals(new Object[] {null, 11, null}, n.keys);
   assertArrayEquals(new Object[] {null, 12, null}, n.vals);
   assertEquals(0, n.next);
 }
Ejemplo n.º 6
0
  @Test
  public void test_size() {

    assertTrue(m.isEmpty());
    assertEquals(0, m.size());
    for (int i = 1; i < 30; i++) {
      m.put(i, i);
      assertEquals(i, m.size());
      assertFalse(m.isEmpty());
    }
  }
Ejemplo n.º 7
0
  @Test
  public void root_leaf_insert() {
    if (valsOutside) return;

    m.put(11, 12);
    final long rootRecid = engine.get(m.rootRecidRef, Serializer.RECID);
    BTreeMap.LeafNode n = (BTreeMap.LeafNode) engine.get(rootRecid, m.nodeSerializer);
    assertTrue(Arrays.equals(new Object[] {null, 11, null}, nodeKeysToArray(n)));
    assertTrue(Arrays.equals(new Object[] {12}, (Object[]) n.vals));
    assertEquals(0, n.next);
  }
Ejemplo n.º 8
0
 @Test
 public void test_size() {
   BTreeMap m = new BTreeMap(engine, 6, true, false, null, null, null, null);
   assertTrue(m.isEmpty());
   assertEquals(0, m.size());
   for (int i = 1; i < 30; i++) {
     m.put(i, i);
     assertEquals(i, m.size());
     assertFalse(m.isEmpty());
   }
 }
Ejemplo n.º 9
0
  @Test
  public void batch_insert() {

    for (int i = 0; i < 1000; i++) {
      m.put(i * 10, i * 10 + 1);
    }

    for (int i = 0; i < 10000; i++) {
      assertEquals(i % 10 == 0 ? i + 1 : null, m.get(i));
    }
  }
Ejemplo n.º 10
0
  @Test
  public void batch_insert() {
    BTreeMap m = new BTreeMap(engine, 6, true, false, null, null, null, null);

    for (int i = 0; i < 1000; i++) {
      m.put(i * 10, i * 10 + 1);
    }

    for (int i = 0; i < 10000; i++) {
      assertEquals(i % 10 == 0 ? i + 1 : null, m.get(i));
    }
  }
Ejemplo n.º 11
0
 @Test
 public void delete() {
   BTreeMap m = new BTreeMap(engine, 6, true, false, null, null, null, null);
   for (int i :
       new int[] {
         10, 50, 20, 42,
         // 44, 68, 20, 93, 85, 71, 62, 77, 4, 37, 66
       }) {
     m.put(i, i);
   }
   assertEquals(10, m.remove(10));
   assertEquals(20, m.remove(20));
   assertEquals(42, m.remove(42));
 }
Ejemplo n.º 12
0
  @Test
  public void test_key_iterator() {
    BTreeMap m = new BTreeMap(engine, 6, true, false, null, null, null, null);
    for (int i = 0; i < 20; i++) {
      m.put(i, i * 10);
    }

    Iterator iter = m.keySet().iterator();

    for (int i = 0; i < 20; i++) {
      assertTrue(iter.hasNext());
      assertEquals(i, iter.next());
    }
    assertFalse(iter.hasNext());
  }
Ejemplo n.º 13
0
  @Test
  public void test_key_iterator() {

    for (int i = 0; i < 20; i++) {
      m.put(i, i * 10);
    }

    Iterator iter = m.keySet().iterator();

    for (int i = 0; i < 20; i++) {
      assertTrue(iter.hasNext());
      assertEquals(i, iter.next());
    }
    assertFalse(iter.hasNext());
  }
Ejemplo n.º 14
0
  @Test
  public void delete() {

    for (int i :
        new int[] {
          10, 50, 20, 42,
          // 44, 68, 20, 93, 85, 71, 62, 77, 4, 37, 66
        }) {
      m.put(i, i);
    }
    assertEquals(10, m.remove(10));
    assertEquals(20, m.remove(20));
    assertEquals(42, m.remove(42));

    assertEquals(null, m.remove(42999));
  }
Ejemplo n.º 15
0
  @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();
  }
Ejemplo n.º 16
0
 /**
  * Compact the storage by moving all live pages to new chunks.
  *
  * @return if anything was written
  */
 public synchronized boolean compactRewriteFully() {
   checkOpen();
   if (lastChunk == null) {
     // nothing to do
     return false;
   }
   BTreeCursor<?, ?> cursor = (BTreeCursor<?, ?>) map.cursor(null);
   BTreePage lastPage = null;
   while (cursor.hasNext()) {
     cursor.next();
     BTreePage p = cursor.getPage();
     if (p == lastPage) {
       continue;
     }
     Object k = p.getKey(0);
     Object v = p.getValue(0);
     map.put(k, v);
     lastPage = p;
   }
   commitAndSave();
   // TODO 删除之前的所有chunk
   return true;
 }
Ejemplo n.º 17
0
  @Test
  public void findSmaller() {

    for (int i = 0; i < 10000; i += 3) {
      m.put(i, "aa" + i);
    }

    for (int i = 0; i < 10000; i += 1) {
      Integer s = i - i % 3;
      Map.Entry e = m.findSmaller(i, true);
      assertEquals(s, e != null ? e.getKey() : null);
    }

    assertEquals(9999, m.findSmaller(100000, true).getKey());

    assertNull(m.findSmaller(0, false));
    for (int i = 1; i < 10000; i += 1) {
      Integer s = i - i % 3;
      if (s == i) s -= 3;
      Map.Entry e = m.findSmaller(i, false);
      assertEquals(s, e != null ? e.getKey() : null);
    }
    assertEquals(9999, m.findSmaller(100000, false).getKey());
  }