| 1 |
// Copyright 2018-2019 Mozilla |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 4 |
// this file except in compliance with the License. You may obtain a copy of the |
| 5 |
// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
// Unless required by applicable law or agreed to in writing, software distributed |
| 7 |
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 8 |
// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 |
// specific language governing permissions and limitations under the License. |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
backend::{ |
| 13 |
BackendDatabase, BackendRoCursor, BackendRoCursorTransaction, BackendRoTransaction, |
| 14 |
BackendRwCursorTransaction, BackendRwTransaction, |
| 15 |
}, |
| 16 |
error::StoreError, |
| 17 |
helpers::read_transform, |
| 18 |
value::Value, |
| 19 |
}; |
| 20 |
|
| 21 |
pub struct Reader<T>(T); |
| 22 |
pub struct Writer<T>(T); |
| 23 |
|
| 24 |
pub trait Readable<'r> { |
| 25 |
type Database: BackendDatabase; |
| 26 |
type RoCursor: BackendRoCursor<'r>; |
| 27 |
|
| 28 |
fn get<K>(&'r self, db: &Self::Database, k: &K) -> Result<Option<Value<'r>>, StoreError> |
| 29 |
where |
| 30 |
K: AsRef<[u8]>; |
| 31 |
|
| 32 |
fn open_ro_cursor(&'r self, db: &Self::Database) -> Result<Self::RoCursor, StoreError>; |
| 33 |
} |
| 34 |
|
| 35 |
impl<'r, T> Readable<'r> for Reader<T> |
| 36 |
where |
| 37 |
T: BackendRoCursorTransaction<'r>, |
| 38 |
{ |
| 39 |
type Database = T::Database; |
| 40 |
type RoCursor = T::RoCursor; |
| 41 |
|
| 42 |
fn get<K>(&'r self, db: &T::Database, k: &K) -> Result<Option<Value<'r>>, StoreError> |
| 43 |
where |
| 44 |
K: AsRef<[u8]>, |
| 45 |
{ |
| 46 |
let bytes = self.0.get(db, k.as_ref()).map_err(|e| e.into()); |
| 47 |
match read_transform(bytes).map(Some) { |
| 48 |
Err(StoreError::KeyValuePairNotFound) => Ok(None), |
| 49 |
result => result, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
fn open_ro_cursor(&'r self, db: &T::Database) -> Result<T::RoCursor, StoreError> { |
| 54 |
self.0.open_ro_cursor(db).map_err(|e| e.into()) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl<T> Reader<T> { |
| 59 |
pub(crate) fn new(txn: T) -> Reader<T> { |
| 60 |
Reader(txn) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl<T> Reader<T> |
| 65 |
where |
| 66 |
T: BackendRoTransaction, |
| 67 |
{ |
| 68 |
pub fn abort(self) { |
| 69 |
self.0.abort(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl<'r, T> Readable<'r> for Writer<T> |
| 74 |
where |
| 75 |
T: BackendRwCursorTransaction<'r>, |
| 76 |
{ |
| 77 |
type Database = T::Database; |
| 78 |
type RoCursor = T::RoCursor; |
| 79 |
|
| 80 |
fn get<K>(&'r self, db: &T::Database, k: &K) -> Result<Option<Value<'r>>, StoreError> |
| 81 |
where |
| 82 |
K: AsRef<[u8]>, |
| 83 |
{ |
| 84 |
let bytes = self.0.get(db, k.as_ref()).map_err(|e| e.into()); |
| 85 |
match read_transform(bytes).map(Some) { |
| 86 |
Err(StoreError::KeyValuePairNotFound) => Ok(None), |
| 87 |
result => result, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
fn open_ro_cursor(&'r self, db: &T::Database) -> Result<T::RoCursor, StoreError> { |
| 92 |
self.0.open_ro_cursor(db).map_err(|e| e.into()) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl<T> Writer<T> { |
| 97 |
pub(crate) fn new(txn: T) -> Writer<T> { |
| 98 |
Writer(txn) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
impl<T> Writer<T> |
| 103 |
where |
| 104 |
T: BackendRwTransaction, |
| 105 |
{ |
| 106 |
pub fn commit(self) -> Result<(), StoreError> { |
| 107 |
self.0.commit().map_err(|e| e.into()) |
| 108 |
} |
| 109 |
|
| 110 |
pub fn abort(self) { |
| 111 |
self.0.abort(); |
| 112 |
} |
| 113 |
|
| 114 |
pub(crate) fn put<K>( |
| 115 |
&mut self, |
| 116 |
db: &T::Database, |
| 117 |
k: &K, |
| 118 |
v: &Value, |
| 119 |
flags: T::Flags, |
| 120 |
) -> Result<(), StoreError> |
| 121 |
where |
| 122 |
K: AsRef<[u8]>, |
| 123 |
{ |
| 124 |
// TODO: don't allocate twice. |
| 125 |
self.0 |
| 126 |
.put(db, k.as_ref(), &v.to_bytes()?, flags) |
| 127 |
.map_err(|e| e.into()) |
| 128 |
} |
| 129 |
|
| 130 |
#[cfg(not(feature = "db-dup-sort"))] |
| 131 |
pub(crate) fn delete<K>(&mut self, db: &T::Database, k: &K) -> Result<(), StoreError> |
| 132 |
where |
| 133 |
K: AsRef<[u8]>, |
| 134 |
{ |
| 135 |
self.0.del(db, k.as_ref()).map_err(|e| e.into()) |
| 136 |
} |
| 137 |
|
| 138 |
#[cfg(feature = "db-dup-sort")] |
| 139 |
pub(crate) fn delete<K>( |
| 140 |
&mut self, |
| 141 |
db: &T::Database, |
| 142 |
k: &K, |
| 143 |
v: Option<&[u8]>, |
| 144 |
) -> Result<(), StoreError> |
| 145 |
where |
| 146 |
K: AsRef<[u8]>, |
| 147 |
{ |
| 148 |
self.0.del(db, k.as_ref(), v).map_err(|e| e.into()) |
| 149 |
} |
| 150 |
|
| 151 |
pub(crate) fn clear(&mut self, db: &T::Database) -> Result<(), StoreError> { |
| 152 |
self.0.clear_db(db).map_err(|e| e.into()) |
| 153 |
} |
| 154 |
} |