index : sparkle-git.git

ascending towards madness

author Jordan Doyle <jordan@doyle.la> 2022-07-17 20:34:14.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-17 20:34:14.0 +00:00:00
commit
a568bf659d337cde7efdc6e64e0c66f5ba673003 [patch]
tree
e956faa71e1b4bb1f346025cec9bf3d26a7f57a2
parent
eb04ecb9cdc0f3c7b91a2979b252659543ac65df
download
a568bf659d337cde7efdc6e64e0c66f5ba673003.tar.gz

Use sled::Batch for index updates rather than transactions



Diff

 src/database/indexer.rs       | 31 ++++++++++++++-----------------
 src/database/schema/commit.rs |  8 +++-----
 2 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/src/database/indexer.rs b/src/database/indexer.rs
index 4c76ed9..275f33e 100644
--- a/src/database/indexer.rs
+++ b/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 @@ fn update_repository_reflog(scan_path: &Path, db: &sled::Db) {
            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;
            let mut update_batch = Batch::default();

            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);
                    }
            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 (revs.len() + 1)..(revs.len() + 100) {
                        tx.remove(&to_remove.to_be_bytes())?;
                    }
            // 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());
            }

                    Ok(())
                })
                .unwrap();
            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
+++ b/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 From<git2::Commit<'_>> for Commit {
}

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());
    }
}