-
Notifications
You must be signed in to change notification settings - Fork 4
/
ArrayMetadata.cs
348 lines (305 loc) · 11 KB
/
ArrayMetadata.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
using TileDB.Interop;
namespace TileDB.CSharp;
/// <summary>
/// Provides a view of an <see cref="Array"/>'s metadata.
/// </summary>
public unsafe class ArrayMetadata : IDictionary<string, byte[]>
{
/// <summary>
/// The <see cref="Array"/> to get the metadata from.
/// </summary>
protected Array _array;
/// <summary>
/// Creates an <see cref="ArrayMetadata"/> object from an <see cref="Array"/>.
/// </summary>
/// <param name="array">The array to get the metadata from.</param>
public ArrayMetadata(Array array)
{
_array = array;
}
/// <inheritdoc cref="Array.PutMetadata{T}(string, T[])"/>
public void PutMetadata<T>(string key, T[] data) where T : struct
{
var tiledb_datatype = (tiledb_datatype_t)EnumUtil.TypeToDataType(typeof(T));
put_metadata(key, data, tiledb_datatype);
}
/// <inheritdoc cref="Array.PutMetadata{T}(string, T)"/>
public void PutMetadata<T>(string key, T v) where T : struct
{
T[] data = [v];
PutMetadata<T>(key, data);
}
/// <inheritdoc cref="Array.PutMetadata(string, string)"/>
public void PutMetadata(string key, string value)
{
var data = Encoding.UTF8.GetBytes(value);
put_metadata(key, data, tiledb_datatype_t.TILEDB_STRING_UTF8);
}
/// <inheritdoc cref="Array.DeleteMetadata"/>
public void DeleteMetadata(string key)
{
var ctx = _array.Context();
using var ctxHandle = ctx.Handle.Acquire();
using var arrayHandle = _array.Handle.Acquire();
using var ms_key = new MarshaledString(key);
ctx.handle_error(Methods.tiledb_array_delete_metadata(ctxHandle, arrayHandle, ms_key));
}
/// <inheritdoc cref="Array.GetMetadata{T}(string)"/>
public T[] GetMetadata<T>(string key) where T : struct
{
var (k, value, dataType, valueNum) = get_metadata(key);
Span<byte> valueSpan = value;
var span = MemoryMarshal.Cast<byte, T>(valueSpan);
return span.ToArray();
}
/// <inheritdoc cref="Array.GetMetadata(string)"/>
public string GetMetadata(string key)
{
var (_, byte_array, datatype, _) = get_metadata(key);
return MarshaledStringOut.GetString(byte_array, (DataType)datatype);
}
/// <inheritdoc cref="Array.MetadataNum"/>
public ulong MetadataNum()
{
var ctx = _array.Context();
ulong num;
using var ctxHandle = ctx.Handle.Acquire();
using var arrayHandle = _array.Handle.Acquire();
ctx.handle_error(Methods.tiledb_array_get_metadata_num(ctxHandle, arrayHandle, &num));
return num;
}
/// <inheritdoc cref="Array.GetMetadataFromIndex"/>
public (string key, T[] data) GetMetadataFromIndex<T>(ulong index) where T : struct
{
var (key, value, dataType, valueNum) = get_metadata_from_index(index);
Span<byte> valueSpan = value;
var span = MemoryMarshal.Cast<byte, T>(valueSpan);
return (key, span.ToArray());
}
/// <inheritdoc cref="Array.MetadataKeys"/>
public string[] MetadataKeys()
{
var num = MetadataNum();
var ret = new string[(int)num];
for (ulong i = 0; i < num; ++i)
{
var (key, _, _, _) = get_metadata_from_index(i);
ret[i] = key;
}
return ret;
}
/// <inheritdoc cref="Array.HasMetadata"/>
public (bool has_key, DataType datatype) HasMetadata(string key)
{
var ctx = _array.Context();
using var ctxHandle = ctx.Handle.Acquire();
using var arrayHandle = _array.Handle.Acquire();
using var ms_key = new MarshaledString(key);
tiledb_datatype_t tiledb_datatype;
var has_key = 0;
ctx.handle_error(Methods.tiledb_array_has_metadata_key(ctxHandle, arrayHandle, ms_key, &tiledb_datatype, &has_key));
return (has_key > 0, (DataType)tiledb_datatype);
}
/// <inheritdoc cref="P:System.Collections.Generic.IDictionary`2.Item(`0)"/>
public byte[] this[string key]
{
get
{
var ctx = _array.Context();
void* value_p;
using var ms_key = new MarshaledString(key);
tiledb_datatype_t datatype;
int has_key = 0;
using (var ctxHandle = ctx.Handle.Acquire())
using (var arrayHandle = _array.Handle.Acquire())
{
ctx.handle_error(Methods.tiledb_array_has_metadata_key(ctxHandle, arrayHandle, ms_key, &datatype, &has_key));
}
if (has_key <= 0)
{
return new byte[0];
}
uint value_num = 0;
using (var ctxHandle = ctx.Handle.Acquire())
using (var arrayHandle = _array.Handle.Acquire())
{
ctx.handle_error(Methods.tiledb_array_get_metadata(ctxHandle, arrayHandle, ms_key, &datatype, &value_num, &value_p));
}
var size = (int)(value_num * Methods.tiledb_datatype_size(datatype));
var fill_span = new ReadOnlySpan<byte>(value_p, size);
return fill_span.ToArray();
}
set
{
Add(key, value);
}
}
/// <inheritdoc cref="IDictionary{TKey, TValue}.Keys"/>
public ICollection<string> Keys
{
get
{
List<string> ret = new List<string>();
var ctx = _array.Context();
ulong num;
using (var ctxHandle = ctx.Handle.Acquire())
using (var arrayHandle = _array.Handle.Acquire())
{
ctx.handle_error(Methods.tiledb_array_get_metadata_num(ctxHandle, arrayHandle, &num));
}
for (ulong i = 0; i < num; ++i)
{
var metadata = get_metadata_from_index(i);
ret.Add(metadata.key);
}
return ret;
}
}
/// <inheritdoc cref="IDictionary{TKey, TValue}.Values"/>
public ICollection<byte[]> Values
{
get
{
List<byte[]> ret = new List<byte[]>();
ulong num = MetadataNum();
for (ulong i = 0; i < num; ++i)
{
var metadata = get_metadata_from_index(i);
ret.Add(metadata.data);
}
return ret;
}
}
/// <inheritdoc cref="ICollection{T}.Count"/>
public int Count
{
get
{
ulong num = MetadataNum();
return (int)num;
}
}
/// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
public bool IsReadOnly
{
get
{
QueryType querytype = _array.QueryType();
return querytype == QueryType.Read;
}
}
/// <inheritdoc cref="IDictionary{TKey, TValue}.Add"/>
public void Add(string key, byte[] value)
{
put_metadata(key, value, tiledb_datatype_t.TILEDB_UINT8);
}
/// <inheritdoc cref="ICollection{T}.Add"/>
public void Add(KeyValuePair<string, byte[]> item)
{
Add(item.Key, item.Value);
}
/// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
public void Clear()
{
throw new NotImplementedException();
}
/// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
public bool Contains(KeyValuePair<string, byte[]> item)
{
return ContainsKey(item.Key);
}
/// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
public bool ContainsKey(string key)
{
var (has_key, _) = HasMetadata(key);
return has_key;
}
/// <inheritdoc cref="ICollection{T}.CopyTo"/>
public void CopyTo(KeyValuePair<string, byte[]>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator()
{
throw new NotImplementedException();
}
/// <inheritdoc cref="IDictionary{TKey, TValue}.Remove"/>
public bool Remove(string key)
{
DeleteMetadata(key);
return true;
}
/// <inheritdoc cref="ICollection{T}.Remove"/>
public bool Remove(KeyValuePair<string, byte[]> item)
{
return Remove(item.Key);
}
/// <inheritdoc cref="IDictionary{TKey, TValue}.TryGetValue"/>
public bool TryGetValue(string key, [MaybeNullWhen(false)] out byte[] value)
{
if (!ContainsKey(key))
{
value = null;
return false;
}
var metadata = get_metadata(key);
value = metadata.data;
return true;
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
private void put_metadata<T>(string key, T[] value, tiledb_datatype_t tiledb_datatype) where T : struct
{
ErrorHandling.CheckDataType<T>((DataType)tiledb_datatype);
if (string.IsNullOrEmpty(key) || value.Length == 0)
{
throw new ArgumentException("ArrayMetadata.put_metadata, null or empty key-value!");
}
var ctx = _array.Context();
using var ms_key = new MarshaledString(key);
using var ctxHandle = ctx.Handle.Acquire();
using var arrayHandle = _array.Handle.Acquire();
fixed (T* dataPtr = &value[0])
ctx.handle_error(Methods.tiledb_array_put_metadata(ctxHandle, arrayHandle, ms_key, tiledb_datatype, (uint)value.Length, dataPtr));
}
private (string key, byte[] data, tiledb_datatype_t tiledb_datatype, uint value_num) get_metadata(string key)
{
var ctx = _array.Context();
void* value_p;
using var ms_key = new MarshaledString(key);
tiledb_datatype_t datatype;
uint value_num = 0;
using (var ctxHandle = ctx.Handle.Acquire())
using (var arrayHandle = _array.Handle.Acquire())
{
ctx.handle_error(Methods.tiledb_array_get_metadata(ctxHandle, arrayHandle, ms_key, &datatype, &value_num, &value_p));
}
var size = (int)(value_num * Methods.tiledb_datatype_size(datatype));
var fill_span = new ReadOnlySpan<byte>(value_p, size);
return (key, fill_span.ToArray(), datatype, value_num);
}
private (string key, byte[] data, tiledb_datatype_t tiledb_datatype, uint value_num) get_metadata_from_index(ulong index)
{
var ctx = _array.Context();
void* value_p;
sbyte* key;
tiledb_datatype_t dataType;
uint valueNum = 0;
uint key_len;
using var ctxHandle = ctx.Handle.Acquire();
using var arrayHandle = _array.Handle.Acquire();
ctx.handle_error(Methods.tiledb_array_get_metadata_from_index(ctxHandle, arrayHandle, index, &key, &key_len, &dataType, &valueNum, &value_p));
var size = (int)(valueNum * Methods.tiledb_datatype_size(dataType));
var fill_span = new ReadOnlySpan<byte>(value_p, size);
return (MarshaledStringOut.GetStringFromNullTerminated(key), fill_span.ToArray(), dataType, valueNum);
}
}