meilisearch/meilisearch-lib/src/document_formats.rs

115 lines
3.5 KiB
Rust
Raw Normal View History

2021-09-29 04:58:48 +08:00
use std::fmt;
use std::io::{self, BufRead, BufReader, BufWriter, Cursor, Read, Seek, Write};
2021-09-28 17:59:55 +08:00
2021-09-30 16:35:24 +08:00
use meilisearch_error::{Code, ErrorCode};
2021-09-28 17:59:55 +08:00
use milli::documents::DocumentBatchBuilder;
type Result<T> = std::result::Result<T, DocumentFormatError>;
#[derive(Debug)]
pub enum PayloadType {
2021-09-29 16:17:52 +08:00
Ndjson,
Json,
2021-09-29 04:58:48 +08:00
Csv,
2021-09-28 17:59:55 +08:00
}
impl fmt::Display for PayloadType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
2021-09-29 16:17:52 +08:00
PayloadType::Ndjson => write!(f, "ndjson"),
PayloadType::Json => write!(f, "json"),
2021-09-29 04:58:48 +08:00
PayloadType::Csv => write!(f, "csv"),
2021-09-28 17:59:55 +08:00
}
}
}
#[derive(thiserror::Error, Debug)]
pub enum DocumentFormatError {
2021-11-03 21:25:49 +08:00
#[error("An internal error has occurred. `{0}`.")]
2021-09-28 17:59:55 +08:00
Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
2021-10-27 01:36:48 +08:00
#[error("The `{1}` payload provided is malformed. `{0}`.")]
2021-09-29 04:22:59 +08:00
MalformedPayload(
Box<dyn std::error::Error + Send + Sync + 'static>,
PayloadType,
),
#[error("The `{0}` payload must contain at least one document.")]
EmptyPayload(PayloadType),
2021-09-28 17:59:55 +08:00
}
impl From<(PayloadType, milli::documents::Error)> for DocumentFormatError {
fn from((ty, error): (PayloadType, milli::documents::Error)) -> Self {
match error {
milli::documents::Error::Io(e) => Self::Internal(Box::new(e)),
e => Self::MalformedPayload(Box::new(e), ty),
}
}
}
2021-09-30 16:35:24 +08:00
impl ErrorCode for DocumentFormatError {
fn error_code(&self) -> Code {
match self {
DocumentFormatError::Internal(_) => Code::Internal,
DocumentFormatError::MalformedPayload(_, _) => Code::MalformedPayload,
DocumentFormatError::EmptyPayload(_) => Code::MalformedPayload,
2021-09-30 16:35:24 +08:00
}
}
}
internal_error!(DocumentFormatError: io::Error);
2021-09-28 17:59:55 +08:00
/// reads csv from input and write an obkv batch to writer.
pub fn read_csv(input: impl Read, writer: impl Write + Seek) -> Result<()> {
let writer = BufWriter::new(writer);
2021-10-27 01:36:48 +08:00
let builder =
DocumentBatchBuilder::from_csv(input, writer).map_err(|e| (PayloadType::Csv, e))?;
if builder.len() == 0 {
return Err(DocumentFormatError::EmptyPayload(PayloadType::Csv));
}
2021-10-27 01:36:48 +08:00
builder.finish().map_err(|e| (PayloadType::Csv, e))?;
2021-09-29 04:58:48 +08:00
Ok(())
2021-09-29 04:58:48 +08:00
}
/// reads jsonl from input and write an obkv batch to writer.
pub fn read_ndjson(input: impl Read, writer: impl Write + Seek) -> Result<()> {
let mut reader = BufReader::new(input);
let writer = BufWriter::new(writer);
let mut builder = DocumentBatchBuilder::new(writer).map_err(|e| (PayloadType::Ndjson, e))?;
let mut buf = String::new();
2021-09-28 17:59:55 +08:00
while reader.read_line(&mut buf)? > 0 {
builder
.extend_from_json(Cursor::new(&buf.as_bytes()))
.map_err(|e| (PayloadType::Ndjson, e))?;
buf.clear();
2021-09-28 17:59:55 +08:00
}
if builder.len() == 0 {
return Err(DocumentFormatError::EmptyPayload(PayloadType::Ndjson));
}
2021-10-27 01:36:48 +08:00
builder.finish().map_err(|e| (PayloadType::Ndjson, e))?;
2021-09-28 17:59:55 +08:00
Ok(())
2021-09-28 17:59:55 +08:00
}
/// reads json from input and write an obkv batch to writer.
pub fn read_json(input: impl Read, writer: impl Write + Seek) -> Result<()> {
let writer = BufWriter::new(writer);
let mut builder = DocumentBatchBuilder::new(writer).map_err(|e| (PayloadType::Json, e))?;
builder
.extend_from_json(input)
.map_err(|e| (PayloadType::Json, e))?;
if builder.len() == 0 {
return Err(DocumentFormatError::EmptyPayload(PayloadType::Json));
}
builder.finish().map_err(|e| (PayloadType::Json, e))?;
Ok(())
}