MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / insert_into_pointer_map

Method insert_into_pointer_map

crates/table/src/table.rs:1014–1051  ·  view source on GitHub ↗

Insert the row identified by `ptr` into the table's [`PointerMap`], if the table has one. This checks for set semantic violations. If a set semantic conflict (i.e. duplicate row) is detected by the pointer map, the row will be deleted and an error returned. If the pointer map confirms that the row was unique, returns the `RowHash` of that row. If this table has no `PointerMap`, returns `Ok(None)

(
        &'a mut self,
        blob_store: &'a mut dyn BlobStore,
        ptr: RowPointer,
    )

Source from the content-addressed store, hash-verified

1012 /// SAFETY: `self.is_row_present(row)` must hold.
1013 /// Post-condition: If this method returns `Ok(_)`, the row still exists.
1014 unsafe fn insert_into_pointer_map<'a>(
1015 &'a mut self,
1016 blob_store: &'a mut dyn BlobStore,
1017 ptr: RowPointer,
1018 ) -> Result<Option<RowHash>, DuplicateError> {
1019 if self.pointer_map.is_none() {
1020 // No pointer map? Set semantic constraint is checked by a unique index instead.
1021 return Ok(None);
1022 };
1023
1024 // SAFETY:
1025 // - `self` trivially has the same `row_layout` as `self`.
1026 // - Caller promised that `self.is_row_present(row)` holds.
1027 let (hash, existing_row) = unsafe { Self::find_same_row_via_pointer_map(self, self, blob_store, ptr, None) };
1028
1029 if let Some(existing_row) = existing_row {
1030 // If an equal row was already present,
1031 // roll back our optimistic insert to avoid violating set semantics.
1032
1033 // SAFETY: Caller promised that `ptr` is a valid row in `self`.
1034 unsafe {
1035 self.inner
1036 .pages
1037 .delete_row(&self.inner.visitor_prog, self.row_size(), ptr, blob_store)
1038 };
1039 return Err(DuplicateError(existing_row));
1040 }
1041
1042 // If the optimistic insertion was correct,
1043 // i.e. this is not a set-semantic duplicate,
1044 // add it to the `pointer_map`.
1045 self.pointer_map
1046 .as_mut()
1047 .expect("pointer map should exist, as it did previously")
1048 .insert(hash, ptr);
1049
1050 Ok(Some(hash))
1051 }
1052
1053 /// Returns the list of pointers to rows which hash to `row_hash`.
1054 ///

Callers 1

confirm_insertionMethod · 0.80

Calls 7

DuplicateErrorClass · 0.85
row_sizeMethod · 0.80
insertMethod · 0.65
OkFunction · 0.50
ErrFunction · 0.50
delete_rowMethod · 0.45
as_mutMethod · 0.45

Tested by

no test coverage detected