trine-kv 0.5.13

Embedded LSM MVCC key-value database.
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
use super::{
    BTreeMap, BlobFileHeader, BlobRecord, CodecId, DurabilityMode, Entry, Error, InternalKey,
    NativeFileBackend, NativeFileObject, Path, Result, Sequence, StorageObjectWriteBackend,
    StorageReadBackend, ValueRef, blob_object_len, blob_storage_backend, checked_blob_read_len,
    checksum, open_blob_read_object_with_backend, open_blob_read_object_with_backend_async,
    read_blob_exact_at, read_blob_exact_at_async, read_indexed_blob_record,
    read_indexed_value_with_backend, read_indexed_value_with_backend_async, usize_to_u64,
    validate_indexed_blob_header, write_blob_file_with_backend_async,
    write_blob_file_with_backend_with_durability,
};

#[allow(dead_code)]
pub(crate) fn write_large_values(
    db_path: &Path,
    file_id: u64,
    threshold: usize,
    compression: CodecId,
    records: &[(InternalKey, Option<ValueRef>)],
) -> Result<Vec<(InternalKey, Option<ValueRef>)>> {
    let backend = blob_storage_backend();
    write_large_values_with_backend(&backend, db_path, file_id, threshold, compression, records)
}

pub(crate) fn write_large_values_with_backend(
    backend: &NativeFileBackend,
    db_path: &Path,
    file_id: u64,
    threshold: usize,
    compression: CodecId,
    records: &[(InternalKey, Option<ValueRef>)],
) -> Result<Vec<(InternalKey, Option<ValueRef>)>> {
    write_large_values_with_backend_with_durability(
        backend,
        db_path,
        file_id,
        threshold,
        compression,
        records,
        DurabilityMode::SyncAll,
    )
}

pub(crate) fn write_large_values_with_backend_with_durability(
    backend: &NativeFileBackend,
    db_path: &Path,
    file_id: u64,
    threshold: usize,
    compression: CodecId,
    records: &[(InternalKey, Option<ValueRef>)],
    durability: DurabilityMode,
) -> Result<Vec<(InternalKey, Option<ValueRef>)>> {
    let needs_blob_file = records.iter().any(
        |(_, value)| matches!(value, Some(ValueRef::Inline(bytes)) if bytes.len() >= threshold),
    );
    if !needs_blob_file {
        return Ok(records.to_vec());
    }

    let mut blob_records = Vec::new();
    for (internal_key, value) in records {
        if let Some(ValueRef::Inline(bytes)) = value {
            if bytes.len() >= threshold {
                blob_records.push(BlobRecord {
                    internal_key: internal_key.clone(),
                    value: bytes.clone(),
                    compression,
                });
            }
        }
    }

    let creation_sequence = records
        .iter()
        .map(|(internal_key, _)| internal_key.sequence())
        .max()
        .unwrap_or(Sequence::ZERO);
    let threshold_bytes = usize_to_u64(threshold, "blob threshold")?;
    let header = BlobFileHeader::new(file_id, creation_sequence, threshold_bytes, compression);
    let indexes = write_blob_file_with_backend_with_durability(
        backend,
        db_path,
        file_id,
        header,
        &blob_records,
        durability,
    )?;
    let mut index_iter = indexes.into_iter();

    let mut rewritten = Vec::with_capacity(records.len());

    for (internal_key, value) in records {
        let value = match value {
            Some(ValueRef::Inline(bytes)) if bytes.len() >= threshold => {
                let index = index_iter.next().ok_or_else(|| Error::Corruption {
                    message: "missing blob index for separated value".to_owned(),
                })?;
                Some(ValueRef::BlobIndex(index))
            }
            value => value.clone(),
        };
        rewritten.push((internal_key.clone(), value));
    }
    if index_iter.next().is_some() {
        return Err(Error::Corruption {
            message: "unused blob index after rewriting large values".to_owned(),
        });
    }

    Ok(rewritten)
}
pub(crate) async fn write_large_values_with_backend_async<B>(
    backend: &B,
    db_path: &Path,
    file_id: u64,
    threshold: usize,
    compression: CodecId,
    records: &[(InternalKey, Option<ValueRef>)],
    durability: DurabilityMode,
) -> Result<Vec<(InternalKey, Option<ValueRef>)>>
where
    B: StorageObjectWriteBackend,
{
    let needs_blob_file = records.iter().any(
        |(_, value)| matches!(value, Some(ValueRef::Inline(bytes)) if bytes.len() >= threshold),
    );
    if !needs_blob_file {
        return Ok(records.to_vec());
    }

    let mut blob_records = Vec::new();
    for (internal_key, value) in records {
        if let Some(ValueRef::Inline(bytes)) = value {
            if bytes.len() >= threshold {
                blob_records.push(BlobRecord {
                    internal_key: internal_key.clone(),
                    value: bytes.clone(),
                    compression,
                });
            }
        }
    }

    let creation_sequence = records
        .iter()
        .map(|(internal_key, _)| internal_key.sequence())
        .max()
        .unwrap_or(Sequence::ZERO);
    let threshold_bytes = usize_to_u64(threshold, "blob threshold")?;
    let header = BlobFileHeader::new(file_id, creation_sequence, threshold_bytes, compression);
    let indexes = write_blob_file_with_backend_async(
        backend,
        db_path,
        file_id,
        header,
        &blob_records,
        durability,
    )
    .await?;
    let mut index_iter = indexes.into_iter();

    let mut rewritten = Vec::with_capacity(records.len());

    for (internal_key, value) in records {
        let value = match value {
            Some(ValueRef::Inline(bytes)) if bytes.len() >= threshold => {
                let index = index_iter.next().ok_or_else(|| Error::Corruption {
                    message: "missing blob index for separated value".to_owned(),
                })?;
                Some(ValueRef::BlobIndex(index))
            }
            value => value.clone(),
        };
        rewritten.push((internal_key.clone(), value));
    }
    if index_iter.next().is_some() {
        return Err(Error::Corruption {
            message: "unused blob index after rewriting large values".to_owned(),
        });
    }

    Ok(rewritten)
}

#[allow(dead_code)]
pub(crate) fn inline_blob_values(
    db_path: &Path,
    records: &[(InternalKey, Option<ValueRef>)],
) -> Result<Vec<(InternalKey, Option<ValueRef>)>> {
    let backend = blob_storage_backend();
    inline_blob_values_with_backend(&backend, db_path, records)
}

pub(crate) fn inline_blob_values_with_backend(
    backend: &NativeFileBackend,
    db_path: &Path,
    records: &[(InternalKey, Option<ValueRef>)],
) -> Result<Vec<(InternalKey, Option<ValueRef>)>> {
    let mut rewritten = Vec::with_capacity(records.len());
    let mut blob_files = BTreeMap::new();
    for (internal_key, value) in records {
        let value = match value {
            Some(ValueRef::Inline(bytes)) => Some(ValueRef::Inline(bytes.clone())),
            Some(value @ (ValueRef::BlobIndex(_) | ValueRef::Blob { .. })) => {
                Some(ValueRef::Inline(read_value_for_internal_key_cached(
                    backend,
                    db_path,
                    value,
                    Some(internal_key),
                    &mut blob_files,
                )?))
            }
            None => None,
        };
        rewritten.push((internal_key.clone(), value));
    }
    Ok(rewritten)
}

pub(super) struct CachedBlobFile {
    object: NativeFileObject,
    len: u64,
}

pub(super) fn read_value_for_internal_key_cached(
    backend: &NativeFileBackend,
    db_path: &Path,
    value: &ValueRef,
    expected_internal_key: Option<&InternalKey>,
    blob_files: &mut BTreeMap<u64, CachedBlobFile>,
) -> Result<Vec<u8>> {
    match value {
        ValueRef::Inline(bytes) => Ok(bytes.clone()),
        ValueRef::BlobIndex(index) => {
            let blob_file = cached_blob_file(backend, db_path, index.file_id, blob_files)?;
            let record = read_indexed_blob_record(&blob_file.object, blob_file.len, index)?;
            if record.index != *index {
                return Err(Error::Corruption {
                    message: "blob index metadata mismatch".to_owned(),
                });
            }
            if expected_internal_key.is_some_and(|expected| record.record.internal_key != *expected)
            {
                return Err(Error::Corruption {
                    message: "blob record internal key mismatch".to_owned(),
                });
            }
            Ok(record.record.value)
        }
        ValueRef::Blob {
            file_id,
            offset,
            len,
            checksum: expected_checksum,
        } => {
            let len = checked_blob_read_len(*len)?;
            let blob_file = cached_blob_file(backend, db_path, *file_id, blob_files)?;
            let mut bytes = vec![0_u8; len];
            read_blob_exact_at(
                &blob_file.object,
                *offset,
                &mut bytes,
                "referenced blob bytes cannot be read",
            )?;
            if checksum(&bytes) != *expected_checksum {
                return Err(Error::Corruption {
                    message: "blob checksum mismatch".to_owned(),
                });
            }
            Ok(bytes)
        }
    }
}

pub(super) fn cached_blob_file<'files>(
    backend: &NativeFileBackend,
    db_path: &Path,
    file_id: u64,
    blob_files: &'files mut BTreeMap<u64, CachedBlobFile>,
) -> Result<&'files CachedBlobFile> {
    match blob_files.entry(file_id) {
        Entry::Occupied(entry) => Ok(entry.into_mut()),
        Entry::Vacant(entry) => {
            let object = open_blob_read_object_with_backend(backend, db_path, file_id)?;
            let len = blob_object_len(&object, "referenced blob file metadata cannot be read")?;
            validate_indexed_blob_header(&object, file_id)?;
            Ok(entry.insert(CachedBlobFile { object, len }))
        }
    }
}
pub(crate) async fn inline_blob_values_with_backend_async<B>(
    backend: &B,
    db_path: &Path,
    records: &[(InternalKey, Option<ValueRef>)],
) -> Result<Vec<(InternalKey, Option<ValueRef>)>>
where
    B: StorageReadBackend,
{
    let mut rewritten = Vec::with_capacity(records.len());
    for (internal_key, value) in records {
        let value = match value {
            Some(ValueRef::Inline(bytes)) => Some(ValueRef::Inline(bytes.clone())),
            Some(value @ (ValueRef::BlobIndex(_) | ValueRef::Blob { .. })) => {
                Some(ValueRef::Inline(
                    read_value_for_internal_key_with_backend_async(
                        backend,
                        db_path,
                        value,
                        Some(internal_key),
                    )
                    .await?,
                ))
            }
            None => None,
        };
        rewritten.push((internal_key.clone(), value));
    }
    Ok(rewritten)
}

pub(crate) fn read_value_for_internal_key(
    db_path: &Path,
    value: &ValueRef,
    expected_internal_key: Option<&InternalKey>,
) -> Result<Vec<u8>> {
    let backend = blob_storage_backend();
    read_value_for_internal_key_with_backend(&backend, db_path, value, expected_internal_key)
}

pub(crate) fn read_value_for_internal_key_with_backend(
    backend: &NativeFileBackend,
    db_path: &Path,
    value: &ValueRef,
    expected_internal_key: Option<&InternalKey>,
) -> Result<Vec<u8>> {
    match value {
        ValueRef::Inline(bytes) => Ok(bytes.clone()),
        ValueRef::BlobIndex(index) => {
            read_indexed_value_with_backend(backend, db_path, index, expected_internal_key)
        }
        ValueRef::Blob {
            file_id,
            offset,
            len,
            checksum: expected_checksum,
        } => {
            let len = checked_blob_read_len(*len)?;
            let mut bytes = vec![0_u8; len];
            let object = open_blob_read_object_with_backend(backend, db_path, *file_id)?;
            read_blob_exact_at(
                &object,
                *offset,
                &mut bytes,
                "referenced blob bytes cannot be read",
            )?;
            if checksum(&bytes) != *expected_checksum {
                return Err(Error::Corruption {
                    message: "blob checksum mismatch".to_owned(),
                });
            }
            Ok(bytes)
        }
    }
}
pub(crate) async fn read_value_for_internal_key_with_backend_async<B>(
    backend: &B,
    db_path: &Path,
    value: &ValueRef,
    expected_internal_key: Option<&InternalKey>,
) -> Result<Vec<u8>>
where
    B: StorageReadBackend,
{
    match value {
        ValueRef::Inline(bytes) => Ok(bytes.clone()),
        ValueRef::BlobIndex(index) => {
            read_indexed_value_with_backend_async(backend, db_path, index, expected_internal_key)
                .await
        }
        ValueRef::Blob {
            file_id,
            offset,
            len,
            checksum: expected_checksum,
        } => {
            let object =
                open_blob_read_object_with_backend_async(backend, db_path, *file_id).await?;
            let len = checked_blob_read_len(*len)?;
            let mut bytes = vec![0_u8; len];
            read_blob_exact_at_async(
                &object,
                *offset,
                &mut bytes,
                "referenced blob bytes cannot be read",
            )
            .await?;
            if checksum(&bytes) != *expected_checksum {
                return Err(Error::Corruption {
                    message: "blob checksum mismatch".to_owned(),
                });
            }
            Ok(bytes)
        }
    }
}