File size: 8,763 Bytes
1e92f2d |
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 |
use std::{
borrow::{Borrow, Cow},
marker::PhantomData,
ops::Deref,
};
use anyhow::Result;
use smallvec::SmallVec;
use crate::database::key_value_database::KeySpace;
pub trait BaseWriteBatch<'a> {
type ValueBuffer<'l>: std::borrow::Borrow<[u8]>
where
Self: 'l,
'a: 'l;
fn get<'l>(&'l self, key_space: KeySpace, key: &[u8]) -> Result<Option<Self::ValueBuffer<'l>>>
where
'a: 'l;
fn commit(self) -> Result<()>;
}
pub enum WriteBuffer<'a> {
Borrowed(&'a [u8]),
Vec(Vec<u8>),
SmallVec(smallvec::SmallVec<[u8; 16]>),
}
impl WriteBuffer<'_> {
pub fn into_static(self) -> WriteBuffer<'static> {
match self {
WriteBuffer::Borrowed(b) => WriteBuffer::SmallVec(SmallVec::from_slice(b)),
WriteBuffer::Vec(v) => WriteBuffer::Vec(v),
WriteBuffer::SmallVec(sv) => WriteBuffer::Vec(sv.into_vec()),
}
}
}
impl Deref for WriteBuffer<'_> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
match self {
WriteBuffer::Borrowed(b) => b,
WriteBuffer::Vec(v) => v,
WriteBuffer::SmallVec(sv) => sv,
}
}
}
impl<'l> From<Cow<'l, [u8]>> for WriteBuffer<'l> {
fn from(c: Cow<'l, [u8]>) -> Self {
match c {
Cow::Borrowed(b) => WriteBuffer::Borrowed(b),
Cow::Owned(o) => WriteBuffer::Vec(o),
}
}
}
pub trait SerialWriteBatch<'a>: BaseWriteBatch<'a> {
fn put(
&mut self,
key_space: KeySpace,
key: WriteBuffer<'_>,
value: WriteBuffer<'_>,
) -> Result<()>;
fn delete(&mut self, key_space: KeySpace, key: WriteBuffer<'_>) -> Result<()>;
fn flush(&mut self, key_space: KeySpace) -> Result<()>;
}
pub trait ConcurrentWriteBatch<'a>: BaseWriteBatch<'a> + Sync + Send {
fn put(&self, key_space: KeySpace, key: WriteBuffer<'_>, value: WriteBuffer<'_>) -> Result<()>;
fn delete(&self, key_space: KeySpace, key: WriteBuffer<'_>) -> Result<()>;
/// Flushes a key space of the write batch, reducing the amount of buffered memory used.
/// Does not commit any data persistently.
///
/// Safety: Caller must ensure that no concurrent put or delete operation is happening on the
/// flushed key space.
unsafe fn flush(&self, key_space: KeySpace) -> Result<()>;
}
pub enum WriteBatch<'a, S, C>
where
S: SerialWriteBatch<'a>,
C: ConcurrentWriteBatch<'a>,
{
Serial(S),
Concurrent(C, PhantomData<&'a ()>),
}
pub enum WriteBatchValueBuffer<S: Borrow<[u8]>, C: Borrow<[u8]>> {
Serial(S),
Concurrent(C),
}
impl<S: Borrow<[u8]>, C: Borrow<[u8]>> Borrow<[u8]> for WriteBatchValueBuffer<S, C> {
fn borrow(&self) -> &[u8] {
match self {
WriteBatchValueBuffer::Serial(s) => s.borrow(),
WriteBatchValueBuffer::Concurrent(c) => c.borrow(),
}
}
}
impl<'a, S, C> WriteBatch<'a, S, C>
where
S: SerialWriteBatch<'a>,
C: ConcurrentWriteBatch<'a>,
{
pub fn serial(s: S) -> Self {
WriteBatch::Serial(s)
}
pub fn concurrent(c: C) -> Self {
WriteBatch::Concurrent(c, PhantomData)
}
}
impl<'a, S, C> BaseWriteBatch<'a> for WriteBatch<'a, S, C>
where
S: SerialWriteBatch<'a>,
C: ConcurrentWriteBatch<'a>,
{
type ValueBuffer<'l>
= WriteBatchValueBuffer<S::ValueBuffer<'l>, C::ValueBuffer<'l>>
where
Self: 'l,
'a: 'l;
fn get<'l>(&'l self, key_space: KeySpace, key: &[u8]) -> Result<Option<Self::ValueBuffer<'l>>>
where
'a: 'l,
{
Ok(match self {
WriteBatch::Serial(s) => s.get(key_space, key)?.map(WriteBatchValueBuffer::Serial),
WriteBatch::Concurrent(c, _) => c
.get(key_space, key)?
.map(WriteBatchValueBuffer::Concurrent),
})
}
fn commit(self) -> Result<()> {
match self {
WriteBatch::Serial(s) => s.commit(),
WriteBatch::Concurrent(c, _) => c.commit(),
}
}
}
impl<'a, S, C> SerialWriteBatch<'a> for WriteBatch<'a, S, C>
where
S: SerialWriteBatch<'a>,
C: ConcurrentWriteBatch<'a>,
{
fn put(
&mut self,
key_space: KeySpace,
key: WriteBuffer<'_>,
value: WriteBuffer<'_>,
) -> Result<()> {
match self {
WriteBatch::Serial(s) => s.put(key_space, key, value),
WriteBatch::Concurrent(c, _) => c.put(key_space, key, value),
}
}
fn delete(&mut self, key_space: KeySpace, key: WriteBuffer<'_>) -> Result<()> {
match self {
WriteBatch::Serial(s) => s.delete(key_space, key),
WriteBatch::Concurrent(c, _) => c.delete(key_space, key),
}
}
fn flush(&mut self, key_space: KeySpace) -> Result<()> {
match self {
WriteBatch::Serial(s) => s.flush(key_space),
WriteBatch::Concurrent(c, _) => {
// Safety: the &mut self ensures that no concurrent operation is happening
unsafe { c.flush(key_space) }
}
}
}
}
pub enum WriteBatchRef<'r, 'a, S, C>
where
S: SerialWriteBatch<'a>,
C: ConcurrentWriteBatch<'a>,
{
Serial(&'r mut S),
Concurrent(&'r C, PhantomData<&'a ()>),
}
impl<'r, 'a, S, C> WriteBatchRef<'r, 'a, S, C>
where
S: SerialWriteBatch<'a>,
C: ConcurrentWriteBatch<'a>,
{
pub fn serial(s: &'r mut S) -> Self {
WriteBatchRef::Serial(s)
}
pub fn concurrent(c: &'r C) -> Self {
WriteBatchRef::Concurrent(c, PhantomData)
}
}
impl<'a, S, C> BaseWriteBatch<'a> for WriteBatchRef<'_, 'a, S, C>
where
S: SerialWriteBatch<'a>,
C: ConcurrentWriteBatch<'a>,
{
type ValueBuffer<'l>
= WriteBatchValueBuffer<S::ValueBuffer<'l>, C::ValueBuffer<'l>>
where
Self: 'l,
'a: 'l;
fn get<'l>(&'l self, key_space: KeySpace, key: &[u8]) -> Result<Option<Self::ValueBuffer<'l>>>
where
'a: 'l,
{
Ok(match self {
WriteBatchRef::Serial(s) => s.get(key_space, key)?.map(WriteBatchValueBuffer::Serial),
WriteBatchRef::Concurrent(c, _) => c
.get(key_space, key)?
.map(WriteBatchValueBuffer::Concurrent),
})
}
fn commit(self) -> Result<()> {
// TODO change the traits to make this a type level constraint
panic!("WriteBatchRef::commit is not usable");
}
}
impl<'a, S, C> SerialWriteBatch<'a> for WriteBatchRef<'_, 'a, S, C>
where
S: SerialWriteBatch<'a>,
C: ConcurrentWriteBatch<'a>,
{
fn put(
&mut self,
key_space: KeySpace,
key: WriteBuffer<'_>,
value: WriteBuffer<'_>,
) -> Result<()> {
match self {
WriteBatchRef::Serial(s) => s.put(key_space, key, value),
WriteBatchRef::Concurrent(c, _) => c.put(key_space, key, value),
}
}
fn delete(&mut self, key_space: KeySpace, key: WriteBuffer<'_>) -> Result<()> {
match self {
WriteBatchRef::Serial(s) => s.delete(key_space, key),
WriteBatchRef::Concurrent(c, _) => c.delete(key_space, key),
}
}
fn flush(&mut self, key_space: KeySpace) -> Result<()> {
match self {
WriteBatchRef::Serial(s) => s.flush(key_space),
WriteBatchRef::Concurrent(c, _) => {
// Safety: the &mut self ensures that no concurrent operation is happening
unsafe { c.flush(key_space) }
}
}
}
}
pub struct UnimplementedWriteBatch;
impl<'a> BaseWriteBatch<'a> for UnimplementedWriteBatch {
type ValueBuffer<'l>
= &'l [u8]
where
Self: 'l,
'a: 'l;
fn get<'l>(&'l self, _key_space: KeySpace, _key: &[u8]) -> Result<Option<Self::ValueBuffer<'l>>>
where
'a: 'l,
{
todo!()
}
fn commit(self) -> Result<()> {
todo!()
}
}
impl SerialWriteBatch<'_> for UnimplementedWriteBatch {
fn put(
&mut self,
_key_space: KeySpace,
_key: WriteBuffer<'_>,
_value: WriteBuffer<'_>,
) -> Result<()> {
todo!()
}
fn delete(&mut self, _key_space: KeySpace, _key: WriteBuffer<'_>) -> Result<()> {
todo!()
}
fn flush(&mut self, _key_space: KeySpace) -> Result<()> {
todo!()
}
}
impl ConcurrentWriteBatch<'_> for UnimplementedWriteBatch {
fn put(
&self,
_key_space: KeySpace,
_key: WriteBuffer<'_>,
_value: WriteBuffer<'_>,
) -> Result<()> {
todo!()
}
fn delete(&self, _key_space: KeySpace, _key: WriteBuffer<'_>) -> Result<()> {
todo!()
}
unsafe fn flush(&self, _key_space: KeySpace) -> Result<()> {
todo!()
}
}
|