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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
use std::error::Error;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use datacake_crdt::{HLCTimestamp, Key};
use datacake_node::NodeId;
use datacake_rpc::Channel;

use crate::core::{Document, DocumentMetadata};

/// A utility for tracking the progress a task has made.
pub struct ProgressWatcher {
    inner: ProgressTracker,
    timeout: Duration,
    last_tick: Instant,
    last_observed_counter: u64,
}

impl ProgressWatcher {
    pub fn new(inner: ProgressTracker, timeout: Duration) -> Self {
        Self {
            inner,
            timeout,
            last_tick: Instant::now(),
            last_observed_counter: 0,
        }
    }

    /// Checks if the task has expired or made progress.
    pub fn has_expired(&mut self) -> bool {
        if self.is_done() {
            return false;
        }

        let counter = self.inner.progress_counter.load(Ordering::Relaxed);

        if counter > self.last_observed_counter {
            self.last_tick = Instant::now();
            self.last_observed_counter = counter;
            return false;
        }

        self.last_tick.elapsed() > self.timeout
    }

    /// Returns if the task is complete or not.
    pub fn is_done(&self) -> bool {
        self.inner.done.load(Ordering::Relaxed)
    }
}

#[derive(Default, Debug, Clone)]
/// A simple atomic counter to indicate to supervisors that the given
/// operation is making progress.
///
/// This can be used in order to prevent supervisors timing out tasks
/// because they have not been completed within the target time frame.
pub struct ProgressTracker {
    pub(crate) progress_counter: Arc<AtomicU64>,
    pub(crate) done: Arc<AtomicBool>,
}

impl ProgressTracker {
    /// Adds a marker to the progress tracker.
    ///
    /// This is so any supervisors don't accidentally cancel or abort a task if it's
    /// taking longer than it expected.
    pub fn register_progress(&self) {
        self.progress_counter.fetch_add(1, Ordering::Relaxed);
    }

    /// Marks the task as complete.
    pub fn set_done(&self) {
        self.done.store(true, Ordering::Relaxed);
    }
}

#[derive(Clone)]
/// Additional information related to the operation which can be useful.
///
/// This can be very useful if you wish to extend Datacake's storage system
/// in order to support objects which don't fit in memory etc...
pub struct PutContext {
    // Info relating to the task itself.
    pub(crate) progress: ProgressTracker,

    // Info relating to the remote node.
    pub(crate) remote_node_id: NodeId,
    pub(crate) remote_addr: SocketAddr,
    pub(crate) remote_rpc_channel: Channel,
}

impl PutContext {
    #[inline]
    /// Adds a marker to the progress tracker.
    ///
    /// This is so any supervisors don't accidentally cancel or abort a task if it's
    /// taking longer than it expected.
    pub fn register_progress(&self) {
        self.progress.register_progress()
    }

    #[inline]
    /// The unique ID of the remote node.
    pub fn remote_node_id(&self) -> NodeId {
        self.remote_node_id
    }

    #[inline]
    /// The socket address of the remote node.
    pub fn remote_addr(&self) -> SocketAddr {
        self.remote_addr
    }

    #[inline]
    /// The existing connection channel which can be used
    /// to communicate with services ran by the Datacake server.
    ///
    /// Additional services can be registered to the server ran by Datacake
    /// using the `ServiceRegistry` trait.
    pub fn remote_channel(&self) -> &Channel {
        &self.remote_rpc_channel
    }
}

#[derive(Debug, thiserror::Error)]
#[error("The operation was not completely successful due to error {inner}")]
/// An error which occurred while mutating the state not allowing the operation
/// to proceed any further but also having some part of the operation complete.
pub struct BulkMutationError<E>
where
    E: Error + Send + 'static,
{
    pub(crate) inner: E,
    pub(crate) successful_doc_ids: Vec<Key>,
}

impl<E> BulkMutationError<E>
where
    E: Error + Send + 'static,
{
    /// Creates a new mutation error from the provided inner error.
    ///
    /// This essentially means that what ever change that was going to happen
    /// was atomic and has therefore been revered.
    ///
    /// WARNING:
    /// *You should under no circumstances return an empty mutation error if **any**
    /// part of the state has been mutated and will not be reversed. Doing so will lead
    /// to state divergence within the cluster*
    pub fn empty_with_error(error: E) -> Self {
        Self::new(error, Vec::new())
    }

    /// Creates a new mutation error from the provided inner error.
    ///
    /// This essentially means that although we ran into an error, we were able to
    /// complete some part of the operation on some documents.
    ///
    /// WARNING:
    /// *You should under no circumstances return an empty mutation error if **any**
    /// part of the state has been mutated and will not be reversed. Doing so will lead
    /// to state divergence within the cluster*
    pub fn new(error: E, successful_doc_ids: Vec<Key>) -> Self {
        Self {
            inner: error,
            successful_doc_ids,
        }
    }

    #[inline]
    /// The cause of the error.
    pub fn cause(&self) -> &E {
        &self.inner
    }

    #[inline]
    /// Consumes the error returning the inner error.
    pub fn into_inner(self) -> E {
        self.inner
    }

    #[inline]
    /// The document ids which the operation was successful on.
    pub fn successful_doc_ids(&self) -> &[Key] {
        &self.successful_doc_ids
    }
}

// TODO: Add default methods with more complicated handlers in order to allow room for lnx stuff.
#[async_trait]
/// The generic storage trait which encapsulates all the required persistence logic.
///
/// A test suite is available for ensuring correct behavour of stores.
pub trait Storage: Send + Sync + 'static {
    type Error: Error + Send + Sync + 'static;
    type DocsIter: Iterator<Item = Document>;
    type MetadataIter: Iterator<Item = (Key, HLCTimestamp, bool)>;

    /// Retrieves all keyspace currently persisted.
    async fn get_keyspace_list(&self) -> Result<Vec<String>, Self::Error>;

    /// Retrieves an iterator producing all values contained within the store.
    ///
    /// This should contain the document ID, when it was last updated and if it's a tombstone or not.
    async fn iter_metadata(
        &self,
        keyspace: &str,
    ) -> Result<Self::MetadataIter, Self::Error>;

    /// Remove a set of keys which are marked as tombstones store.
    ///
    /// If the given `keyspace` does not exist, it should be created. A new keyspace name should
    /// not result in an error being returned by the storage trait.
    async fn remove_tombstones(
        &self,
        keyspace: &str,
        keys: impl Iterator<Item = Key> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>>;

    /// Inserts or updates a document in the persistent store.
    ///
    /// This is the base call for any `put` operation, and is passed the additional
    /// [PutContext] parameter which can provided additional information.
    ///
    /// In the case the context is `None`, this indicates that the operation originates
    /// from the local node itself. If context is `Some(ctx)` then it has originated from
    /// a remote node.
    ///
    /// If the given `keyspace` does not exist, it should be created. A new keyspace name should
    /// not result in an error being returned by the storage trait.
    ///
    /// NOTE:
    ///     It is the implementors responsibility to ensure that this operation is atomic and durable.
    ///     Partially setting the document metadata and failing to also set the data can lead to
    ///     split sate and the system will fail to converge unless a new operation comes in to modify
    ///     the document again.
    async fn put_with_ctx(
        &self,
        keyspace: &str,
        document: Document,
        _ctx: Option<&PutContext>,
    ) -> Result<(), Self::Error> {
        self.put(keyspace, document).await
    }

    /// Inserts or updates a document in the persistent store.
    ///
    /// If the given `keyspace` does not exist, it should be created. A new keyspace name should
    /// not result in an error being returned by the storage trait.
    ///
    /// NOTE:
    ///     It is the implementors responsibility to ensure that this operation is atomic and durable.
    ///     Partially setting the document metadata and failing to also set the data can lead to
    ///     split sate and the system will fail to converge unless a new operation comes in to modify
    ///     the document again.
    async fn put(&self, keyspace: &str, document: Document) -> Result<(), Self::Error>;

    /// Inserts or updates a set of documents in the persistent store.
    ///
    /// This is the base call for any `multi_put` operation, and is passed the additional
    /// [PutContext] parameter which can provided additional information.
    ///
    /// In the case the context is `None`, this indicates that the operation originates
    /// from the local node itself. If context is `Some(ctx)` then it has originated from
    /// a remote node.
    ///
    /// If the given `keyspace` does not exist, it should be created. A new keyspace name should
    /// not result in an error being returned by the storage trait.
    async fn multi_put_with_ctx(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = Document> + Send,
        _ctx: Option<&PutContext>,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        self.multi_put(keyspace, documents).await
    }

    /// Inserts or updates a set of documents in the persistent store.
    ///
    /// If the given `keyspace` does not exist, it should be created. A new keyspace name should
    /// not result in an error being returned by the storage trait.
    async fn multi_put(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = Document> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>>;

    /// Marks a document in the store as a tombstone.
    ///
    /// If the document does not exist this should be a no-op.
    ///
    /// If the given `keyspace` does not exist, it should be created. A new keyspace name should
    /// not result in an error being returned by the storage trait.
    ///
    /// NOTE:
    ///     This operation is permitted to delete the actual value of the document, but there
    ///     must be a marker indicating that the given document has been marked as deleted at
    ///     the provided timestamp.
    async fn mark_as_tombstone(
        &self,
        keyspace: &str,
        doc_id: Key,
        timestamp: HLCTimestamp,
    ) -> Result<(), Self::Error>;

    /// Marks a set of documents in the store as a tombstone.
    ///
    /// If the document does not exist this should be a no-op.
    ///
    /// If the given `keyspace` does not exist, it should be created. A new keyspace name should
    /// not result in an error being returned by the storage trait.
    ///
    /// NOTE:
    ///     This operation is permitted to delete the actual value of the document, but there
    ///     must be a marker indicating that the given document has been marked as deleted at
    ///     the provided timestamp.
    async fn mark_many_as_tombstone(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = DocumentMetadata> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>>;

    /// Retrieves a single document belonging to a given keyspace from the store.
    async fn get(
        &self,
        keyspace: &str,
        doc_id: Key,
    ) -> Result<Option<Document>, Self::Error>;

    /// Retrieves a set of documents belonging to a given keyspace from the store.
    ///
    /// No error should be returned if a document id cannot be found, instead it should
    /// just be ignored.
    async fn multi_get(
        &self,
        keyspace: &str,
        doc_ids: impl Iterator<Item = Key> + Send,
    ) -> Result<Self::DocsIter, Self::Error>;
}

#[cfg(any(test, feature = "test-utils"))]
pub mod test_suite {
    use std::any::type_name;
    use std::collections::HashSet;
    use std::hash::Hash;

    use datacake_crdt::{HLCTimestamp, Key};

    use crate::core::Document;
    use crate::storage::Storage;
    use crate::test_utils::InstrumentedStorage;
    use crate::DocumentMetadata;

    #[tokio::test]
    async fn test_suite_semantics() {
        use crate::test_utils::MemStore;
        let _ = tracing_subscriber::fmt::try_init();
        run_test_suite(MemStore::default()).await
    }

    pub async fn run_test_suite<S: Storage>(storage: S) {
        let mut clock = HLCTimestamp::now(0, 0);
        info!("Starting test suite for storage: {}", type_name::<S>());

        let storage = InstrumentedStorage(storage);

        test_keyspace_semantics(&storage, &mut clock).await;
        info!("test_keyspace_semantics OK");

        test_basic_persistence_test(&storage, &mut clock).await;
        info!("test_basic_persistence_test OK");

        test_basic_metadata_test(&storage, &mut clock).await;
        info!("test_basic_metadata_test OK");
    }

    #[instrument(name = "test_keyspace_semantics", skip(storage))]
    async fn test_keyspace_semantics<S: Storage + Sync>(
        storage: &S,
        clock: &mut HLCTimestamp,
    ) {
        info!("Starting test");

        static KEYSPACE: &str = "first-keyspace";

        let res = storage.iter_metadata(KEYSPACE).await;
        if let Err(e) = res {
            panic!(
                "Iterating through keyspace metadata should return OK. Got {:?}",
                e
            );
        }

        let metadata = storage
            .iter_metadata(KEYSPACE)
            .await
            .expect("Produce metadata iterator.")
            .collect::<HashSet<(Key, HLCTimestamp, bool)>>();
        assert_eq!(metadata, to_hashset([]), "New keyspace should be empty.");

        let doc = Document::new(1, clock.send().unwrap(), Vec::new());
        let res = storage.put_with_ctx(KEYSPACE, doc, None).await;
        assert!(
            res.is_ok(),
            "Setting metadata on a new keyspace should not error. Got {:?}",
            res
        );

        let doc = Document::new(2, clock.send().unwrap(), Vec::new());
        let res = storage.put_with_ctx(KEYSPACE, doc, None).await;
        assert!(
            res.is_ok(),
            "Setting metadata on a existing keyspace should not error. Got {:?}",
            res
        );

        let metadata = storage
            .iter_metadata(KEYSPACE)
            .await
            .expect("Produce metadata iterator.")
            .collect::<HashSet<(Key, HLCTimestamp, bool)>>();
        assert_eq!(
            metadata.len(),
            2,
            "First keyspace should contain 2 entries."
        );

        let keyspace_list = storage
            .get_keyspace_list()
            .await
            .expect("Get keyspace list");
        assert_eq!(
            keyspace_list,
            vec![KEYSPACE.to_string()],
            "Returned keyspace list (left) should match value provided (right)."
        );

        let metadata = storage
            .iter_metadata("second-keyspace")
            .await
            .expect("Produce metadata iterator.")
            .collect::<HashSet<(Key, HLCTimestamp, bool)>>();
        assert_eq!(metadata, to_hashset([]), "Second keyspace should be empty.");
    }

    #[instrument(name = "test_basic_metadata_test", skip(storage))]
    async fn test_basic_metadata_test<S: Storage>(
        storage: &S,
        clock: &mut HLCTimestamp,
    ) {
        info!("Starting test");

        static KEYSPACE: &str = "metadata-test-keyspace";

        let mut doc_1 = Document::new(1, clock.send().unwrap(), Vec::new());
        let mut doc_2 = Document::new(2, clock.send().unwrap(), Vec::new());
        let mut doc_3 = Document::new(3, clock.send().unwrap(), Vec::new());
        storage
            .multi_put(
                KEYSPACE,
                [doc_1.clone(), doc_2.clone(), doc_3.clone()].into_iter(),
            )
            .await
            .expect("Put documents");

        doc_3.metadata.last_updated = clock.send().unwrap();
        storage
            .mark_as_tombstone(KEYSPACE, doc_3.id(), doc_3.last_updated())
            .await
            .expect("Mark document as tombstone.");

        let metadata = storage
            .iter_metadata(KEYSPACE)
            .await
            .expect("Produce metadata iterator.")
            .collect::<HashSet<(Key, HLCTimestamp, bool)>>();
        assert_eq!(
            metadata,
            to_hashset([
                (doc_1.id(), doc_1.last_updated(), false),
                (doc_2.id(), doc_2.last_updated(), false),
                (doc_3.id(), doc_3.last_updated(), true),
            ]),
            "Persisted metadata entries should match expected values."
        );

        doc_1.metadata.last_updated = clock.send().unwrap();
        doc_2.metadata.last_updated = clock.send().unwrap();
        storage
            .mark_many_as_tombstone(
                KEYSPACE,
                [doc_1.metadata, doc_2.metadata].into_iter(),
            )
            .await
            .expect("Mark documents as tombstones.");
        let metadata = storage
            .iter_metadata(KEYSPACE)
            .await
            .expect("Produce metadata iterator.")
            .collect::<HashSet<(Key, HLCTimestamp, bool)>>();
        assert_eq!(
            metadata,
            to_hashset([
                (doc_1.id(), doc_1.last_updated(), true),
                (doc_2.id(), doc_2.last_updated(), true),
                (doc_3.id(), doc_3.last_updated(), true),
            ]),
            "Persisted metadata entries should match expected values."
        );

        storage
            .remove_tombstones(KEYSPACE, [1, 2].into_iter())
            .await
            .expect("Remove tombstone entries.");
        let metadata = storage
            .iter_metadata(KEYSPACE)
            .await
            .expect("Produce metadata iterator.")
            .collect::<HashSet<(Key, HLCTimestamp, bool)>>();
        assert_eq!(
            metadata,
            to_hashset([(doc_3.id(), doc_3.last_updated(), true)]),
            "Persisted metadata entries should match expected values after removal."
        );

        doc_1.metadata.last_updated = clock.send().unwrap();
        doc_2.metadata.last_updated = clock.send().unwrap();
        doc_3.metadata.last_updated = clock.send().unwrap();
        storage
            .multi_put(
                KEYSPACE,
                [doc_1.clone(), doc_2.clone(), doc_3.clone()].into_iter(),
            )
            .await
            .expect("Set metadata entry 3.");
        let metadata = storage
            .iter_metadata(KEYSPACE)
            .await
            .expect("Produce metadata iterator.")
            .collect::<HashSet<(Key, HLCTimestamp, bool)>>();
        assert_eq!(
            metadata,
            to_hashset([
                (doc_1.id(), doc_1.last_updated(), false),
                (doc_2.id(), doc_2.last_updated(), false),
                (doc_3.id(), doc_3.last_updated(), false),
            ]),
            "Persisted metadata entries should match expected values after update."
        );

        doc_1.metadata.last_updated = clock.send().unwrap();
        doc_2.metadata.last_updated = clock.send().unwrap();
        doc_3.metadata.last_updated = clock.send().unwrap();
        storage
            .mark_many_as_tombstone(
                KEYSPACE,
                [doc_1.metadata, doc_2.metadata, doc_3.metadata].into_iter(),
            )
            .await
            .expect("Mark documents as tombstones.");
        let res = storage
            .remove_tombstones(KEYSPACE, [1, 2, 3].into_iter())
            .await;
        assert!(
            res.is_ok(),
            "Expected successful removal of given metadata keys. Got: {:?}",
            res
        );

        let metadata = storage
            .iter_metadata(KEYSPACE)
            .await
            .expect("Produce metadata iterator.")
            .count();
        assert_eq!(
            metadata, 0,
            "Persisted metadata entries should be empty after tombstone purge."
        );

        doc_1.metadata.last_updated = clock.send().unwrap();
        doc_2.metadata.last_updated = clock.send().unwrap();
        doc_3.metadata.last_updated = clock.send().unwrap();
        let doc_4_ts = clock.send().unwrap();
        storage
            .mark_many_as_tombstone(
                KEYSPACE,
                [
                    doc_1.metadata,
                    doc_2.metadata,
                    doc_3.metadata,
                    DocumentMetadata::new(4, doc_4_ts),
                ]
                .into_iter(),
            )
            .await
            .expect("Mark documents as tombstones.");
        let metadata = storage
            .iter_metadata(KEYSPACE)
            .await
            .expect("Produce metadata iterator.")
            .collect::<HashSet<(Key, HLCTimestamp, bool)>>();
        assert_eq!(
            metadata,
            to_hashset([
                (doc_1.id(), doc_1.last_updated(), true),
                (doc_2.id(), doc_2.last_updated(), true),
                (doc_3.id(), doc_3.last_updated(), true),
                (4, doc_4_ts, true),
            ]),
            "Persisted tombstones should be tracked."
        );
    }

    #[instrument(name = "test_basic_persistence_test", skip(storage))]
    async fn test_basic_persistence_test<S: Storage + Sync>(
        storage: &S,
        clock: &mut HLCTimestamp,
    ) {
        info!("Starting test");

        static KEYSPACE: &str = "persistence-test-keyspace";

        let res = storage.get(KEYSPACE, 1).await;
        assert!(
            res.is_ok(),
            "Expected successful get request. Got: {:?}",
            res
        );
        assert!(
            res.unwrap().is_none(),
            "Expected no document to be returned."
        );

        #[allow(clippy::needless_collect)]
        let res = storage
            .multi_get(KEYSPACE, [1, 2, 3].into_iter())
            .await
            .expect("Expected successful get request.")
            .collect::<Vec<_>>();
        assert!(res.is_empty(), "Expected no document to be returned.");

        let mut doc_1 =
            Document::new(1, clock.send().unwrap(), b"Hello, world!".to_vec());
        let mut doc_2 = Document::new(2, clock.send().unwrap(), Vec::new());
        let mut doc_3 = Document::new(
            3,
            clock.send().unwrap(),
            b"Hello, from document 3!".to_vec(),
        );
        let doc_3_updated = Document::new(
            3,
            clock.send().unwrap(),
            b"Hello, from document 3 With an update!".to_vec(),
        );

        storage
            .put_with_ctx(KEYSPACE, doc_1.clone(), None)
            .await
            .expect("Put document in persistent store.");
        let res = storage.get(KEYSPACE, 1).await;
        assert!(
            res.is_ok(),
            "Expected successful get request. Got: {:?}",
            res
        );
        let doc = res
            .unwrap()
            .expect("Expected document to be returned after inserting doc.");
        assert_eq!(doc, doc_1, "Returned document should match.");

        storage
            .multi_put(KEYSPACE, [doc_3.clone(), doc_2.clone()].into_iter())
            .await
            .expect("Put document in persistent store.");
        let res = storage
            .multi_get(KEYSPACE, [1, 2, 3].into_iter())
            .await
            .expect("Expected successful get request.")
            .collect::<HashSet<_>>();
        assert_eq!(
            res,
            to_hashset([doc_1.clone(), doc_2.clone(), doc_3.clone()]),
            "Documents returned should match provided."
        );

        storage
            .put_with_ctx(KEYSPACE, doc_3_updated.clone(), None)
            .await
            .expect("Put updated document in persistent store.");
        let res = storage
            .get(KEYSPACE, 3)
            .await
            .expect("Get updated document.");
        let doc = res.expect("Expected document to be returned after updating doc.");
        assert_eq!(doc, doc_3_updated, "Returned document should match.");

        doc_2.metadata.last_updated = clock.send().unwrap();
        storage
            .mark_as_tombstone(KEYSPACE, doc_2.id(), doc_2.last_updated())
            .await
            .expect("Mark document as tombstone.");
        let res = storage.get(KEYSPACE, 2).await;
        assert!(
            res.is_ok(),
            "Expected successful get request. Got: {:?}",
            res
        );
        assert!(
            res.unwrap().is_none(),
            "Expected no document to be returned."
        );

        doc_1.metadata.last_updated = clock.send().unwrap();
        doc_2.metadata.last_updated = clock.send().unwrap();
        storage
            .mark_many_as_tombstone(
                KEYSPACE,
                [
                    doc_1.metadata,
                    doc_2.metadata,
                    DocumentMetadata::new(4, clock.send().unwrap()),
                ]
                .into_iter(),
            )
            .await
            .expect("Merk documents as tombstones");
        let res = storage
            .multi_get(KEYSPACE, [1, 2, 3].into_iter())
            .await
            .expect("Expected successful get request.")
            .collect::<HashSet<_>>();
        assert_eq!(
            res,
            to_hashset([doc_3_updated]),
            "Expected returned documents to match.",
        );

        doc_3.metadata.last_updated = clock.send().unwrap();
        storage
            .mark_as_tombstone(KEYSPACE, doc_3.id(), doc_3.last_updated())
            .await
            .expect("Delete documents from store.");
        #[allow(clippy::needless_collect)]
        let res = storage
            .multi_get(KEYSPACE, [1, 2, 3].into_iter())
            .await
            .expect("Expected successful get request.")
            .collect::<Vec<_>>();
        assert!(res.is_empty(), "Expected no documents to be returned.");
    }

    fn to_hashset<T: Hash + Eq>(iter: impl IntoIterator<Item = T>) -> HashSet<T> {
        iter.into_iter().collect()
    }
}