2023-11-01 14:15:57 +00:00
|
|
|
use crate::storage::*;
|
2023-12-27 13:58:28 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
|
|
use std::ops::Bound::{self, Excluded, Included, Unbounded};
|
2023-12-16 10:13:32 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2023-12-19 18:02:22 +00:00
|
|
|
use tokio::sync::Notify;
|
2023-12-16 10:13:32 +00:00
|
|
|
|
|
|
|
/// This implementation is very inneficient, and not completely correct
|
|
|
|
/// Indeed, when the connector is dropped, the memory is freed.
|
|
|
|
/// It means that when a user disconnects, its data are lost.
|
|
|
|
/// It's intended only for basic debugging, do not use it for advanced tests...
|
|
|
|
|
2023-12-21 14:36:05 +00:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct MemDb(tokio::sync::Mutex<HashMap<String, Arc<MemBuilder>>>);
|
|
|
|
impl MemDb {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self(tokio::sync::Mutex::new(HashMap::new()))
|
|
|
|
}
|
|
|
|
|
2023-12-27 13:58:28 +00:00
|
|
|
pub async fn builder(&self, username: &str) -> Arc<MemBuilder> {
|
2023-12-21 14:36:05 +00:00
|
|
|
let mut global_storage = self.0.lock().await;
|
|
|
|
global_storage
|
|
|
|
.entry(username.to_string())
|
|
|
|
.or_insert(MemBuilder::new(username))
|
|
|
|
.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 18:02:22 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum InternalData {
|
|
|
|
Tombstone,
|
|
|
|
Value(Vec<u8>),
|
|
|
|
}
|
|
|
|
impl InternalData {
|
|
|
|
fn to_alternative(&self) -> Alternative {
|
|
|
|
match self {
|
|
|
|
Self::Tombstone => Alternative::Tombstone,
|
|
|
|
Self::Value(x) => Alternative::Value(x.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 18:21:36 +00:00
|
|
|
#[derive(Debug)]
|
2023-12-19 18:02:22 +00:00
|
|
|
struct InternalRowVal {
|
|
|
|
data: Vec<InternalData>,
|
|
|
|
version: u64,
|
|
|
|
change: Arc<Notify>,
|
|
|
|
}
|
2023-12-19 18:21:36 +00:00
|
|
|
impl std::default::Default for InternalRowVal {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
data: vec![],
|
|
|
|
version: 1,
|
|
|
|
change: Arc::new(Notify::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 18:02:22 +00:00
|
|
|
impl InternalRowVal {
|
|
|
|
fn concurrent_values(&self) -> Vec<Alternative> {
|
|
|
|
self.data.iter().map(InternalData::to_alternative).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_row_val(&self, row_ref: RowRef) -> RowVal {
|
2023-12-27 13:58:28 +00:00
|
|
|
RowVal {
|
|
|
|
row_ref: row_ref.with_causality(self.version.to_string()),
|
2023-12-19 18:02:22 +00:00
|
|
|
value: self.concurrent_values(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
struct InternalBlobVal {
|
|
|
|
data: Vec<u8>,
|
|
|
|
metadata: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
impl InternalBlobVal {
|
|
|
|
fn to_blob_val(&self, bref: &BlobRef) -> BlobVal {
|
|
|
|
BlobVal {
|
2023-12-27 13:58:28 +00:00
|
|
|
blob_ref: bref.clone(),
|
2023-12-19 18:02:22 +00:00
|
|
|
meta: self.metadata.clone(),
|
|
|
|
value: self.data.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ArcRow = Arc<RwLock<HashMap<String, BTreeMap<String, InternalRowVal>>>>;
|
|
|
|
type ArcBlob = Arc<RwLock<BTreeMap<String, InternalBlobVal>>>;
|
2023-12-16 10:13:32 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct MemBuilder {
|
2023-12-18 16:09:44 +00:00
|
|
|
unicity: Vec<u8>,
|
2023-12-16 10:13:32 +00:00
|
|
|
row: ArcRow,
|
|
|
|
blob: ArcBlob,
|
2023-11-01 14:15:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 16:09:44 +00:00
|
|
|
impl MemBuilder {
|
|
|
|
pub fn new(user: &str) -> Arc<Self> {
|
2023-12-20 12:55:23 +00:00
|
|
|
tracing::debug!("initialize membuilder for {}", user);
|
2023-12-18 16:09:44 +00:00
|
|
|
let mut unicity: Vec<u8> = vec![];
|
|
|
|
unicity.extend_from_slice(file!().as_bytes());
|
|
|
|
unicity.extend_from_slice(user.as_bytes());
|
|
|
|
Arc::new(Self {
|
|
|
|
unicity,
|
|
|
|
row: Arc::new(RwLock::new(HashMap::new())),
|
2023-12-19 18:02:22 +00:00
|
|
|
blob: Arc::new(RwLock::new(BTreeMap::new())),
|
2023-12-18 16:09:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 20:54:36 +00:00
|
|
|
#[async_trait]
|
2023-12-16 10:13:32 +00:00
|
|
|
impl IBuilder for MemBuilder {
|
2023-12-21 20:54:36 +00:00
|
|
|
async fn build(&self) -> Result<Store, StorageError> {
|
2023-12-18 16:09:44 +00:00
|
|
|
Ok(Box::new(MemStore {
|
2023-12-16 10:13:32 +00:00
|
|
|
row: self.row.clone(),
|
|
|
|
blob: self.blob.clone(),
|
2023-12-18 16:09:44 +00:00
|
|
|
}))
|
2023-12-27 13:58:28 +00:00
|
|
|
}
|
2023-12-18 16:09:44 +00:00
|
|
|
|
|
|
|
fn unique(&self) -> UnicityBuffer {
|
|
|
|
UnicityBuffer(self.unicity.clone())
|
|
|
|
}
|
2023-12-16 10:13:32 +00:00
|
|
|
}
|
2023-11-17 09:46:13 +00:00
|
|
|
|
2023-12-16 10:13:32 +00:00
|
|
|
pub struct MemStore {
|
|
|
|
row: ArcRow,
|
|
|
|
blob: ArcBlob,
|
2023-11-01 14:15:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 18:02:22 +00:00
|
|
|
fn prefix_last_bound(prefix: &str) -> Bound<String> {
|
|
|
|
let mut sort_end = prefix.to_string();
|
|
|
|
match sort_end.pop() {
|
|
|
|
None => Unbounded,
|
|
|
|
Some(ch) => {
|
|
|
|
let nc = char::from_u32(ch as u32 + 1).unwrap();
|
|
|
|
sort_end.push(nc);
|
|
|
|
Excluded(sort_end)
|
|
|
|
}
|
2023-11-17 09:46:13 +00:00
|
|
|
}
|
2023-12-16 10:13:32 +00:00
|
|
|
}
|
2023-11-17 09:46:13 +00:00
|
|
|
|
2023-12-27 13:58:09 +00:00
|
|
|
impl MemStore {
|
|
|
|
fn row_rm_single(&self, entry: &RowRef) -> Result<(), StorageError> {
|
|
|
|
tracing::trace!(entry=%entry, command="row_rm_single");
|
|
|
|
let mut store = self.row.write().or(Err(StorageError::Internal))?;
|
|
|
|
let shard = &entry.uid.shard;
|
|
|
|
let sort = &entry.uid.sort;
|
|
|
|
|
|
|
|
let cauz = match entry.causality.as_ref().map(|v| v.parse::<u64>()) {
|
|
|
|
Some(Ok(v)) => v,
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let bt = store.entry(shard.to_string()).or_default();
|
|
|
|
let intval = bt.entry(sort.to_string()).or_default();
|
|
|
|
|
|
|
|
if cauz == intval.version {
|
|
|
|
intval.data.clear();
|
|
|
|
}
|
|
|
|
intval.data.push(InternalData::Tombstone);
|
|
|
|
intval.version += 1;
|
|
|
|
intval.change.notify_waiters();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 10:13:32 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl IStore for MemStore {
|
|
|
|
async fn row_fetch<'a>(&self, select: &Selector<'a>) -> Result<Vec<RowVal>, StorageError> {
|
2023-12-19 20:41:35 +00:00
|
|
|
tracing::trace!(select=%select, command="row_fetch");
|
2023-12-19 18:02:22 +00:00
|
|
|
let store = self.row.read().or(Err(StorageError::Internal))?;
|
|
|
|
|
2023-12-16 10:13:32 +00:00
|
|
|
match select {
|
2023-12-27 13:58:28 +00:00
|
|
|
Selector::Range {
|
|
|
|
shard,
|
|
|
|
sort_begin,
|
|
|
|
sort_end,
|
|
|
|
} => Ok(store
|
|
|
|
.get(*shard)
|
|
|
|
.unwrap_or(&BTreeMap::new())
|
|
|
|
.range((
|
|
|
|
Included(sort_begin.to_string()),
|
|
|
|
Excluded(sort_end.to_string()),
|
|
|
|
))
|
|
|
|
.map(|(k, v)| v.to_row_val(RowRef::new(shard, k)))
|
|
|
|
.collect::<Vec<_>>()),
|
2023-12-16 10:13:32 +00:00
|
|
|
Selector::List(rlist) => {
|
|
|
|
let mut acc = vec![];
|
|
|
|
for row_ref in rlist {
|
2023-12-27 13:58:28 +00:00
|
|
|
let maybe_intval = store
|
|
|
|
.get(&row_ref.uid.shard)
|
|
|
|
.map(|v| v.get(&row_ref.uid.sort))
|
|
|
|
.flatten();
|
2023-12-19 20:41:35 +00:00
|
|
|
if let Some(intval) = maybe_intval {
|
|
|
|
acc.push(intval.to_row_val(row_ref.clone()));
|
|
|
|
}
|
2023-12-16 10:13:32 +00:00
|
|
|
}
|
|
|
|
Ok(acc)
|
2023-12-27 13:58:28 +00:00
|
|
|
}
|
2023-12-16 10:13:32 +00:00
|
|
|
Selector::Prefix { shard, sort_prefix } => {
|
2023-12-19 18:02:22 +00:00
|
|
|
let last_bound = prefix_last_bound(sort_prefix);
|
|
|
|
|
|
|
|
Ok(store
|
2023-12-16 10:13:32 +00:00
|
|
|
.get(*shard)
|
2023-12-19 20:41:35 +00:00
|
|
|
.unwrap_or(&BTreeMap::new())
|
2023-12-16 10:13:32 +00:00
|
|
|
.range((Included(sort_prefix.to_string()), last_bound))
|
2023-12-19 18:02:22 +00:00
|
|
|
.map(|(k, v)| v.to_row_val(RowRef::new(shard, k)))
|
2023-12-16 10:13:32 +00:00
|
|
|
.collect::<Vec<_>>())
|
2023-12-27 13:58:28 +00:00
|
|
|
}
|
2023-12-16 10:13:32 +00:00
|
|
|
Selector::Single(row_ref) => {
|
2023-12-19 18:02:22 +00:00
|
|
|
let intval = store
|
2023-12-27 13:58:28 +00:00
|
|
|
.get(&row_ref.uid.shard)
|
|
|
|
.ok_or(StorageError::NotFound)?
|
|
|
|
.get(&row_ref.uid.sort)
|
|
|
|
.ok_or(StorageError::NotFound)?;
|
2023-12-19 18:02:22 +00:00
|
|
|
Ok(vec![intval.to_row_val((*row_ref).clone())])
|
2023-12-16 10:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn row_rm<'a>(&self, select: &Selector<'a>) -> Result<(), StorageError> {
|
2023-12-19 20:41:35 +00:00
|
|
|
tracing::trace!(select=%select, command="row_rm");
|
2023-12-27 13:58:09 +00:00
|
|
|
|
|
|
|
let values = match select {
|
2023-12-27 13:58:28 +00:00
|
|
|
Selector::Range { .. } | Selector::Prefix { .. } => self
|
|
|
|
.row_fetch(select)
|
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(|rv| rv.row_ref)
|
|
|
|
.collect::<Vec<_>>(),
|
2023-12-27 13:58:09 +00:00
|
|
|
Selector::List(rlist) => rlist.clone(),
|
|
|
|
Selector::Single(row_ref) => vec![(*row_ref).clone()],
|
|
|
|
};
|
2023-12-19 18:02:22 +00:00
|
|
|
|
|
|
|
for v in values.into_iter() {
|
2023-12-27 13:58:09 +00:00
|
|
|
self.row_rm_single(&v)?;
|
2023-12-19 18:02:22 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2023-11-15 14:56:43 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 10:13:32 +00:00
|
|
|
async fn row_insert(&self, values: Vec<RowVal>) -> Result<(), StorageError> {
|
2023-12-19 20:41:35 +00:00
|
|
|
tracing::trace!(entries=%values.iter().map(|v| v.row_ref.to_string()).collect::<Vec<_>>().join(","), command="row_insert");
|
2023-12-19 18:02:22 +00:00
|
|
|
let mut store = self.row.write().or(Err(StorageError::Internal))?;
|
|
|
|
for v in values.into_iter() {
|
|
|
|
let shard = v.row_ref.uid.shard;
|
|
|
|
let sort = v.row_ref.uid.sort;
|
|
|
|
|
|
|
|
let val = match v.value.into_iter().next() {
|
|
|
|
Some(Alternative::Value(x)) => x,
|
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
let cauz = match v.row_ref.causality.map(|v| v.parse::<u64>()) {
|
|
|
|
Some(Ok(v)) => v,
|
|
|
|
_ => 0,
|
|
|
|
};
|
2023-11-02 14:28:19 +00:00
|
|
|
|
2023-12-19 18:02:22 +00:00
|
|
|
let bt = store.entry(shard).or_default();
|
|
|
|
let intval = bt.entry(sort).or_default();
|
|
|
|
|
|
|
|
if cauz == intval.version {
|
|
|
|
intval.data.clear();
|
|
|
|
}
|
|
|
|
intval.data.push(InternalData::Value(val));
|
|
|
|
intval.version += 1;
|
|
|
|
intval.change.notify_waiters();
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-11-01 14:15:57 +00:00
|
|
|
}
|
2023-12-18 16:09:44 +00:00
|
|
|
async fn row_poll(&self, value: &RowRef) -> Result<RowVal, StorageError> {
|
2023-12-19 20:41:35 +00:00
|
|
|
tracing::trace!(entry=%value, command="row_poll");
|
2023-12-19 18:02:22 +00:00
|
|
|
let shard = &value.uid.shard;
|
|
|
|
let sort = &value.uid.sort;
|
|
|
|
let cauz = match value.causality.as_ref().map(|v| v.parse::<u64>()) {
|
|
|
|
Some(Ok(v)) => v,
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let notify_me = {
|
2023-12-19 18:21:36 +00:00
|
|
|
let mut store = self.row.write().or(Err(StorageError::Internal))?;
|
|
|
|
let bt = store.entry(shard.to_string()).or_default();
|
|
|
|
let intval = bt.entry(sort.to_string()).or_default();
|
2023-12-19 18:02:22 +00:00
|
|
|
|
|
|
|
if intval.version != cauz {
|
|
|
|
return Ok(intval.to_row_val(value.clone()));
|
|
|
|
}
|
|
|
|
intval.change.clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
notify_me.notified().await;
|
|
|
|
|
|
|
|
let res = self.row_fetch(&Selector::Single(value)).await?;
|
|
|
|
res.into_iter().next().ok_or(StorageError::NotFound)
|
2023-11-01 14:15:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 10:13:32 +00:00
|
|
|
async fn blob_fetch(&self, blob_ref: &BlobRef) -> Result<BlobVal, StorageError> {
|
2023-12-19 20:41:35 +00:00
|
|
|
tracing::trace!(entry=%blob_ref, command="blob_fetch");
|
2023-12-19 18:02:22 +00:00
|
|
|
let store = self.blob.read().or(Err(StorageError::Internal))?;
|
2023-12-27 13:58:28 +00:00
|
|
|
store
|
|
|
|
.get(&blob_ref.0)
|
|
|
|
.ok_or(StorageError::NotFound)
|
|
|
|
.map(|v| v.to_blob_val(blob_ref))
|
2023-11-01 14:15:57 +00:00
|
|
|
}
|
2023-12-22 18:32:07 +00:00
|
|
|
async fn blob_insert(&self, blob_val: BlobVal) -> Result<(), StorageError> {
|
2023-12-19 20:41:35 +00:00
|
|
|
tracing::trace!(entry=%blob_val.blob_ref, command="blob_insert");
|
2023-12-19 18:02:22 +00:00
|
|
|
let mut store = self.blob.write().or(Err(StorageError::Internal))?;
|
|
|
|
let entry = store.entry(blob_val.blob_ref.0.clone()).or_default();
|
|
|
|
entry.data = blob_val.value.clone();
|
|
|
|
entry.metadata = blob_val.meta.clone();
|
|
|
|
Ok(())
|
2023-12-18 16:09:44 +00:00
|
|
|
}
|
2023-12-19 18:02:22 +00:00
|
|
|
async fn blob_copy(&self, src: &BlobRef, dst: &BlobRef) -> Result<(), StorageError> {
|
2023-12-19 20:41:35 +00:00
|
|
|
tracing::trace!(src=%src, dst=%dst, command="blob_copy");
|
2023-12-19 18:02:22 +00:00
|
|
|
let mut store = self.blob.write().or(Err(StorageError::Internal))?;
|
|
|
|
let blob_src = store.entry(src.0.clone()).or_default().clone();
|
|
|
|
store.insert(dst.0.clone(), blob_src);
|
|
|
|
Ok(())
|
2023-11-01 14:15:57 +00:00
|
|
|
}
|
2023-12-16 10:13:32 +00:00
|
|
|
async fn blob_list(&self, prefix: &str) -> Result<Vec<BlobRef>, StorageError> {
|
2023-12-27 13:58:28 +00:00
|
|
|
tracing::trace!(prefix = prefix, command = "blob_list");
|
2023-12-19 18:02:22 +00:00
|
|
|
let store = self.blob.read().or(Err(StorageError::Internal))?;
|
|
|
|
let last_bound = prefix_last_bound(prefix);
|
2023-12-27 13:58:28 +00:00
|
|
|
let blist = store
|
|
|
|
.range((Included(prefix.to_string()), last_bound))
|
|
|
|
.map(|(k, _)| BlobRef(k.to_string()))
|
|
|
|
.collect::<Vec<_>>();
|
2023-12-19 18:02:22 +00:00
|
|
|
Ok(blist)
|
2023-11-01 14:15:57 +00:00
|
|
|
}
|
2023-12-16 10:13:32 +00:00
|
|
|
async fn blob_rm(&self, blob_ref: &BlobRef) -> Result<(), StorageError> {
|
2023-12-19 20:41:35 +00:00
|
|
|
tracing::trace!(entry=%blob_ref, command="blob_rm");
|
2023-12-19 18:02:22 +00:00
|
|
|
let mut store = self.blob.write().or(Err(StorageError::Internal))?;
|
|
|
|
store.remove(&blob_ref.0);
|
|
|
|
Ok(())
|
2023-11-21 08:56:31 +00:00
|
|
|
}
|
|
|
|
}
|