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().flatten().cloned().collect())
33 }
34
35 fn update(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
36 let mut lock = self.accounts.write();
37 let accounts = lock.entry(account.address).or_default();
38 accounts.retain(|acc| acc.filename != account.filename);
40 accounts.push(account.clone());
41 Ok(account)
42 }
43
44 fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
45 let mut lock = self.accounts.write();
46 let accounts = lock.entry(account.address).or_default();
47 accounts.push(account.clone());
48 Ok(account)
49 }
50
51 fn remove(&self, account: &SafeAccount) -> Result<(), Error> {
52 let mut accounts = self.accounts.write();
53 let is_empty =
54 if let Some(accounts) = accounts.get_mut(&account.address) {
55 if let Some(position) =
56 accounts.iter().position(|acc| acc == account)
57 {
58 accounts.remove(position);
59 }
60 accounts.is_empty()
61 } else {
62 false
63 };
64 if is_empty {
65 accounts.remove(&account.address);
66 }
67 Ok(())
68 }
69
70 fn unique_repr(&self) -> Result<u64, Error> {
71 let mut val = 0u64;
72 let accounts = self.accounts.read();
73 for acc in accounts.keys() {
74 val ^= acc.to_low_u64_be()
75 }
76 Ok(val)
77 }
78}