tet_core/offchain/
storage.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2019-2021 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//! In-memory implementation of offchain workers database.
19
20use std::collections::hash_map::{HashMap, Entry};
21use crate::offchain::OffchainStorage;
22use std::iter::Iterator;
23
24/// In-memory storage for offchain workers.
25#[derive(Debug, Clone, Default)]
26pub struct InMemOffchainStorage {
27	storage: HashMap<Vec<u8>, Vec<u8>>,
28}
29
30impl InMemOffchainStorage {
31	/// Consume the offchain storage and iterate over all key value pairs.
32	pub fn into_iter(self) -> impl Iterator<Item=(Vec<u8>,Vec<u8>)> {
33		self.storage.into_iter()
34	}
35
36	/// Iterate over all key value pairs by reference.
37	pub fn iter<'a>(&'a self) -> impl Iterator<Item=(&'a Vec<u8>,&'a Vec<u8>)> {
38		self.storage.iter()
39	}
40
41	/// Remove a key and its associated value from the offchain database.
42	pub fn remove(&mut self, prefix: &[u8], key: &[u8]) {
43		let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
44		self.storage.remove(&key);
45	}
46}
47
48impl OffchainStorage for InMemOffchainStorage {
49	fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]) {
50		let key = prefix.iter().chain(key).cloned().collect();
51		self.storage.insert(key, value.to_vec());
52	}
53
54	fn remove(&mut self, prefix: &[u8], key: &[u8]) {
55		let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
56		self.storage.remove(&key);
57	}
58
59	fn get(&self, prefix: &[u8], key: &[u8]) -> Option<Vec<u8>> {
60		let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
61		self.storage.get(&key).cloned()
62	}
63
64	fn compare_and_set(
65		&mut self,
66		prefix: &[u8],
67		key: &[u8],
68		old_value: Option<&[u8]>,
69		new_value: &[u8],
70	) -> bool {
71		let key = prefix.iter().chain(key).cloned().collect();
72
73		match self.storage.entry(key) {
74			Entry::Vacant(entry) => if old_value.is_none() {
75				entry.insert(new_value.to_vec());
76				true
77			} else { false },
78			Entry::Occupied(ref mut entry) if Some(entry.get().as_slice()) == old_value => {
79				entry.insert(new_value.to_vec());
80				true
81			},
82			_ => false,
83		}
84	}
85}