Skip to content

Commit

Permalink
Revert "61 regex perf (besok#62)"
Browse files Browse the repository at this point in the history
This reverts commit a07c7b6.
  • Loading branch information
xMAC94x committed May 17, 2024
1 parent c1e3221 commit 8254333
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 449 deletions.
96 changes: 48 additions & 48 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
* **`0.1.0`**
* Initial implementation
* **`0.1.1`**
* Technical improvements
* **`0.1.2`**
* added a trait to obtain the result from value
* added a method to get the cloned as Value
* change the name of the general method*
* **`0.1.4`**
* add an ability to use references instead of values
* fix some clippy issues
* **`0.1.5`**
* correct grammar for `$.[..]`
* **`0.1.6`**
* add logical OR and logical And to filters
* fix bugs with objects in filters
* add internal macros to generate path objects
* **`0.2.0`**
* add json path value as a result for the library
* add functions (size)
* change a logical operator `size` into function `size()`
* **`0.2.1`**
* changed the contract for length() function.
* **`0.2.2`**
* add ..*
* **`0.2.5`**
* build for tags
* **`0.2.6`**
* make parser mod public
* **`0.3.0`**
* introduce the different behaviour for empty results and non-existing result
* **`0.3.2`**
* make jsonpath inst cloneable.
* **`0.3.3`**
* fix a bug with the logical operators
* **`0.3.4`**
* add a result as a path
* **`0.3.5`**
* add `!` negation operation in filters
* allow using () in filters
* **`0.5`**
* add config for jsonpath
* add an option to add a regex cache for boosting performance
* **`0.5.1`**
* add double quotes for the expressions (before it was only possible to use single quotes)
* add Debug on the JsonPathFinder


- **`0.1.0`**
- Initial implementation
- **`0.1.1`**
- Technical improvements
- **`0.1.2`**
- added a trait to obtain the result from value
- added a method to get the cloned as Value
- change the name of the general method\*
- **`0.1.4`**
- add an ability to use references instead of values
- fix some clippy issues
- **`0.1.5`**
- correct grammar for `$.[..]`
- **`0.1.6`**
- add logical OR and logical And to filters
- fix bugs with objects in filters
- add internal macros to generate path objects
- **`0.2.0`**
- add json path value as a result for the library
- add functions (size)
- change a logical operator `size` into function `size()`
- **`0.2.1`**
- changed the contract for length() function.
- **`0.2.2`**
- add ..\*
- **`0.2.5`**
- build for tags
- **`0.2.6`**
- make parser mod public
- **`0.3.0`**
- introduce the different behaviour for empty results and non-existing result
- **`0.3.2`**
- make jsonpath inst cloneable.
- **`0.3.3`**
- fix a bug with the logical operators
- **`0.3.4`**
- add a result as a path
- **`0.3.5`**
- add `!` negation operation in filters
- allow using () in filters
- **`0.5`**
- add config for jsonpath
- add an option to add a regex cache for boosting performance
- **`0.5.1`**
- add double quotes for the expressions (before it was only possible to use single quotes)
- add Debug on the JsonPathFinder
- **`0.6`**
- allow to reuse regex, that improves performance without needing an internal cache
10 changes: 2 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "jsonpath-rust"
description = "The library provides the basic functionality to find the set of the data according to the filtering query."
version = "0.5.1"
version = "0.6.0"
authors = ["BorisZhguchev <zhguchev@gmail.com>"]
edition = "2018"
license-file = "LICENSE"
Expand All @@ -17,12 +17,6 @@ regex = "1"
pest = "2.0"
pest_derive = "2.0"
thiserror = "1.0.50"
lazy_static = "1.4"
once_cell = "1.19.0"

[dev-dependencies]
criterion = "0.5.1"

[[bench]]
name = "regex_bench"
harness = false
lazy_static = "1.0"
43 changes: 0 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,50 +389,7 @@ fn test() {
** If the value has been modified during the search, there is no way to find a path of a new value.
It can happen if we try to find a length() of array, for in stance.**

## Configuration

The JsonPath provides a wat to configure the search by using `JsonPathConfig`.

```rust
pub fn main() {
let cfg = JsonPathConfig::new(RegexCache::Implemented(DefaultRegexCacheInst::default()));
}
```

### Regex cache
The configuration provides an ability to use a regex cache to improve the [performance](https://github.com/besok/jsonpath-rust/issues/61)

To instantiate the cache needs to use `RegexCache` enum with the implementation of the trait `RegexCacheInst`.
Default implementation `DefaultRegexCacheInst` uses `Arc<Mutex<HashMap<String,Regex>>>`.
The pair of Box<Value> or Value and config can be used:
```rust
pub fn main(){
let cfg = JsonPathConfig::new(RegexCache::Implemented(DefaultRegexCacheInst::default()));
let json = Box::new(json!({
"author":"abcd(Rees)",
}));

let _v = (json, cfg).path("$.[?(@.author ~= '.*(?i)d\\(Rees\\)')]")
.expect("the path is correct");


}
```
or using `JsonPathFinder` :

```rust
fn main() {
let cfg = JsonPathConfig::new(RegexCache::Implemented(DefaultRegexCacheInst::default()));
let finder = JsonPathFinder::from_str_with_cfg(
r#"{"first":{"second":[{"active":1},{"passive":1}]}}"#,
"$.first.second[?(@.active)]",
cfg,
).unwrap();
let slice_of_data: Vec<&Value> = finder.find_slice();
let js = json!({"active":1});
assert_eq!(slice_of_data, vec![JsonPathValue::Slice(&js, "$.first.second[0]".to_string())]);
}
```

## The structure

Expand Down
40 changes: 0 additions & 40 deletions benches/regex_bench.rs

This file was deleted.

60 changes: 10 additions & 50 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@

use crate::parser::model::JsonPath;
use crate::parser::parser::parse_json_path;
use crate::path::config::JsonPathConfig;
use crate::path::{json_path_instance, PathInstance};
use serde_json::Value;
use std::convert::TryInto;
Expand Down Expand Up @@ -184,12 +183,8 @@ impl FromStr for JsonPathInst {
}

impl JsonPathInst {
pub fn find_slice<'a>(
&'a self,
value: &'a Value,
cfg: JsonPathConfig,
) -> Vec<JsonPtr<'a, Value>> {
json_path_instance(&self.inner, value, cfg)
pub fn find_slice<'a>(&'a self, value: &'a Value) -> Vec<JsonPtr<'a, Value>> {
json_path_instance(&self.inner, value)
.find(JsonPathValue::from_root(value))
.into_iter()
.filter(|v| v.has_value())
Expand Down Expand Up @@ -230,27 +225,13 @@ impl JsonPathQuery for Box<Value> {
}
}

impl JsonPathQuery for (Box<Value>, JsonPathConfig) {
fn path(self, query: &str) -> Result<Value, String> {
let p = JsonPathInst::from_str(query)?;
Ok(JsonPathFinder::new_with_cfg(self.0, Box::new(p), self.1).find())
}
}

impl JsonPathQuery for Value {
fn path(self, query: &str) -> Result<Value, String> {
let p = JsonPathInst::from_str(query)?;
Ok(JsonPathFinder::new(Box::new(self), Box::new(p)).find())
}
}

impl JsonPathQuery for (Value, JsonPathConfig) {
fn path(self, query: &str) -> Result<Value, String> {
let p = JsonPathInst::from_str(query)?;
Ok(JsonPathFinder::new_with_cfg(Box::new(self.0), Box::new(p), self.1).find())
}
}

/// just to create a json path value of data
/// Example:
/// - json_path_value(&json) = `JsonPathValue::Slice(&json)`
Expand Down Expand Up @@ -314,7 +295,6 @@ type JsPathStr = String;
pub(crate) fn jsp_idx(prefix: &str, idx: usize) -> String {
format!("{}[{}]", prefix, idx)
}

pub(crate) fn jsp_obj(prefix: &str, key: &str) -> String {
format!("{}.['{}']", prefix, key)
}
Expand Down Expand Up @@ -358,7 +338,7 @@ impl<'a, Data: Clone + Debug + Default> JsonPathValue<'a, Data> {
}

impl<'a, Data> JsonPathValue<'a, Data> {
fn only_no_value(input: &[JsonPathValue<'a, Data>]) -> bool {
fn only_no_value(input: &Vec<JsonPathValue<'a, Data>>) -> bool {
!input.is_empty() && input.iter().filter(|v| v.has_value()).count() == 0
}
fn map_vec(data: Vec<(&'a Data, JsPathStr)>) -> Vec<JsonPathValue<'a, Data>> {
Expand Down Expand Up @@ -428,7 +408,6 @@ impl<'a, Data> JsonPathValue<'a, Data> {
pub struct JsonPathFinder {
json: Box<Value>,
path: Box<JsonPathInst>,
cfg: JsonPathConfig,
}

impl Debug for JsonPathFinder {
Expand All @@ -445,20 +424,7 @@ impl Debug for JsonPathFinder {
impl JsonPathFinder {
/// creates a new instance of [JsonPathFinder]
pub fn new(json: Box<Value>, path: Box<JsonPathInst>) -> Self {
JsonPathFinder {
json,
path,
cfg: JsonPathConfig::default(),
}
}

pub fn new_with_cfg(json: Box<Value>, path: Box<JsonPathInst>, cfg: JsonPathConfig) -> Self {
JsonPathFinder { json, path, cfg }
}

/// sets a cfg with a new one
pub fn set_cfg(&mut self, cfg: JsonPathConfig) {
self.cfg = cfg
JsonPathFinder { json, path }
}

/// updates a path with a new one
Expand Down Expand Up @@ -486,15 +452,10 @@ impl JsonPathFinder {
let path = Box::new(JsonPathInst::from_str(path)?);
Ok(JsonPathFinder::new(json, path))
}
pub fn from_str_with_cfg(json: &str, path: &str, cfg: JsonPathConfig) -> Result<Self, String> {
let json = serde_json::from_str(json).map_err(|e| e.to_string())?;
let path = Box::new(JsonPathInst::from_str(path)?);
Ok(JsonPathFinder::new_with_cfg(json, path, cfg))
}

/// creates an instance to find a json slice from the json
pub fn instance(&self) -> PathInstance {
json_path_instance(&self.path.inner, &self.json, self.cfg.clone())
json_path_instance(&self.path.inner, &self.json)
}
/// finds a slice of data in the set json.
/// The result is a vector of references to the incoming structure.
Expand Down Expand Up @@ -545,7 +506,6 @@ impl JsonPathFinder {

#[cfg(test)]
mod tests {
use crate::path::config::JsonPathConfig;
use crate::JsonPathQuery;
use crate::JsonPathValue::{NoValue, Slice};
use crate::{jp_v, JsonPathFinder, JsonPathInst, JsonPathValue};
Expand Down Expand Up @@ -1246,7 +1206,7 @@ mod tests {
let query = JsonPathInst::from_str("$..book[?(@.author size 10)].title")
.expect("the path is correct");

let results = query.find_slice(&json, JsonPathConfig::default());
let results = query.find_slice(&json);
let v = results.first().expect("to get value");

// V can be implicitly converted to &Value
Expand Down Expand Up @@ -1309,7 +1269,7 @@ mod tests {
v,
vec![Slice(
&json!({"second":{"active": 1}}),
"$.['first']".to_string(),
"$.['first']".to_string()
)]
);

Expand All @@ -1323,7 +1283,7 @@ mod tests {
v,
vec![Slice(
&json!({"second":{"active": 1}}),
"$.['first']".to_string(),
"$.['first']".to_string()
)]
);

Expand All @@ -1337,7 +1297,7 @@ mod tests {
v,
vec![Slice(
&json!({"second":{"active": 1}}),
"$.['first']".to_string(),
"$.['first']".to_string()
)]
);

Expand All @@ -1351,7 +1311,7 @@ mod tests {
v,
vec![Slice(
&json!({"second":{"active": 1}}),
"$.['first']".to_string(),
"$.['first']".to_string()
)]
);
}
Expand Down
16 changes: 0 additions & 16 deletions src/path/config.rs

This file was deleted.

Loading

0 comments on commit 8254333

Please sign in to comment.