Skip to content

Commit 82c4753

Browse files
author
Meir Shpilraien (Spielrein)
authored
Use BTreeMap and BTreeSet in addition to HashMap and HashSet. (#327)
The main problem with HashMap and HashSet is that those are unordered. So we get results in different order each time. Using BTreeMap and BTreeSet we will get the results in the same order each time.
1 parent 2cba2be commit 82c4753

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

examples/response.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use redis_module::{
22
redis_module, redisvalue::RedisValueKey, Context, NextArg, RedisError, RedisResult,
33
RedisString, RedisValue,
44
};
5-
use std::collections::{HashMap, HashSet};
5+
use std::collections::{BTreeMap, BTreeSet};
66

77
fn map_mget(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
88
if args.len() < 2 {
@@ -19,14 +19,14 @@ fn map_mget(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
1919
let res = match values {
2020
None => RedisValue::Null,
2121
Some(values) => {
22-
let mut map: HashMap<RedisValueKey, RedisValue> = HashMap::with_capacity(fields.len());
22+
let mut map: BTreeMap<RedisValueKey, RedisValue> = BTreeMap::new();
2323
for (field, value) in values.into_iter() {
2424
map.insert(
2525
RedisValueKey::BulkRedisString(field),
2626
RedisValue::BulkRedisString(value),
2727
);
2828
}
29-
RedisValue::Map(map)
29+
RedisValue::OrderedMap(map)
3030
}
3131
};
3232

@@ -48,11 +48,11 @@ fn map_unique(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
4848
let res = match values {
4949
None => RedisValue::Null,
5050
Some(values) => {
51-
let mut set: HashSet<RedisValueKey> = HashSet::new();
51+
let mut set: BTreeSet<RedisValueKey> = BTreeSet::new();
5252
for (_, value) in values.into_iter() {
5353
set.insert(RedisValueKey::BulkRedisString(value));
5454
}
55-
RedisValue::Set(set)
55+
RedisValue::OrderedSet(set)
5656
}
5757
};
5858

src/context/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,17 @@ impl Context {
466466
raw::Status::Ok
467467
}
468468

469+
Ok(RedisValue::OrderedMap(map)) => {
470+
raw::reply_with_map(self.ctx, map.len() as c_long);
471+
472+
for (key, value) in map {
473+
self.reply_with_key(key);
474+
self.reply(Ok(value));
475+
}
476+
477+
raw::Status::Ok
478+
}
479+
469480
Ok(RedisValue::Set(set)) => {
470481
raw::reply_with_set(self.ctx, set.len() as c_long);
471482
set.into_iter().for_each(|e| {
@@ -475,6 +486,15 @@ impl Context {
475486
raw::Status::Ok
476487
}
477488

489+
Ok(RedisValue::OrderedSet(set)) => {
490+
raw::reply_with_set(self.ctx, set.len() as c_long);
491+
set.into_iter().for_each(|e| {
492+
self.reply_with_key(e);
493+
});
494+
495+
raw::Status::Ok
496+
}
497+
478498
Ok(RedisValue::Null) => raw::reply_with_null(self.ctx),
479499

480500
Ok(RedisValue::NoReply) => raw::Status::Ok,

src/redisvalue.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use crate::{
33
CallReply, RedisError, RedisString,
44
};
55
use std::{
6-
collections::{HashMap, HashSet},
6+
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
77
hash::Hash,
88
};
99

10-
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
10+
#[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)]
1111
pub enum RedisValueKey {
1212
Integer(i64),
1313
String(String),
@@ -32,6 +32,8 @@ pub enum RedisValue {
3232
StaticError(&'static str),
3333
Map(HashMap<RedisValueKey, RedisValue>),
3434
Set(HashSet<RedisValueKey>),
35+
OrderedMap(BTreeMap<RedisValueKey, RedisValue>),
36+
OrderedSet(BTreeSet<RedisValueKey>),
3537
Null,
3638
NoReply, // No reply at all (as opposed to a Null reply)
3739
}

0 commit comments

Comments
 (0)