summaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/SimpleAdapterFactory.java
blob: 8173686b2ec753990cbaaaa3c654fd4d47df203a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage;

import com.yahoo.document.*;
import com.yahoo.document.fieldpathupdate.FieldPathUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;
import com.yahoo.vespa.indexinglanguage.expressions.Expression;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
@SuppressWarnings("rawtypes")
public class SimpleAdapterFactory implements AdapterFactory {
    public static class SelectExpression {
        public Expression selectExpression(DocumentType documentType, String fieldName) {
            return null;
        }
    }
    private final SelectExpression expressionSelector;

    public SimpleAdapterFactory() {
        this(new SelectExpression());
    }
    public SimpleAdapterFactory(SelectExpression expressionSelector) {
        this.expressionSelector = expressionSelector;
    }

    @Override
    public DocumentAdapter newDocumentAdapter(Document doc) {
        return newDocumentAdapter(doc, false);
    }

    public DocumentAdapter newDocumentAdapter(Document doc, boolean isUpdate) {
        if (isUpdate) {
            return new SimpleDocumentAdapter(doc);
        }
        return new SimpleDocumentAdapter(doc, doc);
    }

    @Override
    public List<UpdateAdapter> newUpdateAdapterList(DocumentUpdate upd) {
        List<UpdateAdapter> ret = new ArrayList<>();
        DocumentType docType = upd.getDocumentType();
        DocumentId docId = upd.getId();
        Document complete = new Document(docType, upd.getId());
        for (FieldPathUpdate fieldUpd : upd) {
            if (FieldPathUpdateHelper.isComplete(fieldUpd)) {
                FieldPathUpdateHelper.applyUpdate(fieldUpd, complete);
            } else {
                Document partial = FieldPathUpdateHelper.newPartialDocument(docId, fieldUpd);
                ret.add(new FieldPathUpdateAdapter(newDocumentAdapter(partial, true), fieldUpd));
            }
        }
        for (FieldUpdate fieldUpd : upd.getFieldUpdates()) {
            Field field = fieldUpd.getField();
            for (ValueUpdate valueUpd : fieldUpd.getValueUpdates()) {
                if (FieldUpdateHelper.isComplete(field, valueUpd)) {
                    FieldUpdateHelper.applyUpdate(field, valueUpd, complete);
                } else {
                    Document partial = FieldUpdateHelper.newPartialDocument(docType, docId, field, valueUpd);
                    ret.add(FieldUpdateAdapter.fromPartialUpdate(expressionSelector.selectExpression(docType, field.getName()),newDocumentAdapter(partial, true), valueUpd));
                }
            }
        }
        ret.add(FieldUpdateAdapter.fromCompleteUpdate(newDocumentAdapter(complete, true)));
        return ret;
    }
}