aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/dispatch/LeanHit.java
blob: eaf3be8075f7afba961325aa02dcfb668ebfe5f8 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch;

import com.yahoo.data.access.Inspector;
import com.yahoo.search.result.FeatureData;

import java.util.Arrays;

/**
 * @author baldersheim
 */
public class LeanHit implements Comparable<LeanHit> {

    private final byte[] gid;
    private final double relevance;
    private final byte[] sortData;
    private final int partId;
    private final int distributionKey;
    private FeatureData matchFeatures;

    public LeanHit(byte[] gid, int partId, int distributionKey, double relevance) {
        this(gid, partId, distributionKey, relevance, null);
    }
    public LeanHit(byte[] gid, int partId, int distributionKey, double relevance, byte[] sortData) {
        this.gid = gid;
        this.relevance = Double.isNaN(relevance) ? Double.NEGATIVE_INFINITY : relevance;
        this.sortData = sortData;
        this.partId = partId;
        this.distributionKey = distributionKey;
        this.matchFeatures = null;
    }

    public double getRelevance() { return relevance; }
    public byte[] getGid() { return gid; }
    public byte[] getSortData() { return sortData; }
    public boolean hasSortData() { return sortData != null; }
    public int getPartId() { return partId; }
    public int getDistributionKey() { return distributionKey; }

    public FeatureData getMatchFeatures() { return matchFeatures; }
    public boolean hasMatchFeatures() { return matchFeatures != null; }
    public void addMatchFeatures(Inspector features) {
        matchFeatures = new FeatureData(features);
    }

    @Override
    public int compareTo(LeanHit o) {
        int res = (sortData != null)
                ? compareData(sortData, o.sortData)
                : Double.compare(o.relevance, relevance);
        return (res != 0) ? res : compareData(gid, o.gid);
    }

    public static int compareData(byte[] left, byte[] right) {
        int i = Arrays.mismatch(left, right);
        if (i < 0) {
            return 0;
        }
        int max = Integer.min(left.length, right.length);
        if (i >= max) {
            return left.length - right.length;
        }
        int vl = (int) left[i] & 0xFF;
        int vr = (int) right[i] & 0xFF;
        return vl - vr;
    }

}