use of com.yahoo.document.datatypes.CollectionFieldValue in project vespa by vespa-engine.
the class AddValueUpdate method applyTo.
@Override
public FieldValue applyTo(FieldValue val) {
if (val instanceof WeightedSet) {
WeightedSet wset = (WeightedSet) val;
wset.put(value, weight);
} else if (val instanceof CollectionFieldValue) {
CollectionFieldValue fval = (CollectionFieldValue) val;
fval.add(value);
} else {
throw new IllegalStateException("Cannot add " + value + " to field of type " + val.getClass().getName());
}
return val;
}
use of com.yahoo.document.datatypes.CollectionFieldValue in project vespa by vespa-engine.
the class RemoveValueUpdate method applyTo.
@Override
public FieldValue applyTo(FieldValue fval) {
if (fval instanceof CollectionFieldValue) {
CollectionFieldValue val = (CollectionFieldValue) fval;
val.removeValue(value);
}
return fval;
}
use of com.yahoo.document.datatypes.CollectionFieldValue 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.datatypes.CollectionFieldValue in project vespa by vespa-engine.
the class MapReader method createMapUpdate.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ValueUpdate createMapUpdate(TokenBuffer buffer, DataType currentLevel, FieldValue keyParent, FieldValue topLevelKey) {
TokenBuffer.Token element = buffer.prefetchScalar(UPDATE_ELEMENT);
if (UPDATE_ELEMENT.equals(buffer.currentName())) {
buffer.next();
}
FieldValue key = keyTypeForMapUpdate(element, currentLevel);
if (keyParent != null) {
((CollectionFieldValue) keyParent).add(key);
}
// match will always have element, and either match or action
if (!UPDATE_MATCH.equals(buffer.currentName())) {
// we have reached an action...
if (topLevelKey == null) {
return ValueUpdate.createMap(key, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName()));
} else {
return ValueUpdate.createMap(topLevelKey, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName()));
}
} else {
// next level of matching
if (topLevelKey == null) {
return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, key);
} else {
return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, topLevelKey);
}
}
}
use of com.yahoo.document.datatypes.CollectionFieldValue 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());
}
Aggregations