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
//! Property update/delete paths and their MVCC undo operations.
use std::sync::atomic::Ordering;
use super::VertexTable;
use crate::vertex::{primary_key_mirror_value, Timestamp};
use linkrs_core::{StorageError, StorageResult, Value};
impl VertexTable {
/// Read-only validation and normalization for one property update:
/// primary-key mirror rejection, column existence and type checks.
/// Staging paths call this before buffering so an invalid column fails
/// with no global write.
pub fn prepare_update(&self, col_name: &str, value: &Value) -> StorageResult<Value> {
self.columns.check_column_available(col_name)?;
if self
.schema
.properties
.get(self.schema.primary_key_index)
.is_some_and(|pk| &*pk.name == col_name)
{
return Err(StorageError::invalid_operation(format!(
"Primary key column '{}' mirrors the vertex id and cannot be updated; delete and re-insert the vertex instead",
col_name
)));
}
// Use cached index lookup
let prop_idx = self
.property_index_cache
.get(col_name)
.ok_or_else(|| StorageError::column_not_found(col_name.to_string()))?;
let prop_def = &self.schema.properties[*prop_idx];
if value.data_type() != prop_def.data_type {
value.try_cast_to(&prop_def.data_type)
} else {
Ok(value.clone())
}
}
/// Validate and normalize one property update value, including the
/// primary-key mirror rule: restating the mirror is a no-op value,
/// while a divergent value is rejected. Shared by the apply path and
/// the commit pre-validation so both accept the same updates.
pub fn validate_update_value(
&self,
internal_id: u32,
col_name: &str,
value: &Value,
) -> StorageResult<Value> {
// The primary key column mirrors the row's own key: restating the
// mirror changes nothing and is a no-op (a delete-then-insert folded
// into a whole-row update always carries it), while a divergent
// value would fork the key from its mirror and is rejected.
if let Some(pk_def) = self.schema.properties.get(self.schema.primary_key_index) {
if &*pk_def.name == col_name {
let key = self
.id_indexer
.get_key(internal_id)
.ok_or_else(StorageError::vertex_not_found)?;
let mirror = primary_key_mirror_value(&pk_def.data_type, &key)?
.try_cast_to(&pk_def.data_type)?;
let provided = value.try_cast_to(&pk_def.data_type)?;
if provided != mirror {
return Err(StorageError::invalid_operation(format!(
"Primary key column '{}' must mirror the vertex id: got {:?}, expected {:?}",
col_name, provided, mirror
)));
}
return Ok(provided);
}
}
self.prepare_update(col_name, value)
}
pub fn update_property(
&self,
internal_id: u32,
col_name: &str,
value: &Value,
ts: Timestamp,
) -> StorageResult<()> {
if !self.is_open.load(Ordering::Acquire) {
return Err(StorageError::storage_not_open());
}
// Hold the identity read guard across the column write: the latch
// order allows identity before segments, and holding it blocks a
// concurrent delete (which needs the write latch) from interleaving
// between the liveness check and the versioned write. The probe below
// rechecks liveness inside the segment latch through the already-held
// map, so no new latch is acquired there.
let stamps = self.timestamps.read();
if !stamps.is_valid(internal_id, ts) {
return Err(StorageError::vertex_not_found());
}
let converted_value = self.validate_update_value(internal_id, col_name, value)?;
if self.is_pk_column(col_name) {
return Ok(());
}
self.columns.set_property_versioned_checked(
internal_id as usize,
col_name,
Some(&converted_value),
ts,
|| stamps.is_valid(internal_id, ts),
)?;
Ok(())
}
/// Delete one column of one row by writing a null tombstone version.
///
/// Primary-key mirror columns never participate in row replacement and
/// are rejected here as misuse.
pub fn delete_property(
&self,
internal_id: u32,
col_name: &str,
ts: Timestamp,
) -> StorageResult<()> {
if !self.is_open.load(Ordering::Acquire) {
return Err(StorageError::storage_not_open());
}
if self.is_pk_column(col_name) {
return Err(StorageError::invalid_operation(format!(
"Primary key column '{}' cannot be deleted by row replacement",
col_name
)));
}
let stamps = self.timestamps.read();
if !stamps.is_valid(internal_id, ts) {
return Err(StorageError::vertex_not_found());
}
self.columns.set_property_versioned_checked(
internal_id as usize,
col_name,
None,
ts,
|| stamps.is_valid(internal_id, ts),
)?;
Ok(())
}
/// Apply one delete by internal id: timestamp tombstone under the
/// identity latch, then the row dirty mark on the column segments.
pub fn apply_delete(&self, internal_id: u32, ts: Timestamp) -> StorageResult<()> {
if !self.is_open.load(Ordering::Acquire) {
return Err(StorageError::storage_not_open());
}
{
let mut stamps = self.timestamps.write();
if !stamps.is_valid(internal_id, ts) {
return Err(StorageError::vertex_not_found());
}
stamps.remove(internal_id, ts);
}
self.mark_row_dirty(internal_id as usize);
Ok(())
}
pub fn delete_by_internal_id(&self, internal_id: u32, ts: Timestamp) -> StorageResult<()> {
self.apply_delete(internal_id, ts)
}
pub fn revert_delete(&self, internal_id: u32) {
self.timestamps.write().revert_delete(internal_id);
self.mark_row_dirty(internal_id as usize);
}
/// Backdate one row's creation stamps for offline redistribution.
/// Row and column layers move together so snapshot reads below the
/// rebuild timestamp keep serving the migrated values.
pub fn backdate_row_for_reshard(&self, internal_id: u32, create_ts: Timestamp) {
self.timestamps.write().insert(internal_id, create_ts);
self.columns.backdate_row(internal_id as usize, create_ts);
}
pub fn undo_update(
&self,
internal_id: u32,
col_name: &str,
ts: Timestamp,
) -> StorageResult<()> {
self.columns
.undo_last_versioned_write(internal_id as usize, col_name, ts)
}
}