小编Fre*_*rén的帖子

在 Rust 中过滤/查询多键 btree 索引

我正在尝试使用 BTreeMap 作为内存数据库中的索引,具有多个键。像这样的东西:

let mut map = BTreeMap::new();
map.insert(("a".to_string(), "x".to_string()), "ax".to_string());
map.insert(("a".to_string(), "y".to_string()), "ay".to_string());
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,实际查询这个的最佳方法是什么?举例来说,我想获取(“a”,*),即所有以“a”作为第一个键,任何内容作为第二个键的条目。

我尝试过这样的事情:

use std::{collections::{BTreeMap}, cmp::Ordering};
use std::ops::Bound::{Included, Unbounded};

#[derive(Clone, Debug, Hash)]
pub enum StringKey {
    Exact(String),
    Any,
}
impl PartialOrd for StringKey {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (StringKey::Exact(a), StringKey::Exact(b)) => Some(a.cmp(b)),
            (StringKey::Exact(_), StringKey::Any) => Some(Ordering::Equal),
            (StringKey::Any, StringKey::Exact(_)) => Some(Ordering::Equal),
            (StringKey::Any, StringKey::Any) => Some(Ordering::Equal),
        }
    }
}
impl Ord for StringKey {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap() …
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
619
查看次数

标签 统计

rust ×1