Unverified Commit 863a0ada authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-persistence: add key-value tombstones for MultiValue families (#96929)

Add a new `tombstone` format to the persistence layer so we can delete
key-value pairs out of MultiValued tables

This is in service of the upcoming GC support, but also fills a basic
API gap in the db.

To delete a key-value-pair you need to call `value_delete` and currently
the values are limited to only those that are able to be stored inline
in key blocks. This is a non-trivial limitation but it fits our current
usecase, and makes the compaction/query algorithms a bit simpler (don't
need to 'resolve' values)

One non-trivial complexity was maintaining the 'FixedLayout' block
optimization for a mix of tombstones and values, so now we support a
slightly different mode where all keys have the same length but possibly
different types.

Finally, this branch solves a problem with deleting tombstones.
Tombstones 'shadow' older values and allow us to drop them during
compaction. With GC getting ready to start writing tombstones the risk
becomes 'when can we delete a tombstone! This is solved
probabilistically using the amqf filters, during compaction we drop
tombstones if they could not possibly match anything in an older SST.
Without this, tombstones in the TaskCache table would fill up over time.
parent d45672c0
...@@ -10001,6 +10001,7 @@ name = "turbo-persistence" ...@@ -10001,6 +10001,7 @@ name = "turbo-persistence"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"auto-hash-map",
"bitfield", "bitfield",
"byteorder", "byteorder",
"codspeed-criterion-compat", "codspeed-criterion-compat",
......
...@@ -14,6 +14,7 @@ verbose_log = [] ...@@ -14,6 +14,7 @@ verbose_log = []
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
auto-hash-map = { workspace = true }
bitfield = { workspace = true } bitfield = { workspace = true }
byteorder = { workspace = true } byteorder = { workspace = true }
crc32fast = { workspace = true } crc32fast = { workspace = true }
......
...@@ -29,7 +29,10 @@ Therefore there are these value types: ...@@ -29,7 +29,10 @@ Therefore there are these value types:
- SMALL: Values 9–4096 bytes packed into shared value blocks within `*.sst` files. - SMALL: Values 9–4096 bytes packed into shared value blocks within `*.sst` files.
- MEDIUM: Values 4097 bytes – 64 MB stored in dedicated value blocks within `*.sst` files. - MEDIUM: Values 4097 bytes – 64 MB stored in dedicated value blocks within `*.sst` files.
- BLOB: Values > 64 MB stored in separate `*.blob` files. - BLOB: Values > 64 MB stored in separate `*.blob` files.
- DELETED: Values that are deleted. (Tombstone) - KEY DELETED: Every value for the key is deleted. (Key tombstone)
- KEY-VALUE DELETED: Only one named key → value pair is deleted, leaving other values for the same
key intact. (Key-value tombstone) Only meaningful for `MultiValue` families; see
[Key-value tombstones](#key-value-tombstones).
- Future: - Future:
- MERGE: An application specific update operation that is applied on the old value. - MERGE: An application specific update operation that is applied on the old value.
...@@ -138,7 +141,7 @@ Depending on the `type` field entry has a different format: ...@@ -138,7 +141,7 @@ Depending on the `type` field entry has a different format:
- 8 bytes key hash (if block type 1) - 8 bytes key hash (if block type 1)
- key data - key data
- 4 bytes sequence number - 4 bytes sequence number
- 2: deleted key / tombstone (no data) - 2: deleted key / key tombstone (no data)
- 8 bytes key hash (if block type 1) - 8 bytes key hash (if block type 1)
- key data - key data
- 3: normal key (medium sized value) - 3: normal key (medium sized value)
...@@ -150,25 +153,62 @@ Depending on the `type` field entry has a different format: ...@@ -150,25 +153,62 @@ Depending on the `type` field entry has a different format:
- 2 byte block index - 2 byte block index
- 3 bytes size - 3 bytes size
- 4 bytes position in block - 4 bytes position in block
- 8..255: inlined value (currently only values ≤8 bytes are inlined, though the format supports up to 247) - 8..=16: inlined value, size = type - 8 (the format supports up to 247, but `MAX_INLINE_VALUE_SIZE`
currently caps it at 8)
- 8 bytes key hash (if block type 1) - 8 bytes key hash (if block type 1)
- key data - key data
- (type - 8) bytes value data (inline, no separate value block) - (type - 8) bytes value data (inline, no separate value block)
- 17..=25: key-value tombstone, deleted value size = type - 17 (mirrors the inline range and shifts
with `MAX_INLINE_VALUE_SIZE`)
- 8 bytes key hash (if block type 1)
- key data
- (type - 17) bytes of the deleted value, stored inline
Both ranged kinds are open-ended, so a decoder must test the key-value tombstone range **before**
the inline range.
The entries are sorted by key hash and key. The entries are sorted by key hash and key.
##### Key-value tombstones
A key-value tombstone names the exact pair to remove, so it must carry a copy of the deleted
value's bytes. Since the value lives inline in the key block and its length is encoded in the type
byte, only inline-sized values can be deleted this way — hence `MAX_INLINE_VALUE_SIZE` bounds
`delete_value`.
The size limit is a consequence of that encoding, not of the comparison logic: matching is a plain
byte comparison and does not care how a value is stored. Supporting larger deleted values is
therefore possible but unmotivated — the tombstone stores a second copy of the value, so the cost
of deleting approaches the cost of the value itself, and reclaiming space is the whole point.
If it is ever needed, the natural encoding is a dedicated is-tombstone bit (e.g. the top bit) on the
entry type, making "deleted" orthogonal to storage class rather than a parallel type range. That
would also collapse the current duplication where the tombstone representation mirrors the inline
one at every layer. Note that blob-backed values need a separate design: comparing against a blob
means reading it, which would put unbounded I/O in the compaction path, and blob liveness
accounting would have to handle a tombstone holding a blob reference.
#### Key Block (fixed-size) #### Key Block (fixed-size)
Used when all entries in a block have the same key size and value type. Eliminates the per-entry offset table, enabling direct arithmetic indexing during binary search. Used when all entries in a block have the same key size, and either the same value type or at least
the same value size. Eliminates the per-entry offset table, enabling direct arithmetic indexing
during binary search.
- 1 byte block type (3: fixed-size with hash, 4: fixed-size without hash) - 1 byte block type (3: fixed-size with hash, 4: fixed-size without hash)
- 3 bytes entry count - 3 bytes entry count
- 1 byte key size (uniform across all entries) - 1 byte key size (uniform across all entries)
- 1 byte value type (shared by all entries, same encoding as variable-size type field) - 1 byte value type (shared by all entries, same encoding as variable-size type field), or
`FIXED_KEY_BLOCK_MIXED_VALUE_TYPE` (4) when entries share a value size but not a value type
- 1 byte value size — only present when the value type is `FIXED_KEY_BLOCK_MIXED_VALUE_TYPE`
- foreach entry (packed at stride = hash_len + key_size + val_size): - foreach entry (packed at stride = hash_len + key_size + val_size):
- 8 bytes key hash (if block type 3) - 8 bytes key hash (if block type 3)
- key data (key_size bytes) - key data (key_size bytes)
- value data (size determined by value type) - 1 byte value type — only present when the block is mixed-type
- value data (size determined by the block's or the entry's value type)
The mixed-type form exists so that same-sized inline values and key-value tombstones can share a
fixed-size block: they have equal value sizes but different type bytes. Tag 4 is available as the
mixed marker because it is not itself a valid entry type.
Entry position for index `i` is computed as `header_size + i * stride` with no indirection. The writer automatically selects fixed-size format when all entries in a block qualify; otherwise falls back to the variable-size format above. Entry position for index `i` is computed as `header_size + i * stride` with no indirection. The writer automatically selects fixed-size format when all entries in a block qualify; otherwise falls back to the variable-size format above.
...@@ -321,3 +361,7 @@ Configuration options for compactions are: ...@@ -321,3 +361,7 @@ Configuration options for compactions are:
- fsync! - fsync!
- (this also deleted enqueued files) - (this also deleted enqueued files)
## Compatibility
Currently, the database does not support cross version compatibility. Therefore all updates should be considered breaking changes and the only approach is to rewrite the databases.
...@@ -4,7 +4,8 @@ use crate::{ ...@@ -4,7 +4,8 @@ use crate::{
FamilyKind, ValueBuffer, FamilyKind, ValueBuffer,
collector_entry::{CollectorEntry, CollectorEntryValue, EntryKey, TINY_VALUE_THRESHOLD}, collector_entry::{CollectorEntry, CollectorEntryValue, EntryKey, TINY_VALUE_THRESHOLD},
constants::{ constants::{
DATA_THRESHOLD_PER_INITIAL_FILE, MAX_ENTRIES_PER_INITIAL_FILE, MAX_SMALL_VALUE_SIZE, DATA_THRESHOLD_PER_INITIAL_FILE, MAX_ENTRIES_PER_INITIAL_FILE, MAX_INLINE_VALUE_SIZE,
MAX_SMALL_VALUE_SIZE,
}, },
key::{StoreKey, hash_key}, key::{StoreKey, hash_key},
value_block_count_tracker::ValueBlockCountTracker, value_block_count_tracker::ValueBlockCountTracker,
...@@ -86,7 +87,7 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> { ...@@ -86,7 +87,7 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> {
}); });
} }
/// Adds a tombstone pair to the collector. /// Adds a tombstone pair to the collector. This deletes *all* values for `key`.
pub fn delete(&mut self, key: K) { pub fn delete(&mut self, key: K) {
let key = EntryKey { let key = EntryKey {
hash: hash_key(&key), hash: hash_key(&key),
...@@ -95,7 +96,39 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> { ...@@ -95,7 +96,39 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> {
self.total_key_size += key.len(); self.total_key_size += key.len();
self.entries.push(CollectorEntry { self.entries.push(CollectorEntry {
key, key,
value: CollectorEntryValue::Deleted, value: CollectorEntryValue::KeyDeleted,
});
}
/// Adds a key-value tombstone to the collector: deletes only the single `key` -> `value` pair,
/// leaving any other values for `key` intact.
///
/// Only meaningful for [`FamilyKind::MultiValue`] families, where a key can map to several
/// values and [`Collector::delete`] is too coarse: it would drop the unrelated values too.
/// The motivating case is the task cache, which is keyed by a hash and so holds more than one
/// value whenever two tasks collide. Removing one task must leave the colliding task's entry
/// readable, which requires naming the exact pair to delete.
///
/// Deleting a pair written in the same batch is not supported; see
/// [`WriteBatch::delete_value`][crate::WriteBatch].
///
/// `value` must be at most [`MAX_INLINE_VALUE_SIZE`] bytes; callers must validate this. Larger
/// values could be supported in the future but there is currently no usecase.
pub fn delete_value(&mut self, key: K, value: &[u8]) {
debug_assert!(value.len() <= MAX_INLINE_VALUE_SIZE);
let key = EntryKey {
hash: hash_key(&key),
data: key,
};
self.total_key_size += key.len();
let mut data = [0u8; MAX_INLINE_VALUE_SIZE];
data[..value.len()].copy_from_slice(value);
self.entries.push(CollectorEntry {
key,
value: CollectorEntryValue::KeyValueDeleted {
value: data,
len: value.len() as u8,
},
}); });
} }
...@@ -110,19 +143,19 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> { ...@@ -110,19 +143,19 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> {
self.entries.push(entry); self.entries.push(entry);
} }
/// Sorts entries by key. Tombstones are placed last within each key group. /// Sorts entries by key. Within a key group, key-value tombstones are placed first and key
/// tombstones last (see [`CollectorEntryValue::sort_rank`]).
/// This method does not deduplicate entries. /// This method does not deduplicate entries.
/// ///
/// In debug builds, asserts that SingleValue families have no duplicate keys. /// In debug builds, asserts that SingleValue families have no duplicate keys.
pub fn sorted(&mut self, family_kind: FamilyKind) -> (&[CollectorEntry<K>], usize) { pub fn sorted(&mut self, family_kind: FamilyKind) -> (&[CollectorEntry<K>], usize) {
// Sort by (hash, key) with tombstones placed last within each key group.
// We can use unstable sort because the relative order of equal elements // We can use unstable sort because the relative order of equal elements
// doesn't matter — duplicates are either disallowed (SingleValue) or // doesn't matter — duplicates are either disallowed (SingleValue) or
// allowed without deduplication (MultiValue). // allowed without deduplication (MultiValue).
self.entries.sort_unstable_by(|a, b| { self.entries.sort_unstable_by(|a, b| {
a.key a.key
.cmp(&b.key) .cmp(&b.key)
.then_with(|| a.value.is_deleted().cmp(&b.value.is_deleted())) .then_with(|| a.value.sort_rank().cmp(&b.value.sort_rank()))
}); });
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
......
...@@ -32,17 +32,24 @@ pub enum CollectorEntryValue { ...@@ -32,17 +32,24 @@ pub enum CollectorEntryValue {
Large { Large {
blob: u32, blob: u32,
}, },
Deleted, KeyDeleted,
/// Key-value tombstone: deletes only this one value from the key's group. MultiValue only.
/// The deleted value is stored inline, so it is capped at [`MAX_INLINE_VALUE_SIZE`].
KeyValueDeleted {
value: [u8; MAX_INLINE_VALUE_SIZE],
len: u8,
},
} }
impl CollectorEntryValue { impl CollectorEntryValue {
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
match self { match self {
CollectorEntryValue::Tiny { len, .. } => *len as usize, CollectorEntryValue::KeyValueDeleted { len, .. }
| CollectorEntryValue::Tiny { len, .. } => *len as usize,
CollectorEntryValue::Small { value } => value.len(), CollectorEntryValue::Small { value } => value.len(),
CollectorEntryValue::Medium { value } => value.len(), CollectorEntryValue::Medium { value } => value.len(),
CollectorEntryValue::Large { blob: _ } => 0, CollectorEntryValue::Large { blob: _ } => 0,
CollectorEntryValue::Deleted => 0, CollectorEntryValue::KeyDeleted => 0,
} }
} }
...@@ -51,6 +58,22 @@ impl CollectorEntryValue { ...@@ -51,6 +58,22 @@ impl CollectorEntryValue {
matches!(self, CollectorEntryValue::Medium { .. }) matches!(self, CollectorEntryValue::Medium { .. })
} }
/// The value bytes, or `None` for variants that carry no value data of their own (blob
/// references and key tombstones).
#[cfg(feature = "verify_sst_content")]
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
// Separate arms: the inline buffers have different sizes, so they cannot be bound by
// a single or-pattern.
CollectorEntryValue::Tiny { value, len } => Some(&value[..*len as usize]),
CollectorEntryValue::KeyValueDeleted { value, len } => Some(&value[..*len as usize]),
CollectorEntryValue::Small { value } | CollectorEntryValue::Medium { value } => {
Some(value)
}
CollectorEntryValue::Large { .. } | CollectorEntryValue::KeyDeleted => None,
}
}
/// Returns the value size if it will be packed into a small value block, or 0 otherwise. /// Returns the value size if it will be packed into a small value block, or 0 otherwise.
pub fn small_value_size(&self) -> usize { pub fn small_value_size(&self) -> usize {
match self { match self {
...@@ -62,9 +85,21 @@ impl CollectorEntryValue { ...@@ -62,9 +85,21 @@ impl CollectorEntryValue {
} }
} }
/// Returns true if this value is a deletion tombstone. /// Sort rank within a key group. The two tombstone kinds sit at opposite ends:
pub fn is_deleted(&self) -> bool { ///
matches!(self, CollectorEntryValue::Deleted) /// - Key-value tombstones (rank 0) go **first**, so a reader collects them before the values
/// they filter and can apply them in one forward pass.
/// - Values (rank 1) go in the middle.
/// - Key tombstones (rank 2) go **last**, because they shadow only entries older than
/// themselves — including entries in this same SST. A batch doing `put(A); delete; put(B)`
/// must keep A and B, so a reader that stops at the first key tombstone it sees still returns
/// the same-batch values it already collected.
pub fn sort_rank(&self) -> u8 {
match self {
CollectorEntryValue::KeyValueDeleted { .. } => 0,
CollectorEntryValue::KeyDeleted => 2,
_ => 1,
}
} }
} }
...@@ -133,7 +168,10 @@ impl<K: StoreKey> Entry for CollectorEntry<K> { ...@@ -133,7 +168,10 @@ impl<K: StoreKey> Entry for CollectorEntry<K> {
} }
CollectorEntryValue::Medium { value } => EntryValue::Medium { value }, CollectorEntryValue::Medium { value } => EntryValue::Medium { value },
CollectorEntryValue::Large { blob } => EntryValue::Large { blob: *blob }, CollectorEntryValue::Large { blob } => EntryValue::Large { blob: *blob },
CollectorEntryValue::Deleted => EntryValue::Deleted, CollectorEntryValue::KeyDeleted => EntryValue::KeyDeleted,
CollectorEntryValue::KeyValueDeleted { value, len } => EntryValue::KeyValueDeleted {
value: &value[..*len as usize],
},
} }
} }
} }
...@@ -75,6 +75,9 @@ impl<const FAMILIES: usize> Default for DbConfig<FAMILIES> { ...@@ -75,6 +75,9 @@ impl<const FAMILIES: usize> Default for DbConfig<FAMILIES> {
} }
} }
} }
/// The largest value that [`WriteBatch::delete_value`] can delete, since the tombstone stores
/// a copy of the value inline.
pub use constants::MAX_INLINE_VALUE_SIZE;
pub use key::{KeyBase, QueryKey, StoreKey, hash_key}; pub use key::{KeyBase, QueryKey, StoreKey, hash_key};
pub use meta_file::MetaEntryFlags; pub use meta_file::MetaEntryFlags;
pub use parallel_scheduler::{ParallelScheduler, SerialScheduler}; pub use parallel_scheduler::{ParallelScheduler, SerialScheduler};
......
...@@ -11,7 +11,10 @@ use crate::{ ...@@ -11,7 +11,10 @@ use crate::{
#[derive(PartialEq)] #[derive(PartialEq)]
pub enum LookupValue<B = ArcBytes> { pub enum LookupValue<B = ArcBytes> {
/// The value was deleted. /// The value was deleted.
Deleted, KeyDeleted,
/// A single value was deleted from this key's group (MultiValue families only). Other values
/// for the same key are unaffected. The bytes are the deleted value.
KeyValueDeleted { value: B },
/// The value is stored in the SST file. /// The value is stored in the SST file.
/// ///
/// The bytes will be pointing either at a keyblock or a value block in the SST /// The bytes will be pointing either at a keyblock or a value block in the SST
...@@ -24,7 +27,9 @@ pub enum LookupValue<B = ArcBytes> { ...@@ -24,7 +27,9 @@ pub enum LookupValue<B = ArcBytes> {
/// non-atomic refcounting). /// non-atomic refcounting).
pub enum IterValue { pub enum IterValue {
/// The value was deleted. /// The value was deleted.
Deleted, KeyDeleted,
/// A single value was deleted from this key's group (MultiValue families only).
KeyValueDeleted { value: RcBytes },
/// The value is stored in the SST file. /// The value is stored in the SST file.
Slice { value: RcBytes }, Slice { value: RcBytes },
/// The value is stored in a blob file. /// The value is stored in a blob file.
...@@ -39,7 +44,8 @@ pub enum IterValue { ...@@ -39,7 +44,8 @@ pub enum IterValue {
impl From<LookupValue<RcBytes>> for IterValue { impl From<LookupValue<RcBytes>> for IterValue {
fn from(v: LookupValue<RcBytes>) -> Self { fn from(v: LookupValue<RcBytes>) -> Self {
match v { match v {
LookupValue::Deleted => IterValue::Deleted, LookupValue::KeyDeleted => IterValue::KeyDeleted,
LookupValue::KeyValueDeleted { value } => IterValue::KeyValueDeleted { value },
LookupValue::Slice { value } => IterValue::Slice { value }, LookupValue::Slice { value } => IterValue::Slice { value },
LookupValue::Blob { sequence_number } => IterValue::Blob { sequence_number }, LookupValue::Blob { sequence_number } => IterValue::Blob { sequence_number },
} }
...@@ -70,7 +76,8 @@ impl Entry for LookupEntry { ...@@ -70,7 +76,8 @@ impl Entry for LookupEntry {
fn value(&self) -> EntryValue<'_> { fn value(&self) -> EntryValue<'_> {
match &self.value { match &self.value {
IterValue::Deleted => EntryValue::Deleted, IterValue::KeyDeleted => EntryValue::KeyDeleted,
IterValue::KeyValueDeleted { value } => EntryValue::KeyValueDeleted { value },
IterValue::Slice { value } => { IterValue::Slice { value } => {
if value.len() <= MAX_INLINE_VALUE_SIZE { if value.len() <= MAX_INLINE_VALUE_SIZE {
EntryValue::Inline { value } EntryValue::Inline { value }
......
...@@ -49,6 +49,9 @@ impl Display for MetaEntryFlags { ...@@ -49,6 +49,9 @@ impl Display for MetaEntryFlags {
} }
} }
/// Magic number identifying a `.meta` file.
pub(crate) const META_FILE_MAGIC: u32 = 0xFE4ADA4A;
/// On-disk layout of a single entry header in the `.meta` file. /// On-disk layout of a single entry header in the `.meta` file.
/// ///
/// Fields are big-endian to match the existing wire format written by [`MetaFileBuilder`]. /// Fields are big-endian to match the existing wire format written by [`MetaFileBuilder`].
...@@ -139,6 +142,10 @@ impl MetaEntry { ...@@ -139,6 +142,10 @@ impl MetaEntry {
self.amqf_data_offset.end - self.amqf_data_offset.start self.amqf_data_offset.end - self.amqf_data_offset.start
} }
pub fn amqf(&self) -> &qfilter::FilterRef<'static> {
&self.amqf
}
/// Returns the raw serialized AMQF bytes from the mmap. /// Returns the raw serialized AMQF bytes from the mmap.
pub fn raw_amqf<'l>(&self, amqf_data: &'l [u8]) -> &'l [u8] { pub fn raw_amqf<'l>(&self, amqf_data: &'l [u8]) -> &'l [u8] {
&amqf_data[self.amqf_data_offset.start as usize..self.amqf_data_offset.end as usize] &amqf_data[self.amqf_data_offset.start as usize..self.amqf_data_offset.end as usize]
...@@ -276,7 +283,7 @@ impl MetaFile { ...@@ -276,7 +283,7 @@ impl MetaFile {
// Parse the header from the mmap via ReadBytesExt on &[u8]. // Parse the header from the mmap via ReadBytesExt on &[u8].
let mut reader: &[u8] = &mmap; let mut reader: &[u8] = &mmap;
let magic = reader.read_u32::<BE>()?; let magic = reader.read_u32::<BE>()?;
if magic != 0xFE4ADA4A { if magic != META_FILE_MAGIC {
bail!("Invalid magic number"); bail!("Invalid magic number");
} }
let family = reader.read_u32::<BE>()?; let family = reader.read_u32::<BE>()?;
...@@ -480,10 +487,12 @@ impl MetaFile { ...@@ -480,10 +487,12 @@ impl MetaFile {
// Return immediately with the first result // Return immediately with the first result
return Ok(MetaLookupResult::SstLookup(SstLookupResult::Found(values))); return Ok(MetaLookupResult::SstLookup(SstLookupResult::Found(values)));
} }
// Check for tombstone — stops search across older SSTs within this meta file. // A key tombstone stops the search across older SSTs within this meta file.
// Since tombstones sort last within a key group, if the last value is Deleted, // It sorts last within a key group, so it is the last value if present.
// we have a tombstone. // Key-value tombstones do not stop the search: they delete a single value,
let has_tombstone = values.last().is_some_and(|v| *v == LookupValue::Deleted); // and older SSTs may hold others for this key.
let has_tombstone =
values.last().is_some_and(|v| *v == LookupValue::KeyDeleted);
all_results.extend(values); all_results.extend(values);
if has_tombstone { if has_tombstone {
return Ok(MetaLookupResult::SstLookup(SstLookupResult::Found( return Ok(MetaLookupResult::SstLookup(SstLookupResult::Found(
......
...@@ -9,7 +9,10 @@ use fs_err::File; ...@@ -9,7 +9,10 @@ use fs_err::File;
use qfilter::Filter; use qfilter::Filter;
use zerocopy::IntoBytes; use zerocopy::IntoBytes;
use crate::{meta_file::EntryHeader, static_sorted_file_builder::StaticSortedFileBuilderMeta}; use crate::{
meta_file::{EntryHeader, META_FILE_MAGIC},
static_sorted_file_builder::StaticSortedFileBuilderMeta,
};
pub struct MetaFileBuilder<'a> { pub struct MetaFileBuilder<'a> {
family: u32, family: u32,
...@@ -54,7 +57,7 @@ impl<'a> MetaFileBuilder<'a> { ...@@ -54,7 +57,7 @@ impl<'a> MetaFileBuilder<'a> {
// Wrap the writer to count the bytes written, so callers can accumulate written-byte totals // Wrap the writer to count the bytes written, so callers can accumulate written-byte totals
// without stat'ing the file afterwards. // without stat'ing the file afterwards.
let mut file = CountingWriter::new(BufWriter::new(File::create(file)?)); let mut file = CountingWriter::new(BufWriter::new(File::create(file)?));
file.write_u32::<BE>(0xFE4ADA4A)?; // Magic number file.write_u32::<BE>(META_FILE_MAGIC)?; // Magic number
file.write_u32::<BE>(self.family)?; file.write_u32::<BE>(self.family)?;
self.obsolete_sst_files.sort(); self.obsolete_sst_files.sort();
......
use std::{ use std::{
borrow::Borrow, borrow::Borrow,
fmt::{self, Debug, Formatter}, fmt::{self, Debug, Formatter},
hash::{Hash, Hasher},
ops::{Deref, Range}, ops::{Deref, Range},
rc::Rc, rc::Rc,
}; };
...@@ -75,6 +76,12 @@ impl Debug for RcBytes { ...@@ -75,6 +76,12 @@ impl Debug for RcBytes {
impl Eq for RcBytes {} impl Eq for RcBytes {}
impl Hash for RcBytes {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(self.deref(), state);
}
}
impl SharedBytes for RcBytes { impl SharedBytes for RcBytes {
type MmapHandle = Rc<Mmap>; type MmapHandle = Rc<Mmap>;
......
...@@ -6,7 +6,7 @@ use std::{ ...@@ -6,7 +6,7 @@ use std::{
sync::atomic::{AtomicU32, AtomicU64, Ordering}, sync::atomic::{AtomicU32, AtomicU64, Ordering},
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result, bail};
use byteorder::{BE, WriteBytesExt}; use byteorder::{BE, WriteBytesExt};
use either::Either; use either::Either;
use fs_err::File; use fs_err::File;
...@@ -15,11 +15,11 @@ use smallvec::SmallVec; ...@@ -15,11 +15,11 @@ use smallvec::SmallVec;
use thread_local::ThreadLocal; use thread_local::ThreadLocal;
use crate::{ use crate::{
FamilyConfig, ValueBuffer, FamilyConfig, FamilyKind, ValueBuffer,
collector::Collector, collector::Collector,
collector_entry::CollectorEntry, collector_entry::CollectorEntry,
compression::{checksum_block, compress_into_buffer}, compression::{checksum_block, compress_into_buffer},
constants::{MAX_MEDIUM_VALUE_SIZE, THREAD_LOCAL_SIZE_SHIFT}, constants::{MAX_INLINE_VALUE_SIZE, MAX_MEDIUM_VALUE_SIZE, THREAD_LOCAL_SIZE_SHIFT},
db::WriteOperationGuard, db::WriteOperationGuard,
key::StoreKey, key::StoreKey,
meta_file::MetaEntryFlags, meta_file::MetaEntryFlags,
...@@ -245,7 +245,11 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize ...@@ -245,7 +245,11 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
Ok(()) Ok(())
} }
/// Puts a delete operation into the write batch. /// Puts a delete operation into the write batch. This deletes *all* values for `key`.
///
/// Combining this with a [`WriteBatch::put`] of the same key in the same batch is **not
/// supported**: which one wins is undefined, and callers are expected to resolve the intent
/// themselves before writing.
pub fn delete(&self, family: u32, key: K) -> Result<()> { pub fn delete(&self, family: u32, key: K) -> Result<()> {
let state = self.thread_local_state(); let state = self.thread_local_state();
let collector = self.thread_local_collector_mut(state, family)?; let collector = self.thread_local_collector_mut(state, family)?;
...@@ -253,6 +257,39 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize ...@@ -253,6 +257,39 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
Ok(()) Ok(())
} }
/// Deletes a single key-value pair, leaving any other values for `key` intact.
///
/// Only valid for [`FamilyKind::MultiValue`] families: in a `SingleValue` family a key has one
/// value and [`WriteBatch::delete`] already removes it exactly.
///
/// Deleting a pair that is written in the same batch — by this or any other operation on the
/// key — is **not supported**, for the reason given on [`WriteBatch::delete`]: which one wins
/// is undefined, and it is the caller's job to resolve that before writing.
///
/// Only values of at most [`MAX_INLINE_VALUE_SIZE`] bytes can be deleted this way. This is a
/// simplifying limitation that could be relaxed if needed. Of course in general the storage
/// overhead of deleting large values by value makes it apriori inefficient.
pub fn delete_value(&self, family: u32, key: K, value: ValueBuffer<'_>) -> Result<()> {
let family_config = &self.family_configs[usize_from_u32(family)];
if family_config.kind != FamilyKind::MultiValue {
bail!(
"delete_value is only valid for MultiValue families, but family {} is SingleValue",
family_config.name
);
}
if value.len() > MAX_INLINE_VALUE_SIZE {
bail!(
"delete_value only supports values of at most {MAX_INLINE_VALUE_SIZE} bytes, got \
{} bytes",
value.len()
);
}
let state = self.thread_local_state();
let collector = self.thread_local_collector_mut(state, family)?;
collector.delete_value(key, &value);
Ok(())
}
/// Flushes a family of the write batch, reducing the amount of buffered memory used. /// Flushes a family of the write batch, reducing the amount of buffered memory used.
/// Does not commit any data persistently. /// Does not commit any data persistently.
/// ///
...@@ -547,10 +584,22 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize ...@@ -547,10 +584,22 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
"we wrote a blob but did not read it" "we wrote a blob but did not read it"
); );
} }
CollectorEntryValue::Deleted => assert!( // Key tombstones sort last within a key group, so a same-batch
values.first() == Some(&LookupValue::Deleted), // `put(K, v); delete(K)` reads back as [v, KeyDeleted].
"we wrote a deleted tombstone but it was not first in results" CollectorEntryValue::KeyDeleted => assert!(
values.last() == Some(&LookupValue::KeyDeleted),
"we wrote a key tombstone but it was not last in results"
), ),
CollectorEntryValue::KeyValueDeleted { value, len } => {
let expected = &value[..*len as usize];
assert!(
values.iter().any(|lv| matches!(
lv,
LookupValue::KeyValueDeleted { value } if &**value == expected
)),
"we wrote a key-value tombstone but did not read it back"
)
}
v => { v => {
assert!( assert!(
values.into_iter().any(|lv| { values.into_iter().any(|lv| {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment