Ditch the data compression

It just wasn't working out.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
main
Christopher Snowhill 2022-07-08 07:00:54 -07:00 committed by Christopher Snowhill
parent 6222e25adc
commit dad9275a30
3 changed files with 3 additions and 92 deletions

View File

@ -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"/>
@ -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>

View File

@ -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;

View File

@ -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];
}
@ -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