summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/fs4/DocumentInfo.java
blob: 8294ae5796da186de57e0381104e804aae105957 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fs4;

import com.yahoo.document.GlobalId;

import java.nio.ByteBuffer;

/**
 * Meta attributes on documents (not the document summaries themselves).
 * Used in query results and get docusum packages
 *
 * @author bratseth
 */
public class DocumentInfo implements Cloneable {

    private final byte [] globalId;
    private final double metric;
    private final int partId;
    private final int distributionKey;
    private final byte[] sortData;

    DocumentInfo(ByteBuffer buffer, QueryResultPacket owner, byte[] sortData) {
        globalId = new byte[GlobalId.LENGTH];
        buffer.get(globalId);
        metric = decodeMetric(buffer);
        partId = owner.getMldFeature() ? buffer.getInt() : 0;
        distributionKey = owner.getMldFeature() ? buffer.getInt() : 0;
        this.sortData = sortData;
    }

    public DocumentInfo(GlobalId globalId, int metric, int partId, int distributionKey) {
        this.globalId = globalId.getRawId();
        this.metric = metric;
        this.partId = partId;
        this.distributionKey = distributionKey;
        this.sortData = null;
    }

    private double decodeMetric(ByteBuffer buffer) {
        return buffer.getDouble();
    }

    public GlobalId getGlobalId() { return new GlobalId(globalId); }
    public byte [] getRawGlobalId() { return globalId; }

    /** Raw rank score */
    public double getMetric() { return metric; }

    /** Partition this document resides on */
    public int getPartId() { return partId; }

    /** Unique key for the node this document resides on */
    public int getDistributionKey() { return distributionKey; }

    public byte[] getSortData() {
        return sortData;
    }

    public String toString() {
        return "document info [globalId=" + new GlobalId(globalId).toString() + ", metric=" + metric + "]";
    }

   /**
     * Implements the Cloneable interface
     */
    public Object clone() {
        try {
            return super.clone();
        }
        catch (CloneNotSupportedException e) {
            throw new RuntimeException("Someone inserted a nonclonable superclass");
        }
    }
}