Skip to content

Commit ba5ede9

Browse files
gkorlandgavrie
authored andcommitted
Initial commit of search
1 parent 14bd967 commit ba5ede9

File tree

6 files changed

+136
-39
lines changed

6 files changed

+136
-39
lines changed

Cargo.lock

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ edition = "2018"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
bson = "0.13.0"
11+
bson = "0.13"
1212
serde_json = "1.0"
1313
libc = "0.2"
1414
jsonpath_lib = { git="https://github.com/gkorland/jsonpath.git", branch="reaplce_with_ownership_parser" }
1515
redismodule = { git="https://github.com/redislabsmodules/redismodule-rs.git", branch="master" }
16+
redisearch_api = { git = "https://github.com/RediSearch/redisearch-api-rs.git", branch="master" }

src/error.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use jsonpath_lib::JsonPathError;
2+
3+
#[derive(Debug)]
4+
pub struct Error {
5+
pub msg: String,
6+
}
7+
8+
impl From<String> for Error {
9+
fn from(e: String) -> Self {
10+
Error { msg: e }
11+
}
12+
}
13+
14+
impl From<&str> for Error {
15+
fn from(e: &str) -> Self {
16+
Error { msg: e.to_string() }
17+
}
18+
}
19+
20+
impl From<serde_json::Error> for Error {
21+
fn from(e: serde_json::Error) -> Self {
22+
Error { msg: e.to_string() }
23+
}
24+
}
25+
26+
impl From<JsonPathError> for Error {
27+
fn from(e: JsonPathError) -> Self {
28+
Error {
29+
msg: format!("{:?}", e),
30+
}
31+
}
32+
}
33+
34+
impl From<Error> for redismodule::RedisError {
35+
fn from(e: Error) -> Self {
36+
redismodule::RedisError::String(e.msg)
37+
}
38+
}

src/lib.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
#[macro_use]
22
extern crate redismodule;
33

4+
use redisearch_api;
45
use redismodule::native_types::RedisType;
56
use redismodule::{Context, NextArg, RedisError, RedisResult, RedisValue, REDIS_OK};
67
use serde_json::{Number, Value};
78
use std::{i64, usize};
89

910
mod backward;
11+
mod error;
1012
mod index;
1113
mod nodevisitor;
1214
mod redisjson;
15+
mod redisjsonschema;
1316

1417
use crate::index::Index;
15-
use crate::redisjson::{Error, Format, RedisJSON, SetOptions};
18+
use crate::redisjson::{Format, RedisJSON, SetOptions};
19+
use crate::error::{Error};
1620

1721
static JSON_TYPE_ENCODING_VERSION: i32 = 2;
1822
static JSON_TYPE_NAME: &str = "ReJSON-RL";
@@ -34,6 +38,26 @@ static REDIS_JSON_TYPE: RedisType = RedisType::new(
3438
},
3539
);
3640

41+
static JSON_SCHEMA_ENCODING_VERSION: i32 = 1;
42+
static JSON_SCHEMA_NAME: &str = "ReJSON-SCHEMA";
43+
44+
static REDIS_JSON_SCHEMA_TYPE: RedisType = RedisType::new(
45+
JSON_SCHEMA_NAME,
46+
JSON_SCHEMA_ENCODING_VERSION,
47+
raw::RedisModuleTypeMethods {
48+
version: raw::REDISMODULE_TYPE_METHOD_VERSION as u64,
49+
50+
rdb_load: Some(redisjsonschema::json_schema_rdb_load),
51+
rdb_save: Some(redisjsonschema::json_schema_rdb_save),
52+
aof_rewrite: None, // TODO add support
53+
free: Some(redisjsonschema::json_schema_free),
54+
55+
// Currently unused by Redis
56+
mem_usage: None,
57+
digest: None,
58+
},
59+
);
60+
3761
///
3862
/// Backwards compatibility convertor for RedisJSON 1.x clients
3963
///
@@ -697,6 +721,10 @@ fn json_len<F: Fn(&RedisJSON, &String) -> Result<usize, Error>>(
697721
Ok(length)
698722
}
699723

724+
fn json_createindex(_ctx: &Context, _args: Vec<String>) -> RedisResult {
725+
Err("Command was not implemented".into())
726+
}
727+
700728
fn json_cache_info(_ctx: &Context, _args: Vec<String>) -> RedisResult {
701729
Err("Command was not implemented".into())
702730
}
@@ -711,6 +739,7 @@ redis_module! {
711739
version: 1,
712740
data_types: [
713741
REDIS_JSON_TYPE,
742+
REDIS_JSON_SCHEMA_TYPE
714743
],
715744
commands: [
716745
["json.del", json_del, "write"],
@@ -734,6 +763,7 @@ redis_module! {
734763
["json.debug", json_debug, ""],
735764
["json.forget", json_del, "write"],
736765
["json.resp", json_resp, ""],
766+
["json.createindex", json_createindex, "write"],
737767
["json._cacheinfo", json_cache_info, ""],
738768
["json._cacheinit", json_cache_init, "write"],
739769
],

src/redisjson.rs

+2-37
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
// It can be operated on (e.g. INCR) and serialized back to JSON.
66

77
use crate::backward;
8+
use crate::error::Error;
89
use crate::nodevisitor::NodeVisitorImpl;
10+
911
use bson::decode_document;
1012
use jsonpath_lib::{JsonPathError, SelectorMut};
1113
use redismodule::raw;
@@ -14,50 +16,13 @@ use std::io::Cursor;
1416
use std::mem;
1517
use std::os::raw::{c_int, c_void};
1618

17-
#[derive(Debug)]
18-
pub struct Error {
19-
msg: String,
20-
}
21-
2219
#[derive(Debug, PartialEq)]
2320
pub enum SetOptions {
2421
NotExists,
2522
AlreadyExists,
2623
None,
2724
}
2825

29-
impl From<String> for Error {
30-
fn from(e: String) -> Self {
31-
Error { msg: e }
32-
}
33-
}
34-
35-
impl From<&str> for Error {
36-
fn from(e: &str) -> Self {
37-
Error { msg: e.to_string() }
38-
}
39-
}
40-
41-
impl From<serde_json::Error> for Error {
42-
fn from(e: serde_json::Error) -> Self {
43-
Error { msg: e.to_string() }
44-
}
45-
}
46-
47-
impl From<JsonPathError> for Error {
48-
fn from(e: JsonPathError) -> Self {
49-
Error {
50-
msg: format!("{:?}", e),
51-
}
52-
}
53-
}
54-
55-
impl From<Error> for redismodule::RedisError {
56-
fn from(e: Error) -> Self {
57-
redismodule::RedisError::String(e.msg)
58-
}
59-
}
60-
6126
#[derive(Debug, PartialEq)]
6227
pub enum Format {
6328
JSON,

src/redisjsonschema.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use redismodule::raw;
2+
use std::os::raw::{c_int, c_void};
3+
use crate::error::Error;
4+
5+
#[derive(Debug)]
6+
pub struct RedisJSONSchema {
7+
data: String
8+
}
9+
10+
impl RedisJSONSchema {
11+
pub fn from_str(data: &str) -> Result<Self, Error> {
12+
// let value = RedisJSON::parse_str(data, format)?;
13+
Ok(Self {data: data.to_string()})
14+
}
15+
}
16+
17+
#[allow(non_snake_case, unused)]
18+
pub unsafe extern "C" fn json_schema_rdb_load(rdb: *mut raw::RedisModuleIO, encver: c_int) -> *mut c_void {
19+
if encver < 2 {
20+
panic!("Can't load old RedisJSONSchema RDB"); // TODO add support for backward
21+
}
22+
let json = RedisJSONSchema::from_str(&raw::load_string(rdb)).unwrap();
23+
Box::into_raw(Box::new(json)) as *mut c_void
24+
}
25+
26+
#[allow(non_snake_case, unused)]
27+
#[no_mangle]
28+
pub unsafe extern "C" fn json_schema_free(value: *mut c_void) {
29+
Box::from_raw(value as *mut RedisJSONSchema);
30+
}
31+
32+
#[allow(non_snake_case, unused)]
33+
#[no_mangle]
34+
pub unsafe extern "C" fn json_schema_rdb_save(rdb: *mut raw::RedisModuleIO, value: *mut c_void) {
35+
let schema = &*(value as *mut RedisJSONSchema);
36+
raw::save_string(rdb, &schema.data.to_string());
37+
}

0 commit comments

Comments
 (0)