use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class MapValueUpdate method applyTo.
@Override
public FieldValue applyTo(FieldValue fval) {
if (fval instanceof Array) {
Array array = (Array) fval;
FieldValue element = array.getFieldValue(((IntegerFieldValue) value).getInteger());
element = update.applyTo(element);
array.set(((IntegerFieldValue) value).getInteger(), element);
} else if (fval instanceof WeightedSet) {
WeightedSet wset = (WeightedSet) fval;
WeightedSetDataType wtype = wset.getDataType();
Integer weight = wset.get(value);
if (weight == null) {
if (wtype.createIfNonExistent() && update instanceof ArithmeticValueUpdate) {
weight = 0;
} else {
return fval;
}
}
weight = (Integer) update.applyTo(new IntegerFieldValue(weight)).getWrappedValue();
wset.put(value, weight);
if (wtype.removeIfZero() && update instanceof ArithmeticValueUpdate && weight == 0) {
wset.remove(value);
}
}
return fval;
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class VespaDocumentDeserializer42 method read.
public void read(Annotation annotation) {
int annotationTypeId = buf.getInt();
AnnotationType type = manager.getAnnotationTypeRegistry().getType(annotationTypeId);
if (type == null) {
throw new DeserializationException("Cannot deserialize annotation of type " + annotationTypeId + " (unknown type)");
}
annotation.setType(type);
byte features = buf.get();
int length = buf.getInt1_2_4Bytes();
if ((features & (byte) 1) == (byte) 1) {
// we have a span node
int spanNodeId = buf.getInt1_2_4Bytes();
try {
SpanNode node = spanNodes.get(spanNodeId);
annotation.setSpanNode(node);
} catch (IndexOutOfBoundsException ioobe) {
throw new DeserializationException("Could not deserialize annotation, associated span node not found ", ioobe);
}
}
if ((features & (byte) 2) == (byte) 2) {
// we have a value:
int dataTypeId = buf.getInt();
// if this data type ID the same as the one in our config?
if (dataTypeId != type.getDataType().getId()) {
// not the same, but we will handle it gracefully, and just skip past the data:
buf.position(buf.position() + length - 4);
} else {
FieldValue value = type.getDataType().createFieldValue();
value.deserialize(this);
annotation.setFieldValue(value);
}
}
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class VespaXMLUpdateReader method readAssign.
FieldUpdate readAssign(DocumentUpdate update) throws XMLStreamException {
for (int i = 0; i < reader.getAttributeCount(); i++) {
if ("field".equals(reader.getAttributeName(i).toString())) {
Field f = update.getDocumentType().getField(reader.getAttributeValue(i));
if (f == null) {
throw newDeserializeException("Field " + reader.getAttributeValue(i) + " not found.");
}
FieldValue value = f.getDataType().createFieldValue();
value.deserialize(f, this);
return FieldUpdate.createAssign(f, value);
}
}
throw newDeserializeException("Assignment update without field attribute");
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class VespaXMLUpdateReader method readRemove.
FieldUpdate readRemove(DocumentUpdate update) throws XMLStreamException {
for (int i = 0; i < reader.getAttributeCount(); i++) {
if ("field".equals(reader.getAttributeName(i).toString())) {
Field f = update.getDocumentType().getField(reader.getAttributeValue(i));
FieldValue value = f.getDataType().createFieldValue();
value.deserialize(f, this);
if (value instanceof Array) {
List<FieldValue> l = ((Array) value).getValues();
return FieldUpdate.createRemoveAll(f, l);
} else if (value instanceof WeightedSet) {
return FieldUpdate.createRemoveAll(f, ((WeightedSet) value));
} else {
throw newDeserializeException("Remove operation only applicable to multivalue lists");
}
}
}
throw newDeserializeException("Remove update without field attribute");
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class VespaXMLUpdateReader method readArithmetic.
FieldUpdate readArithmetic(DocumentUpdate update, String type, Field f, FieldUpdate fu) throws XMLStreamException {
Double by = null;
for (int i = 0; i < reader.getAttributeCount(); i++) {
if ("by".equals(reader.getAttributeName(i).toString())) {
by = Double.parseDouble(reader.getAttributeValue(i));
}
}
if (by == null) {
throw newDeserializeException("Assignment update without \"by\" attribute");
}
FieldValue key = null;
do {
reader.next();
if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
if ("key".equals(reader.getName().toString())) {
if (f.getDataType() instanceof WeightedSetDataType) {
DataType nestedType = ((WeightedSetDataType) f.getDataType()).getNestedType();
key = nestedType.createFieldValue();
key.deserialize(this);
} else if (f.getDataType() instanceof MapDataType) {
key = ((MapDataType) f.getDataType()).getKeyType().createFieldValue();
key.deserialize(this);
} else if (f.getDataType() instanceof ArrayDataType) {
key = new IntegerFieldValue(Integer.parseInt(reader.getElementText()));
} else {
throw newDeserializeException("Key tag only applicable for weighted sets and maps");
}
skipToEnd("key");
} else {
throw newDeserializeException("\"" + reader.getName() + "\" not appropriate within " + type + " element.");
}
}
} while (reader.getEventType() != XMLStreamReader.END_ELEMENT);
if (key != null) {
if ("increment".equals(type)) {
fu.addValueUpdate(ValueUpdate.createIncrement(key, by));
}
if ("decrement".equals(type)) {
fu.addValueUpdate(ValueUpdate.createDecrement(key, by));
}
if ("multiply".equals(type)) {
fu.addValueUpdate(ValueUpdate.createMultiply(key, by));
}
if ("divide".equals(type)) {
fu.addValueUpdate(ValueUpdate.createDivide(key, by));
}
} else {
if ("increment".equals(type)) {
fu.addValueUpdate(ValueUpdate.createIncrement(by));
}
if ("decrement".equals(type)) {
fu.addValueUpdate(ValueUpdate.createDecrement(by));
}
if ("multiply".equals(type)) {
fu.addValueUpdate(ValueUpdate.createMultiply(by));
}
if ("divide".equals(type)) {
fu.addValueUpdate(ValueUpdate.createDivide(by));
}
}
return fu;
}
Aggregations