tetsy_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 tetsy_hash_db::{HashDB, Hasher, EMPTY_PREFIX};
16use super::{Result, DBValue, TrieDBMut, TrieMut, TrieLayout, TrieHash, CError};
17
18/// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
19/// Additionaly it stores inserted hash-key mappings for later retrieval.
20///
21/// Use it as a `Trie` or `TrieMut` trait object.
22pub struct FatDBMut<'db, L>
23where
24	L: TrieLayout,
25{
26	raw: TrieDBMut<'db, L>,
27}
28
29impl<'db, L> FatDBMut<'db, L>
30where
31	L: TrieLayout,
32{
33	/// Create a new trie with the backing database `db` and empty `root`
34	/// Initialise to the state entailed by the genesis block.
35	/// This guarantees the trie is built correctly.
36	pub fn new(db: &'db mut dyn HashDB<L::Hash, DBValue>, root: &'db mut TrieHash<L>) -> Self {
37		FatDBMut { raw: TrieDBMut::new(db, root) }
38	}
39
40	/// Create a new trie with the backing database `db` and `root`.
41	///
42	/// Returns an error if root does not exist.
43	pub fn from_existing(
44		db: &'db mut dyn HashDB<L::Hash, DBValue>,
45		root: &'db mut TrieHash<L>
46	) -> Result<Self, TrieHash<L>, CError<L>> {
47		Ok(FatDBMut { raw: TrieDBMut::from_existing(db, root)? })
48	}
49
50	/// Get the backing database.
51	pub fn db(&self) -> &dyn HashDB<L::Hash, DBValue> {
52		self.raw.db()
53	}
54
55	/// Get the backing database.
56	pub fn db_mut(&mut self) -> &mut dyn HashDB<L::Hash, DBValue> {
57		self.raw.db_mut()
58	}
59}
60
61impl<'db, L> TrieMut<L> for FatDBMut<'db, L>
62where
63	L: TrieLayout,
64{
65	fn root(&mut self) -> &TrieHash<L> { self.raw.root() }
66
67	fn is_empty(&self) -> bool { self.raw.is_empty() }
68
69	fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>> {
70		self.raw.contains(L::Hash::hash(key).as_ref())
71	}
72
73	fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Result<Option<DBValue>, TrieHash<L>, CError<L>>
74		where 'a: 'key
75	{
76		self.raw.get(L::Hash::hash(key).as_ref())
77	}
78
79	fn insert(
80		&mut self,
81		key: &[u8],
82		value: &[u8],
83	) -> Result<Option<DBValue>, TrieHash<L>, CError<L>> {
84		let hash = L::Hash::hash(key);
85		let out = self.raw.insert(hash.as_ref(), value)?;
86		let db = self.raw.db_mut();
87
88		// insert if it doesn't exist.
89		if out.is_none() {
90			let aux_hash = L::Hash::hash(hash.as_ref());
91			db.emplace(aux_hash, EMPTY_PREFIX, key.to_vec());
92		}
93		Ok(out)
94	}
95
96	fn remove(&mut self, key: &[u8]) -> Result<Option<DBValue>, TrieHash<L>, CError<L>> {
97		let hash = L::Hash::hash(key);
98		let out = self.raw.remove(hash.as_ref())?;
99
100		// remove if it already exists.
101		if out.is_some() {
102			let aux_hash = L::Hash::hash(hash.as_ref());
103			self.raw.db_mut().remove(&aux_hash, EMPTY_PREFIX);
104		}
105
106		Ok(out)
107	}
108}