aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search/src/main/java/com/yahoo/search/predicate/index/CachedPostingListCounter.java
blob: eb8b0b9927b70e4b93e01a597ebcdd3742ac4c7a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.index;

import com.google.common.collect.MinMaxPriorityQueue;
import org.eclipse.collections.api.tuple.primitive.ObjectLongPair;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectIntHashMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectLongHashMap;

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

/**
 * Counts the number of posting lists per document id.
 * Caches the most expensive posting list in a bit vector.
 *
 * @author bjorncs
 */
public class CachedPostingListCounter {

    // Only use bit vector for counting if the documents covered is more than the threshold (relative to nDocuments)
    private static final double THRESHOLD_USE_BIT_VECTOR = 1;

    private final int nDocuments;
    private final ObjectLongHashMap<int[]> frequency = new ObjectLongHashMap<>();
    private final ObjectIntHashMap<int[]> postingListMapping;
    private final int[] bitVector;

    public CachedPostingListCounter(int nDocuments) {
        this.nDocuments = nDocuments;
        this.postingListMapping = new ObjectIntHashMap<>();
        this.bitVector = new int[0];
    }

    private CachedPostingListCounter(ObjectIntHashMap<int[]> postingListMapping, int[] bitVector) {
        this.nDocuments = bitVector.length;
        this.postingListMapping = postingListMapping;
        this.bitVector = bitVector;
    }

    public synchronized void registerUsage(List<PostingList> postingLists) {
        for (PostingList postingList : postingLists) {
            frequency.updateValue(postingList.getDocIds(), 0, v -> v + 1);
        }
    }

    public void countPostingListsPerDocument(List<PostingList> postingLists, byte[] nPostingListsForDocument) {
        Arrays.fill(nPostingListsForDocument, (byte) 0);
        List<int[]> nonCachedPostingLists = new ArrayList<>(postingLists.size());
        List<int[]> cachedPostingLists = new ArrayList<>(postingLists.size());
        long nDocumentsCachedPostingLists = 0;
        int postingListBitmap = 0;
        for (PostingList postingList : postingLists) {
            int[] docIds = postingList.getDocIds();
            int index = postingListMapping.getIfAbsent(docIds, -1);
            if (index >= 0) {
                cachedPostingLists.add(docIds);
                postingListBitmap |= (1 << index);
                nDocumentsCachedPostingLists += docIds.length;
            } else {
                nonCachedPostingLists.add(docIds);
            }
        }
        if (postingListBitmap != 0) {
            if (nDocumentsCachedPostingLists > nDocuments * THRESHOLD_USE_BIT_VECTOR) {
                countUsingBitVector(nPostingListsForDocument, postingListBitmap);
            } else {
                nonCachedPostingLists.addAll(cachedPostingLists);
            }
        }
        if (!nonCachedPostingLists.isEmpty()) {
            countUsingDocIdIteration(nPostingListsForDocument, nonCachedPostingLists);
        }
    }

    private void countUsingBitVector(byte[] nPostingListsForDocument, int postingListBitmap) {
        for (int docId = 0; docId < nDocuments; docId++) {
            nPostingListsForDocument[docId] += (byte)Integer.bitCount(bitVector[docId] & postingListBitmap);
        }
    }

    private static void countUsingDocIdIteration(byte[] nPostingListsForDocument, List<int[]> nonCachedPostingLists) {
        for (int[] docIds : nonCachedPostingLists) {
            for (int docId : docIds) {
                ++nPostingListsForDocument[docId];
            }
        }
    }

    public CachedPostingListCounter rebuildCache() {
        MinMaxPriorityQueue<Entry> mostExpensive = MinMaxPriorityQueue.maximumSize(32).expectedSize(32).create();
        synchronized (this) {
            for (ObjectLongPair<int[]> p : frequency.keyValuesView()) {
                mostExpensive.add(new Entry(p.getOne(), p.getTwo()));
            }
        }
        ObjectIntHashMap<int[]> postingListMapping = new ObjectIntHashMap<>();
        int[] bitVector = new int[nDocuments];
        int length = mostExpensive.size();
        for (int i = 0; i < length; i++) {
            Entry e = mostExpensive.removeFirst();
            int[] docIds = e.docIds;
            postingListMapping.put(docIds, i);
            for (int docId : docIds) {
                bitVector[docId] |= (1 << i);
            }
        }
        return new CachedPostingListCounter(postingListMapping, bitVector);
    }

    int[] getBitVector() {
        return bitVector;
    }

    ObjectIntHashMap<int[]> getPostingListMapping() {
        return postingListMapping;
    }

    private static class Entry implements Comparable<Entry> {
        public final int[] docIds;
        final double cost;

        private Entry(int[] docIds, long frequency) {
            this.docIds = docIds;
            this.cost = docIds.length * (double) frequency;
            assert cost > 0;
        }

        @Override
        public int compareTo(Entry o) {
            return -Double.compare(cost, o.cost);
        }
    }
}