trie_db/
fatdbmut.rs

1// Copyright 2017, 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16	triedbmut::{TrieDBMutBuilder, Value},
17	CError, DBValue, Result, TrieDBMut, TrieHash, TrieLayout, TrieMut,
18};
19use hash_db::{HashDB, Hasher, EMPTY_PREFIX};
20
21/// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
22/// Additionaly it stores inserted hash-key mappings for later retrieval.
23///
24/// Use it as a `Trie` or `TrieMut` trait object.
25pub struct FatDBMut<'db, L>
26where
27	L: TrieLayout,
28{
29	raw: TrieDBMut<'db, L>,
30}
31
32impl<'db, L> FatDBMut<'db, L>
33where
34	L: TrieLayout,
35{
36	/// Create a new trie with the backing database `db` and empty `root`
37	/// Initialise to the state entailed by the genesis block.
38	/// This guarantees the trie is built correctly.
39	pub fn new(db: &'db mut dyn HashDB<L::Hash, DBValue>, root: &'db mut TrieHash<L>) -> Self {
40		FatDBMut { raw: TrieDBMutBuilder::new(db, root).build() }
41	}
42
43	/// Create a new trie with the backing database `db` and `root`.
44	///
45	/// Returns an error if root does not exist.
46	pub fn from_existing(
47		db: &'db mut dyn HashDB<L::Hash, DBValue>,
48		root: &'db mut TrieHash<L>,
49	) -> Self {
50		FatDBMut { raw: TrieDBMutBuilder::from_existing(db, root).build() }
51	}
52
53	/// Get the backing database.
54	pub fn db(&self) -> &dyn HashDB<L::Hash, DBValue> {
55		self.raw.db()
56	}
57
58	/// Get the backing database.
59	pub fn db_mut(&mut self) -> &mut dyn HashDB<L::Hash, DBValue> {
60		self.raw.db_mut()
61	}
62}
63
64impl<'db, L> TrieMut<L> for FatDBMut<'db, L>
65where
66	L: TrieLayout,
67{
68	fn root(&mut self) -> &TrieHash<L> {
69		self.raw.root()
70	}
71
72	fn is_empty(&self) -> bool {
73		self.raw.is_empty()
74	}
75
76	fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>> {
77		self.raw.contains(L::Hash::hash(key).as_ref())
78	}
79
80	fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Result<Option<DBValue>, TrieHash<L>, CError<L>>
81	where
82		'a: 'key,
83	{
84		self.raw.get(L::Hash::hash(key).as_ref())
85	}
86
87	fn insert(
88		&mut self,
89		key: &[u8],
90		value: &[u8],
91	) -> Result<Option<Value<L>>, TrieHash<L>, CError<L>> {
92		let hash = L::Hash::hash(key);
93		let out = self.raw.insert(hash.as_ref(), value)?;
94		let db = self.raw.db_mut();
95
96		// insert if it doesn't exist.
97		if out.is_none() {
98			let aux_hash = L::Hash::hash(hash.as_ref());
99			db.emplace(aux_hash, EMPTY_PREFIX, key.to_vec());
100		}
101		Ok(out)
102	}
103
104	fn remove(&mut self, key: &[u8]) -> Result<Option<Value<L>>, TrieHash<L>, CError<L>> {
105		let hash = L::Hash::hash(key);
106		let out = self.raw.remove(hash.as_ref())?;
107
108		// remove if it already exists.
109		if out.is_some() {
110			let aux_hash = L::Hash::hash(hash.as_ref());
111			self.raw.db_mut().remove(&aux_hash, EMPTY_PREFIX);
112		}
113
114		Ok(out)
115	}
116}