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
//! Edge partition scatter-gather access: parallel mutation, lazy creation
//! and per-partition lock helpers.
use std::sync::Arc;
use std::time::Instant;
use parking_lot::RwLock;
use crate::edge::EdgeStore;
use linkrs_core::types::LabelId;
use linkrs_core::{StorageError, StorageResult};
use super::GraphDataStore;
impl GraphDataStore {
pub(crate) fn edge_partition_keys(
&self,
edge_label: LabelId,
) -> StorageResult<Vec<super::EdgeTableKey>> {
self.read_edge_label_index()
.get(&edge_label)
.cloned()
.ok_or_else(|| StorageError::label_not_found(format!("edge label {}", edge_label)))
}
/// Iterate over all partitions of a given edge label, calling `operation` on each
/// with only the per-table write lock held (no catalog write lock).
///
/// Uses scatter-gather: all Arcs are collected under a brief catalog read lock,
/// then each table is locked individually so different edge labels' partitions
/// can be mutated concurrently. Partitions run in parallel (rayon); the closure
/// must be `Sync` and its result `Send`, and results preserve partition order.
pub(crate) fn for_each_edge_partition_mut<R: Send>(
&self,
edge_label: LabelId,
operation: impl Fn(super::EdgeTableKey, &mut EdgeStore) -> StorageResult<R> + Sync,
) -> StorageResult<Vec<R>> {
use rayon::prelude::*;
let keys = self.edge_partition_keys(edge_label)?;
let arcs: Vec<(super::EdgeTableKey, Arc<RwLock<EdgeStore>>)> = {
let guard = self.edge_tables.read();
keys.iter()
.filter_map(|key| guard.get(key).map(|arc| (*key, arc.clone())))
.collect()
};
arcs.par_iter()
.map(|(key, arc)| {
let started = Instant::now();
let mut table = arc.write();
self.table_lock_metrics.record(true, started);
operation(*key, &mut table)
})
.collect()
}
/// Iterate over all partitions of all edge labels, calling `operation` on each
/// with only the per-table write lock held (no catalog write lock).
///
/// Uses scatter-gather: all EdgeTableKeys are collected from the edge_label_index
/// under a brief catalog read lock, then each table is locked individually so
/// partitions from different edge labels can be mutated concurrently.
///
/// The operation runs in parallel over the partitions (rayon) because each
/// partition is an independent lock domain: different edge labels' tables
/// are never touched by the same call. The closure must be `Sync` and its
/// result `Send`; results preserve partition order.
pub(crate) fn for_all_edge_partitions_mut<R: Send>(
&self,
operation: impl Fn(super::EdgeTableKey, &mut EdgeStore) -> StorageResult<R> + Sync,
) -> StorageResult<Vec<R>> {
use rayon::prelude::*;
let keys: Vec<super::EdgeTableKey> = {
let index = self.read_edge_label_index();
index.values().flat_map(|v| v.iter()).copied().collect()
};
let arcs: Vec<(super::EdgeTableKey, Arc<RwLock<EdgeStore>>)> = keys
.into_iter()
.filter_map(|key| {
let arc = {
let guard = self.edge_tables.read();
guard.get(&key).cloned()
};
arc.map(|arc| (key, arc))
})
.collect();
arcs.par_iter()
.map(|(key, arc)| {
let started = Instant::now();
let mut table = arc.write();
self.table_lock_metrics.record(true, started);
operation(*key, &mut table)
})
.collect()
}
/// Collect the live partition handles for one edge label.
///
/// Scatter-gather entry for read fan-out: both the label index and the
/// table registry are held only for the handle collection, then released
/// before any table lock is taken. Callers pin the returned `Arc`s across
/// their table reads, so a concurrent DDL drop cannot free a table under
/// them; such a read observes the pre-drop snapshot. Unknown labels
/// return empty instead of an error, matching filter-style traversal.
pub(crate) fn matching_edge_partition_arcs(
&self,
edge_label: LabelId,
) -> Vec<Arc<RwLock<EdgeStore>>> {
let keys: Vec<super::EdgeTableKey> =
self.with_edge_label_index(|index| index.get(&edge_label).cloned().unwrap_or_default());
if keys.is_empty() {
return Vec::new();
}
let guard = self.edge_tables.read();
keys.iter()
.filter_map(|key| guard.get(key).cloned())
.collect()
}
/// Take one partition read lock with contention accounting.
///
/// Used by fan-out loops that already hold collected handles outside the
/// catalog lock, so each table acquisition is timed on its own.
pub(crate) fn read_edge_table<'a>(
&self,
arc: &'a Arc<RwLock<EdgeStore>>,
) -> parking_lot::RwLockReadGuard<'a, EdgeStore> {
let started = Instant::now();
let guard = arc.read();
self.table_lock_metrics.record(false, started);
guard
}
/// Take one partition write lock with contention accounting.
pub(crate) fn write_edge_table<'a>(
&self,
arc: &'a Arc<RwLock<EdgeStore>>,
) -> parking_lot::RwLockWriteGuard<'a, EdgeStore> {
let started = Instant::now();
let guard = arc.write();
self.table_lock_metrics.record(true, started);
guard
}
/// Read a single edge table by key, holding only the table-level lock (not the catalog lock)
/// during the operation. The catalog lock is released after the table lookup.
pub(crate) fn with_single_edge_table<R>(
&self,
key: &super::EdgeTableKey,
operation: impl FnOnce(&EdgeStore) -> StorageResult<R>,
) -> StorageResult<R> {
let arc = {
let guard = self.edge_tables.read();
guard
.get(key)
.ok_or_else(|| StorageError::label_not_found(format!("edge partition {:?}", key)))?
.clone()
};
let guard = self.read_edge_table(&arc);
operation(&guard)
}
/// Try to get a mutable reference to a single edge table by key.
/// Returns `None` if the key does not exist (no catalog lock held during operation).
pub(crate) fn try_get_edge_table_mut(
&self,
key: &super::EdgeTableKey,
) -> Option<std::sync::Arc<parking_lot::RwLock<EdgeStore>>> {
let guard = self.edge_tables.read();
guard.get(key).cloned()
}
/// Mutate a single edge table by key, holding only the table-level lock during the operation.
pub(crate) fn with_single_edge_table_mut<R>(
&self,
key: &super::EdgeTableKey,
operation: impl FnOnce(&mut EdgeStore) -> StorageResult<R>,
) -> StorageResult<R> {
let arc = {
let guard = self.edge_tables.read();
guard
.get(key)
.ok_or_else(|| StorageError::label_not_found(format!("edge partition {:?}", key)))?
.clone()
};
let mut guard = self.write_edge_table(&arc);
operation(&mut guard)
}
pub(crate) fn with_edge_partition_mut<R>(
&self,
key: super::EdgeTableKey,
template_key: super::EdgeTableKey,
create: impl FnOnce(&EdgeStore) -> StorageResult<EdgeStore>,
operation: impl FnOnce(&mut EdgeStore) -> StorageResult<R>,
) -> StorageResult<R> {
let table_arc = {
// Phase 1: read lock (fast path — partition already exists)
let guard = self.edge_tables.read();
if let Some(arc) = guard.get(&key) {
arc.clone()
} else {
drop(guard);
// Phase 2: write lock (slow path — create partition)
let mut tables = self.write_edge_tables();
let mut index = self.write_edge_label_index();
if !tables.contains_key(&key) {
let table = {
let template = tables.get(&template_key).ok_or_else(|| {
StorageError::label_not_found(format!("edge label {}", key.edge_label))
})?;
let guard = template.read();
create(&guard)?
};
tables.insert(key, Arc::new(RwLock::new(table)));
let indexed_keys = index.entry(key.edge_label).or_default();
if !indexed_keys.contains(&key) {
indexed_keys.push(key);
}
}
tables
.get(&key)
.ok_or_else(|| {
StorageError::label_not_found(format!("edge partition {:?}", key))
})?
.clone()
}
};
let mut guard = self.write_edge_table(&table_arc);
operation(&mut guard)
}
}