mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-04-25 09:22:19 +00:00
lua 프로젝트 레이아웃 변경
This commit is contained in:
parent
ea7599c012
commit
b5c5d6b88e
@ -27,7 +27,8 @@ array_tool = "1.0.3"
|
||||
[lib]
|
||||
name = "jsonpath_lib"
|
||||
path = "src/lib.rs"
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[profile.release]
|
||||
#debug = true
|
||||
#lto = false
|
||||
#lto = false
|
@ -1,25 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
# http://luajit.org/index.html
|
||||
|
||||
cargo clean && \
|
||||
cargo build --release
|
||||
|
||||
export JSONPATH_LIB_PATH="${PWD}/../../target/release"
|
||||
export LUA_PATH="${PWD}/../../lua/?.lua;"
|
||||
|
||||
echo
|
||||
time cargo run --release -- 1000
|
||||
echo
|
||||
time luajit example.lua 1000
|
||||
echo
|
||||
time cargo run --release -- 5000
|
||||
echo
|
||||
time luajit example.lua 5000
|
||||
echo
|
||||
time cargo run --release -- 10000
|
||||
echo
|
||||
time luajit example.lua 10000
|
||||
|
@ -1,13 +1,14 @@
|
||||
[package]
|
||||
name = "lua_vs_rust_benchmark"
|
||||
name = "jsonpath_lua"
|
||||
version = "0.1.0"
|
||||
authors = ["Changseok Han <freestrings@gmail.com>"]
|
||||
license = "MIT"
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
jsonpath_lib = { path = "../../" }
|
||||
jsonpath_lib = { path = "../" }
|
||||
|
||||
[[bin]]
|
||||
name = "lua_vs_rust_benchmark"
|
||||
path = "example.rs"
|
||||
name = "bench"
|
||||
path = "bench_lua_vs_rust/example.rs"
|
||||
|
25
lua/bench_lua_vs_rust.sh
Executable file
25
lua/bench_lua_vs_rust.sh
Executable file
@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
# http://luajit.org/index.html
|
||||
|
||||
#cargo clean && \
|
||||
cargo build --release
|
||||
|
||||
export JSONPATH_LIB_PATH="${PWD}/target/release/deps"
|
||||
export LUA_PATH="${PWD}/?.lua;"
|
||||
|
||||
echo
|
||||
time cargo run --release --bin bench -- 1000
|
||||
echo
|
||||
time luajit bench_lua_vs_rust/example.lua 1000
|
||||
echo
|
||||
time cargo run --release --bin bench -- 5000
|
||||
echo
|
||||
time luajit bench_lua_vs_rust/example.lua 5000
|
||||
echo
|
||||
time cargo run --release --bin bench -- 10000
|
||||
echo
|
||||
time luajit bench_lua_vs_rust/example.lua 10000
|
||||
|
@ -9,12 +9,12 @@ end
|
||||
|
||||
print(string.format("%s - %u", "lua iter", iter));
|
||||
|
||||
local file = io.open("../../benchmark/example.json", "r");
|
||||
local file = io.open("../benchmark/example.json", "r");
|
||||
io.input(file)
|
||||
local data = io.read("*a");
|
||||
io.close(file);
|
||||
local cb = compile("$..book[?(@.price<30 && @.category==\"fiction\")]");
|
||||
local template = compile("$..book[?(@.price<30 && @.category==\"fiction\")]");
|
||||
for i = 0, iter do
|
||||
local r = cb(data);
|
||||
local r = template(data);
|
||||
-- print(r);
|
||||
end
|
@ -14,7 +14,7 @@ fn read_json(path: &str) -> String {
|
||||
}
|
||||
|
||||
fn get_string() -> String {
|
||||
read_json("../../benchmark/example.json")
|
||||
read_json("../benchmark/example.json")
|
||||
}
|
||||
|
||||
fn get_json() -> Value {
|
@ -8,10 +8,10 @@ else
|
||||
ext = 'dylib'
|
||||
end
|
||||
|
||||
ffi.cdef[[
|
||||
ffi.cdef [[
|
||||
const char* ffi_select(const char *json_str, const char *path);
|
||||
void *ffi_path_compile(const char *path);
|
||||
const char* ffi_select_with_compiled(void *ptr, const char *json_str);
|
||||
const char* ffi_select_with_compiled_path(void *ptr, const char *json_str);
|
||||
]]
|
||||
|
||||
local jsonpathLibPath = os.getenv("JSONPATH_LIB_PATH");
|
||||
@ -20,6 +20,6 @@ local jsonpath = ffi.load(jsonpathLibPath .. '/libjsonpath_lib.' .. ext);
|
||||
function compile(path)
|
||||
local compiledPath = jsonpath.ffi_path_compile(path);
|
||||
return function(jsonStr)
|
||||
return ffi.string(jsonpath.ffi_select_with_compiled(compiledPath, jsonStr));
|
||||
return ffi.string(jsonpath.ffi_select_with_compiled_path(compiledPath, jsonStr));
|
||||
end
|
||||
end
|
57
src/ffi/mod.rs
Normal file
57
src/ffi/mod.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_void};
|
||||
|
||||
use {parser, select, select_as_str};
|
||||
|
||||
const INVALID_PATH: &str = "invalid path";
|
||||
const INVALID_JSON: &str = "invalud json";
|
||||
|
||||
fn to_str(v: *const c_char, err_msg: &str) -> &str {
|
||||
unsafe { CStr::from_ptr(v) }.to_str().expect(err_msg)
|
||||
}
|
||||
|
||||
fn to_char_ptr(v: &str) -> *const c_char {
|
||||
let s = CString::new(v).expect(&format!("invalid string: {}", v));
|
||||
let ptr = s.as_ptr();
|
||||
std::mem::forget(s);
|
||||
ptr
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ffi_select(json_str: *const c_char, path: *const c_char) -> *const c_char {
|
||||
let json_str = to_str(json_str, INVALID_JSON);
|
||||
let path = to_str(path, INVALID_PATH);
|
||||
match select_as_str(json_str, path) {
|
||||
Ok(v) => to_char_ptr(v.as_str()),
|
||||
Err(e) => {
|
||||
panic!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ffi_path_compile(path: *const c_char) -> *mut c_void {
|
||||
let path = to_str(path, INVALID_PATH);
|
||||
let ref_node = Box::into_raw(Box::new(parser::Parser::compile(path).unwrap()));
|
||||
let ptr = ref_node as *mut c_void;
|
||||
std::mem::forget(ref_node);
|
||||
ptr
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ffi_select_with_compiled_path(
|
||||
path_ptr: *mut c_void,
|
||||
json_ptr: *const c_char,
|
||||
) -> *const c_char {
|
||||
let node = unsafe { Box::from_raw(path_ptr as *mut parser::Node) };
|
||||
let json_str = to_str(json_ptr, INVALID_JSON);
|
||||
let json = serde_json::from_str(json_str).expect(&format!("invalid json string: {}", json_str));
|
||||
|
||||
let mut selector = select::Selector::default();
|
||||
let found = selector.compiled_path(&node).value(&json).select().unwrap();
|
||||
std::mem::forget(node);
|
||||
|
||||
let result =
|
||||
serde_json::to_string(&found).expect(&format!("json serialize error: {:?}", found));
|
||||
to_char_ptr(result.as_str())
|
||||
}
|
72
src/lib.rs
72
src/lib.rs
@ -135,9 +135,9 @@ use serde_json::Value;
|
||||
pub use parser::Parser;
|
||||
pub use select::JsonPathError;
|
||||
pub use select::{Selector, SelectorMut};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_void};
|
||||
|
||||
#[doc(hidden)]
|
||||
mod ffi;
|
||||
#[doc(hidden)]
|
||||
mod parser;
|
||||
#[doc(hidden)]
|
||||
@ -468,71 +468,3 @@ where
|
||||
let value = selector.str_path(path)?.value(value).replace_with(fun)?;
|
||||
Ok(value.take().unwrap_or(Value::Null))
|
||||
}
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ffi_select(json_str: *const c_char, path: *const c_char) -> *const c_char {
|
||||
let json_str = match unsafe { CStr::from_ptr(json_str) }.to_str() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
panic!("{:?}", e);
|
||||
}
|
||||
};
|
||||
|
||||
let path = match unsafe { CStr::from_ptr(path) }.to_str() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
panic!("{:?}", e);
|
||||
}
|
||||
};
|
||||
|
||||
match select_as_str(json_str, path) {
|
||||
Ok(v) => match serde_json::to_string(&v) {
|
||||
Ok(s) => {
|
||||
let s = CString::new(s.as_str()).unwrap();
|
||||
let p = s.as_ptr();
|
||||
std::mem::forget(s);
|
||||
p
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("{:?}", e);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
panic!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ffi_path_compile(path: *const c_char) -> *mut c_void {
|
||||
let path = match unsafe { CStr::from_ptr(path) }.to_str() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
panic!("{:?}", e);
|
||||
}
|
||||
};
|
||||
|
||||
Box::into_raw(Box::new(Parser::compile(path).unwrap())) as *mut c_void
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ffi_select_with_compiled(
|
||||
path_ptr: *mut c_void,
|
||||
json_ptr: *const c_char,
|
||||
) -> *const c_char {
|
||||
let node = unsafe { Box::from_raw(path_ptr as *mut parser::Node) };
|
||||
let json_str = unsafe { CStr::from_ptr(json_ptr) }.to_str().expect("invalid 'json_ptr' input");
|
||||
let json = serde_json::from_str(json_str).expect(&format!("invalid json string: {}", json_str));
|
||||
|
||||
let mut selector = Selector::default();
|
||||
let found = selector.compiled_path(&node).value(&json).select().unwrap();
|
||||
std::mem::forget(node);
|
||||
|
||||
let result = serde_json::to_string(&found).expect(&format!("json serialize error: {:?}", found));
|
||||
let result_cstring = CString::new(result.as_str()).expect(&format!("empty result: {:?}", result));
|
||||
let result_ptr = result_cstring.as_ptr();
|
||||
std::mem::forget(result_cstring);
|
||||
result_ptr
|
||||
}
|
||||
|
@ -2,10 +2,10 @@ use std::collections::HashSet;
|
||||
use std::fmt;
|
||||
|
||||
use array_tool::vec::{Intersect, Union};
|
||||
use serde_json::map::Entry;
|
||||
use serde_json::{Number, Value};
|
||||
|
||||
use parser::*;
|
||||
use serde_json::map::Entry;
|
||||
|
||||
fn to_f64(n: &Number) -> f64 {
|
||||
if n.is_i64() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user