use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class KeyExpressionTest method testNestedThenRepeatsConcatentated.
@Test
public void testNestedThenRepeatsConcatentated() throws Exception {
final KeyExpression expression = field("nesty").nest("repeated_field", FanType.Concatenate);
expression.validate(NestedField.getDescriptor());
assertFalse(expression.createsDuplicates());
assertEquals(Collections.singletonList(scalar(Arrays.asList("lily", "rose"))), evaluate(expression, matryoshkaDolls));
assertEquals(Collections.singletonList(scalar(Collections.emptyList())), evaluate(expression, emptyNested));
assertEquals(Collections.singletonList(scalar(Collections.emptyList())), evaluate(expression, lonelyDoll));
assertEquals(Collections.singletonList(scalar(Collections.emptyList())), evaluate(expression, null));
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class KeyExpressionTest method testNestedThenConatenatedFields.
@Test
public void testNestedThenConatenatedFields() throws Exception {
final KeyExpression expression = field("nesty").nest(concatenateFields("regular_old_field", "regular_int_field"));
expression.validate(NestedField.getDescriptor());
assertFalse(expression.createsDuplicates());
assertEquals(Collections.singletonList(Key.Evaluated.concatenate("Mother", 1066)), evaluate(expression, matryoshkaDolls));
assertEquals(Collections.singletonList(Key.Evaluated.concatenate(NULL, NULL)), evaluate(expression, emptyNested));
assertEquals(Collections.singletonList(Key.Evaluated.concatenate(NULL, NULL)), evaluate(expression, lonelyDoll));
assertEquals(Collections.singletonList(Key.Evaluated.concatenate(NULL, NULL)), evaluate(expression, null));
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreReplaceIndexTest method replacementMoreComplicatedGraphFails.
@Test
public void replacementMoreComplicatedGraphFails() {
try (FDBRecordContext context = openContext()) {
MetaDataException err = assertThrows(MetaDataException.class, () -> openSimpleRecordStore(context, metaDataBuilder -> {
final KeyExpression expr = Key.Expressions.field("num_value_2");
final Index indexA = new Index("indexA", expr, IndexTypes.VALUE, ImmutableMap.of(IndexOptions.REPLACED_BY_OPTION_PREFIX + "_0", "indexB", IndexOptions.REPLACED_BY_OPTION_PREFIX + "_1", "indexC"));
final Index indexB = new Index("indexB", expr, IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "indexD"));
final Index indexC = new Index("indexC", expr, IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "indexD"));
final Index indexD = new Index("indexD", expr);
metaDataBuilder.addIndex("MySimpleRecord", indexA);
metaDataBuilder.addIndex("MySimpleRecord", indexB);
metaDataBuilder.addIndex("MySimpleRecord", indexC);
metaDataBuilder.addIndex("MySimpleRecord", indexD);
}));
assertThat(err.getMessage(), containsString("has replacement indexes"));
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreCountRecordsTest method countRecordsKeyed.
private void countRecordsKeyed(boolean useIndex) {
final KeyExpression key = field("num_value_3_indexed");
final RecordMetaDataHook hook = countKeyHook(key, useIndex, 0);
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
for (int i = 0; i < 100; i++) {
recordStore.saveRecord(makeRecord(i, 0, i % 5));
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertEquals(100, recordStore.getSnapshotRecordCount().join().longValue());
assertEquals(20, recordStore.getSnapshotRecordCount(key, Key.Evaluated.scalar(1)).join().longValue());
commit(context);
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreCountRecordsTest method recountAndClearRecords.
private void recountAndClearRecords(boolean useIndex) throws Exception {
final CountMetaDataHook countMetaDataHook = new CountMetaDataHook();
countMetaDataHook.baseHook = metaData -> metaData.removeIndex(COUNT_INDEX.getName());
final int startingPoint = 7890;
final int value1 = 12345;
final int value2 = 54321;
final int value3 = 24567;
try (FDBRecordContext context = openContext()) {
// Simulate the state the store would be in if this were done before counting was added.
recordStore = getStoreBuilder(context, simpleMetaData(countMetaDataHook)).setFormatVersion(FDBRecordStore.INFO_ADDED_FORMAT_VERSION).uncheckedOpen();
recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_EXISTS).join();
for (int i = 0; i < 90; i++) {
recordStore.saveRecord(makeRecord(i + startingPoint, value1, i % 5));
}
commit(context);
}
KeyExpression key3 = field("num_value_3_indexed");
countMetaDataHook.metaDataVersion++;
countMetaDataHook.baseHook = countKeyHook(key3, useIndex, countMetaDataHook.metaDataVersion);
try (FDBRecordContext context = openContext()) {
recordStore = getStoreBuilder(context, simpleMetaData(countMetaDataHook)).setFormatVersion(FDBRecordStore.RECORD_COUNT_ADDED_FORMAT_VERSION).uncheckedOpen();
for (int i = 90; i < 100; i++) {
recordStore.saveRecord(makeRecord(i + startingPoint, value2, i % 5));
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
recordStore = getStoreBuilder(context, simpleMetaData(countMetaDataHook)).setFormatVersion(FDBRecordStore.RECORD_COUNT_ADDED_FORMAT_VERSION).uncheckedOpen();
assertEquals(10, recordStore.getSnapshotRecordCount().join().longValue(), "should only see new records");
commit(context);
}
// Need to allow immediate rebuild of new count index.
final FDBRecordStoreBase.UserVersionChecker alwaysEnabled = new FDBRecordStoreBase.UserVersionChecker() {
@Override
public CompletableFuture<Integer> checkUserVersion(int oldUserVersion, int oldMetaDataVersion, RecordMetaDataProvider metaData) {
return CompletableFuture.completedFuture(1);
}
@Override
public IndexState needRebuildIndex(Index index, long recordCount, boolean indexOnNewRecordTypes) {
return IndexState.READABLE;
}
};
try (FDBRecordContext context = openContext()) {
uncheckedOpenSimpleRecordStore(context, countMetaDataHook);
// Index is rebuilt here automatically in useIndex case
recordStore.checkVersion(alwaysEnabled, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_NOT_EXISTS).join();
assertEquals(100, recordStore.getSnapshotRecordCount().join().longValue(), "should see all records");
assertEquals(20, recordStore.getSnapshotRecordCount(key3, Key.Evaluated.scalar(2)).join().longValue());
commit(context);
}
KeyExpression key2 = field("num_value_2");
countMetaDataHook.metaDataVersion++;
countMetaDataHook.baseHook = countKeyHook(key2, useIndex, countMetaDataHook.metaDataVersion);
try (FDBRecordContext context = openContext()) {
uncheckedOpenSimpleRecordStore(context, countMetaDataHook);
recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_NOT_EXISTS).join();
if (useIndex) {
// Need to manually rebuild index in this case.
Index index = recordStore.getRecordMetaData().getIndex("record_count");
recordStore.rebuildIndex(index).get();
assertThat(recordStore.isIndexReadable(index), is(true));
}
assertEquals(100, recordStore.getSnapshotRecordCount().join().longValue(), "should see all records");
for (int i = 0; i < 32; i++) {
recordStore.saveRecord(makeRecord(i + startingPoint + 1000, value3, 0));
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
uncheckedOpenSimpleRecordStore(context, countMetaDataHook);
assertEquals(90, recordStore.getSnapshotRecordCount(key2, Key.Evaluated.scalar(value1)).join().longValue());
assertEquals(10, recordStore.getSnapshotRecordCount(key2, Key.Evaluated.scalar(value2)).join().longValue());
assertEquals(32, recordStore.getSnapshotRecordCount(key2, Key.Evaluated.scalar(value3)).join().longValue());
}
KeyExpression pkey = field("rec_no");
countMetaDataHook.metaDataVersion++;
countMetaDataHook.baseHook = countKeyHook(pkey, useIndex, countMetaDataHook.metaDataVersion);
try (FDBRecordContext context = openContext()) {
uncheckedOpenSimpleRecordStore(context, countMetaDataHook);
recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.NONE).join();
if (useIndex) {
// Need to manually rebuild index in this case.
Index index = recordStore.getRecordMetaData().getIndex("record_count");
recordStore.rebuildIndex(index).get();
assertThat(recordStore.isIndexReadable(index), is(true));
}
assertEquals(132, recordStore.getSnapshotRecordCount().join().longValue());
for (int i = 0; i < 100; i++) {
assertEquals(1, recordStore.getSnapshotRecordCount(pkey, Key.Evaluated.scalar(i + startingPoint)).join().longValue(), "Incorrect when i is " + i);
}
}
}
Aggregations