aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search/src/main/java/com/yahoo/search/predicate/index/PredicateSearch.java
blob: 1b8eeb4454aed20a99747388090686e7076650f8 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.index;

import com.yahoo.search.predicate.Hit;
import com.yahoo.search.predicate.SubqueryBitmap;
import com.yahoo.search.predicate.utils.PrimitiveArraySorter;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * Implementation of the "Interval" predicate search algorithm.
 *
 * @author Magnar Nedland
 * @author bjorncs
 */
public class PredicateSearch {

    private final PostingList[] postingLists;
    private final byte[] nPostingListsForDocument;
    private final byte[] minFeatureIndex;
    private final int[] docIds;
    private final int[] intervals;
    private final long[] subqueries;
    private final long[] subqueryMarkers;
    private final boolean[] visited;
    private final short[] intervalEnds;

    private short[] sortedIndexes;
    private short[] sortedIndexesMergeBuffer;
    private int nPostingLists;

    /**
     * Creates a search for a set of posting lists.
     * 
     * @param postingLists Posting lists for the boolean variables that evaluate to true
     * @param nPostingListsForDocument The number of posting list for each docId
     * @param minFeatureIndex Index from docId to min-feature value.
     * @param intervalEnds The interval end for each document.
     * @param highestIntervalEnd The highest end value.
     */
    public PredicateSearch(
            List<PostingList> postingLists, byte[] nPostingListsForDocument,
            byte[] minFeatureIndex, short[] intervalEnds, int  highestIntervalEnd) {
        int size = postingLists.size();
        this.nPostingListsForDocument = nPostingListsForDocument;
        this.minFeatureIndex = minFeatureIndex;
        this.nPostingLists = size;
        this.postingLists = postingLists.toArray(new PostingList[postingLists.size()]);
        this.sortedIndexes = new short[size];
        this.sortedIndexesMergeBuffer = new short[size];
        this.docIds = new int[size];
        this.intervals = new int[size];
        this.subqueries = new long[size];
        this.subqueryMarkers = new long[highestIntervalEnd + 1];
        this.visited = new boolean[highestIntervalEnd + 1];
        this.intervalEnds = intervalEnds;

        // Sort posting list array based on the underlying number of documents (largest first).
        Arrays.sort(this.postingLists, (l, r) -> -Integer.compare(l.size(), r.size()));

        for (short i = 0; i < size; ++i) {
            PostingList postingList = this.postingLists[i];
            sortedIndexes[i] = i;
            docIds[i] = postingList.getDocId();
            subqueries[i] = postingList.getSubquery();
        }
        // All posting lists start at beginId, so no need to sort yet.
    }

    /**
     * @return A stream of Hit-objects from a lazy evaluation of the boolean search algorithm.
     */
    public Stream<Hit> stream() {
        if (nPostingLists == 0) {
            return Stream.empty();
        }
        return StreamSupport.stream(new PredicateSpliterator(), false);
    }

    private class PredicateSpliterator implements java.util.Spliterator<Hit> {
        private int lastHit = -1;

        @Override
        public boolean tryAdvance(Consumer<? super Hit> action) {
            Optional<Hit> optionalHit = seek(lastHit + 1);
            optionalHit.ifPresent(hit -> {
                lastHit = hit.getDocId();
                action.accept(hit);
            });
            return optionalHit.isPresent();
        }

        @Override
        public Spliterator<Hit> trySplit() {
            return null;
        }

        @Override
        public long estimateSize() {
            return Long.MAX_VALUE;
        }

        @Override
        public int characteristics() {
            return ORDERED | DISTINCT | SORTED | NONNULL;
        }

        @Override
        public Comparator<Hit> getComparator() {
            return null;
        }
    }

    private Optional<Hit> seek(int docId) {
        boolean skippedToEnd = skipMinFeature(docId);
        while (nPostingLists > 0 && !skippedToEnd) {
            int docId0 = docIds[sortedIndexes[0]];
            int minFeature = minFeatureIndex[docId0];
            int k = minFeature > 0 ? minFeature - 1 : 0;
            int intervalEnd = Short.toUnsignedInt(intervalEnds[docId0]);
            if (k < nPostingLists) {
                int docIdK = docIds[sortedIndexes[k]];
                if (docId0 == docIdK) {
                    if (evaluateHit(docId0, k, intervalEnd)) {
                        return Optional.of(new Hit(docId0, subqueryMarkers[intervalEnd]));
                    }
                }
            }
            skippedToEnd = skipMinFeature(docId0 + 1);
        }
        return Optional.empty();
    }

    private boolean skipMinFeature(int docId) {
        int nDocuments = nPostingListsForDocument.length;
        while (docId < nDocuments && minFeatureIndex[docId] > nPostingListsForDocument[docId]) {
            ++docId;
        }
        if (docId < nDocuments) {
            advanceAllTo(docId);
            return false;
        }
        return true;
    }

    private boolean evaluateHit(int docId, int k, int intervalEnd) {
        int candidates = k + 1;
        for (int i = candidates; i < nPostingLists; ++i) {
            if (docIds[sortedIndexes[i]] == docId) {
                ++candidates;
            } else {
                break;
            }
        }

        int nNoIntervalIterators = 0;
        for (int i = 0; i < candidates; ++i) {
            short index = sortedIndexes[i];
            PostingList postingList = postingLists[index];
            if (postingList.prepareIntervals()) {
                intervals[index] = postingList.getInterval();
            } else {
                ++nNoIntervalIterators;
                intervals[index] = 0xFFFFFFFF;
            }
        }
        PrimitiveArraySorter.sort(sortedIndexes, 0, candidates, (a, b) -> Integer.compareUnsigned(intervals[a], intervals[b]));
        candidates -= nNoIntervalIterators;

        Arrays.fill(subqueryMarkers, 0, intervalEnd + 1, 0);
        subqueryMarkers[0] = SubqueryBitmap.ALL_SUBQUERIES;
        Arrays.fill(visited, 0, intervalEnd + 1, false);
        visited[0] = true;
        int highestEndSeen = 1;
        for (int i = 0; i < candidates; ) {
            int index = sortedIndexes[i];
            int lastEnd = addInterval(index, highestEndSeen);
            if (lastEnd == -1) {
                return false;
            }
            highestEndSeen = Math.max(lastEnd, highestEndSeen);
            PostingList postingList = postingLists[index];
            if (postingList.nextInterval()) {
                intervals[index] = postingList.getInterval();
                restoreSortedOrder(i, candidates);
            } else {
                ++i;
            }
        }
        return subqueryMarkers[intervalEnd] != 0;
    }

    private void restoreSortedOrder(int first, int last) {
        short indexToMove = sortedIndexes[first];
        long intervalToMove = Integer.toUnsignedLong(intervals[indexToMove]);
        while (++first < last && intervalToMove > Integer.toUnsignedLong(intervals[sortedIndexes[first]])) {
            sortedIndexes[first - 1] = sortedIndexes[first];
        }
        sortedIndexes[first - 1] = indexToMove;
    }

    /**
     * Returns the end value of the interval,
     * or -1 if the highest end value seen is less than the interval begin.
     */
    private int addInterval(int index, int highestEndSeen) {
        int interval = intervals[index];
        long subqueryBitMap = subqueries[index];
        if (Interval.isZStar1Interval(interval)) {
            int begin = Interval.getZStar1Begin(interval);
            int end = Interval.getZStar1End(interval);
            if (highestEndSeen < begin) return -1;
            markSubquery(begin, end, ~subqueryMarkers[begin]);
            return end;
        } else {
            int begin = Interval.getBegin(interval);
            int end = Interval.getEnd(interval);
            if (highestEndSeen < begin -1) return -1;
            markSubquery(begin - 1, end, subqueryMarkers[begin - 1] & subqueryBitMap);
            return end;
        }
    }

    private void markSubquery(int begin, int end, long subqueryBitmap) {
        if (visited[begin]) {
            visited[end] = true;
            subqueryMarkers[end] |= subqueryBitmap;
        }
    }

    // Advances all posting lists to (or beyond) docId.
    private void advanceAllTo(int docId) {
        int i = 0;
        int completedCount = 0;
        for (; i < nPostingLists; ++i) {
            if (docIds[sortedIndexes[i]] >= docId) {
                break;
            }
            if (!advanceOneTo(docId, i)) {
                ++completedCount;
            }
        }
        // No need to sort if all posting lists are finished.
        if (i > 0 && nPostingLists > completedCount) {
            sortIndexes(i);
            // Decrement the number of posting lists.
        }
        nPostingLists -= completedCount;
    }

    // Advances a single posting list to (or beyond) docId.
    private boolean advanceOneTo(int docId, int index) {
        int i = sortedIndexes[index];
        PostingList postingList = postingLists[i];
        if (postingList.nextDocument(docId - 1)) {
            docIds[i] = postingList.getDocId();
            return true;
        }
        docIds[i] = Integer.MAX_VALUE;  // will be last after sorting.
        return false;
    }

    private void sortIndexes(int numUpdated) {
        // Sort the updated elements
        boolean swapMergeBuffer =
                PrimitiveArraySorter.sortAndMerge(sortedIndexes, sortedIndexesMergeBuffer, numUpdated, nPostingLists,
                        (a, b) -> Integer.compare(docIds[a], docIds[b]));
        if (swapMergeBuffer) {
            // Swap references
            short[] temp = sortedIndexes;
            sortedIndexes = sortedIndexesMergeBuffer;
            sortedIndexesMergeBuffer = temp;
        }

    }

}