Package net.sf.joafip.btreeplus.entity

Examples of net.sf.joafip.btreeplus.entity.NonTerminalPage


    assertEquals(BAD_RECORD_POSITION, PageConstant.PAGE_SIZE * 1,
        pageRecord.getPositionInFile());
    assertEquals(BAD_LAST_RECORD_POSITION, PageConstant.PAGE_SIZE * 1,
        btreePlusElementMgr.getLastRecordPositionInFile());

    NonTerminalPage nonTerminalPage = new NonTerminalPage(1, true);
    nonTerminalPage.setKey(0, new DataRecordIdentifier());
    pageRecordable = nonTerminalPage;
    pageRecordable.updateByteSize();
    assertNull(MUST_BE_NOT_STORED, pageRecordable.getPageRecord());
    btreePlusElementMgr.appendPageRecordable(pageRecordable);
    pageRecordable.setValueIsChangedValueToSave();
    pageRecord = pageRecordable.getPageRecord();
    assertNotNull(MUST_BE_STORED, pageRecord);
    assertEquals(BAD_RECORD_POSITION, PageConstant.PAGE_SIZE * 2,
        pageRecord.getPositionInFile());
    assertEquals(BAD_LAST_RECORD_POSITION, PageConstant.PAGE_SIZE * 2,
        btreePlusElementMgr.getLastRecordPositionInFile());
    btreePlusElementMgr.closeTransaction();
    assertEquals("", PageConstant.PAGE_SIZE * 3, btreePlusElementMgr
        .getFileForStorable().getFileSize());

    btreePlusElementMgr.openTransaction();
    final int numberOfKeyEntries = 1;
    final DataRecordIdentifier[] keys = new DataRecordIdentifier[] { new DataRecordIdentifier(
        0) };
    final long[] pagePosition = new long[] { 0, 1 };
    pageRecordable = new NonTerminalPage(numberOfKeyEntries, keys,
        pagePosition, true);
    assertNull(MUST_BE_NOT_STORED, pageRecordable.getPageRecord());
    btreePlusElementMgr.appendPageRecordable(pageRecordable);
    pageRecordable.setValueIsChangedValueToSave();
    pageRecord = pageRecordable.getPageRecord();
    assertNotNull(MUST_BE_STORED, pageRecord);
    assertEquals(BAD_RECORD_POSITION, PageConstant.PAGE_SIZE * 3,
        pageRecord.getPositionInFile());
    assertEquals(BAD_LAST_RECORD_POSITION, PageConstant.PAGE_SIZE * 3,
        btreePlusElementMgr.getLastRecordPositionInFile());
    btreePlusElementMgr.closeTransaction();
    assertEquals("", PageConstant.PAGE_SIZE * 4, btreePlusElementMgr
        .getFileForStorable().getFileSize());

    btreePlusElementMgr.openTransaction();
    long position = PageConstant.PAGE_SIZE;
    pageRecordable = btreePlusElementMgr.getPage(position, null, 0);
    assertTrue("leaf page expected", pageRecordable instanceof LeafPage);
    position += PageConstant.PAGE_SIZE;
    pageRecordable = btreePlusElementMgr.getPage(position, null, 0);
    assertTrue("non terminal page expected",
        pageRecordable instanceof NonTerminalPage);
    nonTerminalPage = (NonTerminalPage) pageRecordable;
    assertEquals("bad non terminal page #1 state", 1,
        nonTerminalPage.getNumberOfKeyEntries());
    position += PageConstant.PAGE_SIZE;
    pageRecordable = btreePlusElementMgr.getPage(position, null, 0);
    assertTrue("non terminal page expected",
        pageRecordable instanceof NonTerminalPage);
    nonTerminalPage = (NonTerminalPage) pageRecordable;
    assertEquals("bad non terminal page #2 state", 1,
        nonTerminalPage.getNumberOfKeyEntries());
    btreePlusElementMgr.closeTransactionDiscardChange();
  }
View Full Code Here


        currentDepth--;
        stack.pop();
      }
        break;
      case NON_TERMINAL_PAGE: {
        final NonTerminalPage nonTerminalPage = (NonTerminalPage) pageRecordable;
        if (index == 0) {

          // check keys
          DataRecordIdentifier currentKey = nonTerminalPage.getKey(0);
          for (int kindex = 1; kindex < nonTerminalPage
              .getNumberOfKeyEntries(); kindex++) {
            final DataRecordIdentifier nextKey = nonTerminalPage
                .getKey(kindex);
            if (compare(currentKey, nextKey) >= 0) {
              throw new HeapException("bad keys");
            }
            currentKey = nextKey;
          }

          // check non terminal page value range
          final DataRecordIdentifier firstKey = nonTerminalPage
              .getFirstKey();
          final DataRecordIdentifier lastKey = nonTerminalPage
              .getLastKey();
          state = stack.peek();
          if (/* state.minValue > firstKeyValue */
          compare(state.minKey, firstKey) >= 0 || /*
                               * state.maxValue <
                               * lastKeyValue
                               */
          compare(state.maxKey, lastKey) < 0) {
            throw new HeapException("not have " + state.minKey
                + "(min value)<" + firstKey
                + "(first key value)  " + lastKey
                + "(last key value) <=" + state.maxKey
                + "(max value)\nnon terminal page at "
                + nonTerminalPage.getPositionInFile());
          }

          // // check well formed
          // if (currentDepth != 0 && !nonTerminalPage.wellFilled()) {
          // throw new HeapException("not well filled");
          // }

          // count non terminal page
          nonTerminalPageCount++;
        }

        // go up or down
        if (index > nonTerminalPage.getNumberOfKeyEntries()) {
          // go up
          pageRecordable = nonTerminalPage.getParentPage();
          index = nonTerminalPage.getInParentIndex() + 1;
          currentDepth--;
          stack.pop();
        } else {
          // go down
          final long position = nonTerminalPage.getPagePointer(index);
          pageRecordable = elementMgr.getPage(position,
              pageRecordable, index);
          state = stack.peek();
          if (state == null) {
            throw new HeapException("stack must not be empty");
          }
          final DataRecordIdentifier minKey = index == 0 ? state.minKey
              : nonTerminalPage.getKey(index - 1);
          final DataRecordIdentifier maxKey = index == nonTerminalPage
              .getNumberOfKeyEntries() ? state.maxKey
              : nonTerminalPage.getKey(index);
          stack.push(new State(minKey, maxKey));// NOPMD
          index = 0;
          currentDepth++;
        }
      }
View Full Code Here

        // data record not found
        created = true;
        final IDataBlock dataBlock = btreePlusElementMgr
            .createDataBlock(data);
        if (!leafPage.add(dataRecordIdentifier, dataBlock)) {
          final NonTerminalPage nonTerminalPage = (NonTerminalPage) leafPage
              .getParentPage();
          if (nonTerminalPage == null) {
            splitLeafPage(leafPage, dataRecordIdentifier,
                nonTerminalPage, dataBlock);
          } else if (tryBalanceOrMerge(leafPage, nonTerminalPage)) {
View Full Code Here

      deleted = true;
      final LeafPage leafPage = dataBlock.getParentLeafPage();
      final int indexInLeafPage = dataBlock.getIndexInLeafPage();
      btreePlusElementMgr.remove(dataBlock);
      leafPage.remove(indexInLeafPage);
      final NonTerminalPage nonTerminalPage = (NonTerminalPage) leafPage
          .getParentPage();
      if (nonTerminalPage == null) {
        if (leafPage.getNumberOfKeyEntries() == 0) {
          btreePlusElementMgr.freePage(leafPage.getPageRecord());
          btreePlusElementMgr.removeRoot();
        }
      } else {
        if (leafPage.wellFilled()) {
          final int leafPageInParentIndex = leafPage
              .getInParentIndex();
          if (leafPageInParentIndex != nonTerminalPage
              .getNumberOfKeyEntries()
              && !nonTerminalPage.setKey(leafPageInParentIndex,
                  leafPage.getLastKey())) {
            split(nonTerminalPage);
          }
        } else {
          tryBalanceOrMerge(leafPage, nonTerminalPage);
View Full Code Here

          .getAndClearMiddleKey();
      // ASSERTX
      assert middleKey != null;
      btreePlusElementMgr.appendPageRecordable(newNonTerminalPage);
      newNonTerminalPage.setValueIsChangedValueToSave();
      final NonTerminalPage parent = (NonTerminalPage) nonTerminalPage
          .getParentPage();
      if (parent == null) {
        btreePlusElementMgr.newRootNonTerminalPage(nonTerminalPage,
            middleKey, newNonTerminalPage);
      } else if (!parent.add(nonTerminalPage, middleKey,
          newNonTerminalPage)) {
        split(parent);
      }
    }
  }
View Full Code Here

   * @return true if change made
   * @throws HeapException
   */
  private boolean tryBalance(final NonTerminalPage nonTerminalPage)
      throws HeapException {
    final NonTerminalPage parentNonterminal = (NonTerminalPage) nonTerminalPage
        .getParentPage();
    int result;
    if (parentNonterminal == null) {
      result = 2;
    } else {
      final int inParentIndex = nonTerminalPage.getInParentIndex();
      if (inParentIndex == 0) {
        result = 2;
      } else {
        final int leftPageInParentIndex = inParentIndex - 1;
        final long leftPagePosition = parentNonterminal
            .getPagePointer(leftPageInParentIndex);
        final NonTerminalPage leftPage = (NonTerminalPage) btreePlusElementMgr
            .getPage(leftPagePosition, parentNonterminal,
                leftPageInParentIndex);
        // final long middlePagePosition =
        // leftPage.getLastPagePosition();
        // final AbstractNodePage middlePage = (AbstractNodePage)
        // btreePlusElementMgr.getPage(middlePagePosition, null,-1);
        // final DataRecordIdentifier middleKey=middlePage.getLastKey();
        final DataRecordIdentifier middleKey = parentNonterminal
            .getKey(leftPageInParentIndex);
        result = leftPage.tryBalanceOrMerge(middleKey, nonTerminalPage);
        if (result == 0) {
          // page merged in left page
          btreePlusElementMgr.freePage(nonTerminalPage
              .getPageRecord());
          parentNonterminal.remove(inParentIndex);
          if (parentNonterminal.setKey(leftPageInParentIndex,
              leftPage.getAndClearMiddleKey())) {
            if (!parentNonterminal.wellFilled()) {
              tryBalance(parentNonterminal);
            }
          } else {
            split(nonTerminalPage);
          }
        } else if (result == 1
            && !parentNonterminal.setKey(leftPageInParentIndex,
                leftPage.getAndClearMiddleKey())) {
          split(nonTerminalPage);
        }
      }

      if (result == 2
          && inParentIndex != parentNonterminal
              .getNumberOfKeyEntries()) {
        final int rightPageinParentIndex = inParentIndex + 1;
        final long rightPagePosition = parentNonterminal
            .getPagePointer(rightPageinParentIndex);
        final NonTerminalPage rightPage = (NonTerminalPage) btreePlusElementMgr
            .getPage(rightPagePosition, parentNonterminal,
                rightPageinParentIndex);
        // final long middlePagePosition =
        // nonTerminalPage.getLastPagePosition();
        // final AbstractNodePage middlePage = (AbstractNodePage)
        // btreePlusElementMgr.getPage(middlePagePosition, null,-1);
        // final DataRecordIdentifier middleKey=middlePage.getLastKey();
        final DataRecordIdentifier middleKey = parentNonterminal
            .getKey(inParentIndex);
        result = nonTerminalPage
            .tryBalanceOrMerge(middleKey, rightPage);
        if (result == 0) {
          // right leaf page merged in leaf page
          btreePlusElementMgr.freePage(rightPage.getPageRecord());
          parentNonterminal.remove(rightPageinParentIndex);
          if (parentNonterminal.setKey(inParentIndex,
              nonTerminalPage.getAndClearMiddleKey())) {
            if (!parentNonterminal.wellFilled()) {
              tryBalance(parentNonterminal);
View Full Code Here

    if (root == null) {
      first = null;
    } else {
      IPageRecordable page = root;
      while (!EnumRecordType.LEAF_PAGE.equals(page.getRecordType())) {
        final NonTerminalPage nonTerminalPage = (NonTerminalPage) page;
        final long pagePosition = nonTerminalPage.getPagePointer(0);
        page = btreePlusElementMgr.getPage(pagePosition, page, 0);
      }
      final LeafPage leafPage = (LeafPage) page;
      first = leafPage.getFirstKey();
      final boolean deleted = deleteDataRecordImpl(first);
View Full Code Here

    if (root == null) {
      page = null;// NOPMD
    } else {
      page = root;
      while (!EnumRecordType.LEAF_PAGE.equals(page.getRecordType())) {
        final NonTerminalPage nonTerminalPage = (NonTerminalPage) page;
        final int index = nonTerminalPage
            .getIndex(dataRecordIdentifier);
        final long pagePosition = nonTerminalPage.getPagePointer(index);
        page = btreePlusElementMgr.getPage(pagePosition, page, index);
      }
    }
    return (LeafPage) page;
  }
View Full Code Here

    header.setFirstLeafPagePosition(firstLeafPagePosition);
  }

  public void newRootNonTerminalPage(final LeafPage leftLeafPage,
      final LeafPage rightLeafPage) throws HeapException {
    final NonTerminalPage nonTerminalPage = new NonTerminalPage(1, longKey);
    nonTerminalPage.setEntry(0, leftLeafPage.getPositionInFile(),
        leftLeafPage.getLastKey());
    nonTerminalPage.setEntry(1, rightLeafPage.getPositionInFile(), null);
    appendPageRecordable(nonTerminalPage);
    nonTerminalPage.updateByteSize();
    nonTerminalPage.setValueIsChangedValueToSave();
    header.setRootPagePosition(nonTerminalPage.getPositionInFile());
  }
View Full Code Here

  public void newRootNonTerminalPage(
      final INonTerminalPage leftNonTerminalPage,
      final DataRecordIdentifier middleKey,
      final INonTerminalPage rightNonTerminalPage) throws HeapException {
    final NonTerminalPage nonTerminalPage = new NonTerminalPage(1, longKey);
    nonTerminalPage.setEntry(0, leftNonTerminalPage.getPositionInFile(),
        middleKey);
    nonTerminalPage.setEntry(1, rightNonTerminalPage.getPositionInFile(),
        null);
    appendPageRecordable(nonTerminalPage);
    nonTerminalPage.updateByteSize();
    nonTerminalPage.setValueIsChangedValueToSave();
    header.setRootPagePosition(nonTerminalPage.getPositionInFile());
  }
View Full Code Here

TOP

Related Classes of net.sf.joafip.btreeplus.entity.NonTerminalPage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.