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 TypeMethodDescriptionvoidclose()Closes this entity and all related blocksvoidcommit()Operation commits all the active blocks.voidfree()Operation is to free the blocks that has previously been allocated by theDataWriter.Provides a status of this entitybooleanhasMore()voiditerateIndices(Consumer<CatalogBlockIndices> consumer) Executed block by block with its content.longlength()static DataReaderopen(BlockAllocator allocator, long startIndex) Opens the data reader to start reading from the given block.static DataReaderOpens the data reader to start reading from the given block.longposition()intread()<T> TreadObject(Class<T> clazz) Reads the java object from the data reader.static <T> TreadObject(Media media, long startIndex, Class<T> clazz) static voidsafeReader(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, transferToMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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
BinaryMapperto 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:
readin classInputStream
-
free
public void free()Operation is to free the blocks that has previously been allocated by theDataWriter.Note, that the
BlockAllocatorshould 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:CloseableCloses this entity and all related blocks- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream
-
getStatus
Description copied from interface:CloseableProvides a status of this entity -
commit
public void commit()Description copied from interface:CommittableOperation 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:
commitin interfaceCommittable
-