Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/fff-mcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ tokio = { version = "1", features = ["full"] }
tracing = { workspace = true }
git2 = { workspace = true }
clap = { version = "4", features = ["derive", "env"] }

[dev-dependencies]
tempfile = "3.8"
92 changes: 87 additions & 5 deletions crates/fff-mcp/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,79 @@
//! Maintains an in-memory map of opaque cursor IDs to file offsets.
//! Cursors are evicted LRU-style when the store exceeds capacity.

use std::collections::{HashMap, VecDeque};
use fff::grep::GrepMatch;
use std::collections::{HashMap, HashSet, VecDeque};

const MAX_CURSORS: usize = 20;

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub(crate) struct MatchKey {
pub(crate) file_path: String,
pub(crate) line_number: u64,
pub(crate) byte_offset: u64,
}

impl MatchKey {
pub(crate) fn new(file_path: String, line_number: u64, byte_offset: u64) -> Self {
Self {
file_path,
line_number,
byte_offset,
}
}
}

#[derive(Clone, Debug)]
pub(crate) struct PendingMatch {
pub(crate) file_path: String,
pub(crate) match_data: GrepMatch,
}

#[derive(Clone, Debug)]
pub(crate) struct MultiGrepCursor {
pub(crate) patterns: Vec<String>,
pub(crate) constraints: String,
pub(crate) next_offsets: Vec<Option<usize>>,
pub(crate) pending: Vec<PendingMatch>,
pub(crate) seen: HashSet<MatchKey>,
pub(crate) seen_files: HashSet<String>,
}

impl MultiGrepCursor {
pub(crate) fn new(patterns: Vec<String>, constraints: String) -> Self {
Self {
next_offsets: vec![Some(0); patterns.len()],
patterns,
constraints,
pending: Vec::new(),
seen: HashSet::new(),
seen_files: HashSet::new(),
}
}

pub(crate) fn has_more(&self) -> bool {
!self.pending.is_empty() || self.next_offsets.iter().any(Option::is_some)
}

pub(crate) fn remember_match(&mut self, key: MatchKey) -> bool {
self.seen.insert(key)
}

pub(crate) fn remember_file(&mut self, path: String) -> bool {
self.seen_files.insert(path)
}
}

enum CursorState {
FileOffset(usize),
MultiGrep(MultiGrepCursor),
}

/// Stores cursor state for paginated grep results.
pub struct CursorStore {
counter: u64,
/// Map from cursor ID string → file offset for next page.
cursors: HashMap<String, usize>,
/// Map from cursor ID string to the state required for the next page.
cursors: HashMap<String, CursorState>,
/// Insertion order for LRU eviction.
insertion_order: VecDeque<String>,
}
Expand All @@ -27,10 +91,25 @@ impl CursorStore {

/// Store a cursor and return its opaque ID string.
pub fn store(&mut self, file_offset: usize) -> String {
self.store_state(CursorState::FileOffset(file_offset))
}

pub(crate) fn store_multi_grep(&mut self, cursor: MultiGrepCursor) -> String {
self.store_state(CursorState::MultiGrep(cursor))
}

pub(crate) fn get_multi_grep(&self, id: &str) -> Option<MultiGrepCursor> {
match self.cursors.get(id) {
Some(CursorState::MultiGrep(cursor)) => Some(cursor.clone()),
Some(CursorState::FileOffset(_)) | None => None,
}
}

fn store_state(&mut self, state: CursorState) -> String {
self.counter = self.counter.wrapping_add(1);
let id = self.counter.to_string();

self.cursors.insert(id.clone(), file_offset);
self.cursors.insert(id.clone(), state);
self.insertion_order.push_back(id.clone());

// Evict oldest cursors
Expand All @@ -47,6 +126,9 @@ impl CursorStore {

/// Retrieve the file offset for a cursor ID.
pub fn get(&self, id: &str) -> Option<usize> {
self.cursors.get(id).copied()
match self.cursors.get(id) {
Some(CursorState::FileOffset(offset)) => Some(*offset),
Some(CursorState::MultiGrep(_)) | None => None,
}
}
}
Loading