use batch write for virtual branches

This commit is contained in:
Nikita Galaiko 2023-12-22 13:53:40 +01:00 committed by GitButler
parent 11f4956503
commit a22319009f
4 changed files with 122 additions and 133 deletions

View File

@ -41,7 +41,7 @@ impl<'writer> DeltasWriter<'writer> {
let path = path.as_ref();
self.writer
.remove(&format!("session/wd/{}", path.display()))?;
.remove(format!("session/wd/{}", path.display()))?;
tracing::debug!(
project_id = %self.repository.get_project_id(),

View File

@ -45,7 +45,6 @@ fn test_should_write_full_session() -> Result<()> {
Writer::new(&gb_repository)?.write(&session)?;
dbg!(&gb_repository.session_path());
assert_eq!(
std::fs::read_to_string(gb_repository.session_path().join("meta/id"))?,
session.id.to_string()

View File

@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use anyhow::Result;
use crate::{gb_repository, writer};
@ -18,100 +18,94 @@ impl<'writer> BranchWriter<'writer> {
self.repository.mark_active_session()?;
let _lock = self.repository.lock();
self.writer.remove(&format!("branches/{}", branch.id))?;
self.writer.remove(format!("branches/{}", branch.id))?;
Ok(())
}
pub fn write(&self, branch: &mut Branch) -> Result<()> {
self.repository.mark_active_session()?;
let _lock = self.repository.lock();
branch.updated_timestamp_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)?
.as_millis();
self.writer
.write_string(
&format!("branches/{}/id", branch.id),
&branch.id.to_string(),
)
.context("Failed to write branch id")?;
let mut batch = vec![];
self.writer
.write_string(&format!("branches/{}/meta/name", branch.id), &branch.name)
.context("Failed to write branch name")?;
batch.push(writer::BatchTask::Write(
format!("branches/{}/id", branch.id),
branch.id.to_string(),
));
self.writer
.write_string(&format!("branches/{}/meta/notes", branch.id), &branch.notes)
.context("Failed to write notes")?;
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/name", branch.id),
branch.name.clone(),
));
self.writer
.write_usize(&format!("branches/{}/meta/order", branch.id), &branch.order)
.context("Failed to write branch order")?;
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/notes", branch.id),
branch.notes.clone(),
));
self.writer
.write_bool(
&format!("branches/{}/meta/applied", branch.id),
&branch.applied,
)
.context("Failed to write branch applied")?;
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/order", branch.id),
branch.order.to_string(),
));
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/applied", branch.id),
branch.applied.to_string(),
));
if let Some(upstream) = &branch.upstream {
self.writer
.write_string(
&format!("branches/{}/meta/upstream", branch.id),
&upstream.to_string(),
)
.context("Failed to write branch upstream")?;
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/upstream", branch.id),
upstream.to_string(),
));
} else {
self.writer
.remove(&format!("branches/{}/meta/upstream", branch.id))?;
batch.push(writer::BatchTask::Remove(format!(
"branches/{}/meta/upstream",
branch.id
)));
}
if let Some(upstream_head) = &branch.upstream_head {
self.writer
.write_string(
&format!("branches/{}/meta/upstream_head", branch.id),
&upstream_head.to_string(),
)
.context("Failed to write branch upstream head")?;
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/upstream_head", branch.id),
upstream_head.to_string(),
));
} else {
self.writer
.remove(&format!("branches/{}/meta/upstream_head", branch.id))?;
batch.push(writer::BatchTask::Remove(format!(
"branches/{}/meta/upstream_head",
branch.id
)));
}
self.writer
.write_string(
&format!("branches/{}/meta/tree", branch.id),
&branch.tree.to_string(),
)
.context("Failed to write branch tree")?;
self.writer
.write_string(
&format!("branches/{}/meta/head", branch.id),
&branch.head.to_string(),
)
.context("Failed to write branch head")?;
self.writer
.write_u128(
&format!("branches/{}/meta/created_timestamp_ms", branch.id),
&branch.created_timestamp_ms,
)
.context("Failed to write branch created timestamp")?;
self.writer
.write_u128(
&format!("branches/{}/meta/updated_timestamp_ms", branch.id),
&branch.updated_timestamp_ms,
)
.context("Failed to write branch updated timestamp")?;
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/tree", branch.id),
branch.tree.to_string(),
));
self.writer
.write_string(
&format!("branches/{}/meta/ownership", branch.id),
&branch.ownership.to_string(),
)
.context("Failed to write branch ownership")?;
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/head", branch.id),
branch.head.to_string(),
));
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/created_timestamp_ms", branch.id),
branch.created_timestamp_ms.to_string(),
));
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/updated_timestamp_ms", branch.id),
branch.updated_timestamp_ms.to_string(),
));
batch.push(writer::BatchTask::Write(
format!("branches/{}/meta/ownership", branch.id),
branch.ownership.to_string(),
));
self.writer.batch(&batch)?;
Ok(())
}
@ -124,6 +118,7 @@ mod tests {
sync::atomic::{AtomicUsize, Ordering},
};
use anyhow::Context;
use once_cell::sync::Lazy;
use crate::{

View File

@ -17,32 +17,32 @@ impl<'writer> TargetWriter<'writer> {
pub fn write_default(&self, target: &Target) -> Result<()> {
self.repository.mark_active_session()?;
let _lock = self.repository.lock();
let mut batch = vec![
writer::BatchTask::Write(
"branches/target/branch_name",
format!("{}/{}", target.branch.remote(), target.branch.branch()),
),
writer::BatchTask::Write(
"branches/target/remote_name",
target.branch.remote().to_string(),
),
writer::BatchTask::Write("branches/target/remote_url", target.remote_url.clone()),
writer::BatchTask::Write("branches/target/sha", target.sha.to_string()),
];
if let Some(last_fetched) = target.last_fetched_ms {
batch.push(writer::BatchTask::Write(
"branches/target/last_fetched_ms",
last_fetched.to_string(),
));
} else {
batch.push(writer::BatchTask::Remove("branches/target/last_fetched_ms"));
}
self.writer
.write_string(
"branches/target/branch_name",
&format!("{}/{}", target.branch.remote(), target.branch.branch()),
)
.context("Failed to write default target branch name")?;
self.writer
.write_string("branches/target/remote_name", target.branch.remote())
.context("Failed to write default target remote name ")?;
self.writer
.write_string("branches/target/remote_url", &target.remote_url)
.context("Failed to write default target remote name ")?;
self.writer
.write_string("branches/target/sha", &target.sha.to_string())
.context("Failed to write default target sha")?;
if let Some(last_fetched) = target.last_fetched_ms {
self.writer
.write_u128("branches/target/last_fetched_ms", &last_fetched)
.context("Failed to write default target last fetched")?;
} else {
self.writer
.remove("branches/target/last_fetched_ms")
.context("Failed to remove default target last fetched")?;
}
.batch(&batch)
.context("Failed to write default target")?;
Ok(())
}
@ -51,46 +51,41 @@ impl<'writer> TargetWriter<'writer> {
.mark_active_session()
.context("Failed to get or create current session")?;
let _lock = self.repository.lock();
let mut batch = vec![
writer::BatchTask::Write(
format!("branches/{}/target/branch_name", id),
format!("{}/{}", target.branch.remote(), target.branch.branch()),
),
writer::BatchTask::Write(
format!("branches/{}/target/remote_name", id),
target.branch.remote().to_string(),
),
writer::BatchTask::Write(
format!("branches/{}/target/remote_url", id),
target.remote_url.clone(),
),
writer::BatchTask::Write(
format!("branches/{}/target/sha", id),
target.sha.to_string(),
),
];
self.writer
.write_string(
&format!("branches/{}/target/branch_name", id),
&format!("{}/{}", target.branch.remote(), target.branch.branch()),
)
.context("Failed to write branch target branch name")?;
self.writer
.write_string(
&format!("branches/{}/target/remote_name", id),
target.branch.remote(),
)
.context("Failed to write branch target remote")?;
self.writer
.write_string(
&format!("branches/{}/target/remote_url", id),
&target.remote_url,
)
.context("Failed to write branch target remote")?;
self.writer
.write_string(
&format!("branches/{}/target/sha", id),
&target.sha.to_string(),
)
.context("Failed to write branch target sha")?;
if let Some(last_fetched_ms) = target.last_fetched_ms {
self.writer
.write_u128(
&format!("branches/{id}/target/last_fetched_ms"),
&last_fetched_ms,
)
.context("Failed to write default target last fetched")?;
if let Some(last_fetched) = target.last_fetched_ms {
batch.push(writer::BatchTask::Write(
format!("branches/{}/target/last_fetched_ms", id),
last_fetched.to_string(),
));
} else {
self.writer
.remove(&format!("branches/{id}/target/last_fetched_ms"))
.context("Failed to remove default target last fetched")?;
batch.push(writer::BatchTask::Remove(format!(
"branches/{}/target/last_fetched_ms",
id
)));
}
self.writer
.batch(&batch)
.context("Failed to write target")?;
Ok(())
}
}