p2panda_store/topics/traits.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use std::collections::BTreeMap;
4use std::error::Error;
5
6/// Maps a topic to a user-defined data type being sent over the wire during sync.
7///
8/// It defines the type of data it is expecting to sync and how the scope for a particular session
9/// should be identified; users provide an implementation of the `TopicStore` trait in order to
10/// define how this mapping occurs.
11///
12/// Since `TopicStore` is generic we can use the same mapping across different sync
13/// implementations for the same data type when necessary.
14///
15/// For example a `TopicStore` map implementation could map a generic `T` to a set of logs.
16///
17/// ## Designing `TopicStore` for applications
18///
19/// Considering an example chat application which is based on append-only log data types, we
20/// probably want to organise messages from an author for a certain chat group into one log each.
21/// Like this, a chat group can be expressed as a collection of one to potentially many logs (one
22/// per member of the group):
23///
24/// ```text
25/// All authors: A, B and C
26/// All chat groups: 1 and 2
27///
28/// "Chat group 1 with members A and B"
29/// - Log A1
30/// - Log B1
31///
32/// "Chat group 2 with members A, B and C"
33/// - Log A2
34/// - Log B2
35/// - Log C2
36/// ```
37///
38/// If we implement `T` to express that we're interested in syncing over a specific chat group,
39/// for example "Chat Group 2" we would implement `TopicStore` to give us all append-only logs of
40/// all members inside this group, that is the entries inside logs `A2`, `B2` and `C2`.
41pub trait TopicStore<T, A, D> {
42 type Error: Error;
43
44 /// Associate an author and data id pair with a topic.
45 fn associate(
46 &self,
47 topic: &T,
48 author: &A,
49 data_id: &D,
50 ) -> impl Future<Output = Result<bool, Self::Error>>;
51
52 /// Remove an association with a topic.
53 fn remove(
54 &self,
55 topic: &T,
56 author: &A,
57 data_id: &D,
58 ) -> impl Future<Output = Result<bool, Self::Error>>;
59
60 /// Retrieve all associations for the provided topic.
61 fn resolve(&self, topic: &T) -> impl Future<Output = Result<BTreeMap<A, Vec<D>>, Self::Error>>;
62}