comparison src/main/java/org/monetdb/merovingian/SabaothDB.java @ 391:f523727db392

Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.* This naming complies to the Java Package Naming convention as MonetDB's main website is www.monetdb.org.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 12 Nov 2020 22:02:01 +0100 (2020-11-12)
parents src/main/java/nl/cwi/monetdb/merovingian/SabaothDB.java@54137aeb1f92
children bf9f6b6ecf40
comparison
equal deleted inserted replaced
390:6199e0be3c6e 391:f523727db392
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V.
7 */
8
9 package org.monetdb.merovingian;
10
11 import java.util.Date;
12
13 /**
14 * Implementation of the Sabaoth C-struct as Java object.
15 *
16 * This Class implements a parser for the string representation of a
17 * sabaoth information struct as returned by monetdbd.
18 *
19 * Currently this class implements version 1 and 2 of the sabdb
20 * serialisation.
21 *
22 * @version 2.0
23 */
24 public class SabaothDB {
25 /** The name of the database */
26 private String dbname;
27 /** The URI how to connect to this database, or null if not
28 * shared */
29 private String uri;
30 /** Whether or not the database is under maintenance */
31 private boolean locked;
32 /** The state of this database, one of SABdbState */
33 private SABdbState state;
34 /** A list of Strings representing the available scenarios of this
35 * database */
36 private String[] scenarios;
37 /** The number of times this database was started */
38 private int startCounter;
39 /** The number of times this database was stopped */
40 private int stopCounter;
41 /** The number of times this database has crashed */
42 private int crashCounter;
43 /** The number of seconds this database was running on average */
44 private long avgUptime;
45 /** The maximum number of seconds this database was running */
46 private long maxUptime;
47 /** The minimum number of seconds this database was running */
48 private long minUptime;
49 /** The last time this database crashed, null if never */
50 private Date lastCrash;
51 /** The last time this database was started, null if never */
52 private Date lastStart;
53 /** The last time this database was stopped, null if never */
54 private Date lastStop;
55 /** Whether the last start was a crash */
56 private boolean crashAvg1;
57 /** Average of crashes in the last 10 start attempts */
58 private double crashAvg10;
59 /** Average of crashes in the last 30 start attempts */
60 private double crashAvg30;
61
62 /** The serialised format header */
63 private final String sabdbhdr = "sabdb:";
64
65 /** Sabaoth state enumeration */
66 public enum SABdbState {
67 SABdbIllegal (0),
68 SABdbRunning (1),
69 SABdbCrashed (2),
70 SABdbInactive(3),
71 SABdbStarting(4);
72
73 private final int cValue;
74
75 SABdbState(int cValue) {
76 this.cValue = cValue;
77 }
78
79 public int getcValue() {
80 return(cValue);
81 }
82
83 static SABdbState getInstance(int val) throws IllegalArgumentException {
84 /* using this syntax because I can't find examples that do
85 * it without it */
86 for (SABdbState s : SABdbState.values()) {
87 if (s.getcValue() == val) {
88 return(s);
89 }
90 }
91 throw new IllegalArgumentException("No such state with value: "
92 + val);
93 }
94 }
95
96 /**
97 * Constructs a new SabaothDB object from a String.
98 *
99 * @param sabdb the serialised sabdb C-struct
100 */
101 public SabaothDB(String sabdb)
102 throws IllegalArgumentException
103 {
104 if (sabdb == null)
105 throw new IllegalArgumentException("String is null");
106 if (!sabdb.startsWith(sabdbhdr))
107 throw new IllegalArgumentException("String is not a Sabaoth struct");
108 String[] parts = sabdb.split(":", 3);
109 if (parts.length != 3)
110 throw new IllegalArgumentException("String seems incomplete, " +
111 "expected 3 components, only found " + parts.length);
112 int protover;
113 try {
114 protover = Integer.parseInt(parts[1]);
115 } catch (NumberFormatException e) {
116 throw new IllegalArgumentException("Illegal protocol version: " +
117 parts[1]);
118 }
119 if (protover != 1 && protover != 2)
120 throw new IllegalArgumentException("Unsupported protocol version: " + protover);
121 sabdb = parts[2];
122 parts = sabdb.split(",", -2);
123 if (protover == 1 && parts.length != 16 || protover == 2 && parts.length != 17)
124 throw new IllegalArgumentException("String seems wrong, " +
125 "unexpected number of components: " + parts.length);
126
127 /* Sabaoth sabdb struct */
128 int pc = 0;
129 this.dbname = parts[pc++];
130 if (protover == 1) {
131 this.uri = "<unknown>";
132 int lastslash = this.dbname.lastIndexOf('/');
133 if (lastslash == -1)
134 throw new IllegalArgumentException("invalid path (needs slash): " + this.dbname);
135 this.dbname = this.dbname.substring(lastslash + 1);
136 } else {
137 this.uri = parts[pc++];
138 }
139 this.locked = parts[pc++].equals("1") ? true : false;
140 this.state = SABdbState.getInstance(Integer.parseInt(parts[pc++]));
141 this.scenarios = parts[pc++].split("'");
142 if (protover == 1) /* skip connections */
143 pc++;
144 /* Sabaoth sabuplog struct */
145 this.startCounter = Integer.parseInt(parts[pc++]);
146 this.stopCounter = Integer.parseInt(parts[pc++]);
147 this.crashCounter = Integer.parseInt(parts[pc++]);
148 this.avgUptime = Long.parseLong(parts[pc++]);
149 this.maxUptime = Long.parseLong(parts[pc++]);
150 this.minUptime = Long.parseLong(parts[pc++]);
151 long t = Long.parseLong(parts[pc++]);
152 if (t == -1) {
153 this.lastCrash = null;
154 } else {
155 this.lastCrash = new Date(t * 1000);
156 }
157 t = Long.parseLong(parts[pc++]);
158 if (t == -1) {
159 this.lastStart = null;
160 } else {
161 this.lastStart = new Date(t * 1000);
162 }
163 if (protover != 1) {
164 t = Long.parseLong(parts[pc++]);
165 if (t == -1) {
166 this.lastStop = null;
167 } else {
168 this.lastStop = new Date(t * 1000);
169 }
170 } else {
171 this.lastStop = null;
172 }
173 this.crashAvg1 = parts[pc++].equals("1") ? true : false;
174 this.crashAvg10 = Double.parseDouble(parts[pc++]);
175 this.crashAvg30 = Double.parseDouble(parts[pc++]);
176 }
177
178 public String getName() {
179 return(dbname);
180 }
181
182 public String getURI() {
183 return(uri);
184 }
185
186 public boolean isLocked() {
187 return(locked);
188 }
189
190 public SABdbState getState() {
191 return(state);
192 }
193
194 public String[] getScenarios() {
195 return(scenarios);
196 }
197
198 public int getStartCount() {
199 return(startCounter);
200 }
201
202 public int getStopCount() {
203 return(stopCounter);
204 }
205
206 public int getCrashCount() {
207 return(crashCounter);
208 }
209
210 public long getAverageUptime() {
211 return(avgUptime);
212 }
213
214 public long getMaximumUptime() {
215 return(maxUptime);
216 }
217
218 public long getMinimumUptime() {
219 return(minUptime);
220 }
221
222 public Date lastCrashed() {
223 return(lastCrash);
224 }
225
226 public Date lastStarted() {
227 return(lastStart);
228 }
229
230 public Date lastStopped() {
231 return(lastStop);
232 }
233
234 public boolean lastStartWasSuccessful() {
235 return(crashAvg1);
236 }
237
238 public double getCrashAverage10() {
239 return(crashAvg10);
240 }
241
242 public double getCrashAverage30() {
243 return(crashAvg30);
244 }
245 }