-
Notifications
You must be signed in to change notification settings - Fork 1
/
G2Engine.cs
350 lines (315 loc) · 11 KB
/
G2Engine.cs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Text;
using System.Runtime.InteropServices;
using Senzing;
namespace Senzing
{
public class G2Engine
{
[DllImport ("G2")]
static extern long G2_init(byte[] moduleName, byte[] iniParams, long verboseLogging);
public static void init(string moduleName, string iniParams, int verboseLogging) {
HandleError(G2_init(Encoding.UTF8.GetBytes(moduleName),Encoding.UTF8.GetBytes(iniParams), verboseLogging));
}
[DllImport ("G2")]
static extern long G2_reinit(long configID);
public static void reinit(long configID) {
HandleError(G2_reinit(configID));
}
[DllImport ("G2")]
static extern long G2_destroy();
public static void destroy() {
HandleError(G2_destroy());
}
struct G2_deleteRecordWithInfo_result
{
public IntPtr response;
public long returnCode;
};
[DllImport ("G2")]
static extern long G2_deleteRecord(byte[] dataSourceCode, byte[] recordID, IntPtr loadID);
[DllImport ("G2")]
static extern G2_deleteRecordWithInfo_result G2_deleteRecordWithInfo_helper(byte[] dataSourceCode, byte[] recordID, IntPtr loadID, long flags);
public static void deleteRecord(string dataSourceCode, string recordID, StringBuilder withInfo = null)
{
if (withInfo == null)
{
HandleError(G2_deleteRecord(Encoding.UTF8.GetBytes(dataSourceCode),Encoding.UTF8.GetBytes(recordID), IntPtr.Zero));
}
else
{
withInfo.Clear();
G2_deleteRecordWithInfo_result result;
result.response = IntPtr.Zero;
result.returnCode = 0;
try
{
result = G2_deleteRecordWithInfo_helper(Encoding.UTF8.GetBytes(dataSourceCode),Encoding.UTF8.GetBytes(recordID), IntPtr.Zero, 0);
HandleError(result.returnCode);
withInfo.Append(Util.UTF8toString(result.response));
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
}
struct G2_addRecordWithInfo_result
{
public IntPtr response;
public long returnCode;
};
[DllImport ("G2")]
static extern long G2_addRecord(byte[] dataSourceCode, byte[] recordID, byte[] jsonData, IntPtr loadID);
[DllImport ("G2")]
static extern G2_addRecordWithInfo_result G2_addRecordWithInfo_helper(byte[] dataSourceCode, byte[] recordID, byte[] jsonData, IntPtr loadID, long flags);
public static void addRecord(string dataSourceCode, string recordID, string jsonData, StringBuilder withInfo = null)
{
if (withInfo == null)
{
HandleError(G2_addRecord(Encoding.UTF8.GetBytes(dataSourceCode),Encoding.UTF8.GetBytes(recordID),Encoding.UTF8.GetBytes(jsonData), IntPtr.Zero));
}
else
{
withInfo.Clear();
G2_addRecordWithInfo_result result;
result.response = IntPtr.Zero;
result.returnCode = 0;
try
{
result = G2_addRecordWithInfo_helper(Encoding.UTF8.GetBytes(dataSourceCode),Encoding.UTF8.GetBytes(recordID),Encoding.UTF8.GetBytes(jsonData), IntPtr.Zero, 0);
HandleError(result.returnCode);
withInfo.Append(Util.UTF8toString(result.response));
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
}
struct G2_getEntityByEntityID_V2_result
{
public IntPtr response;
public long returnCode;
}
[DllImport ("G2")]
static extern G2_getEntityByEntityID_V2_result G2_getEntityByEntityID_V2_helper(long entityID, long flags);
public static string getEntityByEntityID(long entityID, G2EngineFlags flags = G2EngineFlags.G2_ENTITY_DEFAULT_FLAGS)
{
G2_getEntityByEntityID_V2_result result;
result.response = IntPtr.Zero;
result.returnCode = 0;
try
{
result = G2_getEntityByEntityID_V2_helper(entityID, (long)flags);
HandleError(result.returnCode);
return Util.UTF8toString(result.response);
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
struct G2_getEntityByRecordID_V2_result
{
public IntPtr response;
public long returnCode;
}
[DllImport ("G2")]
static extern G2_getEntityByRecordID_V2_result G2_getEntityByRecordID_V2_helper(byte[] dataSourceCode, byte[] recordID, long flags);
public static string getEntityByRecordID(string dataSourceCode, string recordID, G2EngineFlags flags = G2EngineFlags.G2_ENTITY_DEFAULT_FLAGS)
{
G2_getEntityByRecordID_V2_result result;
result.response = IntPtr.Zero;
try
{
result = G2_getEntityByRecordID_V2_helper(Encoding.UTF8.GetBytes(dataSourceCode),Encoding.UTF8.GetBytes(recordID), (long)flags);
HandleError(result.returnCode);
return Util.UTF8toString(result.response);
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
struct G2_getRedoRecord_result
{
public IntPtr response;
public long returnCode;
}
[DllImport ("G2")]
static extern G2_getRedoRecord_result G2_getRedoRecord_helper();
public static string getRedoRecord()
{
G2_getRedoRecord_result result;
result.response = IntPtr.Zero;
try
{
result = G2_getRedoRecord_helper();
HandleError(result.returnCode);
string rec = Util.UTF8toString(result.response);
if (string.IsNullOrEmpty(rec))
return null;
return rec;
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
struct G2_stats_result
{
public IntPtr response;
public long returnCode;
}
[DllImport ("G2")]
static extern G2_stats_result G2_stats_helper();
public static string stats()
{
G2_stats_result result;
result.response = IntPtr.Zero;
try
{
result = G2_stats_helper();
HandleError(result.returnCode);
string rec = Util.UTF8toString(result.response);
if (string.IsNullOrEmpty(rec))
return null;
return rec;
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
struct G2_processWithInfo_result
{
public IntPtr response;
public long returnCode;
};
[DllImport ("G2")]
static extern long G2_process(byte[] redoRecord);
[DllImport ("G2")]
static extern G2_processWithInfo_result G2_processWithInfo_helper(byte[] redoRecord, long flags);
public static void processRedoRecord(string redoRecord, StringBuilder withInfo = null)
{
if (withInfo == null)
{
HandleError(G2_process(Encoding.UTF8.GetBytes(redoRecord)));
}
else
{
withInfo.Clear();
G2_processWithInfo_result result;
result.response = IntPtr.Zero;
result.returnCode = 0;
try
{
result = G2_processWithInfo_helper(Encoding.UTF8.GetBytes(redoRecord), 0);
HandleError(result.returnCode);
withInfo.Append(Util.UTF8toString(result.response));
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
}
struct G2_searchByAttributes_V2_result
{
public IntPtr response;
public long returnCode;
}
[DllImport ("G2")]
static extern G2_searchByAttributes_V2_result G2_searchByAttributes_V2_helper(byte[] jsonData, long flags);
public static string searchByAttributes(string jsonData, G2EngineFlags flags = G2EngineFlags.G2_SEARCH_BY_ATTRIBUTES_DEFAULT_FLAGS)
{
G2_searchByAttributes_V2_result result;
result.response = IntPtr.Zero;
try
{
result = G2_searchByAttributes_V2_helper(Encoding.UTF8.GetBytes(jsonData), (long)flags);
HandleError(result.returnCode);
return Util.UTF8toString(result.response);
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
struct G2_howEntityByEntityID_V2_result
{
public IntPtr response;
public long returnCode;
}
[DllImport ("G2")]
static extern G2_howEntityByEntityID_V2_result G2_howEntityByEntityID_V2_helper(long entityID, long flags);
public static string howEntityByEntityID(long entityID, G2EngineFlags flags = G2EngineFlags.G2_HOW_ENTITY_DEFAULT_FLAGS)
{
G2_howEntityByEntityID_V2_result result;
result.response = IntPtr.Zero;
result.returnCode = 0;
try
{
result = G2_howEntityByEntityID_V2_helper(entityID, (long)flags);
HandleError(result.returnCode);
return Util.UTF8toString(result.response);
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
struct G2_whyEntities_V2_result
{
public IntPtr response;
public long returnCode;
}
[DllImport ("G2")]
static extern G2_whyEntities_V2_result G2_whyEntities_V2_helper(long entityID1, long entityID2, long flags);
public static string whyEntities(long entityID1, long entityID2, G2EngineFlags flags = G2EngineFlags.G2_WHY_ENTITY_DEFAULT_FLAGS)
{
G2_whyEntities_V2_result result;
result.response = IntPtr.Zero;
result.returnCode = 0;
try
{
result = G2_whyEntities_V2_helper(entityID1, entityID2, (long)flags);
HandleError(result.returnCode);
return Util.UTF8toString(result.response);
}
finally
{
Util.FreeG2Buffer(result.response);
}
}
[DllImport ("G2")]
static extern long G2_purgeRepository();
static public void purgeRepository(string secretMessage = "DO NOT DO THIS") {
if (secretMessage != "YES, ERASE ALL MY DATA AND ALL PROCESSES HAVE SHUT DOWN!!!") {
G2Exception.HandleError(87, "Invalid secretMessage");
}
HandleError(G2_purgeRepository());
}
[DllImport ("G2")]
static extern long G2_getLastException([MarshalAs(UnmanagedType.LPArray)] byte[] buf, long length);
[DllImport ("G2")]
static extern long G2_getLastExceptionCode();
[DllImport ("G2")]
static extern void G2_clearLastException();
static void HandleError(long retCode)
{
if (retCode == 0)
return;
if (retCode > 0 || retCode < -2) // there are some cross-platform int size on return issues
Console.Error.WriteLine("BAD retCode: " + retCode);
long errorCode = G2_getLastExceptionCode();
byte[] buf = new byte[4096];
if (G2_getLastException(buf, buf.Length) != 0)
G2Exception.HandleError(errorCode, System.Text.Encoding.UTF8.GetString(buf));
else
G2Exception.HandleError(errorCode, "Failed to return error message");
G2_clearLastException();
}
}
}