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++)));
}
});
}
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);
}
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);
}
}
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;
}
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;
}
Aggregations