public static void main(String args[]) {
   BTree bTree = new BTree();
   bTree.insert(5);
   bTree.insert(4);
   bTree.insert(3);
   bTree.insert(2);
   bTree.insert(1);
   bTree.insert(6);
   bTree.insert(11);
   bTree.insert(13);
   bTree.insert(8);
   bTree.insert(7);
   bTree.insert(10);
   bTree.insert(9);
   bTree.insert(28);
   bTree.insert(22);
   bTree.insert(12);
   bTree.insert(18);
   bTree.insert(16);
   bTree.traverse();
   System.out.print(bTree.search(28));
   System.out.print(bTree.search(11));
   System.out.print(bTree.search(5));
   System.out.print(bTree.search(21));
   System.out.print(bTree.search(3));
   System.out.print(bTree.search(4));
   System.out.print(bTree.search(14));
 }
  private void addRandom(int valueCount) throws IOException {
    Random random = new Random(0L);

    byte[] data = new byte[8];
    for (int i = 0; i < valueCount; i++) {
      ByteArrayUtil.putLong(random.nextLong(), data, 0);
      btree.insert(data);
    }
  }
  private void addAscending(long startValue, long increment, int valueCount) throws IOException {
    long value = startValue;

    byte[] data = new byte[8];
    for (int i = 0; i < valueCount; i++) {
      ByteArrayUtil.putLong(value, data, 0);
      btree.insert(data);
      value += increment;
    }
  }
  /**
   * Create, populate, and return a btree with a branching factor of (3) and ten sequential keys
   * [1:10]. The values are {@link SimpleEntry} objects whose state is the same as the corresponding
   * key.
   *
   * @return The btree.
   * @see src/architecture/btree.xls, which details this input tree and a series of output trees
   *     with various branching factors.
   */
  public BTree getProblem1() {

    final BTree btree = getBTree(3);

    for (int i = 1; i <= 10; i++) {

      btree.insert(TestKeyBuilder.asSortKey(i), new SimpleEntry(i));
    }

    return btree;
  }
  private void update(long startValue, long increment, int valueCount, long updateDelta)
      throws IOException {
    long oldValue = startValue;
    long newValue;

    byte[] oldData = new byte[8];
    byte[] newData = new byte[8];

    for (int i = 0; i < valueCount; i++) {
      newValue = oldValue += updateDelta;

      ByteArrayUtil.putLong(oldValue, oldData, 0);
      ByteArrayUtil.putLong(newValue, newData, 0);

      btree.insert(newData);
      btree.remove(oldData);

      oldValue += increment;
    }
  }
Exemple #6
0
  /**
   * Tests the ability to store a <code>null</code> in a tuple of a {@link BTree}, to reload the
   * {@link BTree} and find the <code>null</code> value still under the tuple, and to build an
   * {@link IndexSegmentBuilder} from the {@link BTree} and find the <code>null</code> value under
   * the tuple.
   *
   * @throws IOException
   * @throws Exception
   */
  public void test_nullValues() throws IOException, Exception {

    final IRawStore store = new SimpleMemoryRawStore();

    final IndexMetadata metadata = new IndexMetadata(UUID.randomUUID());

    BTree btree = BTree.create(store, metadata);

    final byte[] k1 = new byte[] {1};

    assertNull(btree.lookup(k1));
    assertFalse(btree.contains(k1));

    assertNull(btree.insert(k1, null));

    assertNull(btree.lookup(k1));
    assertTrue(btree.contains(k1));

    final long addrCheckpoint1 = btree.writeCheckpoint();

    btree = BTree.load(store, addrCheckpoint1, true /*readOnly*/);

    assertNull(btree.lookup(k1));
    assertTrue(btree.contains(k1));

    File outFile = null;
    File tmpDir = null;

    try {

      outFile = new File(getName() + ".seg");

      if (outFile.exists() && !outFile.delete()) {

        throw new RuntimeException("Could not delete file: " + outFile);
      }

      tmpDir = outFile.getAbsoluteFile().getParentFile();

      final long commitTime = System.currentTimeMillis();

      @SuppressWarnings("unused")
      final IndexSegmentCheckpoint checkpoint =
          IndexSegmentBuilder.newInstance(
                  outFile,
                  tmpDir,
                  btree.getEntryCount(),
                  btree.rangeIterator(),
                  3 /* m */,
                  btree.getIndexMetadata(),
                  commitTime,
                  true /* compactingMerge */,
                  bufferNodes)
              .call();

      //          @see BLZG-1501 (remove LRUNexus)
      //            if (LRUNexus.INSTANCE != null) {
      //
      //                /*
      //                 * Clear the records for the index segment from the cache so we will
      //                 * read directly from the file. This is necessary to ensure that the
      //                 * data on the file is good rather than just the data in the cache.
      //                 */
      //
      //                LRUNexus.INSTANCE.deleteCache(checkpoint.segmentUUID);
      //
      //            }

      /*
       * Verify can load the index file and that the metadata associated
       * with the index file is correct (we are only checking those
       * aspects that are easily defined by the test case and not, for
       * example, those aspects that depend on the specifics of the length
       * of serialized nodes or leaves).
       */

      final IndexSegmentStore segStore = new IndexSegmentStore(outFile);

      final IndexSegment seg = segStore.loadIndexSegment();

      try {

        assertNull(seg.lookup(k1));
        assertTrue(seg.contains(k1));

      } finally {

        seg.close();
      }

    } finally {

      if (outFile != null && outFile.exists() && !outFile.delete()) {

        log.warn("Could not delete file: " + outFile);
      }
    }
  }