11use anyhow:: Result ;
2- use link_aggregator:: { ActionableEvent , RecordId } ;
2+ use link_aggregator:: { ActionableEvent , Did , RecordId } ;
33use std:: collections:: HashMap ;
44use std:: sync:: Mutex ;
55
@@ -12,12 +12,26 @@ pub trait LinkStorage {
1212#[ derive( Debug ) ]
1313pub struct MemStorage ( Mutex < MemStorageData > ) ;
1414
15+ #[ derive( Debug , PartialEq , Hash , Eq , Clone ) ]
16+ struct Source {
17+ collection : String ,
18+ path : String ,
19+ }
20+
21+ impl Source {
22+ fn new ( collection : & str , path : & str ) -> Self {
23+ Self {
24+ collection : collection. into ( ) ,
25+ path : path. into ( ) ,
26+ }
27+ }
28+ }
29+
1530#[ derive( Debug , Default ) ]
1631struct MemStorageData {
17- dids : HashMap < String , bool > , // bool: active or nah
18- targets : HashMap < String , HashMap < ( String , String ) , Vec < String > > > , // target -> (collection, path) -> did[]
19- #[ allow( clippy:: type_complexity) ]
20- links : HashMap < String , HashMap < String , Vec < ( String , String , String ) > > > , // did -> collection:rkey -> (target, collection, path)[]
32+ dids : HashMap < Did , bool > , // bool: active or nah
33+ targets : HashMap < String , HashMap < Source , Vec < Did > > > , // target -> (collection, path) -> did[]
34+ links : HashMap < Did , HashMap < String , Vec < ( String , Source ) > > > , // did -> collection:rkey -> (target, (collection, path))[]
2135}
2236
2337impl MemStorage {
@@ -56,20 +70,20 @@ impl LinkStorage for MemStorage {
5670 } => {
5771 for link in links {
5872 data. dids
59- . entry ( ( * did) . clone ( ) )
73+ . entry ( did. clone ( ) )
6074 . or_insert ( true ) ; // if they are inserting a link, presumably they are active
6175 data. targets
6276 . entry ( link. target . clone ( ) )
6377 . or_default ( )
64- . entry ( ( collection. clone ( ) , link. path . clone ( ) ) )
78+ . entry ( Source :: new ( collection, & link. path ) )
6579 . or_default ( )
66- . push ( ( * did) . clone ( ) ) ;
80+ . push ( did. clone ( ) ) ;
6781 data. links
68- . entry ( ( * did) . clone ( ) )
82+ . entry ( did. clone ( ) )
6983 . or_default ( )
7084 . entry ( Self :: _col_rkey ( collection, rkey) )
7185 . or_insert ( Vec :: with_capacity ( 1 ) )
72- . push ( ( link. target . clone ( ) , collection . clone ( ) , link. path . clone ( ) ) )
86+ . push ( ( link. target . clone ( ) , Source :: new ( collection , & link. path ) ) )
7387 }
7488 Ok ( ( ) )
7589 } ,
@@ -89,51 +103,52 @@ impl LinkStorage for MemStorage {
89103 rkey,
90104 } ) => {
91105 let col_rkey = Self :: _col_rkey ( collection, rkey) ;
92- if let Some ( Some ( link_targets) ) = data. links . get ( & * * did) . map ( |cr| cr. get ( & col_rkey) ) {
106+ if let Some ( Some ( link_targets) ) = data. links . get ( did) . map ( |cr| cr. get ( & col_rkey) ) {
93107 let link_targets = link_targets. clone ( ) ; // satisfy borrowck
94- for ( target, collection , path ) in link_targets {
108+ for ( target, source ) in link_targets {
95109 let dids = data. targets
96110 . get_mut ( & target)
97111 . expect ( "must have the target if we have a link saved" )
98- . get_mut ( & ( collection . clone ( ) , path . clone ( ) ) )
112+ . get_mut ( & source )
99113 . expect ( "must have the target at this path if we have a link to it saved" ) ;
100114 // search from the end: more likely to be visible and deletes are usually soon after creates
101115 // only delete one instance: a user can create multiple links to something, we're only deleting one
102116 // (we don't know which one in the list we should be deleting, and it hopefully mostly doesn't matter)
103- let pos = dids. iter ( ) . rposition ( |d| * d == * * did) . expect ( "must be in dids list if we have a link to it" ) ;
117+ let pos = dids. iter ( ) . rposition ( |d| d == did) . expect ( "must be in dids list if we have a link to it" ) ;
104118 dids. remove ( pos) ;
105119 }
106120 }
107- data. links . get_mut ( & * * did) . map ( |cr| cr. remove ( & col_rkey) ) ;
121+ data. links . get_mut ( did) . map ( |cr| cr. remove ( & col_rkey) ) ;
108122 Ok ( ( ) )
109123 } ,
110124 ActionableEvent :: ActivateAccount ( did) => {
111- if let Some ( account) = data. dids . get_mut ( did. as_ref ( ) ) { // only act if we have any records by this account
125+ if let Some ( account) = data. dids . get_mut ( did) { // only act if we have any records by this account
112126 * account = true ;
113127 }
114128 Ok ( ( ) )
115129 } ,
116130 ActionableEvent :: DeactivateAccount ( did) => {
117- if let Some ( account) = data. dids . get_mut ( did. as_ref ( ) ) { // ignore deactivating unknown accounts should be ok
131+ if let Some ( account) = data. dids . get_mut ( did) { // ignore deactivating unknown accounts should be ok
118132 * account = false ;
119133 }
120134 Ok ( ( ) )
121135 } ,
122136 ActionableEvent :: DeleteAccount ( did) => {
123- if let Some ( links) = data. links . get ( did. as_ref ( ) ) {
137+ if let Some ( links) = data. links . get ( did) {
124138 let links = links. clone ( ) ;
125139 for targets in links. values ( ) {
126- for ( target, collection, path) in targets {
127- data. targets . get_mut ( target)
140+ let targets = targets. clone ( ) ;
141+ for ( target, source) in targets {
142+ data. targets . get_mut ( & target)
128143 . expect ( "must have the target if we have a link saved" )
129- . get_mut ( & ( collection . clone ( ) , path . clone ( ) ) )
144+ . get_mut ( & source )
130145 . expect ( "must have the target at this path if we have a link to it saved" )
131- . retain ( |d| d != did. as_ref ( ) ) ;
146+ . retain ( |d| d != did) ;
132147 }
133148 }
134149 }
135- data. links . remove ( did. as_ref ( ) ) ;
136- data. dids . remove ( did. as_ref ( ) ) ;
150+ data. links . remove ( did) ;
151+ data. dids . remove ( did) ;
137152 Ok ( ( ) )
138153 } ,
139154 }
@@ -143,7 +158,7 @@ impl LinkStorage for MemStorage {
143158 let Some ( paths) = data. targets . get ( target) else {
144159 return Ok ( 0 ) ;
145160 } ;
146- let Some ( dids) = paths. get ( & ( collection. to_string ( ) , path. to_string ( ) ) ) else {
161+ let Some ( dids) = paths. get ( & Source :: new ( collection, path) ) else {
147162 return Ok ( 0 ) ;
148163 } ;
149164 let count = dids. len ( ) . try_into ( ) ?;
0 commit comments