comparison src/main/java/nl/cwi/monetdb/merovingian/SabaothDB.java @ 0:a5a898f6886c

Copy of MonetDB java directory changeset e6e32756ad31.
author Sjoerd Mullender <sjoerd@acm.org>
date Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21)
parents
children 57978db4ee57 17fbaf2635bb
comparison
equal deleted inserted replaced
-1:000000000000 0:a5a898f6886c
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 - 2016 MonetDB B.V.
7 */
8
9 package nl.cwi.monetdb.merovingian;
10
11 import java.util.*;
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 * <br />
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 /**
98 * Constructs a new SabaothDB object from a String.
99 *
100 * @param sabdb the serialised sabdb C-struct
101 */
102 public SabaothDB(String sabdb)
103 throws IllegalArgumentException
104 {
105 if (sabdb == null)
106 throw new IllegalArgumentException("String is null");
107 if (!sabdb.startsWith(sabdbhdr))
108 throw new IllegalArgumentException("String is not a Sabaoth struct");
109 String[] parts = sabdb.split(":", 3);
110 if (parts.length != 3)
111 throw new IllegalArgumentException("String seems incomplete, " +
112 "expected 3 components, only found " + parts.length);
113 int protover;
114 try {
115 protover = Integer.parseInt(parts[1]);
116 } catch (NumberFormatException e) {
117 throw new IllegalArgumentException("Illegal protocol version: " +
118 parts[1]);
119 }
120 if (protover != 1 && protover != 2)
121 throw new IllegalArgumentException("Unsupported protocol version: " + protover);
122 sabdb = parts[2];
123 parts = sabdb.split(",", -2);
124 if (protover == 1 && parts.length != 16 || protover == 2 && parts.length != 17)
125 throw new IllegalArgumentException("String seems wrong, " +
126 "unexpected number of components: " + parts.length);
127
128 /* Sabaoth sabdb struct */
129 int pc = 0;
130 this.dbname = parts[pc++];
131 if (protover == 1) {
132 this.uri = "<unknown>";
133 int lastslash = this.dbname.lastIndexOf('/');
134 if (lastslash == -1)
135 throw new IllegalArgumentException("invalid path (needs slash): " + this.dbname);
136 this.dbname = this.dbname.substring(lastslash + 1);
137 } else {
138 this.uri = parts[pc++];
139 }
140 this.locked = parts[pc++].equals("1") ? true : false;
141 this.state = SABdbState.getInstance(Integer.parseInt(parts[pc++]));
142 this.scenarios = parts[pc++].split("'");
143 if (protover == 1) /* skip connections */
144 pc++;
145 /* Sabaoth sabuplog struct */
146 this.startCounter = Integer.parseInt(parts[pc++]);
147 this.stopCounter = Integer.parseInt(parts[pc++]);
148 this.crashCounter = Integer.parseInt(parts[pc++]);
149 this.avgUptime = Long.parseLong(parts[pc++]);
150 this.maxUptime = Long.parseLong(parts[pc++]);
151 this.minUptime = Long.parseLong(parts[pc++]);
152 long t = Long.parseLong(parts[pc++]);
153 if (t == -1) {
154 this.lastCrash = null;
155 } else {
156 this.lastCrash = new Date(t * 1000);
157 }
158 t = Long.parseLong(parts[pc++]);
159 if (t == -1) {
160 this.lastStart = null;
161 } else {
162 this.lastStart = new Date(t * 1000);
163 }
164 if (protover != 1) {
165 t = Long.parseLong(parts[pc++]);
166 if (t == -1) {
167 this.lastStop = null;
168 } else {
169 this.lastStop = new Date(t * 1000);
170 }
171 } else {
172 this.lastStop = null;
173 }
174 this.crashAvg1 = parts[pc++].equals("1") ? true : false;
175 this.crashAvg10 = Double.parseDouble(parts[pc++]);
176 this.crashAvg30 = Double.parseDouble(parts[pc++]);
177 }
178
179 public String getName() {
180 return(dbname);
181 }
182
183 public String getURI() {
184 return(uri);
185 }
186
187 public boolean isLocked() {
188 return(locked);
189 }
190
191 public SABdbState getState() {
192 return(state);
193 }
194
195 public String[] getScenarios() {
196 return(scenarios);
197 }
198
199 public int getStartCount() {
200 return(startCounter);
201 }
202
203 public int getStopCount() {
204 return(stopCounter);
205 }
206
207 public int getCrashCount() {
208 return(crashCounter);
209 }
210
211 public long getAverageUptime() {
212 return(avgUptime);
213 }
214
215 public long getMaximumUptime() {
216 return(maxUptime);
217 }
218
219 public long getMinimumUptime() {
220 return(minUptime);
221 }
222
223 public Date lastCrashed() {
224 return(lastCrash);
225 }
226
227 public Date lastStarted() {
228 return(lastStart);
229 }
230
231 public Date lastStopped() {
232 return(lastStop);
233 }
234
235 public boolean lastStartWasSuccessful() {
236 return(crashAvg1);
237 }
238
239 public double getCrashAverage10() {
240 return(crashAvg10);
241 }
242
243 public double getCrashAverage30() {
244 return(crashAvg30);
245 }
246 }