mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-01-10 13:34:30 +01:00
Revert "Simplify the code when array_each failed"
This reverts commit 271685cceb06f9203f1f7298e5c21ce0c6e4b11f.
This commit is contained in:
parent
7b2f2a4f9c
commit
a08cc82983
@ -4,8 +4,10 @@ use std::fs::File;
|
|||||||
use std::io::{self, Seek, Write};
|
use std::io::{self, Seek, Write};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use either::Either;
|
||||||
use memmap2::MmapOptions;
|
use memmap2::MmapOptions;
|
||||||
use milli::documents::{DocumentsBatchBuilder, Error};
|
use milli::documents::{DocumentsBatchBuilder, Error};
|
||||||
|
use milli::Object;
|
||||||
use serde::de::{SeqAccess, Visitor};
|
use serde::de::{SeqAccess, Visitor};
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
use serde_json::error::Category;
|
use serde_json::error::Category;
|
||||||
@ -123,7 +125,7 @@ pub fn read_ndjson(file: &File, writer: impl Write + Seek) -> Result<u64> {
|
|||||||
read_json_inner(file, writer, PayloadType::Ndjson)
|
read_json_inner(file, writer, PayloadType::Ndjson)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads JSON from temporary file and write an obkv batch to writer.
|
/// Reads JSON from temporary file and write an obkv batch to writer.
|
||||||
fn read_json_inner(
|
fn read_json_inner(
|
||||||
file: &File,
|
file: &File,
|
||||||
writer: impl Write + Seek,
|
writer: impl Write + Seek,
|
||||||
@ -138,17 +140,26 @@ fn read_json_inner(
|
|||||||
// The data has been transferred to the writer during the deserialization process.
|
// The data has been transferred to the writer during the deserialization process.
|
||||||
Ok(Ok(_)) => (),
|
Ok(Ok(_)) => (),
|
||||||
Ok(Err(e)) => return Err(DocumentFormatError::Internal(Box::new(e))),
|
Ok(Err(e)) => return Err(DocumentFormatError::Internal(Box::new(e))),
|
||||||
Err(_) => {
|
Err(_e) => {
|
||||||
// If we cannot deserialize the content as an array of object then
|
// If we cannot deserialize the content as an array of object then we try
|
||||||
// we try to deserialize it as a single JSON object.
|
// to deserialize it with the original method to keep correct error messages.
|
||||||
let object = serde_json::from_reader(file)
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(transparent)]
|
||||||
|
struct ArrayOrSingleObject {
|
||||||
|
#[serde(with = "either::serde_untagged")]
|
||||||
|
inner: Either<Vec<Object>, Object>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let content: ArrayOrSingleObject = serde_json::from_reader(file)
|
||||||
.map_err(Error::Json)
|
.map_err(Error::Json)
|
||||||
.map_err(|e| (payload_type, e))?;
|
.map_err(|e| (payload_type, e))?;
|
||||||
|
|
||||||
builder
|
for object in content.inner.map_right(|o| vec![o]).into_inner() {
|
||||||
.append_json_object(&object)
|
builder
|
||||||
.map_err(Into::into)
|
.append_json_object(&object)
|
||||||
.map_err(DocumentFormatError::Internal)?;
|
.map_err(Into::into)
|
||||||
|
.map_err(DocumentFormatError::Internal)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user