Skip to main content

hive_router_plan_executor/response/
storage.rs

1use bytes::Bytes;
2
3pub struct ResponsesStorage {
4    responses: Vec<Bytes>,
5}
6
7impl Default for ResponsesStorage {
8    fn default() -> Self {
9        Self::new()
10    }
11}
12
13impl ResponsesStorage {
14    pub fn new() -> Self {
15        Self {
16            responses: Vec::new(),
17        }
18    }
19
20    pub fn add_response(&mut self, response: Bytes) {
21        self.responses.push(response);
22    }
23
24    pub fn estimate_final_response_size(&self) -> usize {
25        let total_size: usize = self.responses.iter().map(|r| r.len()).sum();
26        // Add a 20% buffer to account for JSON syntax, escaping, and other overhead.
27        // I tested a bunch of numbers and it was the best from the bunch.
28        (total_size as f64 * 1.2) as usize
29    }
30}