use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreCountRecordsTest method countRecordUpdates.
@Test
public void countRecordUpdates() {
final KeyExpression key = field("num_value_3_indexed");
final RecordMetaDataHook hook = countUpdatesKeyHook(key, 0);
HashMap<Integer, Integer> expectedCountBuckets = new HashMap<>();
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertEquals(0, recordStore.getSnapshotRecordUpdateCount().join().longValue());
// Create 100 records
for (int i = 0; i < 100; i++) {
int numBucket = i % 5;
recordStore.saveRecord(makeRecord(i, 0, numBucket));
expectedCountBuckets.put(numBucket, expectedCountBuckets.getOrDefault(numBucket, 0) + 1);
}
commit(context);
}
checkRecordUpdateCounts(expectedCountBuckets, hook, key);
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
// Delete 5 records, this shouldn't change the counts
for (int i = 95; i < 100; i++) {
recordStore.deleteRecord(Tuple.from(i));
}
commit(context);
}
checkRecordUpdateCounts(expectedCountBuckets, hook, key);
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
// Update 10 records
for (int i = 0; i < 10; i++) {
int numBucket = i % 5;
recordStore.saveRecord(makeRecord(i, 0, numBucket));
expectedCountBuckets.put(numBucket, expectedCountBuckets.getOrDefault(numBucket, 0) + 1);
}
commit(context);
}
checkRecordUpdateCounts(expectedCountBuckets, hook, key);
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
// Update and create (or re-create) some records
for (int i = 90; i < 110; i++) {
int numBucket = i % 5;
recordStore.saveRecord(makeRecord(i, 0, numBucket));
expectedCountBuckets.put(numBucket, expectedCountBuckets.getOrDefault(numBucket, 0) + 1);
}
// Delete 5 records
for (int i = 20; i < 25; i++) {
recordStore.deleteRecord(Tuple.from(i));
}
commit(context);
}
checkRecordUpdateCounts(expectedCountBuckets, hook, key);
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreDeleteWhereTest method testDeleteWhere.
private void testDeleteWhere(boolean useCountIndex) throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordWithHeaderPrimaryKey(context, useCountIndex);
saveHeaderRecord(1, "a", 0, "lynx");
saveHeaderRecord(1, "b", 1, "bobcat");
saveHeaderRecord(1, "c", 2, "panther");
saveHeaderRecord(2, "a", 3, "jaguar");
saveHeaderRecord(2, "b", 4, "leopard");
saveHeaderRecord(2, "c", 5, "lion");
saveHeaderRecord(2, "d", 6, "tiger");
context.commit();
}
try (FDBRecordContext context = openContext()) {
final KeyExpression groupExpr = openRecordWithHeaderPrimaryKey(context, useCountIndex);
assertEquals(3, recordStore.getSnapshotRecordCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
recordStore.deleteRecordsWhere(Query.field("header").matches(Query.field("rec_no").equalsValue(1)));
context.commit();
}
try (FDBRecordContext context = openContext()) {
final KeyExpression groupExpr = openRecordWithHeaderPrimaryKey(context, useCountIndex);
assertEquals(0, recordStore.getSnapshotRecordCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
int expectedNum = 3;
for (FDBStoredRecord<Message> storedRecord : recordStore.scanRecords(null, ScanProperties.FORWARD_SCAN).asList().join()) {
TestRecordsWithHeaderProto.MyRecord record = parseMyRecord(storedRecord.getRecord());
assertEquals(2, record.getHeader().getRecNo());
assertEquals(expectedNum++, record.getHeader().getNum());
}
assertEquals(7, expectedNum);
expectedNum = 3;
for (FDBIndexedRecord<Message> indexedRecord : recordStore.scanIndexRecords("MyRecord$str_value").asList().join()) {
TestRecordsWithHeaderProto.MyRecord record = parseMyRecord(indexedRecord.getRecord());
assertEquals(2, record.getHeader().getRecNo());
assertEquals(expectedNum++, record.getHeader().getNum());
}
assertEquals(7, expectedNum);
context.commit();
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreDeleteWhereTest method openRecordWithHeaderPrimaryKey.
@SuppressWarnings("deprecation")
private KeyExpression openRecordWithHeaderPrimaryKey(FDBRecordContext context, boolean useCountIndex) throws Exception {
final KeyExpression groupExpr = field("header").nest("rec_no");
openRecordWithHeader(context, metaData -> {
metaData.getRecordType("MyRecord").setPrimaryKey(field("header").nest(concatenateFields("rec_no", "path")));
metaData.addIndex("MyRecord", "MyRecord$str_value", concat(groupExpr, field("str_value")));
if (useCountIndex) {
metaData.addUniversalIndex(new Index("MyRecord$count", new GroupingKeyExpression(groupExpr, 0), IndexTypes.COUNT));
} else {
metaData.setRecordCountKey(groupExpr);
}
});
return groupExpr;
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexTest method minMaxOptional.
@ParameterizedTest(name = "minMaxLongOptional({0})")
@EnumSource(MinMaxIndexTypes.class)
public void minMaxOptional(MinMaxIndexTypes indexTypes) throws Exception {
final KeyExpression key = field("num_value_3_indexed").ungrouped();
final RecordMetaDataHook hook = md -> {
RecordTypeBuilder type = md.getRecordType("MySimpleRecord");
md.addIndex(type, new Index("min", key, indexTypes.min()));
md.addIndex(type, new Index("max", key, indexTypes.max()));
};
final IndexAggregateFunction minOverall = new IndexAggregateFunction(FunctionNames.MIN_EVER, key, null);
final IndexAggregateFunction maxOverall = new IndexAggregateFunction(FunctionNames.MAX_EVER, key, null);
List<String> types = Collections.singletonList("MySimpleRecord");
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(1066L);
recordStore.saveRecord(recBuilder.build());
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
final Tuple expected = indexTypes == MinMaxIndexTypes.TUPLE ? Tuple.fromList(Collections.singletonList(null)) : null;
assertEquals(expected, recordStore.evaluateAggregateFunction(types, minOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertEquals(expected, recordStore.evaluateAggregateFunction(types, maxOverall, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
commit(context);
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexTest method sumUnsetOptional.
@Test
public void sumUnsetOptional() throws Exception {
final KeyExpression key = field("num_value_3_indexed").ungrouped();
final RecordMetaDataHook hook = md -> md.addIndex("MySimpleRecord", new Index("sum", key, IndexTypes.SUM));
final IndexAggregateFunction total = new IndexAggregateFunction(FunctionNames.SUM, key, null);
List<String> types = Collections.singletonList("MySimpleRecord");
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(1066L);
recordStore.saveRecord(recBuilder.build());
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertEquals(0, recordStore.evaluateAggregateFunction(types, total, Key.Evaluated.EMPTY, IsolationLevel.SERIALIZABLE).join().getLong(0));
commit(context);
}
}
Aggregations