Class SingleFilePerBlockCache
java.lang.Object
org.apache.hadoop.fs.impl.prefetch.SingleFilePerBlockCache
- All Implemented Interfaces:
Closeable,AutoCloseable,BlockCache
Provides functionality necessary for caching blocks of data read from FileSystem.
Each cache block is stored on the local disk as a separate file.
-
Constructor Summary
ConstructorsConstructorDescriptionSingleFilePerBlockCache(PrefetchingStatistics prefetchingStatistics, int maxBlocksCount, DurationTrackerFactory trackerFactory) Constructs an instance of aSingleFilePerBlockCache. -
Method Summary
Modifier and TypeMethodDescriptionblocks()Gets the blocks in this cache.voidclose()booleancontainsBlock(int blockNumber) Indicates whether the given block is in this cache.voidget(int blockNumber, ByteBuffer buffer) Gets the block having the givenblockNumber.protected PathgetCacheFilePath(Configuration conf, LocalDirAllocator localDirAllocator) Return temporary file created based on the file path retrieved from local dir allocator.static booleanisCacheSpaceAvailable(long fileSize, Configuration conf, LocalDirAllocator localDirAllocator) Determine if the cache space is available on the local FS.voidput(int blockNumber, ByteBuffer buffer, Configuration conf, LocalDirAllocator localDirAllocator) Puts the given block in this cache.protected intreadFile(Path path, ByteBuffer buffer) intsize()Gets the number of blocks in this cache.toString()protected voidwriteFile(Path path, ByteBuffer buffer)
-
Constructor Details
-
SingleFilePerBlockCache
public SingleFilePerBlockCache(PrefetchingStatistics prefetchingStatistics, int maxBlocksCount, DurationTrackerFactory trackerFactory) Constructs an instance of aSingleFilePerBlockCache.- Parameters:
prefetchingStatistics- statistics for this stream.maxBlocksCount- max blocks count to be kept in cache at any time.trackerFactory- tracker with statistics to update
-
-
Method Details
-
containsBlock
public boolean containsBlock(int blockNumber) Indicates whether the given block is in this cache.- Specified by:
containsBlockin interfaceBlockCache- Parameters:
blockNumber- the id of the given block.- Returns:
- true if the given block is in this cache, false otherwise.
-
blocks
Gets the blocks in this cache.- Specified by:
blocksin interfaceBlockCache- Returns:
- the blocks in this cache.
-
size
public int size()Gets the number of blocks in this cache.- Specified by:
sizein interfaceBlockCache- Returns:
- the number of blocks in this cache.
-
get
Gets the block having the givenblockNumber.- Specified by:
getin interfaceBlockCache- Parameters:
blockNumber- the id of the desired block.buffer- contents of the desired block are copied to this buffer.- Throws:
IllegalArgumentException- if buffer is null.IOException- if there is an error reading the given block.
-
readFile
- Throws:
IOException
-
put
public void put(int blockNumber, ByteBuffer buffer, Configuration conf, LocalDirAllocator localDirAllocator) throws IOException Puts the given block in this cache.- Specified by:
putin interfaceBlockCache- Parameters:
blockNumber- the block number, used as a key for blocks map.buffer- buffer contents of the given block to be added to this cache.conf- the configuration.localDirAllocator- the local dir allocator instance.- Throws:
IOException- if either local dir allocator fails to allocate file or if IO error occurs while writing the buffer content to the file.IllegalArgumentException- if buffer is null, or if buffer.limit() is zero or negative.
-
writeFile
- Throws:
IOException
-
getCacheFilePath
protected Path getCacheFilePath(Configuration conf, LocalDirAllocator localDirAllocator) throws IOException Return temporary file created based on the file path retrieved from local dir allocator.- Parameters:
conf- The configuration object.localDirAllocator- Local dir allocator instance.- Returns:
- Path of the temporary file created.
- Throws:
IOException- if IO error occurs while local dir allocator tries to retrieve path from local FS or file creation fails or permission set fails.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
toString
-
isCacheSpaceAvailable
public static boolean isCacheSpaceAvailable(long fileSize, Configuration conf, LocalDirAllocator localDirAllocator) Determine if the cache space is available on the local FS.- Parameters:
fileSize- The size of the file.conf- The configuration.localDirAllocator- Local dir allocator instance.- Returns:
- True if the given file size is less than the available free space on local FS, False otherwise.
-