为什么Redis中没有有序的hashmap? [英] Why there is no ordered hashmap in Redis?
问题描述
Redis 数据类型 包括 排序集 和其他用于键值存储的必要数据结构.但我想知道为什么它没有像 Java 的 TreeMap
或 C++ 的 std::map
那样的排序映射.我认为底层数据结构将与排序集大致相似,因为两者都应该是平衡的二叉搜索树.
必须有一些用例,我们必须根据键以特定顺序存储键值对.但是当前的 sorted set 仅用于根据 score 存储 key 的目的.
必须有一些用例,我们必须根据键以特定顺序存储键值对
由于Redis键是二进制字符串,我假设您提到的具体顺序是字典顺序(具体来说,键与memcmp
函数进行比较).在这种情况下,您可以使用 SORTED SET
轻松实现 C++ 的 std::map
.您可以通过 2 个步骤实现此目的:
使用 Redis 的排序集构建 std::set
如果 SORTED SET
中的 2 个元素具有相同的分数,则它们按字典顺序排列.因此,为了构建一个 std::set
,只需给 SORTED SET
中的所有成员分配相同的分数:
zadd std::set 0 czadd std::set 0 azadd std::set 0 b//因为所有这些成员都有相同的分数,//结果按字典顺序排列://a b czrange std::set 0 -1//以下命令将失败,因为 'c' 已经存在.zadd std::set 0 c
从 Redis 2.8 开始,它支持一些命令来操作词典范围,以便您可以构建类似于 std::set::lower_bound
或 std::set::upper_bound
//类似于lower_bound:查找不小于b的所有成员zrangebylex std::set [b +//类似于upper_bound:查找所有大于b的成员zrangebylex std::set (b +
<块引用>
用一个值映射集合中的每个键
既然你已经得到了一个std::set
,那么把key映射到一个值,你就可以得到一个std::map
.
设置一个值_a设置 b value_b设置 c value_c
<块引用>
将这两个步骤结合在一起
您可以将整个工作包装到一个 lua 脚本中以具有 内置 std::map
.并像这样使用它:
redis-cli --eval map.lua map_name ,键值
Redis Data types includes sorted set and other necessary data-structures for key-value storage. But I wonder why it doesn't have any sorted map like Java's TreeMap
or C++'s std::map
. I think the underlying data-structure would be mostly similar of sorted set as both are supposed to be balanced binary search tree.
There must be some use-cases where we have to store key-value pair in specific order according to key. But current sorted set only serves the purpose of storing key according to score.
There must be some use-cases where we have to store key-value pair in specific order according to key
Since Redis keys are binary strings, I assume that the specific order you mentioned, is lexicographical order (specifically, keys are compared with the memcmp
function). In that case, you can easily implement a C++'s std::map
with SORTED SET
. You can achieve this with 2 steps:
Build a std::set with Redis' Sorted Set
If 2 elements in a SORTED SET
have the same score, they're ordered in lexicographical order. So in order to build a std::set
, just give all members in a SORTED SET
with the same score:
zadd std::set 0 c
zadd std::set 0 a
zadd std::set 0 b
// since all these members have the same score,
// the result is lexicographical ordered:
// a b c
zrange std::set 0 -1
// the following command will fail, since 'c' already exists.
zadd std::set 0 c
Since Redis 2.8, it supports some commands to operate on the lexicographical ranges, so that you can build something similar to std::set::lower_bound
, or std::set::upper_bound
// something similar to lower_bound: find all members not less than b
zrangebylex std::set [b +
// something similar to upper_bound: find all members greater than b
zrangebylex std::set (b +
Map each key in the set with a value
Since you already get a std::set
, then map the key with a value, you can get a std::map
.
set a value_a
set b value_b
set c value_c
Combine these 2 steps together
You can wrap the whole work into a lua script to have a built-in std::map
. And use it like this:
redis-cli --eval map.lua map_name , key value
这篇关于为什么Redis中没有有序的hashmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!