-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathlib.rs
852 lines (761 loc) · 28.2 KB
/
lib.rs
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! FFI interface for the delta kernel
//!
//! Exposes that an engine needs to call from C/C++ to interface with kernel
#[cfg(feature = "default-engine")]
use std::collections::HashMap;
use std::default::Default;
use std::os::raw::{c_char, c_void};
use std::ptr::NonNull;
use std::sync::Arc;
use tracing::debug;
use url::Url;
use delta_kernel::schema::Schema;
use delta_kernel::snapshot::Snapshot;
use delta_kernel::{DeltaResult, Engine, EngineData, Table};
use delta_kernel_ffi_macros::handle_descriptor;
// cbindgen doesn't understand our use of feature flags here, and by default it parses `mod handle`
// twice. So we tell it to ignore one of the declarations to avoid double-definition errors.
/// cbindgen:ignore
#[cfg(feature = "developer-visibility")]
pub mod handle;
#[cfg(not(feature = "developer-visibility"))]
pub(crate) mod handle;
use handle::Handle;
// The handle_descriptor macro needs this, because it needs to emit fully qualified type names. THe
// actual prod code could use `crate::`, but doc tests can't because they're not "inside" the crate.
// relies on `crate::`
extern crate self as delta_kernel_ffi;
pub mod engine_data;
pub mod engine_funcs;
pub mod error;
use error::{AllocateError, AllocateErrorFn, ExternResult, IntoExternResult};
pub mod expressions;
#[cfg(feature = "tracing")]
pub mod ffi_tracing;
pub mod scan;
pub mod schema;
#[cfg(feature = "test-ffi")]
pub mod test_ffi;
pub(crate) type NullableCvoid = Option<NonNull<c_void>>;
/// Model iterators. This allows an engine to specify iteration however it likes, and we simply wrap
/// the engine functions. The engine retains ownership of the iterator.
#[repr(C)]
pub struct EngineIterator {
// Opaque data that will be iterated over. This data will be passed to the get_next function
// each time a next item is requested from the iterator
data: NonNull<c_void>,
/// A function that should advance the iterator and return the next time from the data
/// If the iterator is complete, it should return null. It should be safe to
/// call `get_next()` multiple times if it returns null.
get_next: extern "C" fn(data: NonNull<c_void>) -> *const c_void,
}
impl Iterator for EngineIterator {
// Todo: Figure out item type
type Item = *const c_void;
fn next(&mut self) -> Option<Self::Item> {
let next_item = (self.get_next)(self.data);
if next_item.is_null() {
None
} else {
Some(next_item)
}
}
}
/// A non-owned slice of a UTF8 string, intended for arg-passing between kernel and engine. The
/// slice is only valid until the function it was passed into returns, and should not be copied.
///
/// # Safety
///
/// Intentionally not Copy, Clone, Send, nor Sync.
///
/// Whoever instantiates the struct must ensure it does not outlive the data it points to. The
/// compiler cannot help us here, because raw pointers don't have lifetimes. A good rule of thumb is
/// to always use the [`kernel_string_slice`] macro to create string slices, and to avoid returning
/// a string slice from a code block or function (since the move risks over-extending its lifetime):
///
/// ```ignore
/// # // Ignored because this code is pub(crate) and doc tests cannot compile it
/// let dangling_slice = {
/// let tmp = String::from("tmp");
/// kernel_string_slice!(tmp)
/// }
/// ```
///
/// Meanwhile, the callee must assume that the slice is only valid until the function returns, and
/// must not retain any references to the slice or its data that might outlive the function call.
#[repr(C)]
pub struct KernelStringSlice {
ptr: *const c_char,
len: usize,
}
impl KernelStringSlice {
/// Creates a new string slice from a source string. This method is dangerous and can easily
/// lead to use-after-free scenarios. The [`kernel_string_slice`] macro should be preferred as a
/// much safer alternative.
///
/// # Safety
///
/// Caller affirms that the source will outlive the statement that creates this slice. The
/// compiler cannot help as raw pointers do not have lifetimes that the compiler can
/// verify. Thus, e.g., the following incorrect code would compile and leave `s` dangling,
/// because the unnamed string arg is dropped as soon as the statement finishes executing.
///
/// ```ignore
/// # // Ignored because this code is pub(crate) and doc tests cannot compile it
/// let s = KernelStringSlice::new_unsafe(String::from("bad").as_str());
/// ```
pub(crate) unsafe fn new_unsafe(source: &str) -> Self {
let source = source.as_bytes();
Self {
ptr: source.as_ptr().cast(),
len: source.len(),
}
}
}
/// Creates a new [`KernelStringSlice`] from a string reference (which must be an identifier, to
/// ensure it is not immediately dropped). This is the safest way to create a kernel string slice.
///
/// NOTE: It is still possible to misuse the resulting kernel string slice in unsafe ways, such as
/// returning it from the function or code block that owns the string reference.
macro_rules! kernel_string_slice {
( $source:ident ) => {{
// Safety: A named source cannot immediately go out of scope, so the resulting slice must
// remain valid at least that long. Any dangerous situations will arise from the subsequent
// misuse of this string slice, not from its creation.
//
// NOTE: The `do_it` wrapper avoids an "unnecessary `unsafe` block" clippy warning in case
// the invocation site of this macro is already in an `unsafe` block. We can't just disable
// the warning with #[allow(unused_unsafe)] because expression annotation is unstable rust.
fn do_it(s: &str) -> $crate::KernelStringSlice {
unsafe { $crate::KernelStringSlice::new_unsafe(s) }
}
do_it(&$source)
}};
}
pub(crate) use kernel_string_slice;
trait TryFromStringSlice<'a>: Sized {
unsafe fn try_from_slice(slice: &'a KernelStringSlice) -> DeltaResult<Self>;
}
impl<'a> TryFromStringSlice<'a> for String {
/// Converts a kernel string slice into a `String`.
///
/// # Safety
///
/// The slice must be a valid (non-null) pointer, and must point to the indicated number of
/// valid utf8 bytes.
unsafe fn try_from_slice(slice: &'a KernelStringSlice) -> DeltaResult<Self> {
let slice: &str = unsafe { TryFromStringSlice::try_from_slice(slice) }?;
Ok(slice.into())
}
}
impl<'a> TryFromStringSlice<'a> for &'a str {
/// Converts a kernel string slice into a borrowed `str`. The result does not outlive the kernel
/// string slice it came from.
///
/// # Safety
///
/// The slice must be a valid (non-null) pointer, and must point to the indicated number of
/// valid utf8 bytes.
unsafe fn try_from_slice(slice: &'a KernelStringSlice) -> DeltaResult<Self> {
let slice = unsafe { std::slice::from_raw_parts(slice.ptr.cast(), slice.len) };
Ok(std::str::from_utf8(slice)?)
}
}
/// Allow engines to allocate strings of their own type. the contract of calling a passed allocate
/// function is that `kernel_str` is _only_ valid until the return from this function
pub type AllocateStringFn = extern "C" fn(kernel_str: KernelStringSlice) -> NullableCvoid;
// Put KernelBoolSlice in a sub-module, with non-public members, so rust code cannot instantiate it
// directly. It can only be created by converting `From<Vec<bool>>`.
mod private {
use std::ptr::NonNull;
/// Represents an owned slice of boolean values allocated by the kernel. Any time the engine
/// receives a `KernelBoolSlice` as a return value from a kernel method, engine is responsible
/// to free that slice, by calling [super::free_bool_slice] exactly once.
#[repr(C)]
pub struct KernelBoolSlice {
ptr: NonNull<bool>,
len: usize,
}
/// An owned slice of u64 row indexes allocated by the kernel. The engine is responsible for
/// freeing this slice by calling [super::free_row_indexes] once.
#[repr(C)]
pub struct KernelRowIndexArray {
ptr: NonNull<u64>,
len: usize,
}
impl KernelBoolSlice {
/// Creates an empty slice.
pub fn empty() -> KernelBoolSlice {
KernelBoolSlice {
ptr: NonNull::dangling(),
len: 0,
}
}
/// Converts this slice back into a `Vec<bool>`.
///
/// # Safety
///
/// The slice must have been originally created `From<Vec<bool>>`, and must not have been
/// already been consumed by a previous call to this method.
pub unsafe fn as_ref(&self) -> &[bool] {
if self.len == 0 {
Default::default()
} else {
unsafe { std::slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
}
}
/// Converts this slice back into a `Vec<bool>`.
///
/// # Safety
///
/// The slice must have been originally created `From<Vec<bool>>`, and must not have been
/// already been consumed by a previous call to this method.
pub unsafe fn into_vec(self) -> Vec<bool> {
if self.len == 0 {
Default::default()
} else {
Vec::from_raw_parts(self.ptr.as_ptr(), self.len, self.len)
}
}
}
impl From<Vec<bool>> for KernelBoolSlice {
fn from(val: Vec<bool>) -> Self {
let len = val.len();
let boxed = val.into_boxed_slice();
let leaked_ptr = Box::leak(boxed).as_mut_ptr();
let ptr = NonNull::new(leaked_ptr)
.expect("This should never be non-null please report this bug.");
KernelBoolSlice { ptr, len }
}
}
/// # Safety
///
/// Whenever kernel passes a [KernelBoolSlice] to engine, engine assumes ownership of the slice
/// memory, but must only free it by calling [super::free_bool_slice]. Since the global
/// allocator is threadsafe, it doesn't matter which engine thread invokes that method.
unsafe impl Send for KernelBoolSlice {}
/// # Safety
///
/// This follows the same contract as KernelBoolSlice above, engine assumes ownership of the
/// slice memory, but must only free it by calling [super::free_row_indexes]. It does not matter
/// from which thread the engine invoke that method
unsafe impl Send for KernelRowIndexArray {}
/// # Safety
///
/// If engine chooses to leverage concurrency, engine is responsible to prevent data races.
unsafe impl Sync for KernelBoolSlice {}
/// # Safety
///
/// If engine chooses to leverage concurrency, engine is responsible to prevent data races.
/// Same contract as KernelBoolSlice above
unsafe impl Sync for KernelRowIndexArray {}
impl KernelRowIndexArray {
/// Converts this slice back into a `Vec<u64>`.
///
/// # Safety
///
/// The slice must have been originally created `From<Vec<u64>>`, and must not have
/// already been consumed by a previous call to this method.
pub unsafe fn into_vec(self) -> Vec<u64> {
Vec::from_raw_parts(self.ptr.as_ptr(), self.len, self.len)
}
/// Creates an empty slice.
pub fn empty() -> KernelRowIndexArray {
Self {
ptr: NonNull::dangling(),
len: 0,
}
}
}
impl From<Vec<u64>> for KernelRowIndexArray {
fn from(vec: Vec<u64>) -> Self {
let len = vec.len();
let boxed = vec.into_boxed_slice();
let leaked_ptr = Box::leak(boxed).as_mut_ptr();
let ptr = NonNull::new(leaked_ptr)
.expect("This should never be non-null please report this bug.");
KernelRowIndexArray { ptr, len }
}
}
}
pub use private::KernelBoolSlice;
pub use private::KernelRowIndexArray;
/// # Safety
///
/// Caller is responsible for passing a valid handle.
#[no_mangle]
pub unsafe extern "C" fn free_bool_slice(slice: KernelBoolSlice) {
let vec = unsafe { slice.into_vec() };
debug!("Dropping bool slice. It is {vec:#?}");
}
/// # Safety
///
/// Caller is responsible for passing a valid handle.
#[no_mangle]
pub unsafe extern "C" fn free_row_indexes(slice: KernelRowIndexArray) {
let _ = slice.into_vec();
}
// TODO: Do we want this handle at all? Perhaps we should just _always_ pass raw *mut c_void pointers
// that are the engine data? Even if we want the type, should it be a shared handle instead?
/// an opaque struct that encapsulates data read by an engine. this handle can be passed back into
/// some kernel calls to operate on the data, or can be converted into the raw data as read by the
/// [`delta_kernel::Engine`] by calling [`get_raw_engine_data`]
#[handle_descriptor(target=dyn EngineData, mutable=true)]
pub struct ExclusiveEngineData;
/// Drop an `ExclusiveEngineData`.
///
/// # Safety
///
/// Caller is responsible for passing a valid handle as engine_data
#[no_mangle]
pub unsafe extern "C" fn free_engine_data(engine_data: Handle<ExclusiveEngineData>) {
engine_data.drop_handle();
}
// A wrapper for Engine which defines additional FFI-specific methods.
pub trait ExternEngine: Send + Sync {
fn engine(&self) -> Arc<dyn Engine>;
fn error_allocator(&self) -> &dyn AllocateError;
}
#[handle_descriptor(target=dyn ExternEngine, mutable=false)]
pub struct SharedExternEngine;
struct ExternEngineVtable {
// Actual engine instance to use
engine: Arc<dyn Engine>,
allocate_error: AllocateErrorFn,
}
impl Drop for ExternEngineVtable {
fn drop(&mut self) {
debug!("dropping engine interface");
}
}
/// # Safety
///
/// Kernel doesn't use any threading or concurrency. If engine chooses to do so, engine is
/// responsible for handling any races that could result.
unsafe impl Send for ExternEngineVtable {}
/// # Safety
///
/// Kernel doesn't use any threading or concurrency. If engine chooses to do so, engine is
/// responsible for handling any races that could result.
///
/// These are needed because anything wrapped in Arc "should" implement it
/// Basically, by failing to implement these traits, we forbid the engine from being able to declare
/// its thread-safety (because rust assumes it is not threadsafe). By implementing them, we leave it
/// up to the engine to enforce thread safety if engine chooses to use threads at all.
unsafe impl Sync for ExternEngineVtable {}
impl ExternEngine for ExternEngineVtable {
fn engine(&self) -> Arc<dyn Engine> {
self.engine.clone()
}
fn error_allocator(&self) -> &dyn AllocateError {
&self.allocate_error
}
}
/// # Safety
///
/// Caller is responsible for passing a valid path pointer.
unsafe fn unwrap_and_parse_path_as_url(path: KernelStringSlice) -> DeltaResult<Url> {
let path: &str = unsafe { TryFromStringSlice::try_from_slice(&path) }?;
let table = Table::try_from_uri(path)?;
Ok(table.location().clone())
}
/// A builder that allows setting options on the `Engine` before actually building it
#[cfg(feature = "default-engine")]
pub struct EngineBuilder {
url: Url,
allocate_fn: AllocateErrorFn,
options: HashMap<String, String>,
}
#[cfg(feature = "default-engine")]
impl EngineBuilder {
fn set_option(&mut self, key: String, val: String) {
self.options.insert(key, val);
}
}
/// Get a "builder" that can be used to construct an engine. The function
/// [`set_builder_option`] can be used to set options on the builder prior to constructing the
/// actual engine
///
/// # Safety
/// Caller is responsible for passing a valid path pointer.
#[cfg(feature = "default-engine")]
#[no_mangle]
pub unsafe extern "C" fn get_engine_builder(
path: KernelStringSlice,
allocate_error: AllocateErrorFn,
) -> ExternResult<*mut EngineBuilder> {
let url = unsafe { unwrap_and_parse_path_as_url(path) };
get_engine_builder_impl(url, allocate_error).into_extern_result(&allocate_error)
}
#[cfg(feature = "default-engine")]
fn get_engine_builder_impl(
url: DeltaResult<Url>,
allocate_fn: AllocateErrorFn,
) -> DeltaResult<*mut EngineBuilder> {
let builder = Box::new(EngineBuilder {
url: url?,
allocate_fn,
options: HashMap::default(),
});
Ok(Box::into_raw(builder))
}
/// Set an option on the builder
///
/// # Safety
///
/// Caller must pass a valid EngineBuilder pointer, and valid slices for key and value
#[cfg(feature = "default-engine")]
#[no_mangle]
pub unsafe extern "C" fn set_builder_option(
builder: &mut EngineBuilder,
key: KernelStringSlice,
value: KernelStringSlice,
) {
let key = unsafe { String::try_from_slice(&key) };
let value = unsafe { String::try_from_slice(&value) };
// TODO: Return ExternalError if key or value is invalid? (builder has an error allocator)
builder.set_option(key.unwrap(), value.unwrap());
}
/// Consume the builder and return a `default` engine. After calling, the passed pointer is _no
/// longer valid_. Note that this _consumes_ and frees the builder, so there is no need to
/// drop/free it afterwards.
///
///
/// # Safety
///
/// Caller is responsible to pass a valid EngineBuilder pointer, and to not use it again afterwards
#[cfg(feature = "default-engine")]
#[no_mangle]
pub unsafe extern "C" fn builder_build(
builder: *mut EngineBuilder,
) -> ExternResult<Handle<SharedExternEngine>> {
let builder_box = unsafe { Box::from_raw(builder) };
get_default_engine_impl(
builder_box.url,
builder_box.options,
builder_box.allocate_fn,
)
.into_extern_result(&builder_box.allocate_fn)
}
/// # Safety
///
/// Caller is responsible for passing a valid path pointer.
#[cfg(feature = "default-engine")]
#[no_mangle]
pub unsafe extern "C" fn get_default_engine(
path: KernelStringSlice,
allocate_error: AllocateErrorFn,
) -> ExternResult<Handle<SharedExternEngine>> {
let url = unsafe { unwrap_and_parse_path_as_url(path) };
get_default_default_engine_impl(url, allocate_error).into_extern_result(&allocate_error)
}
// get the default version of the default engine :)
#[cfg(feature = "default-engine")]
fn get_default_default_engine_impl(
url: DeltaResult<Url>,
allocate_error: AllocateErrorFn,
) -> DeltaResult<Handle<SharedExternEngine>> {
get_default_engine_impl(url?, Default::default(), allocate_error)
}
/// # Safety
///
/// Caller is responsible for passing a valid path pointer.
#[cfg(feature = "sync-engine")]
#[no_mangle]
pub unsafe extern "C" fn get_sync_engine(
allocate_error: AllocateErrorFn,
) -> ExternResult<Handle<SharedExternEngine>> {
get_sync_engine_impl(allocate_error).into_extern_result(&allocate_error)
}
#[cfg(any(feature = "default-engine", feature = "sync-engine"))]
fn engine_to_handle(
engine: Arc<dyn Engine>,
allocate_error: AllocateErrorFn,
) -> Handle<SharedExternEngine> {
let engine: Arc<dyn ExternEngine> = Arc::new(ExternEngineVtable {
engine,
allocate_error,
});
engine.into()
}
#[cfg(feature = "default-engine")]
fn get_default_engine_impl(
url: Url,
options: HashMap<String, String>,
allocate_error: AllocateErrorFn,
) -> DeltaResult<Handle<SharedExternEngine>> {
use delta_kernel::engine::default::executor::tokio::TokioBackgroundExecutor;
use delta_kernel::engine::default::DefaultEngine;
let engine = DefaultEngine::<TokioBackgroundExecutor>::try_new(
&url,
options,
Arc::new(TokioBackgroundExecutor::new()),
);
Ok(engine_to_handle(Arc::new(engine?), allocate_error))
}
#[cfg(feature = "sync-engine")]
fn get_sync_engine_impl(
allocate_error: AllocateErrorFn,
) -> DeltaResult<Handle<SharedExternEngine>> {
let engine = delta_kernel::engine::sync::SyncEngine::new();
Ok(engine_to_handle(Arc::new(engine), allocate_error))
}
/// # Safety
///
/// Caller is responsible for passing a valid handle.
#[no_mangle]
pub unsafe extern "C" fn free_engine(engine: Handle<SharedExternEngine>) {
debug!("engine released engine");
engine.drop_handle();
}
#[handle_descriptor(target=Schema, mutable=false, sized=true)]
pub struct SharedSchema;
#[handle_descriptor(target=Snapshot, mutable=false, sized=true)]
pub struct SharedSnapshot;
/// Get the latest snapshot from the specified table
///
/// # Safety
///
/// Caller is responsible for passing valid handles and path pointer.
#[no_mangle]
pub unsafe extern "C" fn snapshot(
path: KernelStringSlice,
engine: Handle<SharedExternEngine>,
) -> ExternResult<Handle<SharedSnapshot>> {
let url = unsafe { unwrap_and_parse_path_as_url(path) };
let engine = unsafe { engine.as_ref() };
snapshot_impl(url, engine).into_extern_result(&engine)
}
fn snapshot_impl(
url: DeltaResult<Url>,
extern_engine: &dyn ExternEngine,
) -> DeltaResult<Handle<SharedSnapshot>> {
let snapshot = Snapshot::try_new(url?, extern_engine.engine().as_ref(), None)?;
Ok(Arc::new(snapshot).into())
}
/// # Safety
///
/// Caller is responsible for passing a valid handle.
#[no_mangle]
pub unsafe extern "C" fn free_snapshot(snapshot: Handle<SharedSnapshot>) {
debug!("engine released snapshot");
snapshot.drop_handle();
}
/// Get the version of the specified snapshot
///
/// # Safety
///
/// Caller is responsible for passing a valid handle.
#[no_mangle]
pub unsafe extern "C" fn version(snapshot: Handle<SharedSnapshot>) -> u64 {
let snapshot = unsafe { snapshot.as_ref() };
snapshot.version()
}
/// Get the logical schema of the specified snapshot
///
/// # Safety
///
/// Caller is responsible for passing a valid snapshot handle.
#[no_mangle]
pub unsafe extern "C" fn logical_schema(snapshot: Handle<SharedSnapshot>) -> Handle<SharedSchema> {
let snapshot = unsafe { snapshot.as_ref() };
Arc::new(snapshot.schema().clone()).into()
}
/// Free a schema
///
/// # Safety
/// Engine is responsible for providing a valid schema handle.
#[no_mangle]
pub unsafe extern "C" fn free_schema(schema: Handle<SharedSchema>) {
schema.drop_handle();
}
/// Get the resolved root of the table. This should be used in any future calls that require
/// constructing a path
///
/// # Safety
///
/// Caller is responsible for passing a valid handle.
#[no_mangle]
pub unsafe extern "C" fn snapshot_table_root(
snapshot: Handle<SharedSnapshot>,
allocate_fn: AllocateStringFn,
) -> NullableCvoid {
let snapshot = unsafe { snapshot.as_ref() };
let table_root = snapshot.table_root().to_string();
allocate_fn(kernel_string_slice!(table_root))
}
type StringIter = dyn Iterator<Item = String> + Send;
#[handle_descriptor(target=StringIter, mutable=true, sized=false)]
pub struct StringSliceIterator;
/// # Safety
///
/// The iterator must be valid (returned by [kernel_scan_data_init]) and not yet freed by
/// [kernel_scan_data_free]. The visitor function pointer must be non-null.
#[no_mangle]
pub unsafe extern "C" fn string_slice_next(
data: Handle<StringSliceIterator>,
engine_context: NullableCvoid,
engine_visitor: extern "C" fn(engine_context: NullableCvoid, slice: KernelStringSlice),
) -> bool {
string_slice_next_impl(data, engine_context, engine_visitor)
}
fn string_slice_next_impl(
mut data: Handle<StringSliceIterator>,
engine_context: NullableCvoid,
engine_visitor: extern "C" fn(engine_context: NullableCvoid, slice: KernelStringSlice),
) -> bool {
let data = unsafe { data.as_mut() };
if let Some(data) = data.next() {
(engine_visitor)(engine_context, kernel_string_slice!(data));
true
} else {
false
}
}
/// # Safety
///
/// Caller is responsible for (at most once) passing a valid pointer to a [`StringSliceIterator`]
#[no_mangle]
pub unsafe extern "C" fn free_string_slice_data(data: Handle<StringSliceIterator>) {
data.drop_handle();
}
// A set that can identify its contents by address
pub struct ReferenceSet<T> {
map: std::collections::HashMap<usize, T>,
next_id: usize,
}
impl<T> ReferenceSet<T> {
pub fn new() -> Self {
Default::default()
}
// Inserts a new value into the set. This always creates a new entry
// because the new value cannot have the same address as any existing value.
// Returns a raw pointer to the value. This pointer serves as a key that
// can be used later to take() from the set, and should NOT be dereferenced.
pub fn insert(&mut self, value: T) -> usize {
let id = self.next_id;
self.next_id += 1;
self.map.insert(id, value);
id
}
// Attempts to remove a value from the set, if present.
pub fn take(&mut self, i: usize) -> Option<T> {
self.map.remove(&i)
}
// True if the set contains an object whose address matches the pointer.
pub fn contains(&self, id: usize) -> bool {
self.map.contains_key(&id)
}
// The current size of the set.
pub fn len(&self) -> usize {
self.map.len()
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
}
impl<T> Default for ReferenceSet<T> {
fn default() -> Self {
Self {
map: Default::default(),
// NOTE: 0 is interpreted as None
next_id: 1,
}
}
}
#[cfg(test)]
mod tests {
use delta_kernel::engine::default::{executor::tokio::TokioBackgroundExecutor, DefaultEngine};
use object_store::{memory::InMemory, path::Path};
use test_utils::{actions_to_string, add_commit, TestAction};
use super::*;
use crate::error::{EngineError, KernelError};
#[no_mangle]
extern "C" fn allocate_err(etype: KernelError, _: KernelStringSlice) -> *mut EngineError {
let boxed = Box::new(EngineError { etype });
Box::leak(boxed)
}
#[no_mangle]
extern "C" fn allocate_str(kernel_str: KernelStringSlice) -> NullableCvoid {
let s = unsafe { String::try_from_slice(&kernel_str) };
let ptr = Box::into_raw(Box::new(s.unwrap())).cast(); // never null
let ptr = unsafe { NonNull::new_unchecked(ptr) };
Some(ptr)
}
// helper to recover a string from the above
fn recover_string(ptr: NonNull<c_void>) -> String {
let ptr = ptr.as_ptr().cast();
*unsafe { Box::from_raw(ptr) }
}
fn ok_or_panic<T>(result: ExternResult<T>) -> T {
match result {
ExternResult::Ok(t) => t,
ExternResult::Err(e) => unsafe {
panic!("Got engine error with type {:?}", (*e).etype);
},
}
}
#[test]
fn string_slice() {
let s = "foo";
let _ = kernel_string_slice!(s);
}
#[test]
fn bool_slice() {
let bools = vec![true, false, true];
let bool_slice = KernelBoolSlice::from(bools);
unsafe {
free_bool_slice(bool_slice);
}
}
pub(crate) fn get_default_engine() -> Handle<SharedExternEngine> {
let path = "memory:///doesntmatter/foo";
let path = kernel_string_slice!(path);
let builder = unsafe { ok_or_panic(get_engine_builder(path, allocate_err)) };
unsafe { ok_or_panic(builder_build(builder)) }
}
#[test]
fn engine_builder() {
let engine = get_default_engine();
unsafe {
free_engine(engine);
}
}
#[tokio::test]
async fn test_snapshot() -> Result<(), Box<dyn std::error::Error>> {
let storage = Arc::new(InMemory::new());
add_commit(
storage.as_ref(),
0,
actions_to_string(vec![TestAction::Metadata]),
)
.await?;
let engine = DefaultEngine::new(
storage.clone(),
Path::from("/"),
Arc::new(TokioBackgroundExecutor::new()),
);
let engine = engine_to_handle(Arc::new(engine), allocate_err);
let path = "memory:///";
let snapshot =
unsafe { ok_or_panic(snapshot(kernel_string_slice!(path), engine.shallow_copy())) };
let version = unsafe { version(snapshot.shallow_copy()) };
assert_eq!(version, 0);
let table_root = unsafe { snapshot_table_root(snapshot.shallow_copy(), allocate_str) };
assert!(table_root.is_some());
let s = recover_string(table_root.unwrap());
assert_eq!(&s, path);
unsafe { free_snapshot(snapshot) }
unsafe { free_engine(engine) }
Ok(())
}
#[test]
#[cfg(feature = "sync-engine")]
fn sync_engine() {
let engine = unsafe { get_sync_engine(allocate_err) };
let engine = ok_or_panic(engine);
unsafe {
free_engine(engine);
}
}
}