// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.fieldpathupdate;

import com.yahoo.document.Document;
import com.yahoo.document.DocumentType;
import com.yahoo.document.datatypes.FieldPathIteratorHandler;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.serialization.DocumentUpdateReader;

/**
 * @author <a href="mailto:thomasg@yahoo-inc.com">Thomas Gundersen</a>
 */
public class RemoveFieldPathUpdate extends FieldPathUpdate {
    class IteratorHandler extends FieldPathIteratorHandler {
        IteratorHandler() {
        }

        @Override
        public ModificationStatus doModify(FieldValue fv) {
            return ModificationStatus.REMOVED;
        }

        @Override
        public boolean onComplex(FieldValue fv) {
            return false;
        }
    }

    IteratorHandler handler;

    public RemoveFieldPathUpdate(DocumentType type, String fieldPath, String whereClause) {
        super(FieldPathUpdate.Type.REMOVE, type, fieldPath, whereClause);
        handler = new IteratorHandler();
    }

    public RemoveFieldPathUpdate(DocumentType type, String fieldPath) {
        super(FieldPathUpdate.Type.REMOVE, type, fieldPath, null);
        handler = new IteratorHandler();
    }

    public RemoveFieldPathUpdate(DocumentType type, DocumentUpdateReader reader) {
        super(FieldPathUpdate.Type.REMOVE, type, reader);
        reader.read(this);
        handler = new IteratorHandler();
    }

    FieldPathIteratorHandler getIteratorHandler(Document doc) {
        return handler;
    }

    @Override
    public String toString() {
        return "Remove: " + super.toString();
    }
}

@@ -410,16 +410,597 @@
 r;%0A%0A/**%0A
+ * RemoveFieldPathUpdate is used to remove fields or portions of fields based on the field path supplied and optionally%0A * a where clause.%0A *%0A * If document type %22testdoc%22 has a field %22ivec%22 which is an array of integers, then the field path and where clause%0A * to remove all array elements having the value 5 is %22ivec%5B$x%5D%22 and %22testdoc.ivec%5B$x%5D == 5%22.%0A *%0A * If document type %22testdoc%22 has a field %22svec%22 which is an array of struct containing an integer field %22ival%22 then%0A * %22svec%5B$x%5D%22 and %22testdoc.svec%5B$x%5D.ival == 5%22 can be used to remove all array elements where ival is 5.%0A *%0A
  * @auth

Add example in comment for remove field path update class.