add async feature #1

Merged
b4tman merged 62 commits from async into master 2023-08-18 06:29:46 +00:00
4 changed files with 61 additions and 51 deletions
Showing only changes of commit 0c0a4e31ba - Show all commits

View File

@ -14,7 +14,7 @@ name = "db_bench"
harness = false harness = false
[dependencies] [dependencies]
chgk_ledb_lib = {path = "../lib", features = ["sync"]} chgk_ledb_lib = {path = "../lib", features = ["sync", "source"]}
serde_json="1.0" serde_json="1.0"
zip="0.6" zip="0.6"
rand="0.8" rand="0.8"

View File

@ -13,13 +13,14 @@ description = "Библиотека для доступа к файлу базы
default = ["async"] default = ["async"]
sync = ["zstd", "memmap"] sync = ["zstd", "memmap"]
async = ["futures", "futures-core", "futures-util", "fmmap", "tokio", "async-compression", "async-stream"] async = ["futures", "futures-core", "futures-util", "fmmap", "tokio", "async-compression", "async-stream"]
source = ["zip"]
[dependencies] [dependencies]
serde="1.0" serde="1.0"
serde_derive="1.0" serde_derive="1.0"
serde_json="1.0" serde_json="1.0"
bincode = "^2.0.0-rc.2" bincode = "^2.0.0-rc.2"
zip="0.6" zip= {version = "0.6", optional = true}
fmmap = { version = "0.3", features = ["tokio-async"] , optional = true} fmmap = { version = "0.3", features = ["tokio-async"] , optional = true}
tokio = { version = "1", features = ["fs","io-util","rt", "macros"] , optional = true} tokio = { version = "1", features = ["fs","io-util","rt", "macros"] , optional = true}
futures-core = {version = "0.3", optional = true} futures-core = {version = "0.3", optional = true}

View File

@ -3,4 +3,5 @@ pub mod async_db;
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub mod db; pub mod db;
pub mod questions; pub mod questions;
#[cfg(feature = "source")]
pub mod source; pub mod source;

View File

@ -1,7 +1,5 @@
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use crate::source::{SourceQuestion, SourceQuestionsBatch};
macro_rules! make { macro_rules! make {
($Target:ident; by {$($field:ident),+}; from $src:expr) => {$Target {$( ($Target:ident; by {$($field:ident),+}; from $src:expr) => {$Target {$(
$field: $src.$field $field: $src.$field
@ -80,57 +78,67 @@ pub struct Question {
pub batch_info: BatchInfo, pub batch_info: BatchInfo,
} }
impl From<SourceQuestion> for Question { #[cfg(feature = "source")]
fn from(src: SourceQuestion) -> Self { pub mod convert {
make! {Self; with defaults and by { use crate::source::{SourceQuestion, SourceQuestionsBatch};
num, id, description, answer, author, comment, comment1, tour, url,
date, processed_by, redacted_by, copyright, theme, kind, source, rating use super::{BatchInfo, Question};
}; from src}
impl From<SourceQuestion> for Question {
fn from(src: SourceQuestion) -> Self {
make! {Self; with defaults and by {
num, id, description, answer, author, comment, comment1, tour, url,
date, processed_by, redacted_by, copyright, theme, kind, source, rating
}; from src}
}
} }
}
impl From<SourceQuestionsBatch> for BatchInfo { impl From<SourceQuestionsBatch> for BatchInfo {
fn from(src: SourceQuestionsBatch) -> Self { fn from(src: SourceQuestionsBatch) -> Self {
make! {Self; by { make! {Self; by {
filename, description, author, comment, url, date, filename, description, author, comment, url, date,
processed_by, redacted_by, copyright, theme, kind, source, rating processed_by, redacted_by, copyright, theme, kind, source, rating
}; from src} }; from src}
}
} }
}
impl From<SourceQuestionsBatch> for Vec<Question> { impl From<SourceQuestionsBatch> for Vec<Question> {
fn from(src: SourceQuestionsBatch) -> Self { fn from(src: SourceQuestionsBatch) -> Self {
let mut result: Vec<Question> = src let mut result: Vec<Question> = src
.questions .questions
.iter() .iter()
.map(|item| item.clone().into()) .map(|item| item.clone().into())
.collect(); .collect();
let batch_info = BatchInfo::from(src); let batch_info = BatchInfo::from(src);
result.iter_mut().for_each(|question| { result.iter_mut().for_each(|question| {
question.batch_info = batch_info.clone(); question.batch_info = batch_info.clone();
});
result
}
}
pub trait QuestionsConverter {
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a>;
}
impl<T> QuestionsConverter for T
where
T: Iterator<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>,
{
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a> {
let iter = self
.filter(|(_, data)| data.is_ok())
.flat_map(|(filename, data)| {
let mut batch = data.unwrap();
batch.filename = filename;
let questions: Vec<Question> = batch.into();
questions
}); });
Box::new(iter)
result
}
}
pub trait QuestionsConverter {
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a>;
}
impl<T> QuestionsConverter for T
where
T: Iterator<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>,
{
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a> {
let iter = self
.filter(|(_, data)| data.is_ok())
.flat_map(|(filename, data)| {
let mut batch = data.unwrap();
batch.filename = filename;
let questions: Vec<Question> = batch.into();
questions
});
Box::new(iter)
}
} }
} }
#[cfg(feature = "source")]
pub use convert::QuestionsConverter;