From a568bf659d337cde7efdc6e64e0c66f5ba673003 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sun, 17 Jul 2022 21:34:14 +0100 Subject: [PATCH] Use sled::Batch for index updates rather than transactions --- src/database/indexer.rs | 39 ++++++++++++++++++++++----------------- src/database/schema/commit.rs | 8 +++----- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/database/indexer.rs b/src/database/indexer.rs index 4c76ed9..275f33e 100644 --- a/src/database/indexer.rs +++ a/src/database/indexer.rs @@ -1,4 +1,5 @@ use git2::Sort; +use sled::Batch; use std::path::{Path, PathBuf}; use time::OffsetDateTime; use tracing::info; @@ -68,27 +69,23 @@ let mut revwalk = git_repository.revwalk().unwrap(); revwalk.set_sorting(Sort::REVERSE).unwrap(); revwalk.push_ref(reference).unwrap(); - let revs: Vec<_> = revwalk.collect::>().unwrap(); - - let git_repository = &git_repository; - - commit_tree - .transaction::<_, _, std::io::Error>(move |tx| { - for (i, rev) in revs.iter().enumerate() { - let commit = git_repository.find_commit(*rev).unwrap(); - - Commit::from(commit).insert(tx, i); - } - - // a complete and utter hack to remove potentially dropped commits from our tree, - // we'll need to add `clear()` to sled's tx api to remove this - for to_remove in (revs.len() + 1)..(revs.len() + 100) { - tx.remove(&to_remove.to_be_bytes())?; - } - - Ok(()) - }) - .unwrap(); + + let mut update_batch = Batch::default(); + + let mut i = 0; + for rev in revwalk { + let commit = git_repository.find_commit(rev.unwrap()).unwrap(); + Commit::from(commit).insert(&mut update_batch, i); + i += 1; + } + + // a complete and utter hack to remove potentially dropped commits from our tree, + // we'll need to add `clear()` to sled's tx api to remove this + for to_remove in (i + 1)..(i + 100) { + update_batch.remove(&to_remove.to_be_bytes()); + } + + commit_tree.apply_batch(update_batch).unwrap(); } } } diff --git a/src/database/schema/commit.rs b/src/database/schema/commit.rs index 280fd0f..23279db 100644 --- a/src/database/schema/commit.rs +++ a/src/database/schema/commit.rs @@ -1,6 +1,6 @@ use git2::Signature; use serde::{Deserialize, Serialize}; -use sled::transaction::TransactionalTree; +use sled::Batch; use std::ops::Deref; use time::OffsetDateTime; @@ -29,10 +29,8 @@ } impl Commit { - pub fn insert(&self, database: &TransactionalTree, id: usize) { - database - .insert(&id.to_be_bytes(), bincode::serialize(self).unwrap()) - .unwrap(); + pub fn insert(&self, batch: &mut Batch, id: usize) { + batch.insert(&id.to_be_bytes(), bincode::serialize(self).unwrap()); } } -- rgit 0.1.3