use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.
the class FDBRecordContextTest method logWithoutSettingId.
@Test
public void logWithoutSettingId() {
try (FDBRecordContext context = fdb.openContext()) {
RecordCoreException err = assertThrows(RecordCoreException.class, context::logTransaction);
assertEquals("Cannot log transaction as ID is not set", err.getMessage());
context.ensureActive().getReadVersion().join();
context.commit();
}
}
use of com.apple.foundationdb.record.RecordCoreException 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.RecordCoreException in project fdb-record-layer by FoundationDB.
the class FDBReverseDirectoryCacheTest method testPutIfNotExistsWrongValue.
@Test
public void testPutIfNotExistsWrongValue() throws Exception {
final Random random = new Random();
final String name = "dir_" + Math.abs(random.nextInt());
final Long id;
ScopedValue<String> scopedName = globalScope.wrap(name);
try (FDBRecordContext context = openContext()) {
Transaction tr = context.ensureActive();
// need to use the FDB DirectoryLayer to bypass LocatableResolver which populates the reverse directory cache automatically
id = Tuple.fromBytes(DirectoryLayer.getDefault().createOrOpen(tr, PathUtil.from(name)).get().pack()).getLong(0);
commit(context);
}
FDBReverseDirectoryCache rdc = fdb.getReverseDirectoryCache();
// Store the correct value
try (FDBRecordContext context = openContext()) {
rdc.putIfNotExists(context, scopedName, id).get();
// The put should be considered a hard miss because it had to write to the cache
assertEquals(0L, rdc.getPersistentCacheHitCount());
assertEquals(1L, rdc.getPersistentCacheMissCount());
assertEquals(0L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_HIT_COUNT));
assertEquals(1L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_MISS_COUNT));
commit(context);
}
// Try again with a different value
try (FDBRecordContext context = openContext()) {
rdc.putIfNotExists(context, globalScope.wrap(name + "_x"), id).get();
commit(context);
fail("Should have thrown an exception due to wrong value");
} catch (ExecutionException e) {
// This will throw if we didn't get the exception we wanted
RecordCoreException yay = (RecordCoreException) e.getCause();
}
}
use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.
the class LuceneIndexQueryPlan method getToPartialRecord.
/**
* Get the {@link IndexKeyValueToPartialRecord} instance for an {@link IndexEntry} representing a result of Lucene auto-complete suggestion.
* The partial record contains the suggestion in the field where it is indexed from, and the grouping keys if there are any.
*/
@VisibleForTesting
public static IndexKeyValueToPartialRecord getToPartialRecord(@Nonnull Index index, @Nonnull RecordType recordType, @Nonnull IndexScanType scanType) {
final IndexKeyValueToPartialRecord.Builder builder = IndexKeyValueToPartialRecord.newBuilder(recordType);
KeyExpression root = index.getRootExpression();
if (root instanceof GroupingKeyExpression) {
KeyExpression groupingKey = ((GroupingKeyExpression) root).getGroupingSubKey();
for (int i = 0; i < groupingKey.getColumnSize(); i++) {
AvailableFields.addCoveringField(groupingKey, AvailableFields.FieldData.of(IndexKeyValueToPartialRecord.TupleSource.KEY, i), builder);
}
}
builder.addRequiredMessageFields();
if (!builder.isValid(true)) {
throw new RecordCoreException("Missing required field for result record").addLogInfo(LogMessageKeys.INDEX_NAME, index.getName()).addLogInfo(LogMessageKeys.RECORD_TYPE, recordType.getName()).addLogInfo(LogMessageKeys.SCAN_TYPE, scanType);
}
builder.addRegularCopier(new LuceneIndexKeyValueToPartialRecordUtils.LuceneAutoCompleteCopier());
return builder.build();
}
use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.
the class LuceneIndexKeyValueToPartialRecordUtils method buildPartialRecord.
public static void buildPartialRecord(@Nonnull KeyExpression root, @Nonnull Descriptors.Descriptor descriptor, @Nonnull Message.Builder builder, @Nonnull String luceneField, @Nonnull String suggestion, @Nonnull Tuple groupingKey) {
final KeyExpression expression = root instanceof GroupingKeyExpression ? ((GroupingKeyExpression) root).getWholeKey() : root;
LuceneIndexExpressions.getFieldsRecursively(expression, new PartialRecordBuildSource(null, descriptor, builder), (source, fieldName, value, type, stored, overriddenKeyRanges, groupingKeyIndex) -> {
if (groupingKeyIndex > -1) {
if (groupingKeyIndex > groupingKey.size() - 1) {
throw new RecordCoreException("Invalid grouping value tuple given a grouping key").addLogInfo(LogMessageKeys.VALUE, groupingKey.toString());
}
source.buildMessage(groupingKey.get(groupingKeyIndex), (String) value, null, null, false);
} else if (type.equals(LuceneIndexExpressions.DocumentFieldType.TEXT)) {
buildIfFieldNameMatch(source, fieldName, luceneField, overriddenKeyRanges, suggestion, (String) value);
}
}, null, 0, root instanceof GroupingKeyExpression ? ((GroupingKeyExpression) root).getGroupingCount() : 0, new ArrayList<>());
}
Aggregations