-
Notifications
You must be signed in to change notification settings - Fork 46
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
7 changed files
with
181 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use std::ops::Bound; | |
use bytes::Bytes; | ||
use fusio::path::Path; | ||
use futures_util::stream::StreamExt; | ||
use ordered_float::OrderedFloat; | ||
use tokio::fs; | ||
use tonbo::{executor::tokio::TokioExecutor, DbOption, Projection, Record, DB}; | ||
|
||
|
@@ -15,6 +16,7 @@ pub struct User { | |
email: Option<String>, | ||
age: u8, | ||
bytes: Bytes, | ||
float: Option<OrderedFloat<f32>>, | ||
} | ||
|
||
#[tokio::main] | ||
|
@@ -32,6 +34,7 @@ async fn main() { | |
email: Some("[email protected]".into()), | ||
age: 22, | ||
bytes: Bytes::from(vec![0, 1, 2]), | ||
float: Some(OrderedFloat(1.1)), | ||
}) | ||
.await | ||
.unwrap(); | ||
|
@@ -61,7 +64,7 @@ async fn main() { | |
let mut scan = txn | ||
.scan((Bound::Included(&name), Bound::Excluded(&upper))) | ||
// tonbo supports pushing down projection | ||
.projection(vec![1, 3]) | ||
.projection(vec![1, 3, 4]) | ||
// push down limitation | ||
.limit(1) | ||
.take() | ||
|
@@ -75,6 +78,7 @@ async fn main() { | |
email: Some("[email protected]"), | ||
age: None, | ||
bytes: Some(&[0, 1, 2]), | ||
float: Some(OrderedFloat(1.1)), | ||
}) | ||
); | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use fusio::{SeqRead, Write}; | ||
use ordered_float::OrderedFloat; | ||
|
||
use crate::serdes::{Decode, Encode}; | ||
|
||
impl<T> Decode for OrderedFloat<T> | ||
where | ||
T: Decode + ordered_float::FloatCore, | ||
{ | ||
type Error = T::Error; | ||
|
||
async fn decode<R>(reader: &mut R) -> Result<Self, Self::Error> | ||
where | ||
R: SeqRead, | ||
{ | ||
Ok(OrderedFloat::from(T::decode(reader).await?)) | ||
} | ||
} | ||
|
||
impl<T> Encode for OrderedFloat<T> | ||
where | ||
T: Encode + Send + Sync, | ||
{ | ||
type Error = T::Error; | ||
|
||
async fn encode<W>(&self, writer: &mut W) -> Result<(), Self::Error> | ||
where | ||
W: Write, | ||
{ | ||
self.0.encode(writer).await | ||
} | ||
|
||
fn size(&self) -> usize { | ||
Encode::size(&self.0) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::io::Cursor; | ||
|
||
use ordered_float::OrderedFloat; | ||
use tokio::io::AsyncSeekExt; | ||
|
||
use crate::serdes::{Decode, Encode}; | ||
|
||
#[tokio::test] | ||
async fn test_encode_decode() { | ||
let source_0 = OrderedFloat(32f32); | ||
let source_1 = OrderedFloat(64f64); | ||
|
||
let mut bytes = Vec::new(); | ||
let mut cursor = Cursor::new(&mut bytes); | ||
|
||
source_0.encode(&mut cursor).await.unwrap(); | ||
source_1.encode(&mut cursor).await.unwrap(); | ||
|
||
cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap(); | ||
let decoded_0 = OrderedFloat::<f32>::decode(&mut cursor).await.unwrap(); | ||
let decoded_1 = OrderedFloat::<f64>::decode(&mut cursor).await.unwrap(); | ||
|
||
assert_eq!(source_0, decoded_0); | ||
assert_eq!(source_1, decoded_1); | ||
} | ||
} |
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
Oops, something went wrong.