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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import com.yahoo.fs4.PongPacket;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.statistics.ElapsedTime;

/**
 * An answer from Ping.
 *
 * @author Steinar Knutsen
 */
public class Pong {

    private String pingInfo="";
    private final List<ErrorMessage> errors = new ArrayList<>(1);
    private final Optional<PongPacket> pongPacket;
    private ElapsedTime elapsed = new ElapsedTime();

    public Pong() {
        this.pongPacket = Optional.empty();
    }
    
    public Pong(ErrorMessage error) {
        errors.add(error);
        this.pongPacket = Optional.empty();
    }
    
    public Pong(PongPacket pongPacket) {
        this.pongPacket = Optional.of(pongPacket);
    }
    
    public void addError(ErrorMessage error) {
        errors.add(error);
    }

    public ErrorMessage getError(int i) {
        return errors.get(i);
    }

    public int getErrorSize() {
        return errors.size();
    }
    
    /** Returns the number of active documents in the backend responding in this Pong, if available */
    public Optional<Long> activeDocuments() {
        if ( ! pongPacket.isPresent()) return Optional.empty();
        return pongPacket.get().getActiveDocuments();
    }

    /** Returns the number of nodes which responded to this Pong, if available */
    public Optional<Integer> activeNodes() {
        if ( ! pongPacket.isPresent()) return Optional.empty();
        return pongPacket.get().getActiveNodes();
    }

    public List<ErrorMessage> getErrors() {
        return Collections.unmodifiableList(errors);
    }

    /** Returns whether there is an error or not */
    public boolean badResponse() {
        return ! errors.isEmpty();
    }

    /** Sets information about the ping used to produce this. This is included when returning the tostring of this. */
    public void setPingInfo(String pingInfo) {
        if (pingInfo==null)
            pingInfo="";
        this.pingInfo=pingInfo;
    }

    /** Returns information about the ping use, or "" (never null) if none */
    public String getPingInfo() { return pingInfo; }

    public ElapsedTime getElapsedTime() {
        return elapsed;
    }

    /** Returns a string which included the ping info (if any) and any errors added to this */
    @Override
    public String toString() {
        StringBuilder m = new StringBuilder("Result of pinging");
        if (pingInfo.length() > 0) {
            m.append(" using ");
            m.append(pingInfo);
        }
        if (errors.size() > 0)
            m.append(" ");
        for (int i = 0; i < errors.size(); i++) {
            m.append(errors.get(i).toString());
            if ( i <errors.size()-1)
                m.append(", ");
        }
        return m.toString();
    }

}