Use sled::Batch for index updates rather than transactions
Diff
src/database/indexer.rs | 39 ++++++++++++++++++++++-----------------
src/database/schema/commit.rs | 8 +++-----
2 files changed, 21 insertions(+), 26 deletions(-)
@@ -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::<Result<_, _>>().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);
}
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;
}
for to_remove in (i + 1)..(i + 100) {
update_batch.remove(&to_remove.to_be_bytes());
}
commit_tree.apply_batch(update_batch).unwrap();
}
}
}
@@ -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());
}
}