|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +//! CRDT movable-list operation helpers for `NodeDbLite`. |
| 4 | +//! |
| 5 | +//! Reuses the already-wired execution helpers in |
| 6 | +//! `crate::query::crdt_ops::list` — the same handlers the native-protocol |
| 7 | +//! dispatch path (`query::physical_visitor::adapter::crdt`) drives for |
| 8 | +//! `PhysicalPlan::Crdt(CrdtOp::List*)` — instead of duplicating the Loro |
| 9 | +//! movable-list logic here. |
| 10 | +
|
| 11 | +use std::collections::HashMap; |
| 12 | + |
| 13 | +use nodedb_types::error::{NodeDbError, NodeDbResult}; |
| 14 | +use nodedb_types::value::Value; |
| 15 | + |
| 16 | +use crate::nodedb::NodeDbLite; |
| 17 | +use crate::query::crdt_ops::list; |
| 18 | +use crate::storage::engine::StorageEngine; |
| 19 | + |
| 20 | +/// Extract the field map from `fields`, rejecting anything that is not a |
| 21 | +/// JSON-object-shaped `Value::Object`. |
| 22 | +/// |
| 23 | +/// `handle_list_insert` treats `fields_json` as a JSON object whose |
| 24 | +/// top-level keys become fields on the new list block; a scalar or array |
| 25 | +/// here would silently produce a malformed block. |
| 26 | +fn require_object_fields(fields: &Value) -> NodeDbResult<&HashMap<String, Value>> { |
| 27 | + match fields { |
| 28 | + Value::Object(obj) => Ok(obj), |
| 29 | + other => Err(NodeDbError::bad_request(format!( |
| 30 | + "list_insert: fields must be Value::Object, got {other:?}" |
| 31 | + ))), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<S: StorageEngine> NodeDbLite<S> { |
| 36 | + pub(super) async fn list_insert_impl( |
| 37 | + &self, |
| 38 | + collection: &str, |
| 39 | + document_id: &str, |
| 40 | + list_path: &str, |
| 41 | + index: usize, |
| 42 | + fields: &Value, |
| 43 | + ) -> NodeDbResult<()> { |
| 44 | + let obj = require_object_fields(fields)?; |
| 45 | + let fields_json = sonic_rs::to_string(obj) |
| 46 | + .map_err(|e| NodeDbError::serialization("json", format!("list_insert fields: {e}")))?; |
| 47 | + |
| 48 | + list::handle_list_insert( |
| 49 | + &self.query_engine, |
| 50 | + collection, |
| 51 | + document_id, |
| 52 | + list_path, |
| 53 | + index, |
| 54 | + &fields_json, |
| 55 | + ) |
| 56 | + .await?; |
| 57 | + Ok(()) |
| 58 | + } |
| 59 | + |
| 60 | + pub(super) async fn list_delete_impl( |
| 61 | + &self, |
| 62 | + collection: &str, |
| 63 | + document_id: &str, |
| 64 | + list_path: &str, |
| 65 | + index: usize, |
| 66 | + ) -> NodeDbResult<()> { |
| 67 | + list::handle_list_delete( |
| 68 | + &self.query_engine, |
| 69 | + collection, |
| 70 | + document_id, |
| 71 | + list_path, |
| 72 | + index, |
| 73 | + ) |
| 74 | + .await?; |
| 75 | + Ok(()) |
| 76 | + } |
| 77 | + |
| 78 | + pub(super) async fn list_move_impl( |
| 79 | + &self, |
| 80 | + collection: &str, |
| 81 | + document_id: &str, |
| 82 | + list_path: &str, |
| 83 | + from_index: usize, |
| 84 | + to_index: usize, |
| 85 | + ) -> NodeDbResult<()> { |
| 86 | + list::handle_list_move( |
| 87 | + &self.query_engine, |
| 88 | + collection, |
| 89 | + document_id, |
| 90 | + list_path, |
| 91 | + from_index, |
| 92 | + to_index, |
| 93 | + ) |
| 94 | + .await?; |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use super::*; |
| 102 | + |
| 103 | + fn obj_fields() -> Value { |
| 104 | + let mut map = HashMap::new(); |
| 105 | + map.insert("title".to_string(), Value::String("hello".to_string())); |
| 106 | + map.insert("done".to_string(), Value::Bool(false)); |
| 107 | + Value::Object(map) |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn require_object_fields_accepts_object() { |
| 112 | + let fields = obj_fields(); |
| 113 | + let obj = require_object_fields(&fields).expect("object fields must pass"); |
| 114 | + assert_eq!(obj.get("title"), Some(&Value::String("hello".to_string()))); |
| 115 | + assert_eq!(obj.get("done"), Some(&Value::Bool(false))); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn require_object_fields_rejects_scalar() { |
| 120 | + let err = |
| 121 | + require_object_fields(&Value::Integer(1)).expect_err("scalar fields must be rejected"); |
| 122 | + assert!(format!("{err}").to_lowercase().contains("object")); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn require_object_fields_rejects_array() { |
| 127 | + let err = require_object_fields(&Value::Array(vec![Value::Integer(1)])) |
| 128 | + .expect_err("array fields must be rejected"); |
| 129 | + assert!(format!("{err}").to_lowercase().contains("object")); |
| 130 | + } |
| 131 | +} |
0 commit comments