@@ -88,25 +88,53 @@ class CollectionsKVStoreTestBase : public KVStoreBackend, public KVStoreTest {
88
88
89
89
void applyEvents (TransactionContext& txnCtx,
90
90
VB::Commit& commitData,
91
- const CollectionsManifest& cm) {
91
+ const CollectionsManifest& cm,
92
+ bool writeEventNow = true ) {
92
93
manifest.update (*vbucket, makeManifest (cm));
93
94
94
95
std::vector<queued_item> events;
95
96
getEventsFromCheckpoint (events);
96
97
97
98
for (auto & ev : events) {
98
99
commitData.collections .recordSystemEvent (*ev);
100
+ if (writeEventNow) {
101
+ if (ev->isDeleted ()) {
102
+ kvstore->delSystemEvent (txnCtx, ev);
103
+ } else {
104
+ kvstore->setSystemEvent (txnCtx, ev);
105
+ }
106
+ }
107
+ }
108
+ if (!writeEventNow) {
109
+ std::move (events.begin (),
110
+ events.end (),
111
+ std::back_inserter (allEvents));
112
+ }
113
+ }
114
+
115
+ void applyEvents (TransactionContext& txnCtx,
116
+ const CollectionsManifest& cm,
117
+ bool writeEventNow = true ) {
118
+ return applyEvents (txnCtx, flush, cm, writeEventNow);
119
+ }
120
+
121
+ // This function is to be used in conjunction with applyEvents when
122
+ // writeEventNow=false allowing a test to better emulate the flusher and
123
+ // write keys in a sorted batch. Tests can applyEvents so that collection
124
+ // metadata management does updates, but defer the system event writing
125
+ // until ready to commit
126
+ void sortAndWriteAllEvents (TransactionContext& txnCtx) {
127
+ std::sort (allEvents.begin (),
128
+ allEvents.end (),
129
+ OrderItemsForDeDuplication{});
130
+ for (auto & ev : allEvents) {
99
131
if (ev->isDeleted ()) {
100
132
kvstore->delSystemEvent (txnCtx, ev);
101
133
} else {
102
134
kvstore->setSystemEvent (txnCtx, ev);
103
135
}
104
136
}
105
- }
106
-
107
- void applyEvents (TransactionContext& txnCtx,
108
- const CollectionsManifest& cm) {
109
- applyEvents (txnCtx, flush, cm);
137
+ allEvents.clear ();
110
138
}
111
139
112
140
void checkUid (const Collections::KVStore::Manifest& md,
@@ -219,7 +247,8 @@ class CollectionsKVStoreTestBase : public KVStoreBackend, public KVStoreTest {
219
247
VB::Commit commitData (manifest);
220
248
auto ctx = kvstore->begin (vbucket->getId (),
221
249
std::make_unique<PersistenceCallback>());
222
- applyEvents (*ctx, commitData, cm);
250
+ applyEvents (*ctx, commitData, cm, false );
251
+ sortAndWriteAllEvents (*ctx);
223
252
kvstore->commit (std::move (ctx), commitData);
224
253
auto [status, md] = kvstore->getCollectionsManifest (Vbid (0 ));
225
254
EXPECT_TRUE (status);
@@ -235,6 +264,7 @@ class CollectionsKVStoreTestBase : public KVStoreBackend, public KVStoreTest {
235
264
VBucketPtr vbucket;
236
265
WriteCallback wc;
237
266
DeleteCallback dc;
267
+ std::vector<queued_item> allEvents;
238
268
};
239
269
240
270
class CollectionsKVStoreTest
@@ -578,19 +608,21 @@ class CollectionRessurectionKVStoreTest
578
608
auto ctx = kvstore->begin (vbucket->getId (),
579
609
std::make_unique<PersistenceCallback>());
580
610
cm.add (targetScope);
581
- applyEvents (*ctx, cm);
611
+ applyEvents (*ctx, cm, false );
582
612
cm.add (target, targetScope);
583
- applyEvents (*ctx, cm);
613
+ applyEvents (*ctx, cm, false );
614
+ sortAndWriteAllEvents (*ctx);
584
615
kvstore->commit (std::move (ctx), flush);
585
616
}
586
617
587
618
// runs a flush batch that will leave the target collection in dropped state
588
619
void dropScope () {
589
620
openScopeOpenCollection ();
590
- cm.remove (targetScope);
591
621
auto ctx = kvstore->begin (vbucket->getId (),
592
622
std::make_unique<PersistenceCallback>());
593
- applyEvents (*ctx, cm);
623
+ cm.remove (targetScope);
624
+ applyEvents (*ctx, cm, false );
625
+ sortAndWriteAllEvents (*ctx);
594
626
kvstore->commit (std::move (ctx), flush);
595
627
}
596
628
@@ -704,9 +736,9 @@ void CollectionRessurectionKVStoreTest::resurectionScopesTest() {
704
736
std::make_unique<PersistenceCallback>());
705
737
if (!cm.exists (targetScope)) {
706
738
cm.add (targetScope);
707
- applyEvents (*ctx, cm);
739
+ applyEvents (*ctx, cm, false );
708
740
cm.add (target, targetScope);
709
- applyEvents (*ctx, cm);
741
+ applyEvents (*ctx, cm, false );
710
742
}
711
743
712
744
std::string expectedName = target.name ;
@@ -715,22 +747,23 @@ void CollectionRessurectionKVStoreTest::resurectionScopesTest() {
715
747
// iterate cycles of remove/add
716
748
for (int ii = 0 ; ii < getCycles (); ii++) {
717
749
cm.remove (scope);
718
- applyEvents (*ctx, cm);
719
-
750
+ applyEvents (*ctx, cm, false );
720
751
if (resurectWithNewName ()) {
721
752
expectedName = target.name + " _" + std::to_string (ii);
722
753
scope.name = targetScope.name + " _" + std::to_string (ii);
723
754
}
724
755
cm.add (scope);
725
- applyEvents (*ctx, cm);
756
+ applyEvents (*ctx, cm, false );
726
757
cm.add ({expectedName, target.uid }, scope);
727
- applyEvents (*ctx, cm);
758
+ applyEvents (*ctx, cm, false );
728
759
}
729
760
730
761
if (dropCollectionAtEnd ()) {
731
762
cm.remove (scope);
732
- applyEvents (*ctx, cm);
763
+ applyEvents (*ctx, cm, false );
733
764
}
765
+
766
+ sortAndWriteAllEvents (*ctx);
734
767
kvstore->commit (std::move (ctx), flush);
735
768
736
769
// Now validate
0 commit comments