1
- use std:: fs;
2
- use std:: time:: { SystemTime , UNIX_EPOCH } ;
3
-
1
+ use crate :: fuzzy:: LspItem ;
4
2
use heed:: types:: * ;
5
3
use heed:: { Database , Env , EnvOpenOptions } ;
4
+ use serde:: { Deserialize , Serialize } ;
5
+ use std:: fs;
6
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
6
7
7
- // todo: changing score offset will change all the keys
8
+ #[ derive( Clone , Serialize , Deserialize ) ]
9
+ struct CompletionItemKey {
10
+ label : String ,
11
+ kind : u32 ,
12
+ source : String ,
13
+ }
8
14
9
- use crate :: fuzzy:: LspItem ;
15
+ impl From < & LspItem > for CompletionItemKey {
16
+ fn from ( item : & LspItem ) -> Self {
17
+ Self {
18
+ label : item. label . clone ( ) ,
19
+ kind : item. kind ,
20
+ source : item. source . clone ( ) ,
21
+ }
22
+ }
23
+ }
10
24
11
25
pub struct FrecencyTracker {
12
26
env : Env ,
13
- db : Database < SerdeBincode < LspItem > , SerdeBincode < Vec < u64 > > > ,
27
+ db : Database < SerdeBincode < CompletionItemKey > , SerdeBincode < Vec < u64 > > > ,
14
28
access_thresholds : Vec < ( f64 , u64 ) > ,
15
29
}
16
30
17
31
impl FrecencyTracker {
18
32
pub fn new ( db_path : & str ) -> Self {
19
- fs:: create_dir_all ( db_path) ;
33
+ fs:: create_dir_all ( db_path) . unwrap ( ) ;
20
34
let env = unsafe { EnvOpenOptions :: new ( ) . open ( db_path) . unwrap ( ) } ;
21
- env. clear_stale_readers ( ) ;
35
+ env. clear_stale_readers ( ) . unwrap ( ) ;
22
36
23
37
// we will open the default unnamed database
24
38
let mut wtxn = env. write_txn ( ) . unwrap ( ) ;
25
39
let db = env. create_database ( & mut wtxn, None ) . unwrap ( ) ;
26
40
27
41
let access_thresholds = [
28
- ( 2 ., 1000 * 60 * 2 ) , // 2 minutes
29
- ( 1. , 1000 * 60 * 5 ) , // 5 minutes
30
- ( 0.5 , 1000 * 60 * 30 ) , // 2 hours
31
- ( 0.2 , 1000 * 60 * 60 * 24 ) , // 1 day
32
- ( 0.1 , 1000 * 60 * 60 * 24 * 7 ) , // 1 week
42
+ ( 1 ., 1000 * 60 * 2 ) , // 2 minutes
43
+ ( 0.5 , 1000 * 60 * 5 ) , // 5 minutes
44
+ ( 0.2 , 1000 * 60 * 30 ) , // 2 hours
45
+ ( 0.1 , 1000 * 60 * 60 * 24 ) , // 1 day
46
+ ( 0.05 , 1000 * 60 * 60 * 24 * 7 ) , // 1 week
33
47
]
34
48
. to_vec ( ) ;
35
49
@@ -46,7 +60,7 @@ impl FrecencyTracker {
46
60
. read_txn ( )
47
61
. expect ( "Failed to start read transaction" ) ;
48
62
self . db
49
- . get ( & rtxn, item)
63
+ . get ( & rtxn, & CompletionItemKey :: from ( item) )
50
64
. expect ( "Failed to read from database" )
51
65
}
52
66
@@ -61,7 +75,8 @@ impl FrecencyTracker {
61
75
let mut wtxn = self . env . write_txn ( ) ?;
62
76
let mut accesses = self . get_accesses ( item) . unwrap_or_else ( Vec :: new) ;
63
77
accesses. push ( self . get_now ( ) ) ;
64
- self . db . put ( & mut wtxn, item, & accesses) ?;
78
+ self . db
79
+ . put ( & mut wtxn, & CompletionItemKey :: from ( item) , & accesses) ?;
65
80
wtxn. commit ( ) ?;
66
81
Ok ( ( ) )
67
82
}
@@ -79,6 +94,6 @@ impl FrecencyTracker {
79
94
continue ' outer;
80
95
}
81
96
}
82
- ( score * accesses. len ( ) as f64 ) . min ( 5 .) as i64
97
+ ( score * accesses. len ( ) as f64 ) . min ( 4 .) as i64
83
98
}
84
99
}
0 commit comments