Adds the metadata of a new block into this storage dir.

@param tempBlockMeta the metadata of a temp block to add
@throws BlockAlreadyExistsException if blockId already exists
@throws WorkerOutOfSpaceException when not enough space to hold block
973359
public void addTempBlockMeta(TempBlockMeta tempBlockMeta) throws WorkerOutOfSpaceException,
      BlockAlreadyExistsException {
    Preconditions.checkNotNull(tempBlockMeta, "tempBlockMeta");
    long sessionId = tempBlockMeta.getSessionId();
    long blockId = tempBlockMeta.getBlockId();
    long blockSize = tempBlockMeta.getBlockSize();

    if (getAvailableBytes() < blockSize) {
      throw new WorkerOutOfSpaceException(ExceptionMessage.NO_SPACE_FOR_BLOCK_META, blockId,
          blockSize, getAvailableBytes(), tempBlockMeta.getBlockLocation().tierAlias());
    }
    if (hasTempBlockMeta(blockId)) {
      throw new BlockAlreadyExistsException(ExceptionMessage.ADD_EXISTING_BLOCK, blockId,
          tempBlockMeta.getBlockLocation().tierAlias());
    }

    mBlockIdToTempBlockMap.put(blockId, tempBlockMeta);
    Set<Long> sessionTempBlocks = mSessionIdToTempBlockIdsMap.get(sessionId);
    if (sessionTempBlocks == null) {
      mSessionIdToTempBlockIdsMap.put(sessionId, Sets.newHashSet(blockId));
    } else {
      sessionTempBlocks.add(blockId);
    }
    reserveSpace(blockSize, false);
  }
1106135
@Override
	protected void submitRedoMeta(final int futureUse) {
		if (!useRedo) {
			return;
		}
		createRedoThread();
		final ByteBuffer buf = bufstack.pop();
		buf.putInt(0x0C0C0C0C); // META HEADER/FOOTER
		buf.flip();
		if (useRedoThread) {
			try {
				redoQueue.put(buf);
			} catch (InterruptedException e) {
				log.error("InterruptedException in submitRedoMeta(" + futureUse + ")", e);
			}
		} else {
			redoStore.write(buf);
			bufstack.push(buf);
		}
	}
925988
public static void addStmt(BlockStmt block, Statement stmt) {
        List<Statement> stmts = block.getStmts();
        if (stmts == null) {
            stmts = new ArrayList<Statement>();
            block.setStmts(stmts);
        }
        stmts.add(stmt);
    }
894990
private boolean readMetaData() throws InvalidDataException {
		final ByteBuffer buf = storage.get(0);
		int magic1, magic2, t_b_order_leaf, t_b_order_internal, t_blockSize; // sanity
		boolean isClean = false;
		magic1 = buf.getInt();
		if (magic1 != MAGIC_1) {
			throw new InvalidDataException("Invalid metadata (MAGIC1)");
		}
		t_blockSize = buf.getInt();
		if (t_blockSize != blockSize) {
			throw new InvalidDataException("Invalid metadata (blockSize) " + t_blockSize + " != " + blockSize);
		}
		t_b_order_leaf = buf.getInt();
		t_b_order_internal = buf.getInt();
		if (t_b_order_leaf != b_order_leaf) {
			throw new InvalidDataException("Invalid metadata (b-order leaf) " + t_b_order_leaf + " != "
					+ b_order_leaf);
		}
		if (t_b_order_internal != b_order_internal) {
			throw new InvalidDataException("Invalid metadata (b-order internal) " + t_b_order_internal
					+ " != " + b_order_internal);
		}
		storageBlock = buf.getInt();
		rootIdx = buf.getInt();
		lowIdx = buf.getInt();
		highIdx = buf.getInt();
		elements = buf.getInt();
		height = buf.getInt();
		maxInternalNodes = buf.getInt();
		maxLeafNodes = buf.getInt();
		isClean = ((buf.get() == ((byte) 0xEA)) ? true : false);
		magic2 = buf.getInt();
		if (magic2 != MAGIC_2) {
			throw new InvalidDataException("Invalid metadata (MAGIC2)");
		}
		if (log.isDebugEnabled()) {
			log.debug(this.getClass().getName() + "::readMetaData() elements=" + elements + " rootIdx="
					+ rootIdx);
		}
		storage.release(buf);
		// Clear Caches
		clearReadCaches();
		clearWriteCaches();
		if (isClean && fileFreeBlocks.exists()) {
			try {
				final SimpleBitSet newFreeBlocks = SimpleBitSet.deserializeFromFile(fileFreeBlocks);
				freeBlocks = newFreeBlocks;
			} catch (IOException e) {
				log.error("IOException in readMetaData()", e);
			}
		}
		return isClean;
	}
740925
public Metadata updateMetadata(Metadata metadata) {
        String scope;
        if (metadata.getScope().equals(Metadata.GLOBAL_METADATA_SCOPE)) {
            scope = Metadata.GLOBAL_METADATA_SCOPE;
        } else {
            scope = Metadata.ENTERPRISE_METADATA_SCOPE;
        }

        URL url = METADATA_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID(),
                scope, metadata.getTemplateName());
        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "PUT");
        request.addHeader("Content-Type", "application/json-patch+json");
        request.setBody(metadata.getPatch());
        BoxJSONResponse response = (BoxJSONResponse) request.send();
        return new Metadata(JsonObject.readFrom(response.getJSON()));
    }
234217
boolean addNode(Block b, DatanodeDescriptor node, int replication) {
    // insert into the map if not there yet
    BlockInfo info = checkBlockInfo(b, replication);
    // add block to the data-node list and the node to the block info
    return node.addBlock(info);
  }
680745
protected void readMetadata() throws IOException
	{
		int blockCount = super.read();
		// System.out.println ("blocks to read: " + blockCount);
		int byteCount = (blockCount * 16); // 16 bytes per block
		if (byteCount <= 0) return; // WTF?!
		byte[] metadataBlock = new byte[byteCount];
		int index = 0;
		// build an array of this metadata
		while (byteCount > 0)
		{
			int bytesRead = super.read(metadataBlock, index, byteCount);
			index += bytesRead;
			byteCount -= bytesRead;
		}
		// now parse it
		parseInlineIcyTags(metadataBlock);
	}
1045928
public TempBlockMeta getTempBlockMeta(long blockId) throws BlockDoesNotExistException {
    TempBlockMeta blockMeta = getTempBlockMetaOrNull(blockId);
    if (blockMeta == null) {
      throw new BlockDoesNotExistException(ExceptionMessage.TEMP_BLOCK_META_NOT_FOUND, blockId);
    }
    return blockMeta;
  }
975854
private List<Thread> tryUpdateBlock(int namespaceId, 
      Block oldblock, Block newblock) throws IOException {
    lock.writeLock().lock();
    try {
      //check ongoing create threads
      ArrayList<Thread> activeThreads = getActiveThreads(namespaceId, oldblock);
      if (activeThreads != null) {
        return activeThreads;
      }

      DatanodeBlockInfo binfo = volumeMap.get(namespaceId, oldblock);
      if (binfo == null) {
        throw new IOException("Block " + oldblock
            + " doesn't exist or has been recovered to a new generation ");
      }

      File blockFile = binfo.getBlockDataFile().getFile();
  
      long oldgs;
      File oldMetaFile = null;
      if (binfo.isInlineChecksum()) {
        oldgs = BlockInlineChecksumReader
            .getGenerationStampFromInlineChecksumFile(blockFile.getName());
      } else {
        oldMetaFile = BlockWithChecksumFileWriter.findMetaFile(blockFile);
        oldgs = BlockWithChecksumFileReader.parseGenerationStampInMetaFile(
            blockFile, oldMetaFile);
      }
      
    // First validate the update

      //update generation stamp
      if (oldgs > newblock.getGenerationStamp()) {
        throw new IOException("Cannot update block (id=" + newblock.getBlockId()
            + ") generation stamp from " + oldgs
            + " to " + newblock.getGenerationStamp());
      }
      
      //update length
      if (newblock.getNumBytes() > oldblock.getNumBytes()) {
        throw new IOException("Cannot update block file (=" + blockFile
            + ") length from " + oldblock.getNumBytes() + " to " + newblock.getNumBytes());
      }

      // Although we've waited for the active threads all dead before updating
      // the map so there should be no data race there, we still create new
      // ActiveFile object to make sure in case another thread holds it,
      // it won't cause any problem for us.
      //
      try {
        volumeMap.copyOngoingCreates(namespaceId, oldblock);
      } catch (CloneNotSupportedException e) {
        // It should never happen.
        throw new IOException("Cannot clone ActiveFile object", e);
      }

      // Now perform the update
      File tmpMetaFile = null;
      if (!binfo.isInlineChecksum()) {
        // rename meta file to a tmp file
        tmpMetaFile = new File(oldMetaFile.getParent(),
            oldMetaFile.getName() + "_tmp" + newblock.getGenerationStamp());
        if (!oldMetaFile.renameTo(tmpMetaFile)) {
          throw new IOException("Cannot rename block meta file to " + tmpMetaFile);
        }
      }

      long oldBlockLength;
      if (!binfo.isInlineChecksum()) {
        oldBlockLength = blockFile.length();
      } else {
        oldBlockLength = BlockInlineChecksumReader.getBlockSizeFromFileLength(
            blockFile.length(), binfo.getChecksumType(),
            binfo.getBytesPerChecksum());
      }
      ActiveFile file = null;
      if (newblock.getNumBytes() < oldBlockLength) {
        if (!binfo.isInlineChecksum()) {
          new BlockWithChecksumFileWriter(binfo.getBlockDataFile(), tmpMetaFile)
            .truncateBlock(oldBlockLength, newblock.getNumBytes());
        } else {
          new BlockInlineChecksumWriter(binfo.getBlockDataFile(), binfo.getChecksumType(),
              binfo.getBytesPerChecksum(), datanode.writePacketSize)
            .truncateBlock(newblock.getNumBytes());
        }
        file = volumeMap.getOngoingCreates(namespaceId, oldblock);
        if (file != null) {
          file.setBytesAcked(newblock.getNumBytes());
          file.setBytesOnDisk(newblock.getNumBytes());
          file.setBytesReceived(newblock.getNumBytes());
        } else {
          // This should never happen unless called from unit tests.
          binfo.syncInMemorySize();
        }
      }

      String newDataFileName;
      if (!binfo.isInlineChecksum()) {
        //rename the tmp file to the new meta file (with new generation stamp)
        File newMetaFile = BlockWithChecksumFileWriter.getMetaFile(blockFile, newblock);
        if (!tmpMetaFile.renameTo(newMetaFile)) {
          throw new IOException("Cannot rename tmp meta file to " + newMetaFile);
        }
      } else {
        newDataFileName = BlockInlineChecksumWriter.getInlineChecksumFileName(
            newblock, binfo.getChecksumType(), binfo.getBytesPerChecksum());
        File newDataFile = new File(blockFile.getParent(), newDataFileName);
        if (!blockFile.renameTo(newDataFile)) {
          throw new IOException("Cannot rename data file to " + newDataFileName);
        }
        // fsyncIfPossible parent directory to persist rename.
        if (datanode.syncOnClose) {
          NativeIO.fsyncIfPossible(newDataFile.getParent());
        }
        setDataFileForBlock(namespaceId, oldblock, newDataFile);
      }
  
      if(volumeMap.getOngoingCreates(namespaceId, oldblock) != null){
        ActiveFile af = volumeMap.removeOngoingCreates(namespaceId, oldblock);
        volumeMap.addOngoingCreates(namespaceId, newblock, af);
      }
      volumeMap.update(namespaceId, oldblock, newblock);
  
      // paranoia! verify that the contents of the stored block 
      // matches the block file on disk.
      validateBlockMetadata(namespaceId, newblock);
      return null;
    } finally {
      lock.writeLock().unlock();
    }
  }
917136
void addBlockToBeReplicated(Block block, DatanodeDescriptor[] targets) {
    assert(block != null && targets != null && targets.length > 0);
    replicateBlocks.offer(block, targets);
  }
779642
public void addMetadata(Metadata meta) {
    for (String k : meta.keySet()) {
      for (TiffObject to : meta.getList(k)) {
        add(k, to, meta.getMetadataObject(k).isDublinCore(), meta.getMetadataObject(k).getPath());
      }
    }
  }
1078300
public void removeTempBlockMeta(TempBlockMeta tempBlockMeta) throws BlockDoesNotExistException {
    Preconditions.checkNotNull(tempBlockMeta, "tempBlockMeta");
    final long blockId = tempBlockMeta.getBlockId();
    final long sessionId = tempBlockMeta.getSessionId();
    TempBlockMeta deletedTempBlockMeta = mBlockIdToTempBlockMap.remove(blockId);
    if (deletedTempBlockMeta == null) {
      throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_META_NOT_FOUND, blockId);
    }
    Set<Long> sessionBlocks = mSessionIdToTempBlockIdsMap.get(sessionId);
    if (sessionBlocks == null || !sessionBlocks.contains(blockId)) {
      throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_NOT_FOUND_FOR_SESSION, blockId,
          mTier.getTierAlias(), sessionId);
    }
    Preconditions.checkState(sessionBlocks.remove(blockId));
    if (sessionBlocks.isEmpty()) {
      mSessionIdToTempBlockIdsMap.remove(sessionId);
    }
    reclaimSpace(tempBlockMeta.getBlockSize(), false);
  }
232122
DatanodeBlockInfo addBlockInfo(Block block,
      DatanodeBlockInfo replicaInfo) {
    return getBlockBucket(block).addBlockInfo(block, replicaInfo);
  }
538813
@Override
    public void endDocument(MetaData metadata)
    {
        this.builder.addBlock(new XDOM(this.builder.endBlockList(), metadata));
    }
963933
private static BlockMetadataHeader readHeader(short version, DataInputStream in) 
                                   throws IOException {
    DataChecksum checksum = DataChecksum.newDataChecksum(in);
    return new BlockMetadataHeader(version, checksum);
  }
247852
public void add(StorageDirView dir, long blockId, long blockSizeBytes) {
    Pair<List<Long>, Long> candidate;
    if (mDirCandidates.containsKey(dir)) {
      candidate = mDirCandidates.get(dir);
    } else {
      candidate = new Pair<List<Long>, Long>(new ArrayList<Long>(), 0L);
      mDirCandidates.put(dir, candidate);
    }

    candidate.getFirst().add(blockId);
    long blockBytes = candidate.getSecond() + blockSizeBytes;
    candidate.setSecond(blockBytes);

    long sum = blockBytes + dir.getAvailableBytes();
    if (mMaxBytes < sum) {
      mMaxBytes = sum;
      mDirWithMaxBytes = dir;
    }
  }
1156567
public void removeBlockMeta(BlockMeta blockMeta) throws BlockDoesNotExistException {
    Preconditions.checkNotNull(blockMeta, "blockMeta");
    long blockId = blockMeta.getBlockId();
    BlockMeta deletedBlockMeta = mBlockIdToBlockMap.remove(blockId);
    if (deletedBlockMeta == null) {
      throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_META_NOT_FOUND, blockId);
    }
    reclaimSpace(blockMeta.getBlockSize(), true);
  }