Top Description Inners Fields Constructors Methods
org.h2.mvstore

public final Class StreamStore

extends Object
Class Inheritance
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .InputStream, java.nio.ByteBuffer, java.util.Arrays, .Map, java.util.concurrent.atomic.AtomicLong, .AtomicReference, java.util.function.IntConsumer

A facility to store streams in a map. Streams are split into blocks, which are stored in a map. Very small streams are inlined in the stream id.

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).

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
StreamStore.Stream

A stream backed by a map.

Field Summary

Modifier and TypeField and Description
private final Map<Long, byte[]>
private final int
private final int
private final AtomicReference<byte[]>
private final AtomicLong
private final IntConsumer

Constructor Summary

AccessConstructor and Description
public
StreamStore(Map<Long, byte[]>
the map to store blocks of data
map
)

Create a stream store instance.

public
StreamStore(Map<Long, byte[]> map, int minBlockSize, int maxBlockSize)

public
StreamStore(Map<Long, byte[]> map, IntConsumer onStoreCallback)

public
StreamStore(Map<Long, byte[]> map, int minBlockSize, int maxBlockSize, IntConsumer onStoreCallback)

Method Summary

Modifier and TypeMethod and Description
public InputStream

Returns:

the stream
get
(byte[]
the id
id
)

Open an input stream to read data.

private long

Returns:

the new key
getAndIncrementNextKey
()

Generate a new key.

pack-priv byte[]

Returns:

the block
getBlock
(long
the key
key
)

Get the block.

public Map<Long, byte[]>
public long

Returns:

the key, or -1
getMaxBlockKey
(byte[]
the id
id
)

Get the key of the biggest block, of -1 for inline data.

public long
public int
public long
public boolean

Returns:

if the id contains the data
isInPlace
(byte[]
the id
id
)

Check whether the id itself contains all the data.

public long

Returns:

the length
length
(byte[]
the id
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.

public byte[]

Returns:

the id (potentially an empty array)
put
(InputStream
the stream
in
)

Store the stream, and return the id. The stream is not closed.

private boolean
private ByteArrayOutputStream
private static byte[]
read(InputStream in, byte[] target)

public void
remove(byte[]
the id
id
)

Remove all stored blocks for the given id.

public void
setNextKey(long nextKey)

public static String

Returns:

the string
toString
(byte[]
the stream id
id
)

Convert the id to a human readable string.

private long
writeBlock(byte[] data)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

mapback to summary
private final Map<Long, byte[]> map
maxBlockSizeback to summary
private final int maxBlockSize
minBlockSizeback to summary
private final int minBlockSize
nextBufferback to summary
private final AtomicReference<byte[]> nextBuffer
nextKeyback to summary
private final AtomicLong nextKey
onStoreCallbackback to summary
private final IntConsumer onStoreCallback

Constructor Detail

StreamStoreback to summary
public StreamStore(Map<Long, byte[]> map)

Create a stream store instance.

Parameters
map:Map<Long, byte[]>

the map to store blocks of data

StreamStoreback to summary
public StreamStore(Map<Long, byte[]> map, int minBlockSize, int maxBlockSize)
StreamStoreback to summary
public StreamStore(Map<Long, byte[]> map, IntConsumer onStoreCallback)
StreamStoreback to summary
public StreamStore(Map<Long, byte[]> map, int minBlockSize, int maxBlockSize, IntConsumer onStoreCallback)

Method Detail

getback to summary
public InputStream get(byte[] id)

Open an input stream to read data.

Parameters
id:byte[]

the id

Returns:InputStream

the stream

getAndIncrementNextKeyback to summary
private long getAndIncrementNextKey()

Generate a new key.

Returns:long

the new key

getBlockback to summary
pack-priv byte[] getBlock(long key)

Get the block.

Parameters
key:long

the key

Returns:byte[]

the block

getMapback to summary
public Map<Long, byte[]> getMap()
getMaxBlockKeyback to summary
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:byte[]

the id

Returns:long

the key, or -1

getMaxBlockSizeback to summary
public long getMaxBlockSize()
getMinBlockSizeback to summary
public int getMinBlockSize()
getNextKeyback to summary
public long getNextKey()
isInPlaceback to summary
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:byte[]

the id

Returns:boolean

if the id contains the data

lengthback to summary
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:byte[]

the id

Returns:long

the length

putback to summary
public byte[] put(InputStream in) throws IOException

Store the stream, and return the id. The stream is not closed.

Parameters
in:InputStream

the stream

Returns:byte[]

the id (potentially an empty array)

Exceptions
IOException:
If an I/O error occurs
putback to summary
private boolean put(ByteArrayOutputStream id, InputStream in, int level) throws IOException
putIndirectIdback to summary
private ByteArrayOutputStream putIndirectId(ByteArrayOutputStream id) throws IOException
readback to summary
private static byte[] read(InputStream in, byte[] target) throws IOException
removeback to summary
public void remove(byte[] id)

Remove all stored blocks for the given id.

Parameters
id:byte[]

the id

setNextKeyback to summary
public void setNextKey(long nextKey)
toStringback to summary
public static String toString(byte[] id)

Convert the id to a human readable string.

Parameters
id:byte[]

the stream id

Returns:String

the string

writeBlockback to summary
private long writeBlock(byte[] data)
org.h2.mvstore back to summary

pack-priv Class StreamStore.Stream

extends InputStream
Class Inheritance

A stream backed by a map.

Field Summary

Modifier and TypeField and Description
private ByteArrayInputStream
private ByteBuffer
private final long
private byte[]
private long
private long
private final StreamStore

Constructor Summary

AccessConstructor and Description
pack-priv
Stream(StreamStore store, byte[] id)

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

private ByteArrayInputStream
public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.

public int
read(byte[]
the buffer into which the data is read.
b
,
int
the start offset in array b at which the data is written.
off
,
int
the maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.

public long
skip(long
the number of bytes to be skipped.
n
)

Overrides java.io.InputStream.skip.

Skips over and discards n bytes of data from this input stream.

Inherited from java.io.InputStream:
availablemarkmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipNBytestransferTo

Field Detail

bufferback to summary
private ByteArrayInputStream buffer
idBufferback to summary
private ByteBuffer idBuffer
lengthback to summary
private final long length
oneByteBufferback to summary
private byte[] oneByteBuffer
posback to summary
private long pos
skipback to summary
private long skip
storeback to summary
private final StreamStore store

Constructor Detail

Streamback to summary
pack-priv Stream(StreamStore store, byte[] id)

Method Detail

closeback to summary
public void close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.InputStream.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
nextBufferback to summary
private ByteArrayInputStream nextBuffer()
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Parameters
b:byte[]

the buffer into which the data is read.

off:int

the start offset in array b at which the data is written.

len:int

the maximum number of bytes to read.

Returns:int

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
skipback to summary
public long skip(long n)

Overrides java.io.InputStream.skip.

Doc from java.io.InputStream.skip.

Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, the skip method for class InputStream always returns 0, and no bytes are skipped. Subclasses may handle the negative value differently.

Parameters
n:long

the number of bytes to be skipped.

Returns:long

the actual number of bytes skipped which might be zero.

Annotations
@Override