summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/fastsearch/PacketWrapper.java
blob: bc0b8b8b471ac5f0edca60855a86a2bc25c42f58 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;


import java.util.*;
import java.util.logging.Logger;

import com.yahoo.fs4.BasicPacket;
import com.yahoo.fs4.DocsumPacket;
import com.yahoo.fs4.DocumentInfo;
import com.yahoo.fs4.Packet;
import com.yahoo.fs4.QueryResultPacket;
import com.yahoo.document.GlobalId;
import com.yahoo.document.DocumentId;


/**
 * A wrapper for cache entries to make it possible to check whether the
 * hits are truly correct.
 *
 * @author  <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 * @author  <a href="mailto:mathiasm@yahoo-inc.com">Mathias Lidal</a>
 */
public class PacketWrapper implements Cloneable {
    private static Logger log = Logger.getLogger(PacketWrapper.class.getName());

    final int keySize;
    // associated result packets, sorted in regard to offset
    private ArrayList<BasicPacket> resultPackets = new ArrayList<>(3); // length = "some small number"
    LinkedHashMap<DocsumPacketKey, BasicPacket> packets;

    private static class ResultPacketComparator<T extends BasicPacket> implements Comparator<T> {
        @Override
        public int compare(T o1, T o2) {
            QueryResultPacket r1 = (QueryResultPacket) o1;
            QueryResultPacket r2 = (QueryResultPacket) o2;
            return r1.getOffset() - r2.getOffset();
        }
    }

    private static ResultPacketComparator<BasicPacket> resultPacketComparator = new ResultPacketComparator<>();

    public PacketWrapper(CacheKey key, DocsumPacketKey[] packetKeys, BasicPacket[] bpackets) {
        // Should not support key == null
        this.keySize = key.byteSize();
        resultPackets.add(bpackets[0]);
        this.packets = new LinkedHashMap<>();
        Packet[] ppackets = new Packet[packetKeys.length];

        for (int i = 0; i < packetKeys.length; i++) {
            ppackets[i] = (Packet) bpackets[i + 1];
        }
        addDocsums(packetKeys, ppackets);
    }

    /**
     *  Only used by PacketCacheTestCase, should not be used otherwise
     */
    public PacketWrapper(CacheKey key, BasicPacket[] packets) {
        // Should support key == null as this is for testing
        if (key == null) {
            keySize = 0;
        } else {
            this.keySize = key.byteSize();
        }
        resultPackets.add(packets[0]);
        this.packets = new LinkedHashMap<>();
        for (int i = 0; i < packets.length - 1; i++) {
            this.packets.put(new DocsumPacketKey(new GlobalId(new DocumentId("doc:test:" + i).getGlobalId()), i, null), packets[i + 1]);
        }

    }

    public QueryResultPacket getFirstResultPacket() {
        if (resultPackets.size() > 0) {
            return (QueryResultPacket) resultPackets.get(0);
        } else {
            return null;
        }
    }

    /**
     * @return list of documents, null if not all are available
     */
    public List<DocumentInfo> getDocuments(int offset, int hits) {
        // speculatively allocate list for the hits
        List<DocumentInfo> docs = new ArrayList<>(hits);
        int currentOffset = 0;
        QueryResultPacket r = getFirstResultPacket();
        if (offset >= r.getTotalDocumentCount()) {
            // shortcut especially for results with 0 hits
            // >= both necessary for end of result sets and
            // offset == 0 && totalDocumentCount == 0
            return docs;
        }
        for (Iterator<BasicPacket> i = resultPackets.iterator(); i.hasNext();) {
            QueryResultPacket result = (QueryResultPacket) i.next();
            if (result.getOffset() > offset + currentOffset) {
                // we haven't got all the requested document info objects
                return null;
            }
            if (result.getOffset() + result.getDocumentCount()
                    <= currentOffset + offset) {
                // no new hits available
                continue;
            }
            List<DocumentInfo> documents = result.getDocuments();
            int packetOffset = (offset + currentOffset) - result.getOffset();
            int afterLastDoc = Math.min(documents.size(), packetOffset + hits);
            for (Iterator<DocumentInfo> j = documents.subList(packetOffset, afterLastDoc).iterator();
                    docs.size() < hits && j.hasNext();
                    ++currentOffset) {
                docs.add(j.next());
            }
            if (hits == docs.size()
                    || offset + docs.size() >= result.getTotalDocumentCount()) {
                // We have the hits we need, or there are no more hits available
                return docs;
            }
        }
        return null;
    }

    public void addResultPacket(QueryResultPacket resultPacket) {
        // This function only keeps the internal list sorted according
        // to offset
        int insertionPoint;
        QueryResultPacket r;

        if (resultPacket.getDocumentCount() == 0) {
            return; // do not add a packet which does not contain new info
        }

        insertionPoint = Collections.binarySearch(resultPackets,
                                                      resultPacket,
                                                      resultPacketComparator);
        if (insertionPoint < 0) {
            // new offset
            insertionPoint = ~insertionPoint; // (insertionPoint + 1) * -1;
            resultPackets.add(insertionPoint, resultPacket);
            cleanResultPackets();
        } else {
            // there exists a packet with same offset
            r = (QueryResultPacket) resultPackets.get(insertionPoint);
            if (resultPacket.getDocumentCount() > r.getDocumentCount()) {
                resultPackets.set(insertionPoint, resultPacket);
                cleanResultPackets();
            }
        }
    }

    private void cleanResultPackets() {
        int marker;
        QueryResultPacket previous;
        if (resultPackets.size() == 1) {
            return;
        }

        // we know the list is sorted with regard to offset
        // First ensure the list grows in regards to lastOffset as well.
        // Could have done this addResultPacket, but this makes the code
        // simpler.
        previous = (QueryResultPacket) resultPackets.get(0);
        for (int i = 1; i < resultPackets.size(); ++i) {
            QueryResultPacket r = (QueryResultPacket) resultPackets.get(i);
            if (r.getOffset() + r.getDocumentCount()
                    <= previous.getOffset() + previous.getDocumentCount()) {
                resultPackets.remove(i--);
            } else {
                previous = r;
            }
        }

        marker = 0;
        while (marker < (resultPackets.size() - 2)) {
            QueryResultPacket r0 = (QueryResultPacket) resultPackets.get(marker);
            QueryResultPacket r1 = (QueryResultPacket) resultPackets.get(marker + 1);
            QueryResultPacket r2 = (QueryResultPacket) resultPackets.get(marker + 2);
            int nextOffset = r0.getOffset() + r0.getDocumentCount();

            if (r1.getOffset() < nextOffset
                    && r2.getOffset() <= nextOffset) {
                resultPackets.remove(marker + 1);
            }
            ++marker;
        }
    }

    /** Only for testing. */
    public List<BasicPacket> getResultPackets() {
        return resultPackets;
    }

    public void addDocsums(DocsumPacketKey[] packetKeys, BasicPacket[] bpackets,
                           int offset) {
        Packet[] ppackets = new Packet[packetKeys.length];

        for (int i = 0; i < packetKeys.length; i++) {
            ppackets[i] = (Packet) bpackets[i + offset];
        }
        addDocsums(packetKeys, ppackets);
    }

    public void addDocsums(DocsumPacketKey[] packetKeys, Packet[] packets) {
        if (packetKeys == null || packets == null) {
            log.warning(
                    "addDocsums called with "
                            + (packetKeys == null ? "packetKeys == null " : "")
                            + (packets == null ? "packets == null" : ""));
            return;
        }
        for (int i = 0; i < packetKeys.length && i < packets.length; i++) {
            if (packetKeys[i] == null) {
                log.warning(
                        "addDocsums called, but packetsKeys[" + i + "] is null");
            } else if (packets[i] instanceof DocsumPacket) {
                DocsumPacket dp = (DocsumPacket) packets[i];

                if (packetKeys[i].getGlobalId().equals(dp.getGlobalId())
                    && dp.getData().length > 0)
                {
                    this.packets.put(packetKeys[i], packets[i]);
                    log.fine("addDocsums " + i + " globalId: " + dp.getGlobalId());
                } else {
                    log.warning("not caching bad Docsum for globalId " + packetKeys[i].getGlobalId() + ": " + dp);
                }
            } else {
                log.warning(
                        "addDocsums called, but packets[" + i
                        + "] is not a DocsumPacket instance");
            }
        }
    }

    public int getNumPackets() {
        return packets.size();
    }

    BasicPacket getPacket(GlobalId globalId, int partid, String summaryClass) {
        return getPacket(
                new DocsumPacketKey(globalId, partid, summaryClass));
    }

    BasicPacket getPacket(DocsumPacketKey packetKey) {
        return packets.get(packetKey);
    }

    long getTimestamp() {
        return getFirstResultPacket().getTimestamp();
    }

    public void setTimestamp(long timestamp) {
        getFirstResultPacket().setTimestamp(timestamp);
    }

    public int getPacketsSize() {
        int size = 0;

        for (Iterator<BasicPacket> i = resultPackets.iterator(); i.hasNext();) {
            QueryResultPacket r = (QueryResultPacket) i.next();
            int l = r.getLength();

            if (l < 0) {
                log.warning("resultpacket length " + l);
                l = 10240;
            }
            size += l;
        }
        for (Iterator<BasicPacket> i = packets.values().iterator(); i.hasNext();) {
            BasicPacket packet = i.next();
            int l = packet.getLength();

            if (l < 0) {
                log.warning("BasicPacket length " + l);
                l = 10240;
            }
            size += l;
        }
        size += keySize;
        return size;
    }

    /**
     * Straightforward shallow copy.
     */
    @SuppressWarnings("unchecked")
    public Object clone() {
        try {
            PacketWrapper other = (PacketWrapper) super.clone();
            other.resultPackets = (ArrayList<BasicPacket>) resultPackets.clone();
            if (packets != null) {
                other.packets = (LinkedHashMap<DocsumPacketKey, BasicPacket>) packets.clone();
            }
            return other;
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException("A non-cloneable superclass has been inserted.",
                    e);
        }
    }
}