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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Contains structs for indexing records based on a collection& schema.

pub mod errors;
pub(crate) mod fixed_byte_buf;
pub mod mapping;
pub mod mapping_indexer;
pub mod query;
pub mod text;
pub mod vector;

use crate::record::decoder::SchemaField;

use super::record::{
    decoder::{RecordDecoder, RecordSchema},
    Record,
};
use errors::{CollectionSchemaError, LoadIndexerError};
use mapping::MappingWithMeta;
use mapping_indexer::MappingIndexer;

use ciborium::{de::from_reader, ser::into_writer};
use serde::Deserialize;
use std::collections::{hash_map::RandomState, HashMap};

use errors::{QueryIndexingError, RecordIndexingError};
use query::{ConjunctiveCondition, Query};
use vector::{Constraint, TermVector};

use hmac::{Hmac, Mac};
use sha2::Sha256;
type HmacSha256 = Hmac<Sha256>;

/// The schema for a collection - combining the schema for individual records with the collection's
/// index specifications.
#[derive(Deserialize, Debug)]
pub struct CollectionSchema {
    // FIXME: Made these public for the Cloudflare demo (discuss)
    pub indexes: HashMap<String, MappingWithMeta>,
    #[serde(rename = "type")]
    pub schema: RecordSchema,
}

impl CollectionSchema {
    pub fn with_index_key(
        key: [u8; 32],
        mut indexes: HashMap<String, MappingWithMeta>,
        schema: HashMap<String, SchemaField, RandomState>,
    ) -> Result<Self, CollectionSchemaError> {
        for (name, meta) in indexes.iter_mut() {
            let mut mac = HmacSha256::new_from_slice(&key)
                .map_err(|_| CollectionSchemaError::InvalidIndexKey)?;
            mac.update(format!("{}:ORE", name).as_bytes());
            let field_key = mac.finalize().into_bytes();
            meta.prf_key.copy_from_slice(&field_key[0..16]);
            meta.prp_key.copy_from_slice(&field_key[16..]);
        }

        Ok(Self {
            indexes,
            schema: RecordSchema { map: schema },
        })
    }
}

/// An indexer that can index an entire record based on a [`CollectionSchema`]
pub struct Indexer {
    indexes: HashMap<String, MappingIndexer>,
    decoder: RecordDecoder,
}

impl Indexer {
    /// Create an indexer from a [`CollectionSchema`]
    pub fn from_collection_schema(
        CollectionSchema { indexes, schema }: CollectionSchema,
    ) -> Result<Self, LoadIndexerError> {
        let decoder = RecordDecoder { schema };

        let indexes = indexes
            .into_iter()
            .map(|(key, mapping)| {
                MappingIndexer::from_mapping(mapping).map(|indexer| (key, indexer))
            })
            .collect::<Result<HashMap<_, _>, _>>()?;

        Ok(Self { decoder, indexes })
    }

    /// Decode a record indexer from a CBOR byte array
    ///
    /// The CBOR byte array is a serialized [`CollectionSchema`] struct.
    pub fn decode_from_cbor(bytes: &[u8]) -> Result<Self, LoadIndexerError> {
        let schema: CollectionSchema = from_reader(bytes)?;

        Self::from_collection_schema(schema)
    }

    /// Encrypt a record and return an array of [`TermVector`] used for indexing
    pub fn encrypt(&mut self, record: &Record) -> Result<Vec<TermVector>, RecordIndexingError> {
        self.indexes
            .values_mut()
            .filter_map(|index| index.encrypt(record).transpose())
            .collect()
    }

    /// Encrypt a query and return an array of [`Constraint`] used for querying
    pub fn encrypt_query(&mut self, query: Query) -> Result<Vec<Constraint>, QueryIndexingError> {
        let mut output: Vec<Constraint> = vec![];

        fn traverse_query(
            query: Query,
            indexes: &mut HashMap<String, MappingIndexer>,
            output: &mut Vec<Constraint>,
        ) -> Result<(), QueryIndexingError> {
            match query {
                Query::Basic { index_name, kind } => {
                    let mapping = indexes
                        .get_mut(&index_name)
                        .ok_or(QueryIndexingError::InvalidIndexName(index_name))?;

                    if let Some(c) = mapping.encrypt_query(kind)? {
                        output.extend(c.into_iter());
                    }
                }
                Query::Conjunctive(ConjunctiveCondition::All { conditions }) => {
                    for q in conditions.into_iter() {
                        traverse_query(q, indexes, output)?;
                    }
                }
            }

            Ok(())
        }

        traverse_query(query, &mut self.indexes, &mut output)?;

        Ok(output)
    }

    /// Decode a record from CBOR, encrypt it using ORE and return the result as a CBOR byte
    /// buffer.
    ///
    /// The CBOR byte array is a serialized array of [`TermVector`]s.
    pub fn encrypt_cbor(&mut self, record_bytes: &[u8]) -> Result<Vec<u8>, RecordIndexingError> {
        let mut output = vec![];
        let record = self.decoder.decode(record_bytes)?;
        into_writer(&self.encrypt(&record)?, &mut output)?;
        Ok(output)
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        record::{
            decoder::{DataType, RecordSchema, SchemaField},
            Record,
        },
        test_utils::{cbor_buffer, collection},
    };

    use super::{
        errors::QueryIndexingError,
        mapping::{Mapping, MappingWithMeta},
        query::*,
        CollectionSchema, Indexer,
    };

    use serde_bytes::Bytes;
    use uuid::uuid;

    #[test]
    fn test_load_schema_from_cbor() {
        let buf = Bytes::new(&[0_u8; 16]);

        let buffer = cbor_buffer!({
            "type" => {
                "title" => "string",
                "runningTime" => "uint64",
                "year" => "uint64"
            },
            "indexes" => {
                "exactTitle" => {
                    "kind" => "exact",
                    "field" => "title",
                    "prf_key" => &buf,
                    "prp_key" => &buf,
                    "index_id" => &buf
                },
                "runningTime" => {
                    "kind" => "range",
                    "field" => "runningTime",
                    "prf_key" => &buf,
                    "prp_key" => &buf,
                    "index_id" => &buf
                }
            }
        });

        let indexer = Indexer::decode_from_cbor(&buffer).expect("Failed to decode");

        assert_eq!(indexer.indexes.len(), 2);

        let exact = indexer
            .indexes
            .get("exactTitle")
            .expect("Expected exactTitle index to exist");

        assert_eq!(
            exact.mapping,
            Mapping::Exact {
                field: String::from("title")
            }
        );

        assert_eq!(exact.index_id, [0; 16]);

        let time = indexer
            .indexes
            .get("runningTime")
            .expect("Expected runningTime index to exist");

        assert_eq!(
            time.mapping,
            Mapping::Range {
                field: String::from("runningTime")
            }
        );
    }

    #[test]
    fn test_load_schema_from_json() {
        let value = serde_json::json!({
            "type": {
                "title": "string",
                "runningTime": "uint64",
                "year": "uint64"
            },
            "indexes": {
                "exactTitle": {
                    "kind": "exact",
                    "field": "title",
                    "$prfKey": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                    "$prpKey": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                    "$indexId": "00000000-0000-0000-0000-000000000000"
                }
            }
        });

        let schema: CollectionSchema =
            serde_json::from_value(value).expect("Failed to decode schema");

        let indexer = Indexer::from_collection_schema(schema).expect("Failed to load indexer");

        assert_eq!(indexer.indexes.len(), 1);

        let exact = indexer
            .indexes
            .get("exactTitle")
            .expect("Expected exactTitle index to exist");

        assert_eq!(exact.index_id, [0; 16]);
    }

    fn create_test_movie_indexer() -> Indexer {
        let collection_schema = CollectionSchema {
            schema: RecordSchema {
                map: collection! {
                    "title" => SchemaField::DataType(DataType::String),
                    "runningTime" => SchemaField::DataType(DataType::Uint64)
                },
            },
            indexes: collection! {
                "exactTitle" => MappingWithMeta {
                    mapping: Mapping::Exact { field: "title".into() },
                    index_id: uuid!("00000000-0000-0000-0000-000000000000"),
                    prf_key: [0;16],
                    prp_key: [0;16]
                },
                "runningTime" => MappingWithMeta {
                    mapping: Mapping::Range { field: "runningTime".into() },
                    index_id: uuid!("00000000-0000-0000-0000-000000000000"),
                    prf_key: [0;16],
                    prp_key: [0;16]
                }
            },
        };

        Indexer::from_collection_schema(collection_schema).unwrap()
    }

    fn create_test_user_indexer() -> Indexer {
        let collection_schema = CollectionSchema {
            schema: RecordSchema {
                map: collection! {
                    "firstName" => SchemaField::DataType(DataType::String),
                    "lastName" => SchemaField::DataType(DataType::String),
                    "dob" => SchemaField::DataType(DataType::Date),
                    "stashId" => SchemaField::DataType(DataType::String)
                },
            },
            indexes: collection! {
                "firstName" => MappingWithMeta {
                    mapping: Mapping::Exact { field: "firstName".into() },
                    index_id: uuid!("00000000-0000-0000-0000-000000000000"),
                    prf_key: [0;16],
                    prp_key: [0;16]
                }
            },
        };

        Indexer::from_collection_schema(collection_schema).unwrap()
    }

    #[test]
    fn index_record_with_both_indexes() {
        let mut indexer = create_test_movie_indexer();

        let vectors = indexer
            .encrypt(&Record {
                id: [
                    163, 98, 105, 100, 216, 64, 80, 117, 189, 53, 179, 82, 84, 65, 2, 178,
                ],
                fields: collection! {
                    "title" => "Hello!",
                    "runningTime" => 230
                },
            })
            .unwrap();

        assert_eq!(vectors.len(), 2);
    }

    #[test]
    fn index_user_record_from_cbor() {
        let mut indexer = create_test_user_indexer();

        let cbuf = cbor_buffer!({
            "id" => [163,  98, 105, 100, 216, 64,
                    80, 117, 189,  53, 179, 82,
                    84,  65,   2, 178],
            "firstName" => "Dan",
            "lastName" => "Draper",
            "stashId" => "e976a7eb-5a17-46bf-aef9-d70fe77d79b5"
        });

        indexer.encrypt_cbor(&cbuf).unwrap();

        // If we get this far then nothing failed
        // TODO: do the cbor decoding and check the result
        assert!(true)
    }

    #[test]
    fn index_record_with_missing_field() {
        let mut indexer = create_test_movie_indexer();

        let vectors = indexer
            .encrypt(&Record {
                id: [1; 16],
                fields: collection! {
                    "title" => "Hello!"
                },
            })
            .unwrap();

        assert_eq!(vectors.len(), 1);
    }

    #[test]
    fn index_record_with_no_fields() {
        let mut indexer = create_test_movie_indexer();

        let vectors = indexer
            .encrypt(&Record {
                id: [1; 16],
                fields: collection! {},
            })
            .unwrap();

        assert_eq!(vectors.len(), 0);
    }

    #[test]
    fn test_query_collapse_all_query() {
        let mut indexer = create_test_movie_indexer();

        let constraints = indexer
            .encrypt_query(Query::Conjunctive(ConjunctiveCondition::All {
                conditions: vec![
                    Query::Basic {
                        index_name: "exactTitle".into(),
                        kind: QueryKind::Exact {
                            value: "test".into(),
                        },
                    },
                    Query::Basic {
                        index_name: "runningTime".into(),
                        kind: QueryKind::Range {
                            value: RangeValue::Between {
                                min: 0.into(),
                                max: 100.into(),
                            },
                        },
                    },
                ],
            }))
            .expect("Failed to encrypt query");

        assert_eq!(constraints.len(), 2)
    }

    #[test]
    fn test_nested_all_are_collapsed() {
        let mut indexer = create_test_movie_indexer();

        let constraints = indexer
            .encrypt_query(Query::Conjunctive(ConjunctiveCondition::All {
                conditions: vec![Query::Conjunctive(ConjunctiveCondition::All {
                    conditions: vec![Query::Conjunctive(ConjunctiveCondition::All {
                        conditions: vec![Query::Basic {
                            index_name: "exactTitle".into(),
                            kind: QueryKind::Exact {
                                value: "test".into(),
                            },
                        }],
                    })],
                })],
            }))
            .expect("Failed to encrypt query");

        assert_eq!(constraints.len(), 1)
    }

    #[test]
    fn test_encrypt_single_query() {
        let mut indexer = create_test_movie_indexer();

        let constraints = indexer
            .encrypt_query(Query::Basic {
                index_name: "exactTitle".into(),
                kind: QueryKind::Exact {
                    value: "test".into(),
                },
            })
            .expect("Failed to encrypt query");

        assert_eq!(constraints.len(), 1)
    }

    #[test]
    fn test_fail_when_querying_invalid_index() {
        let mut indexer = create_test_movie_indexer();

        let err = indexer
            .encrypt_query(Query::Basic {
                index_name: "not-a-real-index".into(),
                kind: QueryKind::Exact {
                    value: "test".into(),
                },
            })
            .expect_err("Expected encryption to fail");

        assert_eq!(
            err,
            QueryIndexingError::InvalidIndexName("not-a-real-index".into())
        );
    }

    #[test]
    fn test_fail_when_index_is_wrong_type() {
        let mut indexer = create_test_movie_indexer();

        let err = indexer
            .encrypt_query(Query::Basic {
                index_name: "exactTitle".into(),
                kind: QueryKind::Range {
                    value: RangeValue::Single {
                        operator: RangeOperator::Eq,
                        value: 10.into(),
                    },
                },
            })
            .expect_err("Expected encryption to fail");

        assert_eq!(
            err,
            QueryIndexingError::InvalidQueryMappingPair {
                query_name: "range",
                mapping_name: "exact"
            }
        );
    }

    fn collection_schema_from_index_key() {
        let index_key = [0u8; 32];
        let collection_schema = CollectionSchema::with_index_key(
            index_key,
            collection! {
                "exactTitle" => MappingWithMeta {
                    mapping: Mapping::Exact { field: "title".into() },
                    index_id: uuid!("00000000-0000-0000-0000-000000000000"),
                    prf_key: [0;16],
                    prp_key: [0;16]
                },
                "runningTime" => MappingWithMeta {
                    mapping: Mapping::Range { field: "runningTime".into() },
                    index_id: uuid!("00000000-0000-0000-0000-000000000000"),
                    prf_key: [0;16],
                    prp_key: [0;16]
                }
            },
            collection! {
                "title" => SchemaField::DataType(DataType::String),
                "runningTime" => SchemaField::DataType(DataType::Uint64)
            },
        )
        .unwrap();

        if let Some(meta) = collection_schema.indexes.get("exactTitle") {
            // TODO: Handle the index_id too
            assert_ne!(meta.prf_key, [0u8; 16]);
            assert_ne!(meta.prp_key, [0u8; 16]);
        } else {
            assert!(false);
        }
    }
}