use anyhow::Result;

use super::{FragReader, ReadRangeScope};
use crate::{reader::shard_reader::ShardReaderStat, utils::key::ShardingKeyRange};

impl FragReader {
    pub fn estimated_live_doc_count_in_ranges(
        &self,
        read_ranges: &[ShardingKeyRange],
    ) -> Result<u64> {
        let mut stat = ShardReaderStat::default();
        // estimate_count ranks fragments by this value before running the
        // actual count query, so the estimator should stay cheap and monotonic.
        match self.classify_read_scope(read_ranges, &mut stat) {
            ReadRangeScope::Disjoint => Ok(0),
            ReadRangeScope::Subset => Ok(self.live_doc_count()),
            // TODO: estimate overlap live doc count by read_ranges.
            ReadRangeScope::Overlap => Ok(self.live_doc_count()),
        }
    }
}
