From dad9275a3017971cfac6992074f8f32c76ec378e Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Fri, 8 Jul 2022 07:00:54 -0700 Subject: [PATCH] Ditch the data compression It just wasn't working out. Signed-off-by: Christopher Snowhill --- .../DataModel.xcdatamodel/contents | 5 +- Playlist/PlaylistEntry.h | 2 - Playlist/PlaylistEntry.m | 88 +------------------ 3 files changed, 3 insertions(+), 92 deletions(-) diff --git a/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents b/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents index 0664176a8..13eef9ccb 100644 --- a/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents +++ b/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents @@ -34,8 +34,7 @@ - - + @@ -64,7 +63,7 @@ - + \ No newline at end of file diff --git a/Playlist/PlaylistEntry.h b/Playlist/PlaylistEntry.h index 110370abc..45a10c67e 100644 --- a/Playlist/PlaylistEntry.h +++ b/Playlist/PlaylistEntry.h @@ -87,8 +87,6 @@ @property(nonatomic) NSString *_Nullable comment; -@property(nonatomic) NSDictionary *_Nullable metadataBlob; - - (NSString *_Nullable)readAllValuesAsString:(NSString *_Nonnull)tagName; - (void)setValue:(NSString *_Nonnull)tagName fromString:(NSString *_Nullable)value; - (void)addValue:(NSString *_Nonnull)tagName fromString:(NSString *_Nonnull)value; diff --git a/Playlist/PlaylistEntry.m b/Playlist/PlaylistEntry.m index ae8f38b65..c879fce06 100644 --- a/Playlist/PlaylistEntry.m +++ b/Playlist/PlaylistEntry.m @@ -16,23 +16,14 @@ #import "SHA256Digest.h" #import "SecondsFormatter.h" -#import - extern NSPersistentContainer *kPersistentContainer; extern NSMutableDictionary *kArtworkDictionary; -static NSMutableDictionary *kMetadataBlobCache = nil; static NSMutableDictionary *kMetadataCache = nil; -static void *kCompressionScratchBuffer = NULL; -static void *kDecompressionScratchBuffer = NULL; - @implementation PlaylistEntry (Extension) + (void)initialize { - if(!kMetadataBlobCache) { - kMetadataBlobCache = [[NSMutableDictionary alloc] init]; - } if(!kMetadataCache) { kMetadataCache = [[NSMutableDictionary alloc] init]; } @@ -585,7 +576,7 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) { [metaDict setObject:values forKey:lowerKey]; } } - self.metadataBlob = metaDict; + self.metadataBlob = [NSDictionary dictionaryWithDictionary:metaDict]; } [self setMetadataLoaded:YES]; @@ -892,81 +883,4 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) { } } -- (NSDictionary *)metadataBlob { - { - NSDictionary *blob = [kMetadataBlobCache objectForKey:self.urlString]; - if(blob) { - return blob; - } - } - - if(self.metadataCompressed == nil || self.metadataDecompressedSize == 0) { - return @{}; - } - - if(!kDecompressionScratchBuffer) { - size_t scratchSize = compression_decode_scratch_buffer_size(COMPRESSION_ZLIB); - kDecompressionScratchBuffer = malloc(scratchSize); - } - - void *decompressionBuffer = malloc(self.metadataDecompressedSize); - - size_t decodedBytes = compression_decode_buffer(decompressionBuffer, self.metadataDecompressedSize, [self.metadataCompressed bytes], [self.metadataCompressed length], kDecompressionScratchBuffer, COMPRESSION_ZLIB); - - NSData *decodedData = [NSData dataWithBytes:decompressionBuffer length:decodedBytes]; - - free(decompressionBuffer); - - NSSet *allowed = [NSSet setWithArray:@[[NSDictionary class], [NSMutableDictionary class], [NSArray class], [NSMutableArray class], [NSString class]]]; - NSError *error = nil; - NSDictionary *dict; - dict = [NSKeyedUnarchiver unarchivedObjectOfClasses:allowed - fromData:decodedData - error:&error]; - - if(!dict) { - dict = @{}; - } - - [kMetadataBlobCache setObject:dict forKey:self.urlString]; - - return dict; -} - -- (void)setMetadataBlob:(NSMutableDictionary *)metadataBlob { - [kMetadataCache removeObjectForKey:self.urlString]; - - if(metadataBlob == nil) { - self.metadataCompressed = nil; - self.metadataDecompressedSize = 0; - [kMetadataBlobCache removeObjectForKey:self.urlString]; - return; - } - - [kMetadataBlobCache setObject:metadataBlob forKey:self.urlString]; - - NSError *error = nil; - NSData *data = [NSKeyedArchiver archivedDataWithRootObject:metadataBlob - requiringSecureCoding:YES - error:&error]; - - if(!kCompressionScratchBuffer) { - size_t scratchBufferSize = compression_encode_scratch_buffer_size(COMPRESSION_ZLIB); - kCompressionScratchBuffer = malloc(scratchBufferSize); - } - - size_t fullSize = [data length]; - size_t compressedSize = fullSize * 3 / 2 + 256; - void *compressedBuffer = malloc(compressedSize); - - size_t compressedOutSize = compression_encode_buffer(compressedBuffer, compressedSize, [data bytes], fullSize, kCompressionScratchBuffer, COMPRESSION_ZLIB); - - NSData *compressData = [NSData dataWithBytes:compressedBuffer length:compressedOutSize]; - - free(compressedBuffer); - - self.metadataCompressed = compressData; - self.metadataDecompressedSize = fullSize; -} - @end