Package com.caucho.db.block

Examples of com.caucho.db.block.Block


   * Test if any row in the block is free
   */
  private boolean isRowBlockFree(long blockId)
    throws IOException
  {
    Block block = readBlock(blockId);

    try {
      int rowOffset = 0;

      byte []buffer = block.getBuffer();
      boolean isFree = false;

      for (; rowOffset < _rowEnd; rowOffset += _rowLength) {
        if (buffer[rowOffset] == 0) {
          isFree = true;
          _clockRowFree++;
        }
        else
          _clockRowUsed++;
      }

      return isFree;
    } finally {
      block.free();
    }
  }
View Full Code Here


  public Block readBlock(BlockStore store, long blockAddress)
    throws IOException
  {
    long blockId = store.addressToBlockId(blockAddress);
     
    Block block = null;

    if (block != null)
      block.allocate();
    else
      block = store.readBlock(blockId);

    return block;
  }
View Full Code Here

  public Block loadBlock(BlockStore store, long blockAddress)
    throws IOException
  {
    long blockId = store.addressToBlockId(blockAddress);
     
    Block block = store.loadBlock(blockId);

    return block;
  }
View Full Code Here

    ArrayList<Block> updateBlocks = _updateBlocks;
    _updateBlocks = null;
   
    if (updateBlocks != null) {
      while (updateBlocks.size() > 0) {
  Block block = updateBlocks.remove(updateBlocks.size() - 1);

        try {
          block.getStore().saveAllocation();
  } catch (IOException e) {
    log.log(Level.WARNING, e.toString(), e);
        }
       
  try {
    block.commit();
  } catch (IOException e) {
    log.log(Level.WARNING, e.toString(), e);
  }
      }
    }

    if (_deallocateBlocks != null) {
      while (_deallocateBlocks.size() > 0) {
  Block block = _deallocateBlocks.remove(0);

  try {
    block.getStore().freeBlock(block.getBlockId());
  } catch (IOException e) {
    throw new SQLExceptionWrapper(e);
  }
      }
    }
View Full Code Here

         int keyOffset,
         int keyLength,
         long blockId)
    throws IOException, SQLException
  {
    Block block;

    if (blockId == _rootBlockId) {
      block = _rootBlock;
      block.allocate();
    }
    else
      block = _store.loadBlock(blockId);

    try {
      Lock blockLock = block.getLock();
     
      blockLock.lockRead(_timeout);

      try {
        validateIndex(block);
       
        block.read();
       
  byte []buffer = block.getBuffer();

  boolean isLeaf = isLeaf(buffer, block);
     
  long value = lookupTuple(blockId, buffer,
         keyBuffer, keyOffset, keyLength,
         isLeaf);

  if (isLeaf || value == FAIL)
    return value;
  else
    return lookup(keyBuffer, keyOffset, keyLength, value);
      } finally {
        blockLock.unlockRead();
      }
    } finally {
      block.free();
    }
  }
View Full Code Here

       boolean isOverride,
                         boolean isRead,
       long blockId)
    throws IOException, SQLException
  {
    Block block;

    if (blockId == _rootBlockId) {
      block = _rootBlock;
      block.allocate();
    }
    else
      block = _store.loadBlock(blockId);
   
    try {
      validateIndex(block);
       
      if (isRead && insertReadChild(keyBuffer, keyOffset, keyLength,
                                    value, isOverride, block))
        return true;
      else
        return insertWriteChild(keyBuffer, keyOffset, keyLength,
                                value, isOverride, block);
    } finally {
      block.free();
    }
  }
View Full Code Here

   */
  private void split(Block parent,
         long blockId)
    throws IOException, SQLException
  {
    Block block = _store.readBlock(blockId);

    try {
      validate(block);
       
      Lock blockLock = block.getLock();
      blockLock.lockReadAndWrite(_timeout);
 
      try {
        split(parent, block);

        validate(block);
      } finally {
        blockLock.unlockReadAndWrite();
      }
    } finally {
      block.free();
    }
  }
View Full Code Here

    if (length < 2)
      throw new IllegalStateException(L.l("illegal length '{0}' for block {1}",
            length, debugId(blockId)));
     
    Block leftBlock = null;

    try {
      parentBlock.setFlushDirtyOnCommit(false);
   
      byte []parentBuffer = parentBlock.getBuffer();
      int parentLength = getLength(parentBuffer);
   
      validate(parentId, parentBuffer);
      validate(blockId, buffer);
     
      leftBlock = _store.allocateIndexBlock();
      // System.out.println("TREE-alloc1:" + Long.toHexString(leftBlock.getBlockId()));
      leftBlock.setFlushDirtyOnCommit(false);
      // System.out.println("ALLOC: " + leftBlock);
     
      byte []leftBuffer = leftBlock.getBuffer();
      long leftBlockId = leftBlock.getBlockId();

      int pivot = length / 2;

      int pivotSize = pivot * _tupleSize;
      int pivotEnd = HEADER_SIZE + pivotSize;
      int blockEnd = HEADER_SIZE + length * _tupleSize;
     
      System.arraycopy(buffer, HEADER_SIZE,
           leftBuffer, HEADER_SIZE,
           pivotSize);

      setInt(leftBuffer, FLAGS_OFFSET, getInt(buffer, FLAGS_OFFSET));
      setLength(leftBuffer, pivot);
      // XXX: NEXT_OFFSET needs to work with getRightIndex
      setPointer(leftBuffer, NEXT_OFFSET, 0);
     
      setPointer(leftBuffer, PARENT_OFFSET, parentId);

      System.arraycopy(buffer, pivotEnd,
           buffer, HEADER_SIZE,
           blockEnd - pivotEnd);

      setLength(buffer, length - pivot);

      insertLeafBlock(parentId, parentBuffer,
          leftBuffer, pivotEnd - _tupleSize + PTR_SIZE, _keySize,
          leftBlockId,
          true);
     
      validate(parentId, parentBuffer);
      validate(leftBlockId, leftBuffer);
      validate(blockId, buffer);
     
      validate(block);
      validate(parentBlock);
      validate(leftBlock);
     
      leftBlock.setDirty(0, BlockStore.BLOCK_SIZE);
      parentBlock.setDirty(0, BlockStore.BLOCK_SIZE);
    } finally {
      if (leftBlock != null)
  leftBlock.free();
     
      block.setDirty(0, BlockStore.BLOCK_SIZE);
    }
  }
View Full Code Here

   * The length in lBuf is assumed to be the length of the buffer.
   */
  private void splitRoot(long rootBlockId)
    throws IOException, SQLException
  {
    Block rootBlock = _rootBlock; // store.readBlock(rootBlockId);
    rootBlock.allocate();

    try {
      Lock rootLock = rootBlock.getLock();
      rootLock.lockReadAndWrite(_timeout);
     
      try {
  splitRoot(rootBlock);

        validate(rootBlock);
      } finally {
  rootLock.unlockReadAndWrite();
      }
    } finally {
      rootBlock.free();
    }
  }
View Full Code Here

  {
    long parentId = parentBlock.getBlockId();
   
    log.finest("btree splitting root " + (parentId / BLOCK_SIZE));

    Block leftBlock = null;
    Block rightBlock = null;

    try {
      byte []parentBuffer = parentBlock.getBuffer();
      int length = getLength(parentBuffer);

      if (length == 1)
        return;
     
      parentBlock.setFlushDirtyOnCommit(false);

      int parentFlags = getInt(parentBuffer, FLAGS_OFFSET);

      leftBlock = _store.allocateIndexBlock();
      // System.out.println("TREE-alloc2:" + Long.toHexString(leftBlock.getBlockId()));
      leftBlock.setFlushDirtyOnCommit(false);
     
      long leftBlockId = leftBlock.getBlockId();
   
      rightBlock = _store.allocateIndexBlock();
      // System.out.println("TREE-alloc3:" + Long.toHexString(rightBlock.getBlockId()));
      rightBlock.setFlushDirtyOnCommit(false);
     
      long rightBlockId = rightBlock.getBlockId();

      int pivot = (length - 1) / 2;
     
      //System.out.println("INDEX SPLIT ROOT: " + (parentId / BLOCK_SIZE)
      //                    + " PIVOT=" + pivot);

      if (length <= 2 || _n < length || pivot < 1 || length <= pivot)
  throw new IllegalStateException(Long.toHexString(parentBlock.getBlockId()) + ": " + length + " is an illegal length, or pivot " + pivot + " is bad, with n=" + _n);

      int pivotOffset = HEADER_SIZE + pivot * _tupleSize;
      long pivotValue = getPointer(parentBuffer, pivotOffset);

      byte []leftBuffer = leftBlock.getBuffer();

      System.arraycopy(parentBuffer, HEADER_SIZE,
           leftBuffer, HEADER_SIZE,
           pivotOffset + _tupleSize - HEADER_SIZE);
      setInt(leftBuffer, FLAGS_OFFSET, parentFlags);
      setLength(leftBuffer, pivot + 1);
      setPointer(leftBuffer, PARENT_OFFSET, parentId);
      setPointer(leftBuffer, NEXT_OFFSET, 0); // rightBlockId);

      byte []rightBuffer = rightBlock.getBuffer();

      if (length - pivot - 1 < 0)
  throw new IllegalStateException("illegal length " + pivot + " " + length);

      System.arraycopy(parentBuffer, pivotOffset + _tupleSize,
           rightBuffer, HEADER_SIZE,
           (length - pivot - 1) * _tupleSize);

      setInt(rightBuffer, FLAGS_OFFSET, parentFlags);
      setLength(rightBuffer, length - pivot - 1);
      setPointer(rightBuffer, PARENT_OFFSET, parentId);
      setPointer(rightBuffer, NEXT_OFFSET,
     getPointer(parentBuffer, NEXT_OFFSET));

      System.arraycopy(parentBuffer, pivotOffset,
           parentBuffer, HEADER_SIZE,
           _tupleSize);
      setPointer(parentBuffer, HEADER_SIZE, leftBlockId);

      setLeaf(parentBuffer, false);
      setLength(parentBuffer, 1);
      setPointer(parentBuffer, NEXT_OFFSET, rightBlockId);
     
      parentBlock.setDirty(0, BlockStore.BLOCK_SIZE);
      leftBlock.setDirty(0, BlockStore.BLOCK_SIZE);
      rightBlock.setDirty(0, BlockStore.BLOCK_SIZE);
     
      validate(parentBlock);
      validate(leftBlock);
      validate(rightBlock);
    } finally {
      if (leftBlock != null)
  leftBlock.free();
     
      if (rightBlock != null)
  rightBlock.free();
    }
  }
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.