Skip to content

Commit

Permalink
Refactor JSONB functions: Improved API, Documentation, and Data Struc…
Browse files Browse the repository at this point in the history
…tures (#75)

* Refactor JSONB functions api and data structures

* fix clippy

* refactor RawJsonb and OwendJsonb

* split function files

* fix clippy

* add comments
  • Loading branch information
b41sh authored Jan 4, 2025
1 parent e461dc8 commit 7694b92
Show file tree
Hide file tree
Showing 23 changed files with 6,283 additions and 3,971 deletions.
12 changes: 7 additions & 5 deletions benches/get_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ fn jsonb_get(data: &[u8], paths: &[&str], expected: &str) {
.map(|p| jsonb::jsonpath::Path::DotField(std::borrow::Cow::Borrowed(p)))
.collect::<Vec<_>>();
let json_path = jsonb::jsonpath::JsonPath { paths };
let mode = jsonb::jsonpath::Mode::Mixed;

let mut result_data = vec![];
let mut result_offsets = vec![];
let raw_jsonb = jsonb::RawJsonb::new(data);
let result_jsonb = raw_jsonb.get_by_path_opt(&json_path, mode).unwrap();
assert!(result_jsonb.is_some());
let result_jsonb = result_jsonb.unwrap();
let result_raw_jsonb = result_jsonb.as_raw();

jsonb::get_by_path(data, json_path, &mut result_data, &mut result_offsets).unwrap();

let s = jsonb::as_str(&result_data).unwrap();
let s = result_raw_jsonb.as_str().unwrap().unwrap();
assert_eq!(s, expected);
}

Expand Down
8 changes: 4 additions & 4 deletions benches/strip_nulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::{fs, io::Read};

use criterion::{criterion_group, criterion_main, Criterion};
use jsonb::{from_slice, strip_nulls, Value};
use jsonb::{from_slice, Value};

fn read(file: &str) -> Vec<u8> {
let mut f = fs::File::open(file).unwrap();
Expand Down Expand Up @@ -50,9 +50,9 @@ fn strip_value_nulls(val: &mut Value<'_>) {
}

fn strip_nulls_fast(data: &[u8]) {
let mut buf = Vec::new();
strip_nulls(data, &mut buf).unwrap();
assert!(!buf.is_empty());
let raw_jsonb = jsonb::RawJsonb::new(data);
let result_jsonb = raw_jsonb.strip_nulls().unwrap();
assert!(!result_jsonb.is_empty());
}

fn add_benchmark(c: &mut Criterion) {
Expand Down
4 changes: 2 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ use super::value::Value;
/// This structure can be nested. Each group of structures starts with a `Header`.
/// The upper-level `Value` will store the `Header` length or offset of
/// the lower-level `Value`.
///
/// `Header` stores the type of the `Value`, include `Array`, `Object` and `Scalar`,
/// `Scalar` has only one `Value`, and a corresponding `JEntry`.
/// `Array` and `Object` are nested type, they have multiple lower-level `Values`.
/// So the `Header` also stores the number of lower-level `Values`.
///
/// `JEntry` stores the types of `Scalar Value`, including `Null`, `True`, `False`,
/// `Number`, `String` and `Container`. They have three different decode methods.
/// 1. `Null`, `True` and `False` can be obtained by `JEntry`, no extra work required.
Expand Down
14 changes: 7 additions & 7 deletions src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,25 @@ from_float! {
f32 f64
}

impl<'a> From<OrderedFloat<f32>> for Value<'a> {
impl From<OrderedFloat<f32>> for Value<'_> {
fn from(f: OrderedFloat<f32>) -> Self {
Value::Number(Number::Float64(f.0 as f64))
}
}

impl<'a> From<OrderedFloat<f64>> for Value<'a> {
impl From<OrderedFloat<f64>> for Value<'_> {
fn from(f: OrderedFloat<f64>) -> Self {
Value::Number(Number::Float64(f.0))
}
}

impl<'a> From<bool> for Value<'a> {
impl From<bool> for Value<'_> {
fn from(f: bool) -> Self {
Value::Bool(f)
}
}

impl<'a> From<String> for Value<'a> {
impl From<String> for Value<'_> {
fn from(f: String) -> Self {
Value::String(f.into())
}
Expand Down Expand Up @@ -142,13 +142,13 @@ impl<'a, K: Into<String>, V: Into<Value<'a>>> FromIterator<(K, V)> for Value<'a>
}
}

impl<'a> From<()> for Value<'a> {
impl From<()> for Value<'_> {
fn from((): ()) -> Self {
Value::Null
}
}

impl<'a> From<&JsonValue> for Value<'a> {
impl From<&JsonValue> for Value<'_> {
fn from(value: &JsonValue) -> Self {
match value {
JsonValue::Null => Value::Null,
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<'a> From<&JsonValue> for Value<'a> {
}
}

impl<'a> From<JsonValue> for Value<'a> {
impl From<JsonValue> for Value<'_> {
fn from(value: JsonValue) -> Self {
(&value).into()
}
Expand Down
Loading

0 comments on commit 7694b92

Please sign in to comment.