tp_state_machine/overlayed_changes/
offchain.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Overlayed changes for offchain indexing.
19
20use tet_core::offchain::OffchainOverlayedChange;
21use tetcore_std::prelude::Vec;
22use super::changeset::OverlayedMap;
23
24/// In-memory storage for offchain workers recoding changes for the actual offchain storage implementation.
25#[derive(Debug, Clone, Default)]
26pub struct OffchainOverlayedChanges(OverlayedMap<(Vec<u8>, Vec<u8>), OffchainOverlayedChange>);
27
28/// Item for iterating over offchain changes.
29///
30/// First element i a tuple of `(prefix, key)`, second element ist the actual change
31/// (remove or set value).
32type OffchainOverlayedChangesItem<'i> = (&'i (Vec<u8>, Vec<u8>), &'i OffchainOverlayedChange);
33
34/// Iterator over offchain changes, owned memory version.
35type OffchainOverlayedChangesItemOwned = ((Vec<u8>, Vec<u8>), OffchainOverlayedChange);
36
37impl OffchainOverlayedChanges {
38	/// Consume the offchain storage and iterate over all key value pairs.
39	pub fn into_iter(self) -> impl Iterator<Item = OffchainOverlayedChangesItemOwned> {
40		self.0.into_changes().map(|kv| (kv.0, kv.1.into_value()))
41	}
42
43	/// Iterate over all key value pairs by reference.
44	pub fn iter<'a>(&'a self) -> impl Iterator<Item = OffchainOverlayedChangesItem<'a>> {
45		self.0.changes().map(|kv| (kv.0, kv.1.value_ref()))
46	}
47
48	/// Drain all elements of changeset.
49	pub fn drain(&mut self) -> impl Iterator<Item = OffchainOverlayedChangesItemOwned> {
50		tetcore_std::mem::take(self).into_iter()
51	}
52
53	/// Remove a key and its associated value from the offchain database.
54	pub fn remove(&mut self, prefix: &[u8], key: &[u8]) {
55		let _ = self.0.set(
56			(prefix.to_vec(), key.to_vec()),
57			OffchainOverlayedChange::Remove,
58			None,
59		);
60	}
61
62	/// Set the value associated with a key under a prefix to the value provided.
63	pub fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]) {
64		let _ = self.0.set(
65			(prefix.to_vec(), key.to_vec()),
66			OffchainOverlayedChange::SetValue(value.to_vec()),
67			None,
68		);
69	}
70
71	/// Obtain a associated value to the given key in storage with prefix.
72	pub fn get(&self, prefix: &[u8], key: &[u8]) -> Option<OffchainOverlayedChange> {
73		let key = (prefix.to_vec(), key.to_vec());
74		self.0.get(&key).map(|entry| entry.value_ref()).cloned()
75	}
76
77	/// Reference to inner change set.
78	pub fn overlay(&self) -> &OverlayedMap<(Vec<u8>, Vec<u8>), OffchainOverlayedChange> {
79		&self.0
80	}
81
82	/// Mutable reference to inner change set.
83	pub fn overlay_mut(&mut self) -> &mut OverlayedMap<(Vec<u8>, Vec<u8>), OffchainOverlayedChange> {
84		&mut self.0
85	}
86}
87
88#[cfg(test)]
89mod test {
90	use super::*;
91	use tet_core::offchain::STORAGE_PREFIX;
92
93	#[test]
94	fn test_drain() {
95		let mut ooc = OffchainOverlayedChanges::default();
96		ooc.set(STORAGE_PREFIX, b"kkk", b"vvv");
97		let drained = ooc.drain().count();
98		assert_eq!(drained, 1);
99		let leftover = ooc.iter().count();
100		assert_eq!(leftover, 0);
101
102		ooc.set(STORAGE_PREFIX, b"a", b"v");
103		ooc.set(STORAGE_PREFIX, b"b", b"v");
104		ooc.set(STORAGE_PREFIX, b"c", b"v");
105		ooc.set(STORAGE_PREFIX, b"d", b"v");
106		ooc.set(STORAGE_PREFIX, b"e", b"v");
107		assert_eq!(ooc.iter().count(), 5);
108	}
109
110	#[test]
111	fn test_accumulated_set_remove_set() {
112		let mut ooc = OffchainOverlayedChanges::default();
113		ooc.set(STORAGE_PREFIX, b"ppp", b"qqq");
114		ooc.remove(STORAGE_PREFIX, b"ppp");
115		// keys are equiv, so it will overwrite the value and the overlay will contain
116		// one item
117		assert_eq!(ooc.iter().count(), 1);
118
119		ooc.set(STORAGE_PREFIX, b"ppp", b"rrr");
120		let mut iter = ooc.into_iter();
121		assert_eq!(
122			iter.next(),
123			Some(
124				((STORAGE_PREFIX.to_vec(), b"ppp".to_vec()),
125				OffchainOverlayedChange::SetValue(b"rrr".to_vec()))
126			)
127		);
128		assert_eq!(iter.next(), None);
129	}
130}