public class WriterMT
extends java.lang.Object
implements java.lang.AutoCloseable
At the center of how this class works is an ultra-fast ring buffer containing
a store of empty records. As the user calls one of the addEvent(byte[], int, int)
methods,
it gradually fills one of those empty records with data. When the record is full,
it's put back into the ring to wait for one of the compression thread to grab it and compress it.
After compression, it's again placed back into the ring and waits for a final thread to
write it to file. After being written, the record is freed up for reuse.
This entire ring functionality is encapsulated in 2 classes,
RecordSupply
and RecordRingItem
.
Modifier and Type | Class and Description |
---|---|
(package private) class |
WriterMT.RecordCompressor
Class used to take data-filled record from supply, compress it,
and release it back to the supply.
|
(package private) class |
WriterMT.RecordWriter
Class used to take data-filled record from supply, write it,
and release it back to the supply.
|
Modifier and Type | Field and Description |
---|---|
(package private) java.lang.String |
dictionary
String containing evio-format XML dictionary to store in file header's user header.
|
(package private) java.nio.ByteBuffer |
dictionaryFirstEventBuffer
If dictionary and or firstEvent exist, this buffer contains them both as a record.
|
(package private) int |
userHeaderLength
Size in bytes of userHeader array.
|
Constructor and Description |
---|
WriterMT()
Default constructor is single-threaded, no compression.
|
WriterMT(java.nio.ByteOrder order,
int maxEventCount,
int maxBufferSize,
CompressionType compressionType,
int compressionThreads,
int ringSize)
Constructor with byte order.
|
WriterMT(HeaderType hType,
java.nio.ByteOrder order,
int maxEventCount,
int maxBufferSize,
java.lang.String dictionary,
byte[] firstEvent,
int firstEventLen,
CompressionType compType,
int compressionThreads,
boolean addTrailerIndex,
int ringSize)
Constructor with byte order.
|
WriterMT(java.lang.String filename)
Constructor with filename.
|
WriterMT(java.lang.String filename,
java.nio.ByteOrder order,
int maxEventCount,
int maxBufferSize,
CompressionType compressionType,
int compressionThreads,
int ringSize)
Constructor with filename and byte order.
|
Modifier and Type | Method and Description |
---|---|
void |
addEvent(byte[] buffer)
Add a byte array to the current internal record.
|
void |
addEvent(byte[] buffer,
int offset,
int length)
Add a byte array to the current internal record.
|
void |
addEvent(java.nio.ByteBuffer buffer)
Add a ByteBuffer to the internal record.
|
void |
addEvent(EvioBank bank)
Add an EvioBank to the internal record.
|
void |
addEvent(EvioNode node)
Add an EvioNode to the internal record.
|
boolean |
addTrailer()
Does this writer add a trailer to the end of the file?
|
void |
addTrailer(boolean addingTrailer)
Set whether this writer adds a trailer to the end of the file.
|
boolean |
addTrailerWithIndex()
Does this writer add a trailer with a record index to the end of the file?
|
void |
addTrailerWithIndex(boolean addTrailingIndex)
Set whether this writer adds a trailer with a record index to the end of the file.
|
void |
close()
Close opened file.
|
(package private) java.nio.ByteBuffer |
createDictionaryRecord()
Create a buffer representation of a record
containing the dictionary and/or the first event.
|
java.nio.ByteBuffer |
createHeader(byte[] userHdr)
Create and return a buffer containing a general file header
followed by the user header given in the argument.
|
(package private) java.nio.ByteBuffer |
createHeader(java.nio.ByteBuffer userHdr)
Return a buffer with a general file header followed by the given user header (userHdr).
|
java.nio.ByteOrder |
getByteOrder()
Get the file's byte order.
|
(package private) CompressionType |
getCompressionType()
Get the compression type for the file being written.
|
FileHeader |
getFileHeader()
Get the file header.
|
void |
open(java.lang.String filename)
Open a new file and write file header with no user header.
|
void |
open(java.lang.String filename,
byte[] userHdr)
Open a file and write file header with given user's header.
|
void |
reset()
Get this object ready for re-use.
|
WriterMT |
setCompressionType(CompressionType compression)
Convenience method that sets compression type for the file.
|
void |
writeRecord(RecordOutputStream rec)
Appends the record to the file.
|
int userHeaderLength
java.lang.String dictionary
java.nio.ByteBuffer dictionaryFirstEventBuffer
public WriterMT()
public WriterMT(java.nio.ByteOrder order, int maxEventCount, int maxBufferSize, CompressionType compressionType, int compressionThreads, int ringSize) throws java.lang.IllegalArgumentException
open(String, byte[])
with another user header array.order
- byte order of written filemaxEventCount
- max number of events a record can hold.
Value <= O means use default (1M).maxBufferSize
- max number of uncompressed data bytes a record can hold.
Value of < 8MB results in default of 8MB.compressionType
- type of data compression to do (0=none, 1=lz4 fast, 2=lz4 best, 3=gzip)compressionThreads
- number of threads doing compression simultaneouslyringSize
- number of records in supply ring, must be multiple of 2
and >= compressionThreads.java.lang.IllegalArgumentException
- if invalid compression type.public WriterMT(HeaderType hType, java.nio.ByteOrder order, int maxEventCount, int maxBufferSize, java.lang.String dictionary, byte[] firstEvent, int firstEventLen, CompressionType compType, int compressionThreads, boolean addTrailerIndex, int ringSize) throws java.lang.IllegalArgumentException
open(String, byte[])
with another user header array.hType
- the type of the file. If set to HeaderType.HIPO_FILE
,
the header will be written with the first 4 bytes set to HIPO.order
- byte order of written filemaxEventCount
- max number of events a record can hold.
Value <= O means use default (1M).maxBufferSize
- max number of uncompressed data bytes a record can hold.
Value of < 8MB results in default of 8MB.dictionary
- string holding an evio format dictionary to be placed in userHeader.firstEvent
- byte array containing an evio event to be included in userHeader.
It must be in the same byte order as the order argument.firstEventLen
- number of valid bytes in firstEvent.compType
- type of data compression to do (one, lz4 fast, lz4 best, gzip)compressionThreads
- number of threads doing compression simultaneouslyaddTrailerIndex
- if true, we add a record index to the trailer.ringSize
- number of records in supply ring, must be multiple of 2
and >= compressionThreads.java.lang.IllegalArgumentException
- if invalid compression type.public WriterMT(java.lang.String filename) throws HipoException
filename
- output file nameHipoException
- if filename arg is null or bad.public WriterMT(java.lang.String filename, java.nio.ByteOrder order, int maxEventCount, int maxBufferSize, CompressionType compressionType, int compressionThreads, int ringSize) throws HipoException
filename
- output file nameorder
- byte order of written file or null for default (little endian)maxEventCount
- max number of events a record can hold.
Value of O means use default (1M).maxBufferSize
- max number of uncompressed data bytes a record can hold.
Value of < 8MB results in default of 8MB.compressionType
- type of data compression to do (0=none, 1=lz4 fast, 2=lz4 best, 3=gzip)compressionThreads
- number of threads doing compression simultaneouslyringSize
- number of records in supply ring, must be multiple of 2
and >= compressionThreads.HipoException
- if filename arg is null or bad.java.nio.ByteBuffer createDictionaryRecord()
public java.nio.ByteOrder getByteOrder()
public FileHeader getFileHeader()
CompressionType getCompressionType()
public boolean addTrailer()
public void addTrailer(boolean addingTrailer)
addingTrailer
- if true, at the end of file, add an ending header (trailer)
with no index of records and no following data.
Update the file header to contain a file offset to the trailer.public boolean addTrailerWithIndex()
public void addTrailerWithIndex(boolean addTrailingIndex)
addTrailingIndex
- if true, at the end of file, add an ending header (trailer)
with an index of all records but with no following data.
Update the file header to contain a file offset to the trailer.public final void open(java.lang.String filename) throws HipoException
filename
- output file nameHipoException
- if filename arg is null or bad,
if this method already called without being
followed by reset.public final void open(java.lang.String filename, byte[] userHdr) throws HipoException
filename
- disk file name.userHdr
- byte array representing the optional user's header.HipoException
- if filename arg is null or bad,
if this method already called without being
followed by reset.public final WriterMT setCompressionType(CompressionType compression)
compression
- compression typepublic java.nio.ByteBuffer createHeader(byte[] userHdr)
userHdr
- byte array containing a user-defined header, may be null.java.nio.ByteBuffer createHeader(java.nio.ByteBuffer userHdr)
userHdr
- buffer containing a user-defined header which must be READY-TO-READ!public void writeRecord(RecordOutputStream rec) throws java.lang.IllegalArgumentException, HipoException
addEvent(byte[], int, int)
is not thread-safe.rec
- record objectjava.lang.IllegalArgumentException
- if arg's byte order is opposite to output endian.HipoException
- if we cannot replace internal buffer of the current record
if it needs to be expanded since the buffer was provided
by the user.public void addEvent(byte[] buffer, int offset, int length) throws HipoException
writeRecord(RecordOutputStream)
is not thread-safe.buffer
- array to add to the file.offset
- offset into array from which to start writing data.length
- number of bytes to write from array.HipoException
- if buffer arg is null.public void addEvent(byte[] buffer) throws HipoException
writeRecord(RecordOutputStream)
is not thread-safe.buffer
- array to add to the file.HipoException
- if buffer arg is null.public void addEvent(java.nio.ByteBuffer buffer) throws HipoException
buffer
- array to add to the file.HipoException
- if buffer arg is null or byte order is wrong.public void addEvent(EvioBank bank) throws HipoException
bank
- event to add to the file.HipoException
- if arg is null.public void addEvent(EvioNode node) throws HipoException
node
- node to add to the file.HipoException
- if node is null or does not correspond to a bank.public void reset()
open(String)
.public void close()
The addEvent or addRecord methods must no longer be called.
close
in interface java.lang.AutoCloseable