Skip to content

Commit

Permalink
feat: leave and drop docs (#1589)
Browse files Browse the repository at this point in the history
## Description

In `iroh-sync`:
* feat: Add `Store::remove_replica` to remove a replica (namespace
secret key and all entries)
* feat: Adds `closed` atomic bool on `Replica` to ensure no operations
can be performed on closed or removed replicas
* refactor: To not add yet another `Arc` for the `closed` marker, move
all things into `InnerReplica`

In `iroh`:
* feat: Add `DocDrop` to RPC protocol
* rename `StopSync` to `Leave`
* feat: Add `doc leave` command to CLI / console. `doc leave` runs
`DocStopSync` in the RPC layer (we already had that),
* feat: Add `doc drop` command to delete a document

## Notes & open questions

Fixes #1497 

## Change checklist

- [x] Self-review.
- [x] Documentation updates if relevant.
- [x] Tests if relevant.
  • Loading branch information
Frando authored Oct 10, 2023
1 parent 872f3b1 commit d7a3dd3
Show file tree
Hide file tree
Showing 15 changed files with 395 additions and 124 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions iroh-sync/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ impl fmt::Debug for Author {

impl fmt::Debug for NamespacePublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NamespaceId({})", self)
write!(f, "NamespacePublicKey({})", self)
}
}

impl fmt::Debug for AuthorPublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "AuthorId({})", self)
write!(f, "AuthorPublicKey({})", self)
}
}

Expand Down Expand Up @@ -410,7 +410,7 @@ impl AuthorId {
/// Convert into [`AuthorPublicKey`].
///
/// Fails if the bytes of this [`AuthorId`] are not a valid [`ed25519_dalek`] curve point.
pub fn into_public_key<S: PublicKeyStore>(&self) -> Result<AuthorPublicKey, SignatureError> {
pub fn into_public_key(&self) -> Result<AuthorPublicKey, SignatureError> {
AuthorPublicKey::from_bytes(&self.0)
}
}
Expand Down Expand Up @@ -439,7 +439,7 @@ impl NamespaceId {
/// Convert into [`NamespacePublicKey`].
///
/// Fails if the bytes of this [`NamespaceId`] are not a valid [`ed25519_dalek`] curve point.
pub fn into_public_key<S: PublicKeyStore>(&self) -> Result<NamespacePublicKey, SignatureError> {
pub fn into_public_key(&self) -> Result<NamespacePublicKey, SignatureError> {
NamespacePublicKey::from_bytes(&self.0)
}
}
Expand Down
6 changes: 6 additions & 0 deletions iroh-sync/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ pub trait Store: std::fmt::Debug + Clone + Send + Sync + 'static {
/// instance from the store's cache.
fn close_replica(&self, namespace: &NamespaceId);

/// Remove a replica.
///
/// Completely removes a replica and deletes both the namespace private key and all document
/// entries.
fn remove_replica(&self, namespace: &NamespaceId) -> Result<()>;

/// Create a new author key and persist it in the store.
fn new_author<R: CryptoRngCore + ?Sized>(&self, rng: &mut R) -> Result<Author> {
let author = Author::new(rng);
Expand Down
22 changes: 19 additions & 3 deletions iroh-sync/src/store/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl super::Store for Store {

fn close_replica(&self, namespace_id: &NamespaceId) {
if let Some(replica) = self.replicas.write().remove(namespace_id) {
replica.unsubscribe();
replica.close();
}
}

Expand Down Expand Up @@ -206,6 +206,22 @@ impl super::Store for Store {
Ok(replica)
}

fn remove_replica(&self, namespace: &NamespaceId) -> Result<()> {
self.close_replica(namespace);
self.replicas.write().remove(namespace);
let start = range_start(namespace);
let end = range_end(namespace);
let write_tx = self.db.begin_write()?;
{
let mut record_table = write_tx.open_table(RECORDS_TABLE)?;
record_table.drain(start..=end)?;
let mut namespace_table = write_tx.open_table(NAMESPACES_TABLE)?;
namespace_table.remove(namespace.as_bytes())?;
}
write_tx.commit()?;
Ok(())
}

fn get_many(
&self,
namespace: NamespaceId,
Expand Down Expand Up @@ -547,7 +563,7 @@ impl crate::ranger::Store<SignedEntry> for StoreInstance {
// iterator for all entries in replica
let iter = RangeIterator::with_range(
&self.store.db,
|table| table.range(start..end),
|table| table.range(start..=end),
RangeFilter::None,
)?;
// empty iterator, returns nothing
Expand Down Expand Up @@ -583,7 +599,7 @@ impl crate::ranger::Store<SignedEntry> for StoreInstance {
// iterator for entries from range.x to end
let iter2 = RangeIterator::with_range(
&self.store.db,
|table| table.range(start..end),
|table| table.range(start..=end),
RangeFilter::None,
)?;
iter.chain(iter2)
Expand Down
9 changes: 8 additions & 1 deletion iroh-sync/src/store/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl super::Store for Store {

fn close_replica(&self, namespace_id: &NamespaceId) {
if let Some(replica) = self.replicas.read().get(namespace_id) {
replica.unsubscribe();
replica.close();
}
}

Expand Down Expand Up @@ -100,6 +100,13 @@ impl super::Store for Store {
Ok(replica)
}

fn remove_replica(&self, namespace: &NamespaceId) -> Result<()> {
self.close_replica(namespace);
self.replicas.write().remove(namespace);
self.replica_records.write().remove(namespace);
Ok(())
}

fn get_many(
&self,
namespace: NamespaceId,
Expand Down
Loading

0 comments on commit d7a3dd3

Please sign in to comment.