aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/select/ResultList.java
blob: 956efd48be883d59f1451bb23b327ebd291c18ed (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.select;

import com.yahoo.document.datatypes.FieldPathIteratorHandler;
import com.yahoo.document.select.rule.AttributeNode;

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

public class ResultList {
    public static class ResultPair {
        ResultPair(FieldPathIteratorHandler.VariableMap var, Result res) {
            variables = var;
            result = res;
        }

        FieldPathIteratorHandler.VariableMap variables;
        Result result;

        public FieldPathIteratorHandler.VariableMap getVariables() { return variables; }
        public Result getResult() { return result; }

        public String toString() {
            return variables.toString() + " => " + result;
        }
    }

    public static class VariableValue {
       public VariableValue(FieldPathIteratorHandler.VariableMap vars, Object value) {
           variables = vars;
           this.value = value;
       }

        FieldPathIteratorHandler.VariableMap variables;
        Object value;

        public FieldPathIteratorHandler.VariableMap getVariables() { return variables; }
        public Object getValue() { return value; }

        public String toString() {
            return variables.toString() + " => " + value;
        }
    }

    private List<ResultPair> results = new ArrayList<>();

    public ResultList() {
    }

    public ResultList(Result result) {
        add(new FieldPathIteratorHandler.VariableMap(), result);
    }

    public void add(FieldPathIteratorHandler.VariableMap variables, Result result) {
        results.add(new ResultPair(variables, result));
    }

    public List<ResultPair> getResults() {
        return results;
    }

    public static ResultList fromBoolean(boolean result) {
        return new ResultList(result ? Result.TRUE : Result.FALSE);
    }

    public Result toResult() {
        if (results.isEmpty()) {
            return Result.FALSE;
        }

        boolean foundFalse = false;

        for (ResultPair rp : results) {
            if (rp.result == Result.TRUE) {
                return Result.TRUE;
            } else if (rp.result == Result.FALSE) {
                foundFalse = true;
            }
        }

        if (foundFalse) {
            return Result.FALSE;
        } else {
            return Result.INVALID;
        }
    }

    private boolean combineVariables(FieldPathIteratorHandler.VariableMap output, FieldPathIteratorHandler.VariableMap input) {
        // First, verify that all variables are overlapping
        for (Map.Entry<String, FieldPathIteratorHandler.IndexValue> entry : output.entrySet()) {
            FieldPathIteratorHandler.IndexValue found = input.get(entry.getKey());

            if (found != null) {
                if (!(found.equals(entry.getValue()))) {
                    return false;
                }
            }
        }

        for (Map.Entry<String, FieldPathIteratorHandler.IndexValue> entry : input.entrySet()) {
            FieldPathIteratorHandler.IndexValue found = output.get(entry.getKey());

            if (found != null) {
                if (!(found.equals(entry.getValue()))) {
                    return false;
                }
            }
        }

        // Ok, variables are overlapping. Add all variables from input to output.
        for (Map.Entry<String, FieldPathIteratorHandler.IndexValue> entry : input.entrySet()) {
            output.put(entry.getKey(), entry.getValue());
        }

        return true;
    }

    public interface LazyResultList {
        ResultList getResult();
    }

    public ResultList combineAND(LazyResultList other)
    {
        if (Result.FALSE == toResult()) return ResultList.toResultList(false);

        ResultList result = new ResultList();

        // TODO: optimize
        for (ResultPair pair : results) {
            for (ResultPair otherPair : other.getResult().results) {
                FieldPathIteratorHandler.VariableMap varMap = (FieldPathIteratorHandler.VariableMap)pair.variables.clone();

                if (combineVariables(varMap, otherPair.variables)) {
                    result.add(varMap, combineAND(pair.result, otherPair.result));
                }
            }
        }

        return result;
    }

    private static Result combineAND(Result lhs, Result rhs) {
        if (lhs == Result.TRUE && rhs == Result.TRUE) {
            return Result.TRUE;
        }
        if (lhs == Result.FALSE || rhs == Result.FALSE) {
            return Result.FALSE;
        }
        return Result.INVALID;
    }

    public ResultList combineOR(LazyResultList other)
    {
        if (Result.TRUE == toResult()) return ResultList.toResultList(true);

        ResultList result = new ResultList();

        // TODO: optimize
        for (ResultPair pair : results) {
            for (ResultPair otherPair : other.getResult().results) {
                FieldPathIteratorHandler.VariableMap varMap = (FieldPathIteratorHandler.VariableMap)pair.variables.clone();

                if (combineVariables(varMap, otherPair.variables)) {
                    result.add(varMap, combineOR(pair.result, otherPair.result));
                }
            }
        }

        return result;
    }

    private static Result combineOR(Result lhs, Result rhs) {
        if (lhs == Result.TRUE || rhs == Result.TRUE) {
            return Result.TRUE;
        }
        if (lhs == Result.FALSE && rhs == Result.FALSE) {
            return Result.FALSE;
        }
        return Result.INVALID;
    }

    /**
     * Converts the given object value into a result list, so it can be compared by logical operators.
     *
     * @param value The object to convert.
     * @return The corresponding result value.
     */
    public static ResultList toResultList(Object value) {
        if (value instanceof ResultList) {
            return (ResultList)value;
        } else if (value instanceof AttributeNode.VariableValueList) {
            ResultList retVal = new ResultList();
            for (VariableValue vv : (AttributeNode.VariableValueList)value) {
                retVal.add(vv.getVariables(), Result.TRUE);
            }
            return retVal;
        } else if (value == null || value == Result.FALSE || value == Boolean.FALSE ||
            (value instanceof Number && ((Number)value).doubleValue() == 0)) {
            return new ResultList(Result.FALSE);
        } else if (value == Result.INVALID) {
            return new ResultList(Result.INVALID);
        } else {
            return new ResultList(Result.TRUE);
        }
    }

    public String toString() {
        return results.toString();
    }
}