summaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/GuardExpression.java
blob: da7cfcdcaee21e05fd2c0fca7c8f6eb6550defb7 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;

import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.Field;
import com.yahoo.vespa.indexinglanguage.ExpressionVisitor;
import com.yahoo.vespa.indexinglanguage.UpdateAdapter;
import com.yahoo.vespa.objects.ObjectOperation;
import com.yahoo.vespa.objects.ObjectPredicate;

/**
 * @author Simon Thoresen Hult
 */
public final class GuardExpression extends CompositeExpression {

    private final Expression exp;
    private final boolean shouldExecute;

    public GuardExpression(Expression exp) {
        super(exp.requiredInputType());
        this.exp = exp;
        shouldExecute = shouldExecute(exp);
    }

    public Expression getInnerExpression() {
        return exp;
    }

    @Override
    public void setStatementOutput(DocumentType documentType, Field field) {
        exp.setStatementOutput(documentType, field);
    }

    @Override
    protected void doExecute(ExecutionContext context) {
        if (!shouldExecute && context.getAdapter() instanceof UpdateAdapter) {
            context.setValue(null);
        } else {
            exp.execute(context);
        }
    }

    @Override
    protected void doVerify(VerificationContext context) {
        exp.verify(context);
    }

    @Override
    public DataType createdOutputType() {
        return exp.createdOutputType();
    }

    @Override
    public String toString() {
        return "guard " + toScriptBlock(exp);
    }

    @Override
    public void selectMembers(ObjectPredicate predicate, ObjectOperation operation) {
         select(exp, predicate, operation);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof GuardExpression rhs)) return false;
        if (!exp.equals(rhs.exp)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return getClass().hashCode() + exp.hashCode();
    }

    private static boolean shouldExecute(Expression exp) {
        ExecutionGuard guard = new ExecutionGuard();
        guard.visit(exp);
        return guard.shouldExecute;
    }

    private static class ExecutionGuard extends ExpressionVisitor {

        boolean shouldExecute = false;

        @Override
        protected void doVisit(Expression exp) {
            if (exp instanceof InputExpression || exp instanceof SetLanguageExpression) {
                shouldExecute = true;
            }
        }
    }

}