|
585 | 585 | //! |
586 | 586 | //! When working with databases that have sorted keys, you might want to iterate over a specific range |
587 | 587 | //! of keys starting from a particular key. Heed provides the [`Database::range`] method that accepts |
588 | | -//! any type implementing [`RangeBounds`]. |
| 588 | +//! any type implementing [`std::ops::RangeBounds`]. |
589 | 589 | //! |
590 | | -//! Specifically, if your keys are of type [`Bytes`] and you have a `Vec<u8>` as your starting key |
591 | | -//! you can use that in in a [`Database::range`] call like this: |
| 590 | +//! Specifically, if your keys are of type [`heed::types::Bytes`] and you have a `Vec<u8>` as your starting key, |
| 591 | +//! because a tuple of [`std::ops::Bound`] implements such trait you can just use that: |
592 | 592 | //! |
593 | 593 | //! ``` |
594 | 594 | //! use std::error::Error; |
|
599 | 599 | //! use heed::types::*; |
600 | 600 | //! use heed::{Database, DatabaseFlags, EnvOpenOptions}; |
601 | 601 | //! |
602 | | -//! struct BytesRange(Vec<u8>); |
603 | | -//! |
604 | | -//! impl std::ops::RangeBounds<[u8]> for BytesRange { |
605 | | -//! fn start_bound(&self) -> std::ops::Bound<&[u8]> { |
606 | | -//! std::ops::Bound::Included(&self.0) |
607 | | -//! } |
608 | | -//! |
609 | | -//! fn end_bound(&self) -> std::ops::Bound<&[u8]> { |
610 | | -//! std::ops::Bound::Unbounded |
611 | | -//! } |
612 | | -//! } |
613 | | -//! |
614 | 602 | //! fn main() -> Result<(), Box<dyn Error>> { |
615 | 603 | //! let path = tempfile::tempdir()?; |
616 | 604 | //! |
617 | 605 | //! let env = unsafe { |
618 | 606 | //! EnvOpenOptions::new() |
619 | 607 | //! .map_size(1 << 34) |
620 | | -//! .max_dbs(8) |
| 608 | +//! .max_dbs(1) |
621 | 609 | //! .open(&path)? |
622 | 610 | //! }; |
623 | 611 | //! |
|
627 | 615 | //! .database_options() |
628 | 616 | //! .types::<Bytes, U64<NativeEndian>>() |
629 | 617 | //! .name("index") |
630 | | -//! .flags(DatabaseFlags::DUP_SORT | DatabaseFlags::DUP_FIXED) |
631 | 618 | //! .create(&mut wtxn)?; |
632 | 619 | //! |
633 | 620 | //! db.put(&mut wtxn, &vec![1, 2, 3, 3], &55555u64)?; |
|
639 | 626 | //! let txn = env.read_txn()?; |
640 | 627 | //! let key: Vec<u8> = vec![1, 2, 3, 4]; |
641 | 628 | //! |
642 | | -//! for result in db.range(&txn, &BytesRange(key))? { |
| 629 | +//! for res in db |
| 630 | +//! .range( |
| 631 | +//! &txn, |
| 632 | +//! &( |
| 633 | +//! std::ops::Bound::Included(key.as_slice()), |
| 634 | +//! std::ops::Bound::Unbounded, |
| 635 | +//! ), |
| 636 | +//! )? { |
643 | 637 | //! let (k, v) = result?; |
644 | 638 | //! println!("Key: {:?}, Value: {}", k, v); |
645 | 639 | //! } |
|
0 commit comments