@@ -92,7 +92,8 @@ class CollectionsKVStoreTestBase : public KVStoreBackend, public KVStoreTest {
9292
9393 void applyEvents (TransactionContext& txnCtx,
9494 VB::Commit& commitData,
95- const CollectionsManifest& cm) {
95+ const CollectionsManifest& cm,
96+ bool writeEventNow = true ) {
9697 manifest.update (*vbucket, makeManifest (cm));
9798
9899 std::vector<queued_item> events;
@@ -101,17 +102,44 @@ class CollectionsKVStoreTestBase : public KVStoreBackend, public KVStoreTest {
101102
102103 for (auto & ev : events) {
103104 commitData.collections .recordSystemEvent (*ev);
105+ if (writeEventNow) {
106+ if (ev->isDeleted ()) {
107+ kvstore->delSystemEvent (txnCtx, ev);
108+ } else {
109+ kvstore->setSystemEvent (txnCtx, ev);
110+ }
111+ }
112+ }
113+ if (!writeEventNow) {
114+ std::move (events.begin (),
115+ events.end (),
116+ std::back_inserter (allEvents));
117+ }
118+ }
119+
120+ void applyEvents (TransactionContext& txnCtx,
121+ const CollectionsManifest& cm,
122+ bool writeEventNow = true ) {
123+ return applyEvents (txnCtx, flush, cm, writeEventNow);
124+ }
125+
126+ // This function is to be used in conjunction with applyEvents when
127+ // writeEventNow=false allowing a test to better emulate the flusher and
128+ // write keys in a sorted batch. Tests can applyEvents so that collection
129+ // metadata management does updates, but defer the system event writing
130+ // until ready to commit
131+ void sortAndWriteAllEvents (TransactionContext& txnCtx) {
132+ std::sort (allEvents.begin (),
133+ allEvents.end (),
134+ OrderItemsForDeDuplication{});
135+ for (auto & ev : allEvents) {
104136 if (ev->isDeleted ()) {
105137 kvstore->delSystemEvent (txnCtx, ev);
106138 } else {
107139 kvstore->setSystemEvent (txnCtx, ev);
108140 }
109141 }
110- }
111-
112- void applyEvents (TransactionContext& txnCtx,
113- const CollectionsManifest& cm) {
114- applyEvents (txnCtx, flush, cm);
142+ allEvents.clear ();
115143 }
116144
117145 void checkUid (const Collections::KVStore::Manifest& md,
@@ -224,7 +252,8 @@ class CollectionsKVStoreTestBase : public KVStoreBackend, public KVStoreTest {
224252 VB::Commit commitData (manifest);
225253 auto ctx = kvstore->begin (vbucket->getId (),
226254 std::make_unique<PersistenceCallback>());
227- applyEvents (*ctx, commitData, cm);
255+ applyEvents (*ctx, commitData, cm, false );
256+ sortAndWriteAllEvents (*ctx);
228257 kvstore->commit (std::move (ctx), commitData);
229258 auto [status, md] = kvstore->getCollectionsManifest (Vbid (0 ));
230259 EXPECT_TRUE (status);
@@ -240,6 +269,7 @@ class CollectionsKVStoreTestBase : public KVStoreBackend, public KVStoreTest {
240269 VBucketPtr vbucket;
241270 WriteCallback wc;
242271 DeleteCallback dc;
272+ std::vector<queued_item> allEvents;
243273};
244274
245275class CollectionsKVStoreTest
@@ -583,19 +613,21 @@ class CollectionRessurectionKVStoreTest
583613 auto ctx = kvstore->begin (vbucket->getId (),
584614 std::make_unique<PersistenceCallback>());
585615 cm.add (targetScope);
586- applyEvents (*ctx, cm);
616+ applyEvents (*ctx, cm, false );
587617 cm.add (target, targetScope);
588- applyEvents (*ctx, cm);
618+ applyEvents (*ctx, cm, false );
619+ sortAndWriteAllEvents (*ctx);
589620 kvstore->commit (std::move (ctx), flush);
590621 }
591622
592623 // runs a flush batch that will leave the target collection in dropped state
593624 void dropScope () {
594625 openScopeOpenCollection ();
595- cm.remove (targetScope);
596626 auto ctx = kvstore->begin (vbucket->getId (),
597627 std::make_unique<PersistenceCallback>());
598- applyEvents (*ctx, cm);
628+ cm.remove (targetScope);
629+ applyEvents (*ctx, cm, false );
630+ sortAndWriteAllEvents (*ctx);
599631 kvstore->commit (std::move (ctx), flush);
600632 }
601633
@@ -709,9 +741,9 @@ void CollectionRessurectionKVStoreTest::resurectionScopesTest() {
709741 std::make_unique<PersistenceCallback>());
710742 if (!cm.exists (targetScope)) {
711743 cm.add (targetScope);
712- applyEvents (*ctx, cm);
744+ applyEvents (*ctx, cm, false );
713745 cm.add (target, targetScope);
714- applyEvents (*ctx, cm);
746+ applyEvents (*ctx, cm, false );
715747 }
716748
717749 std::string expectedName = target.name ;
@@ -720,22 +752,23 @@ void CollectionRessurectionKVStoreTest::resurectionScopesTest() {
720752 // iterate cycles of remove/add
721753 for (int ii = 0 ; ii < getCycles (); ii++) {
722754 cm.remove (scope);
723- applyEvents (*ctx, cm);
724-
755+ applyEvents (*ctx, cm, false );
725756 if (resurectWithNewName ()) {
726757 expectedName = target.name + " _" + std::to_string (ii);
727758 scope.name = targetScope.name + " _" + std::to_string (ii);
728759 }
729760 cm.add (scope);
730- applyEvents (*ctx, cm);
761+ applyEvents (*ctx, cm, false );
731762 cm.add ({expectedName, target.uid }, scope);
732- applyEvents (*ctx, cm);
763+ applyEvents (*ctx, cm, false );
733764 }
734765
735766 if (dropCollectionAtEnd ()) {
736767 cm.remove (scope);
737- applyEvents (*ctx, cm);
768+ applyEvents (*ctx, cm, false );
738769 }
770+
771+ sortAndWriteAllEvents (*ctx);
739772 kvstore->commit (std::move (ctx), flush);
740773
741774 // Now validate
0 commit comments