solidb 1.2.2

A lightweight, high-performance structured database server written in Rust.
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
//! Per-row mutation writes: UPDATE / REPLACE / INSERT with `OPTIONS`, and the
//! `OLD` / `NEW` pseudo-variables.
//!
//! `clauses.rs` keeps its bulk fast paths for plain mutations. As soon as a
//! statement needs something those paths cannot give — a pre-image (`OLD`),
//! a per-row `NEW` on UPDATE, `REPLACE`, or any OPTIONS — it writes row by
//! row through the helpers here.

use serde_json::{Map, Value};

use super::super::QueryExecutor;
use crate::error::{DbError, DbResult};
use crate::sdbql::ast::{MutationOptions, OverwriteMode};
use crate::storage::Collection;
use crate::sync::protocol::Operation;

/// What one row's write produced: the stored document before and after.
/// `None` where there is no such document (`OLD` of an insert, `NEW` of a
/// remove or of an ignored insert).
pub(super) struct RowWrite {
    pub old: Option<Value>,
    pub new: Option<Value>,
    /// Counted as an update (`true`) or an insert (`false`) in the stats.
    pub updated: bool,
    /// Nothing was written (`overwriteMode: "ignore"` on an existing key).
    pub skipped: bool,
}

/// Document-level failures that `OPTIONS { ignoreErrors: true }` skips.
/// Anything else — an expression error, a timeout, storage trouble — still
/// fails the query.
pub(super) fn is_document_error(err: &DbError) -> bool {
    matches!(
        err,
        DbError::DocumentNotFound(_)
            | DbError::InvalidDocument(_)
            | DbError::ConflictError(_)
            | DbError::SchemaValidationError(_)
    )
}

/// The `_key` a mutation selector names: a key string, or a document with a
/// string `_key`. Errors are `InvalidDocument`, which `ignoreErrors` skips.
pub(super) fn selector_key(value: &Value, statement: &str) -> DbResult<String> {
    match value {
        Value::String(s) => Ok(s.clone()),
        Value::Object(obj) => obj
            .get("_key")
            .and_then(|v| v.as_str())
            .map(str::to_string)
            .ok_or_else(|| {
                DbError::InvalidDocument(format!(
                    "{}: selector object must have a _key field",
                    statement
                ))
            }),
        _ => Err(DbError::InvalidDocument(format!(
            "{}: selector must be a string key or an object with _key field",
            statement
        ))),
    }
}

/// Merge `patch` into `target` with AQL's `keepNull` / `mergeObjects`.
///
/// Top-level system attributes (`_key`, `_rev`, ...) in the patch are
/// skipped, as the storage layer's own merge skips them.
pub(super) fn merge_patch(
    target: &mut Map<String, Value>,
    patch: &Map<String, Value>,
    keep_null: bool,
    merge_objects: bool,
    top_level: bool,
) {
    for (key, value) in patch {
        if top_level && key.starts_with('_') {
            continue;
        }
        if value.is_null() && !keep_null {
            target.remove(key);
            continue;
        }
        match (value, target.get_mut(key)) {
            (Value::Object(sub_patch), Some(Value::Object(existing))) if merge_objects => {
                merge_patch(existing, sub_patch, keep_null, merge_objects, false);
            }
            (Value::Object(_), _) if !keep_null => {
                target.insert(key.clone(), strip_nulls(value.clone()));
            }
            _ => {
                target.insert(key.clone(), value.clone());
            }
        }
    }
}

/// Remove `null`-valued attributes from objects, recursively (arrays are left
/// alone, as in AQL).
fn strip_nulls(value: Value) -> Value {
    match value {
        Value::Object(map) => Value::Object(
            map.into_iter()
                .filter(|(_, v)| !v.is_null())
                .map(|(k, v)| (k, strip_nulls(v)))
                .collect(),
        ),
        other => other,
    }
}

/// The body handed to `insert_or_replace` for a REPLACE of `key`: the new
/// document minus the attributes the server owns, with `_key` pinned.
fn replacement_body(document: Value, key: &str, statement: &str) -> DbResult<Value> {
    let Value::Object(mut map) = document else {
        return Err(DbError::InvalidDocument(format!(
            "{}: the replacement must be an object",
            statement
        )));
    };
    for system in ["_id", "_rev", "_created_at", "_updated_at"] {
        map.remove(system);
    }
    map.insert("_key".to_string(), Value::String(key.to_string()));
    Ok(Value::Object(map))
}

impl<'a> QueryExecutor<'a> {
    /// UPDATE (merge) or REPLACE the document `key` with `changes`.
    ///
    /// The storage merge is shallow and keeps nulls; when OPTIONS ask for
    /// something else the merged document is computed here and written with
    /// `insert_or_replace`. That path, like REPLACE, reads then writes, so a
    /// concurrent writer of the same document between the two can be lost.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn write_update_row(
        &self,
        collection: &Collection,
        collection_name: &str,
        key: &str,
        changes: Value,
        replace: bool,
        options: &MutationOptions,
        want_old: bool,
    ) -> DbResult<RowWrite> {
        let statement = if replace { "REPLACE" } else { "UPDATE" };
        if !changes.is_object() {
            return Err(DbError::InvalidDocument(format!(
                "{}: changes must be an object",
                statement
            )));
        }

        let (old, doc) = if replace {
            // REPLACE requires the document to exist.
            let existing = collection.get(key)?;
            let body = replacement_body(changes, key, statement)?;
            let doc = collection.insert_or_replace(body)?;
            (Some(existing.into_value()), doc)
        } else if options.needs_custom_merge() {
            let existing = collection.get(key)?;
            let mut data = existing.data.as_object().cloned().unwrap_or_default();
            if let Value::Object(patch) = &changes {
                merge_patch(
                    &mut data,
                    patch,
                    options.keep_null.unwrap_or(true),
                    options.merge_objects.unwrap_or(false),
                    true,
                );
            }
            data.insert("_key".to_string(), Value::String(key.to_string()));
            let doc = collection.insert_or_replace(Value::Object(data))?;
            (Some(existing.into_value()), doc)
        } else {
            let old = if want_old {
                Some(collection.get(key)?.into_value())
            } else {
                None
            };
            (old, collection.update(key, changes)?)
        };

        let new_value = doc.into_value();
        self.log_mutation(collection_name, Operation::Update, key, Some(&new_value));
        Ok(RowWrite {
            old: if want_old { old } else { None },
            new: Some(new_value),
            updated: true,
            skipped: false,
        })
    }

    /// REMOVE the document `key`, reading its pre-image first when `OLD` is
    /// wanted.
    pub(super) fn write_remove_row(
        &self,
        collection: &Collection,
        collection_name: &str,
        key: &str,
        want_old: bool,
    ) -> DbResult<RowWrite> {
        let old = if want_old {
            Some(collection.get(key)?.into_value())
        } else {
            None
        };
        collection.delete(key)?;
        self.log_mutation(collection_name, Operation::Delete, key, None);
        Ok(RowWrite {
            old,
            new: None,
            updated: false,
            skipped: false,
        })
    }

    /// INSERT `document`, honouring `overwriteMode` when its `_key` exists.
    pub(super) fn write_insert_row(
        &self,
        collection: &Collection,
        collection_name: &str,
        document: Value,
        options: &MutationOptions,
        want_old: bool,
    ) -> DbResult<RowWrite> {
        let mode = options.overwrite_mode.unwrap_or(OverwriteMode::Conflict);
        let key = document
            .get("_key")
            .and_then(|k| k.as_str())
            .map(str::to_string);

        let key = match (mode, key) {
            (OverwriteMode::Conflict, _) | (_, None) => {
                return self.plain_insert(collection, collection_name, document);
            }
            (_, Some(key)) => key,
        };

        let existing = match collection.get(&key) {
            Ok(doc) => Some(doc),
            Err(DbError::DocumentNotFound(_)) => None,
            Err(e) => return Err(e),
        };

        let Some(existing) = existing else {
            // Absent now; a concurrent insert of the same key between the
            // lookup and the write surfaces as a conflict. Treat that like
            // the key having existed all along.
            match self.plain_insert(collection, collection_name, document.clone()) {
                Err(DbError::ConflictError(_)) => {}
                other => return other,
            }
            let raced = collection.get(&key)?;
            return self.overwrite_existing(
                collection,
                collection_name,
                &key,
                raced.into_value(),
                document,
                mode,
                options,
                want_old,
            );
        };

        self.overwrite_existing(
            collection,
            collection_name,
            &key,
            existing.into_value(),
            document,
            mode,
            options,
            want_old,
        )
    }

    fn plain_insert(
        &self,
        collection: &Collection,
        collection_name: &str,
        document: Value,
    ) -> DbResult<RowWrite> {
        let doc = collection.insert(document)?;
        let key = doc.key.clone();
        let new_value = doc.into_value();
        self.log_mutation(collection_name, Operation::Insert, &key, Some(&new_value));
        Ok(RowWrite {
            old: None,
            new: Some(new_value),
            updated: false,
            skipped: false,
        })
    }

    #[allow(clippy::too_many_arguments)]
    fn overwrite_existing(
        &self,
        collection: &Collection,
        collection_name: &str,
        key: &str,
        existing: Value,
        document: Value,
        mode: OverwriteMode,
        options: &MutationOptions,
        want_old: bool,
    ) -> DbResult<RowWrite> {
        match mode {
            OverwriteMode::Ignore => Ok(RowWrite {
                old: want_old.then_some(existing),
                new: None,
                updated: false,
                skipped: true,
            }),
            OverwriteMode::Replace => {
                let body = replacement_body(document, key, "INSERT")?;
                let new_value = collection.insert_or_replace(body)?.into_value();
                self.log_mutation(collection_name, Operation::Update, key, Some(&new_value));
                Ok(RowWrite {
                    old: want_old.then_some(existing),
                    new: Some(new_value),
                    updated: true,
                    skipped: false,
                })
            }
            OverwriteMode::Update => {
                let mut write = self.write_update_row(
                    collection,
                    collection_name,
                    key,
                    document,
                    false,
                    options,
                    false,
                )?;
                write.old = want_old.then_some(existing);
                Ok(write)
            }
            OverwriteMode::Conflict => Err(DbError::ConflictError(format!(
                "Document with _key '{}' already exists",
                key
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn obj(v: Value) -> Map<String, Value> {
        v.as_object().cloned().unwrap()
    }

    #[test]
    fn shallow_merge_replaces_nested_objects() {
        let mut t = obj(json!({"a": {"x": 1, "y": 2}, "b": 1}));
        merge_patch(&mut t, &obj(json!({"a": {"x": 9}})), true, false, true);
        assert_eq!(Value::Object(t), json!({"a": {"x": 9}, "b": 1}));
    }

    #[test]
    fn deep_merge_keeps_sibling_attributes() {
        let mut t = obj(json!({"a": {"x": 1, "y": 2}}));
        merge_patch(&mut t, &obj(json!({"a": {"x": 9}})), true, true, true);
        assert_eq!(Value::Object(t), json!({"a": {"x": 9, "y": 2}}));
    }

    #[test]
    fn keep_null_false_removes_attributes_at_every_level() {
        let mut t = obj(json!({"a": 1, "b": {"c": 1, "d": 2}}));
        merge_patch(
            &mut t,
            &obj(json!({"a": null, "b": {"c": null}})),
            false,
            true,
            true,
        );
        assert_eq!(Value::Object(t), json!({"b": {"d": 2}}));
    }

    #[test]
    fn keep_null_true_stores_null() {
        let mut t = obj(json!({"a": 1}));
        merge_patch(&mut t, &obj(json!({"a": null})), true, false, true);
        assert_eq!(Value::Object(t), json!({"a": null}));
    }

    #[test]
    fn system_attributes_in_patch_are_ignored() {
        let mut t = obj(json!({"a": 1}));
        merge_patch(
            &mut t,
            &obj(json!({"_key": "x", "_rev": "r"})),
            true,
            false,
            true,
        );
        assert_eq!(Value::Object(t), json!({"a": 1}));
    }

    #[test]
    fn replacement_body_pins_key_and_drops_server_attributes() {
        let body = replacement_body(
            json!({"_key": "other", "_rev": "r", "v": 1}),
            "k",
            "REPLACE",
        )
        .unwrap();
        assert_eq!(body, json!({"_key": "k", "v": 1}));
        assert!(replacement_body(json!(1), "k", "REPLACE").is_err());
    }

    #[test]
    fn selector_key_forms() {
        assert_eq!(selector_key(&json!("k"), "UPDATE").unwrap(), "k");
        assert_eq!(selector_key(&json!({"_key": "k"}), "UPDATE").unwrap(), "k");
        assert!(is_document_error(
            &selector_key(&json!(1), "UPDATE").unwrap_err()
        ));
    }
}