use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexScanPreferenceTest method noIndexes.
@Test
public void noIndexes() throws Exception {
try (FDBRecordContext context = openContext()) {
openAnyRecordStore(TestNoIndexesProto.getDescriptor(), context);
}
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").build();
for (QueryPlanner.IndexScanPreference indexScanPreference : QueryPlanner.IndexScanPreference.values()) {
planner.setIndexScanPreference(indexScanPreference);
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, scan(unbounded()));
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method queryComplexDocumentsWithAdditionalFilters.
@Test
public void queryComplexDocumentsWithAdditionalFilters() throws Exception {
final List<String> textSamples = Arrays.asList(TextSamples.ANGSTROM, TextSamples.ROMEO_AND_JULIET_PROLOGUE, TextSamples.AETHELRED, TextSamples.FRENCH, TextSamples.GERMAN, TextSamples.ROMEO_AND_JULIET_PROLOGUE, TextSamples.YIDDISH, "Napoleon and the Duke of Wellington met in Waterloo in 1815.");
final List<ComplexDocument> documents = IntStream.range(0, textSamples.size()).mapToObj(i -> ComplexDocument.newBuilder().setDocId(i).setGroup(i % 2).setText(textSamples.get(i)).addTag("3:" + (i % 3)).setScore(i).build()).collect(Collectors.toList());
final Index rankIndex = new Index("Complex$rank(score)", field("score").groupBy(field("group")), IndexTypes.RANK);
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
final RecordTypeBuilder complexDocRecordType = metaDataBuilder.getRecordType(COMPLEX_DOC);
metaDataBuilder.addIndex(complexDocRecordType, COMPLEX_TEXT_BY_GROUP);
metaDataBuilder.addIndex(complexDocRecordType, rankIndex);
});
documents.forEach(recordStore::saveRecord);
assertEquals(Collections.singletonList(Tuple.from(1L, 5L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAll("fearful passage love", 7), Query.field("tag").oneOfThem().equalsValue("3:2"), 1, 758136568));
assertEquals(Arrays.asList(Tuple.from(1L, 1L), Tuple.from(1L, 5L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAll("fearful passage love", 7), Query.field("text").text().containsPrefix("continu"), 1, -1043653062));
assertEquals(Collections.singletonList(Tuple.from(1L, 7L)), queryComplexDocumentsWithIndex(Query.field("text").text().contains("napoleon"), Query.and(Query.field("text").text().containsPrefix("th"), Query.field("text").text().contains("waterloo")), 1, -754900112));
assertEquals(Collections.singletonList(Tuple.from(1L, 1L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAll("fearful passage love", 7), Query.not(Query.field("tag").oneOfThem().equalsValue("3:2")), 1, 758136569));
assertEquals(Collections.singletonList(Tuple.from(1L, 1L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAll("fearful passage love", 7), Query.not(Query.field("tag").oneOfThem().equalsValue("3:2")), 1, 758136569));
assertEquals(Arrays.asList(Tuple.from(0L, 0L), Tuple.from(0L, 6L)), queryComplexDocumentsWithOr((OrComponent) Query.or(Query.field("text").text().containsAll("unit named after"), Query.field("text").text().containsPhrase("אן ארמיי און פלאט")), 0, -1558384887));
assertEquals(Collections.singletonList(Tuple.from(1L, 5L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAll("fearful passage love", 7), Query.or(Query.field("tag").oneOfThem().equalsValue("3:2"), Query.field("tag").oneOfThem().equalsValue("3:0")), true, 1, -27568755));
assertEquals(Collections.singletonList(Tuple.from(1L, 1L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAll("fearful passage love", 7), Query.rank(field("score").groupBy(field("group"))).lessThan(2L), true, 1, -2132208833));
assertEquals(Collections.singletonList(Tuple.from(1L, 5L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAllPrefixes("fear pass love", true), Query.field("tag").oneOfThem().equalsValue("3:2"), true, 1, -419325379));
assertEquals(Collections.singletonList(Tuple.from(1L, 5L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAllPrefixes("fear pass love", false), Query.field("tag").oneOfThem().equalsValue("3:2"), false, 1, -1902024530));
assertEquals(Collections.singletonList(Tuple.from(1L, 1L)), queryComplexDocumentsWithIndex(Query.field("text").text().containsAllPrefixes("fear pass love"), Query.rank(field("score").groupBy(field("group"))).lessThan(2L), true, 1, 669157421));
commit(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method performQueryWithRecordStoreScan.
@Nonnull
private Set<Long> performQueryWithRecordStoreScan(@Nonnull RecordMetaDataHook hook, @Nonnull QueryComponent filter) throws Exception {
final ScanProperties scanProperties = new ScanProperties(ExecuteProperties.newBuilder().setTimeLimit(3000).build());
Set<Long> results = new HashSet<>();
byte[] continuation = null;
do {
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
try (RecordCursor<Long> cursor = recordStore.scanRecords(continuation, scanProperties).filter(record -> record.getRecordType().getName().equals(SIMPLE_DOC)).filter(record -> filter.eval(recordStore, EvaluationContext.EMPTY, record) == Boolean.TRUE).map(record -> record.getPrimaryKey().getLong(0))) {
cursor.forEach(results::add).get();
RecordCursorResult<Long> noNextResult = cursor.getNext();
continuation = noNextResult.getContinuation().toBytes();
}
}
} while (continuation != null);
return results;
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method textIndexPerf1000ParallelInsert.
@Tag(Tags.Performance)
@Test
public void textIndexPerf1000ParallelInsert() throws Exception {
// Create 1000 records
Random r = new Random();
List<SimpleDocument> records = getRandomRecords(r, 1000);
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
recordStore.asBuilder().create();
commit(context);
}
final FDBRecordStore.Builder storeBuilder = recordStore.asBuilder();
long startTime = System.nanoTime();
int oldMaxAttempts = FDBDatabaseFactory.instance().getMaxAttempts();
FDBDatabaseFactory.instance().setMaxAttempts(Integer.MAX_VALUE);
try {
CompletableFuture<?>[] workerFutures = new CompletableFuture<?>[10];
int recordsPerWorker = records.size() / workerFutures.length;
for (int i = 0; i < workerFutures.length; i++) {
List<SimpleDocument> workerDocs = records.subList(i * recordsPerWorker, (i + 1) * recordsPerWorker);
CompletableFuture<Void> workerFuture = new CompletableFuture<>();
Thread workerThread = new Thread(() -> {
try {
for (int j = 0; j < workerDocs.size(); j += 10) {
// Use retry loop to catch not_committed errors
List<SimpleDocument> batchDocuments = workerDocs.subList(j, j + 10);
fdb.run(context -> {
try {
FDBRecordStore store = storeBuilder.copyBuilder().setContext(context).open();
for (SimpleDocument document : batchDocuments) {
store.saveRecord(document);
}
return null;
} catch (RecordCoreException e) {
throw e;
} catch (Exception e) {
throw new RecordCoreException(e);
}
});
}
workerFuture.complete(null);
} catch (RuntimeException e) {
workerFuture.completeExceptionally(e);
}
});
workerThread.setName("insert-worker-" + i);
workerThread.start();
workerFutures[i] = workerFuture;
}
CompletableFuture.allOf(workerFutures).get();
long endTime = System.nanoTime();
LOGGER.info("performed 1000 parallel insertions in {} seconds.", (endTime - startTime) * 1e-9);
printUsage();
} finally {
FDBDatabaseFactory.instance().setMaxAttempts(oldMaxAttempts);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method saveComplexMultiDocuments.
@Test
public void saveComplexMultiDocuments() throws Exception {
final ComplexDocument shakespeareDocument = ComplexDocument.newBuilder().setGroup(0).setDocId(1623L).setText(TextSamples.ROMEO_AND_JULIET_PROLOGUE).addTag("a").addTag("b").build();
// First, repeated key comes *before* text.
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> metaDataBuilder.addIndex(COMPLEX_DOC, COMPLEX_MULTI_TAG_INDEX));
recordStore.saveRecord(shakespeareDocument);
int indexKeys = getSaveIndexKeyCount(recordStore);
assertEquals(164, indexKeys);
List<Map.Entry<Tuple, List<Integer>>> entryList = scanMapEntries(recordStore, COMPLEX_MULTI_TAG_INDEX, Tuple.from("a", "civil"));
assertEquals(Collections.singletonList(entryOf(Tuple.from(0L, 1623L), Arrays.asList(22, 25))), entryList);
commit(context);
}
try (FDBRecordContext context = openContext()) {
FDBRecordStore.deleteStore(context, recordStore.getSubspace());
commit(context);
}
// Then, repeated key comes *after* text.
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> metaDataBuilder.addIndex(COMPLEX_DOC, COMPLEX_THEN_TAG_INDEX));
recordStore.saveRecord(shakespeareDocument);
int indexKeys = getSaveIndexKeyCount(recordStore);
assertEquals(164, indexKeys);
List<Map.Entry<Tuple, List<Integer>>> entryList = scanMapEntries(recordStore, COMPLEX_THEN_TAG_INDEX, Tuple.from("civil"));
assertEquals(Arrays.asList(entryOf(Tuple.from("a", 0L, 1623L), Arrays.asList(22, 25)), entryOf(Tuple.from("b", 0L, 1623L), Arrays.asList(22, 25))), entryList);
commit(context);
}
}
Aggregations