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

Method add_batch

atomic-repository/src/repository/tracking.rs:268–325  ·  view source on GitHub ↗

Add multiple files to tracking in a single write transaction. This is much faster than calling `add()` in a loop because it avoids opening a separate write transaction (and fsync) for each file. For git import of a commit adding 20 files, this reduces from 20 fsyncs to 1. # Arguments `working_copy` - Identity of the physical working copy being updated `paths` - Paths to add (relative to reposit

(
        &self,
        working_copy: WorkingCopyId,
        paths: &[&str],
    )

Source from the content-addressed store, hash-verified

266 ///
267 /// Number of files actually added (skips already-tracked files).
268 pub fn add_batch(
269 &self,
270 working_copy: WorkingCopyId,
271 paths: &[&str],
272 ) -> Result<usize, RepositoryError> {
273 use crate::tracking::{TreeProjectionKind, TreeProjectionOperation, TreeProjectionPlan};
274 self.validate_working_copy(working_copy)?;
275 if paths.is_empty() {
276 return Ok(0);
277 }
278
279 let mut txn = self
280 .pristine
281 .write_txn()
282 .map_err(|e| RepositoryError::Database(e.to_string()))?;
283
284 // Performance (RFC §21 measured budgets, CB-13C AC-3): plan the
285 // whole batch as ONE tree projection. `TreeProjectionPlan::plan`
286 // derives directory occupancy from a FULL `iter_tree` scan, so
287 // planning per path rescans the entire TREE table once per file —
288 // O(n²) across a batch (measured: a 10k-file import spent minutes
289 // here). Planning once keeps the scan linear in the final tree.
290 let mut operations: Vec<TreeProjectionOperation> = Vec::with_capacity(paths.len());
291 let mut planned: std::collections::HashSet<String> = std::collections::HashSet::new();
292 let mut count = 0usize;
293 for path in paths {
294 let normalized = normalize_path(Path::new(path));
295 if planned.contains(&normalized) {
296 continue;
297 }
298 if is_tracked(&txn, &normalized)
299 .map_err(|e| RepositoryError::Database(e.to_string()))?
300 {
301 continue;
302 }
303 let inode = txn
304 .alloc_inode()
305 .map_err(|e| RepositoryError::Database(e.to_string()))?;
306 operations.push(TreeProjectionOperation::Add {
307 inode,
308 path: Some(normalized.clone()),
309 position: None,
310 kind: TreeProjectionKind::File,
311 });
312 planned.insert(normalized);
313 count += 1;
314 }
315
316 TreeProjectionPlan::plan(&txn, operations)
317 .map_err(|e| RepositoryError::Database(e.to_string()))?
318 .apply(&mut txn)
319 .map_err(|e| RepositoryError::Database(e.to_string()))?;
320
321 txn.commit()
322 .map_err(|e| RepositoryError::Database(e.to_string()))?;
323
324 Ok(count)
325 }

Calls 14

is_trackedFunction · 0.85
planFunction · 0.85
validate_working_copyMethod · 0.80
write_txnMethod · 0.80
normalize_pathFunction · 0.50
is_emptyMethod · 0.45
lenMethod · 0.45
containsMethod · 0.45
alloc_inodeMethod · 0.45
pushMethod · 0.45
cloneMethod · 0.45
insertMethod · 0.45