mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-09 11:14:20 +01:00
Merge 'Random Text related cleanups' from Pekka Enberg
Just some random cleanups I did while attempting to optimizing things and failing. Closes #901
This commit is contained in:
@@ -188,16 +188,16 @@ pub fn json_remove(args: &[OwnedValue]) -> crate::Result<OwnedValue> {
|
||||
mod tests {
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::types::LimboText;
|
||||
use crate::types::Text;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn create_text(s: &str) -> OwnedValue {
|
||||
OwnedValue::Text(LimboText::new(Rc::new(s.to_string())))
|
||||
OwnedValue::Text(Text::from_str(s))
|
||||
}
|
||||
|
||||
fn create_json(s: &str) -> OwnedValue {
|
||||
OwnedValue::Text(LimboText::json(Rc::new(s.to_string())))
|
||||
OwnedValue::Text(Text::json(Rc::new(s.to_string())))
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::json::error::Error as JsonError;
|
||||
pub use crate::json::json_operations::{json_patch, json_remove};
|
||||
use crate::json::json_path::{json_path, JsonPath, PathElement};
|
||||
pub use crate::json::ser::to_string;
|
||||
use crate::types::{LimboText, OwnedValue, TextSubtype};
|
||||
use crate::types::{OwnedValue, Text, TextSubtype};
|
||||
use indexmap::IndexMap;
|
||||
use jsonb::Error as JsonbError;
|
||||
use ser::to_string_pretty;
|
||||
@@ -47,13 +47,13 @@ pub fn get_json(json_value: &OwnedValue, indent: Option<&str>) -> crate::Result<
|
||||
None => to_string(&json_val)?,
|
||||
};
|
||||
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(json))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(json))))
|
||||
}
|
||||
OwnedValue::Blob(b) => {
|
||||
// TODO: use get_json_value after we implement a single Struct
|
||||
// to represent both JSON and JSONB
|
||||
if let Ok(json) = jsonb::from_slice(b) {
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(json.to_string()))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(json.to_string()))))
|
||||
} else {
|
||||
crate::bail_parse_error!("malformed JSON");
|
||||
}
|
||||
@@ -66,7 +66,7 @@ pub fn get_json(json_value: &OwnedValue, indent: Option<&str>) -> crate::Result<
|
||||
None => to_string(&json_val)?,
|
||||
};
|
||||
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(json))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(json))))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,7 @@ pub fn json_array(values: &[OwnedValue]) -> crate::Result<OwnedValue> {
|
||||
}
|
||||
|
||||
s.push(']');
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(s))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(s))))
|
||||
}
|
||||
|
||||
pub fn json_array_length(
|
||||
@@ -166,7 +166,7 @@ pub fn json_set(json: &OwnedValue, values: &[OwnedValue]) -> crate::Result<Owned
|
||||
|
||||
if let Some(path) = path {
|
||||
let new_value = match value {
|
||||
OwnedValue::Text(LimboText {
|
||||
OwnedValue::Text(Text {
|
||||
value,
|
||||
subtype: TextSubtype::Text,
|
||||
}) => Val::String(value.to_string()),
|
||||
@@ -207,7 +207,7 @@ pub fn json_arrow_extract(value: &OwnedValue, path: &OwnedValue) -> crate::Resul
|
||||
if let Some(val) = extracted {
|
||||
let json = to_string(val)?;
|
||||
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(json))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(json))))
|
||||
} else {
|
||||
Ok(OwnedValue::Null)
|
||||
}
|
||||
@@ -271,7 +271,7 @@ pub fn json_extract(value: &OwnedValue, paths: &[OwnedValue]) -> crate::Result<O
|
||||
result.pop(); // remove the final comma
|
||||
result.push(']');
|
||||
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(result))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(result))))
|
||||
}
|
||||
|
||||
/// Returns a value with type defined by SQLite documentation:
|
||||
@@ -298,13 +298,13 @@ fn convert_json_to_db_type(extracted: &Val, all_as_db: bool) -> crate::Result<Ow
|
||||
Ok(OwnedValue::Integer(0))
|
||||
}
|
||||
}
|
||||
Val::String(s) => Ok(OwnedValue::Text(LimboText::new(Rc::new(s.clone())))),
|
||||
Val::String(s) => Ok(OwnedValue::Text(Text::from_str(s))),
|
||||
_ => {
|
||||
let json = to_string(&extracted)?;
|
||||
if all_as_db {
|
||||
Ok(OwnedValue::Text(LimboText::new(Rc::new(json))))
|
||||
Ok(OwnedValue::Text(Text::new(Rc::new(json))))
|
||||
} else {
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(json))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(json))))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -366,7 +366,7 @@ pub fn json_type(value: &OwnedValue, path: Option<&OwnedValue>) -> crate::Result
|
||||
Val::Removed => unreachable!(),
|
||||
};
|
||||
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(val.to_string()))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(val.to_string()))))
|
||||
}
|
||||
|
||||
/// Returns the value at the given JSON path. If the path does not exist, it returns None.
|
||||
@@ -651,7 +651,7 @@ pub fn json_object(values: &[OwnedValue]) -> crate::Result<OwnedValue> {
|
||||
.collect::<Result<IndexMap<String, Val>, _>>()?;
|
||||
|
||||
let result = crate::json::to_string(&value_map)?;
|
||||
Ok(OwnedValue::Text(LimboText::json(Rc::new(result))))
|
||||
Ok(OwnedValue::Text(Text::json(Rc::new(result))))
|
||||
}
|
||||
|
||||
pub fn is_json_valid(json_value: &OwnedValue) -> crate::Result<OwnedValue> {
|
||||
@@ -804,7 +804,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_json_array_simple() {
|
||||
let text = OwnedValue::build_text(Rc::new("value1".to_string()));
|
||||
let json = OwnedValue::Text(LimboText::json(Rc::new("\"value2\"".to_string())));
|
||||
let json = OwnedValue::Text(Text::json(Rc::new("\"value2\"".to_string())));
|
||||
let input = vec![text, json, OwnedValue::Integer(1), OwnedValue::Float(1.1)];
|
||||
|
||||
let result = json_array(&input).unwrap();
|
||||
@@ -1072,7 +1072,7 @@ mod tests {
|
||||
let text_key = OwnedValue::build_text(Rc::new("text_key".to_string()));
|
||||
let text_value = OwnedValue::build_text(Rc::new("text_value".to_string()));
|
||||
let json_key = OwnedValue::build_text(Rc::new("json_key".to_string()));
|
||||
let json_value = OwnedValue::Text(LimboText::json(Rc::new(
|
||||
let json_value = OwnedValue::Text(Text::json(Rc::new(
|
||||
r#"{"json":"value","number":1}"#.to_string(),
|
||||
)));
|
||||
let integer_key = OwnedValue::build_text(Rc::new("integer_key".to_string()));
|
||||
@@ -1108,7 +1108,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_json_object_json_value_is_rendered_as_json() {
|
||||
let key = OwnedValue::build_text(Rc::new("key".to_string()));
|
||||
let value = OwnedValue::Text(LimboText::json(Rc::new(r#"{"json":"value"}"#.to_string())));
|
||||
let value = OwnedValue::Text(Text::json(Rc::new(r#"{"json":"value"}"#.to_string())));
|
||||
let input = vec![key, value];
|
||||
|
||||
let result = json_object(&input).unwrap();
|
||||
@@ -1121,7 +1121,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_json_object_json_text_value_is_rendered_as_regular_text() {
|
||||
let key = OwnedValue::build_text(Rc::new("key".to_string()));
|
||||
let value = OwnedValue::Text(LimboText::new(Rc::new(r#"{"json":"value"}"#.to_string())));
|
||||
let value = OwnedValue::Text(Text::new(Rc::new(r#"{"json":"value"}"#.to_string())));
|
||||
let input = vec![key, value];
|
||||
|
||||
let result = json_object(&input).unwrap();
|
||||
@@ -1301,7 +1301,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_json_path_from_owned_value_root_strict() {
|
||||
let path = OwnedValue::Text(LimboText {
|
||||
let path = OwnedValue::Text(Text {
|
||||
value: Rc::new("$".to_string()),
|
||||
subtype: TextSubtype::Text,
|
||||
});
|
||||
@@ -1321,7 +1321,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_json_path_from_owned_value_root_non_strict() {
|
||||
let path = OwnedValue::Text(LimboText {
|
||||
let path = OwnedValue::Text(Text {
|
||||
value: Rc::new("$".to_string()),
|
||||
subtype: TextSubtype::Text,
|
||||
});
|
||||
@@ -1341,7 +1341,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_json_path_from_owned_value_named_strict() {
|
||||
let path = OwnedValue::Text(LimboText {
|
||||
let path = OwnedValue::Text(Text {
|
||||
value: Rc::new("field".to_string()),
|
||||
subtype: TextSubtype::Text,
|
||||
});
|
||||
@@ -1351,7 +1351,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_json_path_from_owned_value_named_non_strict() {
|
||||
let path = OwnedValue::Text(LimboText {
|
||||
let path = OwnedValue::Text(Text {
|
||||
value: Rc::new("field".to_string()),
|
||||
subtype: TextSubtype::Text,
|
||||
});
|
||||
|
||||
@@ -38,12 +38,16 @@ pub enum TextSubtype {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct LimboText {
|
||||
pub struct Text {
|
||||
pub value: Rc<String>,
|
||||
pub subtype: TextSubtype,
|
||||
}
|
||||
|
||||
impl LimboText {
|
||||
impl Text {
|
||||
pub fn from_str<S: Into<String>>(value: S) -> Self {
|
||||
Self::new(Rc::new(value.into()))
|
||||
}
|
||||
|
||||
pub fn new(value: Rc<String>) -> Self {
|
||||
Self {
|
||||
value,
|
||||
@@ -64,7 +68,7 @@ pub enum OwnedValue {
|
||||
Null,
|
||||
Integer(i64),
|
||||
Float(f64),
|
||||
Text(LimboText),
|
||||
Text(Text),
|
||||
Blob(Rc<Vec<u8>>),
|
||||
Agg(Box<AggContext>), // TODO(pere): make this without Box. Currently this might cause cache miss but let's leave it for future analysis
|
||||
Record(OwnedRecord),
|
||||
@@ -73,7 +77,7 @@ pub enum OwnedValue {
|
||||
impl OwnedValue {
|
||||
// A helper function that makes building a text OwnedValue easier.
|
||||
pub fn build_text(text: Rc<String>) -> Self {
|
||||
Self::Text(LimboText::new(text))
|
||||
Self::Text(Text::new(text))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -388,7 +392,7 @@ impl From<Value<'_>> for OwnedValue {
|
||||
Value::Null => OwnedValue::Null,
|
||||
Value::Integer(i) => OwnedValue::Integer(i),
|
||||
Value::Float(f) => OwnedValue::Float(f),
|
||||
Value::Text(s) => OwnedValue::Text(LimboText::new(Rc::new(s.to_owned()))),
|
||||
Value::Text(s) => OwnedValue::Text(Text::from_str(s)),
|
||||
Value::Blob(b) => OwnedValue::Blob(Rc::new(b.to_owned())),
|
||||
}
|
||||
}
|
||||
@@ -803,7 +807,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_serialize_text() {
|
||||
let text = Rc::new("hello".to_string());
|
||||
let record = OwnedRecord::new(vec![OwnedValue::Text(LimboText::new(text.clone()))]);
|
||||
let record = OwnedRecord::new(vec![OwnedValue::Text(Text::new(text.clone()))]);
|
||||
let mut buf = Vec::new();
|
||||
record.serialize(&mut buf);
|
||||
|
||||
@@ -845,7 +849,7 @@ mod tests {
|
||||
OwnedValue::Null,
|
||||
OwnedValue::Integer(42),
|
||||
OwnedValue::Float(3.15),
|
||||
OwnedValue::Text(LimboText::new(text.clone())),
|
||||
OwnedValue::Text(Text::new(text.clone())),
|
||||
]);
|
||||
let mut buf = Vec::new();
|
||||
record.serialize(&mut buf);
|
||||
|
||||
@@ -1066,10 +1066,8 @@ pub fn exec_or(mut lhs: &OwnedValue, mut rhs: &OwnedValue) -> OwnedValue {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
types::{LimboText, OwnedValue},
|
||||
types::{OwnedValue, Text},
|
||||
vdbe::insn::exec_or,
|
||||
};
|
||||
|
||||
@@ -1085,15 +1083,15 @@ mod tests {
|
||||
(OwnedValue::Integer(1), OwnedValue::Float(2.2)),
|
||||
(
|
||||
OwnedValue::Integer(0),
|
||||
OwnedValue::Text(LimboText::new(Rc::new("string".to_string()))),
|
||||
OwnedValue::Text(Text::from_str("string")),
|
||||
),
|
||||
(
|
||||
OwnedValue::Integer(0),
|
||||
OwnedValue::Text(LimboText::new(Rc::new("1".to_string()))),
|
||||
OwnedValue::Text(Text::from_str("1")),
|
||||
),
|
||||
(
|
||||
OwnedValue::Integer(1),
|
||||
OwnedValue::Text(LimboText::new(Rc::new("1".to_string()))),
|
||||
OwnedValue::Text(Text::from_str("1")),
|
||||
),
|
||||
];
|
||||
let outpus = [
|
||||
@@ -1134,16 +1132,13 @@ mod tests {
|
||||
(OwnedValue::Float(0.0), OwnedValue::Integer(0)),
|
||||
(
|
||||
OwnedValue::Integer(0),
|
||||
OwnedValue::Text(LimboText::new(Rc::new("string".to_string()))),
|
||||
OwnedValue::Text(Text::from_str("string")),
|
||||
),
|
||||
(
|
||||
OwnedValue::Integer(0),
|
||||
OwnedValue::Text(LimboText::new(Rc::new("1".to_string()))),
|
||||
),
|
||||
(
|
||||
OwnedValue::Integer(0),
|
||||
OwnedValue::Text(LimboText::new(Rc::new("".to_string()))),
|
||||
OwnedValue::Text(Text::from_str("1")),
|
||||
),
|
||||
(OwnedValue::Integer(0), OwnedValue::Text(Text::from_str(""))),
|
||||
];
|
||||
let outpus = [
|
||||
OwnedValue::Null,
|
||||
|
||||
Reference in New Issue
Block a user