use of com.apple.foundationdb.record.metadata.expressions.LiteralKeyExpression in project fdb-record-layer by FoundationDB.
the class LuceneIndexExpressions method getFieldsRecursively.
@SuppressWarnings("squid:S3776")
public static <T extends RecordSource<T>> void getFieldsRecursively(@Nonnull KeyExpression expression, @Nonnull T source, @Nonnull DocumentDestination<T> destination, @Nullable String fieldNamePrefix, int keyIndex, int groupingCount, @Nonnull List<Integer> overriddenKeyRanges) {
if (expression instanceof ThenKeyExpression) {
int count = 0;
for (KeyExpression child : ((ThenKeyExpression) expression).getChildren()) {
getFieldsRecursively(child, source, destination, fieldNamePrefix, keyIndex + count, groupingCount, overriddenKeyRanges);
count += child.getColumnSize();
}
return;
}
String fieldNameSuffix = null;
boolean suffixOverride = false;
if (expression instanceof LuceneFunctionKeyExpression.LuceneFieldName) {
LuceneFunctionKeyExpression.LuceneFieldName fieldNameExpression = (LuceneFunctionKeyExpression.LuceneFieldName) expression;
KeyExpression nameExpression = fieldNameExpression.getNameExpression();
if (nameExpression instanceof LiteralKeyExpression) {
fieldNameSuffix = (String) ((LiteralKeyExpression<?>) nameExpression).getValue();
} else if (nameExpression instanceof FieldKeyExpression) {
Iterator<Object> names = source.getValues((FieldKeyExpression) nameExpression).iterator();
if (names.hasNext()) {
fieldNameSuffix = (String) names.next();
if (names.hasNext()) {
throw new RecordCoreException("Lucene field name override should evaluate to single value");
}
}
} else {
throw new RecordCoreException("Lucene field name override should be a literal or a field");
}
suffixOverride = true;
expression = fieldNameExpression.getNamedExpression();
}
if (expression instanceof NestingKeyExpression) {
NestingKeyExpression nestingExpression = (NestingKeyExpression) expression;
FieldKeyExpression parentExpression = nestingExpression.getParent();
KeyExpression child = nestingExpression.getChild();
if (!suffixOverride) {
fieldNameSuffix = parentExpression.getFieldName();
} else {
addOverriddenKeyRange(overriddenKeyRanges, fieldNamePrefix, fieldNameSuffix);
}
String fieldName = appendFieldName(fieldNamePrefix, fieldNameSuffix);
for (T subsource : source.getChildren(parentExpression)) {
getFieldsRecursively(child, subsource, destination, fieldName, keyIndex, groupingCount, overriddenKeyRanges);
}
if (suffixOverride) {
// Remove the last 2 numbers added above
removedLastOverriddenKeyRange(overriddenKeyRanges);
}
return;
}
boolean fieldStored = false;
boolean fieldText = false;
while (true) {
if (expression instanceof LuceneFunctionKeyExpression.LuceneStored) {
LuceneFunctionKeyExpression.LuceneStored storedExpression = (LuceneFunctionKeyExpression.LuceneStored) expression;
fieldStored = true;
expression = storedExpression.getStoredExpression();
} else if (expression instanceof LuceneFunctionKeyExpression.LuceneText) {
LuceneFunctionKeyExpression.LuceneText textExpression = (LuceneFunctionKeyExpression.LuceneText) expression;
fieldText = true;
expression = textExpression.getFieldExpression();
} else {
// TODO: More text options.
break;
}
}
if (expression instanceof FieldKeyExpression) {
FieldKeyExpression fieldExpression = (FieldKeyExpression) expression;
if (!suffixOverride) {
fieldNameSuffix = fieldExpression.getFieldName();
} else {
addOverriddenKeyRange(overriddenKeyRanges, fieldNamePrefix, fieldNameSuffix);
}
String fieldName = appendFieldName(fieldNamePrefix, fieldNameSuffix);
if (fieldName == null) {
fieldName = "_";
}
Descriptors.Descriptor recordDescriptor = source.getDescriptor();
Descriptors.FieldDescriptor fieldDescriptor = recordDescriptor.findFieldByName(fieldExpression.getFieldName());
DocumentFieldType fieldType;
if (fieldText) {
switch(fieldDescriptor.getJavaType()) {
case STRING:
fieldType = DocumentFieldType.TEXT;
break;
default:
throw new RecordCoreException("Unknown Lucene text field type");
}
} else {
switch(fieldDescriptor.getJavaType()) {
case STRING:
fieldType = DocumentFieldType.STRING;
break;
case INT:
fieldType = DocumentFieldType.INT;
break;
case LONG:
fieldType = DocumentFieldType.LONG;
break;
case DOUBLE:
fieldType = DocumentFieldType.DOUBLE;
break;
case BOOLEAN:
fieldType = DocumentFieldType.BOOLEAN;
break;
default:
throw new RecordCoreException("Unknown Lucene field type");
}
}
for (Object value : source.getValues(fieldExpression)) {
destination.addField(source, fieldName, value, fieldType, fieldStored, overriddenKeyRanges, keyIndex < groupingCount ? keyIndex : -1);
}
if (suffixOverride) {
// Remove the last 2 numbers added above
removedLastOverriddenKeyRange(overriddenKeyRanges);
}
return;
}
throw new RecordCoreException("Unknown Lucene field key expression");
}
Aggregations