Class StreamStore
The key of the map is a long (incremented for each stored block). The default initial value is 0. Before storing blocks into the map, the stream store checks if there is already a block with the next key, and if necessary searches the next free entry using a binary search (0 to Long.MAX_VALUE).
Before storing
The format of the binary id is: An empty id represents 0 bytes of data. In-place data is encoded as 0, the size (a variable size int), then the data. A stored block is encoded as 1, the length of the block (a variable size int), then the key (a variable size long). Multiple ids can be concatenated to concatenate the data. If the id is large, it is stored itself, which is encoded as 2, the total length (a variable size long), and the key of the block that contains the id (a variable size long).
-
Constructor Summary
ConstructorsConstructorDescriptionStreamStore(Map<Long, byte[]> map) Create a stream store instance.StreamStore(Map<Long, byte[]> map, int minBlockSize, int maxBlockSize) StreamStore(Map<Long, byte[]> map, int minBlockSize, int maxBlockSize, IntConsumer onStoreCallback) StreamStore(Map<Long, byte[]> map, IntConsumer onStoreCallback) -
Method Summary
Modifier and TypeMethodDescriptionget(byte[] id) Open an input stream to read data.getMap()longgetMaxBlockKey(byte[] id) Get the key of the biggest block, of -1 for inline data.longintlongbooleanisInPlace(byte[] id) Check whether the id itself contains all the data.longlength(byte[] id) Calculate the number of data bytes for the given id.byte[]put(InputStream in) Store the stream, and return the id.voidremove(byte[] id) Remove all stored blocks for the given id.voidsetNextKey(long nextKey) static StringtoString(byte[] id) Convert the id to a human-readable string.
-
Constructor Details
-
StreamStore
Create a stream store instance.- Parameters:
map- the map to store blocks of data
-
StreamStore
-
StreamStore
-
StreamStore
public StreamStore(Map<Long, byte[]> map, int minBlockSize, int maxBlockSize, IntConsumer onStoreCallback)
-
-
Method Details
-
getMap
-
setNextKey
public void setNextKey(long nextKey) -
getNextKey
public long getNextKey() -
getMinBlockSize
public int getMinBlockSize() -
getMaxBlockSize
public long getMaxBlockSize() -
put
Store the stream, and return the id. The stream is not closed.- Parameters:
in- the stream- Returns:
- the id (potentially an empty array)
- Throws:
IOException- If an I/O error occurs
-
getMaxBlockKey
public long getMaxBlockKey(byte[] id) Get the key of the biggest block, of -1 for inline data. This method is used to garbage collect orphaned blocks.- Parameters:
id- the id- Returns:
- the key, or -1
-
remove
public void remove(byte[] id) Remove all stored blocks for the given id.- Parameters:
id- the id
-
toString
Convert the id to a human-readable string.- Parameters:
id- the stream id- Returns:
- the string
-
length
public long length(byte[] id) Calculate the number of data bytes for the given id. As the length is encoded in the id, this operation does not cause any reads in the map.- Parameters:
id- the id- Returns:
- the length
-
isInPlace
public boolean isInPlace(byte[] id) Check whether the id itself contains all the data. This operation does not cause any reads in the map.- Parameters:
id- the id- Returns:
- if the id contains the data
-
get
Open an input stream to read data.- Parameters:
id- the id- Returns:
- the stream
-