1
use std::borrow::Cow;
2
use std::collections::hash_map::RandomState;
3
use std::collections::{BTreeMap, BTreeSet, HashSet};
4
use std::sync::Arc;
5

            
6
use bonsaidb_core::arc_bytes::serde::Bytes;
7
use bonsaidb_core::arc_bytes::{ArcBytes, OwnedBytes};
8
use bonsaidb_core::connection::Connection;
9
use bonsaidb_core::schema::view::{self, map, Serialized, ViewUpdatePolicy};
10
use bonsaidb_core::schema::{CollectionName, ViewName};
11
use easy_parallel::Parallel;
12
use nebari::io::any::AnyFile;
13
use nebari::tree::{AnyTreeRoot, CompareSwap, KeyOperation, Operation, Unversioned, Versioned};
14
use nebari::{LockedTransactionTree, Tree, UnlockedTransactionTree};
15

            
16
use crate::database::{deserialize_document, document_tree_name, Database};
17
use crate::tasks::{Job, Keyed, Task};
18
use crate::views::{
19
    view_document_map_tree_name, view_entries_tree_name, view_invalidated_docs_tree_name,
20
    EntryMapping, ViewEntry,
21
};
22
use crate::Error;
23

            
24
#[derive(Debug)]
25
pub struct Mapper {
26
    pub database: Database,
27
    pub map: Map,
28
}
29

            
30
647794
#[derive(Debug, Hash, Eq, PartialEq, Clone)]
31
pub struct Map {
32
    pub database: Arc<Cow<'static, str>>,
33
    pub collection: CollectionName,
34
    pub view_name: ViewName,
35
}
36

            
37
impl Job for Mapper {
38
    type Error = Error;
39
    type Output = u64;
40

            
41
    #[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", skip_all))]
42
    #[allow(clippy::too_many_lines)]
43
212533
    fn execute(&mut self) -> Result<Self::Output, Error> {
44
212533
        let documents =
45
212533
            self.database
46
212533
                .roots()
47
212533
                .tree(self.database.collection_tree::<Versioned, _>(
48
212533
                    &self.map.collection,
49
212533
                    document_tree_name(&self.map.collection),
50
212533
                )?)?;
51

            
52
212533
        let view_entries =
53
212533
            self.database
54
212533
                .roots()
55
212533
                .tree(self.database.collection_tree::<Unversioned, _>(
56
212533
                    &self.map.collection,
57
212533
                    view_entries_tree_name(&self.map.view_name),
58
212533
                )?)?;
59

            
60
212533
        let document_map =
61
212533
            self.database
62
212533
                .roots()
63
212533
                .tree(self.database.collection_tree::<Unversioned, _>(
64
212533
                    &self.map.collection,
65
212533
                    view_document_map_tree_name(&self.map.view_name),
66
212533
                )?)?;
67

            
68
212533
        let invalidated_entries =
69
212533
            self.database
70
212533
                .roots()
71
212533
                .tree(self.database.collection_tree::<Unversioned, _>(
72
212533
                    &self.map.collection,
73
212533
                    view_invalidated_docs_tree_name(&self.map.view_name),
74
212533
                )?)?;
75

            
76
212533
        let transaction_id = self
77
212533
            .database
78
212533
            .last_transaction_id()?
79
212533
            .expect("no way to have documents without a transaction");
80
212533

            
81
212533
        let storage = self.database.clone();
82
212533
        let map_request = self.map.clone();
83
212533

            
84
212533
        map_view(
85
212533
            &invalidated_entries,
86
212533
            &document_map,
87
212533
            &documents,
88
212533
            &view_entries,
89
212533
            &storage,
90
212533
            &map_request,
91
212533
        )?;
92

            
93
212533
        self.database.storage.instance.tasks().mark_view_updated(
94
212533
            self.map.database.clone(),
95
212533
            self.map.collection.clone(),
96
212533
            self.map.view_name.clone(),
97
212533
            transaction_id,
98
212533
        );
99
212533

            
100
212533
        Ok(transaction_id)
101
212533
    }
102
}
103

            
104
212533
fn map_view(
105
212533
    invalidated_entries: &Tree<Unversioned, AnyFile>,
106
212533
    document_map: &Tree<Unversioned, AnyFile>,
107
212533
    documents: &Tree<Versioned, AnyFile>,
108
212533
    view_entries: &Tree<Unversioned, AnyFile>,
109
212533
    database: &Database,
110
212533
    map_request: &Map,
111
212533
) -> Result<(), Error> {
112
    const CHUNK_SIZE: usize = 100_000;
113
    // Only do any work if there are invalidated documents to process
114
212533
    let mut invalidated_ids = invalidated_entries
115
212533
        .get_range(&(..))?
116
212533
        .into_iter()
117
212533
        .map(|(key, _)| key)
118
212533
        .collect::<Vec<_>>();
119
233456
    while !invalidated_ids.is_empty() {
120
20923
        let transaction = database
121
20923
            .roots()
122
20923
            .transaction::<_, dyn AnyTreeRoot<AnyFile>>(&[
123
20923
                Box::new(invalidated_entries.clone()) as Box<dyn AnyTreeRoot<AnyFile>>,
124
20923
                Box::new(document_map.clone()),
125
20923
                Box::new(documents.clone()),
126
20923
                Box::new(view_entries.clone()),
127
20923
            ])?;
128
        {
129
20923
            let view = database
130
20923
                .data
131
20923
                .schema
132
20923
                .view_by_name(&map_request.view_name)
133
20923
                .unwrap();
134
20923

            
135
20923
            let document_ids = invalidated_ids
136
20923
                .drain(invalidated_ids.len().saturating_sub(CHUNK_SIZE)..)
137
20923
                .collect::<Vec<_>>();
138
20923
            let document_map = transaction.unlocked_tree(1).unwrap();
139
20923
            let documents = transaction.unlocked_tree(2).unwrap();
140
20923
            let view_entries = transaction.unlocked_tree(3).unwrap();
141
20923
            DocumentRequest {
142
20923
                document_ids: document_ids.clone(),
143
20923
                map_request,
144
20923
                database,
145
20923
                document_map,
146
20923
                documents,
147
20923
                view_entries,
148
20923
                view,
149
20923
            }
150
20923
            .map()?;
151

            
152
20923
            let mut invalidated_entries = transaction.tree::<Unversioned>(0).unwrap();
153
20923
            invalidated_entries.modify(document_ids, nebari::tree::Operation::Remove)?;
154
        }
155
20923
        transaction.commit()?;
156
    }
157

            
158
212533
    Ok(())
159
212533
}
160

            
161
pub struct DocumentRequest<'a> {
162
    pub document_ids: Vec<ArcBytes<'static>>,
163
    pub map_request: &'a Map,
164
    pub database: &'a Database,
165

            
166
    pub document_map: &'a UnlockedTransactionTree<AnyFile>,
167
    pub documents: &'a UnlockedTransactionTree<AnyFile>,
168
    pub view_entries: &'a UnlockedTransactionTree<AnyFile>,
169
    pub view: &'a dyn Serialized,
170
}
171

            
172
type DocumentIdPayload = (ArcBytes<'static>, Option<ArcBytes<'static>>);
173
type BatchPayload = (Vec<ArcBytes<'static>>, flume::Receiver<DocumentIdPayload>);
174

            
175
impl<'a> DocumentRequest<'a> {
176
452268
    fn generate_batches(
177
452268
        batch_sender: flume::Sender<BatchPayload>,
178
452268
        document_ids: &[ArcBytes<'static>],
179
452268
        documents: &UnlockedTransactionTree<AnyFile>,
180
452268
    ) -> Result<(), Error> {
181
452268
        // Generate batches
182
452268
        let mut documents = documents.lock::<Versioned>();
183
452268
        for chunk in document_ids.chunks(1024) {
184
452268
            let (document_id_sender, document_id_receiver) = flume::bounded(chunk.len());
185
452268
            batch_sender
186
452268
                .send((chunk.to_vec(), document_id_receiver))
187
452268
                .unwrap();
188
452268
            let mut documents = documents.get_multiple(chunk.iter().map(ArcBytes::as_slice))?;
189
452268
            documents.sort_by(|a, b| a.0.cmp(&b.0));
190

            
191
532520
            for document_id in chunk.iter().rev() {
192
532520
                let document = documents
193
532520
                    .last()
194
532520
                    .map_or(false, |(key, _)| (key == document_id))
195
532520
                    .then(|| documents.pop().unwrap().1);
196
532520

            
197
532520
                document_id_sender
198
532520
                    .send((document_id.clone(), document))
199
532520
                    .unwrap();
200
532520
            }
201

            
202
452268
            drop(document_id_sender);
203
        }
204
452268
        drop(batch_sender);
205
452268
        Ok(())
206
452268
    }
207

            
208
452268
    fn map_batches(
209
452268
        batch_receiver: &flume::Receiver<BatchPayload>,
210
452268
        mapped_sender: flume::Sender<Batch>,
211
452268
        view: &dyn Serialized,
212
452268
        parallelization: usize,
213
452268
    ) -> Result<(), Error> {
214
        // Process batches
215
904536
        while let Ok((document_ids, document_id_receiver)) = batch_receiver.recv() {
216
452268
            let mut batch = Batch {
217
452268
                document_ids,
218
452268
                ..Batch::default()
219
452268
            };
220
904536
            for result in Parallel::new()
221
904536
                .each(1..=parallelization, |_| -> Result<_, Error> {
222
904536
                    let mut results = Vec::new();
223
1437056
                    while let Ok((document_id, document)) = document_id_receiver.recv() {
224
532520
                        let map_result = if let Some(document) = document {
225
491964
                            let document = deserialize_document(&document)?;
226

            
227
                            // Call the schema map function
228
491964
                            view.map(&document).map_err(bonsaidb_core::Error::from)?
229
                        } else {
230
                            // Get multiple didn't return this document ID.
231
40556
                            Vec::new()
232
                        };
233
532520
                        let keys: HashSet<OwnedBytes> = map_result
234
532520
                            .iter()
235
532520
                            .map(|map| OwnedBytes::from(map.key.as_slice()))
236
532520
                            .collect();
237
532520
                        let new_keys = ArcBytes::from(bincode::serialize(&keys)?);
238

            
239
532520
                        results.push((document_id, new_keys, keys, map_result));
240
                    }
241

            
242
904536
                    Ok(results)
243
904536
                })
244
452268
                .run()
245
            {
246
904536
                for (document_id, new_keys, keys, map_result) in result? {
247
1024841
                    for key in &keys {
248
492321
                        batch.all_keys.insert(key.0.clone());
249
492321
                    }
250
532520
                    batch.document_maps.insert(document_id.clone(), new_keys);
251
532520
                    batch.document_keys.insert(document_id.clone(), keys);
252
1024841
                    for mapping in map_result {
253
492321
                        let key_mappings = batch
254
492321
                            .new_mappings
255
492321
                            .entry(ArcBytes::from(mapping.key.to_vec()))
256
492321
                            .or_insert_with(Vec::default);
257
492321
                        key_mappings.push(mapping);
258
492321
                    }
259
                }
260
            }
261
452268
            mapped_sender.send(batch).unwrap();
262
        }
263
452268
        drop(mapped_sender);
264
452268
        Ok(())
265
452268
    }
266

            
267
452268
    fn update_document_map(
268
452268
        document_ids: Vec<ArcBytes<'static>>,
269
452268
        document_map: &mut LockedTransactionTree<'_, Unversioned, AnyFile>,
270
452268
        document_maps: &BTreeMap<ArcBytes<'static>, ArcBytes<'static>>,
271
452268
        mut document_keys: BTreeMap<ArcBytes<'static>, HashSet<OwnedBytes>>,
272
452268
        all_keys: &mut BTreeSet<ArcBytes<'static>>,
273
452268
    ) -> Result<BTreeMap<ArcBytes<'static>, HashSet<ArcBytes<'static>>>, Error> {
274
452268
        // We need to store a record of all the mappings this document produced.
275
452268
        let mut maps_to_clear = Vec::new();
276
452268
        document_map.modify(
277
452268
            document_ids,
278
452268
            nebari::tree::Operation::CompareSwap(CompareSwap::new(&mut |key, value| {
279
532520
                if let Some(existing_map) = value {
280
80644
                    maps_to_clear.push((key.to_owned(), existing_map));
281
451876
                }
282
532520
                let new_map = document_maps.get(key).unwrap();
283
532520
                KeyOperation::Set(new_map.clone())
284
532520
            })),
285
452268
        )?;
286
452268
        let mut view_entries_to_clean = BTreeMap::new();
287
532912
        for (document_id, existing_map) in maps_to_clear {
288
80644
            let existing_keys = bincode::deserialize::<HashSet<OwnedBytes>>(&existing_map)?;
289
80644
            let new_keys = document_keys.remove(&document_id).unwrap();
290
80644
            for key in existing_keys.difference(&new_keys) {
291
42104
                all_keys.insert(key.clone().0);
292
42104
                let key_documents = view_entries_to_clean
293
42104
                    .entry(key.clone().0)
294
42104
                    .or_insert_with(HashSet::<_, RandomState>::default);
295
42104
                key_documents.insert(document_id.clone());
296
42104
            }
297
        }
298
452268
        Ok(view_entries_to_clean)
299
452268
    }
300

            
301
452268
    fn update_view_entries(
302
452268
        view: &dyn Serialized,
303
452268
        map_request: &Map,
304
452268
        view_entries: &mut LockedTransactionTree<'_, Unversioned, AnyFile>,
305
452268
        all_keys: BTreeSet<ArcBytes<'static>>,
306
452268
        view_entries_to_clean: BTreeMap<ArcBytes<'static>, HashSet<ArcBytes<'static>>>,
307
452268
        new_mappings: BTreeMap<ArcBytes<'static>, Vec<map::Serialized>>,
308
452268
    ) -> Result<(), Error> {
309
452268
        let mut updater = ViewEntryUpdater {
310
452268
            view,
311
452268
            map_request,
312
452268
            view_entries_to_clean,
313
452268
            new_mappings,
314
452268
            result: Ok(()),
315
452268
            has_reduce: true,
316
452268
        };
317
452268
        view_entries
318
452268
            .modify(
319
452268
                all_keys.into_iter().collect(),
320
487321
                Operation::CompareSwap(CompareSwap::new(&mut |key, view_entries| {
321
487321
                    updater.compare_swap_view_entry(key, view_entries)
322
487321
                })),
323
452268
            )
324
452268
            .map_err(Error::from)
325
452268
            .and(updater.result)
326
452268
    }
327

            
328
452268
    fn save_mappings(
329
452268
        mapped_receiver: &flume::Receiver<Batch>,
330
452268
        view: &dyn Serialized,
331
452268
        map_request: &Map,
332
452268
        document_map: &mut LockedTransactionTree<'_, Unversioned, AnyFile>,
333
452268
        view_entries: &mut LockedTransactionTree<'_, Unversioned, AnyFile>,
334
452268
    ) -> Result<(), Error> {
335
        while let Ok(Batch {
336
452268
            document_ids,
337
452268
            document_maps,
338
452268
            document_keys,
339
452268
            new_mappings,
340
452268
            mut all_keys,
341
903834
        }) = mapped_receiver.recv()
342
        {
343
452268
            let view_entries_to_clean = Self::update_document_map(
344
452268
                document_ids,
345
452268
                document_map,
346
452268
                &document_maps,
347
452268
                document_keys,
348
452268
                &mut all_keys,
349
452268
            )?;
350

            
351
452268
            Self::update_view_entries(
352
452268
                view,
353
452268
                map_request,
354
452268
                view_entries,
355
452268
                all_keys,
356
452268
                view_entries_to_clean,
357
452268
                new_mappings,
358
452268
            )?;
359
        }
360
451566
        Ok(())
361
452268
    }
362

            
363
452268
    pub fn map(&mut self) -> Result<(), Error> {
364
452268
        let (batch_sender, batch_receiver) = flume::bounded(1);
365
452268
        let (mapped_sender, mapped_receiver) = flume::bounded(1);
366

            
367
1356804
        for result in Parallel::new()
368
452268
            .add(|| Self::generate_batches(batch_sender, &self.document_ids, self.documents))
369
452268
            .add(|| {
370
452268
                Self::map_batches(
371
452268
                    &batch_receiver,
372
452268
                    mapped_sender,
373
452268
                    self.view,
374
452268
                    self.database.storage().parallelization(),
375
452268
                )
376
452268
            })
377
452268
            .add(|| {
378
452268
                let mut document_map = self.document_map.lock();
379
452268
                let mut view_entries = self.view_entries.lock();
380
452268
                Self::save_mappings(
381
452268
                    &mapped_receiver,
382
452268
                    self.view,
383
452268
                    self.map_request,
384
452268
                    &mut document_map,
385
452268
                    &mut view_entries,
386
452268
                )
387
452268
            })
388
452268
            .run()
389
        {
390
1356804
            result?;
391
        }
392

            
393
451566
        Ok(())
394
452268
    }
395
}
396

            
397
452268
#[derive(Default)]
398
struct Batch {
399
    document_ids: Vec<ArcBytes<'static>>,
400
    document_maps: BTreeMap<ArcBytes<'static>, ArcBytes<'static>>,
401
    document_keys: BTreeMap<ArcBytes<'static>, HashSet<OwnedBytes>>,
402
    new_mappings: BTreeMap<ArcBytes<'static>, Vec<map::Serialized>>,
403
    all_keys: BTreeSet<ArcBytes<'static>>,
404
}
405

            
406
impl Keyed<Task> for Mapper {
407
222728
    fn key(&self) -> Task {
408
222728
        Task::ViewMap(self.map.clone())
409
222728
    }
410
}
411

            
412
struct ViewEntryUpdater<'a> {
413
    view: &'a dyn Serialized,
414
    map_request: &'a Map,
415
    view_entries_to_clean: BTreeMap<ArcBytes<'static>, HashSet<ArcBytes<'static>>>,
416
    new_mappings: BTreeMap<ArcBytes<'static>, Vec<map::Serialized>>,
417
    result: Result<(), Error>,
418
    has_reduce: bool,
419
}
420

            
421
impl<'a> ViewEntryUpdater<'a> {
422
487321
    fn compare_swap_view_entry(
423
487321
        &mut self,
424
487321
        key: &ArcBytes<'_>,
425
487321
        view_entries: Option<ArcBytes<'static>>,
426
487321
    ) -> KeyOperation<ArcBytes<'static>> {
427
487321
        let mut view_entry = view_entries
428
487321
            .and_then(|view_entries| bincode::deserialize::<ViewEntry>(&view_entries).ok())
429
487321
            .unwrap_or_else(|| ViewEntry {
430
174831
                key: Bytes::from(key.to_vec()),
431
174831
                view_version: self.view.version(),
432
174831
                mappings: vec![],
433
174831
                reduced_value: Bytes::default(),
434
487321
            });
435
487321
        let key = key.to_owned();
436
487321
        if let Some(document_ids) = self.view_entries_to_clean.remove(&key) {
437
39828
            view_entry
438
39828
                .mappings
439
43132
                .retain(|m| !document_ids.contains(m.source.id.as_ref()));
440
39828

            
441
39828
            if view_entry.mappings.is_empty() && !self.new_mappings.contains_key(&key[..]) {
442
38796
                return KeyOperation::Remove;
443
1032
            } else if self.has_reduce {
444
1032
                let mappings = view_entry
445
1032
                    .mappings
446
1032
                    .iter()
447
1032
                    .map(|m| (&key[..], m.value.as_slice()))
448
1032
                    .collect::<Vec<_>>();
449
1032

            
450
1032
                match self.view.reduce(&mappings, false) {
451
1032
                    Ok(reduced) => {
452
1032
                        view_entry.reduced_value = Bytes::from(reduced);
453
1032
                    }
454
                    Err(view::Error::Core(bonsaidb_core::Error::ReduceUnimplemented)) => {
455
                        self.has_reduce = false;
456
                    }
457
                    Err(other) => {
458
                        self.result = Err(Error::from(other));
459
                        return KeyOperation::Skip;
460
                    }
461
                }
462
            }
463
447493
        }
464

            
465
448525
        if let Some(new_mappings) = self.new_mappings.remove(&key[..]) {
466
939260
            for map::Serialized { source, value, .. } in new_mappings {
467
                // Before altering any data, verify that the key is unique if this is a unique view.
468
492321
                if self.view.update_policy() == ViewUpdatePolicy::Unique
469
94719
                    && !view_entry.mappings.is_empty()
470
1654
                    && view_entry.mappings[0].source.id != source.id
471
                {
472
702
                    self.result = Err(Error::Core(bonsaidb_core::Error::UniqueKeyViolation {
473
702
                        view: self.map_request.view_name.clone(),
474
702
                        conflicting_document: Box::new(source),
475
702
                        existing_document: Box::new(view_entry.mappings[0].source.clone()),
476
702
                    }));
477
702
                    return KeyOperation::Skip;
478
491619
                }
479
491619
                let entry_mapping = EntryMapping { source, value };
480
491619

            
481
491619
                // attempt to update an existing
482
491619
                // entry for this document, if
483
491619
                // present
484
491619
                let mut found = false;
485
74951159
                for mapping in &mut view_entry.mappings {
486
74498376
                    if mapping.source.id == entry_mapping.source.id {
487
38836
                        found = true;
488
38836
                        mapping.source.revision = entry_mapping.source.revision;
489
38836
                        mapping.value = entry_mapping.value.clone();
490
38836
                        break;
491
74459540
                    }
492
                }
493

            
494
                // If an existing mapping wasn't
495
                // found, add it
496
491619
                if !found {
497
452783
                    view_entry.mappings.push(entry_mapping);
498
452783
                }
499
            }
500

            
501
            // There was a choice to be made here of whether to call
502
            // reduce()  with all of the existing values, or call it with
503
            // rereduce=true passing only the new value and the old stored
504
            // value. In this implementation, it's technically less
505
            // efficient, but we can guarantee that every value has only
506
            // been reduced once, and potentially re-reduced a single-time.
507
            // If we constantly try to update the value to optimize the size
508
            // of `mappings`, the fear is that the value computed may lose
509
            // precision in some contexts over time. Thus, the decision was
510
            // made to always call reduce() with all the mappings within a
511
            // single ViewEntry.
512
446939
            if self.has_reduce {
513
446639
                let mappings = view_entry
514
446639
                    .mappings
515
446639
                    .iter()
516
74793575
                    .map(|m| (key.as_slice(), m.value.as_slice()))
517
446639
                    .collect::<Vec<_>>();
518
446639

            
519
446639
                match self.view.reduce(&mappings, false) {
520
349573
                    Ok(reduced) => {
521
349573
                        view_entry.reduced_value = Bytes::from(reduced);
522
349573
                    }
523
97066
                    Err(view::Error::Core(bonsaidb_core::Error::ReduceUnimplemented)) => {
524
97066
                        self.has_reduce = false;
525
97066
                    }
526
                    Err(other) => {
527
                        self.result = Err(Error::from(other));
528
                        return KeyOperation::Skip;
529
                    }
530
                }
531
300
            }
532
884
        }
533

            
534
447823
        let value = bincode::serialize(&view_entry).unwrap();
535
447823
        KeyOperation::Set(ArcBytes::from(value))
536
487321
    }
537
}