FieldUpdate(Field field, ValueUpdate valueUpd) { this(field); addValueUpdate(valueUpd); }
FieldUpdate(Field field, List<ValueUpdate> valueUpdates) { this(field); addValueUpdates(valueUpdates); }
/** * Creates a new, empty field update with no encapsulated value updates. Use this method to add an arbitrary * set of value updates using the FieldUpdate.addValueUpdate() method. * * @param field the Field to alter * @return a new, empty FieldUpdate * @see com.yahoo.document.update.ValueUpdate * @see FieldUpdate#addValueUpdate(ValueUpdate) */ public static FieldUpdate create(Field field) { return new FieldUpdate(field); }
@Override public DocumentUpdate getOutput() { Document doc = adapter.getUpdatableOutput(); DocumentUpdate upd = new DocumentUpdate(doc.getDataType(), doc.getId()); for (Iterator<Map.Entry<Field, FieldValue>> it = doc.iterator(); it.hasNext();) { Map.Entry<Field, FieldValue> entry = it.next(); Field field = entry.getKey(); if (field.getName().equals("sddocname")) { continue; } FieldUpdate fieldUpd = FieldUpdate.create(field); fieldUpd.addValueUpdates(builder.build(entry.getValue())); if (!fieldUpd.isEmpty()) { upd.addFieldUpdate(fieldUpd); } } return upd.isEmpty() ? null : upd; }
Map.Entry<Integer, FieldUpdate> entry = iter.next(); FieldUpdate update = entry.getValue(); if (!update.isEmpty()) { ValueUpdate last = update.getValueUpdate(update.size() - 1); if (last instanceof AssignValueUpdate) { FieldValue currentValue = doc.getFieldValue(update.getField()); if ((currentValue != null) && currentValue.equals(last.getValue())) { iter.remove(); FieldValue currentValue = doc.getFieldValue(update.getField()); if (currentValue == null) { iter.remove();
@Override public void write(FieldUpdate update) { putInt(null, update.getField().getId(Document.SERIALIZED_VERSION)); putInt(null, update.getValueUpdates().size()); for (ValueUpdate vupd : update.getValueUpdates()) { putInt(null, vupd.getValueUpdateClassID().id); vupd.serialize(this, update.getField().getDataType()); } }
public void read(FieldUpdate fieldUpdate) { int fieldId = getInt(null); Field field = fieldUpdate.getDocumentType().getField(fieldId, fieldUpdate.getSerializationVersion()); if (field == null) { throw new DeserializationException( "Cannot deserialize FieldUpdate, field fieldId " + fieldId + " not found in " + fieldUpdate.getDocumentType()); } fieldUpdate.setField(field); int size = getInt(null); for (int i = 0; i < size; i++) { if (field.getDataType() instanceof CollectionDataType) { CollectionDataType collType = (CollectionDataType) field.getDataType(); fieldUpdate.addValueUpdate(getValueUpdate(collType, collType.getNestedType())); } else { fieldUpdate.addValueUpdate(getValueUpdate(field.getDataType(), null)); } } }
private void addFieldUpdates(DocumentUpdate update, TokenBuffer buffer, String fieldName) { Field field = update.getType().getField(fieldName); int localNesting = buffer.nesting(); FieldUpdate fieldUpdate = FieldUpdate.create(field); buffer.next(); while (localNesting <= buffer.nesting()) { switch (buffer.currentName()) { case UPDATE_REMOVE: createRemoves(buffer, field, fieldUpdate); break; case UPDATE_ADD: createAdds(buffer, field, fieldUpdate); break; case UPDATE_MATCH: fieldUpdate.addValueUpdate(createMapUpdate(buffer, field)); break; case UPDATE_MODIFY: fieldUpdate.addValueUpdate(createModifyUpdate(buffer, field)); break; default: String action = buffer.currentName(); fieldUpdate.addValueUpdate(readSingleUpdate(buffer, field.getDataType(), action)); } buffer.next(); } update.addFieldUpdate(fieldUpdate); }
FieldUpdate readArithmeticField(DocumentUpdate update, String type) throws XMLStreamException { Field f = null; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("field".equals(reader.getAttributeName(i).toString())) { f = update.getDocumentType().getField(reader.getAttributeValue(i)); } } if (f == null) { throw newDeserializeException("Assignment update without \"field\" attribute"); } FieldUpdate fu = FieldUpdate.create(f); readArithmetic(update, type, f, fu); return fu; }
/** * Adds the given {@link FieldUpdate} to this DocumentUpdate. If this DocumentUpdate already contains a FieldUpdate * for the named field, the content of the given FieldUpdate is added to the existing one. * * @param update The FieldUpdate to add to this DocumentUpdate. * @return This, to allow chaining. * @throws IllegalArgumentException If the {@link DocumentType} of this DocumentUpdate does not have a corresponding * field. */ public DocumentUpdate addFieldUpdate(FieldUpdate update) { int fieldId = update.getField().getId(); if (documentType.getField(fieldId) == null) { throw new IllegalArgumentException("Document type '" + documentType.getName() + "' does not have field '" + update.getField().getName() + "'."); } FieldUpdate prevUpdate = getFieldUpdateById(fieldId); if (prevUpdate != update) { if (prevUpdate != null) { prevUpdate.addAll(update); } else { id2FieldUpdates.put(fieldId, update); } } return this; }
fillWeightedSetUpdate(buffer, initNesting, valueType, weightedSet); if (isRemove) { singleUpdate = FieldUpdate.createRemoveAll(field, weightedSet); } else { singleUpdate = FieldUpdate.createAddAll(field, weightedSet); singleUpdate = FieldUpdate.createRemoveAll(field, arrayContents); } else { singleUpdate = FieldUpdate.createAddAll(field, arrayContents); update.addAll(singleUpdate);
/** * Applies this document update. * * @param doc the document to apply the update to * @return a reference to itself * @throws IllegalArgumentException if the document does not have the same document type as this update */ public DocumentUpdate applyTo(Document doc) { verifyType(doc); for (FieldUpdate fieldUpdate : id2FieldUpdates.values()) { fieldUpdate.applyTo(doc); } for (FieldPathUpdate fieldPathUpdate : fieldPathUpdates) { fieldPathUpdate.applyTo(doc); } return this; }
@Override public void write(FieldUpdate update) { putInt(null, update.getField().getId(Document.SERIALIZED_VERSION)); putInt(null, update.getValueUpdates().size()); for (ValueUpdate vupd : update.getValueUpdates()) { putInt(null, vupd.getValueUpdateClassID().id); vupd.serialize(this, update.getField().getDataType()); } }
public void read(FieldUpdate fieldUpdate) { int fieldId = getInt(null); Field field = fieldUpdate.getDocumentType().getField(fieldId, fieldUpdate.getSerializationVersion()); if (field == null) { throw new DeserializationException( "Cannot deserialize FieldUpdate, field fieldId " + fieldId + " not found in " + fieldUpdate.getDocumentType()); } fieldUpdate.setField(field); int size = getInt(null); for (int i = 0; i < size; i++) { if (field.getDataType() instanceof CollectionDataType) { CollectionDataType collType = (CollectionDataType) field.getDataType(); fieldUpdate.addValueUpdate(getValueUpdate(collType, collType.getNestedType())); } else { fieldUpdate.addValueUpdate(getValueUpdate(field.getDataType(), null)); } } }
FieldUpdate fieldUpdate = FieldUpdate.create(docType.getField("headerval")); fieldUpdate.addValueUpdate(AssignValueUpdate.createAssign(new IntegerFieldValue(42))); update.addFieldUpdate(fieldUpdate);
FieldUpdate readAlter(DocumentUpdate update) throws XMLStreamException { Field f = null; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("field".equals(reader.getAttributeName(i).toString())) { f = update.getDocumentType().getField(reader.getAttributeValue(i)); } } if (f == null) { throw newDeserializeException("Alter update without \"field\" attribute"); } FieldUpdate fu = FieldUpdate.create(f); while (reader.hasNext()) { int type = reader.next(); if (type == XMLStreamReader.START_ELEMENT) { if ("increment".equals(reader.getName().toString()) || "decrement".equals(reader.getName().toString()) || "multiply".equals(reader.getName().toString()) || "divide".equals(reader.getName().toString())) { update.addFieldUpdate(readArithmetic(update, reader.getName().toString(), f, fu)); skipToEnd(reader.getName().toString()); } else { throw newDeserializeException("Element \"" + reader.getName() + "\" not appropriate within alter element"); } } else if (type == XMLStreamReader.END_ELEMENT) { break; } } return fu; }
@Override public void write(FieldUpdate fieldUpdate) { wrapIOException(() -> { generator.writeObjectFieldStart(fieldUpdate.getField().getName()); ArrayList<ValueUpdate> removeValueUpdates = new ArrayList<>(); ArrayList<ValueUpdate> addValueUpdates = new ArrayList<>(); final DataType dataType = fieldUpdate.getField().getDataType(); for (ValueUpdate valueUpdate : fieldUpdate.getValueUpdates()) { if (valueUpdate instanceof RemoveValueUpdate) { removeValueUpdates.add(valueUpdate); } else if (valueUpdate instanceof AddValueUpdate) { addValueUpdates.add(valueUpdate); } else { valueUpdate.serialize(this, dataType); } } writeAddOrRemoveValueUpdates("remove", removeValueUpdates, dataType); writeAddOrRemoveValueUpdates("add", addValueUpdates, dataType); generator.writeEndObject(); }); }
/** * Creates a new field update, with one encapsulated value update * specifying an addition of a value to an array or a key to a weighted set (with default weight 1). * * @param field the Field to add a value to * @param value the value to add to the array, or key to add to the weighted set * @return a FieldUpdate specifying the addition * @throws IllegalArgumentException if the runtime type of newValue does not match the type required by field * @throws UnsupportedOperationException if the field type is not array or weighted set * @see com.yahoo.document.update.ValueUpdate#createAdd(FieldValue) */ public static FieldUpdate createAdd(Field field, FieldValue value) { return new FieldUpdate(field, ValueUpdate.createAdd(value)); }
/** * Adds a list of value updates to the list of value updates. * * @param valueUpdates a list containing the value updates to add * @return a reference to itself * @throws IllegalArgumentException if the data type of the value update is not equal to the data type of this field */ public FieldUpdate addValueUpdates(List<ValueUpdate> valueUpdates) { for (ValueUpdate vupd : valueUpdates) { addValueUpdate(vupd); } return this; }
/** * Adds all the {@link ValueUpdate}s of the given FieldUpdate to this. If the given FieldUpdate refers to a * different {@link Field} than this, this method throws an exception. * * @param update The update whose content to add to this. * @throws IllegalArgumentException If the {@link Field} of the given FieldUpdate does not match this. */ public void addAll(FieldUpdate update) { if (update == null) { return; } if (!field.equals(update.field)) { throw new IllegalArgumentException("Expected " + field + ", got " + update.field + "."); } addValueUpdates(update.valueUpdates); }