FSTMutationQueueTests.mm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Example/Tests/Local/FSTMutationQueueTests.h"
  17. #import <FirebaseFirestore/FIRTimestamp.h>
  18. #import "Firestore/Source/Core/FSTQuery.h"
  19. #import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
  20. #import "Firestore/Source/Local/FSTMutationQueue.h"
  21. #import "Firestore/Source/Local/FSTPersistence.h"
  22. #import "Firestore/Source/Local/FSTWriteGroup.h"
  23. #import "Firestore/Source/Model/FSTMutation.h"
  24. #import "Firestore/Source/Model/FSTMutationBatch.h"
  25. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  26. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  27. using firebase::firestore::auth::User;
  28. NS_ASSUME_NONNULL_BEGIN
  29. @implementation FSTMutationQueueTests
  30. - (void)tearDown {
  31. [self.mutationQueue shutdown];
  32. [self.persistence shutdown];
  33. [super tearDown];
  34. }
  35. /**
  36. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  37. * FSTMutationQueueTests since it is incomplete without the implementations supplied by its
  38. * subclasses.
  39. */
  40. - (BOOL)isTestBaseClass {
  41. return [self class] == [FSTMutationQueueTests class];
  42. }
  43. - (void)testCountBatches {
  44. if ([self isTestBaseClass]) return;
  45. XCTAssertEqual(0, [self batchCount]);
  46. XCTAssertTrue([self.mutationQueue isEmpty]);
  47. FSTMutationBatch *batch1 = [self addMutationBatch];
  48. XCTAssertEqual(1, [self batchCount]);
  49. XCTAssertFalse([self.mutationQueue isEmpty]);
  50. FSTMutationBatch *batch2 = [self addMutationBatch];
  51. XCTAssertEqual(2, [self batchCount]);
  52. [self removeMutationBatches:@[ batch2 ]];
  53. XCTAssertEqual(1, [self batchCount]);
  54. [self removeMutationBatches:@[ batch1 ]];
  55. XCTAssertEqual(0, [self batchCount]);
  56. XCTAssertTrue([self.mutationQueue isEmpty]);
  57. }
  58. - (void)testAcknowledgeBatchID {
  59. if ([self isTestBaseClass]) return;
  60. // Initial state of an empty queue
  61. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown);
  62. // Adding mutation batches should not change the highest acked batchID.
  63. FSTMutationBatch *batch1 = [self addMutationBatch];
  64. FSTMutationBatch *batch2 = [self addMutationBatch];
  65. FSTMutationBatch *batch3 = [self addMutationBatch];
  66. XCTAssertGreaterThan(batch1.batchID, kFSTBatchIDUnknown);
  67. XCTAssertGreaterThan(batch2.batchID, batch1.batchID);
  68. XCTAssertGreaterThan(batch3.batchID, batch2.batchID);
  69. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown);
  70. [self acknowledgeBatch:batch1];
  71. [self acknowledgeBatch:batch2];
  72. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  73. [self removeMutationBatches:@[ batch1 ]];
  74. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  75. [self removeMutationBatches:@[ batch2 ]];
  76. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  77. // Batch 3 never acknowledged.
  78. [self removeMutationBatches:@[ batch3 ]];
  79. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  80. }
  81. - (void)testAcknowledgeThenRemove {
  82. if ([self isTestBaseClass]) return;
  83. FSTMutationBatch *batch1 = [self addMutationBatch];
  84. FSTWriteGroup *group = [self.persistence startGroupWithAction:NSStringFromSelector(_cmd)];
  85. [self.mutationQueue acknowledgeBatch:batch1 streamToken:nil group:group];
  86. [self.mutationQueue removeMutationBatches:@[ batch1 ] group:group];
  87. [self.persistence commitGroup:group];
  88. XCTAssertEqual([self batchCount], 0);
  89. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch1.batchID);
  90. }
  91. - (void)testHighestAcknowledgedBatchIDNeverExceedsNextBatchID {
  92. if ([self isTestBaseClass]) return;
  93. FSTMutationBatch *batch1 = [self addMutationBatch];
  94. FSTMutationBatch *batch2 = [self addMutationBatch];
  95. [self acknowledgeBatch:batch1];
  96. [self acknowledgeBatch:batch2];
  97. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  98. [self removeMutationBatches:@[ batch1, batch2 ]];
  99. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  100. // Restart the queue so that nextBatchID will be reset.
  101. [self.mutationQueue shutdown];
  102. self.mutationQueue = [self.persistence mutationQueueForUser:User("user")];
  103. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"Start MutationQueue"];
  104. [self.mutationQueue startWithGroup:group];
  105. [self.persistence commitGroup:group];
  106. // Verify that on restart with an empty queue, nextBatchID falls to a lower value.
  107. XCTAssertLessThan(self.mutationQueue.nextBatchID, batch2.batchID);
  108. // As a result highestAcknowledgedBatchID must also reset lower.
  109. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown);
  110. // The mutation queue will reset the next batchID after all mutations are removed so adding
  111. // another mutation will cause a collision.
  112. FSTMutationBatch *newBatch = [self addMutationBatch];
  113. XCTAssertEqual(newBatch.batchID, batch1.batchID);
  114. // Restart the queue with one unacknowledged batch in it.
  115. group = [self.persistence startGroupWithAction:@"Start MutationQueue"];
  116. [self.mutationQueue startWithGroup:group];
  117. [self.persistence commitGroup:group];
  118. XCTAssertEqual([self.mutationQueue nextBatchID], newBatch.batchID + 1);
  119. // highestAcknowledgedBatchID must still be kFSTBatchIDUnknown.
  120. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown);
  121. }
  122. - (void)testLookupMutationBatch {
  123. if ([self isTestBaseClass]) return;
  124. // Searching on an empty queue should not find a non-existent batch
  125. FSTMutationBatch *notFound = [self.mutationQueue lookupMutationBatch:42];
  126. XCTAssertNil(notFound);
  127. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  128. NSArray<FSTMutationBatch *> *removed = [self makeHoles:@[ @2, @6, @7 ] inBatches:batches];
  129. // After removing, a batch should not be found
  130. for (NSUInteger i = 0; i < removed.count; i++) {
  131. notFound = [self.mutationQueue lookupMutationBatch:removed[i].batchID];
  132. XCTAssertNil(notFound);
  133. }
  134. // Remaining entries should still be found
  135. for (FSTMutationBatch *batch in batches) {
  136. FSTMutationBatch *found = [self.mutationQueue lookupMutationBatch:batch.batchID];
  137. XCTAssertEqual(found.batchID, batch.batchID);
  138. }
  139. // Even on a nonempty queue searching should not find a non-existent batch
  140. notFound = [self.mutationQueue lookupMutationBatch:42];
  141. XCTAssertNil(notFound);
  142. }
  143. - (void)testNextMutationBatchAfterBatchID {
  144. if ([self isTestBaseClass]) return;
  145. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  146. // This is an array of successors assuming the removals below will happen:
  147. NSArray<FSTMutationBatch *> *afters = @[ batches[3], batches[8], batches[8] ];
  148. NSArray<FSTMutationBatch *> *removed = [self makeHoles:@[ @2, @6, @7 ] inBatches:batches];
  149. for (NSUInteger i = 0; i < batches.count - 1; i++) {
  150. FSTMutationBatch *current = batches[i];
  151. FSTMutationBatch *next = batches[i + 1];
  152. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:current.batchID];
  153. XCTAssertEqual(found.batchID, next.batchID);
  154. }
  155. for (NSUInteger i = 0; i < removed.count; i++) {
  156. FSTMutationBatch *current = removed[i];
  157. FSTMutationBatch *next = afters[i];
  158. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:current.batchID];
  159. XCTAssertEqual(found.batchID, next.batchID);
  160. }
  161. FSTMutationBatch *first = batches[0];
  162. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:first.batchID - 42];
  163. XCTAssertEqual(found.batchID, first.batchID);
  164. FSTMutationBatch *last = batches[batches.count - 1];
  165. FSTMutationBatch *notFound = [self.mutationQueue nextMutationBatchAfterBatchID:last.batchID];
  166. XCTAssertNil(notFound);
  167. }
  168. - (void)testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches {
  169. if ([self isTestBaseClass]) return;
  170. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:3];
  171. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:kFSTBatchIDUnknown],
  172. batches[0]);
  173. [self acknowledgeBatch:batches[0]];
  174. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:kFSTBatchIDUnknown],
  175. batches[1]);
  176. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:batches[0].batchID],
  177. batches[1]);
  178. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:batches[1].batchID],
  179. batches[2]);
  180. }
  181. - (void)testAllMutationBatchesThroughBatchID {
  182. if ([self isTestBaseClass]) return;
  183. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  184. [self makeHoles:@[ @2, @6, @7 ] inBatches:batches];
  185. NSArray<FSTMutationBatch *> *found, *expected;
  186. found = [self.mutationQueue allMutationBatchesThroughBatchID:batches[0].batchID - 1];
  187. XCTAssertEqualObjects(found, (@[]));
  188. for (NSUInteger i = 0; i < batches.count; i++) {
  189. found = [self.mutationQueue allMutationBatchesThroughBatchID:batches[i].batchID];
  190. expected = [batches subarrayWithRange:NSMakeRange(0, i + 1)];
  191. XCTAssertEqualObjects(found, expected, @"for index %lu", (unsigned long)i);
  192. }
  193. }
  194. - (void)testAllMutationBatchesAffectingDocumentKey {
  195. if ([self isTestBaseClass]) return;
  196. NSArray<FSTMutation *> *mutations = @[
  197. FSTTestSetMutation(@"fob/bar",
  198. @{ @"a" : @1 }),
  199. FSTTestSetMutation(@"foo/bar",
  200. @{ @"a" : @1 }),
  201. FSTTestPatchMutation("foo/bar",
  202. @{ @"b" : @1 }, {}),
  203. FSTTestSetMutation(@"foo/bar/suffix/key",
  204. @{ @"a" : @1 }),
  205. FSTTestSetMutation(@"foo/baz",
  206. @{ @"a" : @1 }),
  207. FSTTestSetMutation(@"food/bar",
  208. @{ @"a" : @1 })
  209. ];
  210. // Store all the mutations.
  211. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  212. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"New mutation batch"];
  213. for (FSTMutation *mutation in mutations) {
  214. FSTMutationBatch *batch =
  215. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  216. mutations:@[ mutation ]
  217. group:group];
  218. [batches addObject:batch];
  219. }
  220. [self.persistence commitGroup:group];
  221. NSArray<FSTMutationBatch *> *expected = @[ batches[1], batches[2] ];
  222. NSArray<FSTMutationBatch *> *matches =
  223. [self.mutationQueue allMutationBatchesAffectingDocumentKey:FSTTestDocKey(@"foo/bar")];
  224. XCTAssertEqualObjects(matches, expected);
  225. }
  226. - (void)testAllMutationBatchesAffectingQuery {
  227. if ([self isTestBaseClass]) return;
  228. NSArray<FSTMutation *> *mutations = @[
  229. FSTTestSetMutation(@"fob/bar",
  230. @{ @"a" : @1 }),
  231. FSTTestSetMutation(@"foo/bar",
  232. @{ @"a" : @1 }),
  233. FSTTestPatchMutation("foo/bar",
  234. @{ @"b" : @1 }, {}),
  235. FSTTestSetMutation(@"foo/bar/suffix/key",
  236. @{ @"a" : @1 }),
  237. FSTTestSetMutation(@"foo/baz",
  238. @{ @"a" : @1 }),
  239. FSTTestSetMutation(@"food/bar",
  240. @{ @"a" : @1 })
  241. ];
  242. // Store all the mutations.
  243. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  244. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"New mutation batch"];
  245. for (FSTMutation *mutation in mutations) {
  246. FSTMutationBatch *batch =
  247. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  248. mutations:@[ mutation ]
  249. group:group];
  250. [batches addObject:batch];
  251. }
  252. [self.persistence commitGroup:group];
  253. NSArray<FSTMutationBatch *> *expected = @[ batches[1], batches[2], batches[4] ];
  254. FSTQuery *query = FSTTestQuery("foo");
  255. NSArray<FSTMutationBatch *> *matches =
  256. [self.mutationQueue allMutationBatchesAffectingQuery:query];
  257. XCTAssertEqualObjects(matches, expected);
  258. }
  259. - (void)testRemoveMutationBatches {
  260. if ([self isTestBaseClass]) return;
  261. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  262. FSTMutationBatch *last = batches[batches.count - 1];
  263. [self removeMutationBatches:@[ batches[0] ]];
  264. [batches removeObjectAtIndex:0];
  265. XCTAssertEqual([self batchCount], 9);
  266. NSArray<FSTMutationBatch *> *found;
  267. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  268. XCTAssertEqualObjects(found, batches);
  269. XCTAssertEqual(found.count, 9);
  270. [self removeMutationBatches:@[ batches[0], batches[1], batches[2] ]];
  271. [batches removeObjectsInRange:NSMakeRange(0, 3)];
  272. XCTAssertEqual([self batchCount], 6);
  273. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  274. XCTAssertEqualObjects(found, batches);
  275. XCTAssertEqual(found.count, 6);
  276. [self removeMutationBatches:@[ batches[batches.count - 1] ]];
  277. [batches removeObjectAtIndex:batches.count - 1];
  278. XCTAssertEqual([self batchCount], 5);
  279. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  280. XCTAssertEqualObjects(found, batches);
  281. XCTAssertEqual(found.count, 5);
  282. [self removeMutationBatches:@[ batches[3] ]];
  283. [batches removeObjectAtIndex:3];
  284. XCTAssertEqual([self batchCount], 4);
  285. [self removeMutationBatches:@[ batches[1] ]];
  286. [batches removeObjectAtIndex:1];
  287. XCTAssertEqual([self batchCount], 3);
  288. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  289. XCTAssertEqualObjects(found, batches);
  290. XCTAssertEqual(found.count, 3);
  291. XCTAssertFalse([self.mutationQueue isEmpty]);
  292. [self removeMutationBatches:batches];
  293. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  294. XCTAssertEqualObjects(found, @[]);
  295. XCTAssertEqual(found.count, 0);
  296. XCTAssertTrue([self.mutationQueue isEmpty]);
  297. }
  298. - (void)testRemoveMutationBatchesEmitsGarbageEvents {
  299. if ([self isTestBaseClass]) return;
  300. FSTEagerGarbageCollector *garbageCollector = [[FSTEagerGarbageCollector alloc] init];
  301. [garbageCollector addGarbageSource:self.mutationQueue];
  302. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  303. [batches addObjectsFromArray:@[
  304. [self addMutationBatchWithKey:@"foo/bar"],
  305. [self addMutationBatchWithKey:@"foo/ba"],
  306. [self addMutationBatchWithKey:@"foo/bar2"],
  307. [self addMutationBatchWithKey:@"foo/bar"],
  308. [self addMutationBatchWithKey:@"foo/bar/suffix/baz"],
  309. [self addMutationBatchWithKey:@"bar/baz"],
  310. ]];
  311. [self removeMutationBatches:@[ batches[0] ]];
  312. NSSet<FSTDocumentKey *> *garbage = [garbageCollector collectGarbage];
  313. FSTAssertEqualSets(garbage, @[]);
  314. [self removeMutationBatches:@[ batches[1] ]];
  315. garbage = [garbageCollector collectGarbage];
  316. FSTAssertEqualSets(garbage, @[ FSTTestDocKey(@"foo/ba") ]);
  317. [self removeMutationBatches:@[ batches[5] ]];
  318. garbage = [garbageCollector collectGarbage];
  319. FSTAssertEqualSets(garbage, @[ FSTTestDocKey(@"bar/baz") ]);
  320. [self removeMutationBatches:@[ batches[2], batches[3] ]];
  321. garbage = [garbageCollector collectGarbage];
  322. FSTAssertEqualSets(garbage, (@[ FSTTestDocKey(@"foo/bar"), FSTTestDocKey(@"foo/bar2") ]));
  323. [batches addObject:[self addMutationBatchWithKey:@"foo/bar/suffix/baz"]];
  324. garbage = [garbageCollector collectGarbage];
  325. FSTAssertEqualSets(garbage, @[]);
  326. [self removeMutationBatches:@[ batches[4], batches[6] ]];
  327. garbage = [garbageCollector collectGarbage];
  328. FSTAssertEqualSets(garbage, @[ FSTTestDocKey(@"foo/bar/suffix/baz") ]);
  329. }
  330. - (void)testStreamToken {
  331. if ([self isTestBaseClass]) return;
  332. NSData *streamToken1 = [@"token1" dataUsingEncoding:NSUTF8StringEncoding];
  333. NSData *streamToken2 = [@"token2" dataUsingEncoding:NSUTF8StringEncoding];
  334. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"initial stream token"];
  335. [self.mutationQueue setLastStreamToken:streamToken1 group:group];
  336. [self.persistence commitGroup:group];
  337. FSTMutationBatch *batch1 = [self addMutationBatch];
  338. [self addMutationBatch];
  339. XCTAssertEqualObjects([self.mutationQueue lastStreamToken], streamToken1);
  340. group = [self.persistence startGroupWithAction:@"acknowledgeBatchID"];
  341. [self.mutationQueue acknowledgeBatch:batch1 streamToken:streamToken2 group:group];
  342. [self.persistence commitGroup:group];
  343. XCTAssertEqual(self.mutationQueue.highestAcknowledgedBatchID, batch1.batchID);
  344. XCTAssertEqualObjects([self.mutationQueue lastStreamToken], streamToken2);
  345. }
  346. /** Creates a new FSTMutationBatch with the next batch ID and a set of dummy mutations. */
  347. - (FSTMutationBatch *)addMutationBatch {
  348. return [self addMutationBatchWithKey:@"foo/bar"];
  349. }
  350. /**
  351. * Creates a new FSTMutationBatch with the given key, the next batch ID and a set of dummy
  352. * mutations.
  353. */
  354. - (FSTMutationBatch *)addMutationBatchWithKey:(NSString *)key {
  355. FSTSetMutation *mutation = FSTTestSetMutation(key, @{ @"a" : @1 });
  356. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"New mutation batch"];
  357. FSTMutationBatch *batch =
  358. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  359. mutations:@[ mutation ]
  360. group:group];
  361. [self.persistence commitGroup:group];
  362. return batch;
  363. }
  364. /**
  365. * Creates an array of batches containing @a number dummy FSTMutationBatches. Each has a different
  366. * batchID.
  367. */
  368. - (NSMutableArray<FSTMutationBatch *> *)createBatches:(int)number {
  369. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  370. for (int i = 0; i < number; i++) {
  371. FSTMutationBatch *batch = [self addMutationBatch];
  372. [batches addObject:batch];
  373. }
  374. return batches;
  375. }
  376. /**
  377. * Calls -acknowledgeBatch:streamToken:group: on the mutation queue in a new group and commits the
  378. * the group.
  379. */
  380. - (void)acknowledgeBatch:(FSTMutationBatch *)batch {
  381. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"Ack batchID"];
  382. [self.mutationQueue acknowledgeBatch:batch streamToken:nil group:group];
  383. [self.persistence commitGroup:group];
  384. }
  385. /**
  386. * Calls -removeMutationBatches:group: on the mutation queue in a new group and commits the group.
  387. */
  388. - (void)removeMutationBatches:(NSArray<FSTMutationBatch *> *)batches {
  389. FSTWriteGroup *group = [self.persistence startGroupWithAction:@"Remove mutation batch"];
  390. [self.mutationQueue removeMutationBatches:batches group:group];
  391. [self.persistence commitGroup:group];
  392. }
  393. /** Returns the number of mutation batches in the mutation queue. */
  394. - (NSUInteger)batchCount {
  395. return [self.mutationQueue allMutationBatches].count;
  396. }
  397. /**
  398. * Removes entries from from the given @a batches and returns them.
  399. *
  400. * @param holes An array of indexes in the batches array; in increasing order. Indexes are relative
  401. * to the original state of the batches array, not any intermediate state that might occur.
  402. * @param batches The array to mutate, removing entries from it.
  403. * @return A new array containing all the entries that were removed from @a batches.
  404. */
  405. - (NSArray<FSTMutationBatch *> *)makeHoles:(NSArray<NSNumber *> *)holes
  406. inBatches:(NSMutableArray<FSTMutationBatch *> *)batches {
  407. NSMutableArray<FSTMutationBatch *> *removed = [NSMutableArray array];
  408. for (NSUInteger i = 0; i < holes.count; i++) {
  409. NSUInteger index = holes[i].unsignedIntegerValue - i;
  410. FSTMutationBatch *batch = batches[index];
  411. [self removeMutationBatches:@[ batch ]];
  412. [batches removeObjectAtIndex:index];
  413. [removed addObject:batch];
  414. }
  415. return removed;
  416. }
  417. @end
  418. NS_ASSUME_NONNULL_END