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
//! Delete every live document matching a predicate in one mutation, then
//! remove the deleted bytes from every store file (ZE-217).
use crate::lifecycle::{QueryError, Store, StoreError, StoreState};
use crate::meta::Predicate;
use crate::planner::PlanError;
use super::{DocId, IngestError, PurgeError};
/// Outcome of [`Store::delete_matching`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeleteMatchingReport {
deleted_ids: Vec<DocId>,
generation: u64,
}
impl DeleteMatchingReport {
/// Ascending ids of every deleted document; empty when nothing matched.
#[must_use]
pub fn deleted_ids(&self) -> &[DocId] {
&self.deleted_ids
}
/// Store generation when the call returned. Unchanged when nothing
/// matched; otherwise the generation after the physical purge.
#[must_use]
pub const fn generation(&self) -> u64 {
self.generation
}
}
/// Why [`Store::delete_matching`] failed.
#[derive(Debug)]
pub enum DeleteMatchingError {
/// The predicate does not fit the store schema. Nothing changed.
Predicate(PlanError),
/// Resolving the matching documents failed. Nothing changed.
Query(QueryError),
/// The tombstone mutation failed. Nothing changed.
Delete(IngestError),
/// Admission, the purge intent, or the physical purge failed. When the
/// documents were already tombstoned, a writable open finishes the purge.
Purge(PurgeError),
}
impl std::fmt::Display for DeleteMatchingError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Predicate(error) => write!(formatter, "delete filter: {error}"),
Self::Query(error) => write!(formatter, "delete match: {error}"),
Self::Delete(error) => write!(formatter, "delete: {error}"),
Self::Purge(error) => write!(formatter, "delete purge: {error}"),
}
}
}
impl std::error::Error for DeleteMatchingError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Predicate(error) => Some(error),
Self::Query(error) => Some(error),
Self::Delete(error) => Some(error),
Self::Purge(error) => Some(error),
}
}
}
impl From<StoreError> for DeleteMatchingError {
fn from(error: StoreError) -> Self {
Self::Purge(PurgeError::Store(error))
}
}
impl Store {
/// Deletes every live document whose current version matches
/// `predicate`, atomically, and physically removes their bytes.
///
/// The whole call holds the maintenance, state, writer and WAL locks, so
/// no other write lands between resolving the matches and deleting them.
/// The order is: resolve the ids; durably record a purge intent for them;
/// tombstone them all in one WAL record at one generation (readers see all
/// of them or none of them); then run the physical purge, which rewrites
/// every affected segment and the WAL. When this returns, no deleted byte
/// remains in any store file. A crash before the intent is durable deletes
/// nothing; after it, the next writable open completes the purge.
///
/// A cascade (ZE-225) extends the id set between resolution and
/// `delete_and_purge_locked`, inside the same held locks.
pub fn delete_matching(
&self,
predicate: &Predicate,
) -> Result<DeleteMatchingReport, DeleteMatchingError> {
crate::planner::validate_predicate(predicate, &self.schema)
.map_err(DeleteMatchingError::Predicate)?;
let _maintenance = self
.maintenance
.lock()
.map_err(|_| StoreError::Synchronization {
component: "delete matching",
})?;
let state = self
.state
.lock()
.map_err(|_| StoreError::Synchronization { component: "state" })?;
match *state {
StoreState::Open => {}
StoreState::Closing => return Err(StoreError::Closing.into()),
StoreState::Closed => return Err(StoreError::Closed.into()),
}
let writer_lock = self
.writer_lock
.lock()
.map_err(|_| StoreError::Synchronization {
component: "writer lock",
})?;
if writer_lock.is_none() {
return Err(StoreError::ReadOnly.into());
}
self.require_no_snapshot_views()?;
let mut wal = self
.wal_writer
.lock()
.map_err(|_| StoreError::Synchronization {
component: "WAL writer",
})?;
let writer = wal.as_mut().ok_or(StoreError::ReadOnly)?;
let (generation, active) = {
let active = self
.active
.lock()
.map_err(|_| StoreError::Synchronization {
component: "active segment",
})?;
let current = active.as_ref().ok_or(StoreError::Closed)?;
(current.generation, std::sync::Arc::clone(¤t.segment))
};
let snapshot = self
.snapshot
.read()
.map_err(|_| StoreError::Synchronization {
component: "published snapshot",
})?
.as_ref()
.cloned()
.ok_or(StoreError::Closed)?;
let ids = self
.live_document_ids_matching(&active, snapshot.segments(), predicate)
.map_err(DeleteMatchingError::Query)?;
drop(snapshot);
drop(active);
let report = if ids.is_empty() {
DeleteMatchingReport {
deleted_ids: ids,
generation,
}
} else {
self.delete_and_purge_locked(writer, ids)?
};
drop(wal);
drop(writer_lock);
drop(state);
Ok(report)
}
/// Records the purge intent for `ids`, tombstones them in one mutation
/// and completes the physical purge. The caller holds the maintenance,
/// state, writer and WAL locks; `ids` are ascending, unique and non-empty.
fn delete_and_purge_locked(
&self,
writer: &mut super::StoreWal,
ids: Vec<DocId>,
) -> Result<DeleteMatchingReport, DeleteMatchingError> {
let vfs = self.vfs.as_ref();
let available = self
.available_purge_bytes()
.map_err(DeleteMatchingError::Purge)?;
let token = self
.schedule_purge_locked(&ids, available, vfs)
.map_err(DeleteMatchingError::Purge)?;
if let Err(error) = self.delete_with_writer(writer, &ids, &[]) {
// The tombstones did not commit, so the intent must not purge
// these documents at the next open.
self.abandon_scheduled_purge_locked(vfs)
.map_err(DeleteMatchingError::Purge)?;
return Err(DeleteMatchingError::Delete(error));
}
let purged = self
.complete_physical_purge_locked(vfs, writer, token)
.map_err(DeleteMatchingError::Purge)?;
Ok(DeleteMatchingReport {
deleted_ids: ids,
generation: purged.generation(),
})
}
}