use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class FieldValueConverter method convertArray.
@SuppressWarnings({ "unchecked", "rawtypes" })
private FieldValue convertArray(Array val) {
List<FieldValue> next = new LinkedList<FieldValue>();
DataType nextType = null;
for (Iterator<FieldValue> it = val.fieldValueIterator(); it.hasNext(); ) {
FieldValue prevVal = it.next();
FieldValue nextVal = convert(prevVal);
if (nextVal == null) {
continue;
}
if (nextType == null) {
nextType = nextVal.getDataType();
} else if (!nextType.isValueCompatible(nextVal)) {
throw new IllegalArgumentException("Expected " + nextType.getName() + ", got " + nextVal.getDataType().getName() + ".");
}
next.add(nextVal);
}
if (nextType == null) {
return null;
}
Array ret = DataType.getArray(nextType).createFieldValue();
for (FieldValue nextVal : next) {
ret.add(nextVal);
}
return ret;
}
use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class SimpleDocumentAdapter method tryOutputType.
@Override
public void tryOutputType(Expression exp, String fieldName, DataType valueType) {
Field field = output.getDataType().getField(fieldName);
if (field == null) {
throw new VerificationException(exp, "Field '" + fieldName + "' not found.");
}
DataType fieldType = field.getDataType();
if (!fieldType.isAssignableFrom(valueType)) {
throw new VerificationException(exp, "Can not assign " + valueType.getName() + " to field '" + fieldName + "' which is " + fieldType.getName() + ".");
}
}
use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class AddRemoveCreator method createAddsOrRemoves.
// yes, this suppresswarnings ugliness is by intention, the code relies on
// the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes", "unchecked" })
private static void createAddsOrRemoves(TokenBuffer buffer, Field field, FieldUpdate update, boolean isRemove) {
FieldValue container = field.getDataType().createFieldValue();
FieldUpdate singleUpdate;
int initNesting = buffer.nesting();
Preconditions.checkState(buffer.currentToken().isStructStart(), "Expected start of composite, got %s", buffer.currentToken());
if (container instanceof CollectionFieldValue) {
buffer.next();
DataType valueType = ((CollectionFieldValue) container).getDataType().getNestedType();
if (container instanceof WeightedSet) {
// these are objects with string keys (which are the nested
// types) and values which are the weight
WeightedSet weightedSet = (WeightedSet) container;
fillWeightedSetUpdate(buffer, initNesting, valueType, weightedSet);
if (isRemove) {
singleUpdate = FieldUpdate.createRemoveAll(field, weightedSet);
} else {
singleUpdate = FieldUpdate.createAddAll(field, weightedSet);
}
} else {
List<FieldValue> arrayContents = new ArrayList<>();
ArrayReader.fillArrayUpdate(buffer, initNesting, valueType, arrayContents);
if (buffer.currentToken() != JsonToken.END_ARRAY) {
throw new IllegalStateException("Expected END_ARRAY. Got '" + buffer.currentToken() + "'.");
}
if (isRemove) {
singleUpdate = FieldUpdate.createRemoveAll(field, arrayContents);
} else {
singleUpdate = FieldUpdate.createAddAll(field, arrayContents);
}
}
} else {
throw new UnsupportedOperationException("Trying to add or remove from a field of a type the reader does not know how to handle: " + container.getClass().getName());
}
expectCompositeEnd(buffer.currentToken());
update.addAll(singleUpdate);
}
use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class CompositeReader method populateComposite.
// TODO createComposite is extremely similar to add/remove, refactor
// yes, this suppresswarnings ugliness is by intention, the code relies on the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes" })
public static void populateComposite(TokenBuffer buffer, FieldValue fieldValue) {
JsonToken token = buffer.currentToken();
if ((token != JsonToken.START_OBJECT) && (token != JsonToken.START_ARRAY)) {
throw new IllegalArgumentException("Expected '[' or '{'. Got '" + token + "'.");
}
if (fieldValue instanceof CollectionFieldValue) {
DataType valueType = ((CollectionFieldValue) fieldValue).getDataType().getNestedType();
if (fieldValue instanceof WeightedSet) {
fillWeightedSet(buffer, valueType, (WeightedSet) fieldValue);
} else {
fillArray(buffer, (CollectionFieldValue) fieldValue, valueType);
}
} else if (fieldValue instanceof MapFieldValue) {
MapReader.fillMap(buffer, (MapFieldValue) fieldValue);
} else if (fieldValue instanceof StructuredFieldValue) {
StructReader.fillStruct(buffer, (StructuredFieldValue) fieldValue);
} else if (fieldValue instanceof TensorFieldValue) {
TensorReader.fillTensor(buffer, (TensorFieldValue) fieldValue);
} else {
throw new IllegalStateException("Has created a composite field" + " value the reader does not know how to handle: " + fieldValue.getClass().getName() + " This is a bug. token = " + token);
}
expectCompositeEnd(buffer.currentToken());
}
use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class VespaDocumentDeserializer42 method read.
public void read(AssignFieldPathUpdate update) {
byte flags = getByte(null);
update.setRemoveIfZero((flags & AssignFieldPathUpdate.REMOVE_IF_ZERO) != 0);
update.setCreateMissingPath((flags & AssignFieldPathUpdate.CREATE_MISSING_PATH) != 0);
if ((flags & AssignFieldPathUpdate.ARITHMETIC_EXPRESSION) != 0) {
update.setExpression(getString(null));
} else {
DataType dt = update.getFieldPath().getResultingDataType();
FieldValue fv = dt.createFieldValue();
fv.deserialize(this);
update.setNewValue(fv);
}
}
Aggregations