aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/processing/IndexingValues.java
blob: fa4b7d2bc40a70333365286f37fb6cc91788d228 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.schema.RankProfileRegistry;
import com.yahoo.document.Field;
import com.yahoo.schema.Schema;
import com.yahoo.schema.document.SDField;
import com.yahoo.vespa.indexinglanguage.ExpressionConverter;
import com.yahoo.vespa.indexinglanguage.expressions.Expression;
import com.yahoo.vespa.indexinglanguage.expressions.InputExpression;
import com.yahoo.vespa.indexinglanguage.expressions.OutputExpression;
import com.yahoo.vespa.model.container.search.QueryProfiles;

/**
 * @author Simon Thoresen Hult
 */
public class IndexingValues extends Processor {

    public IndexingValues(Schema schema, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
        super(schema, deployLogger, rankProfileRegistry, queryProfiles);
    }

    @Override
    public void process(boolean validate, boolean documentsOnly) {
        if ( ! validate) return;

        for (Field field : schema.getDocument().fieldSet()) {
            SDField sdField = (SDField)field;
            if ( ! sdField.isExtraField()) {
                new RequireThatDocumentFieldsAreImmutable(field).convert(sdField.getIndexingScript());
            }
        }
    }

    private class RequireThatDocumentFieldsAreImmutable extends ExpressionConverter {

        final Field field;
        Expression mutatedBy;

        RequireThatDocumentFieldsAreImmutable(Field field) {
            this.field = field;
        }

        @Override
        public ExpressionConverter branch() {
            return clone();
        }

        @Override
        protected boolean shouldConvert(Expression exp) {
            if (exp instanceof OutputExpression && mutatedBy != null) {
                throw newProcessException(schema, field,
                                          "Indexing expression '" + mutatedBy + "' attempts to modify the value of the " +
                                          "document field '" + field.getName() + "'. Use a field outside the document " +
                                          "block instead.");
            }
            if (exp instanceof InputExpression && ((InputExpression)exp).getFieldName().equals(field.getName())) {
                mutatedBy = null;
            } else if (exp.createdOutputType() != null) {
                mutatedBy = exp;
            }
            return false;
        }

        @Override
        protected Expression doConvert(Expression exp) {
            throw new UnsupportedOperationException();
        }
    }
}