Package org.rostore.v2.data
Class DataReader
java.lang.Object
java.io.InputStream
org.rostore.v2.data.DataReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Closeable
,Committable
This is a counterpart of
DataWriter
, that allows to read the data from the storage that
has been previously written by the writer.
Is not thread-safe-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this entity and all related blocksvoid
commit()
Operation commits all the active blocks.void
free()
Operation is to free the blocks that has previously been allocated by theDataWriter
.Provides a status of this entityboolean
hasMore()
void
iterateIndices
(Consumer<CatalogBlockIndices> consumer) Executed block by block with its content.long
length()
static DataReader
open
(BlockAllocator allocator, long startIndex) Opens the data reader to start reading from the given block.static DataReader
Opens the data reader to start reading from the given block.long
position()
int
read()
<T> T
readObject
(Class<T> clazz) Reads the java object from the data reader.static <T> T
readObject
(Media media, long startIndex, Class<T> clazz) static void
safeReader
(Media media, long startIndex, Consumer<DataReader> dataReaderConsumer) static <T extends OutputStream>
voidtoOutputStream
(Media media, long startIndex, T outputStream) Reads the data from the sequence starting at start index and writes it to the output stream.Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.rostore.v2.media.Closeable
checkOpened
-
Method Details
-
open
Opens the data reader to start reading from the given block.The data reader should not be used to free the blocks
free()
, as it will use the root allocator for it.- Parameters:
media
- the media to be usedstartIndex
- the first block to start reading from- Returns:
- the data reader object
-
open
Opens the data reader to start reading from the given block.This is a more specific operation comparing to
open(Media, long)
, as it specifies a specific allocator, and not the root's one.The
free()
can be executed.- Parameters:
allocator
- allocator to be usedstartIndex
- the first block to start reading from- Returns:
- the data reader object
-
safeReader
public static void safeReader(Media media, long startIndex, Consumer<DataReader> dataReaderConsumer) -
readObject
-
toOutputStream
public static <T extends OutputStream> void toOutputStream(Media media, long startIndex, T outputStream) Reads the data from the sequence starting at start index and writes it to the output stream.- Type Parameters:
T
- the subtype of the output stream- Parameters:
media
- the media to read fromstartIndex
- the first block indexoutputStream
- the output stream to write to
-
readObject
Reads the java object from the data reader.The function uses
BinaryMapper
to deserialize the object.- Type Parameters:
T
- the type of the object- Parameters:
clazz
- the class of the java object- Returns:
- the java object
-
length
public long length() -
position
public long position() -
read
public int read()- Specified by:
read
in classInputStream
-
free
public void free()Operation is to free the blocks that has previously been allocated by theDataWriter
.Note, that the
BlockAllocator
should be correctly providedopen(BlockAllocator, long)
.If the
open(Media, long)
is used, the root allocator will be used. -
iterateIndices
Executed block by block with its content.For the very long data sequences, it might be called several times.
-
hasMore
public boolean hasMore() -
close
public void close()Description copied from interface:Closeable
Closes this entity and all related blocks- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
-
getStatus
Description copied from interface:Closeable
Provides a status of this entity -
commit
public void commit()Description copied from interface:Committable
Operation commits all the active blocks. It will mark the block to be garbage-collected, the data from the associated physical block will be flushed to the persistent storage.- Specified by:
commit
in interfaceCommittable
-