use of herddb.model.TableContext in project herddb by diennea.
the class TableManager method streamTableData.
private Stream<Record> streamTableData(ScanStatement statement, StatementEvaluationContext context, Transaction transaction, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
statement.validateContext(context);
Predicate predicate = statement.getPredicate();
boolean acquireLock = transaction != null || forWrite || lockRequired;
LocalScanPageCache lastPageRead = acquireLock ? null : new LocalScanPageCache();
IndexOperation indexOperation = predicate != null ? predicate.getIndexOperation() : null;
boolean primaryIndexSeek = indexOperation instanceof PrimaryIndexSeek;
AbstractIndexManager useIndex = getIndexForTbleAccess(indexOperation);
Stream<Map.Entry<Bytes, Long>> scanner = keyToPage.scanner(indexOperation, context, tableContext, useIndex);
Stream<Record> resultFromTable = scanner.map(entry -> {
return accessRecord(entry, predicate, context, transaction, lastPageRead, primaryIndexSeek, forWrite, acquireLock);
}).filter(r -> r != null);
return resultFromTable;
}
use of herddb.model.TableContext in project herddb by diennea.
the class MemoryHashIndexManager method scanner.
@Override
public Stream<Bytes> scanner(IndexOperation operation, StatementEvaluationContext context, TableContext tableContext) throws StatementExecutionException {
if (operation instanceof SecondaryIndexSeek) {
SecondaryIndexSeek sis = (SecondaryIndexSeek) operation;
SQLRecordKeyFunction value = sis.value;
byte[] refvalue = value.computeNewValue(null, context, tableContext);
List<Bytes> result = data.get(Bytes.from_array(refvalue));
if (result != null) {
return result.stream();
} else {
return Stream.empty();
}
} else if (operation instanceof SecondaryIndexPrefixScan) {
SecondaryIndexPrefixScan sis = (SecondaryIndexPrefixScan) operation;
SQLRecordKeyFunction value = sis.value;
byte[] refvalue = value.computeNewValue(null, context, tableContext);
Predicate<Map.Entry<Bytes, List<Bytes>>> predicate = (Map.Entry<Bytes, List<Bytes>> entry) -> {
byte[] recordValue = entry.getKey().data;
return Bytes.startsWith(recordValue, refvalue.length, refvalue);
};
return data.entrySet().stream().filter(predicate).map(entry -> entry.getValue()).flatMap(l -> l.stream());
} else if (operation instanceof SecondaryIndexRangeScan) {
byte[] refminvalue;
SecondaryIndexRangeScan sis = (SecondaryIndexRangeScan) operation;
SQLRecordKeyFunction minKey = sis.minValue;
if (minKey != null) {
refminvalue = minKey.computeNewValue(null, context, tableContext);
} else {
refminvalue = null;
}
byte[] refmaxvalue;
SQLRecordKeyFunction maxKey = sis.maxValue;
if (maxKey != null) {
refmaxvalue = maxKey.computeNewValue(null, context, tableContext);
} else {
refmaxvalue = null;
}
Predicate<Map.Entry<Bytes, List<Bytes>>> predicate;
if (refminvalue != null && refmaxvalue == null) {
predicate = (Map.Entry<Bytes, List<Bytes>> entry) -> {
byte[] datum = entry.getKey().data;
return Bytes.compare(datum, refminvalue) >= 0;
};
} else if (refminvalue == null && refmaxvalue != null) {
predicate = (Map.Entry<Bytes, List<Bytes>> entry) -> {
byte[] datum = entry.getKey().data;
return Bytes.compare(datum, refmaxvalue) <= 0;
};
} else if (refminvalue != null && refmaxvalue != null) {
predicate = (Map.Entry<Bytes, List<Bytes>> entry) -> {
byte[] datum = entry.getKey().data;
return Bytes.compare(datum, refmaxvalue) <= 0 && Bytes.compare(datum, refminvalue) >= 0;
};
} else {
predicate = (Map.Entry<Bytes, List<Bytes>> entry) -> {
return true;
};
}
return data.entrySet().stream().filter(predicate).map(entry -> entry.getValue()).flatMap(l -> l.stream());
} else {
throw new UnsupportedOperationException("unsuppported index access type " + operation);
}
}
Aggregations