Skip to content

Commit

Permalink
feat(storage): add assign_storage
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemeowx2 committed Jan 5, 2024
1 parent 707c93b commit 33549d1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,25 @@ pub trait Storage: Send + Sync {
async fn keys(&self) -> Result<Vec<StorageKey>>;
async fn clear(&self) -> Result<()>;
}

// all key-values in `from` will be copied to `to`
pub async fn assign_storage(from: &impl Storage, to: &impl Storage) -> Result<()> {
let keys = from.keys().await?;

for key in keys {
let to_updated_at = to
.get_updated_at(&key.key)
.await?
.unwrap_or_else(|| SystemTime::UNIX_EPOCH);

if to_updated_at >= key.updated_at {
continue;
}

if let Some(item) = from.get(&key.key).await? {
to.set(&key.key, &item.content).await?;
}
}

Ok(())
}

0 comments on commit 33549d1

Please sign in to comment.