Skip to content

Commit

Permalink
feat(scheduler, common): Add repository cache access to RepositoryCache
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 5, 2024
1 parent b46c50b commit fa077e9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
4 changes: 4 additions & 0 deletions crates/tabby-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use crate::{
path::repositories_dir,
terminal::{HeaderFormat, InfoMessage},
SourceFile,
};

#[derive(Serialize, Deserialize, Default)]
Expand Down Expand Up @@ -149,6 +150,9 @@ impl Default for ServerConfig {
#[async_trait]
pub trait RepositoryAccess: Send + Sync {
async fn list_repositories(&self) -> Result<Vec<RepositoryConfig>>;
fn start_snapshot(&self, _version: u64) {}
fn process_file(&self, _version: u64, _file: SourceFile) {}
fn finish_snapshot(&self, _version: u64) {}

Check warning on line 155 in crates/tabby-common/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-common/src/config.rs#L153-L155

Added lines #L153 - L155 were not covered by tests
}

pub struct ConfigRepositoryAccess;
Expand Down
2 changes: 1 addition & 1 deletion crates/tabby-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use path::dataset_dir;
use serde::{Deserialize, Serialize};
use serde_jsonlines::JsonLinesReader;

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct SourceFile {
pub git_url: String,
pub filepath: String,
Expand Down
30 changes: 24 additions & 6 deletions crates/tabby-scheduler/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use kdam::BarExt;
use lazy_static::lazy_static;
use serde_jsonlines::WriteExt;
use tabby_common::{
config::RepositoryConfig,
config::{RepositoryAccess, RepositoryConfig},
path::{dataset_dir, dependency_file},
DependencyFile, SourceFile,
};
Expand All @@ -25,11 +25,21 @@ use tree_sitter_tags::TagsContext;
use crate::utils::tqdm;

trait RepositoryExt {
fn create_dataset(&self, writer: &mut impl Write) -> Result<()>;
fn create_dataset(
&self,
writer: &mut impl Write,
access: &impl RepositoryAccess,
snapshot_version: u64,
) -> Result<()>;
}

impl RepositoryExt for RepositoryConfig {
fn create_dataset(&self, writer: &mut impl Write) -> Result<()> {
fn create_dataset(
&self,
writer: &mut impl Write,
access: &impl RepositoryAccess,
snapshot_version: u64,
) -> Result<()> {

Check warning on line 42 in crates/tabby-scheduler/src/dataset.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/dataset.rs#L37-L42

Added lines #L37 - L42 were not covered by tests
let dir = self.dir();

let walk_dir_iter = || {
Expand Down Expand Up @@ -70,7 +80,8 @@ impl RepositoryExt for RepositoryConfig {
language,
content: file_content,
};
writer.write_json_lines([source_file])?;
writer.write_json_lines([source_file.clone()])?;
access.process_file(snapshot_version, source_file);

Check warning on line 84 in crates/tabby-scheduler/src/dataset.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/dataset.rs#L83-L84

Added lines #L83 - L84 were not covered by tests
}
Err(e) => {
error!("Cannot read {relative_path:?}: {e:?}");
Expand All @@ -95,9 +106,10 @@ fn is_source_code(entry: &DirEntry) -> bool {
}
}

pub fn create_dataset(config: &[RepositoryConfig]) -> Result<()> {
pub fn create_dataset(config: &[RepositoryConfig], access: &impl RepositoryAccess) -> Result<()> {

Check warning on line 109 in crates/tabby-scheduler/src/dataset.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/dataset.rs#L109

Added line #L109 was not covered by tests
fs::remove_dir_all(dataset_dir()).ok();
fs::create_dir_all(dataset_dir())?;

let mut writer = FileRotate::new(
SourceFile::files_jsonl(),
AppendCount::new(usize::max_value()),
Expand All @@ -107,10 +119,16 @@ pub fn create_dataset(config: &[RepositoryConfig]) -> Result<()> {
None,
);

let snapshot_version = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("Failed to read system clock")
.as_millis() as u64;
access.start_snapshot(snapshot_version);

Check warning on line 127 in crates/tabby-scheduler/src/dataset.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/dataset.rs#L122-L127

Added lines #L122 - L127 were not covered by tests
let mut deps = DependencyFile::default();
for repository in config {
deps::collect(repository.dir().as_path(), &mut deps);
repository.create_dataset(&mut writer)?;
repository.create_dataset(&mut writer, access, snapshot_version)?;

Check warning on line 131 in crates/tabby-scheduler/src/dataset.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/dataset.rs#L131

Added line #L131 was not covered by tests
}

serdeconv::to_json_file(&deps, dependency_file())?;
Expand Down
8 changes: 4 additions & 4 deletions crates/tabby-scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tracing::{error, info, warn};
pub async fn scheduler<T: RepositoryAccess + 'static>(now: bool, access: T) -> Result<()> {
if now {
let repositories = access.list_repositories().await?;
job_sync(&repositories)?;
job_sync(&repositories, &access)?;

Check warning on line 18 in crates/tabby-scheduler/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/lib.rs#L18

Added line #L18 was not covered by tests
job_index(&repositories)?;
} else {
let access = Arc::new(access);
Expand All @@ -37,7 +37,7 @@ pub async fn scheduler<T: RepositoryAccess + 'static>(now: bool, access: T) -> R
.list_repositories()
.await
.expect("Must be able to retrieve repositories for sync");
if let Err(e) = job_sync(&repositories) {
if let Err(e) = job_sync(&repositories, &*access) {

Check warning on line 40 in crates/tabby-scheduler/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/lib.rs#L40

Added line #L40 was not covered by tests
error!("{e}");
}
if let Err(e) = job_index(&repositories) {
Expand Down Expand Up @@ -66,15 +66,15 @@ fn job_index(repositories: &[RepositoryConfig]) -> Result<()> {
Ok(())
}

fn job_sync(repositories: &[RepositoryConfig]) -> Result<()> {
fn job_sync(repositories: &[RepositoryConfig], access: &impl RepositoryAccess) -> Result<()> {

Check warning on line 69 in crates/tabby-scheduler/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/lib.rs#L69

Added line #L69 was not covered by tests
println!("Syncing {} repositories...", repositories.len());
let ret = repository::sync_repositories(repositories);
if let Err(err) = ret {
return Err(err.context("Failed to sync repositories"));
}

println!("Building dataset...");
let ret = dataset::create_dataset(repositories);
let ret = dataset::create_dataset(repositories, access);

Check warning on line 77 in crates/tabby-scheduler/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby-scheduler/src/lib.rs#L77

Added line #L77 was not covered by tests
if let Err(err) = ret {
return Err(err.context("Failed to build dataset"));
}
Expand Down

0 comments on commit fa077e9

Please sign in to comment.