2022-06-14 22:03:48 +08:00
|
|
|
use std::io::{self, Write};
|
2021-08-31 17:44:15 +08:00
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
use grenad::{CompressionType, WriterBuilder};
|
2022-07-12 00:38:50 +08:00
|
|
|
use serde::de::Deserializer;
|
2022-06-15 21:36:27 +08:00
|
|
|
use serde_json::{to_writer, Value};
|
2021-08-31 17:44:15 +08:00
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
use super::{DocumentsBatchIndex, Error, DOCUMENTS_BATCH_INDEX_KEY};
|
2022-07-12 00:38:50 +08:00
|
|
|
use crate::documents::serde_impl::DocumentVisitor;
|
2022-06-15 21:36:27 +08:00
|
|
|
use crate::Object;
|
2021-08-31 17:44:15 +08:00
|
|
|
|
|
|
|
/// The `DocumentsBatchBuilder` provides a way to build a documents batch in the intermediary
|
|
|
|
/// format used by milli.
|
|
|
|
///
|
2022-06-14 22:03:48 +08:00
|
|
|
/// The writer used by the `DocumentsBatchBuilder` can be read using a `DocumentsBatchReader`
|
|
|
|
/// to iterate over the documents.
|
2021-10-25 16:26:28 +08:00
|
|
|
///
|
|
|
|
/// ## example:
|
|
|
|
/// ```
|
|
|
|
/// use serde_json::json;
|
2022-06-14 22:03:48 +08:00
|
|
|
/// use milli::documents::DocumentsBatchBuilder;
|
2021-10-25 16:26:28 +08:00
|
|
|
///
|
2022-06-14 22:03:48 +08:00
|
|
|
/// let json = json!({ "id": 1, "name": "foo" });
|
|
|
|
///
|
|
|
|
/// let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
/// builder.append_json_object(json.as_object().unwrap()).unwrap();
|
|
|
|
/// let _vector = builder.into_inner().unwrap();
|
2021-10-25 16:26:28 +08:00
|
|
|
/// ```
|
2022-06-14 22:03:48 +08:00
|
|
|
pub struct DocumentsBatchBuilder<W> {
|
|
|
|
/// The inner grenad writer, the last value must always be the `DocumentsBatchIndex`.
|
|
|
|
writer: grenad::Writer<W>,
|
|
|
|
/// A map that creates the relation between field ids and field names.
|
|
|
|
fields_index: DocumentsBatchIndex,
|
|
|
|
/// The number of documents that were added to this builder,
|
|
|
|
/// it doesn't take the primary key of the documents into account at this point.
|
|
|
|
documents_count: u32,
|
|
|
|
|
|
|
|
/// A buffer to store a temporary obkv buffer and avoid reallocating.
|
2021-10-21 03:26:52 +08:00
|
|
|
obkv_buffer: Vec<u8>,
|
2022-06-14 22:03:48 +08:00
|
|
|
/// A buffer to serialize the values and avoid reallocating,
|
|
|
|
/// serialized values are stored in an obkv.
|
2021-10-21 03:26:52 +08:00
|
|
|
value_buffer: Vec<u8>,
|
2021-08-31 17:44:15 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
impl<W: Write> DocumentsBatchBuilder<W> {
|
|
|
|
pub fn new(writer: W) -> DocumentsBatchBuilder<W> {
|
|
|
|
DocumentsBatchBuilder {
|
|
|
|
writer: WriterBuilder::new().compression_type(CompressionType::None).build(writer),
|
|
|
|
fields_index: DocumentsBatchIndex::default(),
|
|
|
|
documents_count: 0,
|
2021-10-21 03:26:52 +08:00
|
|
|
obkv_buffer: Vec::new(),
|
|
|
|
value_buffer: Vec::new(),
|
2022-06-14 22:03:48 +08:00
|
|
|
}
|
2021-08-31 17:44:15 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
/// Returns the number of documents inserted into this builder.
|
|
|
|
pub fn documents_count(&self) -> u32 {
|
|
|
|
self.documents_count
|
2021-08-31 17:44:15 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
/// Appends a new JSON object into the batch and updates the `DocumentsBatchIndex` accordingly.
|
2022-06-15 21:36:27 +08:00
|
|
|
pub fn append_json_object(&mut self, object: &Object) -> io::Result<()> {
|
2022-06-14 22:03:48 +08:00
|
|
|
// Make sure that we insert the fields ids in order as the obkv writer has this requirement.
|
|
|
|
let mut fields_ids: Vec<_> = object.keys().map(|k| self.fields_index.insert(&k)).collect();
|
|
|
|
fields_ids.sort_unstable();
|
|
|
|
|
|
|
|
self.obkv_buffer.clear();
|
|
|
|
let mut writer = obkv::KvWriter::new(&mut self.obkv_buffer);
|
|
|
|
for field_id in fields_ids {
|
|
|
|
let key = self.fields_index.name(field_id).unwrap();
|
|
|
|
self.value_buffer.clear();
|
|
|
|
to_writer(&mut self.value_buffer, &object[key])?;
|
|
|
|
writer.insert(field_id, &self.value_buffer)?;
|
|
|
|
}
|
2021-10-21 03:26:52 +08:00
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
let internal_id = self.documents_count.to_be_bytes();
|
|
|
|
let document_bytes = writer.into_inner()?;
|
|
|
|
self.writer.insert(internal_id, &document_bytes)?;
|
|
|
|
self.documents_count += 1;
|
2021-10-21 03:26:52 +08:00
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
Ok(())
|
2021-08-31 17:44:15 +08:00
|
|
|
}
|
2021-10-21 17:05:16 +08:00
|
|
|
|
2022-07-12 00:38:50 +08:00
|
|
|
/// Appends a new JSON array of objects into the batch and updates the `DocumentsBatchIndex` accordingly.
|
|
|
|
pub fn append_json_array<R: io::Read>(&mut self, reader: R) -> Result<(), Error> {
|
|
|
|
let mut de = serde_json::Deserializer::from_reader(reader);
|
|
|
|
let mut visitor = DocumentVisitor::new(self);
|
|
|
|
de.deserialize_any(&mut visitor)?
|
|
|
|
}
|
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
/// Appends a new CSV file into the batch and updates the `DocumentsBatchIndex` accordingly.
|
|
|
|
pub fn append_csv<R: io::Read>(&mut self, mut reader: csv::Reader<R>) -> Result<(), Error> {
|
|
|
|
// Make sure that we insert the fields ids in order as the obkv writer has this requirement.
|
|
|
|
let mut typed_fields_ids: Vec<_> = reader
|
2021-10-24 21:39:56 +08:00
|
|
|
.headers()?
|
2021-10-21 17:05:16 +08:00
|
|
|
.into_iter()
|
|
|
|
.map(parse_csv_header)
|
2022-06-14 22:03:48 +08:00
|
|
|
.map(|(k, t)| (self.fields_index.insert(k), t))
|
|
|
|
.enumerate()
|
|
|
|
.collect();
|
2022-07-20 22:20:35 +08:00
|
|
|
// Make sure that we insert the fields ids in order as the obkv writer has this requirement.
|
2022-06-14 22:03:48 +08:00
|
|
|
typed_fields_ids.sort_unstable_by_key(|(_, (fid, _))| *fid);
|
|
|
|
|
|
|
|
let mut record = csv::StringRecord::new();
|
|
|
|
let mut line = 0;
|
|
|
|
while reader.read_record(&mut record)? {
|
|
|
|
// We increment here and not at the end of the while loop to take
|
|
|
|
// the header offset into account.
|
|
|
|
line += 1;
|
|
|
|
|
|
|
|
self.obkv_buffer.clear();
|
|
|
|
let mut writer = obkv::KvWriter::new(&mut self.obkv_buffer);
|
|
|
|
|
|
|
|
for (i, (field_id, type_)) in typed_fields_ids.iter() {
|
|
|
|
self.value_buffer.clear();
|
|
|
|
|
|
|
|
let value = &record[*i];
|
|
|
|
match type_ {
|
2021-10-25 23:38:32 +08:00
|
|
|
AllowedType::Number => {
|
2022-02-03 22:46:11 +08:00
|
|
|
if value.trim().is_empty() {
|
2022-06-14 22:03:48 +08:00
|
|
|
to_writer(&mut self.value_buffer, &Value::Null)?;
|
2022-02-03 22:46:11 +08:00
|
|
|
} else {
|
2022-06-14 22:03:48 +08:00
|
|
|
match value.trim().parse::<f64>() {
|
|
|
|
Ok(float) => {
|
|
|
|
to_writer(&mut self.value_buffer, &float)?;
|
2022-02-03 22:46:11 +08:00
|
|
|
}
|
2022-06-14 22:03:48 +08:00
|
|
|
Err(error) => {
|
|
|
|
return Err(Error::ParseFloat {
|
|
|
|
error,
|
|
|
|
line,
|
|
|
|
value: value.to_string(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 22:46:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AllowedType::String => {
|
|
|
|
if value.is_empty() {
|
2022-06-14 22:03:48 +08:00
|
|
|
to_writer(&mut self.value_buffer, &Value::Null)?;
|
2022-02-03 22:46:11 +08:00
|
|
|
} else {
|
2022-06-14 22:03:48 +08:00
|
|
|
to_writer(&mut self.value_buffer, value)?;
|
2022-02-03 22:46:11 +08:00
|
|
|
}
|
2021-10-25 23:38:32 +08:00
|
|
|
}
|
2022-06-14 22:03:48 +08:00
|
|
|
}
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
// We insert into the obkv writer the value buffer that has been filled just above.
|
|
|
|
writer.insert(*field_id, &self.value_buffer)?;
|
2021-10-21 17:05:16 +08:00
|
|
|
}
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
let internal_id = self.documents_count.to_be_bytes();
|
|
|
|
let document_bytes = writer.into_inner()?;
|
|
|
|
self.writer.insert(internal_id, &document_bytes)?;
|
|
|
|
self.documents_count += 1;
|
2021-10-21 17:05:16 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 22:03:48 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Flushes the content on disk and stores the final version of the `DocumentsBatchIndex`.
|
|
|
|
pub fn into_inner(mut self) -> io::Result<W> {
|
|
|
|
let DocumentsBatchBuilder { mut writer, fields_index, .. } = self;
|
|
|
|
|
|
|
|
// We serialize and insert the `DocumentsBatchIndex` as the last key of the grenad writer.
|
|
|
|
self.value_buffer.clear();
|
|
|
|
to_writer(&mut self.value_buffer, &fields_index)?;
|
|
|
|
writer.insert(DOCUMENTS_BATCH_INDEX_KEY, &self.value_buffer)?;
|
|
|
|
|
|
|
|
writer.into_inner()
|
2021-10-21 17:05:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum AllowedType {
|
|
|
|
String,
|
|
|
|
Number,
|
|
|
|
}
|
|
|
|
|
2022-06-14 16:13:48 +08:00
|
|
|
fn parse_csv_header(header: &str) -> (&str, AllowedType) {
|
2021-10-21 17:05:16 +08:00
|
|
|
// if there are several separators we only split on the last one.
|
|
|
|
match header.rsplit_once(':') {
|
|
|
|
Some((field_name, field_type)) => match field_type {
|
2022-06-14 16:13:48 +08:00
|
|
|
"string" => (field_name, AllowedType::String),
|
|
|
|
"number" => (field_name, AllowedType::Number),
|
2021-10-25 23:38:32 +08:00
|
|
|
// if the pattern isn't reconized, we keep the whole field.
|
2022-06-14 16:13:48 +08:00
|
|
|
_otherwise => (header, AllowedType::String),
|
2021-10-21 17:05:16 +08:00
|
|
|
},
|
2022-06-14 16:13:48 +08:00
|
|
|
None => (header, AllowedType::String),
|
2021-10-21 17:05:16 +08:00
|
|
|
}
|
2021-08-31 17:44:15 +08:00
|
|
|
}
|
2021-10-21 03:26:52 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
2022-06-15 00:12:15 +08:00
|
|
|
use serde_json::json;
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2021-10-21 03:26:52 +08:00
|
|
|
use super::*;
|
2022-06-15 00:12:15 +08:00
|
|
|
use crate::documents::{obkv_to_object, DocumentsBatchReader};
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2021-10-21 03:26:52 +08:00
|
|
|
#[test]
|
|
|
|
fn add_single_documents_json() {
|
|
|
|
let json = serde_json::json!({
|
|
|
|
"id": 1,
|
|
|
|
"field": "hello!",
|
|
|
|
});
|
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_json_object(json.as_object().unwrap()).unwrap();
|
2021-10-21 03:26:52 +08:00
|
|
|
|
|
|
|
let json = serde_json::json!({
|
|
|
|
"blabla": false,
|
|
|
|
"field": "hello!",
|
|
|
|
"id": 1,
|
|
|
|
});
|
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
builder.append_json_object(json.as_object().unwrap()).unwrap();
|
2021-10-21 03:26:52 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
assert_eq!(builder.documents_count(), 2);
|
|
|
|
let vector = builder.into_inner().unwrap();
|
2021-10-21 03:26:52 +08:00
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2021-10-21 03:26:52 +08:00
|
|
|
assert_eq!(index.len(), 3);
|
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let document = cursor.next_document().unwrap().unwrap();
|
2021-10-21 03:26:52 +08:00
|
|
|
assert_eq!(document.iter().count(), 2);
|
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let document = cursor.next_document().unwrap().unwrap();
|
2021-10-21 03:26:52 +08:00
|
|
|
assert_eq!(document.iter().count(), 3);
|
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
assert!(cursor.next_document().unwrap().is_none());
|
2021-10-21 03:26:52 +08:00
|
|
|
}
|
2021-10-21 17:05:16 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_documents_csv() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = "id:number,field:string\n1,hello!\n2,blabla";
|
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
2021-10-21 17:05:16 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
assert_eq!(builder.documents_count(), 2);
|
|
|
|
let vector = builder.into_inner().unwrap();
|
2021-10-21 17:05:16 +08:00
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2021-10-21 17:05:16 +08:00
|
|
|
assert_eq!(index.len(), 2);
|
2022-06-14 22:04:27 +08:00
|
|
|
|
|
|
|
let document = cursor.next_document().unwrap().unwrap();
|
2021-10-21 17:05:16 +08:00
|
|
|
assert_eq!(document.iter().count(), 2);
|
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let document = cursor.next_document().unwrap().unwrap();
|
2021-10-21 17:05:16 +08:00
|
|
|
assert_eq!(document.iter().count(), 2);
|
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
assert!(cursor.next_document().unwrap().is_none());
|
2021-10-21 17:05:16 +08:00
|
|
|
}
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_csv_document() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city,country,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2022-06-14 22:04:27 +08:00
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
"city": "Boston",
|
|
|
|
"country": "United States",
|
|
|
|
"pop": "4628910",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
assert!(cursor.next_document().unwrap().is_none());
|
2021-10-25 15:48:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn coma_in_field() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city,country,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United, States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
|
|
|
|
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
"city": "Boston",
|
|
|
|
"country": "United, States",
|
|
|
|
"pop": "4628910",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quote_in_field() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city,country,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United"" States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2022-06-14 22:04:27 +08:00
|
|
|
|
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
"city": "Boston",
|
|
|
|
"country": "United\" States",
|
|
|
|
"pop": "4628910",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn integer_in_field() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city,country,pop:number
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
|
|
|
|
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2022-06-14 22:04:27 +08:00
|
|
|
|
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
"city": "Boston",
|
|
|
|
"country": "United States",
|
|
|
|
"pop": 4628910.0,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn float_in_field() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city,country,pop:number
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910.01""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
|
|
|
|
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
"city": "Boston",
|
|
|
|
"country": "United States",
|
|
|
|
"pop": 4628910.01,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn several_colon_in_header() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city:love:string,country:state,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2022-06-14 22:04:27 +08:00
|
|
|
|
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
"city:love": "Boston",
|
|
|
|
"country:state": "United States",
|
|
|
|
"pop": "4628910",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ending_by_colon_in_header() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city:,country,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
|
|
|
|
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2022-06-14 22:04:27 +08:00
|
|
|
|
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
"city:": "Boston",
|
|
|
|
"country": "United States",
|
|
|
|
"pop": "4628910",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn starting_by_colon_in_header() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#":city,country,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
|
|
|
|
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
":city": "Boston",
|
|
|
|
"country": "United States",
|
|
|
|
"pop": "4628910",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ignore]
|
|
|
|
#[test]
|
|
|
|
fn starting_by_colon_in_header2() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#":string,country,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, _) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2022-06-14 22:04:27 +08:00
|
|
|
|
|
|
|
assert!(cursor.next_document().is_err());
|
2021-10-25 15:48:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_colon_in_header() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city::string,country,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
|
|
|
|
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
builder.append_csv(csv).unwrap();
|
|
|
|
let vector = builder.into_inner().unwrap();
|
|
|
|
|
2022-07-18 22:08:01 +08:00
|
|
|
let (mut cursor, index) = DocumentsBatchReader::from_reader(Cursor::new(vector))
|
|
|
|
.unwrap()
|
|
|
|
.into_cursor_and_fields_index();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let doc = cursor.next_document().unwrap().unwrap();
|
2022-06-15 00:12:15 +08:00
|
|
|
let val = obkv_to_object(&doc, &index).map(Value::from).unwrap();
|
2021-10-25 15:48:53 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
val,
|
|
|
|
json!({
|
|
|
|
"city:": "Boston",
|
|
|
|
"country": "United States",
|
|
|
|
"pop": "4628910",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_type_in_header() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city,country:number,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States","4628910""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
assert!(builder.append_csv(csv).is_err());
|
2021-10-25 15:48:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_column_count1() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city,country,pop
|
|
|
|
"Boston","United States","4628910", "too much
|
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content"#;
|
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
assert!(builder.append_csv(csv).is_err());
|
2021-10-25 15:48:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_column_count2() {
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv_content = r#"city,country,pop
|
2021-10-25 15:48:53 +08:00
|
|
|
"Boston","United States""#;
|
2022-06-14 22:04:27 +08:00
|
|
|
let csv = csv::Reader::from_reader(Cursor::new(csv_content));
|
2021-10-25 15:48:53 +08:00
|
|
|
|
2022-06-14 22:04:27 +08:00
|
|
|
let mut builder = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
assert!(builder.append_csv(csv).is_err());
|
2021-10-25 15:48:53 +08:00
|
|
|
}
|
2021-10-21 03:26:52 +08:00
|
|
|
}
|