guardian-db 0.15.0

High-performance, local-first decentralized database built on Rust and Iroh
Documentation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use crate::events;
use crate::guardian::error::GuardianError;
use crate::p2p;
use crate::p2p::network::core::gossip::EpidemicPubSub;
use crate::traits::{EventPubSubMessage, PubSubInterface, PubSubTopic, TracerWrapper};
use futures::Stream;
use iroh::NodeId;
use opentelemetry::trace::noop::NoopTracer;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Mutex, RwLock, mpsc};
use tokio_stream::{StreamExt, wrappers::ReceiverStream};
use tokio_util::sync::CancellationToken;
use tracing::{Span, error, instrument, warn};

pub mod direct_channel;
pub mod one_on_one_channel;

pub const PROTOCOL: &str = "/guardian-db/direct-channel/1.2.0";
pub const CONNECTION_TIMEOUT: Duration = Duration::from_secs(30);
pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
pub const MAX_MESSAGE_SIZE: usize = 1024 * 1024; // 1MB

/// A struct `CoreApiPubSub` gerencia a lógica de pubsub P2P para o nó do GuardianDB via iroh-gossip.
pub struct CoreApiPubSub {
    pub epidemic_pubsub: Arc<EpidemicPubSub>,
    pub span: Span,
    pub id: NodeId,
    pub poll_interval: Duration,
    pub tracer: Arc<TracerWrapper>,
    topics: Mutex<HashMap<String, Arc<PsTopic>>>,
    /// Token para cancelamento gracioso de todas as operações
    cancellation_token: CancellationToken,
}

#[async_trait::async_trait]
impl PubSubInterface for CoreApiPubSub {
    type Error = GuardianError;

    #[instrument(level = "debug", skip(self))]
    async fn topic_subscribe(
        &self,
        topic: &str,
    ) -> Result<Arc<dyn crate::traits::PubSubTopic<Error = GuardianError>>, Self::Error> {
        let mut topics_guard = self.topics.lock().await;

        // Se o tópico já estiver na nossa cache, retorna a instância existente.
        if let Some(t) = topics_guard.get(topic) {
            return Ok(t.clone() as Arc<dyn crate::traits::PubSubTopic<Error = GuardianError>>);
        }

        // Subscreve ao tópico via EpidemicPubSub
        let inner_topic = self.epidemic_pubsub.topic_subscribe(topic).await?;

        // Cria um novo Arc<CoreApiPubSub> compartilhando os mesmos recursos
        let ps_arc = Arc::new(CoreApiPubSub {
            epidemic_pubsub: self.epidemic_pubsub.clone(),
            span: self.span.clone(),
            id: self.id,
            poll_interval: self.poll_interval,
            tracer: self.tracer.clone(),
            topics: Mutex::new(HashMap::new()),
            cancellation_token: self.cancellation_token.clone(),
        });

        // Cria um novo tópico
        let new_topic = Arc::new(PsTopic {
            topic: topic.to_string(),
            ps: ps_arc,
            inner_topic,
            members: Default::default(),
            cancellation_token: self.cancellation_token.child_token(),
        });

        // Insere o novo tópico no nosso cache.
        topics_guard.insert(topic.to_string(), new_topic.clone());

        Ok(new_topic as Arc<dyn crate::traits::PubSubTopic<Error = GuardianError>>)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

// A struct `PsTopic` será
// armazenada em um `Arc` para compartilhamento seguro entre threads.
/// A struct `PsTopic` contém o estado e a lógica para um único tópico do pubsub P2P via iroh-gossip.
pub struct PsTopic {
    topic: String,
    ps: Arc<CoreApiPubSub>,
    /// Tópico do EpidemicPubSub para comunicação P2P
    inner_topic: Arc<dyn PubSubTopic<Error = GuardianError>>,
    members: RwLock<Vec<NodeId>>,
    /// Token para cancelamento gracioso das operações deste tópico
    cancellation_token: CancellationToken,
}

impl PsTopic {
    // Publica mensagem via iroh-gossip
    #[instrument(level = "debug", skip(self, message))]
    pub async fn publish(&self, message: &[u8]) -> crate::guardian::error::Result<()> {
        // Verifica se o tópico não foi cancelado
        if self.cancellation_token.is_cancelled() {
            return Err(crate::guardian::error::GuardianError::Store(
                "Cannot publish to cancelled topic".to_string(),
            ));
        }

        // Validação básica da mensagem
        if message.is_empty() {
            return Err(crate::guardian::error::GuardianError::Store(
                "Cannot publish empty message".to_string(),
            ));
        }

        // Publica via EpidemicPubSub usando o método correto
        self.ps
            .epidemic_pubsub
            .publish_to_topic(&self.topic, message)
            .await?;
        Ok(())
    }

    #[instrument(level = "debug", skip(self))]
    pub async fn peers(&self) -> crate::guardian::error::Result<Vec<NodeId>> {
        // Obtém peers do tópico via iroh-gossip
        self.inner_topic.peers().await
    }

    // Calcula diferença de peers (joining/leaving) via iroh-gossip
    #[instrument(level = "debug", skip(self))]
    pub async fn peers_diff(&self) -> crate::guardian::error::Result<(Vec<NodeId>, Vec<NodeId>)> {
        let current_peers = self.inner_topic.peers().await?;
        let mut members_guard = self.members.write().await;

        // Identifica peers que entraram
        let joining: Vec<NodeId> = current_peers
            .iter()
            .filter(|peer| !members_guard.contains(peer))
            .copied()
            .collect();

        // Identifica peers que saíram
        let leaving: Vec<NodeId> = members_guard
            .iter()
            .filter(|peer| !current_peers.contains(peer))
            .copied()
            .collect();

        // Atualiza lista de membros
        *members_guard = current_peers;

        Ok((joining, leaving))
    }

    // Retorna um receptor de canal (`Receiver`) que emitirá eventos de peers
    // entrando ou saindo do tópico.
    // Adiciona cancelamento adequado e melhor gestão de recursos
    #[instrument(level = "debug", skip(self))]
    pub async fn watch_peers_channel(
        self: &Arc<Self>,
    ) -> crate::guardian::error::Result<mpsc::Receiver<Arc<dyn std::any::Any + Send + Sync>>> {
        let (tx, rx) = mpsc::channel(32);

        // Clona o Arc para que a nova task possa ter sua própria referência.
        let topic_clone = self.clone();
        let cancellation_token = self.cancellation_token.clone();

        tokio::spawn(async move {
            loop {
                // Verifica cancelamento antes de cada iteração
                if cancellation_token.is_cancelled() {
                    break;
                }

                // Chama a função que calcula a diferença de peers
                let peers_diff_result = topic_clone.peers_diff().await;

                let (joining, leaving) = match peers_diff_result {
                    Ok((j, l)) => (j, l),
                    Err(e) => {
                        // Loga o erro e encerra a task.
                        // Quando `tx` sai de escopo (é dropado), o lado do receptor
                        // saberá que o canal foi fechado.
                        error!("Erro ao verificar a diferença de peers: {:?}", e);
                        return;
                    }
                };

                for node_id in joining {
                    let event = p2p::new_event_peer_join(node_id, topic_clone.topic().to_string());
                    // Converte EventPubSub para o tipo esperado
                    let event_any: Arc<dyn std::any::Any + Send + Sync> = Arc::new(event);
                    if tx.send(event_any).await.is_err() {
                        // O receptor foi fechado, então a task não precisa mais rodar.
                        return;
                    }
                }

                for node_id in leaving {
                    let event = p2p::new_event_peer_leave(node_id, topic_clone.topic().to_string());
                    // Converte EventPubSub para o tipo esperado
                    let event_any: Arc<dyn std::any::Any + Send + Sync> = Arc::new(event);
                    if tx.send(event_any).await.is_err() {
                        return;
                    }
                }

                // Usa select! para permitir cancelamento durante o sleep
                tokio::select! {
                    _ = tokio::time::sleep(topic_clone.ps.poll_interval) => {},
                    _ = cancellation_token.cancelled() => {
                        break;
                    }
                }
            }
        });

        Ok(rx)
    }

    #[instrument(level = "debug", skip(self))]
    pub async fn watch_messages(
        &self,
    ) -> crate::guardian::error::Result<mpsc::Receiver<EventPubSubMessage>> {
        // Obtém stream de mensagens do tópico P2P via iroh-gossip
        let mut message_stream = self.inner_topic.watch_messages().await?;

        let (tx, rx) = mpsc::channel(128);
        let cancellation_token = self.cancellation_token.clone();
        let _topic_name = self.topic.clone();

        tokio::spawn(async move {
            loop {
                // Verifica cancelamento antes de cada iteração
                if cancellation_token.is_cancelled() {
                    break;
                }

                // Usa select! para permitir cancelamento durante a espera de mensagens
                tokio::select! {
                    msg_result = message_stream.next() => {
                        match msg_result {
                            Some(msg) => {
                                // Mensagem já vem filtrada do EpidemicPubSub
                                if tx.send(msg).await.is_err() {
                                    // O receptor foi fechado, encerra a task.
                                    break;
                                }
                            }
                            None => {
                                // Stream fechado, encerra a task
                                break;
                            }
                        }
                    }
                    _ = cancellation_token.cancelled() => {
                        // Cancelamento solicitado, encerra a task
                        break;
                    }
                }
            }
        });

        Ok(rx)
    }

    // Retorna uma referência ao nome do tópico, o que é mais eficiente
    // do que clonar a String.
    #[instrument(level = "debug", skip(self))]
    pub fn topic(&self) -> &str {
        &self.topic
    }

    /// Cancela todas as operações ativas do tópico
    #[instrument(level = "debug", skip(self))]
    pub fn cancel(&self) {
        self.cancellation_token.cancel();
    }

    /// Verifica se o tópico foi cancelado
    #[instrument(level = "debug", skip(self))]
    pub fn is_cancelled(&self) -> bool {
        self.cancellation_token.is_cancelled()
    }

    /// Limpa a lista de membros do tópico
    #[instrument(level = "debug", skip(self))]
    pub async fn clear_members(&self) {
        let mut members_guard = self.members.write().await;
        members_guard.clear();
    }
}

#[async_trait::async_trait]
impl PubSubTopic for PsTopic {
    type Error = GuardianError;

    #[instrument(level = "debug", skip(self, message))]
    async fn publish(&self, message: Vec<u8>) -> crate::guardian::error::Result<()> {
        PsTopic::publish(self, &message).await
    }

    #[instrument(level = "debug", skip(self))]
    async fn peers(&self) -> crate::guardian::error::Result<Vec<NodeId>> {
        self.peers().await
    }

    #[instrument(level = "debug", skip(self))]
    async fn watch_peers(
        &self,
    ) -> crate::guardian::error::Result<Pin<Box<dyn Stream<Item = events::Event> + Send>>> {
        let (tx, rx) = mpsc::channel(32);

        // Clona dados necessários para a task
        let topic_clone = Arc::new(PsTopic {
            topic: self.topic.clone(),
            ps: self.ps.clone(),
            inner_topic: self.inner_topic.clone(),
            members: RwLock::new(self.members.read().await.clone()),
            cancellation_token: self.cancellation_token.clone(),
        });

        tokio::spawn(async move {
            loop {
                // Verifica cancelamento antes de cada iteração
                if topic_clone.cancellation_token.is_cancelled() {
                    break;
                }

                // Chama a função que calcula a diferença de peers
                let peers_diff_result = topic_clone.peers_diff().await;

                let (joining, leaving) = match peers_diff_result {
                    Ok((j, l)) => (j, l),
                    Err(e) => {
                        // Loga o erro e encerra a task.
                        error!("Erro ao verificar a diferença de peers: {:?}", e);
                        return;
                    }
                };

                for node_id in joining {
                    let event = p2p::new_event_peer_join(node_id, topic_clone.topic().to_string());
                    // Converte EventPubSub para o tipo esperado como events::Event
                    let event_any: events::Event = Arc::new(event);
                    if tx.send(event_any).await.is_err() {
                        // O receptor foi fechado, então a task não precisa mais rodar.
                        return;
                    }
                }

                for node_id in leaving {
                    let event = p2p::new_event_peer_leave(node_id, topic_clone.topic().to_string());
                    // Converte EventPubSub para o tipo esperado como events::Event
                    let event_any: events::Event = Arc::new(event);
                    if tx.send(event_any).await.is_err() {
                        return;
                    }
                }

                // Usa select! para permitir cancelamento durante o sleep
                tokio::select! {
                    _ = tokio::time::sleep(topic_clone.ps.poll_interval) => {},
                    _ = topic_clone.cancellation_token.cancelled() => {
                        break;
                    }
                }
            }
        });

        let stream = ReceiverStream::new(rx);
        Ok(Box::pin(stream))
    }

    #[instrument(level = "debug", skip(self))]
    async fn watch_messages(
        &self,
    ) -> crate::guardian::error::Result<Pin<Box<dyn Stream<Item = EventPubSubMessage> + Send>>>
    {
        let receiver = self.watch_messages().await?;
        let stream = ReceiverStream::new(receiver);
        Ok(Box::pin(stream))
    }

    #[instrument(level = "debug", skip(self))]
    fn topic(&self) -> &str {
        &self.topic
    }
}

impl CoreApiPubSub {
    /// Assina um tópico de pubsub P2P via iroh-gossip, retornando uma instância de `PubSubTopic`.
    /// Se o tópico já existe, retorna a instância existente.
    /// Este método deve ser chamado em contextos onde já se tem um Arc<CoreApiPubSub>
    #[instrument(level = "debug", skip(self))]
    pub async fn topic_subscribe_internal(
        self: &Arc<Self>,
        topic: &str,
    ) -> crate::guardian::error::Result<Arc<PsTopic>> {
        let mut topics_guard = self.topics.lock().await;

        // Se o tópico já estiver na nossa cache, retorna a instância existente.
        if let Some(t) = topics_guard.get(topic) {
            return Ok(t.clone());
        }

        // Subscreve ao tópico via EpidemicPubSub
        let inner_topic = self.epidemic_pubsub.topic_subscribe(topic).await?;

        // Cria uma nova instância do tópico com o tópico P2P
        let new_topic = Arc::new(PsTopic {
            topic: topic.to_string(),
            ps: Arc::clone(self),
            inner_topic,
            members: Default::default(),
            cancellation_token: self.cancellation_token.child_token(),
        });

        // Insere o novo tópico no nosso cache.
        topics_guard.insert(topic.to_string(), new_topic.clone());

        Ok(new_topic)
    }

    /// Cria uma nova instância de `CoreApiPubSub` usando EpidemicPubSub para comunicação P2P.
    /// Os parâmetros `span` e `tracer` podem ser opcionais.
    #[instrument(level = "debug", skip(epidemic_pubsub, span, tracer))]
    pub fn new(
        epidemic_pubsub: Arc<EpidemicPubSub>,
        id: NodeId,
        poll_interval: Duration,
        span: Option<Span>,
        tracer: Option<Arc<TracerWrapper>>,
    ) -> Arc<Self> {
        // Criar tracer padrão caso não seja fornecido
        let default_tracer = Arc::new(TracerWrapper::Noop(NoopTracer::new()));

        Arc::new(Self {
            topics: Mutex::new(HashMap::new()),
            epidemic_pubsub,
            id,
            poll_interval,
            span: span.unwrap_or_else(tracing::Span::current),
            tracer: tracer.unwrap_or(default_tracer),
            cancellation_token: CancellationToken::new(),
        })
    }

    /// Método para cancelar todas as operações do PubSub
    pub fn cancel(&self) {
        self.cancellation_token.cancel();
    }

    /// Verifica se o PubSub foi cancelado
    pub fn is_cancelled(&self) -> bool {
        self.cancellation_token.is_cancelled()
    }

    /// Remove um tópico específico do cache
    #[instrument(level = "debug", skip(self))]
    pub async fn remove_topic(&self, topic_name: &str) -> bool {
        let mut topics_guard = self.topics.lock().await;
        topics_guard.remove(topic_name).is_some()
    }

    /// Remove todos os tópicos cancelados do cache
    #[instrument(level = "debug", skip(self))]
    pub async fn cleanup_cancelled_topics(&self) -> usize {
        let mut topics_guard = self.topics.lock().await;
        let mut cancelled_topics = Vec::new();

        // Identifica tópicos cancelados
        for (name, topic) in topics_guard.iter() {
            if topic.is_cancelled() {
                cancelled_topics.push(name.clone());
            }
        }

        // Remove tópicos cancelados
        for topic_name in &cancelled_topics {
            topics_guard.remove(topic_name);
        }

        cancelled_topics.len()
    }

    /// Retorna estatísticas dos tópicos ativos
    #[instrument(level = "debug", skip(self))]
    pub async fn topic_stats(&self) -> (usize, usize) {
        let topics_guard = self.topics.lock().await;
        let total_topics = topics_guard.len();
        let mut active_topics = 0;

        for topic in topics_guard.values() {
            if !topic.is_cancelled() {
                active_topics += 1;
            }
        }

        (total_topics, active_topics)
    }
}