Skip to content

Commit

Permalink
Use BTreeMap and BTreeSet in addition to HashMap and HashSet. (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
MeirShpilraien authored May 11, 2023
1 parent 2cba2be commit 82c4753
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
10 changes: 5 additions & 5 deletions examples/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use redis_module::{
redis_module, redisvalue::RedisValueKey, Context, NextArg, RedisError, RedisResult,
RedisString, RedisValue,
};
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet};

fn map_mget(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
if args.len() < 2 {
Expand All @@ -19,14 +19,14 @@ fn map_mget(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
let res = match values {
None => RedisValue::Null,
Some(values) => {
let mut map: HashMap<RedisValueKey, RedisValue> = HashMap::with_capacity(fields.len());
let mut map: BTreeMap<RedisValueKey, RedisValue> = BTreeMap::new();
for (field, value) in values.into_iter() {
map.insert(
RedisValueKey::BulkRedisString(field),
RedisValue::BulkRedisString(value),
);
}
RedisValue::Map(map)
RedisValue::OrderedMap(map)
}
};

Expand All @@ -48,11 +48,11 @@ fn map_unique(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
let res = match values {
None => RedisValue::Null,
Some(values) => {
let mut set: HashSet<RedisValueKey> = HashSet::new();
let mut set: BTreeSet<RedisValueKey> = BTreeSet::new();
for (_, value) in values.into_iter() {
set.insert(RedisValueKey::BulkRedisString(value));
}
RedisValue::Set(set)
RedisValue::OrderedSet(set)
}
};

Expand Down
20 changes: 20 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,17 @@ impl Context {
raw::Status::Ok
}

Ok(RedisValue::OrderedMap(map)) => {
raw::reply_with_map(self.ctx, map.len() as c_long);

for (key, value) in map {
self.reply_with_key(key);
self.reply(Ok(value));
}

raw::Status::Ok
}

Ok(RedisValue::Set(set)) => {
raw::reply_with_set(self.ctx, set.len() as c_long);
set.into_iter().for_each(|e| {
Expand All @@ -475,6 +486,15 @@ impl Context {
raw::Status::Ok
}

Ok(RedisValue::OrderedSet(set)) => {
raw::reply_with_set(self.ctx, set.len() as c_long);
set.into_iter().for_each(|e| {
self.reply_with_key(e);
});

raw::Status::Ok
}

Ok(RedisValue::Null) => raw::reply_with_null(self.ctx),

Ok(RedisValue::NoReply) => raw::Status::Ok,
Expand Down
6 changes: 4 additions & 2 deletions src/redisvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::{
CallReply, RedisError, RedisString,
};
use std::{
collections::{HashMap, HashSet},
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
hash::Hash,
};

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)]
pub enum RedisValueKey {
Integer(i64),
String(String),
Expand All @@ -32,6 +32,8 @@ pub enum RedisValue {
StaticError(&'static str),
Map(HashMap<RedisValueKey, RedisValue>),
Set(HashSet<RedisValueKey>),
OrderedMap(BTreeMap<RedisValueKey, RedisValue>),
OrderedSet(BTreeSet<RedisValueKey>),
Null,
NoReply, // No reply at all (as opposed to a Null reply)
}
Expand Down

0 comments on commit 82c4753

Please sign in to comment.