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
// TODO: documentation

use std::{
    collections::{hash_map::Entry, HashMap, VecDeque},
    iter::FromIterator,
    sync::Arc,
};

use async_trait::async_trait;
use tokio::sync::RwLock;

use robespierre_models::{
    channel::{Channel, ChannelField, Message, PartialChannel, PartialMessage},
    id::{ChannelId, MemberId, MessageId, RoleId, ServerId, UserId},
    server::{
        Member, MemberField, PartialMember, PartialRole, PartialServer, Role, RoleField, Server,
        ServerField,
    },
    user::{PartialUser, User, UserField},
};

#[derive(Default)]
pub struct CacheConfig {
    /// number of messages to cache / channel, 0 for no caching
    pub messages: usize,
}

pub struct Cache {
    config: CacheConfig,

    users: RwLock<HashMap<UserId, User>>,
    servers: RwLock<HashMap<ServerId, Server>>,
    roles: RwLock<HashMap<RoleId, Role>>,
    members: RwLock<HashMap<MemberId, Member>>,
    channels: RwLock<HashMap<ChannelId, Channel>>,
    messages: RwLock<HashMap<ChannelId, HashMap<MessageId, Message>>>,
    message_queue: RwLock<HashMap<ChannelId, VecDeque<MessageId>>>,
}

impl Cache {
    pub fn new(config: CacheConfig) -> Arc<Self> {
        Arc::new(Self {
            config,

            users: RwLock::new(HashMap::new()),
            servers: RwLock::new(HashMap::new()),
            roles: RwLock::new(HashMap::new()),
            members: RwLock::new(HashMap::new()),
            channels: RwLock::new(HashMap::new()),
            messages: RwLock::new(HashMap::new()),
            message_queue: RwLock::new(HashMap::new()),
        })
    }
}

macro_rules! cache_field {
    ($id_ty:ty, $full_ty:ty, $cloner:ident, $get_data:ident, $field:ident) => {
        impl Cache {
            pub async fn $cloner(&self, id: $id_ty) -> Option<$full_ty> {
                self.$get_data(id, Clone::clone).await
            }

            pub async fn $get_data<F, T>(&self, id: $id_ty, f: F) -> Option<T>
            where
                F: FnOnce(&$full_ty) -> T,
            {
                self.$field.read().await.get(&id).map(f)
            }
        }
    };

    ($id_ty:ty, $full_ty:ty, $cloner:ident, $get_data:ident, $field:ident, $commit_function:ident, $key_field:ident) => {
        impl Cache {
            pub async fn $cloner(&self, id: $id_ty) -> Option<$full_ty> {
                self.$get_data(id, Clone::clone).await
            }

            pub async fn $get_data<F, T>(&self, id: $id_ty, f: F) -> Option<T>
            where
                F: FnOnce(&$full_ty) -> T,
            {
                self.$field.read().await.get(&id).map(f)
            }

            pub async fn $commit_function(&self, v: &$full_ty) {
                self.$field.write().await.insert(v.$key_field, v.clone());
            }
        }
    };
}

cache_field! {UserId, User, get_user, get_user_data, users, commit_user, id}

impl Cache {
    pub async fn patch_user(
        &self,
        user_id: UserId,
        patch: impl FnOnce() -> PartialUser,
        remove: Option<UserField>,
    ) {
        let mut lock = self.users.write().await;
        if let Some(user) = lock.get_mut(&user_id) {
            let patch = patch();

            patch.patch(user);
            if let Some(remove) = remove {
                remove.remove_patch(user);
            }
        }
    }
}

cache_field! {ServerId, Server, get_server, get_server_data, servers, commit_server, id}

impl Cache {
    pub async fn patch_server(
        &self,
        server_id: ServerId,
        patch: impl FnOnce() -> PartialServer,
        remove: Option<ServerField>,
    ) {
        let mut lock = self.servers.write().await;
        if let Some(server) = lock.get_mut(&server_id) {
            let patch = patch();

            patch.patch(server);
            if let Some(remove) = remove {
                remove.remove_patch(server);
            }
        }
    }
}

cache_field! {RoleId, Role, get_role, get_role_data, roles}

impl Cache {
    pub async fn commit_role(&self, role_id: RoleId, role: &Role) {
        let mut lock = self.roles.write().await;
        lock.insert(role_id, role.clone());
    }

    pub async fn patch_role(
        &self,
        role_id: RoleId,
        patch: impl FnOnce() -> PartialRole,
        remove: Option<RoleField>,
    ) {
        let mut lock = self.roles.write().await;
        if let Some(role) = lock.get_mut(&role_id) {
            let patch = patch();

            patch.patch(role);
            if let Some(remove) = remove {
                remove.remove_patch(role);
            }
        }
    }
}

cache_field! {MemberId, Member, get_member, get_member_data, members, commit_member, id}

impl Cache {
    pub async fn patch_member(
        &self,
        member_id: MemberId,
        patch: impl FnOnce() -> PartialMember,
        remove: Option<MemberField>,
    ) {
        let mut lock = self.members.write().await;
        if let Some(member) = lock.get_mut(&member_id) {
            let patch = patch();

            patch.patch(member);
            if let Some(remove) = remove {
                remove.remove_patch(member);
            }
        }
    }
}

cache_field! {ChannelId, Channel, get_channel, get_channel_data, channels}

impl Cache {
    pub async fn commit_channel(&self, channel: &Channel) {
        self.channels
            .write()
            .await
            .insert(channel.id(), channel.clone());
    }

    pub async fn patch_channel(
        &self,
        channel_id: ChannelId,
        patch: impl FnOnce() -> PartialChannel,
        remove: Option<ChannelField>,
    ) {
        let mut lock = self.channels.write().await;
        if let Some(channel) = lock.get_mut(&channel_id) {
            let patch = patch();

            patch.patch(channel);
            if let Some(remove) = remove {
                remove.remove_patch(channel);
            }
        }
    }
}

impl Cache {
    pub async fn get_message(&self, channel: ChannelId, message: MessageId) -> Option<Message> {
        self.get_message_data(channel, message, Clone::clone).await
    }

    pub async fn get_message_data<F, T>(
        &self,
        channel: ChannelId,
        message: MessageId,
        f: F,
    ) -> Option<T>
    where
        F: FnOnce(&Message) -> T,
    {
        self.messages
            .read()
            .await
            .get(&channel)?
            .get(&message)
            .map(f)
    }

    pub async fn commit_message(&self, message: &Message) {
        if self.config.messages == 0 {
            return;
        }

        let mut queue_lock = self.message_queue.write().await;
        let deque = queue_lock.entry(message.channel).or_insert(VecDeque::new());

        match self.messages.write().await.entry(message.channel) {
            Entry::Occupied(mut m) => {
                m.get_mut().insert(message.id, message.clone());

                deque.push_back(message.id);

                if deque.len() > self.config.messages {
                    if let Some(oldest) = deque.pop_front() {
                        m.get_mut().remove(&oldest);
                    }
                }
            }
            Entry::Vacant(v) => {
                deque.push_back(message.id);
                v.insert(HashMap::from_iter([(message.id, message.clone())]));
            }
        }
    }

    pub async fn patch_message(
        &self,
        channel_id: ChannelId,
        message_id: MessageId,
        patch: impl FnOnce() -> PartialMessage,
    ) {
        let mut lock = self.messages.write().await;
        if let Some(ch) = lock.get_mut(&channel_id) {
            if let Some(message) = ch.get_mut(&message_id) {
                let patch = patch();

                patch.patch(message);
            }
        }
    }
}

pub trait HasCache: Send + Sync {
    fn get_cache(&self) -> Option<&Cache>;
}

impl HasCache for Cache {
    fn get_cache(&self) -> Option<&Cache> {
        Some(self)
    }
}

impl HasCache for Arc<Cache> {
    fn get_cache(&self) -> Option<&Cache> {
        Some(self)
    }
}

#[async_trait]
pub trait CommitToCache: Send + Sync {
    async fn commit_to_cache<C: HasCache>(self, c: &C) -> Self
    where
        Self: Sized,
    {
        self.commit_to_cache_ref(c).await;

        self
    }

    async fn commit_to_cache_ref<C: HasCache>(&self, c: &C) {
        if let Some(c) = c.get_cache() {
            Self::__commit_to_cache(self, c).await;
        }
    }

    async fn __commit_to_cache(&self, cache: &Cache);
}

#[async_trait]
impl CommitToCache for User {
    async fn __commit_to_cache(&self, cache: &Cache) {
        cache.commit_user(self).await;
    }
}

#[async_trait]
impl CommitToCache for Channel {
    async fn __commit_to_cache(&self, cache: &Cache) {
        cache.commit_channel(self).await;
    }
}

#[async_trait]
impl CommitToCache for Server {
    async fn __commit_to_cache(&self, cache: &Cache) {
        cache.commit_server(self).await;
    }
}

#[async_trait]
impl CommitToCache for Member {
    async fn __commit_to_cache(&self, cache: &Cache) {
        cache.commit_member(self).await;
    }
}

#[async_trait]
impl CommitToCache for Message {
    async fn __commit_to_cache(&self, cache: &Cache) {
        cache.commit_message(self).await;
    }
}

#[async_trait]
impl<'a> CommitToCache for (RoleId, &'a Role) {
    async fn __commit_to_cache(&self, cache: &Cache) {
        cache.commit_role(self.0, self.1).await
    }
}