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
use crate::{compare_key_in, same_key_in, Key, KeyExt, TIMESTAMP_SIZE};
use bytes::{BufMut, BytesMut};
use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
use core::ops::{Deref, DerefMut};

/// A general mutable Key for key-value storage, the underlying is u8 slice.
#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct KeyMut {
    data: BytesMut,
}

impl Default for KeyMut {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for KeyMut {
    type Target = BytesMut;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl DerefMut for KeyMut {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

impl AsRef<[u8]> for KeyMut {
    fn as_ref(&self) -> &[u8] {
        self.data.as_ref()
    }
}

impl AsMut<[u8]> for KeyMut {
    fn as_mut(&mut self) -> &mut [u8] {
        self.data.as_mut()
    }
}

impl PartialEq<Self> for KeyMut {
    fn eq(&self, other: &Self) -> bool {
        same_key_in(self.data.as_ref(), other.data.as_ref())
    }
}

impl Eq for KeyMut {}

impl Hash for KeyMut {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.data.hash(state)
    }
}

impl PartialOrd<Self> for KeyMut {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for KeyMut {
    /// Checks the key without timestamp and checks the timestamp if keyNoTs
    /// is same.
    /// a<timestamp> would be sorted higher than aa<timestamp> if we use bytes.compare
    /// All keys should have timestamp.
    fn cmp(&self, other: &Self) -> Ordering {
        compare_key_in(self.data.as_ref(), other.data.as_ref())
    }
}

impl KeyMut {
    /// Creates a new `KeyMut` with default capacity.
    ///
    /// Resulting object has length 0 and unspecified capacity.
    /// This function does not allocate.
    pub fn new() -> Self {
        Self {
            data: BytesMut::new(),
        }
    }

    /// Creates a new `KeyMut` with the specified capacity.
    ///
    /// The returned `KeyMut` will be able to hold at least `capacity` bytes
    /// without reallocating.
    ///
    /// It is important to note that this function does not specify the length
    /// of the returned `KeyMut`, but only the capacity.
    ///
    pub fn with_capacity(cap: usize) -> Self {
        Self {
            data: BytesMut::with_capacity(cap),
        }
    }

    /// Generates a new key by appending timestamp to key.
    #[inline]
    pub fn with_timestamp(mut self, ts: u64) -> Self {
        self.data.put_u64(ts);
        self
    }

    /// Converts self into an immutable Key.
    /// The conversion is zero cost and is used to indicate that
    /// the slice referenced by the handle will no longer be mutated.
    /// Once the conversion is done, the handle can be cloned and shared across threads
    pub fn freeze(self) -> Key {
        Key::from(self.data.freeze())
    }
}

impl<'a> KeyExt for &'a KeyMut {
    fn as_bytes(&self) -> &[u8] {
        self.data.as_ref()
    }
}

impl<'a> KeyExt for &'a mut KeyMut {
    fn as_bytes(&self) -> &[u8] {
        self.data.as_ref()
    }
}

impl KeyExt for KeyMut {
    fn as_bytes(&self) -> &[u8] {
        self.data.as_ref()
    }
}

/// Extensions for `KeyMut`
pub trait KeyMutExt {
    /// Returns the mutable underlying slice of key (with timestamp data).
    fn as_bytes_mut(&mut self) -> &mut [u8];

    /// Returns raw mutable pointer of the underlying byte slice
    #[inline]
    fn as_mut_ptr(&mut self) -> *mut u8 {
        self.as_bytes_mut().as_mut_ptr()
    }

    /// Returns the mutable data slice store in ValueMut
    fn parse_key_mut(&mut self) -> &mut [u8];

    /// Set the timestamp for key
    fn set_timestamp(&mut self, ts: u64);
}

impl KeyMutExt for KeyMut {
    #[inline]
    fn as_bytes_mut(&mut self) -> &mut [u8] {
        self.data.as_mut()
    }

    #[inline]
    fn parse_key_mut(&mut self) -> &mut [u8] {
        self.data.as_mut()
    }

    #[inline]
    fn set_timestamp(&mut self, ts: u64) {
        let sz = self.len();
        match sz.checked_sub(TIMESTAMP_SIZE) {
            None => self.data.put_u64(ts),
            Some(sz) => self.data[sz..].copy_from_slice(ts.to_be_bytes().as_slice()),
        }
    }
}

impl<'a> KeyMutExt for &'a mut KeyMut {
    #[inline]
    fn as_bytes_mut(&mut self) -> &mut [u8] {
        self.data.as_mut()
    }

    #[inline]
    fn parse_key_mut(&mut self) -> &mut [u8] {
        self.data.as_mut()
    }

    #[inline]
    fn set_timestamp(&mut self, ts: u64) {
        let sz = self.len();
        match sz.checked_sub(TIMESTAMP_SIZE) {
            None => self.data.put_u64(ts),
            Some(sz) => self.data[sz..].copy_from_slice(ts.to_be_bytes().as_slice()),
        }
    }
}