Search in sources :

Example 46 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class FDBDirectory method listAllInternal.

private List<String> listAllInternal() {
    // A private form of the method to allow easier instrumentation
    List<String> outList = new ArrayList<>();
    List<String> displayList = null;
    long totalSize = 0L;
    long actualTotalSize = 0L;
    for (KeyValue kv : context.ensureActive().getRange(metaSubspace.range())) {
        String name = metaSubspace.unpack(kv.getKey()).getString(0);
        outList.add(name);
        final FDBLuceneFileReference fileReference = FDBLuceneFileReference.parseFromBytes(LuceneSerializer.decode(kv.getValue()));
        // Only composite files are prefetched.
        if (name.endsWith(".cfs")) {
            try {
                readBlock(name, CompletableFuture.completedFuture(fileReference), 0);
            } catch (RecordCoreException e) {
                LOGGER.warn(getLogMessage("Exception thrown during prefetch", LogMessageKeys.FILE_NAME, name));
            }
        }
        this.fileReferenceCache.put(name, fileReference);
        if (LOGGER.isDebugEnabled()) {
            if (displayList == null) {
                displayList = new ArrayList<>();
            }
            if (kv.getValue() != null) {
                displayList.add(name + "(" + fileReference.getSize() + ")");
                totalSize += fileReference.getSize();
                actualTotalSize += fileReference.getActualSize();
            }
        }
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(getLogMessage("listAllFiles", LogMessageKeys.FILE_COUNT, outList.size(), LogMessageKeys.FILE_LIST, displayList, LogMessageKeys.FILE_TOTAL_SIZE, totalSize, LogMessageKeys.FILE_ACTUAL_TOTAL_SIZE, actualTotalSize));
    }
    return outList;
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) KeyValue(com.apple.foundationdb.KeyValue) ArrayList(java.util.ArrayList)

Example 47 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class LuceneSerializer method compressIfNeeded.

@Nonnull
private static byte[] compressIfNeeded(boolean compressionNeeded, @Nonnull byte[] data, @Nonnull ByteBuffersDataOutput encodedDataOutput, @Nonnull EncodingState state, int offset) {
    if (!compressionNeeded) {
        return fallBackToUncompressed(encodedDataOutput.toArrayCopy(), data, state, offset);
    }
    try (Compressor compressor = CompressionMode.HIGH_COMPRESSION.newCompressor()) {
        encodedDataOutput.writeByte(COMPRESSION_VERSION_FOR_HIGH_COMPRESSION);
        encodedDataOutput.writeVInt(data.length);
        compressor.compress(data, 0, data.length, encodedDataOutput);
        final byte[] compressedData = encodedDataOutput.toArrayCopy();
        // Compress only if it helps to shorten the bytes
        if (compressedData.length < data.length) {
            state.setCompressed(true);
            return compressedData;
        } else {
            return fallBackToUncompressed(compressedData, data, state, offset);
        }
    } catch (Exception e) {
        throw new RecordCoreException("Lucene data compression failure", e);
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Compressor(org.apache.lucene.codecs.compressing.Compressor) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Nonnull(javax.annotation.Nonnull)

Example 48 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class SynonymMapRegistryImpl method buildSynonymMap.

private static SynonymMap buildSynonymMap(final SynonymMapConfig config) {
    try {
        SynonymMap.Parser parser = new SolrSynonymParser(true, true, new Analyzer() {

            @Override
            protected TokenStreamComponents createComponents(String fieldName) {
                final StandardTokenizer src = new StandardTokenizer();
                TokenStream tok = new LowerCaseFilter(src);
                tok = new FlattenGraphFilter(tok);
                return new TokenStreamComponents(src, tok);
            }
        });
        parser.parse(new InputStreamReader(config.getSynonymInputStream(), StandardCharsets.UTF_8));
        return parser.build();
    } catch (IOException | ParseException ex) {
        throw new RecordCoreException("Failed to build synonym map", ex).addLogInfo(LogMessageKeys.SYNONYM_NAME, config.getName());
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Analyzer(org.apache.lucene.analysis.Analyzer) FlattenGraphFilter(org.apache.lucene.analysis.core.FlattenGraphFilter) SynonymMap(org.apache.lucene.analysis.synonym.SynonymMap) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) StandardTokenizer(org.apache.lucene.analysis.standard.StandardTokenizer) SolrSynonymParser(org.apache.lucene.analysis.synonym.SolrSynonymParser) ParseException(java.text.ParseException) LowerCaseFilter(org.apache.lucene.analysis.LowerCaseFilter)

Example 49 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreTest method unionFieldUpdateCompatibility.

@Test
public void unionFieldUpdateCompatibility() throws Exception {
    final TestRecords1Proto.MySimpleRecord record1 = TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(1066L).setNumValue2(42).build();
    final TestRecords1Proto.MySimpleRecord record2 = TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(1415L).setStrValueIndexed("second_record").build();
    final TestRecords1Proto.MySimpleRecord record3 = TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(800L).setNumValue2(14).build();
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        RecordMetaData metaData = recordStore.getRecordMetaData();
        assertSame(TestRecords1Proto.RecordTypeUnion.getDescriptor().findFieldByName("_MySimpleRecord"), metaData.getUnionFieldForRecordType(metaData.getRecordType("MySimpleRecord")));
        // Save a record using the field using the old meta-data
        recordStore.saveRecord(record1);
        // Save a record using the new union descriptor but in the old location
        context.ensureActive().set(recordStore.recordsSubspace().pack(Tuple.from(record2.getRecNo(), SplitHelper.UNSPLIT_RECORD)), TestRecordsDuplicateUnionFields.RecordTypeUnion.newBuilder().setMySimpleRecordOld(record2).build().toByteArray());
        // Save a record using the new union descriptor in the new location
        context.ensureActive().set(recordStore.recordsSubspace().pack(Tuple.from(record3.getRecNo(), SplitHelper.UNSPLIT_RECORD)), TestRecordsDuplicateUnionFields.RecordTypeUnion.newBuilder().setMySimpleRecordNew(record3).build().toByteArray());
        assertEquals(record1, recordStore.loadRecord(Tuple.from(record1.getRecNo())).getRecord());
        assertEquals(record2, recordStore.loadRecord(Tuple.from(record2.getRecNo())).getRecord());
        RecordCoreException e = assertThrows(RecordCoreException.class, () -> recordStore.loadRecord(Tuple.from(record3.getRecNo())).getRecord());
        assertNotNull(e.getCause());
        assertThat(e.getCause(), instanceOf(RecordSerializationException.class));
        assertThat(e.getCause().getMessage(), containsString("because there are unknown fields"));
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, metaDataBuilder -> metaDataBuilder.updateRecords(TestRecordsDuplicateUnionFields.getDescriptor()));
        RecordMetaData metaData = recordStore.getRecordMetaData();
        assertSame(TestRecordsDuplicateUnionFields.RecordTypeUnion.getDescriptor().findFieldByName("_MySimpleRecord_new"), metaData.getUnionFieldForRecordType(metaData.getRecordType("MySimpleRecord")));
        // All three records should be readable even though written by the previous store
        for (TestRecords1Proto.MySimpleRecord record : Arrays.asList(record1, record2, record3)) {
            FDBStoredRecord<Message> storedRecord = recordStore.loadRecord(Tuple.from(record.getRecNo()));
            assertNotNull(storedRecord);
            assertSame(metaData.getRecordType("MySimpleRecord"), storedRecord.getRecordType());
            assertEquals(record, storedRecord.getRecord());
        }
        commit(context);
    }
}
Also used : TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) RecordSerializationException(com.apple.foundationdb.record.provider.common.RecordSerializationException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 50 with RecordCoreException

use of com.apple.foundationdb.record.RecordCoreException in project fdb-record-layer by FoundationDB.

the class MetaDataEvolutionValidatorTest method mutateFile.

@Nonnull
static FileDescriptor mutateFile(@Nonnull FileDescriptor originalFile, @Nonnull Consumer<DescriptorProtos.FileDescriptorProto.Builder> fileMutation) {
    DescriptorProtos.FileDescriptorProto.Builder fileBuilder = originalFile.toProto().toBuilder();
    fileMutation.accept(fileBuilder);
    try {
        return FileDescriptor.buildFrom(fileBuilder.build(), new FileDescriptor[] { RecordMetaDataOptionsProto.getDescriptor() });
    } catch (Descriptors.DescriptorValidationException e) {
        throw new RecordCoreException("unable to build file descriptor", e);
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Descriptors(com.google.protobuf.Descriptors) Nonnull(javax.annotation.Nonnull)

Aggregations

RecordCoreException (com.apple.foundationdb.record.RecordCoreException)121 Nonnull (javax.annotation.Nonnull)58 Test (org.junit.jupiter.api.Test)42 Index (com.apple.foundationdb.record.metadata.Index)37 List (java.util.List)35 Nullable (javax.annotation.Nullable)31 Tuple (com.apple.foundationdb.tuple.Tuple)29 ArrayList (java.util.ArrayList)27 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)26 CompletableFuture (java.util.concurrent.CompletableFuture)25 Collectors (java.util.stream.Collectors)24 Collections (java.util.Collections)22 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)19 Set (java.util.Set)19 Function (java.util.function.Function)19 IndexEntry (com.apple.foundationdb.record.IndexEntry)17 TupleRange (com.apple.foundationdb.record.TupleRange)17 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)17 RecordCursor (com.apple.foundationdb.record.RecordCursor)16 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)15