Class DocumentUpdate

  • All Implemented Interfaces:
    java.lang.Iterable<FieldPathUpdate>

    public class DocumentUpdate
    extends DocumentOperation
    implements java.lang.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:
    FieldUpdate, ValueUpdate
    • Constructor Detail

      • 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​(DocumentType docType,
                              java.lang.String docId)
        Creates a DocumentUpdate.
        Parameters:
        docId - the ID of the update
        docType - the document type that this update is valid for
    • Method Detail

      • 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:
        java.lang.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:
        java.lang.IllegalArgumentException - if the document does not have the same document type as this update
      • fieldUpdates

        public java.util.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 java.util.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​(java.lang.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​(java.util.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:
        java.lang.NullPointerException - if the argument passed is null
      • setFieldUpdates

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

        public void addFieldUpdates​(java.util.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:
        java.lang.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:
        java.lang.IllegalArgumentException - If the DocumentId or DocumentType of the given DocumentUpdate does not match the content of this.
      • removeFieldUpdate

        public FieldUpdate removeFieldUpdate​(java.lang.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)
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • 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 java.util.Optional<java.lang.Boolean> getOptionalCreateIfNonExistent()