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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

macro_rules! mk_iterator_mod {
    {
        modname   = $modname:ident,
        itername  = $itername:ident,
        iteryield = $yield:ty,
        extname   = $extname:ident,
        extfnname = $extfnname:ident,
        fun       = $fun:expr
    } => {
        pub mod $modname {
            use crate::storeid::StoreId;
            #[allow(unused_imports)]
            use crate::store::FileLockEntry;
            use crate::store::Store;
            use failure::Fallible as Result;

            pub struct $itername<'a>(Box<dyn Iterator<Item = Result<StoreId>> + 'a>, &'a Store);

            impl<'a> $itername<'a>
            {
                pub fn new(inner: Box<dyn Iterator<Item = Result<StoreId>> + 'a>, store: &'a Store) -> Self {
                    $itername(inner, store)
                }
            }

            impl<'a> Iterator for $itername<'a>
            {
                type Item = Result<$yield>;

                fn next(&mut self) -> Option<Self::Item> {
                    self.0.next().map(|id| $fun(id?, self.1))
                }
            }

            pub trait $extname<'a> {
                fn $extfnname(self, store: &'a Store) -> $itername<'a>;
            }

            impl<'a, I> $extname<'a> for I
                where I: Iterator<Item = Result<StoreId>> + 'a
            {
                fn $extfnname(self, store: &'a Store) -> $itername<'a> {
                    $itername(Box::new(self), store)
                }
            }
        }
    }
}

mk_iterator_mod! {
    modname   = create,
    itername  = StoreCreateIterator,
    iteryield = FileLockEntry<'a>,
    extname   = StoreIdCreateIteratorExtension,
    extfnname = into_create_iter,
    fun       = |id: StoreId, store: &'a Store| store.create(id)
}

mk_iterator_mod! {
    modname   = delete,
    itername  = StoreDeleteIterator,
    iteryield = (),
    extname   = StoreIdDeleteIteratorExtension,
    extfnname = into_delete_iter,
    fun       = |id: StoreId, store: &'a Store| store.delete(id)
}

mk_iterator_mod! {
    modname   = get,
    itername  = StoreGetIterator,
    iteryield = Option<FileLockEntry<'a>>,
    extname   = StoreIdGetIteratorExtension,
    extfnname = into_get_iter,
    fun       = |id: StoreId, store: &'a Store| store.get(id)
}

mk_iterator_mod! {
    modname   = retrieve,
    itername  = StoreRetrieveIterator,
    iteryield = FileLockEntry<'a>,
    extname   = StoreIdRetrieveIteratorExtension,
    extfnname = into_retrieve_iter,
    fun       = |id: StoreId, store: &'a Store| store.retrieve(id)
}

#[cfg(test)]
#[allow(dead_code)]
mod compile_test {

    // This module contains code to check whether this actually compiles the way we would like it to
    // compile

    use crate::store::Store;
    use crate::storeid::StoreId;

    fn store() -> Store {
        unimplemented!("Not implemented because in compile-test")
    }

    fn test_compile_get() {
        let store = store();
        let _ = store
            .entries()
            .unwrap()
            .into_get_iter();
    }

    fn test_compile_get_result() {
        fn to_result(e: StoreId) -> Result<StoreId, ()> {
            Ok(e)
        }

        let store = store();
        let _ = store
            .entries()
            .unwrap()
            .into_get_iter();
    }
}

use crate::storeid::StoreId;
use crate::storeid::StoreIdIterator;
use self::delete::StoreDeleteIterator;
use self::get::StoreGetIterator;
use self::retrieve::StoreRetrieveIterator;
use crate::file_abstraction::iter::PathIterator;
use crate::store::Store;
use failure::Fallible as Result;

/// Iterator for iterating over all (or a subset of all) entries
///
/// The iterator now has functionality to optimize the iteration, if only a subdirectory of the
/// store is required, for example `$STORE/foo`.
///
/// This is done via functionality where the underlying iterator gets
/// altered.
///
/// As the (for the filesystem backend underlying) `walkdir::WalkDir` type is not as nice as it
/// could be, iterating over two subdirectories with one iterator is not possible. Thus, iterators
/// for two collections in the store should be build like this (untested):
///
/// ```ignore
///     store
///         .entries()?
///         .in_collection("foo")?
///         .chain(store.entries()?.in_collection("bar"))
/// ```
///
/// Functionality to exclude subdirectories is not possible with the current implementation and has
/// to be done during iteration, with filtering (as usual).
pub struct Entries<'a>(PathIterator<'a>, &'a Store);

impl<'a> Entries<'a> {

    pub(crate) fn new(pi: PathIterator<'a>, store: &'a Store) -> Self {
        Entries(pi, store)
    }

    pub fn in_collection(self, c: &str) -> Result<Self> {
        Ok(Entries(self.0.in_collection(c)?, self.1))
    }

    /// Turn `Entries` iterator into generic `StoreIdIterator`
    ///
    /// # TODO
    ///
    /// Revisit whether this can be done in a cleaner way. See commit message for why this is
    /// needed.
    pub fn into_storeid_iter(self) -> StoreIdIterator {
        use crate::storeid::StoreIdWithBase;
        use crate::storeid::IntoStoreId;

        let storepath = self.1.path().to_path_buf();

        let iter = self.0
            .into_inner()
            .map(move |r| {
                r.and_then(|path| {
                    StoreIdWithBase::from_full_path(&storepath, path)?.into_storeid()
                })
            });
        StoreIdIterator::new(Box::new(iter))
    }

    /// Transform the iterator into a StoreDeleteIterator
    ///
    /// This immitates the API from `libimagstore::iter`.
    pub fn into_delete_iter(self) -> StoreDeleteIterator<'a> {
        StoreDeleteIterator::new(Box::new(self.0.map(|r| r.map(|id| id.without_base()))), self.1)
    }

    /// Transform the iterator into a StoreGetIterator
    ///
    /// This immitates the API from `libimagstore::iter`.
    pub fn into_get_iter(self) -> StoreGetIterator<'a> {
        StoreGetIterator::new(Box::new(self.0.map(|r| r.map(|id| id.without_base()))), self.1)
    }

    /// Transform the iterator into a StoreRetrieveIterator
    ///
    /// This immitates the API from `libimagstore::iter`.
    pub fn into_retrieve_iter(self) -> StoreRetrieveIterator<'a> {
        StoreRetrieveIterator::new(Box::new(self.0.map(|r| r.map(|id| id.without_base()))), self.1)
    }

    /// Find entries where the id contains a substring
    ///
    /// This is useful for finding entries if the user supplied only a part of the ID, for example
    /// if the ID contains a UUID where the user did not specify the full UUID, E.G.:
    ///
    /// ```ignore
    ///     imag foo show 827d8596-fad1-4
    /// ```
    ///
    /// # Note
    ///
    /// The substring match is done with `contains()`.
    ///
    pub fn find_by_id_substr<'b>(self, id_substr: &'b str) -> FindContains<'a, 'b> {
        FindContains(self, id_substr)
    }

    /// Find entries where the id starts with a substring
    ///
    /// Same as `Entries::find_by_id_substr()`, but using `starts_with()` rather than `contains`.
    ///
    pub fn find_by_id_startswith<'b>(self, id_substr: &'b str) -> FindStartsWith<'a, 'b> {
        FindStartsWith(self, id_substr)
    }

}

impl<'a> Iterator for Entries<'a> {
    type Item = Result<StoreId>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|r| r.map(|id| id.without_base()))
    }
}

pub struct FindContains<'a, 'b>(Entries<'a>, &'b str);

impl<'a, 'b> Iterator for FindContains<'a, 'b> {
    type Item = Result<StoreId>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.0.next() {
                None           => return None,
                Some(Err(e))   => return Some(Err(e)),
                Some(Ok(next)) => if next.local().to_string_lossy().contains(self.1) {
                    return Some(Ok(next))
                }, // else loop
            }
        }
    }
}

pub struct FindStartsWith<'a, 'b>(Entries<'a>, &'b str);

impl<'a, 'b> Iterator for FindStartsWith<'a, 'b> {
    type Item = Result<StoreId>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.0.next() {
                None           => return None,
                Some(Err(e))   => return Some(Err(e)),
                Some(Ok(next)) => if next.local().to_string_lossy().starts_with(self.1) {
                    return Some(Ok(next))
                }, // else loop
            }
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate env_logger;

    use std::path::PathBuf;
    use std::sync::Arc;

    fn setup_logging() {
        let _ = env_logger::try_init();
    }

    use crate::store::Store;
    use crate::storeid::StoreId;
    use crate::file_abstraction::inmemory::InMemoryFileAbstraction;
    use libimagutil::variants::generate_variants;

    pub fn get_store() -> Store {
        let backend = Arc::new(InMemoryFileAbstraction::default());
        Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap()
    }

    #[test]
    fn test_entries_iterator_in_collection() {
        setup_logging();
        let store = get_store();

        let ids = {
            let base = String::from("entry");
            let variants = vec!["coll_1", "coll_2", "coll_3"];
            let modifier = |base: &String, v: &&str| {
                StoreId::new(PathBuf::from(format!("{}/{}", *v, base))).unwrap()
            };

            generate_variants(&base, variants.iter(), &modifier)
        };

        for id in ids {
            let _ = store.retrieve(id).unwrap();
        }

        let succeeded = store.entries()
            .unwrap()
            .in_collection("coll_3")
            .unwrap()
            .map(|id| { debug!("Processing id = {:?}", id); id })
            .all(|id| id.unwrap().is_in_collection(&["coll_3"]));

        assert!(succeeded, "not all entries in iterator are from coll_3 collection");
    }

    #[test]
    fn test_entries_iterator_substr() {
        setup_logging();
        let store = get_store();

        let ids = {
            let base = String::from("entry");
            let variants = vec!["coll_1", "coll2", "coll_3"];
            let modifier = |base: &String, v: &&str| {
                StoreId::new(PathBuf::from(format!("{}/{}", *v, base))).unwrap()
            };

            generate_variants(&base, variants.iter(), &modifier)
        };

        for id in ids {
            let _ = store.retrieve(id).unwrap();
        }

        let succeeded = store.entries()
            .unwrap()
            .find_by_id_substr("_")
            .map(|id| { debug!("Processing id = {:?}", id); id })
            .all(|id| id.unwrap().local_display_string().contains('_'));

        assert!(succeeded, "not all entries in iterator contain '_'");
    }

    #[test]
    fn test_entries_iterator_startswith() {
        setup_logging();
        let store = get_store();

        let ids = {
            let base = String::from("entry");
            let variants = vec!["coll_1", "coll2", "coll_3"];
            let modifier = |base: &String, v: &&str| {
                StoreId::new(PathBuf::from(format!("{}/{}", *v, base))).unwrap()
            };

            generate_variants(&base, variants.iter(), &modifier)
        };

        for id in ids {
            let _ = store.retrieve(id).unwrap();
        }

        let succeeded = store.entries()
            .unwrap()
            .find_by_id_startswith("entr")
            .map(|id| { debug!("Processing id = {:?}", id); id })
            .all(|id| id.unwrap().local_display_string().starts_with("entry"));

        assert!(succeeded, "not all entries in iterator start with 'entr'");
    }

}