-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSzCoreDiagnostic.java
194 lines (167 loc) · 6.06 KB
/
SzCoreDiagnostic.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.senzing.g2.engine;
import static com.senzing.g2.engine.SzException.*;
/**
* The package-protected implementation of {@link SzDiagnostic} that works with the {@link
* SzCoreEnvironment} class.
*/
public class SzCoreDiagnostic implements SzDiagnostic {
/** Gets the class prefix to use for {@link SzException} construction. */
private static final String CLASS_PREFIX = SzCoreDiagnostic.class.getSimpleName();
/** The {@link SzCoreEnvironment} that constructed this instance. */
private SzCoreEnvironment env = null;
/** The underlying {@link G2DiagnosticJNI} instance. */
G2DiagnosticJNI nativeApi = null;
/**
* Constructs with the specified {@link SzCoreEnvironment}.
*
* @param environment The {@link SzCoreEnvironment} with which to construct.
* @throws IllegalStateException If the underlying {@link SzCoreEnvironment} instance has already
* been destroyed.
* @throws SzException If a Senzing failure occurs during initialization.
*/
SzCoreDiagnostic(SzCoreEnvironment environment) throws IllegalStateException, SzException {
this.env = environment;
this.env.execute(
() -> {
this.nativeApi = new G2DiagnosticJNI();
// check if we are initializing with a config ID
if (this.env.getConfigId() == null) {
// if not then call the basic init
int returnCode =
this.nativeApi.init(
this.env.getInstanceName(),
this.env.getSettings(),
this.env.isVerboseLogging());
// handle any failure
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
CLASS_PREFIX + "()",
paramsOf(
"instanceName",
this.env.getInstanceName(),
"settings",
redact(this.env.getSettings()),
"verboseLogging",
this.env.isVerboseLogging()));
}
} else {
// if so then call init with config ID
int returnCode =
this.nativeApi.initWithConfigID(
this.env.getInstanceName(),
this.env.getSettings(),
this.env.getConfigId(),
this.env.isVerboseLogging());
// handle any failure
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
CLASS_PREFIX + "()",
paramsOf(
"instanceName",
this.env.getInstanceName(),
"settings",
redact(this.env.getSettings()),
"configId",
this.env.getConfigId(),
"verboseLogging",
this.env.isVerboseLogging()));
}
}
return null;
});
}
/** The package-protected function to destroy the Senzing Config SDK. */
void destroy() {
synchronized (this) {
if (this.nativeApi == null) return;
this.nativeApi.destroy();
this.nativeApi = null;
}
}
/**
* Checks if this instance has been destroyed by the associated {@link SzEnvironment}.
*
* @return <code>true</code> if this instance has been destroyed, otherwise <code>false</code>.
*/
protected boolean isDestroyed() {
synchronized (this) {
return (this.nativeApi == null);
}
}
@Override
public String getDatastoreInfo() throws SzException {
return this.env.execute(
() -> {
// declare the buffer for the result
StringBuffer sb = new StringBuffer();
// call the underlying C function
int returnCode = this.nativeApi.getDatastoreInfo(sb);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode, this.nativeApi, CLASS_PREFIX + ".getDatastoreInfo()");
}
// return null
return sb.toString();
});
}
@Override
public String checkDatastorePerformance(int secondsToRun) throws SzException {
return this.env.execute(
() -> {
// declare the buffer for the result
StringBuffer sb = new StringBuffer();
// call the underlying C function
int returnCode = this.nativeApi.checkDatastorePerformance(secondsToRun, sb);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
CLASS_PREFIX + ".checkDatastorePerformance(int)",
paramsOf("secondsToRun", secondsToRun));
}
// return null
return sb.toString();
});
}
@Override
public String getFeature(long featureId) throws SzException {
return this.env.execute(
() -> {
// declare the buffer for the result
StringBuffer sb = new StringBuffer();
// call the underlying C function
int returnCode = this.nativeApi.getFeature(featureId, sb);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
CLASS_PREFIX + ".getFeature(long)",
paramsOf("featureId", featureId));
}
// return null
return sb.toString();
});
}
@Override
public void purgeRepository() throws SzException {
this.env.execute(
() -> {
// call the underlying C function
int returnCode = this.nativeApi.purgeRepository();
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode, this.nativeApi, CLASS_PREFIX + ".purgeRepository()");
}
// return null
return null;
});
}
}