Class DocumentUpdate

java.lang.Object
com.yahoo.document.DocumentOperation
com.yahoo.document.DocumentUpdate
All Implemented Interfaces:
Iterable<FieldPathUpdate>

public class DocumentUpdate extends DocumentOperation implements Iterable<FieldPathUpdate>

Specifies one or more field updates to a document.

A document update contains a list of field updates for fields to be updated by this update. Each field update is applied atomically, but the entire document update is not. A document update can only contain one field update per field. To make multiple updates to the same field in the same document update, add multiple value updates to the same field update.

To update a document and set a string field to a new value:

 DocumentType musicType = DocumentTypeManager.getInstance().getDocumentType("music", 0);
 DocumentUpdate docUpdate = new DocumentUpdate(musicType,
         new DocumentId("id:test:music::http://music.yahoo.com/"));
 FieldUpdate update = FieldUpdate.createAssign(musicType.getField("artist"), "lillbabs");
 docUpdate.addFieldUpdate(update);
 
Author:
Einar M R Rosenvinge
See Also:
  • Field Details

  • Constructor Details

    • DocumentUpdate

      public DocumentUpdate(DocumentType docType, DocumentId docId)
      Creates a DocumentUpdate.
      Parameters:
      docId - the ID of the update
      docType - the document type that this update is valid for
    • DocumentUpdate

      public DocumentUpdate(DocumentUpdateReader reader)
      Creates a new document update using a reader
    • DocumentUpdate

      public DocumentUpdate(DocumentUpdate update)
      Creates a new document update which is a copy of the argument.
    • DocumentUpdate

      public DocumentUpdate(DocumentType docType, String docId)
      Creates a DocumentUpdate.
      Parameters:
      docId - the ID of the update
      docType - the document type that this update is valid for
  • Method Details

    • getId

      public DocumentId getId()
      Specified by:
      getId in class DocumentOperation
    • setId

      public void setId(DocumentId id)
      Sets the document id of the document to update. Use only while deserializing - changing the document id after creation has undefined behaviour.
    • applyTo

      public DocumentUpdate applyTo(Document doc)
      Applies this document update.
      Parameters:
      doc - the document to apply the update to
      Returns:
      a reference to itself
      Throws:
      IllegalArgumentException - if the document does not have the same document type as this update
    • prune

      public DocumentUpdate prune(Document doc)
      Prune away any field update that will not modify any field in the document.
      Parameters:
      doc - document to check against
      Returns:
      a reference to itself
      Throws:
      IllegalArgumentException - if the document does not have the same document type as this update
    • fieldUpdates

      public Collection<FieldUpdate> fieldUpdates()
      Get an unmodifiable collection of all field updates that this document update specifies.
      Returns:
      a collection of all FieldUpdates in this DocumentUpdate
    • fieldPathUpdates

      public Collection<FieldPathUpdate> fieldPathUpdates()
      Get an unmodifiable collection of all field path updates that this document update specifies.
      Returns:
      a collection of all FieldPathUpdates in this DocumentUpdate
    • getDocumentType

      public DocumentType getDocumentType()
      Returns the type of the document this updates
      Returns:
      The documentype of the document
    • setDocumentType

      public void setDocumentType(DocumentType type)
      Sets the document type. Use only while deserializing - changing the document type after creation has undefined behaviour.
    • getFieldUpdate

      public FieldUpdate getFieldUpdate(Field field)
      Returns the update for a field
      Parameters:
      field - the field to return the update of
      Returns:
      the update for the field, or null if that field has no update in this
    • clearFieldUpdates

      public void clearFieldUpdates()
      Removes all field updates from the list for field updates.
    • getFieldUpdate

      public FieldUpdate getFieldUpdate(String fieldName)
      Returns the update for a field name
      Parameters:
      fieldName - the field name to return the update of
      Returns:
      the update for the field, or null if that field has no update in this
    • setFieldUpdates

      public void setFieldUpdates(Collection<FieldUpdate> fieldUpdates)
      Assigns the field updates of this document update. Also note that no assumptions can be made on the order of item after this call. They might have been joined if for the same field or reordered.
      Parameters:
      fieldUpdates - the new list of updates of this
      Throws:
      NullPointerException - if the argument passed is null
    • setFieldUpdates

      public void setFieldUpdates(List<FieldUpdate> fieldUpdates)
      The same as setFieldUpdates(Collection<FieldUpdate>)
    • addFieldUpdates

      public void addFieldUpdates(Collection<FieldUpdate> fieldUpdates)
    • size

      public int size()
      Get the number of field updates in this document update.
      Returns:
      the size of the List of FieldUpdates
    • addFieldUpdate

      public DocumentUpdate addFieldUpdate(FieldUpdate update)
      Adds the given 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.
      Parameters:
      update - The FieldUpdate to add to this DocumentUpdate.
      Returns:
      This, to allow chaining.
      Throws:
      IllegalArgumentException - If the DocumentType of this DocumentUpdate does not have a corresponding field.
    • addFieldPathUpdate

      public DocumentUpdate addFieldPathUpdate(FieldPathUpdate fieldPathUpdate)
      Adds a field path update to perform on the document.
      Returns:
      a reference to itself.
    • addAll

      public void addAll(DocumentUpdate update)
      Adds all the field- and field path updates of the given document update to this. If the given update refers to a different document or document type than this, this method throws an exception.
      Parameters:
      update - The update whose content to add to this.
      Throws:
      IllegalArgumentException - If the DocumentId or DocumentType of the given DocumentUpdate does not match the content of this.
    • removeFieldUpdate

      public FieldUpdate removeFieldUpdate(Field field)
    • removeFieldUpdate

      public FieldUpdate removeFieldUpdate(String fieldName)
    • getType

      public DocumentType getType()
      Returns the document type of this document update.
      Returns:
      the document type of this document update
    • serialize

      public final void serialize(com.yahoo.io.GrowableByteBuffer buf)
    • serialize

      public void serialize(DocumentUpdateWriter data)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public Iterator<FieldPathUpdate> iterator()
      Specified by:
      iterator in interface Iterable<FieldPathUpdate>
    • isEmpty

      public boolean isEmpty()
      Returns whether or not this field update contains any field- or field path updates.
      Returns:
      True if this update is empty.
    • setCreateIfNonExistent

      public void setCreateIfNonExistent(boolean value)
      Sets whether this update should create the document it updates if that document does not exist. In this case an empty document is created before the update is applied.
      Parameters:
      value - Whether the document it updates should be created.
    • getCreateIfNonExistent

      public boolean getCreateIfNonExistent()
      Gets whether this update should create the document it updates if that document does not exist.
      Returns:
      whether the document it updates should be created.
    • getOptionalCreateIfNonExistent

      public Optional<Boolean> getOptionalCreateIfNonExistent()