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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Test utilities for trapping and injecting responses in external storage.

use std::fmt::Debug;
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use bytes::Bytes;
use mz_ore::bytes::SegmentedBytes;

use crate::location::{Blob, BlobMetadata, ExternalError};

/// Post-op closure for [Blob::delete].
pub type PostDeleteFn = Arc<
    dyn Fn(&str, Result<Option<usize>, ExternalError>) -> Result<Option<usize>, ExternalError>
        + Send
        + Sync,
>;

#[derive(Default)]
struct InterceptCore {
    post_delete: Option<PostDeleteFn>,
}

impl Debug for InterceptCore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let InterceptCore { post_delete } = self;
        f.debug_struct("InterceptCore")
            .field(
                "post_delete",
                &post_delete.as_ref().map(|x| format!("{:p}", x)),
            )
            .finish()
    }
}

/// A handle for controlling the behavior of an intercept delegate.
#[derive(Clone, Debug, Default)]
pub struct InterceptHandle {
    core: Arc<Mutex<InterceptCore>>,
}

impl InterceptHandle {
    /// Sets a new [PostDeleteFn].
    ///
    /// Returns the previous closure, if any.
    pub fn set_post_delete(&self, f: Option<PostDeleteFn>) -> Option<PostDeleteFn> {
        let mut core = self.core.lock().expect("lock should not be poisoned");
        std::mem::replace(&mut core.post_delete, f)
    }
}

/// An intercept delegate to [Blob].
///
/// TODO: Tune this pattern to be most useful and then extend it to consensus
/// and the rest of blob.
#[derive(Debug)]
pub struct InterceptBlob {
    handle: InterceptHandle,
    blob: Arc<dyn Blob + Send + Sync>,
}

impl InterceptBlob {
    /// Returns a new [InterceptBlob].
    pub fn new(blob: Arc<dyn Blob + Send + Sync>, handle: InterceptHandle) -> Self {
        InterceptBlob { handle, blob }
    }
}

#[async_trait]
impl Blob for InterceptBlob {
    async fn get(&self, key: &str) -> Result<Option<SegmentedBytes>, ExternalError> {
        self.blob.get(key).await
    }

    async fn list_keys_and_metadata(
        &self,
        key_prefix: &str,
        f: &mut (dyn FnMut(BlobMetadata) + Send + Sync),
    ) -> Result<(), ExternalError> {
        self.blob.list_keys_and_metadata(key_prefix, f).await
    }

    async fn set(&self, key: &str, value: Bytes) -> Result<(), ExternalError> {
        self.blob.set(key, value).await
    }

    async fn delete(&self, key: &str) -> Result<Option<usize>, ExternalError> {
        let ret = self.blob.delete(key).await;
        let post_delete = self
            .handle
            .core
            .lock()
            .expect("lock should not be poisoned")
            .post_delete
            .clone();
        match post_delete {
            Some(x) => x(key, ret),
            None => ret,
        }
    }

    async fn restore(&self, key: &str) -> Result<(), ExternalError> {
        self.blob.restore(key).await
    }
}