/home/runner/work/feoxdb/feoxdb/src/core/store/range.rs
Line | Count | Source |
1 | | use crate::constants::MAX_KEY_SIZE; |
2 | | use crate::error::{FeoxError, Result}; |
3 | | |
4 | | use super::FeoxStore; |
5 | | |
6 | | impl FeoxStore { |
7 | | /// Perform a range query on the store. |
8 | | /// |
9 | | /// Returns all key-value pairs where the key is >= `start_key` and <= `end_key`. |
10 | | /// Both bounds are inclusive. |
11 | | /// |
12 | | /// # Arguments |
13 | | /// |
14 | | /// * `start_key` - Inclusive lower bound |
15 | | /// * `end_key` - Inclusive upper bound |
16 | | /// * `limit` - Maximum number of results to return |
17 | | /// |
18 | | /// # Returns |
19 | | /// |
20 | | /// Returns a vector of (key, value) pairs in sorted order. |
21 | | /// |
22 | | /// # Example |
23 | | /// |
24 | | /// ```rust |
25 | | /// # use feoxdb::FeoxStore; |
26 | | /// # fn main() -> feoxdb::Result<()> { |
27 | | /// # let store = FeoxStore::new(None)?; |
28 | | /// store.insert(b"user:001", b"Alice")?; |
29 | | /// store.insert(b"user:002", b"Bob")?; |
30 | | /// store.insert(b"user:003", b"Charlie")?; |
31 | | /// store.insert(b"user:004", b"David")?; |
32 | | /// |
33 | | /// // Get users 001 through 003 (inclusive) |
34 | | /// let results = store.range_query(b"user:001", b"user:003", 10)?; |
35 | | /// assert_eq!(results.len(), 3); |
36 | | /// # Ok(()) |
37 | | /// # } |
38 | | /// ``` |
39 | 4 | pub fn range_query( |
40 | 4 | &self, |
41 | 4 | start_key: &[u8], |
42 | 4 | end_key: &[u8], |
43 | 4 | limit: usize, |
44 | 4 | ) -> Result<Vec<(Vec<u8>, Vec<u8>)>> { |
45 | 4 | if start_key.len() > MAX_KEY_SIZE || end_key.len() > MAX_KEY_SIZE { |
46 | 0 | return Err(FeoxError::InvalidKeySize); |
47 | 4 | } |
48 | | |
49 | 4 | let mut results = Vec::new(); |
50 | | |
51 | 20 | for entry in self.tree4 .range4 (start_key4 .to_vec4 ()..=end_key.to_vec()) { |
52 | 20 | if results.len() >= limit { |
53 | 1 | break; |
54 | 19 | } |
55 | | |
56 | 19 | let record = entry.value(); |
57 | 19 | let value = if let Some(val8 ) = record.get_value() { |
58 | 8 | val.to_vec() |
59 | | } else { |
60 | 11 | self.load_value_from_disk(record)?0 |
61 | | }; |
62 | | |
63 | 19 | results.push((entry.key().clone(), value)); |
64 | | } |
65 | | |
66 | 4 | Ok(results) |
67 | 4 | } |
68 | | } |