-
Notifications
You must be signed in to change notification settings - Fork 4
/
VFS.cs
495 lines (454 loc) · 19 KB
/
VFS.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using TileDB.CSharp.Marshalling.SafeHandles;
using TileDB.Interop;
using ConfigHandle = TileDB.CSharp.Marshalling.SafeHandles.ConfigHandle;
using VFSHandle = TileDB.CSharp.Marshalling.SafeHandles.VFSHandle;
namespace TileDB.CSharp;
/// <summary>
/// Represents a TileDB VFS (Virtual File System) object.
/// </summary>
public unsafe sealed class VFS : IDisposable
{
private readonly VFSHandle handle_;
private readonly Context ctx_;
/// <summary>
/// Creates a <see cref="VFS"/>.
/// </summary>
public VFS() : this(null, null) { }
/// <summary>
/// Creates a <see cref="VFS"/> associated with the given <see cref="Context"/>.
/// </summary>
/// <param name="ctx">The context to associate the VFS with.</param>
public VFS(Context ctx) : this(ctx, null) { }
/// <summary>
/// Creates a <see cref="VFS"/> associated with the given <see cref="Context"/>.
/// </summary>
/// <param name="ctx">The context to associate the VFS with. Defaults to <see cref="Context.GetDefault"/></param>
/// <param name="config">The <see cref="VFS"/>' <see cref="CSharp.Config"/>. Defaults to <paramref name="ctx"/>'s config.</param>
#pragma warning disable S3427 // Method overloads with default parameter values should not overlap
public VFS(Context? ctx = null, Config? config = null)
#pragma warning restore S3427 // Method overloads with default parameter values should not overlap
{
ctx_ = ctx ?? Context.GetDefault();
handle_ = VFSHandle.Create(ctx_, config?.Handle);
}
/// <summary>
/// Disposes this <see cref="VFS"/>.
/// </summary>
public void Dispose()
{
handle_.Dispose();
}
/// <summary>
/// Gets the <see cref="Config"/> associated with this <see cref="VFS"/>.
/// </summary>
public Config Config()
{
var handle = new ConfigHandle();
tiledb_config_t* config = null;
var successful = false;
try
{
using var ctxHandle = ctx_.Handle.Acquire();
using var vfsHandle = handle_.Acquire();
ctx_.handle_error(Methods.tiledb_vfs_get_config(ctxHandle, vfsHandle, &config));
successful = true;
}
finally
{
if (successful)
{
handle.InitHandle(config);
}
else
{
handle.SetHandleAsInvalid();
}
}
return new Config(handle);
}
/// <summary>
/// Creates an object-store bucket.
/// </summary>
/// <param name="uri">The URI of the bucket to be created.</param>
public void CreateBucket(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ctx_.handle_error(Methods.tiledb_vfs_create_bucket(ctxHandle, handle, ms_uri));
}
/// <summary>
/// Removes an object-store bucket.
/// </summary>
/// <param name="uri">The URI of the bucket to be removed.</param>
public void RemoveBucket(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ctx_.handle_error(Methods.tiledb_vfs_remove_bucket(ctxHandle, handle, ms_uri));
}
/// <summary>
/// Deletes the contents of an object-store bucket.
/// </summary>
/// <param name="uri">The URI of the bucket to be emptied.</param>
public void EmptyBucket(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ctx_.handle_error(Methods.tiledb_vfs_empty_bucket(ctxHandle, handle, ms_uri));
}
/// <summary>
/// Checks whether a bucket is empty or not.
/// </summary>
/// <param name="uri">The URI of the bucket to be checked.</param>
public bool IsEmptyBucket(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
int is_empty = 0;
ctx_.handle_error(Methods.tiledb_vfs_is_empty_bucket(ctxHandle, handle, ms_uri, &is_empty));
return is_empty > 0;
}
/// <summary>
/// Checks whether a URI points to a bucket.
/// </summary>
/// <param name="uri">The URI to check.</param>
public bool IsBucket(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
int is_bucket = 0;
ctx_.handle_error(Methods.tiledb_vfs_is_bucket(ctxHandle, handle, ms_uri, &is_bucket));
return is_bucket > 0;
}
/// <summary>
/// Creates a directory.
/// </summary>
/// <param name="uri">The directory's URI.</param>
public void CreateDir(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ctx_.handle_error(Methods.tiledb_vfs_create_dir(ctxHandle, handle, ms_uri));
}
/// <summary>
/// Checks whether a URI points to a directory.
/// </summary>
/// <param name="uri">The URI to check.</param>
public bool IsDir(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
int is_dir = 0;
ctx_.handle_error(Methods.tiledb_vfs_is_dir(ctxHandle, handle, ms_uri, &is_dir));
return is_dir > 0;
}
/// <summary>
/// Removes a directory.
/// </summary>
/// <param name="uri">The directory's URI.</param>
public void RemoveDir(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ctx_.handle_error(Methods.tiledb_vfs_remove_dir(ctxHandle, handle, ms_uri));
}
/// <summary>
/// Checks whether a URI points to a file.
/// </summary>
/// <param name="uri">The URI to check.</param>
public bool IsFile(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
int is_file = 0;
ctx_.handle_error(Methods.tiledb_vfs_is_file(ctxHandle, handle, ms_uri, &is_file));
return is_file > 0;
}
/// <summary>
/// Removes a file.
/// </summary>
/// <param name="uri">The file's URI.</param>
public void RemoveFile(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ctx_.handle_error(Methods.tiledb_vfs_remove_file(ctxHandle, handle, ms_uri));
}
/// <summary>
/// Gets the size of a directory.
/// </summary>
/// <param name="uri">The directory's URI.</param>
public ulong DirSize(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ulong size = 0;
ctx_.handle_error(Methods.tiledb_vfs_dir_size(ctxHandle, handle, ms_uri, &size));
return size;
}
/// <summary>
/// Gets the size of a file.
/// </summary>
/// <param name="uri">The file's URI.</param>
public ulong FileSize(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ulong size = 0;
ctx_.handle_error(Methods.tiledb_vfs_file_size(ctxHandle, handle, ms_uri, &size));
return size;
}
/// <summary>
/// Renames a file.
/// </summary>
/// <param name="old_uri">The source URI of the file.</param>
/// <param name="new_uri">The destination URI of the file. Will be overwritten if it exists.</param>
public void MoveFile(string old_uri, string new_uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_old_uri = new MarshaledString(old_uri);
using var ms_new_uri = new MarshaledString(new_uri);
ctx_.handle_error(Methods.tiledb_vfs_move_file(ctxHandle, handle, ms_old_uri, ms_new_uri));
}
/// <summary>
/// Renames a directory.
/// </summary>
/// <param name="old_uri">The source URI of the directory.</param>
/// <param name="new_uri">The destination URI of the directory. Will be overwritten if it exists.</param>
public void MoveDir(string old_uri, string new_uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_old_uri = new MarshaledString(old_uri);
using var ms_new_uri = new MarshaledString(new_uri);
ctx_.handle_error(Methods.tiledb_vfs_move_dir(ctxHandle, handle, ms_old_uri, ms_new_uri));
}
/// <summary>
/// Copies a file.
/// </summary>
/// <param name="old_uri">The source URI of the file.</param>
/// <param name="new_uri">The destination URI of the file. Will be overwritten if it exists.</param>
public void CopyFile(string old_uri, string new_uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_old_uri = new MarshaledString(old_uri);
using var ms_new_uri = new MarshaledString(new_uri);
ctx_.handle_error(Methods.tiledb_vfs_copy_file(ctxHandle, handle, ms_old_uri, ms_new_uri));
}
/// <summary>
/// Copies a directory.
/// </summary>
/// <param name="old_uri">The source URI of the directory.</param>
/// <param name="new_uri">The destination URI of the directory. Will be overwritten if it exists.</param>
public void CopyDir(string old_uri, string new_uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_old_uri = new MarshaledString(old_uri);
using var ms_new_uri = new MarshaledString(new_uri);
ctx_.handle_error(Methods.tiledb_vfs_copy_dir(ctxHandle, handle, ms_old_uri, ms_new_uri));
}
/// <summary>
/// Touches a file, i.e., creates a new empty file.
/// </summary>
/// <param name="uri">The file's URI.</param>
public void Touch(string uri)
{
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
ctx_.handle_error(Methods.tiledb_vfs_touch(ctxHandle, handle, ms_uri));
}
/// <summary>
/// Opens a file.
/// </summary>
/// <param name="uri">The file's URI.</param>
/// <param name="mode">The mode in which the file is opened.</param>
/// <returns>A <see cref="VFSFile"/> object that can be used to perform operations on the file.</returns>
public VFSFile Open(string uri, VfsMode mode)
{
VFSFileHandle handle = VFSFileHandle.Open(ctx_, handle_, uri, (tiledb_vfs_mode_t)mode);
return new(ctx_, handle);
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int VisitCallback(sbyte* uriPtr, void* data)
{
string uri = MarshaledStringOut.GetStringFromNullTerminated(uriPtr);
VisitCallbackData* callbackData = (VisitCallbackData*)data;
Debug.Assert(callbackData->Exception is null);
bool shouldContinue;
try
{
shouldContinue = callbackData->Invoke(uri);
}
catch (Exception e)
{
callbackData->Exception = ExceptionDispatchInfo.Capture(e);
shouldContinue = false;
}
return shouldContinue ? 1 : 0;
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int VisitRecursiveCallback(sbyte* uriPtr, nuint uriSize, ulong size, void* data)
{
string uri = MarshaledStringOut.GetStringFromNullTerminated(uriPtr);
VisitRecursiveCallbackData* callbackData = (VisitRecursiveCallbackData*)data;
Debug.Assert(callbackData->Exception is null);
bool shouldContinue;
try
{
shouldContinue = callbackData->Invoke(uri, size);
}
catch (Exception e)
{
callbackData->Exception = ExceptionDispatchInfo.Capture(e);
shouldContinue = false;
}
return shouldContinue ? 1 : 0;
}
/// <summary>
/// Lists the top-level children of a directory.
/// </summary>
/// <param name="uri">The URI of the directory.</param>
/// <returns>A <see cref="List{T}"/> containing the children of the directory in <paramref name="uri"/>.</returns>
/// <remarks>
/// This function does not visit the children recursively; only the
/// top-level children of <paramref name="uri"/> will be visited.
/// </remarks>
public List<string> GetChildren(string uri)
{
var list = new List<string>();
VisitChildren(uri, static (uri, list) =>
{
list.Add(uri);
return true;
}, list);
return list;
}
/// <summary>
/// Lists all files of a directory and its subdirectories recursively.
/// </summary>
/// <param name="uri">The URI of the directory.</param>
/// <returns>A <see cref="List{T}"/> containing the files of the directory in <paramref name="uri"/>
/// and their sizes.</returns>
/// <remarks>
/// This operation is supported only on URIs to AWS S3.
/// </remarks>
public List<(string Uri, ulong Size)> GetChildrenRecursive(string uri)
{
var list = new List<(string Uri, ulong Size)>();
VisitChildrenRecursive(uri, static (uri, size, list) =>
{
list.Add((uri, size));
return true;
}, list);
return list;
}
/// <summary>
/// Visits the top-level children of a directory.
/// </summary>
/// <param name="uri">The URI of the directory to visit.</param>
/// <param name="callback">A callback delegate that will be called with the URI of each child and
/// <paramref name="callbackArg"/>, and returns whether to continue visiting.</param>
/// <param name="callbackArg">An argument that will be passed to <paramref name="callback"/>.</param>
/// <typeparam name="T">The type of <paramref name="callbackArg"/>.</typeparam>
/// <remarks>
/// This function does not visit the children recursively; only the
/// top-level children of <paramref name="uri"/> will be visited.
/// </remarks>
public void VisitChildren<T>(string uri, Func<string, T, bool> callback, T callbackArg)
{
ValueTuple<Func<string, T, bool>, T> data = (callback, callbackArg);
var callbackData = new VisitCallbackData()
{
Callback = static (uri, arg) =>
{
var dataPtr = (ValueTuple<Func<string, T, bool>, T>*)arg;
return dataPtr->Item1(uri, dataPtr->Item2);
},
CallbackArgument = (IntPtr)(&data)
};
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
// Taking a pointer to callbackData is safe; the callback will be invoked only
// during the call to tiledb_vfs_ls. Contrast this with tiledb_query_submit_async where we
// had to use a GCHandle because the callback might be invoked after we return from it.
// We also are not susceptible to GC holes; callbackData is in the stack and won't be moved around.
ctx_.handle_error(Methods.tiledb_vfs_ls(ctxHandle, handle, ms_uri, &VisitCallback, &callbackData));
callbackData.Exception?.Throw();
}
/// <summary>
/// Visits all files of a directory and its subdirectories recursively.
/// </summary>
/// <param name="uri">The URI of the directory to visit.</param>
/// <param name="callback">A callback delegate that will be called with the URI of each file,
/// its size and <paramref name="callbackArg"/>, and returns whether to continue visiting.</param>
/// <param name="callbackArg">An argument that will be passed to <paramref name="callback"/>.</param>
/// <typeparam name="T">The type of <paramref name="callbackArg"/>.</typeparam>
/// <remarks>
/// This operation is supported only on local filesystem and AWS S3 URIs.
/// </remarks>
public void VisitChildrenRecursive<T>(string uri, Func<string, ulong, T, bool> callback, T callbackArg)
{
ValueTuple<Func<string, ulong, T, bool>, T> data = (callback, callbackArg);
var callbackData = new VisitRecursiveCallbackData()
{
Callback = static (uri, size, arg) =>
{
var dataPtr = (ValueTuple<Func<string, ulong, T, bool>, T>*)arg;
return dataPtr->Item1(uri, size, dataPtr->Item2);
},
CallbackArgument = (IntPtr)(&data)
};
using var ctxHandle = ctx_.Handle.Acquire();
using var handle = handle_.Acquire();
using var ms_uri = new MarshaledString(uri);
// Taking a pointer to callbackData is safe; the callback will be invoked only
// during the call to tiledb_vfs_ls_recursive. Contrast this with tiledb_query_submit_async where we
// had to use a GCHandle because the callback might be invoked after we return from it.
// We also are not susceptible to GC holes; callbackData is in the stack and won't be moved around.
ctx_.handle_error(Methods.tiledb_vfs_ls_recursive(ctxHandle, handle, ms_uri, &VisitRecursiveCallback, &callbackData));
callbackData.Exception?.Throw();
}
private struct VisitCallbackData
{
public Func<string, IntPtr, bool> Callback;
public IntPtr CallbackArgument;
// If the callback threw an exception we will save it here and rethrow it once we leave native code.
// The reason we do this is that throwing exceptions in P/Invoke is not portable (works only on Windows
// and because the native library is compiled with MSVC).
public ExceptionDispatchInfo? Exception;
public readonly bool Invoke(string uri) => Callback(uri, CallbackArgument);
}
private struct VisitRecursiveCallbackData
{
public Func<string, ulong, IntPtr, bool> Callback;
public IntPtr CallbackArgument;
// If the callback threw an exception we will save it here and rethrow it once we leave native code.
// The reason we do this is that throwing exceptions in P/Invoke is not portable (works only on Windows
// and because the native library is compiled with MSVC).
public ExceptionDispatchInfo? Exception;
public readonly bool Invoke(string uri, ulong size) => Callback(uri, size, CallbackArgument);
}
}