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};
#[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 {
    
    
    
    
    fn cmp(&self, other: &Self) -> Ordering {
        compare_key_in(self.data.as_ref(), other.data.as_ref())
    }
}
impl KeyMut {
    
    
    
    
    pub fn new() -> Self {
        Self {
            data: BytesMut::new(),
        }
    }
    
    
    
    
    
    
    
    
    pub fn with_capacity(cap: usize) -> Self {
        Self {
            data: BytesMut::with_capacity(cap),
        }
    }
    
    #[inline]
    pub fn with_timestamp(mut self, ts: u64) -> Self {
        self.data.put_u64(ts);
        self
    }
    
    
    
    
    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()
    }
}
pub trait KeyMutExt {
    
    fn as_bytes_mut(&mut self) -> &mut [u8];
    
    #[inline]
    fn as_mut_ptr(&mut self) -> *mut u8 {
        self.as_bytes_mut().as_mut_ptr()
    }
    
    fn parse_key_mut(&mut self) -> &mut [u8];
    
    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()),
        }
    }
}