pub struct BranchGarbageCollector<I: IndexBackend> {
index: Arc<I>,
repo_path: Option<PathBuf>,
}Expand description
Garbage collector for branch-scoped memories.
Identifies memories associated with git branches that no longer exist and marks them as tombstoned. This helps keep the memory index clean by removing memories that are no longer relevant.
§Thread Safety
The garbage collector holds an Arc reference to the index backend,
making it safe to share across threads.
§Example
use subcog::gc::BranchGarbageCollector;
use subcog::storage::index::SqliteBackend;
use std::sync::Arc;
let backend = Arc::new(SqliteBackend::new("memories.db")?);
let gc = BranchGarbageCollector::new(backend);
// Check for stale branches without making changes
let result = gc.gc_stale_branches("github.com/org/repo", true)?;
if result.has_stale_branches() {
println!("Stale branches: {:?}", result.stale_branches);
}Fields§
§index: Arc<I>Reference to the index backend for querying and updating memories.
repo_path: Option<PathBuf>Optional path to the git repository. If None, uses the current working directory.
Implementations§
Source§impl<I: IndexBackend> BranchGarbageCollector<I>
impl<I: IndexBackend> BranchGarbageCollector<I>
Sourcepub fn new(index: Arc<I>) -> Self
pub fn new(index: Arc<I>) -> Self
Creates a new branch garbage collector.
§Arguments
index- Shared reference to the index backend.
§Examples
use subcog::gc::BranchGarbageCollector;
use subcog::storage::index::SqliteBackend;
use std::sync::Arc;
let backend = Arc::new(SqliteBackend::in_memory()?);
let gc = BranchGarbageCollector::new(backend);Sourcepub fn with_repo_path(index: Arc<I>, repo_path: &Path) -> Self
pub fn with_repo_path(index: Arc<I>, repo_path: &Path) -> Self
Creates a new branch garbage collector with a specific repository path.
§Arguments
index- Shared reference to the index backend.repo_path- Path to the git repository.
§Examples
use subcog::gc::BranchGarbageCollector;
use subcog::storage::index::SqliteBackend;
use std::sync::Arc;
use std::path::Path;
let backend = Arc::new(SqliteBackend::in_memory()?);
let gc = BranchGarbageCollector::with_repo_path(
backend,
Path::new("/path/to/repo"),
);Sourcepub fn gc_stale_branches(
&self,
project_id: &str,
dry_run: bool,
) -> Result<GcResult>
pub fn gc_stale_branches( &self, project_id: &str, dry_run: bool, ) -> Result<GcResult>
Performs garbage collection on stale branches.
This method:
- Discovers the git repository from the configured path or CWD
- Gets all current branches from the repository
- Queries the index for all distinct branches associated with the project
- Identifies branches in the index that no longer exist in the repo
- Tombstones memories associated with stale branches (unless
dry_run)
§Arguments
project_id- The project identifier (e.g., “github.com/org/repo”)dry_run- If true, only report what would be done without making changes
§Returns
A GcResult containing statistics about the operation.
§Errors
Returns an error if:
- The git repository cannot be discovered
- The index backend operations fail
§Examples
use subcog::gc::BranchGarbageCollector;
// Dry run first
let result = gc.gc_stale_branches("github.com/org/repo", true)?;
println!("{}", result.summary());
// Then actually perform cleanup
if result.has_stale_branches() {
let result = gc.gc_stale_branches("github.com/org/repo", false)?;
println!("Cleaned up: {}", result.summary());
}Sourcefn discover_repository(&self) -> Result<Repository>
fn discover_repository(&self) -> Result<Repository>
Discovers the git repository.
Sourcefn get_current_branches(repo: &Repository) -> Result<HashSet<String>>
fn get_current_branches(repo: &Repository) -> Result<HashSet<String>>
Gets all current branch names from the repository.
Sourcefn get_indexed_branches(&self, project_id: &str) -> Result<Vec<String>>
fn get_indexed_branches(&self, project_id: &str) -> Result<Vec<String>>
Gets all distinct branch names from the index for a project.
Uses batch query to avoid N+1 pattern (PERF-HIGH-003).
Sourcefn count_memories_for_branches(
&self,
project_id: &str,
branches: &[String],
) -> Result<usize>
fn count_memories_for_branches( &self, project_id: &str, branches: &[String], ) -> Result<usize>
Counts memories that would be tombstoned for the given branches.
Sourcefn tombstone_memories_for_branches(
&self,
project_id: &str,
branches: &[String],
) -> usize
fn tombstone_memories_for_branches( &self, project_id: &str, branches: &[String], ) -> usize
Tombstones memories associated with the given branches.
This is a placeholder that will be enhanced when Task 4.3 adds
update_status to the IndexBackend trait.
Auto Trait Implementations§
impl<I> Freeze for BranchGarbageCollector<I>
impl<I> RefUnwindSafe for BranchGarbageCollector<I>where
I: RefUnwindSafe,
impl<I> Send for BranchGarbageCollector<I>
impl<I> Sync for BranchGarbageCollector<I>
impl<I> Unpin for BranchGarbageCollector<I>
impl<I> UnwindSafe for BranchGarbageCollector<I>where
I: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].