Oolite
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 75 of file OOCacheManager.m.

Method Documentation

◆ buildCachesFromDictionary:

- (void) buildCachesFromDictionary: (NSDictionary*) inDict

Extends class OOCacheManager.

Definition at line 513 of file OOCacheManager.m.

513 :(NSDictionary*)inDict
514{
515 id key = nil;
516 id value = nil;
517 NSMutableDictionary* cache = nil;
518
519 if (inDict == nil)
520 return;
521
522 [_caches release];
523 _caches = [[NSMutableDictionary alloc] initWithCapacity:[inDict count]];
524
525 foreachkey(key, inDict)
526 {
527 value = [inDict oo_dictionaryForKey:key];
528 if (value != nil) {
529 cache = [NSMutableDictionary dictionaryWithDictionary:value];
530 if (cache != nil) {
531 [_caches setObject:cache forKey:key];
532 }
533 }
534 }
535}
#define foreachkey(VAR, DICT)
return nil

References foreachkey, and nil.

◆ cachePathCreatingIfNecessary:

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

Extends class OOCacheManager.

Definition at line 567 of file OOCacheManager.m.

567 :(BOOL)create
568{
569 NSString* cachePath = [self cacheDirectoryPathCreatingIfNecessary:create];
570 return [cachePath stringByAppendingPathComponent:@"Data Cache.plist"];
571}

References OOCacheManager::cacheDirectoryPathCreatingIfNecessary:.

Referenced by loadDict, and writeDict:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear

- (void) clear

Extends class OOCacheManager.

Definition at line 417 of file OOCacheManager.m.

418{
419 [_caches release];
420 _caches = nil;
421}

References clear, and nil.

Referenced by clear.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dictionaryOfCaches

- (NSDictionary *) dictionaryOfCaches

Extends class OOCacheManager.

Definition at line 537 of file OOCacheManager.m.

538{
539 return [OODeepCopy(_caches) autorelease];
540}

References dictionaryOfCaches.

Referenced by dictionaryOfCaches.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ directoryExists:create:

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

Extends class OOCacheManager.

Definition at line 542 of file OOCacheManager.m.

542 :(NSString*)inPath create:(BOOL)inCreate
543{
544 BOOL exists, directory;
545 NSFileManager* fmgr = [NSFileManager defaultManager];
546
547 exists = [fmgr fileExistsAtPath:inPath isDirectory:&directory];
548
549 if (exists && !directory) {
550 OOLog(kOOLogDataCacheBuildPathError, @"Expected %@ to be a folder, but it is a file.", inPath);
551 return NO;
552 }
553 if (!exists) {
554 if (!inCreate)
555 return NO;
556 if (![fmgr oo_createDirectoryAtPath:inPath attributes:nil]) {
557 OOLog(kOOLogDataCacheBuildPathError, @"Could not create folder %@.", inPath);
558 return NO;
559 }
560 }
561
562 return YES;
563}
static NSString *const kOOLogDataCacheBuildPathError
#define OOLog(class, format,...)
Definition OOLogging.h:84

References kOOLogDataCacheBuildPathError, nil, and OOLog.

◆ dirty

- (BOOL) dirty

Extends class OOCacheManager.

Definition at line 423 of file OOCacheManager.m.

424{
425 return _dirty;
426}

References dirty.

Referenced by dirty.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadCache

- (void) loadCache

Extends class OOCacheManager.

Definition at line 285 of file OOCacheManager.m.

286{
287 NSDictionary* cache = nil;
288 NSString* cacheVersion = nil;
289 NSString* ooliteVersion = nil;
290 NSData* endianTag = nil;
291 NSNumber* formatVersion = nil;
292 BOOL accept = YES;
293 uint64_t endianTagValue = 0;
294
295 ooliteVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
296
297 [self clear];
298
299 cache = [self loadDict];
300 if (cache != nil) {
301 // We have a cache
302 OOLog(kOOLogDataCacheFound, @"%@", @"Found data cache.");
304
305 cacheVersion = [cache objectForKey:kCacheKeyVersion];
306 if (![cacheVersion isEqual:ooliteVersion]) {
307 OOLog(kOOLogDataCacheRebuild, @"Data cache version (%@) does not match Oolite version (%@), rebuilding cache.", cacheVersion, ooliteVersion);
308 accept = NO;
309 }
310
311 formatVersion = [cache objectForKey:kCacheKeyFormatVersion];
312 if (accept && [formatVersion unsignedIntValue] != kFormatVersionValue) {
313 OOLog(kOOLogDataCacheRebuild, @"Data cache format (%@) is not supported format (%zu), rebuilding cache.", formatVersion, kFormatVersionValue);
314 accept = NO;
315 }
316
317 if (accept) {
318 endianTag = [cache objectForKey:kCacheKeyEndianTag];
319 if (![endianTag isKindOfClass:[NSData class]] || [endianTag length] != sizeof endianTagValue) {
320 OOLog(kOOLogDataCacheRebuild, @"%@", @"Data cache endian tag is invalid, rebuilding cache.");
321 accept = NO;
322 } else {
323 endianTagValue = *(const uint64_t*)[endianTag bytes];
324 if (endianTagValue != kEndianTagValue) {
325 OOLog(kOOLogDataCacheRebuild, @"%@", @"Data cache endianness is inappropriate for this system, rebuilding cache.");
326 accept = NO;
327 }
328 }
329 }
330
331 if (accept) {
332 // We have a cache, and it's the right format.
333 [self buildCachesFromDictionary:[cache objectForKey:kCacheKeyCaches]];
334 }
335
337 } else {
338 // No cache
339 OOLog(kOOLogDataCacheNotFound, @"%@", @"No data cache found, starting from scratch.");
340 }
341
342 // If loading failed, or there was a version or endianness conflict
343 if (_caches == nil)
344 _caches = [[NSMutableDictionary alloc] init];
345 [self markClean];
346}
@ kFormatVersionValue
@ kEndianTagValue
static NSString *const kOOLogDataCacheRebuild
static NSString *const kOOLogDataCacheNotFound
static NSString *const kOOLogDataCacheFound
#define OOLogOutdentIf(class)
Definition OOLogging.h:112
#define OOLogIndentIf(class)
Definition OOLogging.h:107

References OOCacheManager::buildCachesFromDictionary:, OOCacheManager::clear, kEndianTagValue, kFormatVersionValue, kOOLogDataCacheFound, kOOLogDataCacheNotFound, kOOLogDataCacheRebuild, loadCache, OOCacheManager::loadDict, OOCacheManager::markClean, nil, OOLog, OOLogIndentIf, and OOLogOutdentIf.

Referenced by loadCache.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadDict

- (NSDictionary *) loadDict

Extends class OOCacheManager.

Definition at line 433 of file OOCacheManager.m.

434{
435 NSString* path = nil;
436 NSData* data = nil;
437 NSString* errorString = nil;
438 id contents = nil;
439
440 path = [self cachePathCreatingIfNecessary:NO];
441 if (path == nil)
442 return nil;
443
444 @try {
445 data = [NSData dataWithContentsOfFile:path];
446 if (data == nil)
447 return nil;
448
449 contents = [NSPropertyListSerialization propertyListFromData:data
450 mutabilityOption:NSPropertyListImmutable
451 format:NULL
452 errorDescription:&errorString];
453 }
454 @catch (NSException* exception) {
455 errorString = [exception reason];
456 contents = nil;
457 }
458
459 if (errorString != nil) {
460 OOLog(@"dataCache.badData", @"Could not read data cache: %@", errorString);
461#if OOLITE_RELEASE_PLIST_ERROR_STRINGS
462 [errorString release];
463#endif
464 return nil;
465 }
466 if (![contents isKindOfClass:[NSDictionary class]])
467 return nil;
468
469 return contents;
470}

References OOCacheManager::cachePathCreatingIfNecessary:, loadDict, nil, and OOLog.

Referenced by loadDict.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ markClean

- (void) markClean

Extends class OOCacheManager.

Definition at line 428 of file OOCacheManager.m.

429{
430 _dirty = NO;
431}

References markClean.

Referenced by markClean.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write

- (void) write

Extends class OOCacheManager.

Definition at line 348 of file OOCacheManager.m.

349{
350 NSMutableDictionary* newCache = nil;
351 NSString* ooliteVersion = nil;
352 NSData* endianTag = nil;
353 NSNumber* formatVersion = nil;
354 NSDictionary* pListRep = nil;
355 uint64_t endianTagValue = kEndianTagValue;
356
357 if (_caches == nil)
358 return;
359 if (_scheduledWrite != nil)
360 return;
361
362#if PROFILE_WRITES
363 OOProfilingStopwatch* stopwatch = [OOProfilingStopwatch stopwatch];
364#endif
365
366#if WRITE_ASYNC
367 OOLog(@"dataCache.willWrite", @"%@", @"Scheduling data cache write.");
368#else
369 OOLog(@"dataCache.willWrite", @"%@", @"About to write cache.");
370#endif
371
372 ooliteVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
373 endianTag = [NSData dataWithBytes:&endianTagValue length:sizeof endianTagValue];
374 formatVersion = [NSNumber numberWithUnsignedInt:kFormatVersionValue];
375
376 pListRep = [self dictionaryOfCaches];
377 if (ooliteVersion == nil || endianTag == nil || formatVersion == nil || pListRep == nil) {
378 OOLog(@"dataCache.cantWrite", @"%@", @"Failed to write data cache -- prerequisites not fulfilled. This is an internal error, please report it.");
379 return;
380 }
381
382 newCache = [NSMutableDictionary dictionaryWithCapacity:4];
383 [newCache setObject:ooliteVersion forKey:kCacheKeyVersion];
384 [newCache setObject:formatVersion forKey:kCacheKeyFormatVersion];
385 [newCache setObject:endianTag forKey:kCacheKeyEndianTag];
386 [newCache setObject:pListRep forKey:kCacheKeyCaches];
387
388#if PROFILE_WRITES && !WRITE_ASYNC
389 OOTimeDelta prepareT = [stopwatch reset];
390#endif
391
392#if WRITE_ASYNC
393 NSDictionary* cacheData = newCache;
394 _scheduledWrite = [[OOAsyncCacheWriter alloc] initWithCacheContents:cacheData];
395
396#if PROFILE_WRITES
397 OOTimeDelta endT = [stopwatch reset];
398 OOLog(@"dataCache.profile", @"Time to prepare cache data: %g seconds.", endT);
399#endif
400
402 priority:kOOAsyncPriorityLow];
403#else
404#if PROFILE_WRITES
405 OOLog(@"dataCache.profile", @"Time to prepare cache data: %g seconds.", prepareT);
406#endif
407
408 if ([self writeDict:newCache]) {
409 [self markClean];
410 OOLog(kOOLogDataCacheWriteSuccess, @"%@", @"Wrote data cache.");
411 } else {
412 OOLog(kOOLogDataCacheWriteFailed, @"%@", @"Failed to write data cache.");
413 }
414#endif
415}
static NSString *const kOOLogDataCacheWriteSuccess
static NSString *const kOOLogDataCacheWriteFailed
double OOTimeDelta
Definition OOTypes.h:193
BOOL addTask:priority:(id< OOAsyncWorkTask > task,[priority] OOAsyncWorkPriority priority)
OOAsyncWorkManager * sharedAsyncWorkManager()

References OOAsyncWorkManager::addTask:priority:, OOCacheManager::dictionaryOfCaches, kEndianTagValue, kOOLogDataCacheWriteFailed, kOOLogDataCacheWriteSuccess, OOCacheManager::markClean, nil, OOLog, OOProfilingStopwatch::reset, OOAsyncWorkManager::sharedAsyncWorkManager, OOProfilingStopwatch::stopwatch, and write.

Referenced by write.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeDict:

- (BOOL) writeDict: (NSDictionary*) inDict

Extends class OOCacheManager.

Definition at line 472 of file OOCacheManager.m.

472 :(NSDictionary*)inDict
473{
474 NSString* path = nil;
475 NSData* plist = nil;
476 NSString* errorDesc = nil;
477
478 path = [self cachePathCreatingIfNecessary:YES];
479 if (path == nil)
480 return NO;
481
482#if PROFILE_WRITES
483 OOProfilingStopwatch* stopwatch = [OOProfilingStopwatch stopwatch];
484#endif
485
486 plist = [NSPropertyListSerialization dataFromPropertyList:inDict format:CACHE_PLIST_FORMAT errorDescription:&errorDesc];
487 if (plist == nil) {
488#if OOLITE_RELEASE_PLIST_ERROR_STRINGS
489 [errorDesc autorelease];
490#endif
491 OOLog(kOOLogDataCacheSerializationError, @"Could not convert data cache to property list data: %@", errorDesc);
492 return NO;
493 }
494
495#if PROFILE_WRITES
496 OOTimeDelta serializeT = [stopwatch reset];
497#endif
498
499 BOOL result = [plist writeToFile:path atomically:NO];
500
501#if PROFILE_WRITES
502 OOTimeDelta writeT = [stopwatch reset];
503
504 OOLog(@"dataCache.profile", @"Time to serialize cache: %g seconds. Time to write data: %g seconds.", serializeT, writeT);
505#endif
506
507#if WRITE_ASYNC
508 DESTROY(_scheduledWrite);
509#endif
510 return result;
511}
static NSString *const kOOLogDataCacheSerializationError
#define DESTROY(x)

References OOCacheManager::cachePathCreatingIfNecessary:, DESTROY, kOOLogDataCacheSerializationError, nil, OOLog, OOProfilingStopwatch::reset, and OOProfilingStopwatch::stopwatch.

Here is the call graph for this function:

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