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
use cosmwasm_std::{DepsMut, Order, StdError, StdResult, Storage};
use cw_storage_plus::{Bound, Item, Map, Path};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{error::AbstractError, AbstractResult};

const DEFAULT_LIMIT: u32 = 10;
const MAX_LIMIT: u32 = 30;
const MAX_MSG_LIMIT: u32 = 15;
const PAGED_MAP: &str = "paged_map";

pub type PaginationResult<Acum, PageResult> = AbstractResult<(Option<Acum>, PageResult)>;
pub type PaginationAccumulatorFunction<T, Acum, C, FuncResult> =
    fn(&[u8], &mut dyn Storage, T, &mut Acum, &C) -> AbstractResult<Option<FuncResult>>;
pub type PaginationFunction<T, C, FuncResult> =
    fn(&[u8], &mut dyn Storage, T, &C) -> AbstractResult<Option<FuncResult>>;
/// Allows for multi-transaction computation on a dataset. Required for large datasets due to gas constraints.
pub struct PagedMap<'a, T, Acum> {
    /// Actual data store
    data: Map<'a, &'a [u8], T>,
    /// Pagination progress status
    pub status: Item<'a, PaginationInfo<Acum>>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PaginationInfo<Acum> {
    /// Prevents map manipulation during pagination
    pub is_locked: bool,
    /// Starting item for next iteration
    pub last_processed_item: Option<Vec<u8>>,
    /// Accumulator item available for use in pagination function
    pub accumulator: Option<Acum>,
}

impl<'a, T, Acum> PagedMap<'a, T, Acum> {
    pub const fn new(namespace: &'a str, status_namespace: &'a str) -> Self {
        PagedMap {
            data: Map::new(namespace),
            status: Item::new(status_namespace),
        }
    }

    pub fn instantiate(&self, store: &mut dyn Storage) -> Result<(), StdError>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        self.status.save(
            store,
            &PaginationInfo {
                is_locked: false,
                accumulator: None,
                last_processed_item: None,
            },
        )
    }

    pub fn save(&self, store: &mut dyn Storage, key: &[u8], data: &T) -> AbstractResult<()>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        if self.status.load(store)?.is_locked {
            return Err(AbstractError::Storage {
                object: PAGED_MAP.into(),
                msg: "Can not save to map while locked. Proceed with operation first.".into(),
            });
        }
        self.data.save(store, key, data).map_err(Into::into)
    }

    /// **Warning**: This function circumvents the storage lock. You should only use this in a pagination function.
    pub fn unsafe_save(&self, store: &mut dyn Storage, key: &[u8], data: &T) -> AbstractResult<()>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        self.data.save(store, key, data).map_err(Into::into)
    }

    // Returns the removed item after deleting it
    pub fn remove(&self, store: &mut dyn Storage, key: &[u8]) -> AbstractResult<T>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        if self.status.load(store)?.is_locked {
            return Err(AbstractError::Storage {
                object: PAGED_MAP.into(),
                msg: "Can not save to map while locked. Proceed with operation first.".into(),
            });
        }
        let old_item = self.data.load(store, key)?;
        self.data.remove(store, key);

        Ok(old_item)
    }

    /// **Warning**: This function circumvents the storage lock. You should only use this in a pagination function.
    /// Returns the removed item after deleting it
    pub fn unsafe_remove(&self, store: &mut dyn Storage, key: &[u8]) -> AbstractResult<T>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        let old_item = self.data.load(store, key)?;
        self.data.remove(store, key);

        Ok(old_item)
    }

    pub fn load(&self, store: &dyn Storage, key: &[u8]) -> AbstractResult<T>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        self.data.load(store, key).map_err(Into::into)
    }

    pub fn has(&self, store: &dyn Storage, key: &[u8]) -> bool
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        self.data.has(store, key)
    }

    pub fn may_load(&self, store: &dyn Storage, key: &[u8]) -> AbstractResult<Option<T>>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        self.data.may_load(store, key).map_err(Into::into)
    }

    pub fn load_status(&self, store: &dyn Storage) -> AbstractResult<PaginationInfo<Acum>>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        self.status.load(store).map_err(Into::into)
    }

    pub fn key(&self, key: &[u8]) -> Path<T>
    where
        T: Serialize + DeserializeOwned,
    {
        self.data.key(key)
    }

    /// Perform some operation on a page of the map.
    /// Returns an optional result of that computation.
    /// Repeat until state unlocks to page over the whole map
    /// Omits errors from f()
    pub fn page_with_accumulator<C, FuncResult>(
        &self,
        deps: DepsMut,
        limit: Option<u32>,
        context: &C,
        f: PaginationAccumulatorFunction<T, Acum, C, FuncResult>,
    ) -> PaginationResult<Acum, Vec<FuncResult>>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
        let mut status = self.status.load(deps.storage)?;
        if !status.is_locked {
            status.is_locked = true;
            status.accumulator = Some(Acum::default());
            status.last_processed_item = None;
        }

        let start = status.last_processed_item.clone().map(Bound::ExclusiveRaw);

        let result: Vec<(Vec<u8>, T)> = self
            .data
            .range(deps.storage, start, None, Order::Ascending)
            .take(limit)
            .collect::<StdResult<Vec<(Vec<u8>, T)>>>()?;

        // If not all items processed, update last item
        let return_accumulator = if !result.is_empty() {
            let last_key = result.last().unwrap().0.clone();
            status.last_processed_item = Some(last_key);
            None
        } else {
            // Everything processed, set to None and return accumulator
            let accumulator: Option<Acum> = status.accumulator.clone();
            status.is_locked = false;
            status.accumulator = None;
            accumulator
        };

        let function_results = result
            .into_iter()
            .filter_map(|(key, element)| {
                f(
                    &key,
                    deps.storage,
                    element,
                    status
                        .accumulator
                        .as_mut()
                        .expect("accumulator contains some value"),
                    context,
                )
                .ok()
                .unwrap_or(None)
            })
            .collect::<Vec<FuncResult>>();

        self.status.save(deps.storage, &status)?;

        Ok((return_accumulator, function_results))
    }

    /// Will apply function on each element (key, value) of the map. Errors on function f() are neglected.
    /// Will not lock the set as no accumulator is used so map state changes are allowed.
    pub fn page_without_accumulator<C, FuncResult>(
        &self,
        deps: DepsMut,
        limit: Option<u32>,
        context: &C,
        f: PaginationFunction<T, C, FuncResult>,
    ) -> AbstractResult<Vec<FuncResult>>
    where
        T: Serialize + DeserializeOwned,
        Acum: Serialize + DeserializeOwned + Default + Clone,
    {
        let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_MSG_LIMIT) as usize;
        let mut status = self.status.load(deps.storage)?;

        let start = status.last_processed_item.clone().map(Bound::ExclusiveRaw);
        let result: Vec<(Vec<u8>, T)> = self
            .data
            .range(deps.storage, start, None, Order::Ascending)
            .take(limit)
            .collect::<StdResult<Vec<(Vec<u8>, T)>>>()?;

        // If not all items processed, update last item
        if !result.is_empty() {
            let last_key = result.last().unwrap().0.clone();
            status.last_processed_item = Some(last_key);
        } else {
            // Everything processed, unlock map
            status.last_processed_item = None;
        };

        let function_results = result
            .into_iter()
            .filter_map(|(key, element)| {
                f(&key, deps.storage, element, context).ok().unwrap_or(None)
            })
            .collect::<Vec<FuncResult>>();

        self.status.save(deps.storage, &status)?;

        Ok(function_results)
    }
}

#[cfg(test)]
mod tests {
    use cosmwasm_std::testing::{mock_dependencies, MockStorage};
    use serde::{Deserialize, Serialize};

    use super::*;

    #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
    struct Data {
        pub name: String,
        pub balance: u32,
    }

    #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Default)]
    struct IncomeAcc {
        pub total: u32,
    }

    const USERS: PagedMap<Data, IncomeAcc> = PagedMap::new("people", "status");

    #[test]
    fn save_and_load() {
        let mut store = MockStorage::new();

        // save and load on one key
        let john = USERS.key(b"john");
        let data = Data {
            name: "John".to_string(),
            balance: 32,
        };
        assert_eq!(None, john.may_load(&store).unwrap());
        john.save(&mut store, &data).unwrap();
        assert_eq!(data, john.load(&store).unwrap());

        // nothing on another key
        assert_eq!(None, USERS.may_load(&store, b"jack").unwrap());

        // same named path gets the data
        assert_eq!(data, USERS.load(&store, b"john").unwrap());

        // removing leaves us empty
        john.remove(&mut store);
        assert_eq!(None, john.may_load(&store).unwrap());
    }

    #[test]
    fn page_with_accumulator() {
        // Change balance to 0, add balance to total and return value if even
        fn accumulate_and_subtract_balances(
            key: &[u8],
            store: &mut dyn Storage,
            mut value: Data,
            acc: &mut IncomeAcc,
            #[allow(clippy::ptr_arg)] _context: &String,
        ) -> AbstractResult<Option<u32>> {
            let balance = value.balance;
            value.balance = 0;
            acc.total += balance;
            USERS.unsafe_save(store, key, &value)?;

            if balance % 2 == 0 {
                Ok(Some(balance))
            } else {
                Ok(None)
            }
        }

        let mut deps = mock_dependencies();
        USERS.instantiate(&mut deps.storage).unwrap();
        let mut total = 0;
        let mut even_numbers = vec![];

        for i in 0..100 {
            let data = Data {
                name: "IrrelevantName".to_string(),
                balance: i,
            };
            total += data.balance;
            USERS
                .save(&mut deps.storage, &i.to_be_bytes(), &data)
                .unwrap();
            let stored_data = USERS.load(&deps.storage, &i.to_be_bytes()).unwrap();
            if i % 2 == 0 {
                even_numbers.push(i);
            };
            assert_eq!(stored_data, data);
        }
        let mut result_even_numbers = vec![];

        // first call, external factor (like a time stamp) should determine when you can start the accumulator.
        let (_, mut maybe_even_numbers) = USERS
            .page_with_accumulator(
                deps.as_mut(),
                None,
                &String::new(),
                accumulate_and_subtract_balances,
            )
            .unwrap();

        assert!(USERS
            .status
            .load(&deps.storage)
            .unwrap()
            .accumulator
            .is_some());
        assert!(USERS.status.load(&deps.storage).unwrap().is_locked);
        // Keep track of the output
        result_even_numbers.append(&mut maybe_even_numbers);

        while USERS.status.load(&deps.storage).unwrap().is_locked {
            let (maybe_accumulator, mut maybe_even_numbers) = USERS
                .page_with_accumulator(
                    deps.as_mut(),
                    None,
                    &String::new(),
                    accumulate_and_subtract_balances,
                )
                .unwrap();

            result_even_numbers.append(&mut maybe_even_numbers);

            if let Some(acc) = maybe_accumulator {
                // Accumulator should be done
                assert_eq!(acc.total, total);
                assert_eq!(result_even_numbers, even_numbers);
            }
        }
        for i in 0..100u32 {
            let stored_data = USERS.load(&deps.storage, &i.to_be_bytes()).unwrap();
            assert_eq!(
                stored_data,
                Data {
                    name: "IrrelevantName".to_string(),
                    balance: 0,
                }
            );
        }
    }

    #[test]
    fn page_without_accumulator() {
        // Change balance to 0, add balance to total and return value if even
        fn subtract_balances(
            key: &[u8],
            store: &mut dyn Storage,
            mut value: Data,
            #[allow(clippy::ptr_arg)] _context: &String,
        ) -> AbstractResult<Option<u32>> {
            let balance = value.balance;
            value.balance = 0;
            USERS.unsafe_save(store, key, &value)?;

            if balance % 2 == 0 {
                Ok(Some(balance))
            } else {
                Ok(None)
            }
        }

        let mut deps = mock_dependencies();
        USERS.instantiate(&mut deps.storage).unwrap();
        let mut even_numbers = vec![];

        for i in 0..100 {
            let data = Data {
                name: "IrrelevantName".to_string(),
                balance: i,
            };
            USERS
                .save(&mut deps.storage, &i.to_be_bytes(), &data)
                .unwrap();
            let stored_data = USERS.load(&deps.storage, &i.to_be_bytes()).unwrap();
            if i % 2 == 0 {
                even_numbers.push(i);
            };
            assert_eq!(stored_data, data);
        }
        let mut result_even_numbers = vec![];

        // first call, external factor (like a time stamp) should determine when you can start the accumulator.
        let mut maybe_even_numbers = USERS
            .page_without_accumulator(deps.as_mut(), None, &String::new(), subtract_balances)
            .unwrap();

        assert!(!USERS.status.load(&deps.storage).unwrap().is_locked);
        // Keep track of the output
        result_even_numbers.append(&mut maybe_even_numbers);

        while USERS
            .status
            .load(&deps.storage)
            .unwrap()
            .last_processed_item
            .is_some()
        {
            let mut maybe_even_numbers = USERS
                .page_without_accumulator(deps.as_mut(), None, &String::new(), subtract_balances)
                .unwrap();

            result_even_numbers.append(&mut maybe_even_numbers);
        }

        assert_eq!(result_even_numbers, even_numbers);

        for i in 0..100u32 {
            let stored_data = USERS.load(&deps.storage, &i.to_be_bytes()).unwrap();
            assert_eq!(
                stored_data,
                Data {
                    name: "IrrelevantName".to_string(),
                    balance: 0,
                }
            );
        }
    }
}