@@ -5,70 +5,78 @@ namespace NKikimr::NCache {
55
66namespace {
77
8- struct TPage {
8+ struct TPage : public TIntrusiveListItem <TPage> {
99 ui32 Id;
1010 size_t Size;
1111
12+ TPage (ui32 id, size_t size)
13+ : Id(id), Size(size)
14+ {}
15+
1216 ui32 CacheFlags1 : 4 = 0 ;
1317 ui32 CacheFlags2 : 4 = 0 ;
1418 };
1519
16- struct TKey {
20+ struct TPageKey {
1721 ui32 Id;
1822
19- TKey (ui32 id)
23+ TPageKey (ui32 id)
2024 : Id(id)
2125 {}
2226
27+ static TPageKey Get (const TPage* page) {
28+ return {page->Id };
29+ }
30+
2331 TString ToString () const {
2432 return std::to_string (Id);
2533 }
2634 };
2735
28- struct TKeyHash {
29- inline size_t operator ()(const TKey & key) const {
36+ struct TPageKeyHash {
37+ inline size_t operator ()(const TPageKey & key) const {
3038 return std::hash<ui32>()(key.Id );
3139 }
3240 };
3341
34- struct TKeyEqual {
35- inline bool operator ()(const TKey & left, const TKey & right) const {
42+ struct TPageKeyEqual {
43+ inline bool operator ()(const TPageKey & left, const TPageKey & right) const {
3644 return left.Id == right.Id ;
3745 }
3846 };
3947
40- struct TSize {
41- static ui64 Get (const TPage *x ) {
42- return x ->Size ;
48+ struct TPageSize {
49+ static ui64 Get (const TPage *page ) {
50+ return page ->Size ;
4351 }
4452 };
4553
46- struct TCacheFlags1 {
47- static ui32 Get (const TPage *x ) {
48- return x ->CacheFlags1 ;
54+ struct TPageLocation {
55+ static ui32 Get (const TPage *page ) {
56+ return page ->CacheFlags1 ;
4957 }
5058 static void Set (TPage *x, ui32 flags) {
5159 Y_ABORT_UNLESS (flags < (1 << 4 ));
5260 x->CacheFlags1 = flags;
5361 }
5462 };
5563
56- struct TCacheFlags2 {
57- static ui32 Get (const TPage *x ) {
58- return x ->CacheFlags2 ;
64+ struct TPageFrequency {
65+ static ui32 Get (const TPage *page ) {
66+ return page ->CacheFlags2 ;
5967 }
60- static void Set (TPage *x , ui32 flags) {
68+ static void Set (TPage *page , ui32 flags) {
6169 Y_ABORT_UNLESS (flags < (1 << 4 ));
62- x ->CacheFlags2 = flags;
70+ page ->CacheFlags2 = flags;
6371 }
6472 };
6573
6674}
6775
68- Y_UNIT_TEST_SUITE (TGhostQueue ) {
76+ Y_UNIT_TEST_SUITE (TTS3FIFOGhostQueue ) {
6977
7078 Y_UNIT_TEST (Add) {
71- TGhostQueue<TKey, TKeyHash, TKeyEqual > queue (100 );
79+ TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual > queue (100 );
7280 UNIT_ASSERT_VALUES_EQUAL (queue.Dump (), " " );
7381
7482 queue.Add (1 , 10 );
@@ -88,7 +96,7 @@ Y_UNIT_TEST_SUITE(TGhostQueue) {
8896 }
8997
9098 Y_UNIT_TEST (Erase) {
91- TGhostQueue<TKey, TKeyHash, TKeyEqual > queue (100 );
99+ TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual > queue (100 );
92100 UNIT_ASSERT_VALUES_EQUAL (queue.Dump (), " " );
93101
94102 queue.Add (1 , 10 );
@@ -110,7 +118,7 @@ Y_UNIT_TEST_SUITE(TGhostQueue) {
110118 }
111119
112120 Y_UNIT_TEST (Erase_Add) {
113- TGhostQueue<TKey, TKeyHash, TKeyEqual > queue (100 );
121+ TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual > queue (100 );
114122 UNIT_ASSERT_VALUES_EQUAL (queue.Dump (), " " );
115123
116124 queue.Add (1 , 10 );
@@ -129,15 +137,15 @@ Y_UNIT_TEST_SUITE(TGhostQueue) {
129137 }
130138
131139 Y_UNIT_TEST (Add_Big) {
132- TGhostQueue<TKey, TKeyHash, TKeyEqual > queue (100 );
140+ TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual > queue (100 );
133141 UNIT_ASSERT_VALUES_EQUAL (queue.Dump (), " " );
134142
135143 queue.Add (1 , 101 );
136144 UNIT_ASSERT_VALUES_EQUAL (queue.Dump (), " " );
137145 }
138146
139147 Y_UNIT_TEST (UpdateLimit) {
140- TGhostQueue<TKey, TKeyHash, TKeyEqual > queue (100 );
148+ TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual > queue (100 );
141149 UNIT_ASSERT_VALUES_EQUAL (queue.Dump (), " " );
142150
143151 queue.Add (1 , 10 );
@@ -149,6 +157,54 @@ Y_UNIT_TEST_SUITE(TGhostQueue) {
149157 queue.UpdateLimit (80 );
150158 UNIT_ASSERT_VALUES_EQUAL (queue.Dump (), " {3 30b}, {4 40b}" );
151159 }
160+
161+ }
162+
163+ Y_UNIT_TEST_SUITE (TS3FIFOCache) {
164+
165+ TVector<TPage*> Touch (auto & cache, TPage& page) {
166+ auto evicted = cache.Touch (&page);
167+ TVector<TPage*> result;
168+ for (auto & p : evicted) {
169+ result.push_back (&p);
170+ }
171+ return result;
172+ }
173+
174+ Y_UNIT_TEST (Touch) {
175+ TS3FIFOCache<TPage, TPageKey, TPageKeyHash, TPageKeyEqual, TPageSize, TPageLocation, TPageFrequency> cache (100 );
176+
177+ TPage page1{1 , 2 };
178+ UNIT_ASSERT_VALUES_EQUAL (Touch (cache, page1), TVector<TPage*>{});
179+ UNIT_ASSERT_VALUES_EQUAL (cache.Dump (), (TString)(TStringBuilder ()
180+ << " SmallQueue: {1 0f 2b}" << Endl
181+ << " MainQueue: " << Endl
182+ << " GhostQueue: " ));
183+
184+ TPage page2{2 , 3 };
185+ UNIT_ASSERT_VALUES_EQUAL (Touch (cache, page2), TVector<TPage*>{});
186+ UNIT_ASSERT_VALUES_EQUAL (cache.Dump (), (TString)(TStringBuilder ()
187+ << " SmallQueue: {1 0f 2b}, {2 0f 3b}" << Endl
188+ << " MainQueue: " << Endl
189+ << " GhostQueue: " ));
190+
191+ TPage page3{3 , 4 };
192+ UNIT_ASSERT_VALUES_EQUAL (Touch (cache, page3), TVector<TPage*>{});
193+ UNIT_ASSERT_VALUES_EQUAL (cache.Dump (), (TString)(TStringBuilder ()
194+ << " SmallQueue: {1 0f 2b}, {2 0f 3b}, {3 0f 4b}" << Endl
195+ << " MainQueue: " << Endl
196+ << " GhostQueue: " ));
197+
198+ TPage page4{4 , 1 };
199+ UNIT_ASSERT_VALUES_EQUAL (Touch (cache, page4), TVector<TPage*>{});
200+ UNIT_ASSERT_VALUES_EQUAL (cache.Dump (), (TString)(TStringBuilder ()
201+ << " SmallQueue: {1 0f 2b}, {2 0f 3b}, {3 0f 4b}, {4 0f 1b}" << Endl
202+ << " MainQueue: " << Endl
203+ << " GhostQueue: " ));
204+
205+
206+ }
207+
152208}
153209
154210}
0 commit comments