Here are the examples of the java api com.devexperts.qd.DataRecord taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
260 Examples
19
Source : SubscriptionDumpImpl.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public void visitRecord(DataRecord record) throws IOException {
int rid = record.getId();
if (!seenRecords[rid]) {
out.writeCompactInt(-rid - 2);
out.writeUTFString(record.getName());
seenRecords[rid] = true;
} else
out.writeCompactInt(rid);
}
19
Source : CollectorManagementImplBase.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private void reportSubscriptionImpl(ReportBuilder rb, Collector collector, String recordName, String symbol) {
QDContract contract = collector.getContract();
rb.header(nameCollector(collector), ReportBuilder.HEADER_LEVEL_COLLECTOR);
DataScheme scheme = collector.getScheme();
boolean allSymbols = "*".equals(symbol);
boolean allRecords = "*".equals(recordName);
int cipher = allSymbols ? 0 : scheme.getCodec().encode(symbol);
DataRecord record = allRecords ? null : scheme.findRecordByName(recordName);
if (!allRecords && record == null) {
rb.message("No such record in scheme");
return;
}
ReportSubscriptionSink sink = new ReportSubscriptionSink(rb, contract, cipher, symbol, record, allSymbols, allRecords);
sink.begin();
collector.examineSubscription(sink);
sink.end();
}
19
Source : DumpVisitorBase.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
boolean matches(DataRecord record, String symbol) {
return (filterSymbol == null || filterSymbol.equals(symbol)) && (filterRecord == null || filterRecord.equals(record.getName()));
}
19
Source : DumpUtil.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
static String timeString(DataRecord r, long time) {
return time + " {" + r.getIntField(0).toString((int) (time >> 32)) + " " + r.getIntField(1).toString((int) time) + "}";
}
19
Source : RemoveEventTimeTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
public clreplaced RemoveEventTimeTest extends TestCase {
private static final DataRecord RECORD = new DefaultRecord(0, "TradeHistory", true, new DataIntField[] { new CompactIntField(0, "TradeHistory.Time"), new CompactIntField(1, "TradeHistory.Sequence"), new CompactIntField(2, "TradeHistory.Price"), new CompactIntField(3, "TradeHistory.Size") }, new DataObjField[0]);
private static final DataScheme SCHEME = new DefaultScheme(PentaCodec.INSTANCE, RECORD);
private static final String SYMBOL1 = "SYMBOL1";
private static final String SYMBOL2 = "SYMBOL2";
public void testBinary() {
BinaryQTPComposer composer = new BinaryQTPComposer(SCHEME, true);
BinaryQTPParser parser = new BinaryQTPParser(SCHEME);
composer.setOptSet(ProtocolOption.SUPPORTED_SET);
check(composer, parser);
}
public void testBinaryWithEventTimeFields() {
BinaryQTPComposer composer = new BinaryQTPComposer(SCHEME, true);
BinaryQTPParser parser = new BinaryQTPParser(SCHEME);
composer.setOptSet(ProtocolOption.SUPPORTED_SET);
composer.setWriteEventTimeSequence(true);
check(composer, parser);
}
public void testText() {
TextQTPComposer composer = new TextQTPComposer(SCHEME);
TextQTPParser parser = new TextQTPParser(SCHEME);
composer.setOptSet(ProtocolOption.SUPPORTED_SET);
check(composer, parser);
}
public void testTextWithTimeFields() {
TextQTPComposer composer = new TextQTPComposer(SCHEME);
TextQTPParser parser = new TextQTPParser(SCHEME);
composer.setOptSet(ProtocolOption.SUPPORTED_SET);
composer.setWriteEventTimeSequence(true);
check(composer, parser);
}
private void check(AbstractQTPComposer composer, AbstractQTPParser parser) {
RecordBuffer bufOut = RecordBuffer.getInstance(RecordMode.FLAGGED_DATA);
// no flags
addData(bufOut, SYMBOL1, 0, 42, 80);
// flags
addData(bufOut, SYMBOL1, EventFlag.REMOVE_EVENT.flag(), 42, 80);
// not flags
addData(bufOut, SYMBOL2, 0, 142, 180);
// flags
addData(bufOut, SYMBOL2, EventFlag.REMOVE_EVENT.flag(), 142, 180);
ByteArrayOutput out = new ByteArrayOutput();
composer.setOutput(out);
composer.visitData(bufOut, MessageType.TICKER_DATA);
RecordBuffer bufIn = RecordBuffer.getInstance(RecordMode.FLAGGED_DATA);
ByteArrayInput in = new ByteArrayInput(out.toByteArray());
parser.setInput(in);
parser.parse(new MessageConsumerAdapter() {
@Override
public void processTickerData(DataIterator iterator) {
bufIn.processData(iterator);
}
});
replacedertEquals(bufOut.size(), bufIn.size());
replacedertData(bufIn, SYMBOL1, 0, 42, 80);
replacedertData(bufIn, SYMBOL1, EventFlag.REMOVE_EVENT.flag(), 42, 80);
replacedertData(bufIn, SYMBOL2, 0, 142, 180);
replacedertData(bufIn, SYMBOL2, EventFlag.REMOVE_EVENT.flag(), 142, 180);
}
private void addData(RecordBuffer buf, String symbol, int flags, int time, int sequence) {
RecordCursor cursor = buf.add(RECORD, 0, symbol);
cursor.setEventFlags(flags);
cursor.setInt(0, time);
cursor.setInt(1, sequence);
}
private void replacedertData(RecordBuffer buf, String symbol, int flags, int time, int sequence) {
RecordCursor cursor = buf.next();
replacedertEquals(0, cursor.getCipher());
replacedertEquals(symbol, cursor.getSymbol());
replacedertEquals(flags, cursor.getEventFlags());
replacedertEquals(time, cursor.getInt(0));
replacedertEquals(sequence, cursor.getInt(1));
}
}
19
Source : TextQTPParser.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
/**
* Parses subscription for specified record and stores it in the record buffer.
*
* @param buf the record buffer.
* @param recordName name of a record.
* @return true, if subscription was parsed successfully, false otherwise.
*/
private boolean parseSubscription(RecordBuffer buf, String recordName) {
DataRecord record = scheme.findRecordByName(recordName);
DataField[] fieldsRearrangement = describedRecords.get(recordName);
if (record == null) {
if (fieldsRearrangement == null) {
QDLog.log.error("Subscription to unknown record \"" + recordName + "\"");
return false;
} else {
// This record was not initially described in our scheme, but was described later.
// We have nothing to do with it and just skip it.
return true;
}
}
String symbol = tokenizer.nextToken();
if (symbol == null) {
QDLog.log.error("Symbol name expected for record \"" + recordName + "\"");
return false;
}
int cipher = scheme.getCodec().encode(symbol);
long time = 0;
if (lastMessageType.isHistorySubscriptionAdd()) {
String timeStr = tokenizer.nextToken();
if (timeStr != null) {
try {
time = ((long) record.getIntField(0).parseString(timeStr) << 32);
timeStr = tokenizer.nextToken();
if (timeStr != null)
time |= record.getIntField(1).parseString(timeStr) & 0xFFFFFFFFL;
} catch (IllegalArgumentException e) {
QDLog.log.error("Cannot parse time for historySubscriptionAdd subscription");
return false;
}
}
}
RecordCursor cur = buf.add(record, cipher, symbol);
cur.setTime(time);
cur.setEventFlags(lastMessageType.isSubscriptionRemove() ? EventFlag.REMOVE_SYMBOL.flag() : 0);
parseExtraTokens(cur, false);
return true;
}
19
Source : TextQTPParser.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
/**
* Parses description for specified record.
* @param recordName name of a record which is being described.
* @return true, if description was parsed successfully, false otherwise.
*/
private boolean parseRecordDescription(String recordName) {
String symbolString = tokenizer.nextToken();
if (!BuiltinFields.SYMBOL_FIELD_NAME.equals(symbolString) && !BuiltinFields.EVENT_SYMBOL_FIELD_NAME.equals(symbolString)) {
QDLog.log.error("Invalid symbol field name '" + symbolString + "'");
return false;
}
DataRecord record = scheme.findRecordByName(recordName);
ArrayList<DataField> fieldsRearrangement = new ArrayList<>();
for (String fieldName; (fieldName = tokenizer.nextToken()) != null; ) {
DataField field = record == null ? null : record.findFieldByName(fieldName);
if (field == null && readEventTimeSequence && fieldName.equals(BuiltinFields.EVENT_TIME_FIELD_NAME))
field = EVENT_TIME_DESC;
fieldsRearrangement.add(field);
}
describedRecords.put(recordName, fieldsRearrangement.toArray(new DataField[fieldsRearrangement.size()]));
return true;
}
19
Source : TextQTPParser.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
/**
* Parses specified record inside some data message and stores it
* in the record buffer.
*
* @param buf the record buffer.
* @param recordName name of a record to parse.
* @return true, if record was parsed successfully, false otherwise.
*/
private boolean parseData(RecordBuffer buf, String recordName) {
DataRecord record = scheme.findRecordByName(recordName);
DataField[] fieldsRearrangement = describedRecords.get(recordName);
if (record == null) {
if (fieldsRearrangement == null) {
QDLog.log.error("Unknown record \"" + recordName + "\"");
return false;
} else {
// This record was not initially described in our scheme, but was described later.
// We have nothing to do with it and just skip it.
return true;
}
}
String symbol = tokenizer.nextToken();
if (symbol == null) {
QDLog.log.error("Symbol name expected for record \"" + recordName + "\"");
return false;
}
int cipher = scheme.getCodec().encode(symbol);
RecordCursor cur = buf.add(record, cipher, symbol);
if (fieldsRearrangement == null) {
// Record was not re-described. Just read it.
for (int i = 0, n = record.getIntFieldCount(); tokenizer.hasMoreTokens() && i < n; i++) if (!(record.getIntField(i) instanceof VoidIntField))
trySetField(record.getIntField(i), cur, tokenizer.nextToken());
for (int i = 0, n = record.getObjFieldCount(); tokenizer.hasMoreTokens() && i < n; i++) trySetField(record.getObjField(i), cur, tokenizer.nextToken());
} else {
// Record was re-described.
for (int i = 0; tokenizer.hasMoreTokens() && i < fieldsRearrangement.length; i++) {
DataField field = fieldsRearrangement[i];
String token = tokenizer.nextToken();
if (field == EVENT_TIME_DESC) {
trySetEventTime(cur, token);
} else if (field != null) {
trySetField(field, cur, token);
}
}
}
parseExtraTokens(cur, true);
setEventTimeSequenceIfNeeded(cur);
replaceFieldIfNeeded(cur);
return true;
}
19
Source : TextQTPComposer.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
// ------------------------ describe records support ------------------------
@Override
protected void describeRecord(DataRecord record) {
writeln();
writeDescribeRecordLine(record);
}
19
Source : TextQTPComposer.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
protected void writeHistorySubscriptionTime(DataRecord record, long time) {
separator();
if (!record.hasTime()) {
throw new IllegalArgumentException("Met history subscription for record with no time coordinate.");
}
if (time != 0) {
writeIntField(record.getIntField(0), (int) (time >>> 32));
writeIntField(record.getIntField(1), (int) (time));
}
}
19
Source : TextQTPComposer.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
protected int writeRecordHeader(DataRecord record, int cipher, String symbol, int eventFlags) {
writeln();
write(record.getName());
separator();
write(record.getScheme().getCodec().decode(cipher, symbol));
return eventFlags;
}
19
Source : BlobQTPParser.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
/**
* Parses QTP messages in blob format from byte stream.
* Blob format contains {@link MessageType#HISTORY_DATA} messages
* (by default, changeable with {@link #readAs(MessageType) readAs} method)
* for a single record and symbol that are specified in constructor.
* The input for this parser must be configured with {@link #setInput(BufferedInput)} method
* immediately after construction.
*
* @see AbstractQTPParser
*/
public clreplaced BlobQTPParser extends AbstractQTPParser {
private final DataRecord record;
private final int cipher;
private final String symbol;
/**
* Constructs parser with a specified record and symbol.
*/
public BlobQTPParser(DataRecord record, String symbol) {
super(record.getScheme());
this.record = record;
this.cipher = record.getScheme().getCodec().encode(symbol);
this.symbol = symbol;
}
@Override
protected void parseImpl(BufferedInput in, MessageConsumer consumer) {
try {
RecordBuffer buf = nextRecordsMessage(consumer, MessageType.HISTORY_DATA);
// Parsing loop
long position = in.totalPosition();
while (in.hasAvailable()) {
try {
RecordCursor cursor = buf.add(record, cipher, symbol);
record.readFields(in, cursor);
} catch (EOFException e) {
// record is not complete
break;
}
}
stats.updateIOReadRecordBytes(record.getId(), in.totalPosition() - position);
stats.updateIOReadDataRecord();
} catch (IOException e) {
QDLog.log.error("Cannot parse data blob", e);
consumer.handleCorruptedStream();
}
}
}
19
Source : BlobQTPComposer.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
/**
* Composes QTP messages in blob format into byte stream.
* The output for this composer must be configured with {@link #setOutput(BufferedOutput)} method
* immediately after construction.
*
* @see AbstractQTPComposer
*/
public clreplaced BlobQTPComposer extends AbstractQTPComposer {
// ======================== private instance fields ========================
private final DataRecord record;
private final int cipher;
private final String symbol;
// ======================== constructor and instance methods ========================
/**
* Constructs blob composer with a specified record and symbol.
* You must {@link #setOutput(BufferedOutput) setOutput} before using this composer.
*
* @param record composing record.
* @param symbol composing symbol.
*/
public BlobQTPComposer(DataRecord record, String symbol) {
super(record.getScheme(), false);
this.record = record;
this.cipher = record.getScheme().getCodec().encode(symbol);
this.symbol = symbol;
}
// ------------------------ AbstractQTPComposer implementation ------------------------
@Override
protected int writeRecordHeader(DataRecord record, int cipher, String symbol, int eventFlags) throws IOException {
if (record != this.record)
throw new IOException("Wrong record for this BLOB");
if (cipher != this.cipher || cipher == 0 && !this.symbol.equals(symbol))
throw new IOException("Wrong symbol for this BLOB");
return 0;
}
@Override
protected void writeHistorySubscriptionTime(DataRecord record, long time) throws IOException {
throw new IOException("Unsupported message for BLOB");
}
@Override
protected void writeIntField(DataIntField field, int value) throws IOException {
field.writeInt(msg, value);
}
@Override
protected void writeObjField(DataObjField field, Object value) throws IOException {
field.writeObj(msg, value);
}
@Override
protected void writeField(DataField field, RecordCursor cursor) throws IOException {
field.write(msg, cursor);
}
@Override
protected void writeMessageHeader(MessageType messageType) throws IOException {
if (messageType != MessageType.HISTORY_DATA && messageType != MessageType.RAW_DATA)
throw new IOException("Unsupported message for BLOB");
}
}
19
Source : BlobQTPComposer.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
protected void writeHistorySubscriptionTime(DataRecord record, long time) throws IOException {
throw new IOException("Unsupported message for BLOB");
}
19
Source : BlobQTPComposer.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
// ------------------------ AbstractQTPComposer implementation ------------------------
@Override
protected int writeRecordHeader(DataRecord record, int cipher, String symbol, int eventFlags) throws IOException {
if (record != this.record)
throw new IOException("Wrong record for this BLOB");
if (cipher != this.cipher || cipher == 0 && !this.symbol.equals(symbol))
throw new IOException("Wrong symbol for this BLOB");
return 0;
}
19
Source : ConnectTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private NavigableSet<Item> parseSubscription(RecordBuffer subscription) {
subscription.rewind();
NavigableSet<Item> items = new TreeSet<>();
for (RecordCursor cursor; (cursor = subscription.next()) != null; ) {
DataRecord record = cursor.getRecord();
int cipher = cursor.getCipher();
String symbol = cursor.getSymbol();
items.add(new Item(record, cipher, symbol));
}
return items;
}
19
Source : ConnectTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private void fillRecord(RecordCursor cursor, DataRecord record) {
for (int i = 0; i < record.getIntFieldCount(); ++i) {
DataIntField field = record.getIntField(i);
if (field instanceof PlainIntField) {
int val = RNG.nextInt(15000);
if (RNG.nextBoolean()) {
val = -val;
}
cursor.setInt(i, val);
} else if (field instanceof CompactCharField) {
int val = RNG.nextInt('Z' - 'A' + 1) + 'A';
cursor.setInt(i, val);
} else if (field instanceof DecimalField) {
int val = RNG.nextInt(100500);
cursor.setInt(i, val);
} else {
cursor.setInt(i, 0);
}
}
for (int i = 0; i < record.getObjFieldCount(); ++i) {
DataObjField field = record.getObjField(i);
if (field instanceof StringField) {
cursor.setObj(i, "" + RNG.nextDouble());
} else {
cursor.setObj(i, null);
}
}
}
19
Source : Tools.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
// ======== Some static util methods ========
public static DataRecord[] parseRecords(String recordList, DataScheme scheme) {
RecordOnlyFilter filter = RecordOnlyFilter.valueOf(recordList, scheme);
List<DataRecord> result = new ArrayList<>();
for (int i = 0, n = scheme.getRecordCount(); i < n; i++) {
DataRecord record = scheme.getRecord(i);
if (filter.acceptRecord(record))
result.add(record);
}
return result.toArray(new DataRecord[result.size()]);
}
19
Source : StampComposer.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
protected void describeRecord(DataRecord record) {
writeln();
write(TimeFormat.DEFAULT.withMillis().format(timeMillis));
separator();
writeDescribeRecordLine(record);
}
19
Source : SchemeDump.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
protected void executeImpl(String[] args) {
if (args.length > 1)
wrongNumberOfArguments();
for (DataRecord record : Tools.parseRecords(args.length == 0 ? "*" : args[0], QDFactory.getDefaultScheme())) printRecord(record);
}
19
Source : SchemeDump.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
public static void printRecord(DataRecord record) {
System.out.println("Record #" + record.getId() + " : " + record.getName() + (record.hasTime() ? " (hasTime)" : ""));
for (int j = 0; j < record.getIntFieldCount(); j++) printField("Int", j, record.getIntField(j));
for (int j = 0; j < record.getObjFieldCount(); j++) printField("Obj", j, record.getObjField(j));
}
19
Source : RecordFields.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
final clreplaced RecordFields {
private final DataRecord record;
private final int intFieldCount;
private final int objFieldCount;
private final int[] intIndexes;
private final int[] objIndexes;
private final Set<DataField> fieldSet = new HashSet<>();
RecordFields(DataRecord record, OptionFields fields) {
this.record = record;
// int fields
int ifc = 0;
for (int i = 0; i < record.getIntFieldCount(); i++) if (fields.matches(record.getIntField(i))) {
ifc++;
fieldSet.add(record.getIntField(i));
}
intFieldCount = ifc;
intIndexes = new int[ifc];
for (int i = 0, j = 0; i < record.getIntFieldCount(); i++) if (fields.matches(record.getIntField(i)))
intIndexes[j++] = i;
// obj fields
int ofs = 0;
for (int i = 0; i < record.getObjFieldCount(); i++) if (fields.matches(record.getObjField(i))) {
ofs++;
fieldSet.add(record.getObjField(i));
}
objFieldCount = ofs;
objIndexes = new int[ofs];
for (int i = 0, j = 0; i < record.getObjFieldCount(); i++) if (fields.matches(record.getObjField(i)))
objIndexes[j++] = i;
}
public DataRecord getRecord() {
return record;
}
public boolean isEmpty() {
return intFieldCount == 0 && objFieldCount == 0;
}
public int getIntFieldCount() {
return intFieldCount;
}
public int getObjFieldCount() {
return objFieldCount;
}
public int getIntIndex(int i) {
return intIndexes[i];
}
public int getObjIndex(int i) {
return objIndexes[i];
}
public boolean contains(DataField f) {
return fieldSet.contains(f);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < intFieldCount; i++) {
if (sb.length() > 0)
sb.append(", ");
sb.append(record.getIntField(intIndexes[i]));
}
for (int i = 0; i < objFieldCount; i++) {
if (sb.length() > 0)
sb.append(", ");
sb.append(record.getObjField(objIndexes[i]));
}
return sb.toString();
}
}
19
Source : RandomRecordsProvider.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private static DataRecord[] getRecords(DataScheme scheme) {
DataRecord[] records = new DataRecord[scheme.getRecordCount()];
for (int i = records.length; --i >= 0; ) records[i] = scheme.getRecord(i);
return records;
}
19
Source : PostingThread.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private void recordInfo(String name) {
DataRecord record = scheme.findRecordByName(name);
if (record == null) {
helpRecordNotFound(name);
return;
}
System.out.print(record.getName() + "\t" + BuiltinFields.EVENT_SYMBOL_FIELD_NAME);
for (int i = 0; i < record.getIntFieldCount(); i++) if (!(record.getIntField(i) instanceof VoidIntField))
System.out.print("\t" + record.getIntField(i).getPropertyName());
for (int i = 0; i < record.getObjFieldCount(); i++) System.out.print("\t" + record.getObjField(i).getPropertyName());
System.out.println();
}
19
Source : ConnectorRecordsSymbols.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
clreplaced ConnectorRecordsSymbols {
static interface Listener {
public void recordsAvailable(RecordProvider provider, MessageType message);
}
private final DataRecord[] records;
private final String[] symbols;
private final long millis;
ConnectorRecordsSymbols(DataRecord[] records, String[] symbols, long millis) {
this.records = records;
this.symbols = symbols;
this.millis = millis;
}
void subscribe(QDEndpoint endpoint, Listener listener) {
RecordBuffer sub = RecordBuffer.getInstance(RecordMode.HISTORY_SUBSCRIPTION);
long time = (millis / 1000) << 32;
for (String symbol : symbols) {
int cipher = endpoint.getScheme().getCodec().encode(symbol);
for (DataRecord record : records) sub.add(record, cipher, symbol).setTime(time);
}
for (QDCollector c : endpoint.getCollectors()) {
// create agent with all supported options
QDAgent agent = c.agentBuilder().withOptSet(ProtocolOption.SUPPORTED_SET).build();
agent.setSubscription(sub);
sub.rewind();
MessageType message = MessageType.forData(c.getContract());
agent.setRecordListener(provider -> listener.recordsAvailable(provider, message));
}
sub.release();
}
}
19
Source : Parser.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private String getRecordCategory(DataRecord record) {
if (record == null)
// records that are not in our scheme
return "UNKNOWN";
String name = record.getName();
int i = Math.max(name.lastIndexOf('&'), name.lastIndexOf('*'));
return i >= 0 ? name.substring(0, i + 1) + '*' : name;
}
19
Source : StripedTicker.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public void getData(RecordCursor.Owner owner, DataRecord record, int cipher, String symbol) {
collector(cipher, symbol).getData(owner, record, cipher, symbol);
}
19
Source : StripedTicker.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public boolean isAvailable(DataRecord record, int cipher, String symbol) {
return collector(cipher, symbol).isAvailable(record, cipher, symbol);
}
19
Source : StripedTicker.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public boolean getDataIfAvailable(RecordCursor.Owner owner, DataRecord record, int cipher, String symbol) {
return collector(cipher, symbol).getDataIfAvailable(owner, record, cipher, symbol);
}
19
Source : StripedTicker.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public boolean getDataIfSubscribed(RecordCursor.Owner owner, DataRecord record, int cipher, String symbol) {
return collector(cipher, symbol).getDataIfSubscribed(owner, record, cipher, symbol);
}
19
Source : StripedHistory.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public boolean examineData(DataRecord record, int cipher, String symbol, long startTime, long endTime, DataVisitor visitor) {
return collector(cipher, symbol).examineData(record, cipher, symbol, startTime, endTime, visitor);
}
19
Source : StripedHistory.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public long getMaxAvailableTime(DataRecord record, int cipher, String symbol) {
return collector(cipher, symbol).getMaxAvailableTime(record, cipher, symbol);
}
19
Source : StripedHistory.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public boolean examineData(DataRecord record, int cipher, String symbol, long startTime, long endTime, RecordSink sink) {
return collector(cipher, symbol).examineData(record, cipher, symbol, startTime, endTime, sink);
}
19
Source : StripedHistory.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public long getMinAvailableTime(DataRecord record, int cipher, String symbol) {
return collector(cipher, symbol).getMinAvailableTime(record, cipher, symbol);
}
19
Source : StripedHistory.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public int getAvailableCount(DataRecord record, int cipher, String symbol, long startTime, long endTime) {
return collector(cipher, symbol).getAvailableCount(record, cipher, symbol, startTime, endTime);
}
19
Source : StripedCollector.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public boolean isSubscribed(DataRecord record, int cipher, String symbol, long time) {
return collector(cipher, symbol).isSubscribed(record, cipher, symbol, time);
}
19
Source : StripedAgent.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
@Override
public boolean isSubscribed(DataRecord record, int cipher, String symbol, long time) {
return agents[collector.index(cipher, symbol)].isSubscribed(record, cipher, symbol, time);
}
19
Source : SampleClient.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private static GUIColumn[] createColumns(DataRecord record) {
return createColumns(new DataRecord[] { record });
}
19
Source : SampleClient.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private static GUIColumn[] createColumns(DataRecord[] records) {
ArrayList<GUIColumn> result = new ArrayList<>();
for (DataRecord record : records) {
int nint = record.getIntFieldCount();
int nobj = record.getObjFieldCount();
for (int j = 0; j < nint; j++) result.add(new SampleColumn.IntColumn(record.getIntField(j)));
for (int j = 0; j < nobj; j++) result.add(new SampleColumn.ObjColumn(record.getObjField(j)));
}
return result.toArray(new GUIColumn[result.size()]);
}
19
Source : WildcardStreamWithFiltersTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
void check(boolean distributorFilter, boolean agentFilter) {
// create stuff
DataScheme scheme = new TestDataScheme(20110825);
DataRecord record = scheme.getRecord(0);
SymbolCodec codec = scheme.getCodec();
QDStream stream = QDFactory.getDefaultFactory().createStream(scheme);
stream.setEnableWildcards(true);
SubscriptionFilter filter = PatternFilter.valueOf("A*", scheme);
QDDistributor distributor = stream.distributorBuilder().withFilter(QDFilter.fromFilter(distributorFilter ? filter : null, scheme)).build();
QDAgent agent = stream.agentBuilder().withFilter(QDFilter.fromFilter(agentFilter ? filter : null, scheme)).build();
// subscribe to wildcard
SubscriptionBuffer sub = new SubscriptionBuffer();
sub.visitRecord(record, codec.getWildcardCipher(), null);
agent.addSubscription(sub);
// distribute 3 records
RecordBuffer buf = RecordBuffer.getInstance();
int validCipher = codec.encode("A");
buf.add(record, validCipher, null);
buf.add(record, codec.encode("B"), null);
buf.add(record, validCipher, null);
distributor.processData(buf);
// check that only two records with valid symbols are processed
buf.clear();
agent.retrieveData(buf);
replacedertEquals(2, buf.size());
replacedertEquals(validCipher, buf.next().getCipher());
replacedertEquals(validCipher, buf.next().getCipher());
}
19
Source : TickerAdditionalTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
public clreplaced TickerAdditionalTest extends TestCase {
private static final DataScheme SCHEME = new TestDataScheme(20081107);
private static final DataRecord RECORD = SCHEME.getRecord(0);
public void testTickerAddRemoveLong() {
checkAddRemove(QDFactory.getDefaultFactory().createTicker(SCHEME), "Long symbol");
}
private void checkAddRemove(QDCollector collector, String symbol) {
RecordBuffer buf = new RecordBuffer();
QDAgent agent = collector.agentBuilder().build();
QDDistributor distributor = collector.distributorBuilder().build();
// create big static subscription & data
for (int i = 0; i < SCHEME.getRecordCount() * 100; i++) {
String s = Integer.toString(i);
int cipher = SCHEME.getCodec().encode(s);
buf.add(RECORD, cipher, s);
}
agent.addSubscription(buf);
buf.rewind();
distributor.processData(buf);
buf.clear();
// add remove one element
int cipher = SCHEME.getCodec().encode(symbol);
buf.add(RECORD, cipher, symbol);
for (int i = 0; i < 50000; i++) {
agent.addSubscription(buf);
buf.rewind();
distributor.processData(buf);
buf.rewind();
agent.removeSubscription(buf);
buf.rewind();
}
}
}
19
Source : TestDataScheme.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private static DataRecord[] createRecords(int record_count, long seed, Type type, DataRecord record) {
Sequencer seq = new Sequencer(seed);
DataRecord[] records = new DataRecord[record_count];
for (int i = 0; i < record_count; i++) if (record != null && i == record.getId())
records[i] = record;
else {
String name = "Record" + i;
records[i] = new DefaultRecord(i, name, type.has_time, createIntFields(seq, name, type.min_int_fields), createObjFields(seq, name));
}
return records;
}
19
Source : SnapshotProviderTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
public clreplaced SnapshotProviderTest extends TestCase {
private static final DataScheme SCHEME = new TestDataScheme(20140721, TestDataScheme.Type.HAS_TIME_AND_VALUE);
private static final DataRecord RECORD = SCHEME.getRecord(0);
// no cipher
private static final String SYMBOL = "LongTestSymbol";
public void testTickerSnapshotProvider() {
checkSnapshotProvider(QDFactory.getDefaultFactory().createTicker(SCHEME));
}
public void testHistorySnapshotProvider() {
checkSnapshotProvider(QDFactory.getDefaultFactory().createHistory(SCHEME));
}
static final int SNAPSHOT = 0;
static final int DATA = 1;
private void checkSnapshotProvider(QDCollector collector) {
// Create distributor & agent
QDDistributor distributor = collector.distributorBuilder().build();
final QDAgent agent = collector.agentBuilder().build();
// Install snapshot & data records listeners
final boolean[] available = new boolean[2];
agent.getSnapshotProvider().setRecordListener(new RecordListener() {
@Override
public void recordsAvailable(RecordProvider provider) {
replacedertSame(agent.getSnapshotProvider(), provider);
available[SNAPSHOT] = true;
}
});
agent.setRecordListener(new RecordListener() {
@Override
public void recordsAvailable(RecordProvider provider) {
replacedertSame(agent, provider);
available[DATA] = true;
}
});
// Subscribe to record & symbol
RecordBuffer sub = RecordBuffer.getInstance(RecordMode.SUBSCRIPTION);
sub.add(RECORD, 0, SYMBOL);
agent.addSubscription(sub);
replacedertFalse(available[SNAPSHOT]);
replacedertFalse(available[DATA]);
// Process data item via distributor
RecordBuffer dataIn = RecordBuffer.getInstance();
dataIn.add(RECORD, 0, SYMBOL);
distributor.process(dataIn);
// make sure both snapshot & data are available
replacedertTrue(available[SNAPSHOT]);
replacedertTrue(available[DATA]);
// retrieve this item via snapshot
RecordBuffer dataOut = RecordBuffer.getInstance();
replacedertFalse(agent.getSnapshotProvider().retrieve(dataOut));
replacedertEquals(1, dataOut.size());
RecordCursor cur = dataOut.next();
replacedertEquals(RECORD, cur.getRecord());
replacedertEquals(SYMBOL, cur.getSymbol());
// ensure that nothing more comes out via regular data retrieve
dataOut.clear();
replacedertFalse(agent.retrieve(dataOut));
replacedertEquals(0, dataOut.size());
}
}
19
Source : SchemeAdaptationTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
public clreplaced SchemeAdaptationTest extends TestCase {
private static final int SYMBOL_COUNT = 8;
private static final int GEN_RECORDS = 1000;
private static final int GEN_FIELDS = 6 * GEN_RECORDS;
private static final int GEN_BLOCK_SIZE = 100;
private static final int PORT = 10789;
private static final String I_CHECK_FLD = "MyRecord.FldC";
private static final String S_CHECK_FLD = "MyRecord.FldF";
private static final String D2I_CHECK_FLD = "MyRecord.FldK";
private static final String I2D_CHECK_FLD = "MyRecord.FldL";
private static final String O2B_CHECK_FLD = "MyRecord.FldM";
private static final String B2O_CHECK_FLD = "MyRecord.FldN";
private static final DataRecord SRC_REC = new DefaultRecord(15, "MyRecord", false, new DataIntField[] { new PlainIntField(0, "MyRecord.FldA"), new DecimalField(1, D2I_CHECK_FLD), new CompactIntField(2, "MyRecord.FldB"), new DecimalField(3, I_CHECK_FLD), new CompactIntField(4, "MyRecord.FldD"), new CompactIntField(5, I2D_CHECK_FLD) }, new DataObjField[] { new PlainObjField(0, O2B_CHECK_FLD), new ByteArrayField(1, B2O_CHECK_FLD), new StringField(2, "MyRecord.FldE"), new StringField(3, S_CHECK_FLD) });
private static final DataRecord DST_REC = new DefaultRecord(23, "MyRecord", false, new DataIntField[] { new DecimalField(0, I2D_CHECK_FLD), new CompactIntField(1, "MyRecord.FldB"), new DecimalField(2, I_CHECK_FLD), new CompactIntField(3, "MyRecord.FldH"), new CompactIntField(4, "MyRecord.FldD"), new PlainIntField(5, D2I_CHECK_FLD), new CompactIntField(6, "MyRecord.FldG") }, new DataObjField[] { new StringField(0, S_CHECK_FLD), new StringField(1, "MyRecord.FldI"), new ByteArrayField(2, O2B_CHECK_FLD), new StringField(3, "MyRecord.FldJ"), new PlainObjField(4, B2O_CHECK_FLD) });
private static final DataScheme SRC_SCHEME = new TestDataScheme(100, 7543, TestDataScheme.Type.SIMPLE, SRC_REC);
private static final DataScheme DST_SCHEME = new TestDataScheme(100, 3255, TestDataScheme.Type.SIMPLE, DST_REC);
private static final SymbolCodec CODEC = SRC_SCHEME.getCodec();
private final String[] symbols = { "A", "GE", "IBM", "MSFT", "OGGZY", ".IBMGQ", "SIXCHR", "$NIKKEI" };
private final int[] ciphers = new int[SYMBOL_COUNT];
{
for (int i = 0; i < SYMBOL_COUNT; i++) {
ciphers[i] = CODEC.encode(symbols[i]);
if (ciphers[i] != 0)
symbols[i] = null;
}
}
private int gen_hash = 0;
private interface Task {
void start();
void join() throws InterruptedException;
void stop();
}
private clreplaced Server implements Task, Runnable, SubscriptionListener, SubscriptionVisitor {
QDCollector collector = QDFactory.getDefaultFactory().createStream(SRC_SCHEME);
QDDistributor distributor = collector.distributorBuilder().build();
MessageConnector connector = new ServerSocketConnector(new AgentAdapter.Factory(collector), PORT);
Thread thread = new Thread(this, "Server");
Random r = new Random(23424);
int sub_received_cnt;
boolean[] sub_received = new boolean[SYMBOL_COUNT];
Server() {
}
@Override
public void start() {
connector.start();
thread.start();
distributor.getAddedSubscriptionProvider().setSubscriptionListener(this);
}
@Override
public void subscriptionAvailable(SubscriptionProvider provider) {
provider.retrieveSubscription(this);
}
@Override
public boolean hasCapacity() {
return true;
}
@Override
public void visitRecord(DataRecord record, int cipher, String symbol, long time) {
if (record == SRC_REC)
synchronized (this) {
for (int i = 0; i < SYMBOL_COUNT; i++) if (cipher == ciphers[i] && symbol == symbols[i])
if (!sub_received[i]) {
sub_received[i] = true;
sub_received_cnt++;
notifyAll();
}
}
}
@Override
public void run() {
try {
synchronized (this) {
while (sub_received_cnt < SYMBOL_COUNT) wait();
}
DataBuffer buf = new DataBuffer();
int generated_records_cnt = 0;
int last_percent = 0;
while (generated_records_cnt < GEN_RECORDS) {
int cnt = r.nextInt(Math.min(GEN_RECORDS - generated_records_cnt, GEN_BLOCK_SIZE)) + 1;
for (int i = 0; i < cnt; i++) {
int j = r.nextInt(SYMBOL_COUNT);
buf.visitRecord(SRC_REC, ciphers[j], symbols[j]);
gen_hash *= 13;
for (int k = 0; k < SRC_REC.getIntFieldCount(); k++) {
DataIntField fld = SRC_REC.getIntField(k);
int value = r.nextInt(100000);
if (fld.getName().equals(I_CHECK_FLD))
gen_hash += value;
if (fld.getName().equals(D2I_CHECK_FLD)) {
gen_hash += 2 * value;
value = Decimal.compose(value);
}
if (fld.getName().equals(I2D_CHECK_FLD)) {
gen_hash += 3 * value;
}
buf.visitIntField(fld, value);
}
for (int k = 0; k < SRC_REC.getObjFieldCount(); k++) {
DataObjField fld = SRC_REC.getObjField(k);
Object value = new Point(r.nextInt(1000), r.nextInt(1000));
if (fld.getName().equals(S_CHECK_FLD)) {
value = value.toString();
gen_hash += 5 * value.hashCode();
}
if (fld.getName().equals(O2B_CHECK_FLD)) {
gen_hash += 7 * value.hashCode();
}
if (fld.getName().equals(B2O_CHECK_FLD)) {
gen_hash += 11 * value.hashCode();
value = IOUtil.objectToBytes(value);
}
buf.visitObjField(fld, value);
}
}
distributor.processData(buf);
Thread.sleep(10);
generated_records_cnt += cnt;
int new_percent = generated_records_cnt * 10 / GEN_RECORDS;
if (new_percent != last_percent) {
System.out.println(new_percent * 10 + "%");
last_percent = new_percent;
}
}
} catch (InterruptedException e) {
// quit
} catch (IOException e) {
fail(e.toString());
}
}
@Override
public void join() throws InterruptedException {
thread.join();
}
@Override
public void stop() {
connector.stop();
}
}
private clreplaced Client implements Task, Runnable, DataListener, DataVisitor {
QDStats root_stats = new QDStats(QDStats.SType.ANY, DST_SCHEME);
QDCollector collector = QDFactory.getDefaultFactory().createStream(DST_SCHEME, root_stats);
QDAgent agent = collector.agentBuilder().build();
MessageConnector connector = new ClientSocketConnector(new DistributorAdapter.Factory(collector), "localhost", PORT);
Thread thread = new Thread(this, "Client");
MARSEndpoint marsEndpoint;
ConnectorsMonitoringTask monitoring;
boolean broken;
int received_recs;
int received_flds;
int received_hash;
Client() {
}
@Override
public void start() {
connector.setStats(root_stats.create(QDStats.SType.CLIENT_SOCKET_CONNECTOR));
marsEndpoint = MARSEndpoint.newBuilder().acquire();
monitoring = new ConnectorsMonitoringTask(null, QDLog.log, root_stats, marsEndpoint.getRoot(), Collections.singletonList(connector));
connector.start();
thread.start();
}
@Override
public void dataAvailable(DataProvider provider) {
provider.retrieveData(this);
}
@Override
public boolean hasCapacity() {
return true;
}
@Override
public void visitRecord(DataRecord record, int cipher, String symbol) {
if (record != DST_REC)
synchronized (this) {
broken = true;
notifyAll();
}
else
synchronized (this) {
received_recs++;
received_hash *= 13;
notifyAll();
}
}
@Override
public void visitIntField(DataIntField field, int value) {
if (field.getName().equals(I_CHECK_FLD))
synchronized (this) {
received_flds++;
received_hash += value;
notifyAll();
}
if (field.getName().equals(D2I_CHECK_FLD))
synchronized (this) {
received_flds++;
received_hash += 2 * value;
notifyAll();
}
if (field.getName().equals(I2D_CHECK_FLD))
synchronized (this) {
received_flds++;
received_hash += 3 * (int) Decimal.toDouble(value);
notifyAll();
}
}
@Override
public void visitObjField(DataObjField field, Object value) {
if (field.getName().equals(S_CHECK_FLD))
synchronized (this) {
received_flds++;
received_hash += 5 * value.hashCode();
notifyAll();
}
if (field.getName().equals(O2B_CHECK_FLD))
synchronized (this) {
received_flds++;
try {
received_hash += 7 * IOUtil.bytesToObject((byte[]) value).hashCode();
} catch (IOException e) {
fail(e.toString());
}
notifyAll();
}
if (field.getName().equals(B2O_CHECK_FLD))
synchronized (this) {
received_flds++;
received_hash += 11 * value.hashCode();
notifyAll();
}
}
@Override
public void run() {
try {
SubscriptionBuffer buf = new SubscriptionBuffer();
for (int i = 0; i < SYMBOL_COUNT; i++) buf.visitRecord(DST_REC, ciphers[i], symbols[i]);
agent.setSubscription(buf);
agent.setDataListener(this);
synchronized (this) {
while (!broken && received_flds < GEN_FIELDS) wait();
}
} catch (InterruptedException e) {
// quit
}
}
@Override
public void join() throws InterruptedException {
thread.join();
replacedertTrue(!broken);
replacedertEquals(received_recs, GEN_RECORDS);
replacedertEquals(received_flds, GEN_FIELDS);
replacedertEquals(received_hash, gen_hash);
String rep = monitoring.report();
replacedertTrue(rep, rep.matches("Subscription: 8; Storage: 0; Buffer: 0; Read: ([^;]+); Write: ([^;]+); (.*); CPU: \\d+\\.\\d\\d%\n" + " ClientSocket-Distributor localhost:\\d+ \\[1\\] Read: \\1; Write: \\2; \\3"));
}
@Override
public void stop() {
connector.stop();
monitoring.close();
marsEndpoint.release();
}
}
public void testSchemeAdaptation() throws InterruptedException {
Task srv = new Server();
Task cli = new Client();
srv.start();
cli.start();
srv.join();
cli.join();
srv.stop();
cli.stop();
}
@Override
protected void setUp() throws Exception {
ThreadCleanCheck.before();
}
@Override
protected void tearDown() throws Exception {
ThreadCleanCheck.after();
}
}
19
Source : RefCountTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
/**
* Reference counting unit test that is known to reproduce DR#4.
*/
public clreplaced RefCountTest extends TestCase {
public RefCountTest(String s) {
super(s);
}
private static final SymbolCodec codec = new PentaCodec();
private static final DataRecord record = new DefaultRecord(0, "Haba", false, null, null);
private static final DataScheme scheme = new DefaultScheme(codec, new DataRecord[] { record });
private static SubscriptionIterator sub(int size) {
SubscriptionBuffer sb = new SubscriptionBuffer();
while (--size >= 0) sb.visitRecord(record, 0, "abcde" + size);
return sb;
}
private static DataIterator data(int size) {
DataBuffer db = new DataBuffer();
while (--size >= 0) {
db.visitRecord(record, 0, "abcde" + size);
for (int i = 0; i < record.getIntFieldCount(); i++) db.visitIntField(record.getIntField(i), 0);
for (int i = 0; i < record.getObjFieldCount(); i++) db.visitObjField(record.getObjField(i), null);
}
return db;
}
public void testDistributorCloseA() {
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(scheme);
ticker.agentBuilder().build().setSubscription(sub(1));
for (int i = 10; --i >= 0; ) ticker.distributorBuilder().build().close();
}
public void testDistributorCloseB() {
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(scheme);
for (int i = 10; --i >= 0; ) {
ticker.distributorBuilder().build().close();
ticker.agentBuilder().build().setSubscription(sub(1));
}
}
public void testAgentCloseA() {
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(scheme);
for (int i = 10; --i >= 0; ) {
QDAgent agent = ticker.agentBuilder().build();
agent.setSubscription(sub(1));
agent.close();
}
}
public void testAgentCloseB() {
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(scheme);
QDDistributor distributor = ticker.distributorBuilder().build();
for (int i = 10; --i >= 0; ) {
QDAgent agent = ticker.agentBuilder().build();
agent.setSubscription(sub(1000));
agent.close();
}
}
public void testAgentCloseC() {
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(scheme);
QDDistributor distributor = ticker.distributorBuilder().build();
for (int i = 10; --i >= 0; ) {
QDAgent agent = ticker.agentBuilder().build();
agent.setSubscription(sub(1000));
distributor.getAddedSubscriptionProvider().retrieveSubscription(new SubscriptionBuffer());
agent.close();
distributor.getRemovedSubscriptionProvider().retrieveSubscription(new SubscriptionBuffer());
}
}
public void testTotal() {
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(scheme);
QDDistributor distributor = ticker.distributorBuilder().build();
QDAgent agent = ticker.agentBuilder().build();
agent.addSubscription(sub(1));
agent.removeSubscription(sub(1));
agent.addSubscription(sub(1));
distributor.processData(data(1));
}
public void testMultipleRecords() {
DataRecord[] records = new DataRecord[10];
SubscriptionBuffer sb = new SubscriptionBuffer();
for (int i = records.length; --i >= 0; ) sb.visitRecord(records[i] = new DefaultRecord(i, Integer.toString(i), false, null, null), 0, "abcde0");
DataScheme scheme = new DefaultScheme(codec, records);
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(scheme);
ticker.agentBuilder().build().setSubscription(sb);
}
}
19
Source : RefCountTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
public void testMultipleRecords() {
DataRecord[] records = new DataRecord[10];
SubscriptionBuffer sb = new SubscriptionBuffer();
for (int i = records.length; --i >= 0; ) sb.visitRecord(records[i] = new DefaultRecord(i, Integer.toString(i), false, null, null), 0, "abcde0");
DataScheme scheme = new DefaultScheme(codec, records);
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(scheme);
ticker.agentBuilder().build().setSubscription(sb);
}
19
Source : PartialRetrieveTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
private void testPartialRetrieve(QDCollector collector, boolean check_size) {
QDDistributor distributor = collector.distributorBuilder().build();
QDAgent agent = collector.agentBuilder().build();
DataRecord record = SCHEME.getRecord(0);
SubscriptionBuffer sub = new SubscriptionBuffer();
sub.visitRecord(record, SCHEME.getCodec().encode(TEST_SYMBOL), null, Long.MIN_VALUE);
agent.setSubscription(sub);
DataProvider provider = new RandomRecordsProvider(record, new String[] { TEST_SYMBOL }, MAX_RECORDS_PROVIDE);
int size_provided = 0;
int size_retrieved = 0;
for (int repeat = 0; repeat < REPEAT; repeat++) {
// Provide data
DataBuffer provider_buffer = new DataBuffer();
provider.retrieveData(provider_buffer);
size_provided += provider_buffer.size();
distributor.processData(provider_buffer);
// Retrive in small chunks -- should not fail!!!
DataBuffer retrieve_buffer = new DataBuffer() {
public boolean hasCapacity() {
return size() < MAX_RECORDS_RETRIEVE;
}
};
agent.retrieveData(retrieve_buffer);
size_retrieved += retrieve_buffer.size();
}
// Retrieve rest & check
DataBuffer buf = new DataBuffer();
agent.retrieveData(buf);
size_retrieved += buf.size();
if (check_size)
replacedertEquals(size_retrieved, size_provided);
}
19
Source : NoReentryTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
public clreplaced NoReentryTest extends TestCase {
private static final DataScheme SCHEME = new TestDataScheme(20091005);
private static final int SYM_A = SCHEME.getCodec().encode("A");
private static final DataRecord RECORD = SCHEME.getRecord(0);
public void testPotentialDeadLock() {
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(SCHEME);
final boolean[] seen_exception = new boolean[1];
ticker.setErrorHandler(new QDErrorHandler() {
public void handleDataError(DataProvider provider, Throwable t) {
if (t instanceof IllegalStateException)
seen_exception[0] = true;
else
fail(t.toString());
}
public void handleSubscriptionError(SubscriptionProvider provider, Throwable t) {
fail(t.toString());
}
});
final QDAgent agent = ticker.agentBuilder().build();
QDDistributor dist = ticker.distributorBuilder().build();
final SubscriptionBuffer sub = new SubscriptionBuffer();
sub.visitRecord(RECORD, SYM_A, null);
agent.setSubscription(sub.examiningIterator());
agent.setDataListener(new DataListener() {
public void dataAvailable(DataProvider provider) {
provider.retrieveData(new DataVisitor() {
public boolean hasCapacity() {
return true;
}
public void visitRecord(DataRecord record, int cipher, String symbol) {
// try to rerenter QD from inside of here
agent.addSubscription(sub.examiningIterator());
}
public void visitIntField(DataIntField field, int value) {
}
public void visitObjField(DataObjField field, Object value) {
}
});
}
});
RecordBuffer buf = new RecordBuffer();
buf.add(RECORD, SYM_A, null);
dist.processData(buf);
replacedertTrue(seen_exception[0]);
}
}
19
Source : LockTimeoutTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
public clreplaced LockTimeoutTest extends TestCase {
private static final DataScheme SCHEME = new TestDataScheme(20091005);
private static final int SYM_A = SCHEME.getCodec().encode("A");
private static final DataRecord RECORD = SCHEME.getRecord(0);
private static final String LOG_FILE = "LockTimeoutTest.log";
private static final Pattern LOG_PATTERN = Pattern.compile(".*Ticker local lock is taking too long to acquire for setSub operation. Last operation was retData.*");
private volatile boolean stopVisitor;
public void testLockTooLongWarning() throws IOException {
Logging.configureLogFile(LOG_FILE);
QDTicker ticker = QDFactory.getDefaultFactory().createTicker(SCHEME);
Tweaks.setTickerLockWaitLogInterval(SCHEME, "0.2s");
final QDAgent agent = ticker.agentBuilder().build();
QDDistributor dist = ticker.distributorBuilder().build();
final SubscriptionBuffer sub = new SubscriptionBuffer();
sub.visitRecord(RECORD, SYM_A, null);
agent.setSubscription(sub.examiningIterator());
// sync two threads
final CyclicBarrier barrier = new CyclicBarrier(2);
stopVisitor = false;
agent.setDataListener(new DataListener() {
public void dataAvailable(DataProvider provider) {
provider.retrieveData(new DataVisitor() {
public boolean hasCapacity() {
return true;
}
public void visitRecord(DataRecord record, int cipher, String symbol) {
try {
// wait for sub changing thread
try {
barrier.await();
} catch (BrokenBarrierException e) {
fail(e.toString());
}
// block until long lock waiting is reported
long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10);
do {
Thread.sleep(100);
if (checkLogFile(false))
break;
} while (!stopVisitor && System.currentTimeMillis() < deadline);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (IOException e) {
e.printStackTrace();
}
}
public void visitIntField(DataIntField field, int value) {
}
public void visitObjField(DataObjField field, Object value) {
}
});
}
});
// try to change subscription (waits for lock) in the different tread (only after data)
new Thread(new Runnable() {
public void run() {
// wait on barrier
try {
barrier.await();
} catch (InterruptedException e) {
fail(e.toString());
} catch (BrokenBarrierException e) {
Thread.currentThread().interrupt();
}
// now try!!!
agent.setSubscription(sub.examiningIterator());
stopVisitor = true;
}
}).start();
// process data (will block local lock in retrieve data)
RecordBuffer buf = new RecordBuffer();
buf.add(RECORD, SYM_A, null);
dist.processData(buf);
Tweaks.setTickerDefaults(SCHEME);
Logging.configureLogFile(System.getProperty("log.file"));
checkLogFile(true);
replacedertTrue(new File(LOG_FILE).delete());
}
private boolean checkLogFile(boolean reportFailure) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(LOG_FILE));
try {
String line;
while ((line = br.readLine()) != null) {
if (LOG_PATTERN.matcher(line).matches())
return true;
}
if (reportFailure)
fail("Required log pattern is not found in " + LOG_FILE);
} finally {
br.close();
}
return false;
}
}
19
Source : LargeSubscriptionTest.java
with Mozilla Public License 2.0
from devexperts
with Mozilla Public License 2.0
from devexperts
/**
* Large subscription performance unit test.
*/
public clreplaced LargeSubscriptionTest extends TestCase {
public LargeSubscriptionTest(String s) {
super(s);
// Measure only CPU time if possible to minimize CI system overload effects.
ThreadMXBean threadMX = ManagementFactory.getThreadMXBean();
cpuTimeMarksSupplier = threadMX.isCurrentThreadCpuTimeSupported() && threadMX.isThreadCpuTimeEnabled() ? threadMX::getCurrentThreadCpuTime : System::nanoTime;
}
private static final SymbolCodec codec = PentaCodec.INSTANCE;
private static final DataRecord record = new DefaultRecord(0, "Haba", false, null, null);
private static final DataScheme scheme = new DefaultScheme(codec, record);
private final LongSupplier cpuTimeMarksSupplier;
private static RecordBuffer sub(int size, boolean encodeable) {
Random rnd = new Random();
BitSet sub = new BitSet(1 << 24);
char[] c = new char[6];
RecordBuffer sb = new RecordBuffer();
while (sb.size() < size) {
int r = rnd.nextInt() & 0xFFFFFF;
if (sub.get(r))
continue;
sub.set(r);
for (int i = 0; i < c.length; i++) c[c.length - 1 - i] = (char) ((encodeable ? 'A' : 'a') + ((r >> (i << 2)) & 0x0F));
int cipher = codec.encode(c, 0, c.length);
String symbol = cipher == 0 ? new String(c) : null;
sb.add(record, cipher, symbol);
}
return sb;
}
public void testLargeSubscription() {
int size = 100_000;
long threshold = 1_000;
// measure until get a good result: warm-up & test environment instability may affect a couple of iterations
long minTime = 0;
for (int i = 0; i < 10; i++) {
long time = measureSubscribeAndClose(size);
if (i == 0 || minTime > time)
minTime = time;
if (minTime < threshold)
break;
}
replacedertTrue("subscribe & close exceeds " + threshold + " ms: " + minTime, minTime < threshold);
}
private long measureSubscribeAndClose(int subSize) {
QDTicker ticker = QDFactory.getDefaultFactory().tickerBuilder().withScheme(scheme).build();
QDAgent agent = ticker.agentBuilder().build();
RecordBuffer sub = sub(subSize, true);
long time1 = getTimeMark();
agent.setSubscription(sub);
long time2 = getTimeMark();
agent.close();
long time3 = getTimeMark();
long subscribeMillis = TimeUnit.NANOSECONDS.toMillis(time2 - time1);
long closeMillis = TimeUnit.NANOSECONDS.toMillis(time3 - time2);
System.out.println("Size " + subSize + ", subscribe " + subscribeMillis + " ms, close " + closeMillis + " ms");
return closeMillis + subscribeMillis;
}
private long getTimeMark() {
return cpuTimeMarksSupplier.getAsLong();
}
}
See More Examples