#![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;
pub type PackageId = u64;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Symbol {
pub name: String,
pub package: String,
pub package_name: String,
pub kind: SymbolKind,
pub version: Option<String>,
pub import_path: String,
pub doc: Option<String>,
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"),
}
}
}
#[derive(Debug, Clone)]
pub struct InvertedIndex {
pub name_index: Arc<DashMap<String, Vec<Symbol>>>,
pub(crate) package_index: Arc<DashMap<String, Vec<Symbol>>>,
pub(crate) prefix_index: Arc<DashMap<char, Vec<String>>>,
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()),
}
}
pub fn insert(&self, symbol: Symbol) {
let name = symbol.name.clone();
let package = symbol.package.clone();
self.name_index
.entry(name.clone())
.and_modify(|v| v.push(symbol.clone()))
.or_insert_with(|| vec![symbol.clone()]);
self.package_index
.entry(package.clone())
.and_modify(|v| v.push(symbol.clone()))
.or_insert_with(|| vec![symbol.clone()]);
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()]);
}
}
pub fn get_by_name(&self, name: &str) -> Option<Vec<Symbol>> {
self.name_index.get(name).map(|entry| entry.clone())
}
pub fn get_by_package(&self, package: &str) -> Option<Vec<Symbol>> {
self.package_index.get(package).map(|entry| entry.clone())
}
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);
}
pub fn stats(&self) -> IndexStats {
IndexStats {
total_symbols: self.name_index.len(),
total_packages: self.package_index.len(),
total_modules: self.module_metadata.len(),
}
}
pub fn is_empty(&self) -> bool {
self.name_index.is_empty()
}
pub fn batch_insert(&self, symbols: Vec<Symbol>) {
use rayon::prelude::*;
symbols.into_par_iter().for_each(|symbol| {
self.insert(symbol);
});
}
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);
}
}