move enumerate to reader_task
This commit is contained in:
parent
17446a6318
commit
f275069f23
37
src/main.rs
37
src/main.rs
@ -63,7 +63,15 @@ fn reader_task(tx: mpsc::Sender<Question>) {
|
|||||||
let zip_reader = io::BufReader::new(zip_file);
|
let zip_reader = io::BufReader::new(zip_file);
|
||||||
let archive = zip::ZipArchive::new(zip_reader).unwrap();
|
let archive = zip::ZipArchive::new(zip_reader).unwrap();
|
||||||
let mut source_questions = archive.source_questions();
|
let mut source_questions = archive.source_questions();
|
||||||
for question in source_questions.convert() {
|
|
||||||
|
let questions = source_questions
|
||||||
|
.convert()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(num, mut question)| {
|
||||||
|
question.num = 1 + num as u32;
|
||||||
|
question
|
||||||
|
});
|
||||||
|
for question in questions {
|
||||||
let res = tx.send(question);
|
let res = tx.send(question);
|
||||||
if res.is_err() {
|
if res.is_err() {
|
||||||
break;
|
break;
|
||||||
@ -95,22 +103,20 @@ fn db_writer_task(rx: mpsc::Receiver<Question>) {
|
|||||||
let collection = storage.collection("questions").unwrap();
|
let collection = storage.collection("questions").unwrap();
|
||||||
|
|
||||||
let mut count: usize = 0;
|
let mut count: usize = 0;
|
||||||
let count = &mut count;
|
|
||||||
rx.into_iter().for_each(|question| {
|
rx.into_iter().for_each(|question| {
|
||||||
let result = collection.insert(&question);
|
collection.insert(question).expect("insert question");
|
||||||
if result.is_err() {
|
count += 1;
|
||||||
println!("-- {:#?}", question);
|
|
||||||
panic!("{:#?}", result);
|
|
||||||
} else {
|
|
||||||
*count += 1;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
println!("inserted {}\nwriting...", count);
|
println!("inserted {count}");
|
||||||
|
|
||||||
|
println!("syncing to disk...");
|
||||||
storage.sync(true).unwrap();
|
storage.sync(true).unwrap();
|
||||||
|
|
||||||
print!("stats: ");
|
print!("stats: ");
|
||||||
let stats = storage.stat().unwrap();
|
let stats = storage.stat().unwrap();
|
||||||
println!("{:?}", stats);
|
println!("{:?}", stats);
|
||||||
|
|
||||||
drop(storage);
|
drop(storage);
|
||||||
println!("write done");
|
println!("write done");
|
||||||
}
|
}
|
||||||
@ -238,7 +244,7 @@ fn main() {
|
|||||||
fn read_from_db2(id: u32) -> Option<Question> {
|
fn read_from_db2(id: u32) -> Option<Question> {
|
||||||
let mut reader: db::Reader<Question> =
|
let mut reader: db::Reader<Question> =
|
||||||
db::Reader::new(NEW_DB_FILENAME, 2048).expect("new db reader");
|
db::Reader::new(NEW_DB_FILENAME, 2048).expect("new db reader");
|
||||||
|
|
||||||
let mut questions = reader.iter();
|
let mut questions = reader.iter();
|
||||||
|
|
||||||
match id {
|
match id {
|
||||||
@ -264,15 +270,8 @@ fn db_writer2_task(rx: mpsc::Receiver<Question>) {
|
|||||||
let mut writer: db::Writer<Question> =
|
let mut writer: db::Writer<Question> =
|
||||||
db::Writer::new(NEW_DB_FILENAME, writer_opts).expect("new db writer");
|
db::Writer::new(NEW_DB_FILENAME, writer_opts).expect("new db writer");
|
||||||
|
|
||||||
let mut num = 1;
|
|
||||||
let mut iter = rx.iter().map(|mut q| {
|
|
||||||
q.num = num;
|
|
||||||
num += 1;
|
|
||||||
q
|
|
||||||
});
|
|
||||||
|
|
||||||
writer
|
writer
|
||||||
.load(&mut iter)
|
.load(&mut rx.iter())
|
||||||
.unwrap_or_else(|e| panic!("db writer load, {e:#?}"));
|
.unwrap_or_else(|e| panic!("db writer load, {e:#?}"));
|
||||||
|
|
||||||
writer.finish().expect("db writer finish");
|
writer.finish().expect("db writer finish");
|
||||||
|
Loading…
Reference in New Issue
Block a user