Parallel Implementation: UCXLWatcher (FS-to-Metadata) and chrs-sync (P2P Graph Sync)
This commit is contained in:
13
chrs-sync/Cargo.toml
Normal file
13
chrs-sync/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "chrs-sync"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
chrs-mail = { path = "../chrs-mail" }
|
||||
chrs-graph = { path = "../chrs-graph" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
uuid = { version = "1.0", features = ["v4"] }
|
||||
chrono = "0.4"
|
||||
62
chrs-sync/src/lib.rs
Normal file
62
chrs-sync/src/lib.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use chrs_mail::{Mailbox, Message};
|
||||
use chrono::Utc;
|
||||
use std::process::Command;
|
||||
use uuid::Uuid;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub struct SyncManager {
|
||||
mailbox: Mailbox,
|
||||
repo_path: PathBuf,
|
||||
}
|
||||
|
||||
impl SyncManager {
|
||||
pub fn new(mailbox: Mailbox, repo_path: PathBuf) -> Self {
|
||||
Self { mailbox, repo_path }
|
||||
}
|
||||
|
||||
pub fn broadcast_state(&self, from_peer: &str, to_peer: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Get current dolt hash
|
||||
let output = Command::new("dolt")
|
||||
.args(&["log", "-n", "1", "--format", "%H"])
|
||||
.current_dir(&self.repo_path)
|
||||
.output()?;
|
||||
|
||||
let current_hash = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||
|
||||
let msg = Message {
|
||||
id: Uuid::new_v4(),
|
||||
from_peer: from_peer.into(),
|
||||
to_peer: to_peer.into(),
|
||||
topic: "sync_signal".into(),
|
||||
payload: serde_json::json!({ "commit_hash": current_hash }),
|
||||
sent_at: Utc::now(),
|
||||
read_at: None,
|
||||
};
|
||||
|
||||
self.mailbox.send(&msg)?;
|
||||
println!("Broadcasted sync signal: {} from {}", current_hash, from_peer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn handle_sync_signal(&self, msg: &Message) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if msg.topic != "sync_signal" {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let remote_hash = msg.payload["commit_hash"].as_str().unwrap_or_default();
|
||||
println!("Received sync signal for hash: {}", remote_hash);
|
||||
|
||||
// In a real P2P scenario, we would pull from the remote peer's URL.
|
||||
// For now, we simulate by attempting a 'dolt pull' if a remote is configured.
|
||||
let status = Command::new("dolt")
|
||||
.args(&["pull", "origin"])
|
||||
.current_dir(&self.repo_path)
|
||||
.status()?;
|
||||
|
||||
if status.success() {
|
||||
println!("Successfully pulled updates for hash: {}", remote_hash);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user