Skip to content

Commit

Permalink
Ditch the data compression
Browse files Browse the repository at this point in the history
It just wasn't working out.

Signed-off-by: Christopher Snowhill <[email protected]>
  • Loading branch information
kode54 committed Jul 8, 2022
1 parent 6222e25 commit dad9275
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 92 deletions.
5 changes: 2 additions & 3 deletions DataModel.xcdatamodeld/DataModel.xcdatamodel/contents
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
<attribute name="errorMessage" optional="YES" attributeType="String"/>
<attribute name="floatingPoint" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="index" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="metadataCompressed" optional="YES" attributeType="Binary"/>
<attribute name="metadataDecompressedSize" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="metadataBlob" optional="YES" attributeType="Transformable"/>
<attribute name="metadataLoaded" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="queued" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="queuePosition" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
Expand Down Expand Up @@ -64,7 +63,7 @@
<elements>
<element name="AlbumArtwork" positionX="0" positionY="207" width="128" height="59"/>
<element name="PlayCount" positionX="-18" positionY="171" width="128" height="149"/>
<element name="PlaylistEntry" positionX="-36" positionY="9" width="128" height="629"/>
<element name="PlaylistEntry" positionX="-36" positionY="9" width="128" height="614"/>
<element name="SandboxToken" positionX="-18" positionY="171" width="128" height="74"/>
</elements>
</model>
2 changes: 0 additions & 2 deletions Playlist/PlaylistEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
88 changes: 1 addition & 87 deletions Playlist/PlaylistEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@
#import "SHA256Digest.h"
#import "SecondsFormatter.h"

#import <compression.h>

extern NSPersistentContainer *kPersistentContainer;
extern NSMutableDictionary<NSString *, AlbumArtwork *> *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];
}
Expand Down Expand Up @@ -585,7 +576,7 @@ - (void)setMetadata:(NSDictionary *)metadata {
[metaDict setObject:values forKey:lowerKey];
}
}
self.metadataBlob = metaDict;
self.metadataBlob = [NSDictionary dictionaryWithDictionary:metaDict];
}

[self setMetadataLoaded:YES];
Expand Down Expand Up @@ -892,81 +883,4 @@ - (void)addValue:(NSString *_Nonnull)tagName fromString:(NSString *_Nonnull)valu
}
}

- (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

0 comments on commit dad9275

Please sign in to comment.