|
25 | 25 | static NSString *const FICImageTableMetadataFileExtension = @"metadata";
|
26 | 26 | static NSString *const FICImageTableFileExtension = @"imageTable";
|
27 | 27 |
|
28 |
| -static NSString *const FICImageTableIndexMapKey = @"indexMap"; |
29 |
| -static NSString *const FICImageTableContextMapKey = @"contextMap"; |
30 |
| -static NSString *const FICImageTableMRUArrayKey = @"mruArray"; |
| 28 | +static NSString *const FICImageTableMetadataKey = @"metadata"; |
| 29 | +static NSString *const FICImageTableMRUIndexKey = @"mruIndex"; |
| 30 | +static NSString *const FICImageTableContextUUIDKey = @"contextUUID"; |
| 31 | +static NSString *const FICImageTableIndexKey = @"tableIndex"; |
31 | 32 | static NSString *const FICImageTableFormatKey = @"format";
|
32 | 33 |
|
33 | 34 | #pragma mark - Class Extension
|
@@ -595,11 +596,28 @@ - (NSNumber *)_numberForEntryAtIndex:(NSInteger)index {
|
595 | 596 | - (void)saveMetadata {
|
596 | 597 | [_lock lock];
|
597 | 598 |
|
| 599 | + NSMutableDictionary *entryMetadata = [NSMutableDictionary dictionary]; |
| 600 | + for (NSString *entityUUID in [_indexMap allKeys]) { |
| 601 | + NSMutableDictionary *entryDict = [entryMetadata objectForKey:entityUUID]; |
| 602 | + if (!entryDict) { |
| 603 | + entryDict = [[NSMutableDictionary alloc] init]; |
| 604 | + [entryMetadata setObject:entryDict forKey:entityUUID]; |
| 605 | + } |
| 606 | + NSNumber *tableIndexVal = [_indexMap objectForKey:entityUUID]; |
| 607 | + NSString *contextUUID = [_sourceImageMap objectForKey:entityUUID]; |
| 608 | + NSInteger mruIndex = [_MRUEntries indexOfObject:entityUUID]; |
| 609 | + |
| 610 | + [entryDict setValue:tableIndexVal forKey:FICImageTableIndexKey]; |
| 611 | + [entryDict setValue:contextUUID forKey:FICImageTableContextUUIDKey]; |
| 612 | + if (mruIndex != NSNotFound) { |
| 613 | + [entryDict setValue:[NSNumber numberWithInteger:mruIndex] forKey:FICImageTableMRUIndexKey]; |
| 614 | + } |
| 615 | + } |
| 616 | + |
598 | 617 | NSDictionary *metadataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
|
599 |
| - [_indexMap copy], FICImageTableIndexMapKey, |
600 |
| - [_sourceImageMap copy], FICImageTableContextMapKey, |
601 |
| - [[_MRUEntries array] copy], FICImageTableMRUArrayKey, |
| 618 | + entryMetadata, FICImageTableMetadataKey, |
602 | 619 | [_imageFormatDictionary copy], FICImageTableFormatKey, nil];
|
| 620 | + |
603 | 621 | [_lock unlock];
|
604 | 622 |
|
605 | 623 | static dispatch_queue_t __metadataQueue = nil;
|
@@ -635,19 +653,39 @@ - (void)_loadMetadata {
|
635 | 653 | [[FICImageCache sharedImageCache] _logMessage:message];
|
636 | 654 | }
|
637 | 655 |
|
638 |
| - [_indexMap setDictionary:[metadataDictionary objectForKey:FICImageTableIndexMapKey]]; |
639 |
| - |
640 |
| - for (NSNumber *index in [_indexMap allValues]) { |
641 |
| - [_occupiedIndexes addIndex:[index intValue]]; |
| 656 | + NSDictionary *tableMetadata = [metadataDictionary objectForKey:FICImageTableMetadataKey]; |
| 657 | + NSInteger count = [tableMetadata count]; |
| 658 | + NSMutableArray *mruArray = [NSMutableArray arrayWithCapacity:count]; |
| 659 | + for (NSInteger i = 0; i < count; i++) { |
| 660 | + [mruArray addObject:[NSNull null]]; |
642 | 661 | }
|
| 662 | + NSMutableIndexSet *nullIndexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, count)]; |
643 | 663 |
|
644 |
| - [_sourceImageMap setDictionary:[metadataDictionary objectForKey:FICImageTableContextMapKey]]; |
645 |
| - |
| 664 | + [_indexMap removeAllObjects]; |
| 665 | + [_sourceImageMap removeAllObjects]; |
646 | 666 | [_MRUEntries removeAllObjects];
|
647 | 667 |
|
648 |
| - NSArray *mruArray = [metadataDictionary objectForKey:FICImageTableMRUArrayKey]; |
649 |
| - if (mruArray) { |
650 |
| - [_MRUEntries addObjectsFromArray:mruArray]; |
| 668 | + for (NSString *entityUUID in [tableMetadata allKeys]) { |
| 669 | + NSDictionary *entryDict = [tableMetadata objectForKey:entityUUID]; |
| 670 | + [_indexMap setValue:[entryDict objectForKey:FICImageTableIndexKey] forKey:entityUUID]; |
| 671 | + [_sourceImageMap setValue:[entryDict objectForKey:FICImageTableContextUUIDKey] forKey:entityUUID]; |
| 672 | + NSNumber *mruIndexVal = [entryDict objectForKey:FICImageTableMRUIndexKey]; |
| 673 | + if (mruIndexVal) { |
| 674 | + NSInteger mruIndex = [mruIndexVal integerValue]; |
| 675 | + [mruArray replaceObjectAtIndex:mruIndex withObject:entityUUID]; |
| 676 | + [nullIndexes removeIndex:mruIndex]; |
| 677 | + } |
| 678 | + } |
| 679 | + |
| 680 | + NSUInteger index = [nullIndexes lastIndex]; |
| 681 | + while (index != NSNotFound) { |
| 682 | + [mruArray removeObjectAtIndex:index]; |
| 683 | + index = [nullIndexes indexLessThanIndex:index]; |
| 684 | + } |
| 685 | + [_MRUEntries addObjectsFromArray:mruArray]; |
| 686 | + |
| 687 | + for (NSNumber *index in [_indexMap allValues]) { |
| 688 | + [_occupiedIndexes addIndex:[index intValue]]; |
651 | 689 | }
|
652 | 690 | }
|
653 | 691 | }
|
|
0 commit comments