5
5
using Flow . Launcher . Infrastructure ;
6
6
using Flow . Launcher . Infrastructure . Storage ;
7
7
using Flow . Launcher . Plugin ;
8
+ using Flow . Launcher . ViewModel ;
8
9
9
10
namespace Flow . Launcher . Storage
10
11
{
@@ -14,72 +15,90 @@ public class UserSelectedRecord
14
15
private const int HASH_INITIAL = 23 ;
15
16
16
17
[ JsonInclude ]
17
- public Dictionary < int , int > records { get ; private set ; }
18
+ public Dictionary < int , int > recordsWithQuery { get ; private set ; }
19
+
20
+ [ JsonInclude , JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingNull ) ]
21
+ public Dictionary < string , int > records { get ; private set ; }
22
+
18
23
19
24
public UserSelectedRecord ( )
20
25
{
21
- records = new Dictionary < int , int > ( ) ;
26
+ recordsWithQuery = new Dictionary < int , int > ( ) ;
22
27
}
23
28
24
- private static int GenerateCustomHashCode ( Query query , Result result )
29
+ private static int GenerateStaticHashCode ( string s , int start = HASH_INITIAL )
25
30
{
26
- int hashcode = HASH_INITIAL ;
27
-
28
31
unchecked
29
32
{
30
33
// skip the empty space
31
34
// https://stackoverflow.com/a/5155015 31 prime and is 2^5 - 1 which allows fast
32
35
// optimization without information lost when int overflow
33
-
34
- for ( int i = 0 ; i < query . ActionKeyword . Length ; i ++ )
35
- {
36
- char item = query . ActionKeyword [ i ] ;
37
- hashcode = hashcode * HASH_MULTIPLIER + item ;
38
- }
39
-
40
- for ( int i = 0 ; i < query . Search . Length ; i ++ )
41
- {
42
- char item = query . Search [ i ] ;
43
- hashcode = hashcode * HASH_MULTIPLIER + item ;
44
- }
45
36
46
- for ( int i = 0 ; i < result . Title . Length ; i ++ )
37
+ for ( int i = 0 ; i < s . Length ; i ++ )
47
38
{
48
- char item = result . Title [ i ] ;
49
- hashcode = hashcode * HASH_MULTIPLIER + item ;
39
+ start = start * HASH_MULTIPLIER + s [ i ] ;
50
40
}
51
41
52
- for ( int i = 0 ; i < result . SubTitle . Length ; i ++ )
42
+ return start ;
43
+ }
44
+ }
45
+
46
+ private static int GenerateResultHashCode ( Result result )
47
+ {
48
+ int hashcode = GenerateStaticHashCode ( result . Title ) ;
49
+ return GenerateStaticHashCode ( result . SubTitle , hashcode ) ;
50
+ }
51
+
52
+ private static int GenerateQueryAndResultHashCode ( Query query , Result result )
53
+ {
54
+ int hashcode = GenerateStaticHashCode ( query . ActionKeyword ) ;
55
+ hashcode = GenerateStaticHashCode ( query . Search , hashcode ) ;
56
+ hashcode = GenerateStaticHashCode ( result . Title , hashcode ) ;
57
+ hashcode = GenerateStaticHashCode ( result . SubTitle , hashcode ) ;
58
+
59
+ return hashcode ;
60
+ }
61
+
62
+ private void TransformOldRecords ( )
63
+ {
64
+ if ( records != null )
65
+ {
66
+ var localRecords = records ;
67
+ records = null ;
68
+
69
+ foreach ( var pair in localRecords )
53
70
{
54
- char item = result . SubTitle [ i ] ;
55
- hashcode = hashcode * HASH_MULTIPLIER + item ;
71
+ recordsWithQuery . TryAdd ( GenerateStaticHashCode ( pair . Key ) , pair . Value ) ;
56
72
}
57
- return hashcode ;
58
-
59
73
}
60
74
}
61
75
62
76
public void Add ( Result result )
63
77
{
64
- var key = GenerateCustomHashCode ( result . OriginQuery , result ) ;
65
- if ( records . ContainsKey ( key ) )
66
- {
67
- records [ key ] ++ ;
68
- }
69
- else
70
- {
71
- records . Add ( key , 1 ) ;
78
+ TransformOldRecords ( ) ;
72
79
73
- }
80
+ var keyWithQuery = GenerateQueryAndResultHashCode ( result . OriginQuery , result ) ;
81
+
82
+ if ( ! recordsWithQuery . TryAdd ( keyWithQuery , 1 ) )
83
+ recordsWithQuery [ keyWithQuery ] ++ ;
84
+
85
+ var keyWithoutQuery = GenerateResultHashCode ( result ) ;
86
+
87
+ if ( ! recordsWithQuery . TryAdd ( keyWithoutQuery , 1 ) )
88
+ recordsWithQuery [ keyWithoutQuery ] ++ ;
74
89
}
75
90
76
91
public int GetSelectedCount ( Result result )
77
92
{
78
- if ( records . TryGetValue ( GenerateCustomHashCode ( result . OriginQuery , result ) , out int value ) )
79
- {
80
- return value ;
81
- }
82
- return 0 ;
93
+ var selectedCount = 0 ;
94
+
95
+ recordsWithQuery . TryGetValue ( GenerateQueryAndResultHashCode ( result . OriginQuery , result ) , out int value ) ;
96
+ selectedCount += value * 5 ;
97
+
98
+ recordsWithQuery . TryGetValue ( GenerateResultHashCode ( result ) , out value ) ;
99
+ selectedCount += value ;
100
+
101
+ return selectedCount ;
83
102
}
84
103
}
85
104
}
0 commit comments