-
Notifications
You must be signed in to change notification settings - Fork 557
/
local.rs
385 lines (344 loc) · 14 KB
/
local.rs
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
use super::{Catalog, Transaction as _};
use crate::encoding::{self, Key as _, Value as _};
use crate::error::Result;
use crate::sql::types::schema::Table;
use crate::sql::types::{Expression, Row, Rows, Value};
use crate::storage::{self, mvcc};
use crate::{errdata, errinput};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
/// A SQL engine using local storage. This provides the main SQL storage logic,
/// also for the Raft SQL engine which dispatches to this engine for node-local
/// SQL storage.
pub struct Local<E: storage::Engine> {
/// The local MVCC storage engine.
pub(super) mvcc: mvcc::MVCC<E>,
}
impl<E: storage::Engine> Local<E> {
/// Creates a new local SQL engine using the given storage engine.
pub fn new(engine: E) -> Self {
Self { mvcc: mvcc::MVCC::new(engine) }
}
/// Resumes a transaction from the given state. This is usually encapsulated
/// in `mvcc::Transaction`, but the Raft-based engine can't retain the MVCC
/// transaction between each request since it may be executed across
/// multiple leader nodes, so it instead keeps the state in the session.
pub fn resume(&self, state: mvcc::TransactionState) -> Result<Transaction<E>> {
Ok(Transaction::new(self.mvcc.resume(state)?))
}
/// Fetches an unversioned key.
pub fn get_unversioned(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
self.mvcc.get_unversioned(key)
}
/// Sets an unversioned key.
pub fn set_unversioned(&self, key: &[u8], value: Vec<u8>) -> Result<()> {
self.mvcc.set_unversioned(key, value)
}
}
impl<'a, E: storage::Engine + 'a> super::Engine<'a> for Local<E> {
type Transaction = Transaction<E>;
fn begin(&self) -> Result<Self::Transaction> {
Ok(Self::Transaction::new(self.mvcc.begin()?))
}
fn begin_read_only(&self) -> Result<Self::Transaction> {
Ok(Self::Transaction::new(self.mvcc.begin_read_only()?))
}
fn begin_as_of(&self, version: mvcc::Version) -> Result<Self::Transaction> {
Ok(Self::Transaction::new(self.mvcc.begin_as_of(version)?))
}
}
/// A SQL transaction, wrapping an MVCC transaction.
pub struct Transaction<E: storage::Engine> {
txn: mvcc::Transaction<E>,
}
impl<E: storage::Engine> Transaction<E> {
/// Creates a new SQL transaction using the given MVCC transaction.
fn new(txn: mvcc::Transaction<E>) -> Self {
Self { txn }
}
/// Returns the transaction's internal state.
pub(super) fn state(&self) -> &mvcc::TransactionState {
self.txn.state()
}
/// Fetches the matching primary keys for the given indexed column value, or
/// an empty set if there is none.
fn get_index(&self, table: &str, column: &str, value: &Value) -> Result<HashSet<Value>> {
Ok(self
.txn
.get(&Key::Index(table.into(), column.into(), value.into()).encode())?
.map(|v| HashSet::decode(&v))
.transpose()?
.unwrap_or_default())
}
/// Stores an index entry for the given column value.
fn set_index(
&self,
table: &str,
column: &str,
value: &Value,
ids: HashSet<Value>,
) -> Result<()> {
let key = Key::Index(table.into(), column.into(), value.into()).encode();
if ids.is_empty() {
self.txn.delete(&key)
} else {
self.txn.set(&key, ids.encode())
}
}
/// Returns all foreign key references to a table, as table -> columns.
/// This includes references from the table itself.
fn references(&self, table: &str) -> Result<Vec<(String, Vec<String>)>> {
Ok(self
.list_tables()?
.into_iter()
.map(|t| {
let references: Vec<String> = t
.columns
.into_iter()
.filter(|c| c.references.as_deref() == Some(table))
.map(|c| c.name)
.collect();
(t.name, references)
})
.filter(|(_, references)| !references.is_empty())
.collect())
}
}
impl<E: storage::Engine> super::Transaction for Transaction<E> {
fn version(&self) -> u64 {
self.txn.version()
}
fn read_only(&self) -> bool {
self.txn.read_only()
}
fn commit(self) -> Result<()> {
self.txn.commit()
}
fn rollback(self) -> Result<()> {
self.txn.rollback()
}
fn delete(&self, table: &str, ids: &[Value]) -> Result<()> {
// Check for foreign key referenes.
// TODO: try to be more clever than simply iterating over each ID.
for id in ids {
let table = self.must_get_table(table)?;
for (t, cs) in self.references(&table.name)? {
let t = self.must_get_table(&t)?;
let cs = cs
.into_iter()
.map(|c| Ok((t.get_column_index(&c)?, c)))
.collect::<Result<Vec<_>>>()?;
let mut scan = self.scan(&t.name, None)?;
while let Some(row) = scan.next().transpose()? {
for (i, c) in &cs {
if &row[*i] == id
&& (table.name != t.name || id != table.get_row_key(&row)?)
{
return errinput!(
"primary key {id} referenced by table {} column {c}",
t.name
);
}
}
}
}
let indexes: Vec<_> =
table.columns.iter().enumerate().filter(|(_, c)| c.index).collect();
if !indexes.is_empty() {
for row in self.get(&table.name, &[id.clone()])? {
for (i, column) in &indexes {
let mut index = self.get_index(&table.name, &column.name, &row[*i])?;
index.remove(id);
self.set_index(&table.name, &column.name, &row[*i], index)?;
}
}
}
self.txn.delete(&Key::Row(table.name.into(), id.into()).encode())?;
}
Ok(())
}
fn get(&self, table: &str, ids: &[Value]) -> Result<Vec<Row>> {
ids.iter()
.filter_map(|id| {
self.txn
.get(&Key::Row(table.into(), id.into()).encode())
.transpose()
.map(|r| r.and_then(|v| Row::decode(&v)))
})
.collect()
}
fn insert(&self, table: &str, rows: Vec<Row>) -> Result<()> {
let table = self.must_get_table(table)?;
for row in rows {
table.validate_row(&row, self)?;
let id = table.get_row_key(&row)?;
if !self.get(&table.name, &[id.clone()])?.is_empty() {
return errinput!("primary key {id} already exists for table {}", table.name);
}
self.txn.set(&Key::Row((&table.name).into(), id.into()).encode(), row.encode())?;
// Update indexes
for (i, column) in table.columns.iter().enumerate().filter(|(_, c)| c.index) {
let mut index = self.get_index(&table.name, &column.name, &row[i])?;
index.insert(id.clone());
self.set_index(&table.name, &column.name, &row[i], index)?;
}
}
Ok(())
}
fn lookup_index(&self, table: &str, column: &str, value: &[Value]) -> Result<HashSet<Value>> {
if !self.must_get_table(table)?.get_column(column)?.index {
return errinput!("no index on {table}.{column}");
}
let mut pks = HashSet::new();
for v in value {
pks.extend(self.get_index(table, column, v)?)
}
Ok(pks)
}
fn scan(&self, table: &str, filter: Option<Expression>) -> Result<Rows> {
let table = self.must_get_table(table)?;
Ok(Box::new(
self.txn
.scan_prefix(&KeyPrefix::Row((&table.name).into()).encode())
.iter()
.map(|r| r.and_then(|(_, v)| Row::decode(&v)))
.filter_map(move |r| match r {
Ok(row) => match &filter {
Some(filter) => match filter.evaluate(Some(&row)) {
Ok(Value::Boolean(b)) if b => Some(Ok(row)),
Ok(Value::Boolean(_)) | Ok(Value::Null) => None,
Ok(v) => Some(errdata!("filter returned {v}, expected boolean")),
Err(err) => Some(Err(err)),
},
None => Some(Ok(row)),
},
err => Some(err),
})
.collect::<Vec<_>>()
.into_iter(),
))
}
fn scan_index(&self, table: &str, column: &str) -> Result<super::IndexScan> {
let table = self.must_get_table(table)?;
let column = table.get_column(column)?;
if !column.index {
return errinput!("no index for {}.{}", table.name, column.name);
}
Ok(Box::new(
self.txn
.scan_prefix(
&KeyPrefix::Index((&table.name).into(), (&column.name).into()).encode(),
)
.iter()
.map(|r| -> Result<(Value, HashSet<Value>)> {
let (k, v) = r?;
let value = match Key::decode(&k)? {
Key::Index(_, _, pk) => pk.into_owned(),
_ => return errdata!("invalid index key"),
};
Ok((value, HashSet::<Value>::decode(&v)?))
})
.collect::<Vec<_>>()
.into_iter(),
))
}
fn update(&self, table: &str, rows: HashMap<Value, Row>) -> Result<()> {
let table = self.must_get_table(table)?;
// TODO: be more clever than just iterating here.
for (id, row) in rows {
// If the primary key changes we do a delete and create, otherwise we replace the row
if id != *table.get_row_key(&row)? {
self.delete(&table.name, &[id.clone()])?;
self.insert(&table.name, vec![row])?;
return Ok(());
}
// Update indexes, knowing that the primary key has not changed
let indexes: Vec<_> =
table.columns.iter().enumerate().filter(|(_, c)| c.index).collect();
if !indexes.is_empty() {
let old = self.get(&table.name, &[id.clone()])?.remove(0);
for (i, column) in indexes {
if old[i] == row[i] {
continue;
}
let mut index = self.get_index(&table.name, &column.name, &old[i])?;
index.remove(&id);
self.set_index(&table.name, &column.name, &old[i], index)?;
let mut index = self.get_index(&table.name, &column.name, &row[i])?;
index.insert(id.clone());
self.set_index(&table.name, &column.name, &row[i], index)?;
}
}
table.validate_row(&row, self)?;
self.txn.set(&Key::Row((&table.name).into(), id.into()).encode(), row.encode())?;
}
Ok(())
}
}
impl<E: storage::Engine> Catalog for Transaction<E> {
fn create_table(&self, table: Table) -> Result<()> {
if self.get_table(&table.name)?.is_some() {
return errinput!("table {} already exists", table.name);
}
table.validate(self)?;
self.txn.set(&Key::Table((&table.name).into()).encode(), table.encode())
}
fn drop_table(&self, table: &str, if_exists: bool) -> Result<bool> {
let table = if !if_exists {
self.must_get_table(table)?
} else if let Some(table) = self.get_table(table)? {
table
} else {
return Ok(false);
};
if let Some((t, cs)) = self.references(&table.name)?.iter().find(|(t, _)| *t != table.name)
{
return errinput!("table {} is referenced by table {} column {}", table.name, t, cs[0]);
}
let mut scan = self.scan(&table.name, None)?;
while let Some(row) = scan.next().transpose()? {
self.delete(&table.name, &[table.get_row_key(&row)?.clone()])?
}
self.txn.delete(&Key::Table(table.name.into()).encode())?;
Ok(true)
}
fn get_table(&self, table: &str) -> Result<Option<Table>> {
self.txn.get(&Key::Table(table.into()).encode())?.map(|v| Table::decode(&v)).transpose()
}
fn list_tables(&self) -> Result<Vec<Table>> {
self.txn
.scan_prefix(&KeyPrefix::Table.encode())
.iter()
.map(|r| r.and_then(|(_, v)| Table::decode(&v)))
.collect()
}
}
/// SQL engine keys, using the KeyCode order-preserving encoding. For
/// simplicity, table and column names are used directly as identifiers in
/// keys, instead of e.g. numberic IDs. It is not possible to change
/// table/column names, so this is fine.
///
/// Uses Cow to allow encoding borrowed values but decoding owned values.
#[derive(Deserialize, Serialize)]
enum Key<'a> {
/// A table schema by table name.
Table(Cow<'a, str>),
/// An index entry, by table name, index name, and index value.
Index(Cow<'a, str>, Cow<'a, str>, Cow<'a, Value>),
/// A table row, by table name and primary key value.
Row(Cow<'a, str>, Cow<'a, Value>),
}
impl<'a> encoding::Key<'a> for Key<'a> {}
/// Key prefixes, allowing prefix scans of specific parts of the keyspace. These
/// must match the keys -- in particular, the enum variant indexes must match.
#[derive(Deserialize, Serialize)]
enum KeyPrefix<'a> {
/// All table schemas.
Table,
/// An entire table index, by table and index name.
Index(Cow<'a, str>, Cow<'a, str>),
/// An entire table's rows, by table name.
Row(Cow<'a, str>),
}
impl<'a> encoding::Key<'a> for KeyPrefix<'a> {}