mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-09-06 20:56:31 +00:00
Remove JsonDocument
Co-Authored-By: Louis Dureuil <louis.dureuil@xinra.net>
This commit is contained in:
@ -182,7 +182,7 @@ enum RenderError<'a> {
|
|||||||
BothInlineDocAndDocId,
|
BothInlineDocAndDocId,
|
||||||
TemplateParsing(json_template::Error),
|
TemplateParsing(json_template::Error),
|
||||||
TemplateRendering(json_template::Error),
|
TemplateRendering(json_template::Error),
|
||||||
CouldNotHandleInput,
|
InputConversion(liquid::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<heed::Error> for RenderError<'_> {
|
impl From<heed::Error> for RenderError<'_> {
|
||||||
@ -321,8 +321,8 @@ impl From<RenderError<'_>> for ResponseError {
|
|||||||
format!("Error rendering template: {}", err.rendering_error("input")),
|
format!("Error rendering template: {}", err.rendering_error("input")),
|
||||||
Code::TemplateRenderingError,
|
Code::TemplateRenderingError,
|
||||||
),
|
),
|
||||||
CouldNotHandleInput => ResponseError::from_msg(
|
InputConversion(err) => ResponseError::from_msg(
|
||||||
String::from("Could not handle the input provided."),
|
format!("Error converting input to a liquid object: {err}"),
|
||||||
Code::InvalidRenderInput,
|
Code::InvalidRenderInput,
|
||||||
),
|
),
|
||||||
ExpectedDotAfterValue(span) => ResponseError::from_msg(
|
ExpectedDotAfterValue(span) => ResponseError::from_msg(
|
||||||
@ -516,8 +516,8 @@ async fn render(index: Index, query: RenderQuery) -> Result<RenderResult, Respon
|
|||||||
|
|
||||||
if let Some(doc) = media.get("doc") {
|
if let Some(doc) = media.get("doc") {
|
||||||
if insert_fields {
|
if insert_fields {
|
||||||
let fields = get_inline_document_fields(&index, &rtxn, doc)?
|
let fields =
|
||||||
.map_err(|_| CouldNotHandleInput)?;
|
get_inline_document_fields(&index, &rtxn, doc)?.map_err(InputConversion)?;
|
||||||
object.insert("fields".into(), fields.to_value());
|
object.insert("fields".into(), fields.to_value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@ use liquid::{ObjectView, ValueView};
|
|||||||
use rustc_hash::FxBuildHasher;
|
use rustc_hash::FxBuildHasher;
|
||||||
use serde_json::value::RawValue;
|
use serde_json::value::RawValue;
|
||||||
|
|
||||||
use crate::constants::{RESERVED_GEO_FIELD_NAME, RESERVED_VECTORS_FIELD_NAME};
|
|
||||||
use crate::update::del_add::{DelAdd, KvReaderDelAdd};
|
use crate::update::del_add::{DelAdd, KvReaderDelAdd};
|
||||||
use crate::FieldsIdsMap;
|
use crate::FieldsIdsMap;
|
||||||
|
|
||||||
@ -144,110 +143,6 @@ impl ValueView for Document<'_> {
|
|||||||
/// Implementation for any type that implements the Document trait
|
/// Implementation for any type that implements the Document trait
|
||||||
use crate::update::new::document::Document as DocumentTrait;
|
use crate::update::new::document::Document as DocumentTrait;
|
||||||
|
|
||||||
pub struct JsonDocument {
|
|
||||||
object: liquid::Object,
|
|
||||||
cached: BTreeMap<String, Box<RawValue>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl JsonDocument {
|
|
||||||
pub fn new(value: &serde_json::Value) -> Result<Self, ()> {
|
|
||||||
let to_string = serde_json::to_string(&value).map_err(|_| ())?;
|
|
||||||
let back_to_value: BTreeMap<String, Box<RawValue>> =
|
|
||||||
serde_json::from_str(&to_string).map_err(|_| ())?;
|
|
||||||
let object = liquid::to_object(&value).map_err(|_| ())?;
|
|
||||||
Ok(Self { object, cached: back_to_value })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for JsonDocument {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
self.object.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> DocumentTrait<'a> for &'a JsonDocument {
|
|
||||||
fn iter_top_level_fields(
|
|
||||||
&self,
|
|
||||||
) -> impl Iterator<Item = crate::Result<(&'a str, &'a RawValue)>> {
|
|
||||||
self.cached.iter().filter_map(|(k, v)| {
|
|
||||||
if k == RESERVED_VECTORS_FIELD_NAME || k == RESERVED_GEO_FIELD_NAME {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(Ok((k.as_str(), v.as_ref())))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn top_level_fields_count(&self) -> usize {
|
|
||||||
self.cached.len()
|
|
||||||
- self.cached.contains_key(RESERVED_VECTORS_FIELD_NAME) as usize
|
|
||||||
- self.cached.contains_key(RESERVED_GEO_FIELD_NAME) as usize
|
|
||||||
}
|
|
||||||
|
|
||||||
fn top_level_field(&self, k: &str) -> crate::Result<Option<&'a RawValue>> {
|
|
||||||
if k == RESERVED_VECTORS_FIELD_NAME || k == RESERVED_GEO_FIELD_NAME {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
Ok(self.cached.get(k).map(|r| r.as_ref()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn vectors_field(&self) -> crate::Result<Option<&'a RawValue>> {
|
|
||||||
Ok(self.cached.get(RESERVED_VECTORS_FIELD_NAME).map(|r| r.as_ref()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn geo_field(&self) -> crate::Result<Option<&'a RawValue>> {
|
|
||||||
Ok(self.cached.get(RESERVED_GEO_FIELD_NAME).map(|r| r.as_ref()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ObjectView for JsonDocument {
|
|
||||||
fn as_value(&self) -> &dyn ValueView {
|
|
||||||
self.object.as_value()
|
|
||||||
}
|
|
||||||
fn size(&self) -> i64 {
|
|
||||||
self.object.size()
|
|
||||||
}
|
|
||||||
fn keys<'k>(&'k self) -> Box<dyn Iterator<Item = KStringCow<'k>> + 'k> {
|
|
||||||
Box::new(self.object.keys().map(|s| s.into()))
|
|
||||||
}
|
|
||||||
fn values<'k>(&'k self) -> Box<dyn Iterator<Item = &'k dyn ValueView> + 'k> {
|
|
||||||
Box::new(self.object.values().map(|v| v.as_view()))
|
|
||||||
}
|
|
||||||
fn iter<'k>(&'k self) -> Box<dyn Iterator<Item = (KStringCow<'k>, &'k dyn ValueView)> + 'k> {
|
|
||||||
Box::new(self.object.iter().map(|(k, v)| (k.into(), v.as_view())))
|
|
||||||
}
|
|
||||||
fn contains_key(&self, index: &str) -> bool {
|
|
||||||
self.object.contains_key(index)
|
|
||||||
}
|
|
||||||
fn get<'s>(&'s self, index: &str) -> Option<&'s dyn ValueView> {
|
|
||||||
self.object.get(index).map(|v| v.as_view())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ValueView for JsonDocument {
|
|
||||||
fn as_debug(&self) -> &dyn fmt::Debug {
|
|
||||||
self.object.as_debug()
|
|
||||||
}
|
|
||||||
fn render(&self) -> DisplayCow<'_> {
|
|
||||||
self.object.render()
|
|
||||||
}
|
|
||||||
fn source(&self) -> DisplayCow<'_> {
|
|
||||||
self.object.source()
|
|
||||||
}
|
|
||||||
fn type_name(&self) -> &'static str {
|
|
||||||
self.object.type_name()
|
|
||||||
}
|
|
||||||
fn query_state(&self, state: State) -> bool {
|
|
||||||
self.object.query_state(state)
|
|
||||||
}
|
|
||||||
fn to_kstr(&self) -> KStringCow<'_> {
|
|
||||||
self.object.to_kstr()
|
|
||||||
}
|
|
||||||
fn to_value(&self) -> LiquidValue {
|
|
||||||
self.object.to_value()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ParseableDocument<'a, 'doc, D: DocumentTrait<'a> + Debug> {
|
pub struct ParseableDocument<'a, 'doc, D: DocumentTrait<'a> + Debug> {
|
||||||
document: D,
|
document: D,
|
||||||
|
@ -18,7 +18,6 @@ use liquid::ValueView;
|
|||||||
|
|
||||||
pub use self::context::Context;
|
pub use self::context::Context;
|
||||||
use crate::fields_ids_map::metadata::FieldIdMapWithMetadata;
|
use crate::fields_ids_map::metadata::FieldIdMapWithMetadata;
|
||||||
use crate::prompt::document::JsonDocument;
|
|
||||||
use crate::update::del_add::DelAdd;
|
use crate::update::del_add::DelAdd;
|
||||||
use crate::update::new::document::DocumentFromDb;
|
use crate::update::new::document::DocumentFromDb;
|
||||||
use crate::{GlobalFieldsIdsMap, Index, MetadataBuilder};
|
use crate::{GlobalFieldsIdsMap, Index, MetadataBuilder};
|
||||||
@ -173,10 +172,13 @@ pub fn get_inline_document_fields(
|
|||||||
index: &Index,
|
index: &Index,
|
||||||
rtxn: &RoTxn<'_>,
|
rtxn: &RoTxn<'_>,
|
||||||
inline_doc: &serde_json::Value,
|
inline_doc: &serde_json::Value,
|
||||||
) -> Result<Result<LiquidValue, ()>, crate::Error> {
|
) -> Result<Result<LiquidValue, liquid::Error>, crate::Error> {
|
||||||
let fid_map_with_meta = index.fields_ids_map_with_metadata(rtxn)?;
|
let fid_map_with_meta = index.fields_ids_map_with_metadata(rtxn)?;
|
||||||
let Ok(inline_doc) = JsonDocument::new(inline_doc) else {
|
let inline_doc = match liquid::to_object(&inline_doc) {
|
||||||
return Ok(Err(()));
|
Ok(inline_doc) => inline_doc,
|
||||||
|
Err(e) => {
|
||||||
|
return Ok(Err(e));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let fields = OwnedFields::new(&inline_doc, &fid_map_with_meta);
|
let fields = OwnedFields::new(&inline_doc, &fid_map_with_meta);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user