MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / create_view

Method create_view

atomic-core/src/pristine/txn/write/mod.rs:1277–1343  ·  view source on GitHub ↗
(
        &mut self,
        name: &str,
        kind: ViewScope,
        parent: Option<u64>,
    )

Source from the content-addressed store, hash-verified

1275 }
1276
1277 fn create_view(
1278 &mut self,
1279 name: &str,
1280 kind: ViewScope,
1281 parent: Option<u64>,
1282 ) -> PristineResult<ViewState> {
1283 // Check if view already exists
1284 {
1285 let table = self.txn.open_table(VIEWS)?;
1286 if table.get(name)?.is_some() {
1287 return Err(PristineError::ViewAlreadyExists {
1288 name: name.to_string(),
1289 });
1290 }
1291 }
1292
1293 // Validate parent exists if specified, and detect cycles
1294 if let Some(parent_id) = parent {
1295 let parent_view = ViewTxnT::get_view_by_id(self, parent_id)?.ok_or_else(|| {
1296 PristineError::ViewNotFound {
1297 name: format!("parent view id={}", parent_id),
1298 }
1299 })?;
1300
1301 // Cycle detection: walk the parent chain from the proposed parent
1302 // upward. If we ever encounter our own (not-yet-allocated) name,
1303 // there's a cycle. Since the view doesn't exist yet, we only need
1304 // to check that the parent chain terminates without revisiting
1305 // `parent_id` — which is guaranteed as long as the existing graph
1306 // is acyclic and we're adding a leaf.
1307 //
1308 // However, we also guard against the degenerate case where someone
1309 // passes parent == self (once IDs are known). Since we haven't
1310 // allocated an ID yet, the only risk is the parent chain itself
1311 // being cyclic (which would be a pre-existing bug). We do a bounded
1312 // walk as a safety check.
1313 let mut visited = std::collections::HashSet::new();
1314 visited.insert(parent_id);
1315 let mut cursor = parent_view.parent;
1316 while let Some(ancestor_id) = cursor {
1317 if !visited.insert(ancestor_id) {
1318 // We've seen this ID before — cycle detected in existing chain
1319 return Err(PristineError::ViewCycleDetected {
1320 name: name.to_string(),
1321 parent_name: parent_view.name.clone(),
1322 });
1323 }
1324 match ViewTxnT::get_view_by_id(self, ancestor_id)? {
1325 Some(ancestor) => cursor = ancestor.parent,
1326 None => break, // Broken chain — parent doesn't exist (shouldn't happen)
1327 }
1328 }
1329 }
1330
1331 // Allocate ID and create state
1332 let id = self.allocate_view_id()?;
1333 let state = ViewState::with_scope(id, name.to_string(), kind, parent);
1334

Calls 6

serialize_view_stateFunction · 0.85
allocate_view_idMethod · 0.80
getMethod · 0.65
insertMethod · 0.45
cloneMethod · 0.45
as_sliceMethod · 0.45