Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async backends are here #2342

Merged
merged 4 commits into from
Oct 9, 2023
Merged
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
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ keywords = ["VCS", "DVCS", "SCM", "Git", "Mercurial"]
anyhow = "1.0.75"
assert_cmd = "2.0.8"
assert_matches = "1.5.0"
async-trait = "0.1.73"
backoff = "0.4.0"
blake2 = "0.10.6"
byteorder = "1.5.0"
Expand All @@ -39,6 +40,7 @@ digest = "0.10.7"
dirs = "5.0.1"
either = "1.9.0"
esl01-renderdag = "0.3.0"
futures = "0.3.28"
glob = "0.3.1"
git2 = "0.17.2"
hex = "0.4.3"
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ libc = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
assert_cmd = { workspace = true }
assert_matches = { workspace = true }
insta = { workspace = true }
Expand Down
22 changes: 12 additions & 10 deletions cli/examples/custom-backend/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::any::Any;
use std::io::Read;
use std::path::Path;

use async_trait::async_trait;
use jj_cli::cli_util::{CliRunner, CommandError, CommandHelper};
use jj_cli::ui::Ui;
use jj_lib::backend::{
Expand Down Expand Up @@ -86,6 +87,7 @@ impl JitBackend {
}
}

#[async_trait]
impl Backend for JitBackend {
fn as_any(&self) -> &dyn Any {
self
Expand Down Expand Up @@ -115,40 +117,40 @@ impl Backend for JitBackend {
self.inner.empty_tree_id()
}

fn read_file(&self, path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
self.inner.read_file(path, id)
async fn read_file(&self, path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
self.inner.read_file(path, id).await
}

fn write_file(&self, path: &RepoPath, contents: &mut dyn Read) -> BackendResult<FileId> {
self.inner.write_file(path, contents)
}

fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String> {
self.inner.read_symlink(path, id)
async fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String> {
self.inner.read_symlink(path, id).await
}

fn write_symlink(&self, path: &RepoPath, target: &str) -> BackendResult<SymlinkId> {
self.inner.write_symlink(path, target)
}

fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
self.inner.read_tree(path, id)
async fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
self.inner.read_tree(path, id).await
}

fn write_tree(&self, path: &RepoPath, contents: &Tree) -> BackendResult<TreeId> {
self.inner.write_tree(path, contents)
}

fn read_conflict(&self, path: &RepoPath, id: &ConflictId) -> BackendResult<Conflict> {
self.inner.read_conflict(path, id)
async fn read_conflict(&self, path: &RepoPath, id: &ConflictId) -> BackendResult<Conflict> {
self.inner.read_conflict(path, id).await
}

fn write_conflict(&self, path: &RepoPath, contents: &Conflict) -> BackendResult<ConflictId> {
self.inner.write_conflict(path, contents)
}

fn read_commit(&self, id: &CommitId) -> BackendResult<Commit> {
self.inner.read_commit(id)
async fn read_commit(&self, id: &CommitId) -> BackendResult<Commit> {
self.inner.read_commit(id).await
}

fn write_commit(&self, contents: Commit) -> BackendResult<(CommitId, Commit)> {
Expand Down
3 changes: 3 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ harness = false
version_check = { workspace = true }

[dependencies]
async-trait = { workspace = true}
backoff = { workspace = true }
blake2 = { workspace = true }
byteorder = { workspace = true }
bytes = { workspace = true }
chrono = { workspace = true }
config = { workspace = true }
digest = { workspace = true }
futures = { workspace = true }
either = { workspace = true }
git2 = { workspace = true }
hex = { workspace = true }
Expand Down Expand Up @@ -63,6 +65,7 @@ num_cpus = { workspace = true }
pretty_assertions = { workspace = true }
test-case = { workspace = true }
testutils = { workspace = true }
tokio = { workspace = true, features = ["full"] }

[features]
default = []
Expand Down
12 changes: 7 additions & 5 deletions lib/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::io::Read;
use std::result::Result;
use std::vec::Vec;

use async_trait::async_trait;
use thiserror::Error;

use crate::content_hash::ContentHash;
Expand Down Expand Up @@ -465,6 +466,7 @@ pub fn make_root_commit(root_change_id: ChangeId, empty_tree_id: TreeId) -> Comm
}
}

#[async_trait]
pub trait Backend: Send + Sync + Debug {
fn as_any(&self) -> &dyn Any;

Expand All @@ -484,23 +486,23 @@ pub trait Backend: Send + Sync + Debug {

fn empty_tree_id(&self) -> &TreeId;

fn read_file(&self, path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>>;
async fn read_file(&self, path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>>;

fn write_file(&self, path: &RepoPath, contents: &mut dyn Read) -> BackendResult<FileId>;

fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String>;
async fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String>;

fn write_symlink(&self, path: &RepoPath, target: &str) -> BackendResult<SymlinkId>;

fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree>;
async fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree>;

fn write_tree(&self, path: &RepoPath, contents: &Tree) -> BackendResult<TreeId>;

fn read_conflict(&self, path: &RepoPath, id: &ConflictId) -> BackendResult<Conflict>;
async fn read_conflict(&self, path: &RepoPath, id: &ConflictId) -> BackendResult<Conflict>;

fn write_conflict(&self, path: &RepoPath, contents: &Conflict) -> BackendResult<ConflictId>;

fn read_commit(&self, id: &CommitId) -> BackendResult<Commit>;
async fn read_commit(&self, id: &CommitId) -> BackendResult<Commit>;

/// Writes a commit and returns its ID and the commit itself. The commit
/// should contain the data that was actually written, which may differ
Expand Down
Loading