Run indexer every 5 minutes
Diff
src/main.rs | 9 +++++++--
src/database/indexer.rs | 88 ++++++++++++++++++++++++++++++++++++++++----------------------------------------
src/methods/repo.rs | 3 +--
3 files changed, 46 insertions(+), 54 deletions(-)
@@ -9,8 +9,10 @@
};
use bat::assets::HighlightingAssets;
use std::sync::Arc;
use std::time::Duration;
use syntect::html::ClassStyle;
use tower_layer::layer_fn;
use tracing::info;
use crate::{git::Git, layers::logger::LoggingMiddleware};
@@ -33,9 +35,12 @@
std::thread::spawn({
let db = db.clone();
move || {
move || loop {
info!("Running periodic index");
crate::database::indexer::run_indexer(&db);
eprintln!("finished indexer");
info!("Finished periodic index");
std::thread::sleep(Duration::from_secs(300));
}
});
@@ -8,7 +8,40 @@
pub fn run_indexer(db: &sled::Db) {
let scan_path = Path::new("/Users/jordan/Code/test-git");
update_repository_metadata(scan_path, &db);
update_repository_reflog(scan_path, &db);
}
fn update_repository_metadata(scan_path: &Path, db: &sled::Db) {
let mut discovered = Vec::new();
discover_repositories(scan_path, &mut discovered);
for repository in discovered {
let relative = get_relative_path(scan_path, &repository);
let id = Repository::open(db, relative)
.map(|v| v.id)
.unwrap_or_else(|| RepositoryId::new(db));
let name = relative.file_name().unwrap().to_string_lossy().to_string();
let description = Some(
String::from_utf8_lossy(
&std::fs::read(repository.join("description")).unwrap_or_default(),
)
.to_string(),
)
.filter(|v| !v.is_empty());
Repository {
id,
name,
description,
owner: None,
last_modified: OffsetDateTime::now_utc(),
}
.insert(db, relative);
}
}
fn update_repository_reflog(scan_path: &Path, db: &sled::Db) {
for (relative_path, db_repository) in Repository::fetch_all(&db) {
let git_repository = git2::Repository::open(scan_path.join(relative_path)).unwrap();
@@ -31,7 +64,11 @@
for rev in revwalk {
let rev = rev.unwrap();
let commit = git_repository.find_commit(rev).unwrap();
let commit = if let Ok(commit) = git_repository.find_commit(rev) {
commit
} else {
continue;
};
let author = commit.author();
let committer = commit.committer();
@@ -71,42 +108,10 @@
db_commit.insert(&commit_tree, i);
}
}
}
}
fn update_repository_metadata(scan_path: &Path, db: &sled::Db) {
let mut discovered = Vec::new();
discover_repositories(scan_path, &mut discovered);
for repository in discovered {
let relative = get_relative_path(scan_path, &repository);
let id = Repository::open(db, relative)
.map(|v| v.id)
.unwrap_or_else(|| RepositoryId::new(db));
let name = relative.file_name().unwrap().to_string_lossy().to_string();
let description = Some(
String::from_utf8_lossy(
&std::fs::read(repository.join("description")).unwrap_or_default(),
)
.to_string(),
)
.filter(|v| !v.is_empty());
Repository {
id,
name,
description,
owner: None,
last_modified: OffsetDateTime::now_utc(),
}
.insert(db, relative);
}
}
fn get_relative_path<'a>(relative_to: &Path, full_path: &'a Path) -> &'a Path {
full_path.strip_prefix(relative_to).unwrap()
}
@@ -125,22 +130,5 @@
discover_repositories(&dir, discovered_repos);
}
}
}
#[cfg(test)]
mod test {
use crate::database::schema::repository::Repository;
use time::Instant;
#[test]
fn test_discovery() {
let db = sled::open(std::env::temp_dir().join("sled-test.db")).unwrap();
let start = Instant::now();
super::run_indexer(&db);
let repo = Repository::open(&db, "1p.git");
panic!("{} - {:#?}", start.elapsed(), repo);
}
}
@@ -171,8 +171,7 @@
let offset = query.offset.unwrap_or(0);
let reference = format!("refs/heads/{}", query.branch.as_deref().unwrap_or("master"));
let repository =
crate::database::schema::repository::Repository::open(&db, &*repo).unwrap();
let repository = crate::database::schema::repository::Repository::open(&db, &*repo).unwrap();
let commit_tree = repository.commit_tree(&db, &reference);
let mut commits = commit_tree.fetch_latest(101, offset);