-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89b3460
commit 508d725
Showing
2 changed files
with
118 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,72 @@ | ||
const DEFAULT_MAX_RETRIES: usize = 5; | ||
|
||
use crate::Result; | ||
|
||
pub struct SyncContext { | ||
pub sync_url: String, | ||
pub auth_token: Option<String>, | ||
pub max_retries: usize, | ||
pub durable_frame_num: u32, | ||
max_frame_no: u32, | ||
db_path: String, | ||
} | ||
|
||
impl SyncContext { | ||
pub fn new(sync_url: String, auth_token: Option<String>) -> Self { | ||
Self { | ||
pub fn new(sync_url: String, auth_token: Option<String>, db_path: impl Into<String>) -> Self { | ||
let mut ctx = Self { | ||
sync_url, | ||
auth_token, | ||
durable_frame_num: 0, | ||
max_retries: DEFAULT_MAX_RETRIES, | ||
} | ||
max_frame_no: 0, | ||
db_path: db_path.into(), | ||
}; | ||
|
||
ctx.read_and_update_metadata().unwrap(); | ||
|
||
ctx | ||
} | ||
|
||
pub(crate) fn max_frame_no(&self) -> u32 { | ||
self.max_frame_no | ||
} | ||
|
||
pub(crate) fn set_max_frame_no(&mut self, max_frame_no: u32) -> Result<()> { | ||
// TODO: check if max_frame_no is larger than current known max_frame_no | ||
self.max_frame_no = max_frame_no; | ||
|
||
self.update_metadata()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn update_metadata(&self) -> Result<()> { | ||
let path = format!("{}-info", self.db_path); | ||
|
||
let contents = serde_json::to_vec(&MetadataJson { | ||
max_frame_no: self.max_frame_no, | ||
}) | ||
.unwrap(); | ||
|
||
std::fs::write(path, contents).unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn read_and_update_metadata(&mut self) -> Result<()> { | ||
let path = format!("{}-info", self.db_path); | ||
|
||
let contents = std::fs::read(&path).unwrap(); | ||
|
||
let metadata = serde_json::from_slice::<MetadataJson>(&contents[..]).unwrap(); | ||
|
||
self.max_frame_no = metadata.max_frame_no; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(serde::Serialize, serde::Deserialize)] | ||
struct MetadataJson { | ||
max_frame_no: u32, | ||
} |