use of com.questdb.common.RecordCursor in project questdb by bluestreak01.
the class CachedRowAnalyticRecordSource method prepareCursor.
@Override
public RecordCursor prepareCursor(ReaderFactory factory, CancellationHandler cancellationHandler) {
recordList.clear();
for (int i = 0; i < orderGroupCount; i++) {
RedBlackTree tree = orderedSources.getQuick(i);
if (tree != null) {
tree.clear();
}
}
for (int i = 0, n = functions.size(); i < n; i++) {
functions.getQuick(i).reset();
}
final RecordCursor cursor = delegate.prepareCursor(factory, cancellationHandler);
this.parentCursor = cursor;
this.storageFacade.prepare(cursor.getStorageFacade());
// red&black trees, one for each comparator where comparator is not null
for (int i = 0; i < orderGroupCount; i++) {
RedBlackTree tree = orderedSources.getQuick(i);
if (tree != null) {
((MyComparator) tree.getComparator()).setCursor(cursor);
}
}
// step #1: store source cursor in record list
// - add record list' row ids to all trees, which will put these row ids in necessary order
// for this we will be using out comparator, which helps tree compare long values
// based on record these values are addressing
long rowid = -1;
while (cursor.hasNext()) {
cancellationHandler.check();
Record record = cursor.next();
long row = record.getRowId();
rowid = recordList.append(fakeRecord.of(row), rowid);
if (orderGroupCount > 0) {
for (int i = 0; i < orderGroupCount; i++) {
RedBlackTree tree = orderedSources.getQuick(i);
if (tree != null) {
tree.add(row);
}
}
}
}
for (int i = 0; i < orderGroupCount; i++) {
RedBlackTree tree = orderedSources.getQuick(i);
ObjList<AnalyticFunction> functions = functionGroups.getQuick(i);
if (tree != null) {
// step #2: populate all analytic functions with records in order of respective tree
RedBlackTree.LongIterator iterator = tree.iterator();
while (iterator.hasNext()) {
cancellationHandler.check();
Record record = cursor.recordAt(iterator.next());
for (int j = 0, n = functions.size(); j < n; j++) {
functions.getQuick(j).add(record);
}
}
} else {
// step #2: alternatively run record list through two-pass functions
for (int j = 0, n = functions.size(); j < n; j++) {
AnalyticFunction f = functions.getQuick(j);
if (f.getType() != AnalyticFunction.STREAM) {
recordList.toTop();
while (recordList.hasNext()) {
f.add(cursor.recordAt(recordList.next().getLong(0)));
}
}
}
}
}
recordList.toTop();
for (int i = 0, n = functions.size(); i < n; i++) {
functions.getQuick(i).prepare(cursor);
}
return this;
}
Aggregations