radixdb-plugin-host 1.1.0

Internal trusted-native plugin loader and immutable registry for RadixDB
Documentation
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
use std::{collections::BTreeMap, sync::Arc};

use radixdb_plugin_abi::{
    RadixAbiBatchFnV1, RadixAbiCodecFnV1, RadixAbiCompareFnV1, RadixAbiEqualFnV1, RadixAbiHashFnV1,
    RadixAbiKeyEncodeFnV1, RadixAbiParseFnV1, RadixAbiPlannerSupportFnV1, RadixAbiScalarFnV1,
};
use semver::Version;

pub type ObjectId = [u8; 16];

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ObjectKind {
    ExternalType,
    Function,
    Operator,
    OperatorClass,
    PlannerSupport,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RegisteredTypeRef {
    Builtin(u16),
    External {
        object_id: ObjectId,
        codec_version: u32,
    },
}

#[derive(Clone)]
pub struct RegisteredExternalType {
    pub package_id: ObjectId,
    pub object_id: ObjectId,
    pub local_id: String,
    pub display_name: String,
    pub codec_version: u32,
    pub semantic_revision: u32,
    pub storage_kind: u16,
    pub fixed_bytes: u32,
    pub max_bytes: u32,
    pub capabilities: u64,
    pub codec_fingerprint: [u8; 32],
    pub encode: RadixAbiCodecFnV1,
    pub decode: RadixAbiParseFnV1,
    pub equality: Option<RadixAbiEqualFnV1>,
    pub hash: Option<RadixAbiHashFnV1>,
    pub ordering: Option<RadixAbiCompareFnV1>,
    pub text_input: Option<RadixAbiParseFnV1>,
    pub text_output: Option<RadixAbiCodecFnV1>,
    pub binary_input: Option<RadixAbiParseFnV1>,
    pub binary_output: Option<RadixAbiCodecFnV1>,
}

#[derive(Clone)]
pub struct RegisteredFunction {
    pub package_id: ObjectId,
    pub object_id: ObjectId,
    pub local_id: String,
    pub display_name: String,
    pub semantic_revision: u32,
    pub arguments: Vec<RegisteredTypeRef>,
    pub result: RegisteredTypeRef,
    pub volatility: u16,
    pub cancellation: u16,
    pub strict: bool,
    pub parallel_safe: bool,
    pub cost: u32,
    pub max_output_bytes: u32,
    pub scalar: RadixAbiScalarFnV1,
    pub batch: Option<RadixAbiBatchFnV1>,
}

#[derive(Clone)]
pub struct RegisteredOperator {
    pub package_id: ObjectId,
    pub object_id: ObjectId,
    pub local_id: String,
    pub symbol: String,
    pub semantic_revision: u32,
    pub left: Option<RegisteredTypeRef>,
    pub right: RegisteredTypeRef,
    pub result: RegisteredTypeRef,
    pub function_id: ObjectId,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RegisteredBinding {
    pub slot: u16,
    pub object_id: ObjectId,
}

#[derive(Clone)]
pub struct RegisteredOperatorClass {
    pub package_id: ObjectId,
    pub object_id: ObjectId,
    pub local_id: String,
    pub semantic_revision: u32,
    pub access_method: u16,
    pub input_type: RegisteredTypeRef,
    pub key_type: RegisteredTypeRef,
    pub key_codec_revision: u32,
    pub strategies: Vec<RegisteredBinding>,
    pub supports: Vec<RegisteredBinding>,
    pub fingerprint: [u8; 32],
    pub encode_key: RadixAbiKeyEncodeFnV1,
}

#[derive(Clone)]
pub struct RegisteredPlannerSupport {
    pub package_id: ObjectId,
    pub object_id: ObjectId,
    pub local_id: String,
    pub semantic_revision: u32,
    pub max_spans: u32,
    pub max_output_bytes: u32,
    pub recheck_policy: u16,
    pub target_function_id: Option<ObjectId>,
    pub target_operator_class_id: Option<ObjectId>,
    pub fingerprint: [u8; 32],
    pub callback: RadixAbiPlannerSupportFnV1,
}

#[derive(Debug, Clone)]
pub struct RegisteredPackage {
    pub package_id: ObjectId,
    pub name: String,
    pub version: Version,
    pub abi_major: u16,
    pub abi_min_minor: u16,
    pub abi_max_minor: u16,
    pub abi_minor: u16,
    pub descriptor_fingerprint: [u8; 32],
}

#[cfg(any(test, feature = "test-hooks"))]
impl RegisteredPackage {
    #[doc(hidden)]
    pub fn for_test(
        package_id: ObjectId,
        name: impl Into<String>,
        version: &str,
        descriptor_fingerprint: [u8; 32],
    ) -> Self {
        Self {
            package_id,
            name: name.into(),
            version: Version::parse(version).expect("test package version must be valid SemVer"),
            abi_major: radixdb_plugin_abi::RADIX_ABI_MAJOR,
            abi_min_minor: radixdb_plugin_abi::RADIX_ABI_MINOR,
            abi_max_minor: radixdb_plugin_abi::RADIX_ABI_MINOR,
            abi_minor: radixdb_plugin_abi::RADIX_ABI_MINOR,
            descriptor_fingerprint,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PluginRegistryStatus {
    pub generation: u64,
    pub packages: usize,
    pub external_types: usize,
    pub functions: usize,
    pub operators: usize,
    pub operator_classes: usize,
    pub planner_support: usize,
    pub shadowed_versions: usize,
    pub loaded_library_bytes: u64,
}

/// A complete generation. All maps are built before this value is returned;
/// consumers receive it through `Arc` and cannot mutate its contents.
#[derive(Clone)]
pub struct PluginRegistry {
    pub(crate) generation: u64,
    pub(crate) packages: BTreeMap<ObjectId, Arc<RegisteredPackage>>,
    pub(crate) external_types: BTreeMap<ObjectId, Arc<RegisteredExternalType>>,
    pub(crate) functions: BTreeMap<ObjectId, Arc<RegisteredFunction>>,
    pub(crate) operators: BTreeMap<ObjectId, Arc<RegisteredOperator>>,
    pub(crate) operator_classes: BTreeMap<ObjectId, Arc<RegisteredOperatorClass>>,
    pub(crate) planner_support: BTreeMap<ObjectId, Arc<RegisteredPlannerSupport>>,
    pub(crate) shadowed_versions: usize,
    pub(crate) loaded_library_bytes: u64,
}

impl std::fmt::Debug for PluginRegistry {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("PluginRegistry")
            .field("status", &self.status())
            .finish_non_exhaustive()
    }
}

impl Default for PluginRegistry {
    fn default() -> Self {
        Self::empty()
    }
}

impl PluginRegistry {
    pub fn empty() -> Self {
        Self {
            generation: 0,
            packages: BTreeMap::new(),
            external_types: BTreeMap::new(),
            functions: BTreeMap::new(),
            operators: BTreeMap::new(),
            operator_classes: BTreeMap::new(),
            planner_support: BTreeMap::new(),
            shadowed_versions: 0,
            loaded_library_bytes: 0,
        }
    }

    #[cfg(any(test, feature = "test-hooks"))]
    #[doc(hidden)]
    pub fn from_test_packages(packages: impl IntoIterator<Item = RegisteredPackage>) -> Self {
        let packages = packages
            .into_iter()
            .map(|package| (package.package_id, Arc::new(package)))
            .collect();
        Self {
            generation: 1,
            packages,
            external_types: BTreeMap::new(),
            functions: BTreeMap::new(),
            operators: BTreeMap::new(),
            operator_classes: BTreeMap::new(),
            planner_support: BTreeMap::new(),
            shadowed_versions: 0,
            loaded_library_bytes: 0,
        }
    }

    #[cfg(any(test, feature = "test-hooks"))]
    #[doc(hidden)]
    #[allow(clippy::too_many_arguments)]
    pub fn from_test_objects(
        packages: impl IntoIterator<Item = RegisteredPackage>,
        external_types: impl IntoIterator<Item = RegisteredExternalType>,
        functions: impl IntoIterator<Item = RegisteredFunction>,
        operators: impl IntoIterator<Item = RegisteredOperator>,
        operator_classes: impl IntoIterator<Item = RegisteredOperatorClass>,
        planner_support: impl IntoIterator<Item = RegisteredPlannerSupport>,
    ) -> Self {
        Self {
            generation: 1,
            packages: packages
                .into_iter()
                .map(|value| (value.package_id, Arc::new(value)))
                .collect(),
            external_types: external_types
                .into_iter()
                .map(|value| (value.object_id, Arc::new(value)))
                .collect(),
            functions: functions
                .into_iter()
                .map(|value| (value.object_id, Arc::new(value)))
                .collect(),
            operators: operators
                .into_iter()
                .map(|value| (value.object_id, Arc::new(value)))
                .collect(),
            operator_classes: operator_classes
                .into_iter()
                .map(|value| (value.object_id, Arc::new(value)))
                .collect(),
            planner_support: planner_support
                .into_iter()
                .map(|value| (value.object_id, Arc::new(value)))
                .collect(),
            shadowed_versions: 0,
            loaded_library_bytes: 0,
        }
    }

    pub fn generation(&self) -> u64 {
        self.generation
    }

    pub fn package(&self, id: &ObjectId) -> Option<&Arc<RegisteredPackage>> {
        self.packages.get(id)
    }

    pub fn package_by_name_and_version(
        &self,
        name: &str,
        version: &str,
    ) -> Option<&Arc<RegisteredPackage>> {
        self.packages
            .values()
            .find(|package| package.name == name && package.version.to_string() == version)
    }

    pub fn external_type(&self, id: &ObjectId) -> Option<&Arc<RegisteredExternalType>> {
        self.external_types.get(id)
    }

    pub fn external_type_by_package_and_local_id(
        &self,
        package_id: &ObjectId,
        local_id: &str,
    ) -> Option<&Arc<RegisteredExternalType>> {
        self.external_types.values().find(|external_type| {
            &external_type.package_id == package_id && external_type.local_id == local_id
        })
    }

    pub fn function(&self, id: &ObjectId) -> Option<&Arc<RegisteredFunction>> {
        self.functions.get(id)
    }

    pub fn function_by_package_and_local_id(
        &self,
        package_id: &ObjectId,
        local_id: &str,
    ) -> Option<&Arc<RegisteredFunction>> {
        self.functions
            .values()
            .find(|function| &function.package_id == package_id && function.local_id == local_id)
    }

    pub fn operator(&self, id: &ObjectId) -> Option<&Arc<RegisteredOperator>> {
        self.operators.get(id)
    }

    pub fn operator_by_package_and_local_id(
        &self,
        package_id: &ObjectId,
        local_id: &str,
    ) -> Option<&Arc<RegisteredOperator>> {
        self.operators
            .values()
            .find(|value| &value.package_id == package_id && value.local_id == local_id)
    }

    pub fn operator_class(&self, id: &ObjectId) -> Option<&Arc<RegisteredOperatorClass>> {
        self.operator_classes.get(id)
    }

    pub fn operator_class_by_package_and_local_id(
        &self,
        package_id: &ObjectId,
        local_id: &str,
    ) -> Option<&Arc<RegisteredOperatorClass>> {
        self.operator_classes
            .values()
            .find(|value| &value.package_id == package_id && value.local_id == local_id)
    }

    pub fn planner_support(&self, id: &ObjectId) -> Option<&Arc<RegisteredPlannerSupport>> {
        self.planner_support.get(id)
    }

    pub fn planner_support_by_package_and_local_id(
        &self,
        package_id: &ObjectId,
        local_id: &str,
    ) -> Option<&Arc<RegisteredPlannerSupport>> {
        self.planner_support
            .values()
            .find(|value| &value.package_id == package_id && value.local_id == local_id)
    }

    pub fn status(&self) -> PluginRegistryStatus {
        PluginRegistryStatus {
            generation: self.generation,
            packages: self.packages.len(),
            external_types: self.external_types.len(),
            functions: self.functions.len(),
            operators: self.operators.len(),
            operator_classes: self.operator_classes.len(),
            planner_support: self.planner_support.len(),
            shadowed_versions: self.shadowed_versions,
            loaded_library_bytes: self.loaded_library_bytes,
        }
    }

    pub fn assess_requirements(
        &self,
        requirements: &[PackageRequirement],
    ) -> DatabasePluginAdmission {
        let mut issues = Vec::new();
        for requirement in requirements {
            let Some(package) = self.packages.get(&requirement.package_id) else {
                issues.push(RequirementIssue::MissingPackage {
                    package_id: requirement.package_id,
                });
                continue;
            };
            if package.version != requirement.version {
                issues.push(RequirementIssue::PackageVersion {
                    package_id: requirement.package_id,
                    required: requirement.version.clone(),
                    active: package.version.clone(),
                });
            }
            if package.abi_major != requirement.abi_major
                || package.abi_min_minor != requirement.abi_min_minor
                || package.abi_max_minor != requirement.abi_max_minor
            {
                issues.push(RequirementIssue::PackageAbi {
                    package_id: requirement.package_id,
                    required_major: requirement.abi_major,
                    required_min_minor: requirement.abi_min_minor,
                    required_max_minor: requirement.abi_max_minor,
                    active_major: package.abi_major,
                    active_min_minor: package.abi_min_minor,
                    active_max_minor: package.abi_max_minor,
                });
            }
            if package.descriptor_fingerprint != requirement.descriptor_fingerprint {
                issues.push(RequirementIssue::DescriptorFingerprint {
                    package_id: requirement.package_id,
                });
            }
            for object in &requirement.objects {
                let present = match object.kind {
                    ObjectKind::ExternalType => self
                        .external_types
                        .get(&object.object_id)
                        .is_some_and(|value| {
                            object
                                .codec_version
                                .is_none_or(|version| value.codec_version == version)
                                && object
                                    .semantic_revision
                                    .is_none_or(|revision| value.semantic_revision == revision)
                        }),
                    ObjectKind::Function => {
                        self.functions.get(&object.object_id).is_some_and(|v| {
                            object
                                .semantic_revision
                                .is_none_or(|revision| v.semantic_revision == revision)
                        })
                    }
                    ObjectKind::Operator => {
                        self.operators.get(&object.object_id).is_some_and(|v| {
                            object
                                .semantic_revision
                                .is_none_or(|revision| v.semantic_revision == revision)
                        })
                    }
                    ObjectKind::OperatorClass => self
                        .operator_classes
                        .get(&object.object_id)
                        .is_some_and(|v| {
                            object
                                .codec_version
                                .is_none_or(|revision| v.key_codec_revision == revision)
                                && object
                                    .semantic_revision
                                    .is_none_or(|revision| v.semantic_revision == revision)
                        }),
                    ObjectKind::PlannerSupport => self
                        .planner_support
                        .get(&object.object_id)
                        .is_some_and(|v| {
                            object
                                .semantic_revision
                                .is_none_or(|revision| v.semantic_revision == revision)
                        }),
                };
                if !present {
                    issues.push(RequirementIssue::MissingOrStaleObject {
                        package_id: requirement.package_id,
                        object_id: object.object_id,
                        kind: object.kind,
                    });
                }
            }
        }
        if issues.is_empty() {
            DatabasePluginAdmission::Normal
        } else {
            DatabasePluginAdmission::Restricted { issues }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackageRequirement {
    pub package_id: ObjectId,
    pub version: Version,
    pub abi_major: u16,
    pub abi_min_minor: u16,
    pub abi_max_minor: u16,
    pub descriptor_fingerprint: [u8; 32],
    pub objects: Vec<ObjectRequirement>,
}

impl PackageRequirement {
    #[allow(clippy::too_many_arguments)]
    pub fn for_package_binding(
        package_id: ObjectId,
        version: &str,
        abi_major: u16,
        abi_min_minor: u16,
        abi_max_minor: u16,
        descriptor_fingerprint: [u8; 32],
    ) -> Result<Self, String> {
        let parsed = Version::parse(version).map_err(|error| error.to_string())?;
        if parsed.to_string() != version || !parsed.build.is_empty() {
            return Err(
                "package binding version is not canonical SemVer without build metadata".to_owned(),
            );
        }
        Ok(Self {
            package_id,
            version: parsed,
            abi_major,
            abi_min_minor,
            abi_max_minor,
            descriptor_fingerprint,
            objects: Vec::new(),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObjectRequirement {
    pub object_id: ObjectId,
    pub kind: ObjectKind,
    pub codec_version: Option<u32>,
    pub semantic_revision: Option<u32>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RequirementIssue {
    MissingPackage {
        package_id: ObjectId,
    },
    PackageVersion {
        package_id: ObjectId,
        required: Version,
        active: Version,
    },
    PackageAbi {
        package_id: ObjectId,
        required_major: u16,
        required_min_minor: u16,
        required_max_minor: u16,
        active_major: u16,
        active_min_minor: u16,
        active_max_minor: u16,
    },
    DescriptorFingerprint {
        package_id: ObjectId,
    },
    MissingOrStaleObject {
        package_id: ObjectId,
        object_id: ObjectId,
        kind: ObjectKind,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DatabasePluginAdmission {
    Normal,
    Restricted { issues: Vec<RequirementIssue> },
}

pub fn derive_object_id(package_id: ObjectId, local_id: &str) -> Result<ObjectId, &'static str> {
    radixdb_core::derive_plugin_object_identity_bytes(package_id, local_id)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn identity_is_stable_and_sql_name_independent() {
        let package = [7; 16];
        assert_eq!(
            derive_object_id(package, "point").unwrap(),
            derive_object_id(package, "point").unwrap()
        );
        assert_ne!(
            derive_object_id(package, "point").unwrap(),
            derive_object_id(package, "point_v2").unwrap()
        );
    }

    #[test]
    fn empty_registry_is_compatibility_default() {
        let registry = PluginRegistry::empty();
        assert_eq!(registry.status().generation, 0);
        assert_eq!(registry.status().packages, 0);
        assert_eq!(
            registry.assess_requirements(&[]),
            DatabasePluginAdmission::Normal
        );
    }
}