-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeLibrary.cs
More file actions
171 lines (159 loc) · 6.2 KB
/
NativeLibrary.cs
File metadata and controls
171 lines (159 loc) · 6.2 KB
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
namespace AVXFramework
{
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Unicode;
using YamlDotNet.Core.Tokens;
public class NativeLibrary
{
#if USE_NATIVE_LIBRARIES
// AVX-Search:
[DllImport("AVXSearch.dll", CharSet = CharSet.Ansi)]
private static extern UInt64 query_create(UInt64 client_id_1, UInt64 client_id_2, string blueprint, UInt16 span, byte lexicon, byte similarity, byte fuzzy_lemmata);
[DllImport("AVXSearch.dll")]
private static extern byte query_add_scope(UInt64 client_id_1, UInt64 client_id_2, UInt64 query_id, byte book, byte chapter, byte verse);
[DllImport("AVXSearch.dll", CharSet = CharSet.Ansi)]
private static extern string query_fetch(UInt64 client_id_1, UInt64 client_id_2, UInt64 query_id);
[DllImport("AVXSearch.dll", CharSet = CharSet.Ansi)]
private static extern string chapter_fetch(UInt64 client_id_1, UInt64 client_id_2, UInt64 query_id, byte book);
[DllImport("AVXSearch.dll")]
private static extern void client_release(UInt64 client_id_1, UInt64 client_id_2);
[DllImport("AVXSearch.dll")]
private static extern void query_release(UInt64 client_id_1, UInt64 client_id_2, UInt64 query_id);
private UInt64 ClientId_1
{
get
{
byte[] bytes = this.ClientId.ToByteArray();
UInt64 result = 0;
for (int i = 0; i < 8; i++)
{
result <<= 8;
result |= bytes[i];
}
return result;
}
}
private UInt64 ClientId_2
{
get
{
byte[] bytes = this.ClientId.ToByteArray();
UInt64 result = 0;
for (int i = 8; i < 16; i++)
{
result <<= 8;
result |= bytes[i];
}
return result;
}
}
private Guid ClientId;
internal NativeLibrary()
{
this.ClientId = new Guid();
}
internal UInt64 query_create(string blueprint, UInt16 span, byte lexicon, byte similarity, byte fuzzy_lemmata)
{
var result = NativeLibrary.query_create(this.ClientId_1, this.ClientId_2, blueprint, span, lexicon, similarity, fuzzy_lemmata);
return result;
}
internal string fetch_results(UInt64 query_id, byte book)
{
return NativeLibrary.chapter_fetch(this.ClientId_1, this.ClientId_2, query_id, book);
}
internal bool query_add_scope(UInt64 query_id, byte book, byte chapter, byte verse)
{
return NativeLibrary.query_add_scope(this.ClientId_1, this.ClientId_2, query_id, book, chapter, verse) == (byte)1 ? true : false;
}
internal string fetch_summary(UInt64 query_id)
{
return NativeLibrary.query_fetch(this.ClientId_1, this.ClientId_2, query_id);
}
internal void client_release()
{
NativeLibrary.client_release(this.ClientId_1, this.ClientId_2);
}
internal void query_release(UInt64 queryId) // QueryId is in the payload returned by create_query
{
NativeLibrary.query_release(this.ClientId_1, this.ClientId_2, queryId);
}
// AVX-Text
[DllImport("AVXSearch.dll")]
internal static extern UInt64 create_avxtext(byte[] path);
[DllImport("AVXSearch.dll")]
internal static extern void free_avxtext(UInt64 data);
#endif
}
public class NativeStatement
{
#if USE_NATIVE_LIBRARIES
private UInt64 AVXTextData;
private NativeLibrary External;
private UInt64 Address;
public string Summary { get; private set; } // This is the YAML representation of the TQuery object
public NativeStatement(string omega) // (full path-spec to omega file)
{
byte[] omega_utf8 = System.Text.Encoding.UTF8.GetBytes(omega);
this.AVXTextData = NativeLibrary.create_avxtext(omega_utf8);
this.External = new NativeLibrary();
this.Address = 0; // we need to extract this from the yaml/result
this.Summary = string.Empty;
}
public bool Search(string blueprint, UInt16 span, byte lexicon, byte similarity, bool fuzzy_lemmata, List<(byte book, byte chapter, byte verse)> scope)
{
this.Free();
this.Summary = string.Empty;
this.Address = this.External.query_create(blueprint, span, lexicon, similarity, fuzzy_lemmata ? (byte)1 : (byte)0);
if (this.Address != 0)
{
foreach (var spec in scope)
{
this.External.query_add_scope(this.Address, spec.book, spec.chapter, spec.verse);
}
this.Summary = this.External.fetch_summary(this.Address);
return (this.Address != 0 && !string.IsNullOrWhiteSpace(this.Summary));
}
return false;
}
public string Fetch(UInt64 client_id, byte book)
{
this.Summary = this.External.fetch_results(this.Address, book);
return this.Summary;
}
public void Free()
{
if (this.Address != 0)
this.External.query_release(this.Address);
this.Address = 0;
}
public void Release()
{
this.Free();
NativeLibrary.free_avxtext(this.AVXTextData);
}
~NativeStatement()
{
this.Release();
}
}
public class NativeText
{
private UInt64 address;
public NativeText(byte[] path)
{
this.address = NativeLibrary.create_avxtext(path);
}
public void Free()
{
if (this.address != 0)
NativeLibrary.free_avxtext(this.address);
this.address = 0;
}
~NativeText()
{
this.Free();
}
#endif
}
}