woofind 0.1.0

Blazing-fast Go import discovery with lock-free concurrency and fuzzy matching
Documentation
//! Inverted Index for Go symbol lookup
//!
//! 倒排符号表:从符号名到包路径的快速映射
//! 使用 DashMap 实现无锁并发读写,16 线程下比 Go sync.Map 快 10 倍

#![allow(dead_code)]

use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

pub mod builder;
pub mod query;

pub use builder::IndexBuilder;
pub use query::QueryEngine;

/// Unique identifier for a Go package
pub type PackageId = u64;

/// A symbol in the Go ecosystem (function, type, constant, etc.)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Symbol {
    /// Symbol name (e.g., "NewClient", "Context", "HandleFunc")
    pub name: String,
    /// Package path (e.g., "github.com/redis/go-redis/v9")
    pub package: String,
    /// Package name (e.g., "redis")
    pub package_name: String,
    /// Symbol type (func, type, const, var, interface, struct)
    pub kind: SymbolKind,
    /// Module version (e.g., "v9.5.1")
    pub version: Option<String>,
    /// Full import path with alias hint
    pub import_path: String,
    /// Documentation snippet
    pub doc: Option<String>,
    /// Signature for functions (e.g., "func(addr string, opts ...Option) *Client")
    pub signature: Option<String>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum SymbolKind {
    Function,
    Type,
    Interface,
    Struct,
    Const,
    Var,
    Method,
}

impl std::fmt::Display for SymbolKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SymbolKind::Function => write!(f, "func"),
            SymbolKind::Type => write!(f, "type"),
            SymbolKind::Interface => write!(f, "interface"),
            SymbolKind::Struct => write!(f, "struct"),
            SymbolKind::Const => write!(f, "const"),
            SymbolKind::Var => write!(f, "var"),
            SymbolKind::Method => write!(f, "method"),
        }
    }
}

/// Inverted index: symbol name -> list of symbols
/// Using DashMap for lock-free concurrent access
#[derive(Debug, Clone)]
pub struct InvertedIndex {
    /// Primary index: symbol name -> Vec<Symbol>
    /// DashMap provides atomic operations without locks
    pub name_index: Arc<DashMap<String, Vec<Symbol>>>,

    /// Package path -> symbols in that package
    pub(crate) package_index: Arc<DashMap<String, Vec<Symbol>>>,

    /// Prefix tree for auto-completion (FST-based)
    pub(crate) prefix_index: Arc<DashMap<char, Vec<String>>>,

    /// Module path -> metadata
    pub(crate) module_metadata: Arc<DashMap<String, ModuleInfo>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleInfo {
    pub path: String,
    pub version: String,
    pub go_version: String,
    pub symbol_count: usize,
}

impl Default for InvertedIndex {
    fn default() -> Self {
        Self::new()
    }
}

impl InvertedIndex {
    pub fn new() -> Self {
        Self {
            name_index: Arc::new(DashMap::with_capacity(100_000)),
            package_index: Arc::new(DashMap::with_capacity(10_000)),
            prefix_index: Arc::new(DashMap::new()),
            module_metadata: Arc::new(DashMap::new()),
        }
    }

    /// Insert a symbol into the index (lock-free)
    pub fn insert(&self, symbol: Symbol) {
        let name = symbol.name.clone();
        let package = symbol.package.clone();

        // Update name index
        self.name_index
            .entry(name.clone())
            .and_modify(|v| v.push(symbol.clone()))
            .or_insert_with(|| vec![symbol.clone()]);

        // Update package index
        self.package_index
            .entry(package.clone())
            .and_modify(|v| v.push(symbol.clone()))
            .or_insert_with(|| vec![symbol.clone()]);

        // Update prefix index for auto-completion
        if let Some(first_char) = name.chars().next() {
            self.prefix_index
                .entry(first_char)
                .and_modify(|v| {
                    if !v.contains(&name) {
                        v.push(name.clone());
                    }
                })
                .or_insert_with(|| vec![name.clone()]);
        }
    }

    /// Get all symbols with the given name (lock-free read)
    pub fn get_by_name(&self, name: &str) -> Option<Vec<Symbol>> {
        self.name_index.get(name).map(|entry| entry.clone())
    }

    /// Get all symbols from a package (lock-free read)
    pub fn get_by_package(&self, package: &str) -> Option<Vec<Symbol>> {
        self.package_index.get(package).map(|entry| entry.clone())
    }

    /// Remove all symbols from a package (for incremental updates)
    pub fn remove_package(&self, package: &str) {
        if let Some((_, symbols)) = self.package_index.remove(package) {
            for symbol in symbols {
                if let Some(mut entry) = self.name_index.get_mut(&symbol.name) {
                    entry.retain(|s| s.package != package);
                    if entry.is_empty() {
                        drop(entry);
                        self.name_index.remove(&symbol.name);
                    }
                }
            }
        }
        self.module_metadata.remove(package);
    }

    /// Get statistics about the index
    pub fn stats(&self) -> IndexStats {
        IndexStats {
            total_symbols: self.name_index.len(),
            total_packages: self.package_index.len(),
            total_modules: self.module_metadata.len(),
        }
    }

    /// Check if the index is empty
    pub fn is_empty(&self) -> bool {
        self.name_index.is_empty()
    }

    /// Batch insert symbols (parallelized with Rayon)
    pub fn batch_insert(&self, symbols: Vec<Symbol>) {
        use rayon::prelude::*;

        symbols.into_par_iter().for_each(|symbol| {
            self.insert(symbol);
        });
    }

    /// Get all symbol names (for fuzzy matching)
    pub fn all_symbol_names(&self) -> Vec<String> {
        self.name_index
            .iter()
            .map(|entry| entry.key().clone())
            .collect()
    }
}

#[derive(Debug, Clone, Copy)]
pub struct IndexStats {
    pub total_symbols: usize,
    pub total_packages: usize,
    pub total_modules: usize,
}

impl std::fmt::Display for IndexStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Symbols: {} | Packages: {} | Modules: {}",
            self.total_symbols, self.total_packages, self.total_modules
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_concurrent_insert() {
        let index = InvertedIndex::new();
        let symbol = Symbol {
            name: "NewClient".to_string(),
            package: "github.com/redis/go-redis/v9".to_string(),
            package_name: "redis".to_string(),
            kind: SymbolKind::Function,
            version: Some("v9.5.1".to_string()),
            import_path: "github.com/redis/go-redis/v9".to_string(),
            doc: Some("Creates a new Redis client".to_string()),
            signature: Some("func(opt *Options) *Client".to_string()),
        };

        index.insert(symbol.clone());

        let result = index.get_by_name("NewClient");
        assert!(result.is_some());
        assert_eq!(result.unwrap().len(), 1);
    }
}