/** * Removes a temp block from this storage dir or throws IOException. * * @param tempBlockMeta the meta data of the temp block to remove * @throws IOException if no temp block is found */ public void removeTempBlockMeta(TempBlockMeta tempBlockMeta) throws IOException { Preconditions.checkNotNull(tempBlockMeta); final long blockId = tempBlockMeta.getBlockId(); final long userId = tempBlockMeta.getUserId(); TempBlockMeta deletedTempBlockMeta = mBlockIdToTempBlockMap.remove(blockId); if (deletedTempBlockMeta == null) { throw new IOException("Failed to remove TempBlockMeta: blockId " + blockId + " not found"); } Set<Long> userBlocks = mUserIdToTempBlockIdsMap.get(userId); if (userBlocks == null) { throw new IOException( "Failed to remove TempBlockMeta: blockId " + blockId + " has userId " + userId + " not found"); } if (!userBlocks.contains(blockId)) { throw new IOException( "Failed to remove TempBlockMeta: blockId " + blockId + " not " + "associated with userId " + userId); } Preconditions.checkState(userBlocks.remove(blockId)); if (userBlocks.isEmpty()) { mUserIdToTempBlockIdsMap.remove(userId); } reclaimSpace(tempBlockMeta.getBlockSize(), false); }
/** * Adds the metadata of a new block into this storage dir or throws IOException. * * @param tempBlockMeta the meta data of a temp block to add * @throws IOException if blockId already exists or not enough space */ public void addTempBlockMeta(TempBlockMeta tempBlockMeta) throws IOException { Preconditions.checkNotNull(tempBlockMeta); long userId = tempBlockMeta.getUserId(); long blockId = tempBlockMeta.getBlockId(); long blockSize = tempBlockMeta.getBlockSize(); if (getAvailableBytes() < blockSize) { throw new IOException( "Failed to add TempBlockMeta: blockId " + blockId + " is " + blockSize + " bytes, but only " + getAvailableBytes() + " bytes available"); } if (hasTempBlockMeta(blockId)) { throw new IOException("Failed to add TempBlockMeta: blockId " + blockId + " exists"); } mBlockIdToTempBlockMap.put(blockId, tempBlockMeta); Set<Long> userTempBlocks = mUserIdToTempBlockIdsMap.get(userId); if (userTempBlocks == null) { mUserIdToTempBlockIdsMap.put(userId, Sets.newHashSet(blockId)); } else { userTempBlocks.add(blockId); } reserveSpace(blockSize, false); }