Skip to main content

BranchGarbageCollector

Struct BranchGarbageCollector 

Source
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>

Source

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);
Source

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"),
);
Source

pub fn gc_stale_branches( &self, project_id: &str, dry_run: bool, ) -> Result<GcResult>

Performs garbage collection on stale branches.

This method:

  1. Discovers the git repository from the configured path or CWD
  2. Gets all current branches from the repository
  3. Queries the index for all distinct branches associated with the project
  4. Identifies branches in the index that no longer exist in the repo
  5. 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());
}
Source

fn discover_repository(&self) -> Result<Repository>

Discovers the git repository.

Source

fn get_current_branches(repo: &Repository) -> Result<HashSet<String>>

Gets all current branch names from the repository.

Source

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).

Source

fn count_memories_for_branches( &self, project_id: &str, branches: &[String], ) -> Result<usize>

Counts memories that would be tombstoned for the given branches.

Source

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.

Source

fn tombstone_branch_memories( &self, project_id: &str, branch: &str, now: u64, ) -> usize

Tombstones all memories for a single branch.

Uses batch query to avoid N+1 pattern (PERF-HIGH-003).

Source

fn try_tombstone_memory(&self, id: &MemoryId, memory: Memory, now: u64) -> bool

Attempts to tombstone a single memory, returning true on success.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,