Package com.caucho.db.block

Examples of com.caucho.db.block.Block


   * Closes the buffer.
   */
  public void close()
  {
    if (_block != null) {
      Block block = _block;
      _block = null;
      block.free();
    }
  }
View Full Code Here


          int keyOffset,
          int keyLength)
    throws SQLException
  {
    try {
      Block rootBlock = _rootBlock; // _store.readBlock(_rootBlockId);
      rootBlock.allocate();

      try {
        if (! removeRead(rootBlock, keyBuffer, keyOffset, keyLength)) {
          removeWrite(rootBlock, keyBuffer, keyOffset, keyLength);
        }
      } finally {
  rootBlock.free();
      }
    } catch (IOException e) {
      throw new SQLExceptionWrapper(e.toString(), e);
    }
  }
View Full Code Here

   */
  public void readBlock()
    throws IOException
  {
    if (_block != null) {
      Block block = _block;
      _block = null;
      block.free();
    }

    long addr;

    int blockCount = (int) (_offset / BlockStore.BLOCK_SIZE);
     
    if (blockCount < INODE_DIRECT_BLOCKS) {
      addr = readLong(_inode, _inodeOffset + 8 * (blockCount + 1));
    }
    else {
      long ptrAddr = readLong(_inode,
            _inodeOffset + 8 * (INODE_DIRECT_BLOCKS + 1));

      Block ptr = _store.readBlock(_store.addressToBlockId(ptrAddr));

      addr = readLong(ptr.getBuffer(), 8 * (blockCount - INODE_DIRECT_BLOCKS));

      ptr.free();
    }

    _block = _store.readBlock(_store.addressToBlockId(addr));
    _buffer = _block.getBuffer();

View Full Code Here

                            false);

      if (childId == FAIL)
        return true;

      Block childBlock = _store.readBlock(childId);
       
      try {
        validateIndex(childBlock);
       
        if (removeRead(childBlock, keyBuffer, keyOffset, keyLength))
          return true;
        else
          return removeWrite(childBlock, keyBuffer, keyOffset, keyLength);
      } finally {
        childBlock.free();
      }
    } finally {
      blockLock.unlockRead();
    }
  }
View Full Code Here

                              isLeaf);

        if (childId == FAIL)
          return true;

        Block childBlock = _store.readBlock(childId);
        try {
          validateIndex(childBlock);

          boolean isJoin;

          isJoin = ! removeWrite(childBlock, keyBuffer, keyOffset, keyLength);

          if (isJoin && joinBlocks(block, childBlock)) {
            if (childBlock.getUseCount() > 2) {
              System.out.println("USE: " + childBlock.getUseCount() + " " + block + " " + block.getLock());
            }
            childBlock.deallocate();
          }

          validate(block);
        } finally {
          childBlock.free();
        }
      }
     
      return _minN <= getLength(buffer);
    } finally {
View Full Code Here

    long rightBlockId = getRightBlockId(parent, blockId);

    // If the left block has extra data, shift the last left item
    // to the block
    if (leftBlockId > 0) {
      Block leftBlock = _store.readBlock(leftBlockId);

      try {
        byte []leftBuffer = leftBlock.getBuffer();
 
        Lock leftLock = leftBlock.getLock();
        leftLock.lockReadAndWrite(_timeout);
   
        try {
          int leftLength = getLength(leftBuffer);

          Lock blockLock = block.getLock();
          blockLock.lockReadAndWrite(_timeout);
     
          try {
            if (_minN < leftLength) {
              validateEqualLeaf(buffer, leftBuffer, block, leftBlock);
             
              parent.setFlushDirtyOnCommit(false);
     
              leftBlock.setFlushDirtyOnCommit(false);
   
              validate(parentBlockId, parentBuffer);
              validate(leftBlockId, leftBuffer);
              validate(blockId, buffer);
             
              // System.out.println("MOVE_FROM_LEFT: " + debugId(blockId) + " from " + debugId(leftBlockId));
              moveFromLeft(parentBuffer, leftBuffer, buffer, blockId);
              validate(parentBlockId, parentBuffer);
              validate(leftBlockId, leftBuffer);
              validate(blockId, buffer);
             
              parent.setDirty(0, BlockStore.BLOCK_SIZE);
              leftBlock.setDirty(0, BlockStore.BLOCK_SIZE);

              return false;
            }
          } finally {
            blockLock.unlockReadAndWrite();
          }
        } finally {
          leftLock.unlockReadAndWrite();
        }
      } finally {
        leftBlock.free();
      }
    }

    // If the right block has extra data, shift the first right item
    // to the block
    if (rightBlockId > 0) {
      Block rightBlock = _store.readBlock(rightBlockId);

      try {
        byte []rightBuffer = rightBlock.getBuffer();
 
        Lock blockLock = block.getLock();
        blockLock.lockReadAndWrite(_timeout);

        try {
          Lock rightLock = rightBlock.getLock();
          rightLock.lockReadAndWrite(_timeout);
       
          try {
            int rightLength = getLength(rightBuffer);

            if (_minN < rightLength) {
              validateEqualLeaf(buffer, rightBuffer, block, rightBlock);
             
              parent.setFlushDirtyOnCommit(false);
     
              rightBlock.setFlushDirtyOnCommit(false);

              // System.out.println("MOVE_FROM_RIGHT: " + debugId(blockId) + " from " + debugId(rightBlockId));
     
              moveFromRight(parentBuffer, buffer, rightBuffer, blockId);
              validate(parentBlockId, parentBuffer);
              validate(blockId, buffer);
              validate(rightBlockId, rightBuffer);
             
              parent.setDirty(0, BlockStore.BLOCK_SIZE);
              rightBlock.setDirty(0, BlockStore.BLOCK_SIZE);

              return false;
            }
          } finally {
            rightLock.unlockReadAndWrite();
          }
        } finally {
          blockLock.unlockReadAndWrite();
        }
      } finally {
        rightBlock.free();
      }
    }

    if (parentLength < 2)
      return false;
   
    // If the left block has space, merge with it
    if (leftBlockId > 0) {
      Block leftBlock = _store.readBlock(leftBlockId);
     
      try {
        byte []leftBuffer = leftBlock.getBuffer();
   
        Lock leftLock = leftBlock.getLock();
        leftLock.lockReadAndWrite(_timeout);

        try {
          int leftLength = getLength(leftBuffer);
     
          Lock blockLock = block.getLock();
          blockLock.lockReadAndWrite(_timeout);

          try {
            int length = getLength(buffer);
       
            if (length + leftLength <= _n) {
              validateEqualLeaf(leftBuffer, buffer, leftBlock, block);
             
              parent.setFlushDirtyOnCommit(false);
   
              leftBlock.setFlushDirtyOnCommit(false);
     
              // System.out.println("MERGE_LEFT: " + debugId(blockId) + " from " + debugId(leftBlockId));
     
              mergeLeft(parentBuffer,
                        leftBuffer, leftBlockId,
                        buffer, blockId);
             
              validate(parentBlockId, parentBuffer);
              validate(leftBlockId, leftBuffer);
             
              parent.setDirty(0, BlockStore.BLOCK_SIZE);
              leftBlock.setDirty(0, BlockStore.BLOCK_SIZE);

              // System.out.println("FREE-ML: " + block);

              return true;
            }
          } finally {
            blockLock.unlockReadAndWrite();
          }
        } finally {
          leftLock.unlockReadAndWrite();
        }
      } finally {
        leftBlock.free();
      }
    }
   
    // If the right block has space, merge with it
    if (rightBlockId > 0) {
      Block rightBlock = _store.readBlock(rightBlockId);

      try {
        byte []rightBuffer = rightBlock.getBuffer();

        Lock blockLock = block.getLock();
        blockLock.lockReadAndWrite(_timeout);

        try {
          Lock rightLock = rightBlock.getLock();
          rightLock.lockReadAndWrite(_timeout);

          try {
            int length = getLength(buffer);
            int rightLength = getLength(rightBuffer);
       
            if (length + rightLength <= _n) {
              validateEqualLeaf(rightBuffer, buffer, rightBlock, block);

              rightBlock.setFlushDirtyOnCommit(false);
   
              parent.setFlushDirtyOnCommit(false);
   
              // System.out.println("MERGE_RIGHT: " + debugId(blockId) + " from " + debugId(rightBlockId));
     
              validate(blockId, buffer);
              validate(parentBlockId, parentBuffer);
              validate(rightBlockId, rightBuffer);
             
              mergeRight(parentBuffer, buffer, rightBuffer, blockId);
             
              validate(parentBlockId, parentBuffer);
              validate(rightBlockId, rightBuffer);
             
              rightBlock.setDirty(0, BlockStore.BLOCK_SIZE);
              parent.setDirty(0, BlockStore.BLOCK_SIZE);

              // System.out.println("FREE-MR: " + block);

              return true;
            }
          } finally {
            rightLock.unlockReadAndWrite();
          }
        } finally {
          blockLock.unlockReadAndWrite();
        }
      } finally {
        rightBlock.free();
      }
    }

    // XXX: error
View Full Code Here

    if (! _store.isIndexBlock(blockId)) {
      return null;
    }
   
    Block block = _store.readBlock(blockId);

    block.read();
    byte []buffer = block.getBuffer();
     
    int length = getInt(buffer, LENGTH_OFFSET);
    int offset = HEADER_SIZE;
    int tupleSize = _tupleSize;

    ArrayList<String> keys = new ArrayList<String>();
    for (int i = 0; i < length; i++) {
      keys.add(_keyCompare.toString(buffer,
            offset + i * tupleSize + PTR_SIZE,
            tupleSize - PTR_SIZE));
    }

    block.free();
   
    return keys;
  }
View Full Code Here

        writeMiniFragAddr(inode, inodeOffset,
                          store, xa,
                          currentLength, miniFragAddr);

        Block writeBlock = store.writeMiniFragment(miniFragAddr, 0,
                                                   buffer, offset, sublen);
        xa.addUpdateBlock(writeBlock);

        offset += sublen;
        length -= sublen;
View Full Code Here

        int sublen = length;

        if (BLOCK_SIZE - blockOffset < sublen)
          sublen = BLOCK_SIZE - blockOffset;

        Block block = store.writeBlock(addr, blockOffset,
                                       buffer, offset, sublen);
        xa.addUpdateBlock(block);

        offset += sublen;
        length -= sublen;

        currentLength += sublen;
      }
      else {
        int sublen = length;

        if (BLOCK_SIZE < sublen)
          sublen = BLOCK_SIZE;

        Block block = store.allocateBlock();

        long blockAddr = BlockStore.blockIdToAddress(block.getBlockId());

        block.free();

        if (blockAddr == 0) {
          store.setCorrupted(true);

          throw new IllegalStateException(L.l("{0}: illegal block",
                                              store));
        }

        writeBlockAddr(inode, inodeOffset,
                       store, xa,
                       currentLength, blockAddr);

        Block writeBlock = store.writeBlock(blockAddr, 0,
                                            buffer, offset, sublen);
        xa.addUpdateBlock(writeBlock);

        offset += sublen;
        length -= sublen;
View Full Code Here

    if (! _store.isIndexBlock(blockId)) {
      return -1;
    }
   
    Block block = _store.readBlock(blockId);

    block.read();
    byte []buffer = block.getBuffer();
     
    long next = getPointer(buffer, NEXT_OFFSET);

    block.free();
   
    return next / BlockStore.BLOCK_SIZE;
  }
View Full Code Here

TOP

Related Classes of com.caucho.db.block.Block

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.