| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// Copyright by contributors to this project. |
| 3 |
// SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 4 |
|
| 5 |
use std::{ |
| 6 |
fmt::{self, Debug}, |
| 7 |
sync::{Arc, Mutex}, |
| 8 |
}; |
| 9 |
|
| 10 |
use rusqlite::{params, Connection, OptionalExtension}; |
| 11 |
|
| 12 |
use crate::SqLiteDataStorageError; |
| 13 |
|
| 14 |
const INSERT_SQL: &str = |
| 15 |
"INSERT INTO kvs (key, value) VALUES (?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value"; |
| 16 |
|
| 17 |
#[derive(Debug, Clone)] |
| 18 |
/// SQLite key-value storage for application specific data. |
| 19 |
pub struct SqLiteApplicationStorage { |
| 20 |
connection: Arc<Mutex<Connection>>, |
| 21 |
} |
| 22 |
|
| 23 |
impl SqLiteApplicationStorage { |
| 24 |
pub(crate) fn new(connection: Connection) -> SqLiteApplicationStorage { |
| 25 |
SqLiteApplicationStorage { |
| 26 |
connection: Arc::new(Mutex::new(connection)), |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/// Insert `value` into storage indexed by `key`. |
| 31 |
/// |
| 32 |
/// If a value already exists for `key` it will be overwritten. |
| 33 |
pub fn insert(&self, key: &str, value: &[u8]) -> Result<(), SqLiteDataStorageError> { |
| 34 |
let connection = self.connection.lock().unwrap(); |
| 35 |
|
| 36 |
// Upsert into the database |
| 37 |
connection |
| 38 |
.execute(INSERT_SQL, params![key, value]) |
| 39 |
.map(|_| ()) |
| 40 |
.map_err(sql_engine_error) |
| 41 |
} |
| 42 |
|
| 43 |
/// Execute multiple [`SqLiteApplicationStorage::insert`] operations in a transaction. |
| 44 |
pub fn transact_insert(&self, items: &[Item]) -> Result<(), SqLiteDataStorageError> { |
| 45 |
let mut connection = self.connection.lock().unwrap(); |
| 46 |
|
| 47 |
// Upsert into the database |
| 48 |
let tx = connection.transaction().map_err(sql_engine_error)?; |
| 49 |
|
| 50 |
items.iter().try_for_each(|item| { |
| 51 |
tx.execute(INSERT_SQL, params![item.key, item.value]) |
| 52 |
.map_err(sql_engine_error) |
| 53 |
.map(|_| ()) |
| 54 |
})?; |
| 55 |
|
| 56 |
tx.commit().map_err(sql_engine_error)?; |
| 57 |
|
| 58 |
Ok(()) |
| 59 |
} |
| 60 |
|
| 61 |
/// Get a value from storage based on its `key`. |
| 62 |
pub fn get(&self, key: &str) -> Result<Option<Vec<u8>>, SqLiteDataStorageError> { |
| 63 |
let connection = self.connection.lock().unwrap(); |
| 64 |
|
| 65 |
connection |
| 66 |
.query_row("SELECT value FROM kvs WHERE key = ?", params![key], |row| { |
| 67 |
row.get(0) |
| 68 |
}) |
| 69 |
.optional() |
| 70 |
.map_err(sql_engine_error) |
| 71 |
} |
| 72 |
|
| 73 |
/// Delete a value from storage based on its `key`. |
| 74 |
pub fn delete(&self, key: &str) -> Result<(), SqLiteDataStorageError> { |
| 75 |
let connection = self.connection.lock().unwrap(); |
| 76 |
|
| 77 |
connection |
| 78 |
.execute("DELETE FROM kvs WHERE key = ?", params![key]) |
| 79 |
.map(|_| ()) |
| 80 |
.map_err(sql_engine_error) |
| 81 |
} |
| 82 |
|
| 83 |
/// Get all keys and values from storage for which key starts with `key_prefix`. |
| 84 |
pub fn get_by_prefix(&self, key_prefix: &str) -> Result<Vec<Item>, SqLiteDataStorageError> { |
| 85 |
let connection = self.connection.lock().unwrap(); |
| 86 |
let mut key_prefix = sanitize(key_prefix); |
| 87 |
key_prefix.push('%'); |
| 88 |
|
| 89 |
let mut stmt = connection |
| 90 |
.prepare("SELECT key, value FROM kvs WHERE key LIKE ? ESCAPE '$'") |
| 91 |
.map_err(sql_engine_error)?; |
| 92 |
|
| 93 |
let rows = stmt |
| 94 |
.query(params![key_prefix]) |
| 95 |
.map_err(sql_engine_error)? |
| 96 |
.mapped(|row| Ok(Item::new(row.get(0)?, row.get(1)?))); |
| 97 |
|
| 98 |
rows.collect::<Result<_, _>>().map_err(sql_engine_error) |
| 99 |
} |
| 100 |
|
| 101 |
/// Delete all values from storage for which key starts with `key_prefix`. |
| 102 |
pub fn delete_by_prefix(&self, key_prefix: &str) -> Result<(), SqLiteDataStorageError> { |
| 103 |
let connection = self.connection.lock().unwrap(); |
| 104 |
let mut key_prefix = sanitize(key_prefix); |
| 105 |
key_prefix.push('%'); |
| 106 |
|
| 107 |
connection |
| 108 |
.execute( |
| 109 |
"DELETE FROM kvs WHERE key LIKE ? ESCAPE '$'", |
| 110 |
params![key_prefix], |
| 111 |
) |
| 112 |
.map(|_| ()) |
| 113 |
.map_err(sql_engine_error) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
fn sanitize(string: &str) -> String { |
| 118 |
string.replace('_', "$_").replace('%', "$%") |
| 119 |
} |
| 120 |
|
| 121 |
fn sql_engine_error(e: rusqlite::Error) -> SqLiteDataStorageError { |
| 122 |
SqLiteDataStorageError::SqlEngineError(e.into()) |
| 123 |
} |
| 124 |
|
| 125 |
#[derive(Clone, Default, Hash, PartialEq, Eq, PartialOrd, Ord)] |
| 126 |
pub struct Item { |
| 127 |
pub key: String, |
| 128 |
pub value: Vec<u8>, |
| 129 |
} |
| 130 |
|
| 131 |
impl Debug for Item { |
| 132 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 133 |
f.debug_struct("Item") |
| 134 |
.field("key", &self.key) |
| 135 |
.field("value", &mls_rs_core::debug::pretty_bytes(&self.value)) |
| 136 |
.finish() |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl Item { |
| 141 |
pub fn new(key: String, value: Vec<u8>) -> Self { |
| 142 |
Self { key, value } |
| 143 |
} |
| 144 |
|
| 145 |
pub fn key(&self) -> &str { |
| 146 |
&self.key |
| 147 |
} |
| 148 |
|
| 149 |
pub fn value(&self) -> &[u8] { |
| 150 |
&self.value |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
#[cfg(test)] |
| 155 |
mod tests { |
| 156 |
use crate::{ |
| 157 |
application::Item, connection_strategy::MemoryStrategy, test_utils::gen_rand_bytes, |
| 158 |
SqLiteDataStorageEngine, |
| 159 |
}; |
| 160 |
|
| 161 |
use super::SqLiteApplicationStorage; |
| 162 |
|
| 163 |
fn test_kv() -> (String, Vec<u8>) { |
| 164 |
let key = hex::encode(gen_rand_bytes(32)); |
| 165 |
let value = gen_rand_bytes(64); |
| 166 |
|
| 167 |
(key, value) |
| 168 |
} |
| 169 |
|
| 170 |
fn test_storage() -> SqLiteApplicationStorage { |
| 171 |
SqLiteDataStorageEngine::new(MemoryStrategy) |
| 172 |
.unwrap() |
| 173 |
.application_data_storage() |
| 174 |
.unwrap() |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn test_insert() { |
| 179 |
let (key, value) = test_kv(); |
| 180 |
let storage = test_storage(); |
| 181 |
|
| 182 |
storage.insert(&key, &value).unwrap(); |
| 183 |
|
| 184 |
let from_storage = storage.get(&key).unwrap().unwrap(); |
| 185 |
assert_eq!(from_storage, value); |
| 186 |
} |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn test_insert_existing_overwrite() { |
| 190 |
let (key, value) = test_kv(); |
| 191 |
let (_, new_value) = test_kv(); |
| 192 |
|
| 193 |
let storage = test_storage(); |
| 194 |
|
| 195 |
storage.insert(&key, &value).unwrap(); |
| 196 |
storage.insert(&key, &new_value).unwrap(); |
| 197 |
|
| 198 |
let from_storage = storage.get(&key).unwrap().unwrap(); |
| 199 |
assert_eq!(from_storage, new_value); |
| 200 |
} |
| 201 |
|
| 202 |
#[test] |
| 203 |
fn test_delete() { |
| 204 |
let (key, value) = test_kv(); |
| 205 |
let storage = test_storage(); |
| 206 |
|
| 207 |
storage.insert(&key, &value).unwrap(); |
| 208 |
storage.delete(&key).unwrap(); |
| 209 |
|
| 210 |
assert!(storage.get(&key).unwrap().is_none()); |
| 211 |
} |
| 212 |
|
| 213 |
#[test] |
| 214 |
fn test_by_prefix() { |
| 215 |
let keys = ["prefix one", "prefix two", "prefiy ", "prefiw "].map(ToString::to_string); |
| 216 |
let value = gen_rand_bytes(5); |
| 217 |
|
| 218 |
let storage = test_storage(); |
| 219 |
|
| 220 |
keys.iter().for_each(|k| storage.insert(k, &value).unwrap()); |
| 221 |
|
| 222 |
let mut expected = vec![ |
| 223 |
Item::new(keys[0].clone(), value.clone()), |
| 224 |
Item::new(keys[1].clone(), value.clone()), |
| 225 |
]; |
| 226 |
|
| 227 |
expected.sort(); |
| 228 |
|
| 229 |
let mut result = storage.get_by_prefix("prefix").unwrap(); |
| 230 |
result.sort(); |
| 231 |
|
| 232 |
assert_eq!(result, expected); |
| 233 |
|
| 234 |
let result = storage.get_by_prefix("a").unwrap(); |
| 235 |
assert!(result.is_empty()); |
| 236 |
|
| 237 |
let result = storage.get_by_prefix("").unwrap(); |
| 238 |
assert_eq!(result.len(), keys.len()); |
| 239 |
|
| 240 |
storage.delete_by_prefix("prefix").unwrap(); |
| 241 |
let result = storage.get_by_prefix("").unwrap(); |
| 242 |
assert_eq!(result.len(), 2); |
| 243 |
assert!(result.contains(&Item::new("prefiy ".to_string(), value.clone()))); |
| 244 |
assert!(result.contains(&Item::new("prefiw ".to_string(), value))); |
| 245 |
} |
| 246 |
|
| 247 |
#[test] |
| 248 |
fn test_special_characters() { |
| 249 |
let storage = test_storage(); |
| 250 |
|
| 251 |
storage.insert("%$_ƕ❤_$%", &gen_rand_bytes(5)).unwrap(); |
| 252 |
storage.insert("%$_ƕ❤a$%", &gen_rand_bytes(5)).unwrap(); |
| 253 |
storage.insert("%$_ƕ❤Ḉ$%", &gen_rand_bytes(5)).unwrap(); |
| 254 |
|
| 255 |
let items = storage.get_by_prefix("%$_ƕ❤_").unwrap(); |
| 256 |
let keys = items.into_iter().map(|i| i.key).collect::<Vec<_>>(); |
| 257 |
assert_eq!(vec!["%$_ƕ❤_$%".to_string()], keys); |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn batch_insert() { |
| 262 |
let storage = test_storage(); |
| 263 |
let items = vec![test_item(), test_item(), test_item()]; |
| 264 |
|
| 265 |
storage.transact_insert(&items).unwrap(); |
| 266 |
|
| 267 |
for item in items { |
| 268 |
assert_eq!(storage.get(&item.key).unwrap(), Some(item.value)); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
fn test_item() -> Item { |
| 273 |
Item::new(hex::encode(gen_rand_bytes(5)), gen_rand_bytes(5)) |
| 274 |
} |
| 275 |
} |