prolly-map 0.4.0

Content-addressed versioned map storage primitives.
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
use serde::{Deserialize, Serialize};
use std::collections::{btree_map::Entry, BTreeMap};
use std::fmt;
use std::sync::Arc;

use super::super::error::Error;

/// Bytes stored beside a matching primary key in the physical index tree.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IndexProjection {
    /// Store only the physical `(term, primary_key)` key.
    #[default]
    KeysOnly,
    /// Store extractor-supplied deterministic projection bytes.
    Include,
    /// Store the complete raw source value.
    All,
}

/// One logical index emission from one source record.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SecondaryIndexEntry {
    /// Logical term used for exact, prefix, and range lookup.
    pub term: Vec<u8>,
    /// Application projection bytes. Present only for [`IndexProjection::Include`].
    pub projection: Option<Vec<u8>>,
}

/// Callback-scoped index emission used by allocation-reusing extractors.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SecondaryIndexEntryRef<'a> {
    pub term: &'a [u8],
    pub projection: Option<&'a [u8]>,
}

/// Extractor that emits borrowed terms/projections into a synchronous sink.
pub trait StreamingSecondaryIndexExtractor: Send + Sync {
    fn extract(
        &self,
        primary_key: &[u8],
        source_value: &[u8],
        emit: &mut dyn FnMut(SecondaryIndexEntryRef<'_>) -> Result<(), SecondaryIndexError>,
    ) -> Result<(), SecondaryIndexError>;
}

impl SecondaryIndexEntry {
    /// Emit a term without application projection bytes.
    pub fn term(term: impl AsRef<[u8]>) -> Self {
        Self {
            term: term.as_ref().to_vec(),
            projection: None,
        }
    }

    /// Emit a term and deterministic application projection bytes.
    pub fn included(term: impl AsRef<[u8]>, projection: impl AsRef<[u8]>) -> Self {
        Self {
            term: term.as_ref().to_vec(),
            projection: Some(projection.as_ref().to_vec()),
        }
    }
}

/// Error returned by an application index extractor.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SecondaryIndexError {
    reason: String,
}

impl SecondaryIndexError {
    /// Create an extractor failure with a stable application-facing reason.
    pub fn new(reason: impl Into<String>) -> Self {
        Self {
            reason: reason.into(),
        }
    }

    /// Borrow the failure reason.
    pub fn reason(&self) -> &str {
        &self.reason
    }
}

impl fmt::Display for SecondaryIndexError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.reason)
    }
}

impl std::error::Error for SecondaryIndexError {}

/// Deterministic runtime callback that derives zero or more index emissions.
pub trait SecondaryIndexExtractor: Send + Sync + 'static {
    /// Derive emissions for one source record.
    fn extract(
        &self,
        primary_key: &[u8],
        source_value: &[u8],
    ) -> Result<Vec<SecondaryIndexEntry>, SecondaryIndexError>;
}

impl<F> SecondaryIndexExtractor for F
where
    F: Fn(&[u8], &[u8]) -> Result<Vec<SecondaryIndexEntry>, SecondaryIndexError>
        + Send
        + Sync
        + 'static,
{
    fn extract(
        &self,
        primary_key: &[u8],
        source_value: &[u8],
    ) -> Result<Vec<SecondaryIndexEntry>, SecondaryIndexError> {
        self(primary_key, source_value)
    }
}

struct TermsExtractor<F>(F);

impl<F> SecondaryIndexExtractor for TermsExtractor<F>
where
    F: Fn(&[u8], &[u8]) -> Result<Vec<Vec<u8>>, SecondaryIndexError> + Send + Sync + 'static,
{
    fn extract(
        &self,
        primary_key: &[u8],
        source_value: &[u8],
    ) -> Result<Vec<SecondaryIndexEntry>, SecondaryIndexError> {
        (self.0)(primary_key, source_value)
            .map(|terms| terms.into_iter().map(SecondaryIndexEntry::term).collect())
    }
}

/// Resource bounds applied before index-derived buffers are published.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SecondaryIndexLimits {
    pub max_term_bytes: usize,
    pub max_projection_bytes: usize,
    pub max_all_value_bytes: usize,
    pub max_terms_per_record: usize,
    pub max_projected_bytes_per_record: usize,
    pub max_derived_mutations_per_transaction: usize,
    pub max_projected_bytes_per_transaction: usize,
    pub max_indexes: usize,
    pub build_page_size: usize,
    pub max_temporary_sort_bytes: usize,
    pub max_bundle_nodes: usize,
    pub max_bundle_bytes: usize,
    pub max_verification_entries: usize,
    pub max_write_retries: usize,
    pub max_build_retries: usize,
}

impl Default for SecondaryIndexLimits {
    fn default() -> Self {
        Self {
            max_term_bytes: 4 * 1024,
            max_projection_bytes: 64 * 1024,
            max_all_value_bytes: 1024 * 1024,
            max_terms_per_record: 1024,
            max_projected_bytes_per_record: 1024 * 1024,
            max_derived_mutations_per_transaction: 100_000,
            max_projected_bytes_per_transaction: 64 * 1024 * 1024,
            max_indexes: 32,
            build_page_size: 4096,
            max_temporary_sort_bytes: 256 * 1024 * 1024,
            max_bundle_nodes: 1_000_000,
            max_bundle_bytes: 1024 * 1024 * 1024,
            max_verification_entries: 10_000_000,
            max_write_retries: 8,
            max_build_retries: 8,
        }
    }
}

impl SecondaryIndexLimits {
    fn validate(&self) -> Result<(), Error> {
        let values = [
            ("max_term_bytes", self.max_term_bytes),
            ("max_projection_bytes", self.max_projection_bytes),
            ("max_all_value_bytes", self.max_all_value_bytes),
            ("max_terms_per_record", self.max_terms_per_record),
            (
                "max_projected_bytes_per_record",
                self.max_projected_bytes_per_record,
            ),
            (
                "max_derived_mutations_per_transaction",
                self.max_derived_mutations_per_transaction,
            ),
            (
                "max_projected_bytes_per_transaction",
                self.max_projected_bytes_per_transaction,
            ),
            ("max_indexes", self.max_indexes),
            ("build_page_size", self.build_page_size),
            ("max_temporary_sort_bytes", self.max_temporary_sort_bytes),
            ("max_bundle_nodes", self.max_bundle_nodes),
            ("max_bundle_bytes", self.max_bundle_bytes),
            ("max_verification_entries", self.max_verification_entries),
            ("max_write_retries", self.max_write_retries),
            ("max_build_retries", self.max_build_retries),
        ];
        if let Some((field, _)) = values.into_iter().find(|(_, value)| *value == 0) {
            return Err(Error::InvalidIndexDefinition {
                reason: format!("{field} must be greater than zero"),
            });
        }
        Ok(())
    }
}

/// One immutable runtime index definition.
#[derive(Clone)]
pub struct SecondaryIndex {
    name: Vec<u8>,
    generation: u64,
    extractor_id: String,
    projection: IndexProjection,
    limits: SecondaryIndexLimits,
    extractor: Arc<dyn SecondaryIndexExtractor>,
}

impl fmt::Debug for SecondaryIndex {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SecondaryIndex")
            .field("name", &self.name)
            .field("generation", &self.generation)
            .field("extractor_id", &self.extractor_id)
            .field("projection", &self.projection)
            .field("limits", &self.limits)
            .finish_non_exhaustive()
    }
}

impl SecondaryIndex {
    /// Define a non-unique keys-only index from a zero-or-more term callback.
    pub fn non_unique<N, I, F>(
        name: N,
        generation: u64,
        extractor_id: I,
        extractor: F,
    ) -> Result<Self, Error>
    where
        N: AsRef<[u8]>,
        I: Into<String>,
        F: Fn(&[u8], &[u8]) -> Result<Vec<Vec<u8>>, SecondaryIndexError> + Send + Sync + 'static,
    {
        Self::builder(name, generation, extractor_id)
            .projection(IndexProjection::KeysOnly)
            .extract_terms(extractor)
    }

    /// Start a general runtime definition builder.
    pub fn builder<N, I>(name: N, generation: u64, extractor_id: I) -> SecondaryIndexBuilder
    where
        N: AsRef<[u8]>,
        I: Into<String>,
    {
        SecondaryIndexBuilder {
            name: name.as_ref().to_vec(),
            generation,
            extractor_id: extractor_id.into(),
            projection: IndexProjection::KeysOnly,
            limits: SecondaryIndexLimits::default(),
        }
    }

    /// Stable arbitrary-byte index name.
    pub fn name(&self) -> &[u8] {
        &self.name
    }
    /// Monotonically increasing semantic definition generation.
    pub fn generation(&self) -> u64 {
        self.generation
    }
    /// Application-controlled identity for deterministic extractor semantics.
    pub fn extractor_id(&self) -> &str {
        &self.extractor_id
    }
    /// Projection mode for physical index values.
    pub fn projection(&self) -> IndexProjection {
        self.projection
    }
    /// Resource bounds attached to this definition.
    pub fn limits(&self) -> &SecondaryIndexLimits {
        &self.limits
    }

    /// Run and validate the deterministic extractor for one record.
    pub fn extract(
        &self,
        primary_key: &[u8],
        source_value: &[u8],
    ) -> Result<Vec<SecondaryIndexEntry>, Error> {
        if self.projection == IndexProjection::All
            && source_value.len() > self.limits.max_all_value_bytes
        {
            return Err(Error::IndexResourceLimitExceeded {
                resource: "all_source_value_bytes",
                limit: self.limits.max_all_value_bytes,
                actual: source_value.len(),
            });
        }
        let entries = self
            .extractor
            .extract(primary_key, source_value)
            .map_err(|error| Error::IndexExtractionFailed {
                name: self.name.clone(),
                primary_key: primary_key.to_vec(),
                reason: error.to_string(),
            })?;
        self.validate_entries(primary_key, entries)
    }

    fn validate_entries(
        &self,
        primary_key: &[u8],
        entries: Vec<SecondaryIndexEntry>,
    ) -> Result<Vec<SecondaryIndexEntry>, Error> {
        if entries.len() > self.limits.max_terms_per_record {
            return Err(Error::IndexResourceLimitExceeded {
                resource: "terms_per_record",
                limit: self.limits.max_terms_per_record,
                actual: entries.len(),
            });
        }
        let mut canonical = BTreeMap::<Vec<u8>, Option<Vec<u8>>>::new();
        let mut projected_bytes = 0usize;
        for entry in entries {
            if entry.term.len() > self.limits.max_term_bytes {
                return Err(Error::IndexResourceLimitExceeded {
                    resource: "term_bytes",
                    limit: self.limits.max_term_bytes,
                    actual: entry.term.len(),
                });
            }
            let projection_matches = match self.projection {
                IndexProjection::KeysOnly | IndexProjection::All => entry.projection.is_none(),
                IndexProjection::Include => entry.projection.is_some(),
            };
            if !projection_matches {
                return Err(Error::IndexProjectionMismatch {
                    name: self.name.clone(),
                    mode: self.projection,
                    primary_key: primary_key.to_vec(),
                });
            }
            if let Some(projection) = &entry.projection {
                if projection.len() > self.limits.max_projection_bytes {
                    return Err(Error::IndexResourceLimitExceeded {
                        resource: "projection_bytes",
                        limit: self.limits.max_projection_bytes,
                        actual: projection.len(),
                    });
                }
                projected_bytes = projected_bytes.saturating_add(projection.len());
            }
            match canonical.entry(entry.term) {
                Entry::Vacant(slot) => {
                    slot.insert(entry.projection);
                }
                Entry::Occupied(slot) if slot.get() == &entry.projection => {}
                Entry::Occupied(slot) => {
                    return Err(Error::ConflictingIndexProjection {
                        name: self.name.clone(),
                        primary_key: primary_key.to_vec(),
                        term: slot.key().clone(),
                    });
                }
            }
        }
        if projected_bytes > self.limits.max_projected_bytes_per_record {
            return Err(Error::IndexResourceLimitExceeded {
                resource: "projected_bytes_per_record",
                limit: self.limits.max_projected_bytes_per_record,
                actual: projected_bytes,
            });
        }
        Ok(canonical
            .into_iter()
            .map(|(term, projection)| SecondaryIndexEntry { term, projection })
            .collect())
    }
}

/// Builder for a general projection-aware runtime definition.
#[derive(Clone, Debug)]
pub struct SecondaryIndexBuilder {
    name: Vec<u8>,
    generation: u64,
    extractor_id: String,
    projection: IndexProjection,
    limits: SecondaryIndexLimits,
}

impl SecondaryIndexBuilder {
    /// Select the persisted projection mode.
    pub fn projection(mut self, projection: IndexProjection) -> Self {
        self.projection = projection;
        self
    }
    /// Override all default resource bounds.
    pub fn limits(mut self, limits: SecondaryIndexLimits) -> Self {
        self.limits = limits;
        self
    }

    /// Finish with a projection-aware entry callback.
    pub fn extract<F>(self, extractor: F) -> Result<SecondaryIndex, Error>
    where
        F: Fn(&[u8], &[u8]) -> Result<Vec<SecondaryIndexEntry>, SecondaryIndexError>
            + Send
            + Sync
            + 'static,
    {
        self.finish(Arc::new(extractor))
    }

    /// Finish with a term-only callback for `KeysOnly` or `All` projection.
    pub fn extract_terms<F>(self, extractor: F) -> Result<SecondaryIndex, Error>
    where
        F: Fn(&[u8], &[u8]) -> Result<Vec<Vec<u8>>, SecondaryIndexError> + Send + Sync + 'static,
    {
        if self.projection == IndexProjection::Include {
            return Err(Error::InvalidIndexDefinition {
                reason: "Include projection requires an entry extractor".to_string(),
            });
        }
        self.finish(Arc::new(TermsExtractor(extractor)))
    }

    fn finish(self, extractor: Arc<dyn SecondaryIndexExtractor>) -> Result<SecondaryIndex, Error> {
        if self.name.is_empty() {
            return Err(Error::InvalidIndexDefinition {
                reason: "index name must not be empty".to_string(),
            });
        }
        if self.generation == 0 {
            return Err(Error::InvalidIndexDefinition {
                reason: "index generation must be greater than zero".to_string(),
            });
        }
        if self.extractor_id.is_empty() {
            return Err(Error::InvalidIndexDefinition {
                reason: "extractor ID must not be empty".to_string(),
            });
        }
        self.limits.validate()?;
        Ok(SecondaryIndex {
            name: self.name,
            generation: self.generation,
            extractor_id: self.extractor_id,
            projection: self.projection,
            limits: self.limits,
            extractor,
        })
    }
}

/// Deterministically ordered runtime definitions supplied when opening an indexed map.
#[derive(Clone, Debug, Default)]
pub struct SecondaryIndexRegistry {
    indexes: BTreeMap<Vec<u8>, SecondaryIndex>,
    historical: BTreeMap<Vec<u8>, Vec<SecondaryIndex>>,
}

impl SecondaryIndexRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Register one definition, rejecting duplicate logical names.
    pub fn register(mut self, index: SecondaryIndex) -> Result<Self, Error> {
        if self.indexes.contains_key(index.name()) {
            return Err(Error::InvalidIndexDefinition {
                reason: format!("duplicate index name {:?}", index.name()),
            });
        }
        self.indexes.insert(index.name.clone(), index);
        Ok(self)
    }

    /// Replace the active runtime definition while retaining the previous
    /// generation for exact historical snapshot reopening.
    pub fn replace(mut self, index: SecondaryIndex) -> Result<Self, Error> {
        let name = index.name.clone();
        let current = self
            .indexes
            .remove(&name)
            .ok_or_else(|| Error::InvalidIndexDefinition {
                reason: format!("cannot replace unregistered index name {:?}", name),
            })?;
        if index.generation() <= current.generation() {
            self.indexes.insert(name, current);
            return Err(Error::InvalidIndexDefinition {
                reason: "replacement generation must be strictly greater than active generation"
                    .to_string(),
            });
        }
        self.historical
            .entry(name.clone())
            .or_default()
            .push(current);
        self.indexes.insert(name, index);
        Ok(self)
    }

    /// Borrow a definition by its exact raw name.
    pub fn get(&self, name: &[u8]) -> Option<&SecondaryIndex> {
        self.indexes.get(name)
    }
    /// Iterate definitions in raw-name order.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = &SecondaryIndex> {
        self.indexes.values()
    }

    pub(crate) fn definitions_for_name(&self, name: &[u8]) -> Vec<SecondaryIndex> {
        self.indexes
            .get(name)
            .into_iter()
            .chain(
                self.historical
                    .get(name)
                    .into_iter()
                    .flat_map(|definitions| definitions.iter()),
            )
            .cloned()
            .collect()
    }
    /// Number of definitions in the registry.
    pub fn len(&self) -> usize {
        self.indexes.len()
    }
    /// Whether the registry contains no definitions.
    pub fn is_empty(&self) -> bool {
        self.indexes.is_empty()
    }
}