Skip to content
This repository was archived by the owner on Apr 20, 2020. It is now read-only.

Commit fe0bd8f

Browse files
committed
Merge branch 'redisearch_type_methods'
2 parents 14bd967 + 23ba26e commit fe0bd8f

File tree

7 files changed

+189
-74
lines changed

7 files changed

+189
-74
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
command: |
1616
sudo apt-get install -y software-properties-common
1717
sudo add-apt-repository -y ppa:chris-lea/redis-server
18-
sudo apt-get install -y clang python-pip redis-server
18+
sudo apt-get install -y clang llvm cmake python-pip redis-server
1919
2020
- run:
2121
name: Version information

Cargo.lock

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
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

Lines changed: 38 additions & 0 deletions
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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,50 @@
22
extern crate redismodule;
33

44
use redismodule::native_types::RedisType;
5+
use redismodule::raw as rawmod;
6+
use redismodule::raw::RedisModuleTypeMethods;
57
use redismodule::{Context, NextArg, RedisError, RedisResult, RedisValue, REDIS_OK};
68
use serde_json::{Number, Value};
79
use std::{i64, usize};
810

911
mod backward;
12+
mod error;
1013
mod index;
1114
mod nodevisitor;
1215
mod redisjson;
16+
mod schema;
1317

18+
use crate::error::Error;
1419
use crate::index::Index;
15-
use crate::redisjson::{Error, Format, RedisJSON, SetOptions};
16-
17-
static JSON_TYPE_ENCODING_VERSION: i32 = 2;
18-
static JSON_TYPE_NAME: &str = "ReJSON-RL";
20+
use crate::redisjson::{Format, RedisJSON, SetOptions};
1921

2022
static REDIS_JSON_TYPE: RedisType = RedisType::new(
21-
JSON_TYPE_NAME,
22-
JSON_TYPE_ENCODING_VERSION,
23-
raw::RedisModuleTypeMethods {
24-
version: raw::REDISMODULE_TYPE_METHOD_VERSION as u64,
23+
"ReJSON-RL",
24+
2,
25+
RedisModuleTypeMethods {
26+
version: redismodule::TYPE_METHOD_VERSION,
2527

26-
rdb_load: Some(redisjson::json_rdb_load),
27-
rdb_save: Some(redisjson::json_rdb_save),
28+
rdb_load: Some(redisjson::type_methods::rdb_load),
29+
rdb_save: Some(redisjson::type_methods::rdb_save),
2830
aof_rewrite: None, // TODO add support
29-
free: Some(redisjson::json_free),
31+
free: Some(redisjson::type_methods::free),
32+
33+
// Currently unused by Redis
34+
mem_usage: None,
35+
digest: None,
36+
},
37+
);
38+
39+
static REDIS_JSON_SCHEMA_TYPE: RedisType = RedisType::new(
40+
"ReJSON-SC",
41+
1,
42+
RedisModuleTypeMethods {
43+
version: redismodule::TYPE_METHOD_VERSION,
44+
45+
rdb_load: Some(schema::type_methods::rdb_load),
46+
rdb_save: Some(schema::type_methods::rdb_save),
47+
aof_rewrite: None, // TODO add support
48+
free: Some(schema::type_methods::free),
3049

3150
// Currently unused by Redis
3251
mem_usage: None,
@@ -697,6 +716,10 @@ fn json_len<F: Fn(&RedisJSON, &String) -> Result<usize, Error>>(
697716
Ok(length)
698717
}
699718

719+
fn json_createindex(ctx: &Context, args: Vec<String>) -> RedisResult {
720+
Err("Command was not implemented".into())
721+
}
722+
700723
fn json_cache_info(_ctx: &Context, _args: Vec<String>) -> RedisResult {
701724
Err("Command was not implemented".into())
702725
}
@@ -706,12 +729,18 @@ fn json_cache_init(_ctx: &Context, _args: Vec<String>) -> RedisResult {
706729
}
707730
//////////////////////////////////////////////////////
708731

732+
pub extern "C" fn init(raw_ctx: *mut rawmod::RedisModuleCtx) -> c_int {
733+
redisearch_api::init(raw_ctx)
734+
}
735+
709736
redis_module! {
710737
name: "redisjson",
711738
version: 1,
712739
data_types: [
713740
REDIS_JSON_TYPE,
741+
REDIS_JSON_SCHEMA_TYPE
714742
],
743+
init: init,
715744
commands: [
716745
["json.del", json_del, "write"],
717746
["json.get", json_get, ""],
@@ -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

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,29 @@
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;
10-
use jsonpath_lib::{JsonPathError, SelectorMut};
12+
use jsonpath_lib::SelectorMut;
1113
use redismodule::raw;
12-
use serde_json::{Map, Value};
14+
use serde_json::Value;
1315
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,
6429
BSON,
6530
}
66-
6731
impl Format {
6832
pub fn from_str(s: &str) -> Result<Format, Error> {
6933
match s {
@@ -362,27 +326,29 @@ impl RedisJSON {
362326
}
363327
}
364328

365-
#[allow(non_snake_case, unused)]
366-
pub unsafe extern "C" fn json_rdb_load(rdb: *mut raw::RedisModuleIO, encver: c_int) -> *mut c_void {
367-
let json = match encver {
368-
0 => RedisJSON {
369-
data: backward::json_rdb_load(rdb),
370-
},
371-
2 => RedisJSON::from_str(&raw::load_string(rdb), Format::JSON).unwrap(),
372-
_ => panic!("Can't load old RedisJSON RDB"),
373-
};
374-
Box::into_raw(Box::new(json)) as *mut c_void
375-
}
329+
pub mod type_methods {
330+
use super::*;
331+
332+
#[allow(non_snake_case, unused)]
333+
pub unsafe extern "C" fn rdb_load(rdb: *mut raw::RedisModuleIO, encver: c_int) -> *mut c_void {
334+
let json = match encver {
335+
0 => RedisJSON {
336+
data: backward::json_rdb_load(rdb),
337+
},
338+
2 => RedisJSON::from_str(&raw::load_string(rdb), Format::JSON).unwrap(),
339+
_ => panic!("Can't load old RedisJSON RDB"),
340+
};
341+
Box::into_raw(Box::new(json)) as *mut c_void
342+
}
376343

377-
#[allow(non_snake_case, unused)]
378-
#[no_mangle]
379-
pub unsafe extern "C" fn json_free(value: *mut c_void) {
380-
Box::from_raw(value as *mut RedisJSON);
381-
}
344+
#[allow(non_snake_case, unused)]
345+
pub unsafe extern "C" fn free(value: *mut c_void) {
346+
Box::from_raw(value as *mut RedisJSON);
347+
}
382348

383-
#[allow(non_snake_case, unused)]
384-
#[no_mangle]
385-
pub unsafe extern "C" fn json_rdb_save(rdb: *mut raw::RedisModuleIO, value: *mut c_void) {
386-
let json = &*(value as *mut RedisJSON);
387-
raw::save_string(rdb, &json.data.to_string());
349+
#[allow(non_snake_case, unused)]
350+
pub unsafe extern "C" fn rdb_save(rdb: *mut raw::RedisModuleIO, value: *mut c_void) {
351+
let json = &*(value as *mut RedisJSON);
352+
raw::save_string(rdb, &json.data.to_string());
353+
}
388354
}

src/schema.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::error::Error;
2+
use redisearch_api::Index;
3+
use redismodule::raw;
4+
use std::os::raw::{c_int, c_void};
5+
6+
pub struct RedisJSONSchema {
7+
index: Index,
8+
}
9+
10+
impl RedisJSONSchema {
11+
pub fn new(name: &str) -> RedisJSONSchema {
12+
RedisJSONSchema {
13+
index: Index::create(name),
14+
}
15+
}
16+
17+
pub fn from_str(data: &str) -> Result<Self, Error> {
18+
// let value = RedisJSON::parse_str(data, format)?;
19+
Ok(Self {
20+
// TODO better handle RDB read
21+
index: Index::create(data.to_string().as_str()),
22+
})
23+
}
24+
25+
pub fn add_index(&mut self, path: &str) -> Result<(), Error> {
26+
self.index.create_field(path);
27+
Ok(())
28+
}
29+
}
30+
31+
pub mod type_methods {
32+
use super::*;
33+
34+
#[allow(non_snake_case, unused)]
35+
pub unsafe extern "C" fn rdb_load(rdb: *mut raw::RedisModuleIO, encver: c_int) -> *mut c_void {
36+
if encver < 2 {
37+
panic!("Can't load old RedisJSONSchema RDB"); // TODO add support for backward
38+
}
39+
let json = RedisJSONSchema::from_str(&raw::load_string(rdb)).unwrap();
40+
Box::into_raw(Box::new(json)) as *mut c_void
41+
}
42+
43+
#[allow(non_snake_case, unused)]
44+
pub unsafe extern "C" fn free(value: *mut c_void) {
45+
Box::from_raw(value as *mut RedisJSONSchema);
46+
}
47+
48+
#[allow(non_snake_case, unused)]
49+
pub unsafe extern "C" fn rdb_save(rdb: *mut raw::RedisModuleIO, value: *mut c_void) {
50+
let schema = &*(value as *mut RedisJSONSchema);
51+
// TODO implement RDB write
52+
// raw::save_string(rdb, &schema.schema.to_string());
53+
}
54+
}

0 commit comments

Comments
 (0)