This commit is contained in:
John Smith 2022-12-28 09:48:25 -05:00
parent f3330e7589
commit 99d840e281
4 changed files with 62 additions and 16 deletions

5
Cargo.lock generated
View File

@ -2787,6 +2787,9 @@ dependencies = [
"keyvaluedb", "keyvaluedb",
"keyvaluedb-shared-tests", "keyvaluedb-shared-tests",
"parking_lot 0.12.1", "parking_lot 0.12.1",
"tokio 1.23.0",
"wasm-bindgen-futures",
"wasm-bindgen-test",
] ]
[[package]] [[package]]
@ -2815,6 +2818,7 @@ dependencies = [
"rusqlite", "rusqlite",
"sysinfo", "sysinfo",
"tempfile", "tempfile",
"tokio 1.23.0",
] ]
[[package]] [[package]]
@ -2822,6 +2826,7 @@ name = "keyvaluedb-web"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"console_log", "console_log",
"flume",
"futures", "futures",
"js-sys", "js-sys",
"keyvaluedb", "keyvaluedb",

2
external/keyvaluedb vendored

@ -1 +1 @@
Subproject commit e30d0058defd9cfd7bd546bb177228edda8076ab Subproject commit 3408e0b2ae3df0088e0714bc23fb33c82a58e22c

View File

@ -17,13 +17,19 @@ pub struct TableDBInner {
database: Database, database: Database,
} }
impl fmt::Debug for TableDBInner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "TableDBInner(table={})", self.table)
}
}
impl Drop for TableDBInner { impl Drop for TableDBInner {
fn drop(&mut self) { fn drop(&mut self) {
self.table_store.on_table_db_drop(self.table.clone()); self.table_store.on_table_db_drop(self.table.clone());
} }
} }
#[derive(Clone)] #[derive(Debug, Clone)]
pub struct TableDB { pub struct TableDB {
inner: Arc<Mutex<TableDBInner>>, inner: Arc<Mutex<TableDBInner>>,
} }
@ -68,12 +74,12 @@ impl TableDB {
} }
/// Start a TableDB write transaction. The transaction object must be committed or rolled back before dropping. /// Start a TableDB write transaction. The transaction object must be committed or rolled back before dropping.
pub fn transact<'a>(&'a self) -> TableDBTransaction<'a> { pub fn transact(&self) -> TableDBTransaction {
let dbt = { let dbt = {
let db = &self.inner.lock().database; let db = &self.inner.lock().database;
db.transaction() db.transaction()
}; };
TableDBTransaction::new(self, dbt) TableDBTransaction::new(self.clone(), dbt)
} }
/// Store a key with a value in a column in the TableDB. Performs a single transaction immediately. /// Store a key with a value in a column in the TableDB. Performs a single transaction immediately.
@ -172,31 +178,54 @@ impl TableDB {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// A TableDB transaction struct TableDBTransactionInner {
/// Atomically commits a group of writes or deletes to the TableDB
pub struct TableDBTransaction<'a> {
db: &'a TableDB,
dbt: Option<DBTransaction>, dbt: Option<DBTransaction>,
_phantom: core::marker::PhantomData<&'a ()>,
} }
impl<'a> TableDBTransaction<'a> { impl fmt::Debug for TableDBTransactionInner {
fn new(db: &'a TableDB, dbt: DBTransaction) -> Self { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"TableDBTransactionInner({})",
match &self.dbt {
Some(dbt) => format!("len={}", dbt.ops.len()),
None => "".to_owned(),
}
)
}
}
/// A TableDB transaction
/// Atomically commits a group of writes or deletes to the TableDB
#[derive(Debug, Clone)]
pub struct TableDBTransaction {
db: TableDB,
inner: Arc<Mutex<TableDBTransactionInner>>,
}
impl TableDBTransaction {
fn new(db: TableDB, dbt: DBTransaction) -> Self {
Self { Self {
db, db,
dbt: Some(dbt), inner: Arc::new(Mutex::new(TableDBTransactionInner { dbt: Some(dbt) })),
_phantom: Default::default(),
} }
} }
/// Commit the transaction. Performs all actions atomically. /// Commit the transaction. Performs all actions atomically.
pub fn commit(mut self) -> EyreResult<()> { pub fn commit(mut self) -> EyreResult<()> {
let dbt = {
let inner = self.inner.lock();
inner
.dbt
.take()
.ok_or_else(|| Err(eyre!("transaction already completed")))?
};
self.db self.db
.inner .inner
.lock() .lock()
.database .database
.write(self.dbt.take().unwrap()) .write(dbt)
.wrap_err("commit failed") .wrap_err("commit failed, transaction lost")
} }
/// Rollback the transaction. Does nothing to the TableDB. /// Rollback the transaction. Does nothing to the TableDB.
@ -235,7 +264,7 @@ impl<'a> TableDBTransaction<'a> {
} }
} }
impl<'a> Drop for TableDBTransaction<'a> { impl Drop for TableDBTransactionInner {
fn drop(&mut self) { fn drop(&mut self) {
if self.dbt.is_some() { if self.dbt.is_some() {
warn!("Dropped transaction without commit or rollback"); warn!("Dropped transaction without commit or rollback");

View File

@ -1847,6 +1847,14 @@ abstract class VeilidRoutingContext {
Future<void> appMessage(String target, Uint8List message); Future<void> appMessage(String target, Uint8List message);
} }
/////////////////////////////////////
/// VeilidTableDB
abstract class VeilidTableDB {
int getColumnCount();
List<Uint8List> getKeys();
VeilidTableDBTransaction transact()
}
////////////////////////////////////// //////////////////////////////////////
/// Veilid singleton factory /// Veilid singleton factory
@ -1874,6 +1882,10 @@ abstract class Veilid {
// App calls // App calls
Future<void> appCallReply(String id, Uint8List message); Future<void> appCallReply(String id, Uint8List message);
// TableStore
Future<VeilidTableDB> openTableDB(String name, int columnCount);
Future<bool> deleteTableDB(String name);
// Misc // Misc
String veilidVersionString(); String veilidVersionString();
VeilidVersion veilidVersion(); VeilidVersion veilidVersion();