Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOCacheManager(Private) Category Reference

Instance Methods

(void) - loadCache
 
(void) - write
 
(void) - clear
 
(BOOL) - dirty
 
(void) - markClean
 
(NSDictionary *) - loadDict
 
(BOOL) - writeDict:
 
(void) - buildCachesFromDictionary:
 
(NSDictionary *) - dictionaryOfCaches
 
(BOOL) - directoryExists:create:
 
(NSString *) - cachePathCreatingIfNecessary: [implementation]
 

Detailed Description

Definition at line 83 of file OOCacheManager.m.

Method Documentation

◆ buildCachesFromDictionary:

- (void) buildCachesFromDictionary: (NSDictionary *) inDict

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

576{
577 NSEnumerator *keyEnum = nil;
578 id key = nil;
579 id value = nil;
580 NSMutableDictionary *cache = nil;
581
582 if (inDict == nil ) return;
583
584 [_caches release];
585 _caches = [[NSMutableDictionary alloc] initWithCapacity:[inDict count]];
586
587 for (keyEnum = [inDict keyEnumerator]; (key = [keyEnum nextObject]); )
588 {
589 value = [inDict oo_dictionaryForKey:key];
590 if (value != nil)
591 {
592 cache = [NSMutableDictionary dictionaryWithDictionary:value];
593 if (cache != nil)
594 {
595 [_caches setObject:cache forKey:key];
596 }
597 }
598 }
599}
600
return nil

◆ cachePathCreatingIfNecessary:

- (NSString *) cachePathCreatingIfNecessary: (BOOL) create
implementation

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

637{
638 NSString *cachePath = [self cacheDirectoryPathCreatingIfNecessary:create];
639 return [cachePath stringByAppendingPathComponent:@"Data Cache.plist"];
640}
641

◆ clear

- (void) clear

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

475{
476 [_caches release];
477 _caches = nil;
478}
479

◆ dictionaryOfCaches

- (NSDictionary *) dictionaryOfCaches

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

603{
604 return [OODeepCopy(_caches) autorelease];
605}
606

◆ directoryExists:create:

- (BOOL) directoryExists: (NSString *) inPath
create: (BOOL) inCreate 

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

609{
610 BOOL exists, directory;
611 NSFileManager *fmgr = [NSFileManager defaultManager];
612
613 exists = [fmgr fileExistsAtPath:inPath isDirectory:&directory];
614
615 if (exists && !directory)
616 {
617 OOLog(kOOLogDataCacheBuildPathError, @"Expected %@ to be a folder, but it is a file.", inPath);
618 return NO;
619 }
620 if (!exists)
621 {
622 if (!inCreate) return NO;
623 if (![fmgr oo_createDirectoryAtPath:inPath attributes:nil])
624 {
625 OOLog(kOOLogDataCacheBuildPathError, @"Could not create folder %@.", inPath);
626 return NO;
627 }
628 }
629
630 return YES;
631}
632
static NSString *const kOOLogDataCacheBuildPathError
#define OOLog(class, format,...)
Definition OOLogging.h:88

◆ dirty

- (BOOL) dirty

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

482{
483 return _dirty;
484}
485

◆ loadCache

- (void) loadCache

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

330{
331 NSDictionary *cache = nil;
332 NSString *cacheVersion = nil;
333 NSString *ooliteVersion = nil;
334 NSData *endianTag = nil;
335 NSNumber *formatVersion = nil;
336 BOOL accept = YES;
337 uint64_t endianTagValue = 0;
338
339 ooliteVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
340
341 [self clear];
342
343 cache = [self loadDict];
344 if (cache != nil)
345 {
346 // We have a cache
347 OOLog(kOOLogDataCacheFound, @"%@", @"Found data cache.");
349
350 cacheVersion = [cache objectForKey:kCacheKeyVersion];
351 if (![cacheVersion isEqual:ooliteVersion])
352 {
353 OOLog(kOOLogDataCacheRebuild, @"Data cache version (%@) does not match Oolite version (%@), rebuilding cache.", cacheVersion, ooliteVersion);
354 accept = NO;
355 }
356
357 formatVersion = [cache objectForKey:kCacheKeyFormatVersion];
358 if (accept && [formatVersion unsignedIntValue] != kFormatVersionValue)
359 {
360 OOLog(kOOLogDataCacheRebuild, @"Data cache format (%@) is not supported format (%u), rebuilding cache.", formatVersion, kFormatVersionValue);
361 accept = NO;
362 }
363
364 if (accept)
365 {
366 endianTag = [cache objectForKey:kCacheKeyEndianTag];
367 if (![endianTag isKindOfClass:[NSData class]] || [endianTag length] != sizeof endianTagValue)
368 {
369 OOLog(kOOLogDataCacheRebuild, @"%@", @"Data cache endian tag is invalid, rebuilding cache.");
370 accept = NO;
371 }
372 else
373 {
374 endianTagValue = *(const uint64_t *)[endianTag bytes];
375 if (endianTagValue != kEndianTagValue)
376 {
377 OOLog(kOOLogDataCacheRebuild, @"%@", @"Data cache endianness is inappropriate for this system, rebuilding cache.");
378 accept = NO;
379 }
380 }
381 }
382
383 if (accept)
384 {
385 // We have a cache, and it's the right format.
386 [self buildCachesFromDictionary:[cache objectForKey:kCacheKeyCaches]];
387 }
388
390 }
391 else
392 {
393 // No cache
394 OOLog(kOOLogDataCacheNotFound, @"%@", @"No data cache found, starting from scratch.");
395 }
396
397 // If loading failed, or there was a version or endianness conflict
398 if (_caches == nil) _caches = [[NSMutableDictionary alloc] init];
399 [self markClean];
400}
401
@ kFormatVersionValue
@ kEndianTagValue
static NSString *const kOOLogDataCacheRebuild
static NSString *const kOOLogDataCacheNotFound
static NSString *const kOOLogDataCacheFound
#define OOLogOutdentIf(class)
Definition OOLogging.h:102
#define OOLogIndentIf(class)
Definition OOLogging.h:101

◆ loadDict

- (NSDictionary *) loadDict

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

494{
495 NSString *path = nil;
496 NSData *data = nil;
497 NSString *errorString = nil;
498 id contents = nil;
499
500 path = [self cachePathCreatingIfNecessary:NO];
501 if (path == nil) return nil;
502
503 @try
504 {
505 data = [NSData dataWithContentsOfFile:path];
506 if (data == nil) return nil;
507
508 contents = [NSPropertyListSerialization propertyListFromData:data
509 mutabilityOption:NSPropertyListImmutable
510 format:NULL
511 errorDescription:&errorString];
512 }
513 @catch (NSException *exception)
514 {
515 errorString = [exception reason];
516 contents = nil;
517 }
518
519 if (errorString != nil)
520 {
521 OOLog(@"dataCache.badData", @"Could not read data cache: %@", errorString);
522#if OOLITE_RELEASE_PLIST_ERROR_STRINGS
523 [errorString release];
524#endif
525 return nil;
526 }
527 if (![contents isKindOfClass:[NSDictionary class]]) return nil;
528
529 return contents;
530}
531

◆ markClean

- (void) markClean

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

488{
489 _dirty = NO;
490}
491

◆ write

- (void) write

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

404{
405 NSMutableDictionary *newCache = nil;
406 NSString *ooliteVersion = nil;
407 NSData *endianTag = nil;
408 NSNumber *formatVersion = nil;
409 NSDictionary *pListRep = nil;
410 uint64_t endianTagValue = kEndianTagValue;
411
412 if (_caches == nil) return;
413 if (_scheduledWrite != nil) return;
414
415#if PROFILE_WRITES
417#endif
418
419#if WRITE_ASYNC
420 OOLog(@"dataCache.willWrite", @"%@", @"Scheduling data cache write.");
421#else
422 OOLog(@"dataCache.willWrite", @"%@", @"About to write cache.");
423#endif
424
425 ooliteVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
426 endianTag = [NSData dataWithBytes:&endianTagValue length:sizeof endianTagValue];
427 formatVersion = [NSNumber numberWithUnsignedInt:kFormatVersionValue];
428
429 pListRep = [self dictionaryOfCaches];
430 if (ooliteVersion == nil || endianTag == nil || formatVersion == nil || pListRep == nil)
431 {
432 OOLog(@"dataCache.cantWrite", @"%@", @"Failed to write data cache -- prerequisites not fulfilled. This is an internal error, please report it.");
433 return;
434 }
435
436 newCache = [NSMutableDictionary dictionaryWithCapacity:4];
437 [newCache setObject:ooliteVersion forKey:kCacheKeyVersion];
438 [newCache setObject:formatVersion forKey:kCacheKeyFormatVersion];
439 [newCache setObject:endianTag forKey:kCacheKeyEndianTag];
440 [newCache setObject:pListRep forKey:kCacheKeyCaches];
441
442#if PROFILE_WRITES && !WRITE_ASYNC
443 OOTimeDelta prepareT = [stopwatch reset];
444#endif
445
446#if WRITE_ASYNC
447 NSDictionary *cacheData = newCache;
448 _scheduledWrite = [[OOAsyncCacheWriter alloc] initWithCacheContents:cacheData];
449
450#if PROFILE_WRITES
451 OOTimeDelta endT = [stopwatch reset];
452 OOLog(@"dataCache.profile", @"Time to prepare cache data: %g seconds.", endT);
453#endif
454
455 [[OOAsyncWorkManager sharedAsyncWorkManager] addTask:_scheduledWrite priority:kOOAsyncPriorityLow];
456#else
457#if PROFILE_WRITES
458 OOLog(@"dataCache.profile", @"Time to prepare cache data: %g seconds.", prepareT);
459#endif
460
461 if ([self writeDict:newCache])
462 {
463 [self markClean];
464 OOLog(kOOLogDataCacheWriteSuccess, @"%@", @"Wrote data cache.");
465 }
466 else
467 {
468 OOLog(kOOLogDataCacheWriteFailed, @"%@", @"Failed to write data cache.");
469 }
470#endif
471}
472
static NSString *const kOOLogDataCacheWriteSuccess
static NSString *const kOOLogDataCacheWriteFailed
double OOTimeDelta
Definition OOTypes.h:224
BOOL addTask:priority:(id< OOAsyncWorkTask > task,[priority] OOAsyncWorkPriority priority)
OOAsyncWorkManager * sharedAsyncWorkManager()

◆ writeDict:

- (BOOL) writeDict: (NSDictionary *) inDict

Extends class OOCacheManager.

Definition at line 114 of file OOCacheManager.m.

534{
535 NSString *path = nil;
536 NSData *plist = nil;
537 NSString *errorDesc = nil;
538
539 path = [self cachePathCreatingIfNecessary:YES];
540 if (path == nil) return NO;
541
542#if PROFILE_WRITES
544#endif
545
546 plist = [NSPropertyListSerialization dataFromPropertyList:inDict format:CACHE_PLIST_FORMAT errorDescription:&errorDesc];
547 if (plist == nil)
548 {
549#if OOLITE_RELEASE_PLIST_ERROR_STRINGS
550 [errorDesc autorelease];
551#endif
552 OOLog(kOOLogDataCacheSerializationError, @"Could not convert data cache to property list data: %@", errorDesc);
553 return NO;
554 }
555
556#if PROFILE_WRITES
557 OOTimeDelta serializeT = [stopwatch reset];
558#endif
559
560 BOOL result = [plist writeToFile:path atomically:NO];
561
562#if PROFILE_WRITES
563 OOTimeDelta writeT = [stopwatch reset];
564
565 OOLog(@"dataCache.profile", @"Time to serialize cache: %g seconds. Time to write data: %g seconds.", serializeT, writeT);
566#endif
567
568#if WRITE_ASYNC
569 DESTROY(_scheduledWrite);
570#endif
571 return result;
572}
573
static NSString *const kOOLogDataCacheSerializationError
#define DESTROY(x)
Definition OOCocoa.h:77

The documentation for this category was generated from the following file: