Skip to content

Commit

Permalink
Add 'contains()' method to Find (#266)
Browse files Browse the repository at this point in the history
The plan is to separate pack location entirely from Object and put
the location specific functions into a separate trait.
  • Loading branch information
Byron committed Dec 1, 2021
1 parent 58294dd commit 532e9e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 8 additions & 6 deletions git-odb/src/store/linked/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use crate::{
store::{compound, linked},
};

impl linked::Store {
impl crate::Find for linked::Store {
type Error = compound::find::Error;

/// Return true if the given object `id` is contained in the store.
pub fn contains(&self, id: impl AsRef<oid>) -> bool {
fn contains(&self, id: impl AsRef<oid>) -> bool {
let id = id.as_ref();
for db in self.dbs.iter() {
if db.internal_find_packed(id).is_some() || db.loose.contains(id) {
Expand All @@ -20,10 +22,6 @@ impl linked::Store {
}
false
}
}

impl crate::Find for linked::Store {
type Error = compound::find::Error;

fn try_find<'a>(
&self,
Expand Down Expand Up @@ -107,6 +105,10 @@ impl crate::Find for linked::Store {
impl crate::Find for &linked::Store {
type Error = compound::find::Error;

fn contains(&self, id: impl AsRef<oid>) -> bool {
(*self).contains(id)
}

fn try_find<'a>(
&self,
id: impl AsRef<oid>,
Expand Down
13 changes: 12 additions & 1 deletion git-pack/src/find_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub trait Find {
/// The error returned by [`try_find()`][Find::try_find()]
type Error: std::error::Error + 'static;

/// Returns true if the object exists in the database.
fn contains(&self, id: impl AsRef<git_hash::oid>) -> bool;

/// Find an object matching `id` in the database while placing its raw, undecoded data into `buffer`.
/// A `pack_cache` can be used to speed up subsequent lookups, set it to [`crate::cache::Never`] if the
/// workload isn't suitable for caching.
Expand All @@ -28,7 +31,7 @@ pub trait Find {
/// Find the packs location where an object with `id` can be found in the database, or `None` if there is no pack
/// holding the object.
///
/// _Note_ that the object database may have no notion of packs and thus always returns `None`.
/// _Note_ that this is always None if the object isn't packed.
fn location_by_oid(&self, id: impl AsRef<git_hash::oid>, buf: &mut Vec<u8>) -> Option<crate::bundle::Location>;

/// Find the bundle matching `pack_id`, or `None` if there is no such pack.
Expand Down Expand Up @@ -149,6 +152,10 @@ mod find_impls {
{
type Error = T::Error;

fn contains(&self, id: impl AsRef<oid>) -> bool {
self.deref().contains(id)
}

fn try_find<'a>(
&self,
id: impl AsRef<oid>,
Expand Down Expand Up @@ -177,6 +184,10 @@ mod find_impls {
{
type Error = T::Error;

fn contains(&self, id: impl AsRef<oid>) -> bool {
self.deref().contains(id)
}

fn try_find<'a>(
&self,
id: impl AsRef<oid>,
Expand Down

0 comments on commit 532e9e0

Please sign in to comment.