Skip to content

Commit

Permalink
feat!: optionally store objects new objects in memory only.
Browse files Browse the repository at this point in the history
The default object database changed to a version that allows to
keep objects in memory. This needs a mutable `Repository` instance
to setup.
  • Loading branch information
Byron committed Sep 5, 2024
1 parent ca5294a commit dfbc732
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
5 changes: 4 additions & 1 deletion gix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ pub mod path;
/// The standard type for a store to handle git references.
pub type RefStore = gix_ref::file::Store;
/// A handle for finding objects in an object database, abstracting away caches for thread-local use.
pub type OdbHandle = gix_odb::Handle;
pub type OdbHandle = gix_odb::memory::Proxy<gix_odb::Handle>;
/// A handle for finding objects in an object database, abstracting away caches for moving across threads.
pub type OdbHandleArc = gix_odb::memory::Proxy<gix_odb::HandleArc>;

/// A way to access git configuration
pub(crate) type Config = OwnShared<gix_config::File<'static>>;

Expand Down
11 changes: 11 additions & 0 deletions gix/src/repository/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ impl crate::Repository {
(ten_mb_for_every_10k_files as usize).max(4 * 1024)
}
}

/// Handling of InMemory object writing
impl crate::Repository {
/// When writing objects, keep them in memory instead of writing them to disk.
/// This makes any change to the object database non-persisting, while keeping the view
/// to the object database consistent for this instance.
pub fn with_object_memory(mut self) -> Self {
self.objects.enable_object_memory();
self
}
}
4 changes: 2 additions & 2 deletions gix/src/repository/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl From<&crate::ThreadSafeRepository> for crate::Repository {
fn from(repo: &crate::ThreadSafeRepository) -> Self {
crate::Repository::from_refs_and_objects(
repo.refs.clone(),
repo.objects.to_handle().into(),
gix_odb::memory::Proxy::from(gix_odb::Cache::from(repo.objects.to_handle())).with_write_passthrough(),
repo.work_tree.clone(),
repo.common_dir.clone(),
repo.config.clone(),
Expand All @@ -56,7 +56,7 @@ impl From<crate::ThreadSafeRepository> for crate::Repository {
fn from(repo: crate::ThreadSafeRepository) -> Self {
crate::Repository::from_refs_and_objects(
repo.refs,
repo.objects.to_handle().into(),
gix_odb::memory::Proxy::from(gix_odb::Cache::from(repo.objects.to_handle())).with_write_passthrough(),
repo.work_tree,
repo.common_dir,
repo.config,
Expand Down
2 changes: 1 addition & 1 deletion gix/src/repository/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl crate::Repository {
#[doc(alias = "exists", alias = "git2")]
pub fn has_object(&self, id: impl AsRef<gix_hash::oid>) -> bool {
let id = id.as_ref();
if id == ObjectId::empty_tree(self.object_hash()) {
if id.to_owned().is_empty_tree() {
true
} else {
self.objects.exists(id)
Expand Down
2 changes: 1 addition & 1 deletion gix/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub struct PathspecDetached {
/// The prepared search to use for checking matches.
pub search: gix_pathspec::Search,
/// A thread-safe version of an ODB.
pub odb: gix_odb::HandleArc,
pub odb: crate::OdbHandleArc,
}

/// A stand-in for the submodule of a particular name.
Expand Down
18 changes: 17 additions & 1 deletion gix/tests/repository/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,24 @@ mod write_blob {
#[test]
fn from_slice() -> crate::Result {
let (_tmp, repo) = empty_bare_repo()?;
let expected = hex_to_id("95d09f2b10159347eece71399a7e2e907ea3df4f");
assert!(!repo.has_object(expected));

let oid = repo.write_blob(b"hello world")?;
assert_eq!(oid, hex_to_id("95d09f2b10159347eece71399a7e2e907ea3df4f"));
assert_eq!(oid, expected);

let mut other_repo = gix::open_opts(repo.path(), gix::open::Options::isolated())?;
other_repo.objects.enable_object_memory();
assert!(
other_repo.has_object(oid),
"we definitely don't accidentally write to memory only"
);
let in_memory_id = other_repo.write_blob("hello world - to memory")?;
assert!(!repo.has_object(in_memory_id), "the object was never written to disk…");
assert!(
other_repo.has_object(in_memory_id),
"…and exists only in the instance that wrote it"
);
Ok(())
}

Expand Down

0 comments on commit dfbc732

Please sign in to comment.