(
&mut self,
task: &ExecutionTask,
tid: u64,
collection: &str,
document_id: &str,
surrogate: Surrogate,
value: &[u8],
if_absent: bool,
| 20 | impl CoreLoop { |
| 21 | #[allow(clippy::too_many_arguments)] |
| 22 | pub(in crate::data::executor) fn execute_point_insert( |
| 23 | &mut self, |
| 24 | task: &ExecutionTask, |
| 25 | tid: u64, |
| 26 | collection: &str, |
| 27 | document_id: &str, |
| 28 | surrogate: Surrogate, |
| 29 | value: &[u8], |
| 30 | if_absent: bool, |
| 31 | ) -> Response { |
| 32 | let row_key = surrogate_to_doc_id(surrogate); |
| 33 | let row_key = row_key.as_str(); |
| 34 | debug!( |
| 35 | core = self.core_id, |
| 36 | %collection, %document_id, if_absent, |
| 37 | "point insert" |
| 38 | ); |
| 39 | |
| 40 | let txn = match self.sparse.begin_write() { |
| 41 | Ok(t) => t, |
| 42 | Err(e) => return self.response_error(task, e), |
| 43 | }; |
| 44 | |
| 45 | // Existence probe inside the write transaction: linearizable with |
| 46 | // the apply_point_put commit — no other writer can insert between |
| 47 | // this check and our insert commit. Probe uses `document_id` as |
| 48 | // the row key, which is how the primary key is encoded for strict |
| 49 | // and schemaless collections alike (see `dml::convert_insert`). |
| 50 | let bitemporal = self.is_bitemporal(tid, collection); |
| 51 | let exists_result = if bitemporal { |
| 52 | self.sparse |
| 53 | .versioned_exists_current_in_txn(&txn, tid, collection, row_key) |
| 54 | } else { |
| 55 | self.sparse.exists_in_txn(&txn, tid, collection, row_key) |
| 56 | }; |
| 57 | match exists_result { |
| 58 | Ok(true) => { |
| 59 | // Drop the txn without committing — no-op on redb. |
| 60 | if if_absent { |
| 61 | // `INSERT ... ON CONFLICT DO NOTHING`: silent skip. |
| 62 | return self.response_ok(task); |
| 63 | } |
| 64 | return self.response_error( |
| 65 | task, |
| 66 | crate::Error::RejectedConstraint { |
| 67 | collection: collection.to_string(), |
| 68 | constraint: "unique".to_string(), |
| 69 | detail: format!( |
| 70 | "duplicate key value '{document_id}' violates primary-key \ |
| 71 | uniqueness on '{collection}'" |
| 72 | ), |
| 73 | }, |
| 74 | ); |
| 75 | } |
| 76 | Ok(false) => {} |
| 77 | Err(e) => return self.response_error(task, e), |
| 78 | } |
| 79 |
no test coverage detected