com.devexperts.qd.ng
Class RecordBuffer

java.lang.Object
  extended by com.devexperts.qd.ng.RecordSource
      extended by com.devexperts.qd.ng.RecordBuffer
All Implemented Interfaces:
DataConsumer, DataIterator, DataProvider, DataVisitor, RecordSink, SubscriptionIterator, SubscriptionVisitor

public final class RecordBuffer
extends RecordSource
implements RecordSink, DataProvider, DataConsumer, DataIterator, DataVisitor, SubscriptionIterator, SubscriptionVisitor

Buffers int and obj values of data records. This class has two cursors of its own -- one is used for read and is returned by cursorAt(long), current(), and next() methods, the other is used for write and is returned by add(com.devexperts.qd.DataRecord, int, java.lang.String) method. This class is faster replacement for DataBuffer.

This class is not synchronized and is not thread-safe without external synchronization.


Constructor Summary
RecordBuffer()
          Creates new RecordBuffer.
 
Method Summary
 RecordCursor add(DataRecord record, int cipher, java.lang.String symbol)
          Returns write cursor at the current limit and advances limit.
 RecordCursor add(RecordCursor from)
          Adds a record from the specified RecordCursor.
 void addAll(RecordSource source)
           
 void addAll(RecordSource source, SubscriptionFilter filter)
           
 void append(RecordCursor cursor)
          Adds a record from the specified RecordCursor.
 boolean appendAndCompactIfNeeded(RecordCursor cursor)
          Adds a record from the specified RecordCursor performing compact() if needed to avoid growing the size of internal arrays.
 void cleanup(RecordCursor cursor)
          Cleanups the data at the corresponding cursor and reduces size() by one.
 void clear()
           
 void compact()
          Compacts data by copying everything between position and limit to the beginning of the buffer.
 boolean compact(RecordFilter filter)
          Compacts data by copying all records that are accepted by filter between position and limit to the beginning of the buffer.
 RecordCursor current()
          Returs read cursor at the current position.
 RecordCursor cursorAt(long position)
          Returns read cursor at a specified position.
 boolean examineData(DataVisitor visitor)
           
 int getCipher()
          Deprecated. Use next() and RecordCursor.getCipher()
static RecordBuffer getInstance()
          Returns instance from thread-local pool.
 long getLimit()
          Retruns limit of this buffer, which is the position of the record that will be added with add(com.devexperts.qd.DataRecord, int, java.lang.String) method.
 long getPosition()
          Retruns position of the current record.
 java.lang.String getSymbol()
          Deprecated. Use next() and RecordCursor.getSymbol()
 long getTime()
          Deprecated. Use next() and RecordCursor.getTime()
 boolean hasCapacity()
          Returns whether visitor has capacity to efficiently visit next record.
 boolean hasNext()
           
 boolean isEmpty()
           
 RecordSource newSource()
          Returns new RecordSource that reads this record buffer from current position to its current limit.
 RecordSource newSource(long start, long end)
          Returns new RecordSource that reads this record buffer from the specified start position to the specified end position.
 RecordCursor next()
          Returns read cursor at the current position and advances position to next record.
 int nextIntField()
          Deprecated. Use next() and RecordCursor.getInt(int)
 java.lang.Object nextObjField()
          Deprecated. Use next() and RecordCursor.getObj(int)
 DataRecord nextRecord()
          Deprecated. Use next()
 void processData(DataIterator it)
          This implementation calls processData(it, null).
 void processData(DataIterator it, SubscriptionFilter filter)
          Processes data from specified iterator and adds to this record buffer all records that are accepted by the specified filter.
 void release()
          Releases this RecordBuffer to a thread-local pool.
 void removeAt(long position)
          Removes record at the specified position.
 void replaceRecordAt(long position, DataRecord newRecord)
           
 void replaceSymbolAt(long position, int cipher, java.lang.String symbol)
           
 boolean retrieveData(DataVisitor visitor)
          Retrieves accumulated data into specified data visitor.
 boolean retrieveData(RecordSink sink)
           
 int retrieveDataAndCount(RecordFilter filter, DataVisitor visitor, boolean cleanup)
          Retrieves data starting from this buffer's position into the specified visitor and counts the number of retrieved records.
 int retrieveDataAndCount(RecordFilter filter, RecordSink sink, boolean cleanup)
          Retrieves data starting from this buffer's position into the specified sink and counts the number of retrieved records.
 void rewind()
           
 void setDataListener(DataListener listener)
          This method throws UnsupportedOperationException.
 void setLimit(long limit)
          Sets new buffer limit and discards everything that written after that point.
 void setPosition(long position)
           
 int size()
          Returns the number of records that can be retrieved from this RecordBuffer.
 void visitIntField(DataIntField field, int value)
          Deprecated. Use add(com.devexperts.qd.DataRecord, int, java.lang.String) or get visitor via visitor().
 void visitObjField(DataObjField field, java.lang.Object value)
          Deprecated. Use add(com.devexperts.qd.DataRecord, int, java.lang.String) or get visitor via visitor().
 DataVisitor visitor()
          Returns DataVisitor that adds all records to this buffer.
 DataVisitor visitor(int max, RecordFilter filter)
          Returns DataVisitor that adds at most max records that match the specified filter to this buffer.
 void visitRecord(DataRecord record, int cipher, java.lang.String symbol)
          Deprecated. Use add(com.devexperts.qd.DataRecord, int, java.lang.String) or get visitor via visitor().
 void visitRecord(DataRecord record, int cipher, java.lang.String symbol, long time)
          Deprecated. Use add(com.devexperts.qd.DataRecord, int, java.lang.String).
 RecordCursor writeCurrent()
          Returs write cursor at the current position.
 RecordCursor writeCursorAt(long position)
          Returns write cursor at a specified position.
 RecordCursor writeNext()
          Returns write cursor at the current position and advances position to next record.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RecordBuffer

public RecordBuffer()
Creates new RecordBuffer.

Method Detail

getInstance

public static RecordBuffer getInstance()
Returns instance from thread-local pool. This instance can be returned to pool with release() method. The recommended usage pattern is:

RecordBuffer buf = RecordBuffer.getInstance();
// do some local data transfer via buf
buf.release();


release

public void release()
Releases this RecordBuffer to a thread-local pool.

See Also:
getInstance()

isEmpty

public boolean isEmpty()

size

public int size()
Returns the number of records that can be retrieved from this RecordBuffer.


clear

public void clear()

rewind

public void rewind()

newSource

public RecordSource newSource()
Returns new RecordSource that reads this record buffer from current position to its current limit. It is equivalent to newSource(getPosition(), getLimit()).

See Also:
newSource(long, long)

newSource

public RecordSource newSource(long start,
                              long end)
Returns new RecordSource that reads this record buffer from the specified start position to the specified end position.

See Also:
newSource()

getLimit

public long getLimit()
Retruns limit of this buffer, which is the position of the record that will be added with add(com.devexperts.qd.DataRecord, int, java.lang.String) method.


getPosition

public long getPosition()
Retruns position of the current record. This record will be read by next() and current() methods.

Specified by:
getPosition in class RecordSource

setPosition

public void setPosition(long position)

setLimit

public void setLimit(long limit)
Sets new buffer limit and discards everything that written after that point. This method also resets write cursor to avoid accidental corruption of data.


cursorAt

public RecordCursor cursorAt(long position)
Returns read cursor at a specified position. This postion should have been previously returned by getPosition() method.

Specified by:
cursorAt in class RecordSource

writeCursorAt

public RecordCursor writeCursorAt(long position)
Returns write cursor at a specified position. This postion should have been previously returned by getPosition() method.


current

public RecordCursor current()
Returs read cursor at the current position. Effect is same as cursorAt(getPosition()), but faster.


writeCurrent

public RecordCursor writeCurrent()
Returs write cursor at the current position. Effect is same as writeCursorAt(getPosition()), but faster.


hasNext

public boolean hasNext()

next

public RecordCursor next()
Returns read cursor at the current position and advances position to next record.

Specified by:
next in class RecordSource

writeNext

public RecordCursor writeNext()
Returns write cursor at the current position and advances position to next record.


add

public RecordCursor add(DataRecord record,
                        int cipher,
                        java.lang.String symbol)
Returns write cursor at the current limit and advances limit.

Returns:
Cursor to the resently added record.

add

public RecordCursor add(RecordCursor from)
Adds a record from the specified RecordCursor. This method is a shortcut to
 add(from.getRecord(), from.getCipher(), from.getSymbol()).
     copyFrom(from)
Use faster append(RecordCursor) method if you do not need to change appended data.

Returns:
Cursor to the resently added record.

append

public void append(RecordCursor cursor)
Adds a record from the specified RecordCursor.

Specified by:
append in interface RecordSink

appendAndCompactIfNeeded

public boolean appendAndCompactIfNeeded(RecordCursor cursor)
Adds a record from the specified RecordCursor performing compact() if needed to avoid growing the size of internal arrays.

Returns:
true if the buffer was compacted.

replaceRecordAt

public void replaceRecordAt(long position,
                            DataRecord newRecord)

replaceSymbolAt

public void replaceSymbolAt(long position,
                            int cipher,
                            java.lang.String symbol)

removeAt

public void removeAt(long position)
Removes record at the specified position. All records to the right are shifted. Limit is correspondingly decreased, but position is not changed. This method also resets both cursors to avoid accidental corruption of data.


examineData

public boolean examineData(DataVisitor visitor)

addAll

public void addAll(RecordSource source)

addAll

public void addAll(RecordSource source,
                   SubscriptionFilter filter)

processData

public void processData(DataIterator it,
                        SubscriptionFilter filter)
Processes data from specified iterator and adds to this record buffer all records that are accepted by the specified filter. When iterator implements RecordSource interface, this method calls addAll((RecordSource)it, filter).


visitor

public DataVisitor visitor()
Returns DataVisitor that adds all records to this buffer.


visitor

public DataVisitor visitor(int max,
                           RecordFilter filter)
Returns DataVisitor that adds at most max records that match the specified filter to this buffer.


compact

public void compact()
Compacts data by copying everything between position and limit to the beginning of the buffer. After compaction all previously queried long positions (via getPosition() or getLimit() methods) become invalid. This method has the same effect as compact(null).


compact

public boolean compact(RecordFilter filter)
Compacts data by copying all records that are accepted by filter between position and limit to the beginning of the buffer. After compaction all previously queried long positions (via getPosition() or getLimit() methods) become invalid.

Parameters:
filter - Filter for records. Only accepted records are retained. null filter is assumed to accept everything.
Returns:
true if any data was filtered out (not accepted by the filter)

cleanup

public void cleanup(RecordCursor cursor)
Cleanups the data at the corresponding cursor and reduces size() by one. The cursor should have been retrieved via one of the following methods: next, current, add(record,cipher,symbol), add(cursor), cursorAt, writeCursorAt


retrieveDataAndCount

public int retrieveDataAndCount(RecordFilter filter,
                                DataVisitor visitor,
                                boolean cleanup)
Retrieves data starting from this buffer's position into the specified visitor and counts the number of retrieved records. The retrieved data is optionally cleaned up from the buffer.

Parameters:
filter - The filter to apply to all records before retrieiving. null means retrieve all.
visitor - The visitor to retrieve data to (will only receive records accepted by filter).
cleanup - when true, retrieved data is cleaned up from the buffer. When data is cleaned up, you cannot rewind() to retrieve it again; size is also reduced during cleanup.
Returns:
The number of processed records (that were passed to filter).

retrieveDataAndCount

public int retrieveDataAndCount(RecordFilter filter,
                                RecordSink sink,
                                boolean cleanup)
Retrieves data starting from this buffer's position into the specified sink and counts the number of retrieved records. The retrieved data is optionally cleaned up from the buffer.

Parameters:
filter - The filter to apply to all records before retrieiving. null means retrieve all.
sink - The sink to retrieve data to (will only receive records accepted by filter).
cleanup - when true, retrieved data is cleaned up from the buffer. When data is cleaned up, you cannot rewind() to retrieve it again; size is also reduced during cleanup.
Returns:
The number of processed records (that were passed to filter).

processData

public void processData(DataIterator it)
This implementation calls processData(it, null).

Specified by:
processData in interface DataConsumer

nextRecord

public DataRecord nextRecord()
Deprecated. Use next()

Description copied from interface: DataIterator
Returns next record. Returns null if no more records available.

Specified by:
nextRecord in interface DataIterator
Specified by:
nextRecord in interface SubscriptionIterator

getCipher

public int getCipher()
Deprecated. Use next() and RecordCursor.getCipher()

Description copied from interface: DataIterator
Returns cipher for the current record returned by last call to DataIterator.nextRecord(). Returns 0 if not encoded or if no current record is being iterated.

Specified by:
getCipher in interface DataIterator
Specified by:
getCipher in interface SubscriptionIterator

getSymbol

public java.lang.String getSymbol()
Deprecated. Use next() and RecordCursor.getSymbol()

Description copied from interface: DataIterator
Returns symbol for the current record returned by last call to DataIterator.nextRecord(). Returns null if encoded or if no current record is being iterated.

Specified by:
getSymbol in interface DataIterator
Specified by:
getSymbol in interface SubscriptionIterator

getTime

public long getTime()
Deprecated. Use next() and RecordCursor.getTime()

Description copied from interface: SubscriptionIterator
Returns time for the current record returned by last call to SubscriptionIterator.nextRecord(). Returns 0 if not historical or if no current record is being iterated.

Specified by:
getTime in interface SubscriptionIterator

nextIntField

public int nextIntField()
Deprecated. Use next() and RecordCursor.getInt(int)

Description copied from interface: DataIterator
Returns next Int-field within current record being iterated.

Specified by:
nextIntField in interface DataIterator

nextObjField

public java.lang.Object nextObjField()
Deprecated. Use next() and RecordCursor.getObj(int)

Description copied from interface: DataIterator
Returns next Obj-field within current record being iterated.

Specified by:
nextObjField in interface DataIterator

hasCapacity

public boolean hasCapacity()
Description copied from interface: DataVisitor
Returns whether visitor has capacity to efficiently visit next record. This method may be used to advise data provider that it is desirable to stop current string of visiting and to keep remaining data. However, at present, data provider is not obliged to adhere to this method contract.

NOTE: data visitor must process all data that is passed to it via visitXXX calls no matter whether it has capacity to do it efficiently.

Specified by:
hasCapacity in interface DataVisitor
Specified by:
hasCapacity in interface RecordSink
Specified by:
hasCapacity in interface SubscriptionVisitor

visitRecord

public void visitRecord(DataRecord record,
                        int cipher,
                        java.lang.String symbol)
Deprecated. Use add(com.devexperts.qd.DataRecord, int, java.lang.String) or get visitor via visitor().

Description copied from interface: DataVisitor
Visits next record.

Specified by:
visitRecord in interface DataVisitor

visitRecord

public void visitRecord(DataRecord record,
                        int cipher,
                        java.lang.String symbol,
                        long time)
Deprecated. Use add(com.devexperts.qd.DataRecord, int, java.lang.String).

Description copied from interface: SubscriptionVisitor
Visits next record.

Specified by:
visitRecord in interface SubscriptionVisitor

visitIntField

public void visitIntField(DataIntField field,
                          int value)
Deprecated. Use add(com.devexperts.qd.DataRecord, int, java.lang.String) or get visitor via visitor().

Description copied from interface: DataVisitor
Visits next Int-field within current record.

Specified by:
visitIntField in interface DataVisitor

visitObjField

public void visitObjField(DataObjField field,
                          java.lang.Object value)
Deprecated. Use add(com.devexperts.qd.DataRecord, int, java.lang.String) or get visitor via visitor().

Description copied from interface: DataVisitor
Visits next Obj-field within current record.

Specified by:
visitObjField in interface DataVisitor

retrieveData

public boolean retrieveData(DataVisitor visitor)
Description copied from interface: DataProvider
Retrieves accumulated data into specified data visitor. Returns true if some data still remains in the provider or false if all accumulated data were retrieved.

Specified by:
retrieveData in interface DataProvider

retrieveData

public boolean retrieveData(RecordSink sink)

setDataListener

public void setDataListener(DataListener listener)
This method throws UnsupportedOperationException.

Specified by:
setDataListener in interface DataProvider