cfxstore/accounts_dir/
memory.rs1use cfxkey::Address;
18use parking_lot::RwLock;
19use std::collections::HashMap;
20
21use super::KeyDirectory;
22use crate::{Error, SafeAccount};
23
24#[derive(Default)]
26pub struct MemoryDirectory {
27 accounts: RwLock<HashMap<Address, Vec<SafeAccount>>>,
28}
29
30impl KeyDirectory for MemoryDirectory {
31 fn load(&self) -> Result<Vec<SafeAccount>, Error> {
32 Ok(self.accounts.read().values().cloned().flatten().collect())
33 }
34
35 fn update(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
36 let mut lock = self.accounts.write();
37 let accounts =
38 lock.entry(account.address.clone()).or_insert_with(Vec::new);
39 accounts.retain(|acc| acc.filename != account.filename);
41 accounts.push(account.clone());
42 Ok(account)
43 }
44
45 fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
46 let mut lock = self.accounts.write();
47 let accounts =
48 lock.entry(account.address.clone()).or_insert_with(Vec::new);
49 accounts.push(account.clone());
50 Ok(account)
51 }
52
53 fn remove(&self, account: &SafeAccount) -> Result<(), Error> {
54 let mut accounts = self.accounts.write();
55 let is_empty =
56 if let Some(accounts) = accounts.get_mut(&account.address) {
57 if let Some(position) =
58 accounts.iter().position(|acc| acc == account)
59 {
60 accounts.remove(position);
61 }
62 accounts.is_empty()
63 } else {
64 false
65 };
66 if is_empty {
67 accounts.remove(&account.address);
68 }
69 Ok(())
70 }
71
72 fn unique_repr(&self) -> Result<u64, Error> {
73 let mut val = 0u64;
74 let accounts = self.accounts.read();
75 for acc in accounts.keys() {
76 val ^= acc.to_low_u64_be()
77 }
78 Ok(val)
79 }
80}