@@ -4311,16 +4311,9 @@ - (NSString *)versionTag
43114311/* *
43124312 * See header file for extensive documentation for this method.
43134313**/
4314- - (NSRange )findRangeInGroup : (NSString *)group
4315- usingBlock : (YapDatabaseViewFindBlock)block
4316- blockType : (YapDatabaseViewBlockType)blockType
4314+ - (NSRange )findRangeInGroup : (NSString *)group using : (YapDatabaseViewFind *)find
43174315{
4318- BOOL invalidBlockType = blockType != YapDatabaseViewBlockTypeWithKey &&
4319- blockType != YapDatabaseViewBlockTypeWithObject &&
4320- blockType != YapDatabaseViewBlockTypeWithMetadata &&
4321- blockType != YapDatabaseViewBlockTypeWithRow;
4322-
4323- if (group == nil || block == NULL || invalidBlockType)
4316+ if (group == nil || find == NULL )
43244317 {
43254318 return NSMakeRange (NSNotFound , 0 );
43264319 }
@@ -4338,9 +4331,11 @@ - (NSRange)findRangeInGroup:(NSString *)group
43384331 return NSMakeRange (NSNotFound , 0 );
43394332 }
43404333
4341- NSComparisonResult (^compare)(NSUInteger ) = ^NSComparisonResult (NSUInteger index){
4342-
4343- int64_t rowid = 0 ;
4334+ // Helper block:
4335+ //
4336+ // Finds the rowid for a given index (within the view.group).
4337+
4338+ int64_t (^findRowid)(NSUInteger ) = ^int64_t (NSUInteger index){
43444339
43454340 NSUInteger pageOffset = 0 ;
43464341 for (YapDatabaseViewPageMetadata *pageMetadata in pagesMetadataForGroup)
@@ -4349,59 +4344,92 @@ - (NSRange)findRangeInGroup:(NSString *)group
43494344 {
43504345 YapDatabaseViewPage *page = [self pageForPageKey: pageMetadata->pageKey];
43514346
4352- rowid = [page rowidAtIndex: (index - pageOffset)];
4353- break ;
4347+ return [page rowidAtIndex: (index - pageOffset)];
43544348 }
43554349 else
43564350 {
43574351 pageOffset += pageMetadata->count ;
43584352 }
43594353 }
43604354
4361- if (blockType == YapDatabaseViewBlockTypeWithKey)
4355+ NSAssert (NO , @" index(%lu ) not found !!!" , (unsigned long )index);
4356+ return (int64_t )0 ;
4357+ };
4358+
4359+ // Helper block:
4360+ //
4361+ // Executes the findBlock against the row represented by the given index (within the view.group).
4362+
4363+ NSComparisonResult (^compare)(NSUInteger );
4364+
4365+ switch (find.findBlockType )
4366+ {
4367+ case YapDatabaseViewBlockTypeWithKey :
43624368 {
43634369 __unsafe_unretained YapDatabaseViewFindWithKeyBlock findBlock =
4364- (YapDatabaseViewFindWithKeyBlock)block;
4365-
4366- YapCollectionKey *ck = [databaseTransaction collectionKeyForRowid: rowid];
4370+ (YapDatabaseViewFindWithKeyBlock)find.findBlock ;
43674371
4368- return findBlock (ck.collection , ck.key );
4372+ compare = ^NSComparisonResult (NSUInteger index){
4373+
4374+ int64_t rowid = findRowid (index);
4375+
4376+ YapCollectionKey *ck = [databaseTransaction collectionKeyForRowid: rowid];
4377+
4378+ return findBlock (ck.collection , ck.key );
4379+ };
43694380 }
4370- else if (blockType == YapDatabaseViewBlockTypeWithObject)
4381+ case YapDatabaseViewBlockTypeWithObject :
43714382 {
43724383 __unsafe_unretained YapDatabaseViewFindWithObjectBlock findBlock =
4373- (YapDatabaseViewFindWithObjectBlock)block;
4374-
4375- YapCollectionKey *ck = nil ;
4376- id object = nil ;
4377- [databaseTransaction getCollectionKey: &ck object: &object forRowid: rowid];
4384+ (YapDatabaseViewFindWithObjectBlock)find.findBlock ;
43784385
4379- return findBlock (ck.collection , ck.key , object);
4386+ compare = ^NSComparisonResult (NSUInteger index){
4387+
4388+ int64_t rowid = findRowid (index);
4389+
4390+ YapCollectionKey *ck = nil ;
4391+ id object = nil ;
4392+ [databaseTransaction getCollectionKey: &ck object: &object forRowid: rowid];
4393+
4394+ return findBlock (ck.collection , ck.key , object);
4395+ };
43804396 }
4381- else if (blockType == YapDatabaseViewBlockTypeWithMetadata)
4397+ case YapDatabaseViewBlockTypeWithMetadata :
43824398 {
43834399 __unsafe_unretained YapDatabaseViewFindWithMetadataBlock findBlock =
4384- (YapDatabaseViewFindWithMetadataBlock)block;
4385-
4386- YapCollectionKey *ck = nil ;
4387- id metadata = nil ;
4388- [databaseTransaction getCollectionKey: &ck metadata: &metadata forRowid: rowid];
4400+ (YapDatabaseViewFindWithMetadataBlock)find.findBlock ;
43894401
4390- return findBlock (ck.collection , ck.key , metadata);
4402+ compare = ^NSComparisonResult (NSUInteger index){
4403+
4404+ int64_t rowid = findRowid (index);
4405+
4406+ YapCollectionKey *ck = nil ;
4407+ id metadata = nil ;
4408+ [databaseTransaction getCollectionKey: &ck metadata: &metadata forRowid: rowid];
4409+
4410+ return findBlock (ck.collection , ck.key , metadata);
4411+ };
43914412 }
4392- else
4413+ default :
43934414 {
43944415 __unsafe_unretained YapDatabaseViewFindWithRowBlock findBlock =
4395- (YapDatabaseViewFindWithRowBlock)block;
4396-
4397- YapCollectionKey *ck = nil ;
4398- id object = nil ;
4399- id metadata = nil ;
4400- [databaseTransaction getCollectionKey: &ck object: &object metadata: &metadata forRowid: rowid];
4416+ (YapDatabaseViewFindWithRowBlock)find.findBlock ;
44014417
4402- return findBlock (ck.collection , ck.key , object, metadata);
4418+ compare = ^NSComparisonResult (NSUInteger index){
4419+
4420+ int64_t rowid = findRowid (index);
4421+
4422+ YapCollectionKey *ck = nil ;
4423+ id object = nil ;
4424+ id metadata = nil ;
4425+ [databaseTransaction getCollectionKey: &ck object: &object metadata: &metadata forRowid: rowid];
4426+
4427+ return findBlock (ck.collection , ck.key , object, metadata);
4428+ };
44034429 }
4404- };
4430+
4431+ } // end switch (blockType)
4432+
44054433
44064434 NSUInteger loopCount = 0 ;
44074435
@@ -4479,6 +4507,17 @@ - (NSRange)findRangeInGroup:(NSString *)group
44794507 return NSMakeRange (sMin , (eMax - sMin ));
44804508}
44814509
4510+ /* *
4511+ * This method is deprecated.
4512+ * Use findRangeInGroup:using: instead.
4513+ **/
4514+ - (NSRange )findRangeInGroup : (NSString *)group
4515+ usingBlock : (YapDatabaseViewFindBlock)block
4516+ blockType : (YapDatabaseViewBlockType)blockType
4517+ {
4518+ return [self findRangeInGroup: group using: [YapDatabaseViewFind withBlock: block blockType: blockType]];
4519+ }
4520+
44824521// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
44834522#pragma mark Public API - Enumerating
44844523// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments