···
// let mut ts = CLOCK.now();
405
-
let mut current_item_count = 0;
406
-
let map_block = move |(key, val)| {
405
+
let map_block = move |(res, current_item_count)| -> AppResult<(Option<_>, usize)> {
if current_item_count >= max_items {
407
+
return Ok((None, current_item_count));
409
+
let (key, val) = res?;
let mut key_reader = Cursor::new(key);
let start_timestamp = key_reader.read_varint::<u64>()?;
// let end_timestamp = key_reader.read_varint::<u64>()?;
···
// "stopped at block with timestamps {start_timestamp}..{end_timestamp} because {start_limit} is greater"
417
+
return Ok((None, current_item_count));
let decoder = handle::ItemDecoder::new(Cursor::new(val), start_timestamp)?;
420
-
current_item_count += decoder.item_count();
420
+
let current_item_count = current_item_count + decoder.item_count();
// "took {}ns to get block with size {}",
// ts.elapsed().as_nanos(),
429
-
.take_while(move |item| {
430
-
item.as_ref().map_or(true, |item| {
431
-
item.timestamp <= end_limit && item.timestamp >= start_limit
430
+
.take_while(move |item| {
431
+
item.as_ref().map_or(true, |item| {
432
+
item.timestamp <= end_limit && item.timestamp >= start_limit
434
-
.map(|res| res.map_err(AppError::from)),
435
+
.map(|res| res.map_err(AppError::from)),
437
+
current_item_count,
438
-
let blocks = handle
441
+
let (blocks, counted) = handle
443
+
.map(|res| res.map_err(AppError::from))
441
-
.map_while(move |res| res.map_err(AppError::from).and_then(map_block).transpose())
446
+
(Vec::with_capacity(20), 0),
447
+
|(mut blocks, current_item_count), res| {
448
+
use itertools::FoldWhile::*;
450
+
match map_block((res, current_item_count)) {
451
+
Ok((Some(block), current_item_count)) => {
452
+
blocks.push(Ok(block));
453
+
Continue((blocks, current_item_count))
455
+
Ok((None, current_item_count)) => Done((blocks, current_item_count)),
457
+
blocks.push(Err(err));
458
+
Done((blocks, current_item_count))
445
-
"got blocks with size {}, item count {current_item_count}",
466
+
"got blocks with size {}, item count {counted}",