Skip to content

Commit

Permalink
Update to new redismodule-rs and redisearch-api-rs releases (#231)
Browse files Browse the repository at this point in the history
Update to new redismodule-rs and redisearch-api-rs releases
  • Loading branch information
gavrie committed Oct 14, 2020
1 parent 5d76cd6 commit 9088750
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
36 changes: 32 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ serde_json = "1.0"
serde = "1.0"
libc = "0.2"
jsonpath_lib = { git="https://github.com/RedisJSON/jsonpath.git", branch="public-parser" }
redis-module = { version="0.9.1", features = ["experimental-api"]}
redisearch_api = "0.4.4"
redis-module = { version="0.11", features = ["experimental-api"]}
redisearch_api = "0.5"

[features]
# Workaround to allow cfg(feature = "test") in redismodue-rs dependencies:
Expand Down
31 changes: 18 additions & 13 deletions src/commands/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::thread;

use serde_json::{Map, Value};

use redis_module::{Context, NextArg, RedisError, RedisResult, RedisValue, REDIS_OK};
use redis_module::{
Context, NextArg, RedisError, RedisResult, RedisValue, ThreadSafeContext, REDIS_OK,
};

use redisearch_api::{Document, FieldType, TagOptions};

Expand Down Expand Up @@ -59,7 +61,7 @@ pub fn add_field(index_name: &str, field_name: &str, path: &str) -> RedisResult
};

if schema.fields.contains_key(field_name) {
Err("Field already exists".into())
Err(RedisError::Str("Field already exists"))
} else {
schema
.index
Expand All @@ -72,7 +74,7 @@ pub fn add_field(index_name: &str, field_name: &str, path: &str) -> RedisResult
fn del_schema(index_name: &str) -> RedisResult {
match schema_map::as_mut().remove(index_name) {
Some(_) => REDIS_OK,
None => Err("Index not found".into()),
None => Err(RedisError::Str("Index not found")),
}
}

Expand Down Expand Up @@ -149,12 +151,13 @@ where
return; // TODO handle this case
};

let ctx = Context::get_thread_safe_context();
let ts_ctx = ThreadSafeContext::new();
let mut cursor: u64 = 0;
loop {
ctx.lock();
let res = scan_and_index(&ctx, &schema, cursor);
ctx.unlock();
let res = {
let ts_ctx = ts_ctx.lock();
scan_and_index(&*ts_ctx, &schema, cursor)
};

match res {
Ok(c) => cursor = c,
Expand All @@ -178,7 +181,9 @@ where
Ok(res)
}
//"INFO" => {}
_ => Err("ERR unknown subcommand - try `JSON.INDEX HELP`".into()),
_ => Err(RedisError::Str(
"ERR unknown subcommand - try `JSON.INDEX HELP`",
)),
}
}

Expand All @@ -201,18 +206,18 @@ fn scan_and_index(ctx: &Context, schema: &Schema, cursor: u64) -> Result<u64, Re
}
Ok(())
} else {
Err("Error on get value from key".into())
Err(RedisError::Str("Error on get value from key"))
}
})
} else {
Err("Error on parsing reply from scan".into())
Err(RedisError::Str("Error on parsing reply from scan"))
}
});
res.map(|_| cursor)
}
_ => Err("Error on parsing reply from scan".into()),
_ => Err(RedisError::Str("Error on parsing reply from scan")),
},
_ => Err("Error on parsing reply from scan".into()),
_ => Err(RedisError::Str("Error on parsing reply from scan")),
}
}

Expand All @@ -230,7 +235,7 @@ where
let map = schema_map::as_ref();

map.get(&index_name)
.ok_or_else(|| "ERR no such index".into())
.ok_or_else(|| RedisError::Str("ERR no such index"))
.map(|schema| &schema.index)
.and_then(|index| {
let result =
Expand Down
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
ctx.replicate_verbatim();
REDIS_OK
} else {
Err("ERR new objects must be created at the root".into())
Err(RedisError::Str(
"ERR new objects must be created at the root",
))
}
}
}
Expand Down Expand Up @@ -693,7 +695,9 @@ fn json_debug(ctx: &Context, args: Vec<String>) -> RedisResult {
];
Ok(results.into())
}
_ => Err("ERR unknown subcommand - try `JSON.DEBUG HELP`".into()),
_ => Err(RedisError::Str(
"ERR unknown subcommand - try `JSON.DEBUG HELP`",
)),
}
}

Expand Down Expand Up @@ -764,11 +768,11 @@ fn json_len<F: Fn(&RedisJSON, &String) -> Result<usize, Error>>(
}

fn json_cache_info(_ctx: &Context, _args: Vec<String>) -> RedisResult {
Err("Command was not implemented".into())
Err(RedisError::Str("Command was not implemented"))
}

fn json_cache_init(_ctx: &Context, _args: Vec<String>) -> RedisResult {
Err("Command was not implemented".into())
Err(RedisError::Str("Command was not implemented"))
}
//////////////////////////////////////////////////////

Expand Down

0 comments on commit 9088750

Please sign in to comment.