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
use std::collections::BTreeMap;
use std::fmt::Debug;

use arc_bytes::serde::Bytes;
use serde::{Deserialize, Serialize};

use crate::document::{CollectionHeader, DocumentId, Header, OwnedDocument};
use crate::schema::view::{self, ByteSource, Key, SerializedView, View, ViewSchema};
use crate::schema::Collection;

/// A document's entry in a View's mappings.
#[derive(Eq, PartialEq, Debug)]
pub struct Map<K = (), V = ()> {
    /// The header of the document that emitted this entry.
    pub source: Header,

    /// The key used to index the View.
    pub key: K,

    /// An associated value stored in the view.
    pub value: V,
}

impl<K, V> Map<K, V> {
    /// Serializes this map.
    pub(crate) fn serialized<'a, View>(&self) -> Result<Serialized, view::Error>
    where
        K: Key<'a>,
        View: SerializedView<Value = V>,
    {
        Ok(Serialized {
            source: self.source.clone(),
            key: Bytes::from(
                self.key
                    .as_ord_bytes()
                    .map_err(view::Error::key_serialization)?
                    .to_vec(),
            ),
            value: Bytes::from(View::serialize(&self.value)?),
        })
    }
}

impl<K, V> Map<K, V> {
    /// Creates a new Map entry for the document with id `source`.
    pub const fn new(source: Header, key: K, value: V) -> Self {
        Self { source, key, value }
    }
}

/// A document's entry in a View's mappings.
#[derive(Eq, PartialEq, Debug)]
pub struct CollectionMap<PrimaryKey, K = (), V = ()> {
    /// The header of the document that emitted this entry.
    pub source: CollectionHeader<PrimaryKey>,

    /// The key used to index the View.
    pub key: K,

    /// An associated value stored in the view.
    pub value: V,
}

/// This type is the result of `query()`. It is a list of mappings, which
/// contains:
///
/// - The key emitted during the map function.
/// - The value emitted during the map function.
/// - The source document header that the mappings originated from.
pub type ViewMappings<V> = Vec<
    CollectionMap<
        <<V as View>::Collection as Collection>::PrimaryKey,
        <V as View>::Key,
        <V as View>::Value,
    >,
>;

/// A collection of [`Map`]s.
#[derive(Debug, Eq, PartialEq)]
#[must_use]
pub enum Mappings<K = (), V = ()> {
    /// Zero or one mappings.
    Simple(Option<Map<K, V>>),
    /// More than one mapping.
    List(Vec<Map<K, V>>),
}

impl<K, V> Default for Mappings<K, V> {
    fn default() -> Self {
        Self::none()
    }
}

impl<K, V> Mappings<K, V> {
    /// Returns an empty collection of mappings.
    pub const fn none() -> Self {
        Self::Simple(None)
    }

    /// Appends `mapping` to the end of this collection.
    pub fn push(&mut self, mapping: Map<K, V>) {
        match self {
            Self::Simple(existing_mapping) => {
                *self = if let Some(existing_mapping) = existing_mapping.take() {
                    Self::List(vec![existing_mapping, mapping])
                } else {
                    Self::Simple(Some(mapping))
                };
            }
            Self::List(vec) => vec.push(mapping),
        }
    }

    /// Appends `mappings` to the end of this collection and returns self.
    pub fn and(mut self, mappings: Self) -> Self {
        self.extend(mappings);
        self
    }

    /// Returns an iterator for these mappings.
    pub fn iter(&self) -> MappingsIter<'_, K, V> {
        self.into_iter()
    }

    /// Returns the number of mappings contained.
    pub fn len(&self) -> usize {
        match self {
            Mappings::Simple(None) => 0,
            Mappings::Simple(Some(_)) => 1,
            Mappings::List(v) => v.len(),
        }
    }

    /// Returns true if there are no mappings in this collection.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<K, V> Extend<Map<K, V>> for Mappings<K, V> {
    fn extend<T: IntoIterator<Item = Map<K, V>>>(&mut self, iter: T) {
        let iter = iter.into_iter();
        for map in iter {
            self.push(map);
        }
    }
}

impl<K, V> FromIterator<Map<K, V>> for Mappings<K, V> {
    fn from_iter<T: IntoIterator<Item = Map<K, V>>>(iter: T) -> Self {
        let mut mappings = Self::none();
        mappings.extend(iter);
        mappings
    }
}

impl<K, V> FromIterator<Self> for Mappings<K, V> {
    fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self {
        let mut iter = iter.into_iter();
        if let Some(mut collected) = iter.next() {
            for mappings in iter {
                collected.extend(mappings);
            }
            collected
        } else {
            Self::none()
        }
    }
}

impl<K, V> IntoIterator for Mappings<K, V> {
    type IntoIter = MappingsIntoIter<K, V>;
    type Item = Map<K, V>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            Mappings::Simple(option) => MappingsIntoIter::Inline(option),
            Mappings::List(list) => MappingsIntoIter::Vec(list.into_iter()),
        }
    }
}

impl<'a, K, V> IntoIterator for &'a Mappings<K, V> {
    type IntoIter = MappingsIter<'a, K, V>;
    type Item = &'a Map<K, V>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            Mappings::Simple(option) => MappingsIter::Inline(option.iter()),
            Mappings::List(list) => MappingsIter::Vec(list.iter()),
        }
    }
}

/// An iterator over [`Mappings`] that returns owned [`Map`] entries.
pub enum MappingsIntoIter<K = (), V = ()> {
    /// An iterator over a [`Mappings::Simple`] value.
    Inline(Option<Map<K, V>>),
    /// An iterator over a [`Mappings::List`] value.
    Vec(std::vec::IntoIter<Map<K, V>>),
}

impl<K, V> Iterator for MappingsIntoIter<K, V> {
    type Item = Map<K, V>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            MappingsIntoIter::Inline(opt) => opt.take(),
            MappingsIntoIter::Vec(iter) => iter.next(),
        }
    }
}

/// An iterator over [`Mappings`] that returns [`Map`] entry references.
pub enum MappingsIter<'a, K = (), V = ()> {
    /// An iterator over a [`Mappings::Simple`] value.
    Inline(std::option::Iter<'a, Map<K, V>>),
    /// An iterator over a [`Mappings::List`] value.
    Vec(std::slice::Iter<'a, Map<K, V>>),
}

impl<'a, K, V> Iterator for MappingsIter<'a, K, V> {
    type Item = &'a Map<K, V>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            MappingsIter::Inline(i) => i.next(),
            MappingsIter::Vec(i) => i.next(),
        }
    }
}

/// A collection of mappings and the associated documents.
pub struct MappedDocuments<D, V: View> {
    /// The collection of mappings.
    pub mappings: ViewMappings<V>,
    /// All associated documents by ID.
    ///
    /// Documents can appear in a mapping query multiple times. As a result, they are stored separately to avoid duplication.
    pub documents: BTreeMap<<V::Collection as Collection>::PrimaryKey, D>,
}

impl<D, V: View> MappedDocuments<D, V> {
    /// The number of mappings contained in this collection.
    #[must_use]
    pub fn len(&self) -> usize {
        self.mappings.len()
    }

    /// Returns true if there are no mappings in this collection.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the mapped document at`index`, or `None` if `index >=
    /// self.len()`.
    #[must_use]
    #[allow(clippy::missing_panics_doc)]
    pub fn get(&self, index: usize) -> Option<MappedDocument<'_, D, V::Key, V::Value>> {
        if index < self.len() {
            let mapping = &self.mappings[index];
            let document = self
                .documents
                .get(&mapping.source.id)
                .expect("missing mapped document");
            Some(MappedDocument {
                key: &mapping.key,
                value: &mapping.value,
                document,
            })
        } else {
            None
        }
    }
}

impl<D, V: View> Debug for MappedDocuments<D, V>
where
    V::Key: Debug,
    V::Value: Debug,
    D: Debug,
    <V::Collection as Collection>::PrimaryKey: Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MappedDocuments")
            .field("mappings", &self.mappings)
            .field("documents", &self.documents)
            .finish()
    }
}

/// An iterator of mapped documents.
pub struct MappedDocumentsIter<'a, D, V: View> {
    docs: &'a MappedDocuments<D, V>,
    index: usize,
}

impl<'a, D, V: View> IntoIterator for &'a MappedDocuments<D, V> {
    type IntoIter = MappedDocumentsIter<'a, D, V>;
    type Item = MappedDocument<'a, D, V::Key, V::Value>;

    fn into_iter(self) -> Self::IntoIter {
        MappedDocumentsIter {
            docs: self,
            index: 0,
        }
    }
}

impl<'a, D, V: View> Iterator for MappedDocumentsIter<'a, D, V> {
    type Item = MappedDocument<'a, D, V::Key, V::Value>;

    fn next(&mut self) -> Option<Self::Item> {
        let doc = self.docs.get(self.index);
        self.index = self.index.saturating_add(1);
        doc
    }
}

/// A mapped document returned from a view query.
pub struct MappedDocument<'a, D, K, V> {
    /// The key that this document mapped to.
    pub key: &'a K,
    /// The associated value of this key.
    pub value: &'a V,
    /// The source document of this mapping.
    pub document: &'a D,
}

/// Represents a document's entry in a View's mappings, serialized and ready to store.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Serialized {
    /// The header of the document that emitted this entry.
    pub source: Header,

    /// The key used to index the View.Operation
    pub key: Bytes,

    /// An associated value stored in the view.Operation
    pub value: Bytes,
}

impl Serialized {
    /// Deserializes this map.
    pub fn deserialized<View: SerializedView>(
        &self,
    ) -> Result<Map<View::Key, View::Value>, view::Error> {
        Ok(Map::new(
            self.source.clone(),
            <View::Key as Key>::from_ord_bytes(ByteSource::Borrowed(&self.key))
                .map_err(view::Error::key_serialization)?,
            View::deserialize(&self.value)?,
        ))
    }
}

/// A serialized [`MappedDocument`](MappedDocument).
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct MappedSerializedDocuments {
    /// The serialized mapped value.
    pub mappings: Vec<Serialized>,
    /// The source document.
    pub documents: BTreeMap<DocumentId, OwnedDocument>,
}

impl MappedSerializedDocuments {
    /// Deserialize into a [`MappedDocument`](MappedDocument).
    pub fn deserialized<View: SerializedView>(
        self,
    ) -> Result<MappedDocuments<OwnedDocument, View>, crate::Error> {
        let mappings = self
            .mappings
            .iter()
            .map(|mapping| {
                let deserialized = Serialized::deserialized::<View>(mapping)?;
                Ok(CollectionMap {
                    source: deserialized.source.try_into()?,
                    key: deserialized.key,
                    value: deserialized.value,
                })
            })
            .collect::<Result<Vec<_>, crate::Error>>()?;

        Ok(MappedDocuments {
            mappings,
            documents: self
                .documents
                .into_iter()
                .map(|(key, value)| {
                    let key = key.deserialize()?;
                    Ok((key, value))
                })
                .collect::<Result<BTreeMap<_, _>, crate::Error>>()?,
        })
    }
}

/// A key value pair
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct MappedValue<K, V> {
    /// The key responsible for generating the value
    pub key: K,

    /// The value generated by the `View`
    pub value: V,
}

impl<K, V> MappedValue<K, V> {
    /// Returns a new instance with the key/value pair.
    pub const fn new(key: K, value: V) -> Self {
        Self { key, value }
    }
}

/// A mapped value in a [`View`].
pub type ViewMappedValue<'doc, V> =
    MappedValue<<V as ViewSchema>::MappedKey<'doc>, <<V as ViewSchema>::View as View>::Value>;

/// A serialized [`MappedValue`].
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct MappedSerializedValue {
    /// The serialized key.
    pub key: Bytes,
    /// The serialized value.
    pub value: Bytes,
}