hawdb-storage 0.12.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::{
    RelationalError, RelationalOverflowSegment, RelationalProjectedRow, RelationalRow,
    RelationalScalarType, RelationalState, RelationalTableSchema, RelationalValue,
};
use hawdb_integrity::Sha256Digest;
use std::collections::BTreeSet;
use std::sync::Arc;

pub(super) mod checkpoint;
mod envelope;
mod exact;
mod publication;

pub(in crate::relational) use envelope::admit_overflow_hydration;
use envelope::DEFAULT_ZSTD_LEVEL;
pub(crate) use envelope::{
    decode_overflow_envelope, encode_overflow_envelope, encode_overflow_envelope_with_work_context,
    validate_overflow_envelope_with_work_context, CheckpointRelationalOverflow,
    EncodedRelationalOverflow,
};
pub use exact::{
    RelationalOverflowReferenceSet, RelationalOverflowReferenceSetBuilder,
    RelationalOverflowReferenceSortConfig, RelationalOverflowReferenceSortReport,
    DEFAULT_RELATIONAL_OVERFLOW_REFERENCE_OCCURRENCES, DEFAULT_RELATIONAL_OVERFLOW_REFERENCE_RUNS,
    DEFAULT_RELATIONAL_OVERFLOW_REFERENCE_SORT_MEMORY_BYTES,
    DEFAULT_RELATIONAL_OVERFLOW_REFERENCE_SPILL_BYTES,
};
pub use publication::{
    relational_overflow_descriptor_file, relational_overflow_extent_file,
    relational_overflow_manifest_generation_file, RelationalOverflowArtifactMetadata,
    RelationalOverflowExactGenerationRequest, RelationalOverflowExactPublicationReport,
    RelationalOverflowExtentDescriptor, RelationalOverflowExtentInput,
    RelationalOverflowGenerationArtifacts, RelationalOverflowInputs, RelationalOverflowInputsIter,
    RelationalOverflowPublicationConfig, RelationalOverflowPublicationError,
    RelationalOverflowPublicationPhase, RelationalOverflowPublicationReport,
    RelationalOverflowPublisher, RelationalOverflowRootBinding, RelationalOverflowRootManifest,
    RelationalOverflowRootReader, DEFAULT_RELATIONAL_OVERFLOW_EXTENTS,
    DEFAULT_RELATIONAL_OVERFLOW_MANIFEST_BYTES, DEFAULT_RELATIONAL_OVERFLOW_NEW_EXTENT_BYTES,
    RELATIONAL_OVERFLOW_MANIFEST_FILE,
};

pub const DEFAULT_RELATIONAL_OVERFLOW_THRESHOLD_BYTES: usize = 4 * 1024;
pub const DEFAULT_MAX_RELATIONAL_HYDRATION_BYTES: usize = 64 * 1024 * 1024;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RelationalOverflowConfig {
    pub threshold_bytes: usize,
    pub compression_level: i32,
    pub max_value_bytes: usize,
}

impl Default for RelationalOverflowConfig {
    fn default() -> Self {
        Self {
            threshold_bytes: DEFAULT_RELATIONAL_OVERFLOW_THRESHOLD_BYTES,
            compression_level: DEFAULT_ZSTD_LEVEL,
            max_value_bytes: DEFAULT_MAX_RELATIONAL_HYDRATION_BYTES,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RelationalOverflowRef {
    pub digest: Sha256Digest,
    pub scalar_type: RelationalScalarType,
    pub compressed_bytes: u64,
    pub uncompressed_bytes: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RelationalHydrationBudget {
    pub max_rows: usize,
    pub max_compressed_bytes: usize,
    pub max_decompressed_bytes: usize,
    pub max_memory_bytes: usize,
    pub hydrated_rows: usize,
    pub compressed_bytes: usize,
    pub decompressed_bytes: usize,
    pub memory_bytes: usize,
}

impl Default for RelationalHydrationBudget {
    fn default() -> Self {
        Self {
            max_rows: 1_000,
            max_compressed_bytes: DEFAULT_MAX_RELATIONAL_HYDRATION_BYTES,
            max_decompressed_bytes: DEFAULT_MAX_RELATIONAL_HYDRATION_BYTES,
            max_memory_bytes: DEFAULT_MAX_RELATIONAL_HYDRATION_BYTES,
            hydrated_rows: 0,
            compressed_bytes: 0,
            decompressed_bytes: 0,
            memory_bytes: 0,
        }
    }
}

pub(super) fn externalize_row(
    state: &mut RelationalState,
    schema: &RelationalTableSchema,
    row: &mut RelationalRow,
    config: RelationalOverflowConfig,
) -> Result<(), RelationalError> {
    let protected = protected_column_positions(schema)?;
    let values = Arc::make_mut(&mut row.values);
    for (position, value) in values.iter_mut().enumerate() {
        if protected.contains(&position) {
            continue;
        }
        let (scalar_type, raw) = match value {
            RelationalValue::Text(text) if text.len() >= config.threshold_bytes => {
                (RelationalScalarType::Text, text.as_bytes())
            }
            RelationalValue::Bytea(bytes) if bytes.len() >= config.threshold_bytes => {
                (RelationalScalarType::Bytea, bytes.as_slice())
            }
            _ => continue,
        };
        let encoded = encode_overflow_envelope(scalar_type, raw, config)?;
        let digest = encoded.reference.digest;
        state
            .overflow_segments
            .entry(digest)
            .or_insert_with(|| RelationalOverflowSegment::Inline(Arc::clone(&encoded.bytes)));
        *value = RelationalValue::Overflow(encoded.reference);
    }
    Ok(())
}

pub(super) fn externalize_row_with_allocations(
    state: &mut RelationalState,
    schema: &RelationalTableSchema,
    row: &mut RelationalRow,
    config: RelationalOverflowConfig,
    allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<(), RelationalError> {
    let Some(context) = allocations else {
        return externalize_row(state, schema, row, config);
    };
    let protection_context = crate::background::CheckpointDecodeContext {
        work: context.work.clone(),
        memory: Default::default(),
    };
    let protected = protected_column_positions_with_allocations(schema, &protection_context)?;
    if Arc::strong_count(&row.values) != 1 {
        *row = RelationalRow::reconstruct_with_allocations(row, Some(context), |ordinal| {
            Ok(row.values()[ordinal].as_ref())
        })?;
    }
    let values = Arc::get_mut(&mut row.values).expect("externalization has unique row backing");
    for (position, value) in values.iter_mut().enumerate() {
        let unit = context
            .start_unit()
            .map_err(|error| RelationalError::Admission(error.to_string()))?;
        let input = if protected.contains(&position) {
            None
        } else {
            match value {
                RelationalValue::Text(text) if text.len() >= config.threshold_bytes => {
                    Some((RelationalScalarType::Text, text.as_bytes()))
                }
                RelationalValue::Bytea(bytes) if bytes.len() >= config.threshold_bytes => {
                    Some((RelationalScalarType::Bytea, bytes.as_slice()))
                }
                _ => None,
            }
        };
        unit.finish();
        let Some((scalar_type, raw)) = input else {
            continue;
        };
        let encoded =
            encode_overflow_envelope_with_work_context(scalar_type, raw, config, &context.work)?;
        let digest = encoded.reference.digest;
        if !state.overflow_segments.contains_key(&digest) {
            let node_context = crate::background::CheckpointDecodeContext {
                work: context.work.clone(),
                memory: Default::default(),
            };
            node_context
                .reserve(crate::cow::checkpoint_tree_node_bytes::<
                    Sha256Digest,
                    RelationalOverflowSegment,
                >())
                .map_err(|error| RelationalError::Admission(error.to_string()))?;
            // Transfer node ownership before allocating the map entry. Refusal
            // during transfer cannot leave a live node without its inventory.
            let mut memory = node_context.memory.into_inner();
            state
                ._metadata_memory
                .append_retained(&mut memory, &context.work)
                .map_err(|error| RelationalError::Admission(error.to_string()))?;
            let unit = context
                .start_unit()
                .map_err(|error| RelationalError::Admission(error.to_string()))?;
            state.overflow_segments.insert(
                digest,
                RelationalOverflowSegment::CheckpointInline(encoded.bytes),
            );
            unit.finish();
        }
        *value = RelationalValue::Overflow(encoded.reference);
    }
    context
        .checkpoint()
        .map_err(|error| RelationalError::Admission(error.to_string()))?;
    Ok(())
}

fn protected_column_positions_with_allocations(
    schema: &RelationalTableSchema,
    context: &crate::background::CheckpointDecodeContext,
) -> Result<BTreeSet<usize>, RelationalError> {
    let mut positions = BTreeSet::new();
    for name in schema
        .primary_key
        .iter()
        .chain(schema.unique_constraints.iter().flatten())
        .chain(
            schema
                .foreign_keys
                .iter()
                .flat_map(|key| key.columns.iter()),
        )
        .chain(schema.indexes.iter().flat_map(|index| index.columns.iter()))
    {
        let position = schema.column_position(name).ok_or_else(|| {
            RelationalError::Schema(format!(
                "overflow protection references unknown column {name}"
            ))
        })?;
        if !positions.contains(&position) {
            context
                .reserve(crate::cow::checkpoint_tree_node_bytes::<usize, ()>())
                .map_err(|error| RelationalError::Admission(error.to_string()))?;
        }
        let unit = context
            .start_unit()
            .map_err(|error| RelationalError::Admission(error.to_string()))?;
        positions.insert(position);
        unit.finish();
    }
    Ok(positions)
}

pub(super) fn hydrate_row(
    state: &RelationalState,
    row: &RelationalRow,
    budget: &mut RelationalHydrationBudget,
    task_context: Option<&hawdb_core::RuntimeTaskContext>,
) -> Result<RelationalRow, RelationalError> {
    hydrate_row_values(
        state,
        || RelationalRow::new(row.values.to_vec()),
        budget,
        task_context,
        None,
    )
}

pub(super) fn hydrate_owned_row(
    state: &RelationalState,
    row: RelationalRow,
    budget: &mut RelationalHydrationBudget,
    task_context: Option<&hawdb_core::RuntimeTaskContext>,
    allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<RelationalRow, RelationalError> {
    hydrate_row_values(state, || row, budget, task_context, allocations)
}

fn hydrate_row_values(
    state: &RelationalState,
    row: impl FnOnce() -> RelationalRow,
    budget: &mut RelationalHydrationBudget,
    task_context: Option<&hawdb_core::RuntimeTaskContext>,
    allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<RelationalRow, RelationalError> {
    runtime_checkpoint(task_context)?;
    let mut staged_budget = *budget;
    if staged_budget.hydrated_rows >= staged_budget.max_rows {
        return Err(RelationalError::Admission(format!(
            "relational hydration exceeds max_rows {}",
            staged_budget.max_rows
        )));
    }
    staged_budget.hydrated_rows += 1;
    let mut row = row();
    let values = Arc::get_mut(&mut row.values).ok_or_else(|| {
        RelationalError::Admission("owned recovery hydration requires unique row backing".into())
    })?;
    for value in values {
        let RelationalValue::Overflow(reference) = value else {
            continue;
        };
        let segment = state
            .overflow_segments
            .get(&reference.digest)
            .ok_or_else(|| {
                RelationalError::Corruption(format!(
                    "missing overflow segment {}",
                    reference.digest
                ))
            })?;
        runtime_checkpoint(task_context)?;
        let envelope = segment.read_with_work_context(allocations.map(|context| &context.work))?;
        *value = envelope::decode_overflow_envelope_with_allocations(
            reference,
            &envelope,
            &mut staged_budget,
            task_context,
            allocations,
        )?;
    }
    *budget = staged_budget;
    Ok(row)
}

pub(super) fn hydrate_projected_row(
    state: &RelationalState,
    table: &str,
    row: &mut RelationalProjectedRow,
    budget: &mut RelationalHydrationBudget,
    task_context: Option<&hawdb_core::RuntimeTaskContext>,
) -> Result<(), RelationalError> {
    hydrate_projected_row_fields(state, table, row, None, budget, task_context)
}

pub(super) fn hydrate_projected_row_fields(
    state: &RelationalState,
    table: &str,
    row: &mut RelationalProjectedRow,
    required_fields: Option<&[usize]>,
    budget: &mut RelationalHydrationBudget,
    task_context: Option<&hawdb_core::RuntimeTaskContext>,
) -> Result<(), RelationalError> {
    hydrate_projected_row_fields_with_allocations(
        state,
        table,
        row,
        required_fields,
        budget,
        task_context,
        None,
    )
}

pub(super) fn hydrate_projected_row_fields_with_allocations(
    state: &RelationalState,
    table: &str,
    row: &mut RelationalProjectedRow,
    required_fields: Option<&[usize]>,
    budget: &mut RelationalHydrationBudget,
    task_context: Option<&hawdb_core::RuntimeTaskContext>,
    allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<(), RelationalError> {
    if !row
        .fields
        .iter()
        .any(|field| matches!(field.value, RelationalValue::Overflow(_)))
    {
        return Ok(());
    }
    runtime_checkpoint(task_context)?;
    let canonical = if state.canonical_row_metadata_only() {
        None
    } else {
        Some(state.row(table, &row.primary_key).ok_or_else(|| {
            RelationalError::Corruption(format!(
                "projected overflow resolver cannot find row in table {table}"
            ))
        })?)
    };
    let schema = state.table_schema(table).ok_or_else(|| {
        RelationalError::Corruption(format!(
            "projected overflow resolver cannot find schema for table {table}"
        ))
    })?;
    let mut staged_budget = *budget;
    let mut hydrated_row = false;
    for field in &mut row.fields {
        let RelationalValue::Overflow(reference) = &field.value else {
            continue;
        };
        let column = schema.columns.get(field.ordinal).ok_or_else(|| {
            RelationalError::Corruption(format!(
                "projected field {} is outside the canonical row shape for table {table}",
                field.ordinal
            ))
        })?;
        if column.scalar_type != reference.scalar_type
            || !matches!(
                column.scalar_type,
                RelationalScalarType::Text | RelationalScalarType::Bytea
            )
        {
            return Err(RelationalError::Corruption(format!(
                "projected overflow reference at field {} has type {:?}, expected {:?} in table {table}",
                field.ordinal, reference.scalar_type, column.scalar_type
            )));
        }
        if let Some(canonical) = canonical {
            let canonical_value = canonical.values().get(field.ordinal).ok_or_else(|| {
                RelationalError::Corruption(format!(
                    "projected field {} is outside the canonical row shape for table {table}",
                    field.ordinal
                ))
            })?;
            if canonical_value != &RelationalValue::Overflow(*reference) {
                return Err(RelationalError::Corruption(format!(
                    "projected overflow reference at field {} differs from the canonical row in table {table}",
                    field.ordinal
                )));
            }
        }
        let segment = state
            .overflow_segments
            .get(&reference.digest)
            .ok_or_else(|| {
                RelationalError::Corruption(format!(
                    "missing overflow segment {}",
                    reference.digest
                ))
            })?;
        if required_fields.is_some_and(|fields| !fields.contains(&field.ordinal)) {
            continue;
        }
        if !hydrated_row {
            if staged_budget.hydrated_rows >= staged_budget.max_rows {
                return Err(RelationalError::Admission(format!(
                    "relational hydration exceeds max_rows {}",
                    staged_budget.max_rows
                )));
            }
            staged_budget.hydrated_rows += 1;
            hydrated_row = true;
        }
        runtime_checkpoint(task_context)?;
        let envelope = segment.read_with_work_context(allocations.map(|context| &context.work))?;
        field.value = envelope::decode_overflow_envelope_with_allocations(
            reference,
            &envelope,
            &mut staged_budget,
            task_context,
            allocations,
        )?;
    }
    *budget = staged_budget;
    Ok(())
}

fn runtime_checkpoint(
    task_context: Option<&hawdb_core::RuntimeTaskContext>,
) -> Result<(), RelationalError> {
    task_context.map_or(Ok(()), |context| {
        context.checkpoint().map_err(|reason| {
            RelationalError::Admission(format!("relational hydration stopped: {reason}"))
        })
    })
}

pub(super) fn prune_unreachable_segments(state: &mut RelationalState) {
    let reachable = state
        .segments
        .values()
        .flat_map(|segment| segment.rows.values())
        .flat_map(|row| row.values.iter())
        .filter_map(|value| match value {
            RelationalValue::Overflow(reference) => Some(reference.digest),
            _ => None,
        })
        .collect::<BTreeSet<_>>();
    state
        .overflow_segments
        .retain(|digest, _| reachable.contains(digest));
}

pub(super) fn prune_unreachable_segments_with_allocations(
    state: &mut RelationalState,
    allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<(), RelationalError> {
    let Some(context) = allocations else {
        prune_unreachable_segments(state);
        return Ok(());
    };
    let scratch = crate::background::CheckpointDecodeContext {
        work: context.work.clone(),
        memory: Default::default(),
    };
    let mut reachable = BTreeSet::new();
    for segment in state.segments.values() {
        for row in segment.rows.values() {
            for value in row.values.iter() {
                if let RelationalValue::Overflow(reference) = value
                    && !reachable.contains(&reference.digest)
                {
                    scratch
                        .reserve(crate::cow::checkpoint_tree_node_bytes::<Sha256Digest, ()>())
                        .map_err(|error| RelationalError::Admission(error.to_string()))?;
                }
                let unit = scratch
                    .start_unit()
                    .map_err(|error| RelationalError::Admission(error.to_string()))?;
                if let RelationalValue::Overflow(reference) = value {
                    reachable.insert(reference.digest);
                }
                unit.finish();
            }
        }
    }
    let mut refusal = None;
    state.overflow_segments.retain(|digest, _| {
        if refusal.is_some() {
            return true;
        }
        match scratch.start_unit() {
            Ok(unit) => {
                let keep = reachable.contains(digest);
                unit.finish();
                keep
            }
            Err(error) => {
                refusal = Some(error);
                true
            }
        }
    });
    if let Some(error) = refusal {
        return Err(RelationalError::Admission(error.to_string()));
    }
    scratch
        .checkpoint()
        .map_err(|error| RelationalError::Admission(error.to_string()))?;
    Ok(())
}

fn protected_column_positions(
    schema: &RelationalTableSchema,
) -> Result<BTreeSet<usize>, RelationalError> {
    let names = schema
        .primary_key
        .iter()
        .chain(schema.unique_constraints.iter().flatten())
        .chain(
            schema
                .foreign_keys
                .iter()
                .flat_map(|key| key.columns.iter()),
        )
        .chain(schema.indexes.iter().flat_map(|index| index.columns.iter()))
        .collect::<BTreeSet<_>>();
    names
        .into_iter()
        .map(|name| {
            schema.column_position(name).ok_or_else(|| {
                RelationalError::Schema(format!(
                    "overflow protection references unknown column {name}"
                ))
            })
        })
        .collect()
}