@@ -102,35 +102,29 @@ impl StoreTrait for VectorStore {
102102 }
103103
104104 fn favorites ( & self ) -> anyhow:: Result < usize > {
105- let mut favorites = 0 ;
106- for result in self . items . iter ( ) {
107- if let Ok ( result) = result. lock ( ) {
108- if result. is_fav ( ) {
109- favorites += 1 ;
110- }
111- }
112- }
105+ let favorites = self
106+ . items
107+ . iter ( )
108+ . filter_map ( |item| item. lock ( ) . ok ( ) )
109+ . filter ( |item| item. is_fav ( ) )
110+ . count ( ) ;
113111 Ok ( favorites)
114112 }
115113
116114 fn mark_favorite ( & self , id : & Key ) {
117- if let Some ( h_item) = self . items_index . get ( id) {
118- if let Ok ( mut h_item) = h_item. lock ( ) {
119- h_item. flip_fav ( ) ;
120- if let Ok ( bytes) = bincode:: encode_to_vec ( & * h_item, self . config ) {
121- let _ = self . database . insert ( h_item. id ( ) , bytes) ;
122- }
115+ if let Some ( mut h_item) = self . items_index . get ( id) . and_then ( |item| item. lock ( ) . ok ( ) ) {
116+ h_item. flip_fav ( ) ;
117+ if let Ok ( bytes) = bincode:: encode_to_vec ( & * h_item, self . config ) {
118+ let _ = self . database . insert ( h_item. id ( ) , bytes) ;
123119 }
124120 }
125121 }
126122
127123 fn mark_hit ( & self , id : & Key ) {
128- if let Some ( h_item) = self . items_index . get ( id) {
129- if let Ok ( mut h_item) = h_item. lock ( ) {
130- h_item. inc_hits ( ) ;
131- if let Ok ( bytes) = bincode:: encode_to_vec ( & * h_item, self . config ) {
132- let _ = self . database . insert ( h_item. id ( ) , bytes) ;
133- }
124+ if let Some ( mut h_item) = self . items_index . get ( id) . and_then ( |item| item. lock ( ) . ok ( ) ) {
125+ h_item. inc_hits ( ) ;
126+ if let Ok ( bytes) = bincode:: encode_to_vec ( & * h_item, self . config ) {
127+ let _ = self . database . insert ( h_item. id ( ) , bytes) ;
134128 }
135129 }
136130 }
0 commit comments