Example #1
1
  @Test
  public void testOptimizeShouldNotThrow() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys).close();
    Map.optimize(DIRECTORY, DIRECTORY2);

    Map map = new Map(DIRECTORY2);
    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      for (int j = 0; j < numValuesPerKeys; ++j) {
        Assert.assertTrue(iter.hasNext());
        Assert.assertEquals(j, getSuffix(iter.next()));
      }
      iter.close();
    }
    map.close();
  }
Example #2
0
  @Test
  public void testReplaceAllValues() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    Map map = createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys);

    for (int i = 0; i < numKeys; ++i) {
      long expectedNumReplaced = 1;
      long actualNumRemoved = map.replaceAllEqual(makeKey(i), makeValue(123), makeValue(124));
      Assert.assertEquals(expectedNumReplaced, actualNumRemoved);
    }

    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      for (int j = 0; j < numValuesPerKeys; ++j) {
        if (j == 123) {
          // This value has been removed.
        } else {
          Assert.assertTrue(iter.hasNext());
          Assert.assertArrayEquals(makeValue(j), iter.nextAsByteArray());
        }
      }
      // The last element is the replaced value.
      Assert.assertTrue(iter.hasNext());
      Assert.assertArrayEquals(makeValue(124), iter.nextAsByteArray());
      Assert.assertFalse(iter.hasNext());
      Assert.assertEquals(0, iter.available());
      iter.close();
    }
    map.close();
  }
Example #3
0
  @Test
  public void testExportToBase64ShouldNotThrow() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys).close();
    Map.exportToBase64(DIRECTORY, DATAFILE);

    // Clear the map.
    Map map = new Map(DIRECTORY);
    // TODO Check return value, which is a pair now.
    Utils.Pair<Integer, Long> result =
        map.removeAllMatches(
            new Predicate() {
              @Override
              public boolean call(ByteBuffer bytes) {
                return true;
              }
            });
    Assert.assertEquals(numKeys, result.a().intValue());
    Assert.assertEquals(numKeys * numValuesPerKeys, result.b().longValue());
    map.close();

    // Import and check content.
    Map.importFromBase64(DIRECTORY, DATAFILE);
    map = new Map(DIRECTORY);
    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      for (int j = 0; j < numValuesPerKeys; ++j) {
        Assert.assertTrue(iter.hasNext());
        Assert.assertEquals(j, getSuffix(iter.next()));
      }
      iter.close();
    }
    map.close();
  }
Example #4
0
  @Test
  public void testReplaceAllValuesViaFunction() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    Map map = createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys);

    for (int i = 0; i < numKeys; ++i) {
      long expectedNumReplaced = numValuesPerKeys / 2;
      long actualNumReplaced = map.replaceAllMatches(makeKey(i), NEXT_IF_EVEN);
      Assert.assertEquals(expectedNumReplaced, actualNumReplaced);
    }

    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      for (int j = 1; j < numValuesPerKeys; j += 2) {
        Assert.assertTrue(iter.hasNext());
        Assert.assertArrayEquals(makeValue(j), iter.nextAsByteArray());
      }
      // Replaced values have been inserted at the end.
      for (int j = 1; j < numValuesPerKeys; j += 2) {
        Assert.assertTrue(iter.hasNext());
        Assert.assertArrayEquals(makeValue(j), iter.nextAsByteArray());
      }
      Assert.assertFalse(iter.hasNext());
      Assert.assertEquals(0, iter.available());
      iter.close();
    }
    map.close();
  }
Example #5
0
  @Test
  public void testRemoveAllValuesViaPredicate() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    Map map = createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys);

    for (int i = 0; i < numKeys; ++i) {
      long expectedNumRemoved = numValuesPerKeys / 2;
      long actualNumRemoved = map.removeAllMatches(makeKey(i), IS_EVEN);
      Assert.assertEquals(expectedNumRemoved, actualNumRemoved);
    }

    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      for (int j = 0; j < numValuesPerKeys; ++j) {
        byte[] value = makeValue(j);
        if (IS_EVEN.call(ByteBuffer.wrap(value))) {
          // This value has been removed.
        } else {
          Assert.assertTrue(iter.hasNext());
          Assert.assertArrayEquals(value, iter.nextAsByteArray());
        }
      }
      Assert.assertFalse(iter.hasNext());
      Assert.assertEquals(0, iter.available());
      iter.close();
    }
    map.close();
  }
Example #6
0
  @Test
  public void testReplaceOneValueViaFunction() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    Map map = createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys);

    for (int i = 0; i < numKeys; ++i) {
      boolean replaced = map.replaceFirstMatch(makeKey(i), NEXT_IF_EVEN);
      Assert.assertTrue(replaced);
    }

    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      for (int j = 1; j < numValuesPerKeys; ++j) {
        Assert.assertTrue(iter.hasNext());
        Assert.assertArrayEquals(makeValue(j), iter.nextAsByteArray());
      }
      // The last element is the replaced value.
      Assert.assertTrue(iter.hasNext());
      Assert.assertArrayEquals(makeValue(1), iter.nextAsByteArray());
      Assert.assertFalse(iter.hasNext());
      Assert.assertEquals(0, iter.available());
      iter.close();
    }
    map.close();
  }
Example #7
0
  @Test
  public void testRemoveOneValueViaPredicate() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    Map map = createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys);

    for (int i = 0; i < numKeys; ++i) {
      boolean removed = map.removeFirstMatch(makeKey(i), IS_EVEN);
      Assert.assertTrue(removed);
    }

    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      int j = 0;
      for (; j < numValuesPerKeys; ++j) {
        byte[] value = makeValue(j);
        if (IS_EVEN.call(ByteBuffer.wrap(value))) {
          // This value has been removed.
          break;
        } else {
          Assert.assertTrue(iter.hasNext());
          Assert.assertArrayEquals(value, iter.nextAsByteArray());
        }
      }
      for (++j; j < numValuesPerKeys; ++j) {
        Assert.assertTrue(iter.hasNext());
        Assert.assertArrayEquals(makeValue(j), iter.nextAsByteArray());
      }
      Assert.assertFalse(iter.hasNext());
      Assert.assertEquals(0, iter.available());
      iter.close();
    }
    map.close();
  }
Example #8
0
  @Test
  public void testRemoveOneValue() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    Map map = createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys);

    for (int i = 0; i < numKeys; ++i) {
      boolean removed = map.removeFirstEqual(makeKey(i), makeValue(123));
      Assert.assertTrue(removed);
    }

    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      int j = 0;
      for (; j < numValuesPerKeys; ++j) {
        if (j == 123) {
          // This value has been removed.
          break;
        } else {
          Assert.assertTrue(iter.hasNext());
          Assert.assertArrayEquals(makeValue(j), iter.nextAsByteArray());
        }
      }
      for (++j; j < numValuesPerKeys; ++j) {
        Assert.assertTrue(iter.hasNext());
        Assert.assertArrayEquals(makeValue(j), iter.nextAsByteArray());
      }
      Assert.assertFalse(iter.hasNext());
      Assert.assertEquals(0, iter.available());
      iter.close();
    }
    map.close();
  }
Example #9
0
 @Test
 public void testGet() throws Exception {
   int numKeys = 1000;
   int numValuesPerKeys = 1000;
   Map map = createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys);
   for (int i = 0; i < numKeys; ++i) {
     Iterator iter = map.get(makeKey(i));
     for (int j = 0; j < numValuesPerKeys; ++j) {
       Assert.assertTrue(iter.hasNext());
       Assert.assertEquals(numValuesPerKeys - j, iter.available());
       Assert.assertArrayEquals(makeValue(j), iter.nextAsByteArray());
     }
     Assert.assertFalse(iter.hasNext());
     Assert.assertEquals(0, iter.available());
     iter.close();
   }
   map.close();
 }
Example #10
0
  @Test
  public void testImportFromBase64ShouldNotThrow() throws Exception {
    int numKeys = 1000;
    int numValuesPerKeys = 1000;
    writeAsBase64ToFile(DATAFILE, numKeys, numValuesPerKeys);

    Options options = new Options();
    options.setCreateIfMissing(true);
    Map.importFromBase64(DIRECTORY, DATAFILE, options);

    Map map = new Map(DIRECTORY);
    for (int i = 0; i < numKeys; ++i) {
      Iterator iter = map.get(makeKey(i));
      for (int j = 0; j < numValuesPerKeys; ++j) {
        Assert.assertTrue(iter.hasNext());
        Assert.assertEquals(j, getSuffix(iter.next()));
      }
      iter.close();
    }
    map.close();
  }
Example #11
0
 @Test
 public void testRemove() throws Exception {
   int numKeys = 1000;
   int numValuesPerKeys = 1000;
   Map map = createAndFillMap(DIRECTORY, numKeys, numValuesPerKeys);
   for (int i = 0; i < numKeys; i += 2) {
     Assert.assertEquals(numValuesPerKeys, map.remove(makeKey(i)));
   }
   for (int i = 0; i < numKeys; ++i) {
     Iterator iter = map.get(makeKey(i));
     if (i % 2 == 0) {
       Assert.assertFalse(iter.hasNext());
       Assert.assertEquals(0, iter.available());
     } else {
       Assert.assertTrue(iter.hasNext());
       Assert.assertEquals(numValuesPerKeys, iter.available());
     }
     iter.close();
   }
   map.close();
 }
Example #12
0
 /** Closes the iterator, releasing any resources (i.e. pinned pages). */
 public void close() {
   // throw new UnsupportedOperationException("Not implemented");
   open = false;
   iter.close();
 }