blob: a6bc3e7975d2cc2ed44ee8e6c1de6d881f86b87b (
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
|
// 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 com.yahoo.fs4.PongPacket;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.statistics.ElapsedTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* 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();
private final Optional<Long> activeDocuments;
public Pong() {
this.pongPacket = Optional.empty();
this.activeDocuments = Optional.empty();
}
public Pong(ErrorMessage error) {
errors.add(error);
this.pongPacket = Optional.empty();
this.activeDocuments = Optional.empty();
}
public Pong(PongPacket pongPacket) {
this.pongPacket = Optional.of(pongPacket);
this.activeDocuments = Optional.empty();
}
public Pong(long activeDocuments) {
this.pongPacket = Optional.empty();
this.activeDocuments = Optional.of(activeDocuments);
}
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 (activeDocuments.isPresent()) return 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();
}
}
|