-
Notifications
You must be signed in to change notification settings - Fork 4
/
CoreUtil.cs
229 lines (201 loc) · 6.64 KB
/
CoreUtil.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
using System;
using System.Text;
using System.Collections.Generic;
using TileDB.Interop;
using TileDB.CSharp.Marshalling;
namespace TileDB.CSharp;
/// <summary>
/// Contains general utility functions.
/// </summary>
public static class CoreUtil
{
private static Version? _coreLibVersion;
private static string? _buildConfiguration;
/// <summary>
/// Returns the version of the TileDB Embedded binary being used.
/// </summary>
public static Version GetCoreLibVersion()
{
return _coreLibVersion ??= GetVersion();
static unsafe Version GetVersion()
{
int major, minor, rev;
Methods.tiledb_version(&major, &minor, &rev);
return new Version(major, minor, rev);
}
}
/// <summary>
/// Returns a string describing the build configuration of the TileDB Embedded binary being used.
/// </summary>
/// <remarks>
/// This method exposes the <c>tiledb_as_built_dump</c> function of the TileDB Embedded C API.
/// </remarks>
public static string GetBuildConfiguration()
{
return _buildConfiguration ??= Create();
static unsafe string Create()
{
using var resultHolder = new StringHandleHolder();
ErrorHandling.ThrowOnError(Methods.tiledb_as_built_dump(&resultHolder._handle));
return resultHolder.ToString();
}
}
#region Buffer
/// <summary>
/// Convert element offsets to byte offsets.
/// </summary>
/// <param name="element_offsets"></param>
/// <returns></returns>
public static ulong[] ElementOffsetsToByteOffsets(ulong[] element_offsets, DataType dataType)
{
ulong[] ret = new ulong[element_offsets.Length];
for (var i = 0; i < element_offsets.Length; ++i)
{
ret[i] = element_offsets[i] * EnumUtil.DataTypeSize(dataType);
}
return ret;
}
/// <summary>
/// Convert byte offsets to element offsets
/// </summary>
/// <param name="offsets"></param>
/// <returns></returns>
public static ulong[] ByteOffsetsToElementOffsets(ulong[] offsets, DataType dataType)
{
ulong[] ret = new ulong[offsets.Length];
for (var i = 0; i < offsets.Length; ++i)
{
ret[i] = offsets[i] / EnumUtil.DataTypeSize(dataType);
}
return ret;
}
/// <summary>
/// Pack string array.
/// </summary>
/// <param name="strarray"></param>
/// <param name="encodingmethod"></param>
/// <returns></returns>
public static (byte[] data, ulong[] offsets) PackStringArray(string[] strarray, string encodingmethod = "ASCII")
{
if (strarray.Length == 0)
{
return (new byte[0], new ulong[0]);
}
int totsize = 0;
for (int i = 0; i < strarray.Length; ++i)
{
int bytes_size = 0;
if (encodingmethod == "ASCII")
{
bytes_size = Encoding.ASCII.GetByteCount(strarray[i]);
}
else if (encodingmethod == "UTF16")
{
bytes_size = Encoding.Unicode.GetByteCount(strarray[i]);
}
else if (encodingmethod == "UTF32")
{
bytes_size = Encoding.UTF32.GetByteCount(strarray[i]);
}
else
{
bytes_size = Encoding.UTF8.GetByteCount(strarray[i]);
}
totsize += bytes_size;
}
byte[] data = new byte[totsize];
ulong[] offsets = new ulong[strarray.Length];
offsets[0] = 0;
int idx = 0;
for (int i = 0; i < strarray.Length; ++i)
{
byte[] bytes;
if (encodingmethod == "ASCII")
{
bytes = Encoding.ASCII.GetBytes(strarray[i]);
}
else if (encodingmethod == "UTF16")
{
bytes = Encoding.Unicode.GetBytes(strarray[i]);
}
else if (encodingmethod == "UTF32")
{
bytes = Encoding.UTF32.GetBytes(strarray[i]);
}
else
{
bytes = Encoding.UTF8.GetBytes(strarray[i]);
}
bytes.CopyTo(data, idx);
idx += bytes.Length;
if (i < (strarray.Length - 1))
{
offsets[i + 1] = offsets[i] + (ulong)bytes.Length;
}
}
return (data, offsets);
}
/// <summary>
/// Unpack string array.
/// </summary>
/// <param name="data"></param>
/// <param name="offsets"></param>
/// <param name="validities"></param>
/// <param name="encodingmethod"></param>
/// <returns></returns>
/// <exception cref="System.ArgumentException"></exception>
public static List<string> UnPackStringArray(byte[] data, ulong[] offsets, byte[]? validities = null, string encodingmethod = "ASCII")
{
List<string> result = new List<string>();
int tot_bytes_size = data.Length;
int length = offsets.Length;
bool is_nullable = (validities != null);
if (is_nullable && (validities?.Length != offsets.Length))
{
throw new System.ArgumentException("CoreUtil.UnpackStringArray, the lengths of offsets and validities are not equal!");
}
int totsize = 0;
int indexFrom = 0;
for (int i = 0; i < length; ++i)
{
if (is_nullable && validities?[i] == 0)
{
continue;
}
int bytesize = 0;
if (i < (length - 1))
{
bytesize = (int)(offsets[i + 1] - offsets[i]);
}
else
{
bytesize = tot_bytes_size - (int)offsets[length - 1];
}
totsize += bytesize;
if (totsize <= tot_bytes_size && bytesize > 0)
{
byte[] bytes = new byte[bytesize];
System.Array.Copy(data, indexFrom, bytes, 0, bytesize);
if (encodingmethod == "ASCII")
{
result.Add(Encoding.ASCII.GetString(bytes));
}
else if (encodingmethod == "UTF16")
{
result.Add(Encoding.Unicode.GetString(bytes));
}
else if (encodingmethod == "UTF32")
{
result.Add(Encoding.UTF32.GetString(bytes));
}
else
{
result.Add(Encoding.UTF8.GetString(bytes));
}
indexFrom = totsize;
}
} //for
return result;
}
#endregion Buffer
}