Search in sources :

Example 91 with RecordCoreException

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

the class LuceneAutoCompleteResultCursor method onNext.

@SuppressWarnings("cast")
@Nonnull
@Override
public CompletableFuture<RecordCursorResult<IndexEntry>> onNext() {
    CompletableFuture<Lookup.LookupResult> lookupResult = CompletableFuture.supplyAsync(() -> {
        if (lookupResults == null) {
            try {
                performLookup();
            } catch (IOException ioException) {
                throw new RecordCoreException("Exception to lookup the auto complete suggestions", ioException).addLogInfo(LogMessageKeys.QUERY, query);
            }
        }
        return currentPosition < lookupResults.size() ? lookupResults.get(currentPosition) : null;
    }, executor);
    return lookupResult.thenApply(r -> {
        if (r == null) {
            return RecordCursorResult.exhausted();
        } else {
            final String suggestion = highlight ? (String) r.highlightKey : (String) r.key;
            if (r.payload == null) {
                throw new RecordCoreException("Empty payload of lookup result for lucene auto complete suggestion").addLogInfo(LogMessageKeys.QUERY, query).addLogInfo(LogMessageKeys.RESULT, suggestion);
            }
            IndexEntry indexEntry = new IndexEntry(state.index, Tuple.fromBytes(r.payload.bytes).add(suggestion), Tuple.from(r.value));
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Suggestion read as an index entry={}", indexEntry);
            }
            return RecordCursorResult.withNextValue(indexEntry, continuationHelper(lookupResults.get(currentPosition++)));
        }
    });
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) IndexEntry(com.apple.foundationdb.record.IndexEntry) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) Nonnull(javax.annotation.Nonnull)

Example 92 with RecordCoreException

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

the class LuceneDocumentFromRecord method getGroupedFields.

// Grouping keys are evaluated more or less normally, turning into multiple groups.
// Each group corresponds to a single document in a separate index / directory.
// Within that document, the grouped fields are merged.
protected static <M extends Message> void getGroupedFields(@Nonnull List<KeyExpression> keys, int keyIndex, int keyPosition, int groupingCount, @Nonnull Tuple groupPrefix, @Nonnull FDBRecord<M> rec, @Nonnull Message message, @Nonnull Map<Tuple, List<DocumentField>> result, @Nullable String fieldNamePrefix) {
    if (keyIndex >= keys.size()) {
        return;
    }
    KeyExpression key = keys.get(keyIndex);
    int keySize = key.getColumnSize();
    if (keyPosition + keySize <= groupingCount) {
        // Entirely in the grouping portion: extend group prefix with normal evaluation.
        List<Key.Evaluated> groups = key.evaluateMessage(rec, message);
        for (Key.Evaluated group : groups) {
            Tuple wholeGroup = groupPrefix.addAll(group.toTupleAppropriateList());
            if (groupingCount == wholeGroup.size()) {
                result.putIfAbsent(wholeGroup, new ArrayList<>());
            }
            getGroupedFields(keys, keyIndex + 1, keyPosition + key.getColumnSize(), groupingCount, wholeGroup, rec, message, result, fieldNamePrefix);
        }
        return;
    }
    if (groupingCount <= keyPosition) {
        // Entirely in the grouped portion: add fields to groups.
        List<DocumentField> fields = getFields(key, rec, message, fieldNamePrefix);
        for (Map.Entry<Tuple, List<DocumentField>> entry : result.entrySet()) {
            if (TupleHelpers.isPrefix(groupPrefix, entry.getKey())) {
                entry.getValue().addAll(fields);
            }
        }
    // Grouping ends in the middle of this key: break it apart.
    } else if (key instanceof NestingKeyExpression) {
        NestingKeyExpression nesting = (NestingKeyExpression) key;
        final String parentFieldName = nesting.getParent().getFieldName();
        for (Key.Evaluated value : nesting.getParent().evaluateMessage(rec, message)) {
            final Message submessage = (Message) value.toList().get(0);
            getGroupedFields(Collections.singletonList(nesting.getChild()), 0, keyPosition, groupingCount, groupPrefix, rec, submessage, result, fieldNamePrefix == null ? parentFieldName : fieldNamePrefix + "_" + parentFieldName);
        }
    } else if (key instanceof ThenKeyExpression) {
        ThenKeyExpression then = (ThenKeyExpression) key;
        getGroupedFields(then.getChildren(), 0, keyPosition, groupingCount, groupPrefix, rec, message, result, fieldNamePrefix);
    } else {
        throw new RecordCoreException("Cannot split key for document grouping: " + key);
    }
    // Continue with remaining keys.
    getGroupedFields(keys, keyIndex + 1, keyPosition + key.getColumnSize(), groupingCount, groupPrefix, rec, message, result, fieldNamePrefix);
}
Also used : ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) Message(com.google.protobuf.Message) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.apple.foundationdb.record.metadata.Key) Tuple(com.apple.foundationdb.tuple.Tuple)

Example 93 with RecordCoreException

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

the class LuceneSerializer method decompressIfNeeded.

@Nonnull
private static byte[] decompressIfNeeded(boolean decompressionNeeded, @Nonnull byte[] data, int offset) {
    if (!decompressionNeeded) {
        // Return the array to be the final output for decoding, so the offset bytes need to be removed
        return Arrays.copyOfRange(data, offset, data.length);
    }
    if (data.length < 2 + offset) {
        throw new RecordCoreException("Invalid data for decompression").addLogInfo(LogMessageKeys.DIR_VALUE, data);
    }
    byte version = data[offset];
    if (version != COMPRESSION_VERSION_FOR_HIGH_COMPRESSION) {
        throw new RecordCoreException("Un-supported compression version").addLogInfo(LogMessageKeys.COMPRESSION_VERSION, version);
    }
    try {
        BytesRef ref = new BytesRef();
        ByteArrayDataInput input = new ByteArrayDataInput(data, offset + 1, data.length - offset - 1);
        int originalLength = input.readVInt();
        Decompressor decompressor = CompressionMode.HIGH_COMPRESSION.newDecompressor();
        decompressor.decompress(input, originalLength, 0, originalLength, ref);
        return Arrays.copyOfRange(ref.bytes, ref.offset, ref.length);
    } catch (Exception e) {
        throw new RecordCoreException("Lucene data decompression failure", e);
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Decompressor(org.apache.lucene.codecs.compressing.Decompressor) ByteArrayDataInput(org.apache.lucene.store.ByteArrayDataInput) BytesRef(org.apache.lucene.util.BytesRef) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Nonnull(javax.annotation.Nonnull)

Example 94 with RecordCoreException

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

the class LuceneSerializer method decode.

@Nullable
public static byte[] decode(@Nullable byte[] data) {
    if (data == null) {
        return null;
    }
    if (data.length < 2) {
        throw new RecordCoreException("Invalid data").addLogInfo(LogMessageKeys.DATA_VALUE, data);
    }
    final byte encoding = data[0];
    final boolean encrypted = (encoding & ENCODING_ENCRYPTED) == ENCODING_ENCRYPTED;
    final boolean compressed = (encoding & ENCODING_COMPRESSED) == ENCODING_COMPRESSED;
    byte[] decoded = decompressIfNeeded(compressed, data, 1);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(KeyValueLogMessage.of("Decoded lucene data", LogMessageKeys.COMPRESSED_EVENTUALLY, compressed, LogMessageKeys.ENCRYPTED_EVENTUALLY, encrypted, LogMessageKeys.ENCODED_DATA_SIZE, data.length, LogMessageKeys.ORIGINAL_DATA_SIZE, decoded.length));
    }
    return decoded;
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Nullable(javax.annotation.Nullable)

Example 95 with RecordCoreException

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

the class GeophileSpatialFunctionKeyExpression method evaluateFunction.

@Nonnull
@Override
public <M extends Message> List<Key.Evaluated> evaluateFunction(@Nullable FDBRecord<M> record, @Nullable Message message, @Nonnull Key.Evaluated arguments) {
    SpatialObject spatialObject;
    try {
        spatialObject = parseSpatialObject(arguments);
    } catch (ParseException ex) {
        throw new RecordCoreException(ex);
    }
    if (spatialObject == null) {
        return Collections.singletonList(Key.Evaluated.NULL);
    }
    long[] zs = new long[spatialObject.maxZ()];
    GeophileSpatial.shuffle(space, spatialObject, zs);
    List<Key.Evaluated> result = new ArrayList<>(zs.length);
    for (long z : zs) {
        if (z == Space.Z_NULL) {
            break;
        }
        result.add(Key.Evaluated.scalar(z));
    }
    return result;
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) ArrayList(java.util.ArrayList) ParseException(org.locationtech.jts.io.ParseException) SpatialObject(com.geophile.z.SpatialObject) 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