1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/// Wrapper around the iroh-docs protocol for integration with GuardianDB.
///
/// This module provides an abstraction layer over the iroh-docs protocol,
/// enabling distributed key-value storage with automatic synchronization
/// and Last-Write-Wins conflict resolution.
use crate::guardian::error::{GuardianError, Result};
use bytes::Bytes;
use iroh_docs::{AuthorId, NamespaceId, api::Doc, protocol::Docs, store::Query, sync::Entry};
use std::sync::Arc;
use tracing::{debug, error, info, warn};
// Import IrohBackend to access Docs.
use super::IrohBackend;
/// iroh-docs client that manages documents and authors.
///
/// This wrapper simplifies using the iroh-docs protocol for KV stores,
/// encapsulating document creation, author management, and basic
/// read/write operations.
#[derive(Clone)]
pub struct WillowDocs {
/// Instance of the Docs protocol.
docs: Arc<Docs>,
/// Default author used for all write operations.
default_author: Option<AuthorId>,
}
impl WillowDocs {
/// Creates a new iroh-docs client from the IrohBackend.
///
/// # Arguments
/// * `backend` - Reference to the IrohBackend that holds the configured Docs
///
/// # Returns
/// Ok(WillowDocs) with no default author configured, Err if Docs is not initialized
pub async fn new(backend: Arc<IrohBackend>) -> Result<Self> {
// Get Docs from the backend.
let docs_lock_guard = backend.get_docs().await?;
let docs_lock = docs_lock_guard.read().await;
let docs = docs_lock
.as_ref()
.ok_or_else(|| GuardianError::Other("Docs not initialized in the backend".into()))?
.clone();
drop(docs_lock);
Ok(Self {
docs: Arc::new(docs),
default_author: None,
})
}
/// Initializes the default author for this client.
///
/// Tries to use the system's default author. If none exists, creates a new one.
/// This author will be used for all write operations.
///
/// # Returns
/// Ok(AuthorId) on success, Err if creating/getting the author fails
pub async fn init_default_author(&mut self) -> Result<AuthorId> {
match self.docs.author_default().await {
Ok(author_id) => {
info!("Using existing default author: {:?}", author_id);
self.default_author = Some(author_id);
Ok(author_id)
}
Err(_) => {
// If no default author exists, create a new one.
match self.docs.author_create().await {
Ok(author_id) => {
info!("Created new default author: {:?}", author_id);
// Try to set it as the default.
if let Err(e) = self.docs.author_set_default(author_id).await {
warn!("Failed to set default author: {:?}", e);
}
self.default_author = Some(author_id);
Ok(author_id)
}
Err(e) => {
error!("Failed to create author: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to create author: {:?}",
e
)))
}
}
}
}
}
/// Returns the default author, initializing it if necessary.
///
/// # Returns
/// Ok(AuthorId) of the default author, Err if initialization fails
pub async fn get_or_init_author(&mut self) -> Result<AuthorId> {
if let Some(author) = self.default_author {
Ok(author)
} else {
self.init_default_author().await
}
}
/// Creates a new document (replica).
///
/// # Returns
/// Ok(Doc) - Handle to the created document
/// Err(GuardianError) - If creating the document fails
pub async fn create_doc(&self) -> Result<Doc> {
match self.docs.create().await {
Ok(doc) => {
info!("Created new document: {:?}", doc.id());
Ok(doc)
}
Err(e) => {
error!("Failed to create document: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to create document: {:?}",
e
)))
}
}
}
/// Shares an existing document by generating a `DocTicket` (capability model).
///
/// The ticket carries the namespace key (secret for writing, public for reading)
/// and this node's addresses so the peer can connect and synchronize. Only whoever
/// receives the ticket can import and synchronize the document — it is the
/// replication access-control point.
///
/// # Arguments
/// * `doc` - The document to share
/// * `write` - `true` grants write capability; `false` read-only
pub async fn share_doc(&self, doc: &Doc, write: bool) -> Result<iroh_docs::DocTicket> {
use iroh_docs::api::protocol::ShareMode;
let mode = if write {
ShareMode::Write
} else {
ShareMode::Read
};
// AddrInfoOptions::default() includes the address/relay information needed for dialing.
doc.share(mode, Default::default()).await.map_err(|e| {
GuardianError::Storage(format!("Failed to share document via ticket: {:?}", e))
})
}
/// Imports a document from a `DocTicket`, joining the ticket's peers.
///
/// This is the secure counterpart of [`share_doc`]: the node starts using the **same
/// namespace** as the creator and begins synchronization (range-based + live) with the
/// peers embedded in the ticket.
pub async fn import_doc(&self, ticket: iroh_docs::DocTicket) -> Result<Doc> {
match self.docs.import(ticket).await {
Ok(doc) => {
info!("Imported shared document via ticket: {:?}", doc.id());
Ok(doc)
}
Err(e) => {
error!("Failed to import document from ticket: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to import document from ticket: {:?}",
e
)))
}
}
}
/// Opens an existing document by its NamespaceId.
///
/// # Arguments
/// * `namespace_id` - ID of the namespace (document) to open
///
/// # Returns
/// Ok(Some(Doc)) if the document exists, Ok(None) if it does not, Err on error
pub async fn open_doc(&self, namespace_id: NamespaceId) -> Result<Option<Doc>> {
match self.docs.open(namespace_id).await {
Ok(doc_option) => {
if let Some(ref doc) = doc_option {
debug!("Opened existing document: {:?}", doc.id());
}
Ok(doc_option)
}
Err(e) => {
error!("Failed to open document: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to open document: {:?}",
e
)))
}
}
}
/// Closes a document.
///
/// # Arguments
/// * `doc` - Reference to the document to close
///
/// # Returns
/// Ok(()) on success, Err on error
pub async fn close_doc(&self, doc: &Doc) -> Result<()> {
match doc.close().await {
Ok(_) => {
debug!("Closed document: {:?}", doc.id());
Ok(())
}
Err(e) => {
error!("Failed to close document: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to close document: {:?}",
e
)))
}
}
}
/// Removes a document permanently.
///
/// # Arguments
/// * `namespace_id` - ID of the namespace (document) to remove
///
/// # Returns
/// Ok(()) on success, Err on error
pub async fn drop_doc(&self, namespace_id: NamespaceId) -> Result<()> {
match self.docs.drop_doc(namespace_id).await {
Ok(_) => {
info!("Dropped document: {:?}", namespace_id);
Ok(())
}
Err(e) => {
error!("Failed to drop document: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to drop document: {:?}",
e
)))
}
}
}
/// Sets a value for a key in a document.
///
/// # Arguments
/// * `doc` - Reference to the document
/// * `author_id` - Author ID for this operation
/// * `key` - Key (will be converted to Bytes)
/// * `value` - Value (will be converted to Bytes)
///
/// # Returns
/// Ok(Hash) - Hash of the stored content, Err on error
pub async fn set_bytes(
&self,
doc: &Doc,
author_id: AuthorId,
key: impl Into<Bytes>,
value: impl Into<Bytes>,
) -> Result<iroh_blobs::Hash> {
match doc.set_bytes(author_id, key, value).await {
Ok(hash) => {
debug!("Set bytes in document {:?}: hash={:?}", doc.id(), hash);
// Convert from iroh_docs::Hash (0.92.0) to iroh_blobs::Hash (0.94.0).
// Both share the same hash structure (BLAKE3, 32 bytes), so the
// conversion through the bytes is safe.
let hash_bytes = hash.as_bytes();
let result_hash = iroh_blobs::Hash::from_bytes(*hash_bytes);
Ok(result_hash)
}
Err(e) => {
error!("Failed to set bytes: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to set bytes: {:?}",
e
)))
}
}
}
/// Removes a key from a document.
///
/// # Arguments
/// * `doc` - Reference to the document
/// * `author_id` - Author ID for this operation
/// * `key` - Key to remove (prefix)
///
/// # Returns
/// Ok(usize) - Number of deleted entries, Err on error
pub async fn del(
&self,
doc: &Doc,
author_id: AuthorId,
key: impl Into<Bytes>,
) -> Result<usize> {
match doc.del(author_id, key).await {
Ok(count) => {
debug!("Deleted {} entries from document {:?}", count, doc.id());
Ok(count)
}
Err(e) => {
error!("Failed to delete: {:?}", e);
Err(GuardianError::Storage(format!("Failed to delete: {:?}", e)))
}
}
}
/// Gets a single entry from a document.
///
/// # Arguments
/// * `doc` - Reference to the document
/// * `query` - Query used to look up the entry
///
/// # Returns
/// Ok(Some(Entry)) if found, Ok(None) if not found, Err on error
pub async fn get_one(&self, doc: &Doc, query: impl Into<Query>) -> Result<Option<Entry>> {
match doc.get_one(query).await {
Ok(entry_option) => {
if let Some(ref entry) = entry_option {
debug!(
"Got entry from document {:?}: key={:?}",
doc.id(),
String::from_utf8_lossy(entry.key())
);
}
Ok(entry_option)
}
Err(e) => {
error!("Failed to get entry: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to get entry: {:?}",
e
)))
}
}
}
/// Gets multiple entries from a document using a query.
///
/// # Arguments
/// * `doc` - Reference to the document
/// * `query` - Query used to filter the entries
///
/// # Returns
/// Ok(Vec<Entry>) - List of matching entries, Err on error
pub async fn get_many(&self, doc: &Doc, query: impl Into<Query>) -> Result<Vec<Entry>> {
use futures::StreamExt;
match doc.get_many(query).await {
Ok(stream) => {
let entries: Vec<Entry> = stream
.filter_map(|result| async move {
match result {
Ok(entry) => Some(entry),
Err(e) => {
warn!("Error reading entry from stream: {:?}", e);
None
}
}
})
.collect()
.await;
debug!("Got {} entries from document {:?}", entries.len(), doc.id());
Ok(entries)
}
Err(e) => {
error!("Failed to get entries: {:?}", e);
Err(GuardianError::Storage(format!(
"Failed to get entries: {:?}",
e
)))
}
}
}
/// Returns the underlying Docs instance for advanced operations.
pub fn docs(&self) -> &Arc<Docs> {
&self.docs
}
/// Returns the default AuthorId (without initializing one if it does not exist).
///
/// # Returns
/// Ok(AuthorId) if a default author exists, Err otherwise
pub async fn default_author_id(&self) -> Result<AuthorId> {
if let Some(author) = self.default_author {
Ok(author)
} else {
// Try to get the default author from docs.
self.docs.author_default().await.map_err(|e| {
GuardianError::Storage(format!("No default author configured: {:?}", e))
})
}
}
}
#[cfg(test)]
mod tests {
// Note: Full tests require a configured Iroh environment.
// These are basic unit tests to verify the interface.
#[tokio::test]
async fn test_client_creation() {
// Basic creation test - requires a mock Docs or skip.
// This test serves as documentation of the expected API.
}
}