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
use crate::error::Result;
use crate::vec::VecExt;
use crate::{
    DeleteOptions, DeleteResponse, GetOptions, GetResponse, KvClient, PutOptions, PutResponse, Txn,
    TxnResponse,
};

pub struct KvClientPrefix {
    pfx: Vec<u8>,
    kv: KvClient,
}

impl KvClientPrefix {
    pub fn new(kv: KvClient, pfx: Vec<u8>) -> Self {
        Self { pfx, kv }
    }

    #[inline]
    fn prefixed_key(&self, key: impl Into<Vec<u8>>) -> Vec<u8> {
        let mut key = key.into();
        key.prefix_with(&self.pfx);
        key
    }

    pub async fn put(
        &mut self,
        key: impl Into<Vec<u8>>,
        value: impl Into<Vec<u8>>,
        options: Option<PutOptions>,
    ) -> Result<PutResponse> {
        let key = self.prefixed_key(key);
        let mut resp = self.kv.put(key, value, options).await?;
        resp.strip_prev_key_prefix(&self.pfx);
        Ok(resp)
    }

    pub async fn get(
        &mut self,
        key: impl Into<Vec<u8>>,
        mut options: Option<GetOptions>,
    ) -> Result<GetResponse> {
        let key = self.prefixed_key(key);
        options = options.map(|mut opts| {
            opts.key_range_end_mut().prefix_range_end_with(&self.pfx);
            opts
        });
        let mut resp = self.kv.get(key, options).await?;
        resp.strip_kvs_prefix(&self.pfx);
        Ok(resp)
    }

    pub async fn delete(
        &mut self,
        key: impl Into<Vec<u8>>,
        mut options: Option<DeleteOptions>,
    ) -> Result<DeleteResponse> {
        let key = self.prefixed_key(key);
        options = options.map(|mut opts| {
            opts.key_range_end_mut().prefix_range_end_with(&self.pfx);
            opts
        });
        let mut resp = self.kv.delete(key, options).await?;
        resp.strip_prev_kvs_prefix(&self.pfx);
        Ok(resp)
    }

    pub async fn txn(&mut self, mut txn: Txn) -> Result<TxnResponse> {
        txn.prefix_with(&self.pfx);
        let mut resp = self.kv.txn(txn).await?;
        resp.strip_key_prefix(&self.pfx);
        Ok(resp)
    }
}