pub fn new<D: MetaDb>(db: &D) -> Result<NonCanonicalOverlay<BlockHash, Key>, Error<D::Error>> {
		let last_canonicalized = db.get_meta(&to_meta_key(LAST_CANONICAL, &()))
			.map_err(|e| Error::Db(e))?;
		let last_canonicalized = match last_canonicalized {
			Some(buffer) => Some(<(BlockHash, u64)>::decode(&mut buffer.as_slice()).ok_or(Error::Decoding)?),
			None => None,
		};
		let mut levels = VecDeque::new();
		let mut parents = HashMap::new();
		let mut values = HashMap::new();
		if let Some((ref hash, mut block)) = last_canonicalized {
			// read the journal
			trace!(target: "state-db", "Reading uncanonicalized journal. Last canonicalized #{} ({:?})", block, hash);
			let mut total: u64 = 0;
			block += 1;
			loop {
				let mut index: u64 = 0;
				let mut level = Vec::new();
				loop {
					let journal_key = to_journal_key(block, index);
					match db.get_meta(&journal_key).map_err(|e| Error::Db(e))? {
						Some(record) => {
							let record: JournalRecord<BlockHash, Key> = Decode::decode(&mut record.as_slice()).ok_or(Error::Decoding)?;
							let inserted = record.inserted.iter().map(|(k, _)| k.clone()).collect();
							let overlay = BlockOverlay {
								hash: record.hash.clone(),
								journal_key,
								inserted: inserted,
								deleted: record.deleted,
							};
							insert_values(&mut values, record.inserted);
							trace!(target: "state-db", "Uncanonicalized journal entry {}.{} ({} inserted, {} deleted)", block, index, overlay.inserted.len(), overlay.deleted.len());
							level.push(overlay);
							parents.insert(record.hash, record.parent_hash);
							index += 1;
							total += 1;
						},
						None => break,
					}
				}
				if level.is_empty() {
					break;
				}
				levels.push_back(level);
				block += 1;
			}
			trace!(target: "state-db", "Finished reading uncanonicalized journal, {} entries", total);
		}
		Ok(NonCanonicalOverlay {
			last_canonicalized,
			levels,
			parents,
			pending_canonicalizations: Default::default(),
			pending_insertions: Default::default(),
			values: values,
		})
	}
/// Creates a new instance. Does not expect any metadata to be present in the DB.
Creates a new instance. Does not expect any metadata to be present in the DB.
Creates a new instance . Does not expect any metadata to be present in the DB .
pub fn new<D: MetaDb>(db: &D) -> Result<NonCanonicalOverlay<BlockHash, Key>, Error<D::Error>> {
		let last_canonicalized = db.get_meta(&to_meta_key(LAST_CANONICAL, &()))
			.map_err(|e| Error::Db(e))?;
		let last_canonicalized = match last_canonicalized {
			Some(buffer) => Some(<(BlockHash, u64)>::decode(&mut buffer.as_slice()).ok_or(Error::Decoding)?),
			None => None,
		};
		let mut levels = VecDeque::new();
		let mut parents = HashMap::new();
		let mut values = HashMap::new();
		if let Some((ref hash, mut block)) = last_canonicalized {
			// read the journal
			trace!(target: "state-db", "Reading uncanonicalized journal. Last canonicalized #{} ({:?})", block, hash);
			let mut total: u64 = 0;
			block += 1;
			loop {
				let mut index: u64 = 0;
				let mut level = Vec::new();
				loop {
					let journal_key = to_journal_key(block, index);
					match db.get_meta(&journal_key).map_err(|e| Error::Db(e))? {
						Some(record) => {
							let record: JournalRecord<BlockHash, Key> = Decode::decode(&mut record.as_slice()).ok_or(Error::Decoding)?;
							let inserted = record.inserted.iter().map(|(k, _)| k.clone()).collect();
							let overlay = BlockOverlay {
								hash: record.hash.clone(),
								journal_key,
								inserted: inserted,
								deleted: record.deleted,
							};
							insert_values(&mut values, record.inserted);
							trace!(target: "state-db", "Uncanonicalized journal entry {}.{} ({} inserted, {} deleted)", block, index, overlay.inserted.len(), overlay.deleted.len());
							level.push(overlay);
							parents.insert(record.hash, record.parent_hash);
							index += 1;
							total += 1;
						},
						None => break,
					}
				}
				if level.is_empty() {
					break;
				}
				levels.push_back(level);
				block += 1;
			}
			trace!(target: "state-db", "Finished reading uncanonicalized journal, {} entries", total);
		}
		Ok(NonCanonicalOverlay {
			last_canonicalized,
			levels,
			parents,
			pending_canonicalizations: Default::default(),
			pending_insertions: Default::default(),
			values: values,
		})
	}
pub fn new < D : MetaDb > ( db : & D ) -> Result < NonCanonicalOverlay < BlockHash , Key > , Error < D :: Error > > { let last_canonicalized = db . get_meta ( & to_meta_key ( LAST_CANONICAL , & ( ) ) ) . map_err ( | e | Error :: Db ( e ) ) ? ; let last_canonicalized = match last_canonicalized { Some ( buffer ) => Some ( < ( BlockHash , u64 ) > :: decode ( & mut buffer . as_slice ( ) ) . ok_or ( Error :: Decoding ) ? ) , None => None , } ; let mut levels = VecDeque :: new ( ) ; let mut parents = HashMap :: new ( ) ; let mut values = HashMap :: new ( ) ; if let Some ( ( ref hash , mut block ) ) = last_canonicalized { // read the journal trace ! ( target " " " " block hash ) ; let mut total : u64 = 0 ; block += 1 ; loop { let mut index : u64 = 0 ; let mut level = Vec :: new ( ) ; loop { let journal_key = to_journal_key ( block , index ) ; match db . get_meta ( & journal_key ) . map_err ( | e | Error :: Db ( e ) ) ? { Some ( record ) => { let record : JournalRecord < BlockHash , Key > = Decode :: decode ( & mut record . as_slice ( ) ) . ok_or ( Error :: Decoding ) ? ; let inserted = record . inserted . iter ( ) . map ( | ( k , _ ) | k . clone ( ) ) . collect ( ) ; let overlay = BlockOverlay { hash : record . hash . clone ( ) , journal_key , inserted : inserted , deleted : record . deleted , } ; insert_values ( & mut values , record . inserted ) ; trace ! ( target " " " " block index overlay inserted len ( ) overlay deleted len ( ) ) ; level . push ( overlay ) ; parents . insert ( record . hash , record . parent_hash ) ; index += 1 ; total += 1 ; } , None => break , } } if level . is_empty ( ) { break ; } levels . push_back ( level ) ; block += 1 ; } trace ! ( target " " " " total ) ; } Ok ( NonCanonicalOverlay { last_canonicalized , levels , parents , pending_canonicalizations : Default :: default ( ) , pending_insertions : Default :: default ( ) , values : values , } ) }
Creates a new instance.
Creates a new instance .
db
&D
db
&D