-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
170 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,154 @@ | ||
use std::{collections::BTreeMap, io, sync::Arc}; | ||
use std::{ | ||
collections::{btree_map::Entry, BTreeMap}, | ||
io, | ||
mem::transmute, | ||
}; | ||
|
||
use crate::{executor::Executor, oracle::Timestamp, Record, DB}; | ||
use async_lock::RwLockReadGuard; | ||
use parquet::errors::ParquetError; | ||
use thiserror::Error; | ||
|
||
pub struct Transaction<R, E> | ||
use crate::{ | ||
executor::Executor, | ||
oracle::{Timestamp, WriteConflict}, | ||
record::KeyRef, | ||
stream, Record, Schema, DB, | ||
}; | ||
|
||
pub struct Transaction<'txn, R, E> | ||
where | ||
R: Record, | ||
E: Executor, | ||
{ | ||
db: Arc<DB<R, E>>, | ||
read_at: Timestamp, | ||
local: BTreeMap<R::Key, Option<R>>, | ||
share: RwLockReadGuard<'txn, Schema<R>>, | ||
db: &'txn DB<R, E>, | ||
} | ||
|
||
impl<R, E> Transaction<R, E> | ||
impl<'txn, R, E> Transaction<'txn, R, E> | ||
where | ||
R: Record, | ||
R: Record + Send, | ||
E: Executor, | ||
{ | ||
pub(crate) fn new(db: Arc<DB<R, E>>, read_at: Timestamp) -> Self { | ||
pub(crate) fn new(db: &'txn DB<R, E>, share: RwLockReadGuard<'txn, Schema<R>>) -> Self { | ||
Self { | ||
db, | ||
read_at, | ||
read_at: db.oracle.start_read(), | ||
local: BTreeMap::new(), | ||
share, | ||
db, | ||
} | ||
} | ||
|
||
pub async fn get<'get>( | ||
&'get self, | ||
key: &'get R::Key, | ||
) -> Result<Option<TransactionEntry<'get, R>>, ParquetError> { | ||
Ok(match self.local.get(key).and_then(|v| v.as_ref()) { | ||
Some(v) => Some(TransactionEntry::Local(v.as_record_ref())), | ||
None => self | ||
.share | ||
.get::<E>(key, self.read_at) | ||
.await? | ||
.map(TransactionEntry::Stream), | ||
}) | ||
} | ||
|
||
pub fn set(&mut self, value: R) { | ||
self.entry(value.key().to_key(), Some(value)) | ||
} | ||
|
||
pub fn remove(&mut self, key: R::Key) { | ||
self.entry(key, None) | ||
} | ||
|
||
fn entry(&mut self, key: R::Key, value: Option<R>) { | ||
match self.local.entry(key) { | ||
Entry::Vacant(v) => { | ||
v.insert(value); | ||
} | ||
Entry::Occupied(mut o) => *o.get_mut() = value, | ||
} | ||
} | ||
|
||
pub async fn commit(self) -> Result<(), CommitError<R>> { | ||
self.db.oracle.read_commit(self.read_at); | ||
if self.local.is_empty() { | ||
return Ok(()); | ||
} | ||
let write_at = self.db.oracle.start_write(); | ||
self.db.oracle.write_commit( | ||
self.read_at, | ||
write_at, | ||
self.local.keys().cloned().collect(), | ||
)?; | ||
|
||
for (key, record) in self.local { | ||
match record { | ||
Some(record) => self.share.write(record, write_at).await?, | ||
None => self.share.remove(key, write_at).await?, | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub enum TransactionEntry<'entry, R> | ||
where | ||
R: Record, | ||
{ | ||
Stream(stream::Entry<'entry, R>), | ||
Local(R::Ref<'entry>), | ||
} | ||
|
||
impl<'entry, R> TransactionEntry<'entry, R> | ||
where | ||
R: Record, | ||
{ | ||
pub fn get(&self) -> R::Ref<'_> { | ||
match self { | ||
TransactionEntry::Stream(entry) => entry.value(), | ||
TransactionEntry::Local(value) => { | ||
// Safety: shorter lifetime must be safe | ||
unsafe { transmute::<R::Ref<'entry>, R::Ref<'_>>(*value) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub async fn get(&self, key: &R::Key) -> io::Result<Option<&R>> { | ||
// match self.local.get(key).and_then(|v| v.as_ref()) { | ||
// Some(v) => Ok(Some(v)), | ||
// None => self.db.get(key, self.read_at).await, | ||
// } | ||
todo!() | ||
#[derive(Debug, Error)] | ||
pub enum CommitError<R> | ||
where | ||
R: Record, | ||
{ | ||
#[error("commit transaction error {:?}", .0)] | ||
Io(#[from] io::Error), | ||
#[error(transparent)] | ||
WriteConflict(#[from] WriteConflict<R::Key>), | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{executor::tokio::TokioExecutor, DB}; | ||
|
||
#[tokio::test] | ||
async fn transaction_read_write() { | ||
let db = DB::<String, TokioExecutor>::default(); | ||
{ | ||
let mut txn1 = db.transaction().await; | ||
txn1.set("foo".to_string()); | ||
|
||
let txn2 = db.transaction().await; | ||
dbg!(txn2.get(&"foo".to_string()).await.unwrap().is_none()); | ||
|
||
txn1.commit().await.unwrap(); | ||
txn2.commit().await.unwrap(); | ||
} | ||
|
||
{ | ||
let txn3 = db.transaction().await; | ||
dbg!(txn3.get(&"foo".to_string()).await.unwrap().is_none()); | ||
txn3.commit().await.unwrap(); | ||
} | ||
} | ||
} |