summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/RankingConstant.java
blob: 7b7265e02aed10e0d596889aa02c94181a643e38 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;

import com.yahoo.config.FileReference;
import com.yahoo.tensor.TensorType;
import com.yahoo.vespa.model.AbstractService;
import com.yahoo.vespa.model.utils.FileSender;

import java.util.Collection;
import java.util.Objects;

/**
 * A global ranking constant distributed using file distribution.
 * Ranking constants must be sent to some services to be useful - this is done
 * by calling the sentTo method during the prepare phase of building models.
 *
 * @author arnej
 * @author bratseth
 */
public class RankingConstant {

    public enum PathType {FILE, URI};

    /** The search definition-unique name of this constant */
    private final String name;
    private TensorType tensorType = null;
    private String path = null;
    private String fileReference = "";

    public PathType getPathType() {
        return pathType;
    }

    private PathType pathType = PathType.FILE;

    public RankingConstant(String name) {
        this.name = name;
    }

    public RankingConstant(String name, TensorType type, String fileName) {
        this(name);
        this.tensorType = type;
        this.path = fileName;
        validate();
    }

    public void setFileName(String fileName) {
        Objects.requireNonNull(fileName, "Filename cannot be null");
        this.path = fileName;
        this.pathType = PathType.FILE;
    }

    public void setUri(String uri) {
        Objects.requireNonNull(uri, "uri cannot be null");
        this.path = uri;
        this.pathType = PathType.URI;
    }

    public void setType(TensorType tensorType) { this.tensorType = tensorType; }

    /** Initiate sending of this constant to some services over file distribution */
    public void sendTo(Collection<? extends AbstractService> services) {
        FileReference reference = (pathType == RankingConstant.PathType.FILE)
                                  ? FileSender.sendFileToServices(path, services)
                                  : FileSender.sendUriToServices(path, services);
        this.fileReference = reference.value();
    }

    public String getName() { return name; }
    public String getFileName() { return path; }
    public String getUri() { return path; }
    public String getFileReference() { return fileReference; }
    public TensorType getTensorType() { return tensorType; }
    public String getType() { return tensorType.toString(); }

    public void validate() {
        if (path == null || path.isEmpty())
            throw new IllegalArgumentException("Ranking constants must have a file or uri.");
        if (tensorType == null)
            throw new IllegalArgumentException("Ranking constant '" + name + "' must have a type.");
    }

    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append("constant '").append(name)
         .append(pathType == PathType.FILE ? "' from file '" : " from uri ").append(path)
         .append("' with ref '").append(fileReference)
         .append("' of type '").append(tensorType)
         .append("'");
        return b.toString();
    }

}