use of com.apple.foundationdb.record.metadata.Key in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreCountRecordsTest method addCountIndex.
@Test
public void addCountIndex() throws Exception {
RecordMetaDataHook removeCountHook = metaData -> metaData.removeIndex(COUNT_INDEX.getName());
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, removeCountHook);
for (int i = 0; i < 10; i++) {
recordStore.saveRecord(makeRecord(i, 1066, i % 5));
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, removeCountHook);
recordStore.getSnapshotRecordCount().get();
fail("evaluated count without index or key");
} catch (RecordCoreException e) {
assertThat(e.getMessage(), containsString("requires appropriate index"));
}
RecordMetaDataHook hook = countKeyHook(field("num_value_3_indexed"), true, 10);
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
Index countIndex = recordStore.getRecordMetaData().getIndex("record_count");
assertThat(recordStore.getRecordStoreState().isReadable(countIndex), is(false));
assertThat(recordStore.getRecordStoreState().isDisabled(countIndex), is(true));
RecordCoreException e = assertThrows(RecordCoreException.class, () -> recordStore.getSnapshotRecordCount().get());
assertThat(e.getMessage(), containsString("requires appropriate index"));
}
// Build the index
try (OnlineIndexer onlineIndexBuilder = OnlineIndexer.forRecordStoreAndIndex(recordStore, "record_count")) {
onlineIndexBuilder.buildIndex();
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
Index countIndex = recordStore.getRecordMetaData().getIndex("record_count");
assertThat(recordStore.getRecordStoreState().isWriteOnly(countIndex), is(false));
assertEquals(10L, recordStore.getSnapshotRecordCount().get().longValue());
}
}
use of com.apple.foundationdb.record.metadata.Key in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreDeleteWhereTest method testDeleteWhereGroupedCount.
@Test
public void testDeleteWhereGroupedCount() throws Exception {
KeyExpression groupExpr = concat(field("header").nest(field("rec_no")), field("header").nest(field("path")));
RecordMetaDataHook hook = metaData -> {
metaData.getRecordType("MyRecord").setPrimaryKey(concat(field("header").nest(field("rec_no")), field("header").nest(field("path")), field("header").nest(field("num"))));
metaData.addUniversalIndex(new Index("MyRecord$groupedCount", new GroupingKeyExpression(groupExpr, 0), IndexTypes.COUNT));
metaData.addUniversalIndex(new Index("MyRecord$groupedUpdateCount", new GroupingKeyExpression(groupExpr, 0), IndexTypes.COUNT_UPDATES));
};
try (FDBRecordContext context = openContext()) {
openRecordWithHeader(context, hook);
saveHeaderRecord(1, "a", 0, "lynx");
saveHeaderRecord(1, "a", 1, "bobcat");
saveHeaderRecord(1, "b", 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()) {
openRecordWithHeader(context, hook);
// Number of records where first component of primary key is 1
assertEquals(3, recordStore.getSnapshotRecordCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
// Number of updates to such records
assertEquals(3, recordStore.getSnapshotRecordUpdateCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
recordStore.deleteRecordsWhere(Query.and(Query.field("header").matches(Query.field("rec_no").equalsValue(1)), Query.field("header").matches(Query.field("path").equalsValue("a"))));
assertEquals(1, recordStore.getSnapshotRecordCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
// Deleting by group prefix resets the update counter for the group(s)
assertEquals(1, recordStore.getSnapshotRecordUpdateCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
context.commit();
}
}
use of com.apple.foundationdb.record.metadata.Key in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreCountRecordsTest method addCountKey.
@Test
@SuppressWarnings("deprecation")
public void addCountKey() throws Exception {
RecordMetaDataHook removeCountHook = metaData -> metaData.removeIndex(COUNT_INDEX.getName());
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, removeCountHook);
for (int i = 0; i < 10; i++) {
recordStore.saveRecord(makeRecord(i, 1066, i % 5));
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, removeCountHook);
recordStore.getSnapshotRecordCount().get();
fail("evaluated count without index or key");
} catch (RecordCoreException e) {
assertThat(e.getMessage(), containsString("requires appropriate index"));
}
AtomicInteger versionCounter = new AtomicInteger(recordStore.getRecordMetaData().getVersion());
RecordMetaDataHook hook = md -> {
md.setRecordCountKey(field("num_value_3_indexed"));
md.setVersion(md.getVersion() + versionCounter.incrementAndGet());
};
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertThat(timer.getCount(FDBStoreTimer.Events.RECOUNT_RECORDS), equalTo(1));
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertThat(timer.getCount(FDBStoreTimer.Events.RECOUNT_RECORDS), equalTo(0));
assertEquals(10L, recordStore.getSnapshotRecordCount().get().longValue());
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
// Before it was deprecated, this is how a key would have been written.
RecordMetaDataProto.DataStoreInfo.Builder infoBuilder = recordStore.getRecordStoreState().getStoreHeader().toBuilder();
infoBuilder.getRecordCountKeyBuilder().getFieldBuilder().clearNullInterpretation();
recordStore.saveStoreHeader(infoBuilder.build());
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertThat(timer.getCount(FDBStoreTimer.Events.RECOUNT_RECORDS), equalTo(0));
assertEquals(10L, recordStore.getSnapshotRecordCount().get().longValue());
}
}
use of com.apple.foundationdb.record.metadata.Key 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.Key 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