referrerpolicy=no-referrer-when-downgrade
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
use codec::FullCodec;
use core::{cmp::Ord, marker::PhantomData, ops::Add};
use frame_support::storage::{types::QueryKindTrait, StorageMap, StorageValue};
use sp_core::{Get, GetDefault};
use sp_runtime::traits::{One, Zero};

/// Trait object presenting the ringbuffer interface.
pub trait RingBufferMap<Key, Value, QueryKind>
where
	Key: FullCodec,
	Value: FullCodec,
	QueryKind: QueryKindTrait<Value, GetDefault>,
{
	/// Insert a map entry.
	fn insert(k: Key, v: Value);

	/// Check if map contains a key
	fn contains_key(k: Key) -> bool;

	/// Get the value of the key
	fn get(k: Key) -> QueryKind::Query;
}

pub struct RingBufferMapImpl<Index, B, CurrentIndex, Intermediate, M, QueryKind>(
	PhantomData<(Index, B, CurrentIndex, Intermediate, M, QueryKind)>,
);

/// Ringbuffer implementation based on `RingBufferTransient`
impl<Key, Value, Index, B, CurrentIndex, Intermediate, M, QueryKind>
	RingBufferMap<Key, Value, QueryKind>
	for RingBufferMapImpl<Index, B, CurrentIndex, Intermediate, M, QueryKind>
where
	Key: FullCodec + Clone,
	Value: FullCodec,
	Index: Ord + One + Zero + Add<Output = Index> + Copy + FullCodec + Eq,
	B: Get<Index>,
	CurrentIndex: StorageValue<Index, Query = Index>,
	Intermediate: StorageMap<Index, Key, Query = Key>,
	M: StorageMap<Key, Value, Query = QueryKind::Query>,
	QueryKind: QueryKindTrait<Value, GetDefault>,
{
	/// Insert a map entry.
	fn insert(k: Key, v: Value) {
		let bound = B::get();
		let mut current_index = CurrentIndex::get();

		// Adding one here as bound denotes number of items but our index starts with zero.
		if (current_index + Index::one()) >= bound {
			current_index = Index::zero();
		} else {
			current_index = current_index + Index::one();
		}

		// Deleting earlier entry if it exists
		if Intermediate::contains_key(current_index) {
			let older_key = Intermediate::get(current_index);
			M::remove(older_key);
		}

		Intermediate::insert(current_index, k.clone());
		CurrentIndex::set(current_index);
		M::insert(k, v);
	}

	/// Check if map contains a key
	fn contains_key(k: Key) -> bool {
		M::contains_key(k)
	}

	/// Get the value associated with key
	fn get(k: Key) -> M::Query {
		M::get(k)
	}
}