55using Flow . Launcher . Infrastructure ;
66using Flow . Launcher . Infrastructure . Storage ;
77using Flow . Launcher . Plugin ;
8+ using Flow . Launcher . ViewModel ;
89
910namespace Flow . Launcher . Storage
1011{
@@ -14,72 +15,90 @@ public class UserSelectedRecord
1415 private const int HASH_INITIAL = 23 ;
1516
1617 [ 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+
1823
1924 public UserSelectedRecord ( )
2025 {
21- records = new Dictionary < int , int > ( ) ;
26+ recordsWithQuery = new Dictionary < int , int > ( ) ;
2227 }
2328
24- private static int GenerateCustomHashCode ( Query query , Result result )
29+ private static int GenerateStaticHashCode ( string s , int start = HASH_INITIAL )
2530 {
26- int hashcode = HASH_INITIAL ;
27-
2831 unchecked
2932 {
3033 // skip the empty space
3134 // https://stackoverflow.com/a/5155015 31 prime and is 2^5 - 1 which allows fast
3235 // 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- }
4536
46- for ( int i = 0 ; i < result . Title . Length ; i ++ )
37+ for ( int i = 0 ; i < s . Length ; i ++ )
4738 {
48- char item = result . Title [ i ] ;
49- hashcode = hashcode * HASH_MULTIPLIER + item ;
39+ start = start * HASH_MULTIPLIER + s [ i ] ;
5040 }
5141
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 )
5370 {
54- char item = result . SubTitle [ i ] ;
55- hashcode = hashcode * HASH_MULTIPLIER + item ;
71+ recordsWithQuery . TryAdd ( GenerateStaticHashCode ( pair . Key ) , pair . Value ) ;
5672 }
57- return hashcode ;
58-
5973 }
6074 }
6175
6276 public void Add ( Result result )
6377 {
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 ( ) ;
7279
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 ] ++ ;
7489 }
7590
7691 public int GetSelectedCount ( Result result )
7792 {
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 ;
83102 }
84103 }
85104}
0 commit comments