1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
#include "checkpoint_map.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/random/random.h>
namespace NYql {
Y_UNIT_TEST_SUITE(CheckpointHashMapTests) {
Y_UNIT_TEST(BasicOperations) {
TCheckpointHashMap<ui32, TString> map;
// Check empty map
UNIT_ASSERT(map.empty());
UNIT_ASSERT_VALUES_EQUAL(0, map.size());
// Add elements
map.Set(1, "one");
map.Set(2, "two");
UNIT_ASSERT(!map.empty());
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
// Check values using find
auto it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("one", it1->second);
auto it2 = map.find(2);
UNIT_ASSERT(it2 != map.end());
UNIT_ASSERT_VALUES_EQUAL("two", it2->second);
}
Y_UNIT_TEST(CheckpointAndRollback) {
TCheckpointHashMap<ui32, TString> map;
// Add initial data
map.Set(1, "one");
map.Set(2, "two");
// Make checkpoint
map.Checkpoint();
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount());
// Modify data after checkpoint
map.Set(1, "ONE");
map.Set(3, "three");
map.erase(2);
UNIT_ASSERT_VALUES_EQUAL(3, map.GetCommandCount());
auto it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("ONE", it1->second);
auto it3 = map.find(3);
UNIT_ASSERT(it3 != map.end());
UNIT_ASSERT_VALUES_EQUAL("three", it3->second);
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT(map.find(2) == map.end());
// Rollback
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount());
it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("one", it1->second);
auto it2 = map.find(2);
UNIT_ASSERT(it2 != map.end());
UNIT_ASSERT_VALUES_EQUAL("two", it2->second);
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT(map.find(3) == map.end());
}
Y_UNIT_TEST(SetOperations) {
TCheckpointHashMap<ui32, TString> map;
map.Checkpoint();
// Test Set
map.Set(1, "one");
auto it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("one", it1->second);
// Overwrite existing key
map.Set(1, "ONE");
it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("ONE", it1->second);
// Test Set with another key
map.Set(2, "two");
auto it2 = map.find(2);
UNIT_ASSERT(it2 != map.end());
UNIT_ASSERT_VALUES_EQUAL("two", it2->second);
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
// Rollback
map.Rollback();
UNIT_ASSERT(map.empty());
}
Y_UNIT_TEST(EraseOperations) {
TCheckpointHashMap<ui32, TString> map;
// Prepare data
map.Set(1, "one");
map.Set(2, "two");
map.Set(3, "three");
map.Checkpoint();
// Erase by key
UNIT_ASSERT_VALUES_EQUAL(1, map.erase(2));
UNIT_ASSERT_VALUES_EQUAL(0, map.erase(10)); // non-existent key
// Erase by key
UNIT_ASSERT_VALUES_EQUAL(1, map.erase(3));
UNIT_ASSERT_VALUES_EQUAL(1, map.size());
auto it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("one", it1->second);
// Rollback
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("one", it1->second);
auto it2 = map.find(2);
UNIT_ASSERT(it2 != map.end());
UNIT_ASSERT_VALUES_EQUAL("two", it2->second);
auto it3 = map.find(3);
UNIT_ASSERT(it3 != map.end());
UNIT_ASSERT_VALUES_EQUAL("three", it3->second);
}
Y_UNIT_TEST(SearchOperations) {
TCheckpointHashMap<ui32, TString> map;
map.Set(1, "one");
map.Set(2, "two");
// Test find
auto it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("one", it1->second);
auto it2 = map.find(10);
UNIT_ASSERT(it2 == map.end());
// Test count
UNIT_ASSERT_VALUES_EQUAL(1, map.count(1));
UNIT_ASSERT_VALUES_EQUAL(0, map.count(10));
// Test contains
UNIT_ASSERT(map.contains(1));
UNIT_ASSERT(!map.contains(10));
}
Y_UNIT_TEST(Iterators) {
TCheckpointHashMap<ui32, TString> map;
map.Set(1, "one");
map.Set(2, "two");
map.Set(3, "three");
// Test iterators
ui32 count = 0;
for (auto it = map.cbegin(); it != map.cend(); ++it) {
count++;
UNIT_ASSERT(it->first >= 1 && it->first <= 3);
}
UNIT_ASSERT_VALUES_EQUAL(3, count);
// Test const iterators
const auto& constMap = map;
count = 0;
for (auto it = constMap.cbegin(); it != constMap.cend(); ++it) {
count++;
}
UNIT_ASSERT_VALUES_EQUAL(3, count);
// Test range-based for
count = 0;
for (const auto& pair : map) {
count++;
UNIT_ASSERT(pair.first >= 1 && pair.first <= 3);
}
UNIT_ASSERT_VALUES_EQUAL(3, count);
}
Y_UNIT_TEST(ComparisonOperators) {
TCheckpointHashMap<ui32, TString> map1;
TCheckpointHashMap<ui32, TString> map2;
// Empty maps are equal
UNIT_ASSERT(map1 == map2);
UNIT_ASSERT(!(map1 != map2));
// Add data to first map
map1.Set(1, "one");
UNIT_ASSERT(!(map1 == map2));
UNIT_ASSERT(map1 != map2);
// Add same data to second map
map2.Set(1, "one");
UNIT_ASSERT(map1 == map2);
UNIT_ASSERT(!(map1 != map2));
}
Y_UNIT_TEST(MultipleCheckpointsAndRollbacks) {
TCheckpointHashMap<ui32, TString> map;
// State 1
map.Set(1, "one");
map.Checkpoint();
// State 2
map.Set(2, "two");
map.Checkpoint();
// State 3
map.Set(3, "three");
map.Set(1, "ONE");
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
auto it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("ONE", it1->second);
// Rollback to state 2
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("one", it1->second);
auto it2 = map.find(2);
UNIT_ASSERT(it2 != map.end());
UNIT_ASSERT_VALUES_EQUAL("two", it2->second);
UNIT_ASSERT(map.find(3) == map.end());
// New changes
map.Set(4, "four");
map.erase(1);
// Verify state after changes
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
it2 = map.find(2);
UNIT_ASSERT(it2 != map.end());
UNIT_ASSERT_VALUES_EQUAL("two", it2->second);
auto it4 = map.find(4);
UNIT_ASSERT(it4 != map.end());
UNIT_ASSERT_VALUES_EQUAL("four", it4->second);
UNIT_ASSERT(map.find(1) == map.end());
// Rollback to state 1
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(1, map.size());
it1 = map.find(1);
UNIT_ASSERT(it1 != map.end());
UNIT_ASSERT_VALUES_EQUAL("one", it1->second);
UNIT_ASSERT(map.find(2) == map.end());
UNIT_ASSERT(map.find(4) == map.end());
}
Y_UNIT_TEST(ComplexScenario) {
TCheckpointHashMap<TString, ui32> map;
// Initial state
map.Set("a", 1);
map.Set("b", 2);
map.Set("c", 3);
map.Checkpoint();
// Complex changes
map.Set("a", 10); // update
map.Set("d", 4); // new element
map.erase("b"); // deletion
map.Set("e", 5); // another new element
map.Set("c", 30); // another update
UNIT_ASSERT_VALUES_EQUAL(4, map.size());
auto ita = map.find("a");
UNIT_ASSERT(ita != map.end());
UNIT_ASSERT_VALUES_EQUAL(10, ita->second);
auto itc = map.find("c");
UNIT_ASSERT(itc != map.end());
UNIT_ASSERT_VALUES_EQUAL(30, itc->second);
auto itd = map.find("d");
UNIT_ASSERT(itd != map.end());
UNIT_ASSERT_VALUES_EQUAL(4, itd->second);
auto ite = map.find("e");
UNIT_ASSERT(ite != map.end());
UNIT_ASSERT_VALUES_EQUAL(5, ite->second);
UNIT_ASSERT(map.find("b") == map.end());
// Rollback
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
ita = map.find("a");
UNIT_ASSERT(ita != map.end());
UNIT_ASSERT_VALUES_EQUAL(1, ita->second);
auto itb = map.find("b");
UNIT_ASSERT(itb != map.end());
UNIT_ASSERT_VALUES_EQUAL(2, itb->second);
itc = map.find("c");
UNIT_ASSERT(itc != map.end());
UNIT_ASSERT_VALUES_EQUAL(3, itc->second);
UNIT_ASSERT(map.find("d") == map.end());
UNIT_ASSERT(map.find("e") == map.end());
}
Y_UNIT_TEST(EmptyMapOperations) {
TCheckpointHashMap<ui32, TString> map;
map.Checkpoint();
// Operations on empty map
UNIT_ASSERT_VALUES_EQUAL(0, map.erase(1));
UNIT_ASSERT(map.find(1) == map.end());
UNIT_ASSERT_VALUES_EQUAL(0, map.count(1));
UNIT_ASSERT(!map.contains(1));
map.Rollback();
UNIT_ASSERT(map.empty());
}
Y_UNIT_TEST(RollbackWithNoChanges) {
TCheckpointHashMap<ui32, TString> map;
map.Checkpoint();
map.Checkpoint();
map.Set(1, "string");
map.Checkpoint();
UNIT_ASSERT_VALUES_EQUAL(1, map.size());
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(1, map.size());
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(0, map.size());
map.Rollback();
UNIT_ASSERT(map.empty());
}
Y_UNIT_TEST(RandomOperationsTest) {
// Test constants
constexpr ui32 initialSize = 20;
constexpr ui32 numOperations = 100;
constexpr ui32 keyRange = 50;
constexpr ui32 valueRange = 1000;
constexpr ui32 initialValueMultiplier = 10;
enum class EOperation {
Insert = 0,
Erase = 1,
Update = 2,
Count = 3
};
TCheckpointHashMap<ui32, ui32> map;
THashMap<ui32, ui32> referenceMap;
// Fill initial data
for (ui32 i = 0; i < initialSize; ++i) {
ui32 value = i * initialValueMultiplier;
map.Set(i, value);
referenceMap[i] = value;
}
// Save reference state before checkpoint
auto savedState = referenceMap;
map.Checkpoint();
// Perform random operations
for (ui32 op = 0; op < numOperations; ++op) {
auto operation = static_cast<EOperation>(RandomNumber<ui32>() % static_cast<ui32>(EOperation::Count));
ui32 key = RandomNumber<ui32>() % keyRange;
switch (operation) {
case EOperation::Insert: {
ui32 value = RandomNumber<ui32>() % valueRange;
map.Set(key, value);
referenceMap[key] = value;
break;
}
case EOperation::Erase: {
map.erase(key);
referenceMap.erase(key);
break;
}
case EOperation::Update: {
auto it = referenceMap.find(key);
if (it != referenceMap.end()) {
ui32 newValue = RandomNumber<ui32>() % valueRange;
map.Set(key, newValue);
referenceMap[key] = newValue;
}
break;
}
case EOperation::Count:
// Should not reach here
break;
}
}
// Verify current state matches reference
UNIT_ASSERT_VALUES_EQUAL(map.size(), referenceMap.size());
for (const auto& pair : referenceMap) {
UNIT_ASSERT(map.contains(pair.first));
auto it = map.find(pair.first);
UNIT_ASSERT(it != map.end());
UNIT_ASSERT_VALUES_EQUAL(it->second, pair.second);
}
// Rollback and verify state matches saved state
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(map.size(), savedState.size());
for (const auto& pair : savedState) {
UNIT_ASSERT(map.contains(pair.first));
auto it = map.find(pair.first);
UNIT_ASSERT(it != map.end());
UNIT_ASSERT_VALUES_EQUAL(it->second, pair.second);
}
// Verify no extra elements exist
for (const auto& pair : map) {
UNIT_ASSERT(savedState.find(pair.first) != savedState.end());
}
}
Y_UNIT_TEST(MultipleCheckpointsAndRollbacksTest) {
TCheckpointHashMap<ui32, TString> map;
// Initial state
map.Set(1, "initial1");
map.Set(2, "initial2");
// First checkpoint
map.Checkpoint();
map.Set(1, "checkpoint1_1");
map.Set(3, "checkpoint1_3");
// Second checkpoint
map.Checkpoint();
map.Set(2, "checkpoint2_2");
map.Set(4, "checkpoint2_4");
// Third checkpoint
map.Checkpoint();
map.Set(1, "checkpoint3_1");
map.erase(3);
// Verify current state
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
UNIT_ASSERT_VALUES_EQUAL("checkpoint3_1", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("checkpoint2_2", map.Get(2));
UNIT_ASSERT_VALUES_EQUAL("checkpoint2_4", map.Get(4));
UNIT_ASSERT(map.find(3) == map.end());
// First rollback (undo checkpoint 3)
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(4, map.size());
UNIT_ASSERT_VALUES_EQUAL("checkpoint1_1", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("checkpoint2_2", map.Get(2));
UNIT_ASSERT_VALUES_EQUAL("checkpoint1_3", map.Get(3));
UNIT_ASSERT_VALUES_EQUAL("checkpoint2_4", map.Get(4));
// Second rollback (undo checkpoint 2)
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
UNIT_ASSERT_VALUES_EQUAL("checkpoint1_1", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("initial2", map.Get(2));
UNIT_ASSERT_VALUES_EQUAL("checkpoint1_3", map.Get(3));
UNIT_ASSERT(map.find(4) == map.end());
// Third rollback (undo checkpoint 1)
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT_VALUES_EQUAL("initial1", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("initial2", map.Get(2));
UNIT_ASSERT(map.find(3) == map.end());
UNIT_ASSERT(map.find(4) == map.end());
// Test multiple checkpoints without rollback
map.Checkpoint();
map.Set(5, "test5");
map.Checkpoint();
map.Set(6, "test6");
map.Checkpoint();
map.Set(7, "test7");
UNIT_ASSERT_VALUES_EQUAL(5, map.size());
// Multiple rollbacks
map.Rollback(); // Remove 7
UNIT_ASSERT_VALUES_EQUAL(4, map.size());
UNIT_ASSERT(map.find(7) == map.end());
map.Rollback(); // Remove 6
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
UNIT_ASSERT(map.find(6) == map.end());
map.Rollback(); // Remove 5
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT(map.find(5) == map.end());
}
Y_UNIT_TEST(RollbackWithoutCheckpointTest) {
TCheckpointHashMap<ui32, TString> map;
// Try to rollback without checkpoint - should throw
UNIT_ASSERT_EXCEPTION_CONTAINS(map.Rollback(), yexception, "Cannot rollback without checkpoint");
// Add some data and try rollback again - should still throw
map.Set(1, "test");
UNIT_ASSERT_EXCEPTION_CONTAINS(map.Rollback(), yexception, "Cannot rollback without checkpoint");
// Now make checkpoint and rollback should work
map.Checkpoint();
map.Set(2, "test2");
map.Rollback(); // Should not throw
// After rollback, another rollback should throw
UNIT_ASSERT_EXCEPTION_CONTAINS(map.Rollback(), yexception, "Cannot rollback without checkpoint");
}
Y_UNIT_TEST(GetUnderlyingMapTest) {
TCheckpointHashMap<ui32, TString> map;
map.Set(1, "one");
map.Set(2, "two");
const auto& underlying = map.GetUnderlyingMap();
UNIT_ASSERT_VALUES_EQUAL(2, underlying.size());
UNIT_ASSERT(underlying.find(1) != underlying.end());
UNIT_ASSERT(underlying.find(2) != underlying.end());
UNIT_ASSERT_VALUES_EQUAL("one", underlying.at(1));
UNIT_ASSERT_VALUES_EQUAL("two", underlying.at(2));
}
Y_UNIT_TEST(GetCommandCountTest) {
TCheckpointHashMap<ui32, TString> map;
// No checkpoint - command count should be 0
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount());
map.Set(1, "one");
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount()); // Still no checkpoint
map.Checkpoint();
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount()); // Just after checkpoint
map.Set(2, "two");
UNIT_ASSERT_VALUES_EQUAL(1, map.GetCommandCount());
map.Set(3, "three");
UNIT_ASSERT_VALUES_EQUAL(2, map.GetCommandCount());
map.erase(1);
UNIT_ASSERT_VALUES_EQUAL(3, map.GetCommandCount());
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount()); // After rollback
}
Y_UNIT_TEST(OperationsWithoutCheckpointTest) {
TCheckpointHashMap<ui32, TString> map;
// Operations without checkpoint should not record commands
map.Set(1, "one");
map.Set(2, "two");
map.erase(1);
map.Set(3, "three");
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount());
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT_VALUES_EQUAL("two", map.Get(2));
UNIT_ASSERT_VALUES_EQUAL("three", map.Get(3));
// After checkpoint, operations should record commands
map.Checkpoint();
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount());
map.Set(4, "four");
UNIT_ASSERT_VALUES_EQUAL(1, map.GetCommandCount());
map.Set(2, "TWO");
UNIT_ASSERT_VALUES_EQUAL(2, map.GetCommandCount());
// Rollback should work
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(0, map.GetCommandCount());
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT_VALUES_EQUAL("two", map.Get(2));
UNIT_ASSERT(map.find(4) == map.end());
}
Y_UNIT_TEST(RollbackThenModifyWithoutCheckpointTest) {
TCheckpointHashMap<ui32, TString> map;
// Initial state
map.Set(1, "one");
map.Set(2, "two");
map.Set(3, "three");
// Make checkpoint
map.Checkpoint();
map.Set(1, "ONE");
map.Set(4, "four");
map.erase(2);
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
UNIT_ASSERT_VALUES_EQUAL("ONE", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("three", map.Get(3));
UNIT_ASSERT_VALUES_EQUAL("four", map.Get(4));
UNIT_ASSERT(map.find(2) == map.end());
// Rollback
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
UNIT_ASSERT_VALUES_EQUAL("one", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("two", map.Get(2));
UNIT_ASSERT_VALUES_EQUAL("three", map.Get(3));
UNIT_ASSERT(map.find(4) == map.end());
// Make changes AFTER rollback but WITHOUT new checkpoint
map.Set(2, "TWO");
map.Set(5, "five");
map.erase(3);
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
UNIT_ASSERT_VALUES_EQUAL("one", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("TWO", map.Get(2));
UNIT_ASSERT_VALUES_EQUAL("five", map.Get(5));
UNIT_ASSERT(map.find(3) == map.end());
// Try to rollback again - should throw because no active checkpoint
UNIT_ASSERT_EXCEPTION_CONTAINS(map.Rollback(), yexception, "Cannot rollback without checkpoint");
// State should remain unchanged after failed rollback
UNIT_ASSERT_VALUES_EQUAL(3, map.size());
UNIT_ASSERT_VALUES_EQUAL("one", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("TWO", map.Get(2));
UNIT_ASSERT_VALUES_EQUAL("five", map.Get(5));
UNIT_ASSERT(map.find(3) == map.end());
}
Y_UNIT_TEST(RollbackThenCheckpointThenModifyThenRollbackTest) {
TCheckpointHashMap<ui32, TString> map;
// Initial state
map.Set(1, "one");
map.Set(2, "two");
// First checkpoint
map.Checkpoint();
map.Set(1, "ONE");
map.Set(3, "three");
// First rollback
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT_VALUES_EQUAL("one", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("two", map.Get(2));
UNIT_ASSERT(map.find(3) == map.end());
// Second checkpoint AFTER rollback
map.Checkpoint();
map.Set(2, "TWO");
map.Set(4, "four");
map.erase(1);
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT_VALUES_EQUAL("TWO", map.Get(2));
UNIT_ASSERT_VALUES_EQUAL("four", map.Get(4));
UNIT_ASSERT(map.find(1) == map.end());
// Second rollback
map.Rollback();
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT_VALUES_EQUAL("one", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("two", map.Get(2));
UNIT_ASSERT(map.find(3) == map.end());
UNIT_ASSERT(map.find(4) == map.end());
}
Y_UNIT_TEST(CheckpointGuardTest) {
TCheckpointHashMap<ui32, TString> map;
// Initial state
map.Set(1, "one");
map.Set(2, "two");
{
// Guard automatically creates checkpoint
TCheckpointGuard<ui32, TString> guard(map);
// Make changes
map.Set(1, "ONE");
map.Set(3, "three");
map.erase(2);
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT_VALUES_EQUAL("ONE", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("three", map.Get(3));
UNIT_ASSERT(map.find(2) == map.end());
// Guard destructor will automatically rollback
}
// After guard destruction, changes should be rolled back
UNIT_ASSERT_VALUES_EQUAL(2, map.size());
UNIT_ASSERT_VALUES_EQUAL("one", map.Get(1));
UNIT_ASSERT_VALUES_EQUAL("two", map.Get(2));
UNIT_ASSERT(map.find(3) == map.end());
}
} // Y_UNIT_TEST_SUITE(CheckpointHashMapTests)
} // namespace NYql
|