1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use std::net::IpAddr;
use std::str::{from_utf8, FromStr};

use base64::Engine;
use serde_json::{json, Value as JsonValue};
use tantivy::schema::{Facet, FieldType, IntoIpv6Addr, OwnedValue, Schema};
use tantivy::tokenizer::PreTokenizedString;
use tantivy::{DateTime, TantivyDocument};
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;

use crate::errors::{Error, SummaResult, ValidationError};
use crate::page_rank::quantize_page_rank;
use crate::utils::current_time;

/// Wrapper for carrying `tantivy::Document` from various sources
pub enum SummaDocument<'a> {
    BoundJsonBytes((&'a Schema, &'a [u8])),
    UnboundJsonBytes(&'a [u8]),
    TantivyDocument(TantivyDocument),
}

/// Possible error that may occur while parsing a field value
/// At this point the JSON is known to be valid.
#[derive(thiserror::Error, Debug)]
pub enum ValueParsingError {
    #[error("overflow_error: <expected: {expected}, got: {json}>")]
    OverflowError { expected: &'static str, json: JsonValue },
    #[error("type_error: <expected: {expected}, got: {json}")]
    TypeError { expected: &'static str, json: JsonValue },
    #[error("invalid_base64: {base64}")]
    InvalidBase64 { base64: String },
    #[error("null_value_error")]
    NullValueError,
    #[error("parse_error: {json}, error: {error}")]
    ParseError { error: String, json: serde_json::Value },
}

/// Error that may happen when deserializing
/// a document from JSON.
#[derive(thiserror::Error, Debug)]
pub enum DocumentParsingError {
    /// The payload given is not valid JSON.
    #[error("The provided string is not valid JSON")]
    InvalidJson(String),
    /// One of the value node could not be parsed.
    #[error("The field '{0:?}' could not be parsed: {1:?}")]
    ValueError(String, ValueParsingError),
}

pub fn process_dynamic_fields(schema: &Schema, json_object: &mut serde_json::Map<String, JsonValue>) {
    if schema.get_field("page_rank").is_ok() && schema.get_field("quantized_page_rank").is_ok() {
        if let Some(page_rank_value) = json_object.get_mut("page_rank") {
            if let Some(v) = page_rank_value.as_f64() {
                json_object.insert("quantized_page_rank".to_string(), json!(quantize_page_rank(v)));
            }
        }
    }
    if schema.get_field("updated_at").is_ok() {
        json_object.insert("updated_at".to_string(), json!(current_time()));
    }
}

/// Parse single json value
#[inline]
pub fn value_from_json(field_type: &FieldType, json: JsonValue) -> Result<OwnedValue, ValueParsingError> {
    match json {
        JsonValue::String(field_text) => match *field_type {
            FieldType::Date(_) => {
                let dt_with_fixed_tz = OffsetDateTime::parse(&field_text, &Rfc3339).map_err(|_err| ValueParsingError::TypeError {
                    expected: "rfc3339 format",
                    json: JsonValue::String(field_text),
                })?;
                Ok(DateTime::from_utc(dt_with_fixed_tz).into())
            }
            FieldType::Str(_) => Ok(OwnedValue::Str(field_text)),
            FieldType::U64(_) | FieldType::I64(_) | FieldType::F64(_) => Err(ValueParsingError::TypeError {
                expected: "an integer",
                json: JsonValue::String(field_text),
            }),
            FieldType::Bool(_) => Err(ValueParsingError::TypeError {
                expected: "a boolean",
                json: JsonValue::String(field_text),
            }),
            FieldType::Facet(_) => Ok(OwnedValue::Facet(Facet::from(&field_text))),
            FieldType::Bytes(_) => base64::engine::general_purpose::STANDARD
                .decode(&field_text)
                .map(OwnedValue::Bytes)
                .map_err(|_| ValueParsingError::InvalidBase64 { base64: field_text }),
            FieldType::JsonObject(_) => Err(ValueParsingError::TypeError {
                expected: "a json object",
                json: JsonValue::String(field_text),
            }),
            FieldType::IpAddr(_) => {
                let ip_addr: IpAddr = IpAddr::from_str(&field_text).map_err(|err| ValueParsingError::ParseError {
                    error: err.to_string(),
                    json: JsonValue::String(field_text),
                })?;
                Ok(OwnedValue::IpAddr(ip_addr.into_ipv6_addr()))
            }
        },
        JsonValue::Number(field_val_num) => match field_type {
            FieldType::I64(_) | FieldType::Date(_) => {
                if let Some(field_val_i64) = field_val_num.as_i64() {
                    Ok(OwnedValue::I64(field_val_i64))
                } else {
                    Err(ValueParsingError::OverflowError {
                        expected: "an i64 int",
                        json: JsonValue::Number(field_val_num),
                    })
                }
            }
            FieldType::U64(_) => {
                if let Some(field_val_u64) = field_val_num.as_u64() {
                    Ok(OwnedValue::U64(field_val_u64))
                } else {
                    Err(ValueParsingError::OverflowError {
                        expected: "u64",
                        json: JsonValue::Number(field_val_num),
                    })
                }
            }
            FieldType::F64(_) => {
                if let Some(field_val_f64) = field_val_num.as_f64() {
                    Ok(OwnedValue::F64(field_val_f64))
                } else {
                    Err(ValueParsingError::OverflowError {
                        expected: "a f64",
                        json: JsonValue::Number(field_val_num),
                    })
                }
            }
            FieldType::Bool(_) => Err(ValueParsingError::TypeError {
                expected: "a boolean",
                json: JsonValue::Number(field_val_num),
            }),
            FieldType::Str(_) | FieldType::Facet(_) | FieldType::Bytes(_) => Err(ValueParsingError::TypeError {
                expected: "a string",
                json: JsonValue::Number(field_val_num),
            }),
            FieldType::JsonObject(_) => Err(ValueParsingError::TypeError {
                expected: "a json object",
                json: JsonValue::Number(field_val_num),
            }),
            FieldType::IpAddr(_) => Err(ValueParsingError::TypeError {
                expected: "a string with an ip addr",
                json: JsonValue::Number(field_val_num),
            }),
        },
        JsonValue::Object(json_map) => match field_type {
            FieldType::Str(_) => {
                if let Ok(tok_str_val) = serde_json::from_value::<PreTokenizedString>(serde_json::Value::Object(json_map.clone())) {
                    Ok(OwnedValue::PreTokStr(tok_str_val))
                } else {
                    Err(ValueParsingError::TypeError {
                        expected: "a string or an pretokenized string",
                        json: JsonValue::Object(json_map),
                    })
                }
            }
            FieldType::JsonObject(_) => Ok(OwnedValue::from(json_map)),
            _ => Err(ValueParsingError::TypeError {
                expected: field_type.value_type().name(),
                json: JsonValue::Object(json_map),
            }),
        },
        JsonValue::Bool(json_bool_val) => match field_type {
            FieldType::Bool(_) => Ok(OwnedValue::Bool(json_bool_val)),
            _ => Err(ValueParsingError::TypeError {
                expected: field_type.value_type().name(),
                json: JsonValue::Bool(json_bool_val),
            }),
        },
        JsonValue::Null => Err(ValueParsingError::NullValueError),
        _ => Err(ValueParsingError::TypeError {
            expected: field_type.value_type().name(),
            json: json.clone(),
        }),
    }
}

impl<'a> SummaDocument<'a> {
    pub fn bound_with(self, schema: &'a Schema) -> SummaDocument {
        match self {
            SummaDocument::UnboundJsonBytes(json_bytes) => SummaDocument::BoundJsonBytes((schema, json_bytes)),
            SummaDocument::BoundJsonBytes((_, json_bytes)) => SummaDocument::BoundJsonBytes((schema, json_bytes)),
            other => other,
        }
    }

    /// Build a document object from a json-object.
    pub fn parse_and_setup_document(&self, schema: &Schema, doc_json: &str) -> SummaResult<TantivyDocument> {
        let mut json_obj: serde_json::Map<String, JsonValue> =
            serde_json::from_str(doc_json).map_err(|_| DocumentParsingError::InvalidJson(doc_json.to_owned()))?;
        process_dynamic_fields(schema, &mut json_obj);
        self.json_object_to_doc(schema, json_obj)
    }

    /// Build a document object from a json-object.
    pub fn json_object_to_doc(&self, schema: &Schema, json_obj: serde_json::Map<String, JsonValue>) -> SummaResult<TantivyDocument> {
        let mut doc = TantivyDocument::default();
        for (field_name, json_value) in json_obj {
            if let Ok(field) = schema.get_field(&field_name) {
                let field_entry = schema.get_field_entry(field);
                let field_type = field_entry.field_type();
                match json_value {
                    JsonValue::Array(json_items) => {
                        for json_item in json_items {
                            match value_from_json(field_type, json_item) {
                                Ok(value) => doc.add_field_value(field, value),
                                Err(ValueParsingError::NullValueError) => continue,
                                Err(error) => return Err(DocumentParsingError::ValueError(field_name.to_owned(), error).into()),
                            }
                        }
                    }
                    _ => match value_from_json(field_type, json_value) {
                        Ok(value) => doc.add_field_value(field, value),
                        Err(ValueParsingError::NullValueError) => continue,
                        Err(error) => return Err(DocumentParsingError::ValueError(field_name.to_owned(), error).into()),
                    },
                }
            }
        }
        Ok(doc)
    }
}

impl<'a> TryInto<TantivyDocument> for SummaDocument<'a> {
    type Error = Error;

    fn try_into(self) -> SummaResult<TantivyDocument> {
        match self {
            SummaDocument::BoundJsonBytes((schema, json_bytes)) => {
                let text_document = from_utf8(json_bytes).map_err(ValidationError::Utf8)?;
                let parsed_document = self.parse_and_setup_document(schema, text_document)?;
                Ok(parsed_document)
            }
            SummaDocument::UnboundJsonBytes(_) => Err(Error::UnboundDocument),
            SummaDocument::TantivyDocument(document) => Ok(document),
        }
    }
}

impl<'a> From<&'a Vec<u8>> for SummaDocument<'a> {
    fn from(v: &'a Vec<u8>) -> Self {
        SummaDocument::UnboundJsonBytes(v)
    }
}