112#ifndef OOCACHE_PERFORM_INTEGRITY_CHECKS
113#define OOCACHE_PERFORM_INTEGRITY_CHECKS 0
117@protocol OOCacheComparable <NSObject, NSCopying>
118- (NSComparisonResult)compare:(
id<OOCacheComparable>)other;
138 id<OOCacheComparable>
key;
166#if OOCACHE_PERFORM_INTEGRITY_CHECKS
167static NSString*
const kOOLogCacheIntegrityCheck =
@"dataCache.integrityCheck";
168static void CacheCheckIntegrity(
OOCacheImpl* cache, NSString* context);
170#define CHECK_INTEGRITY(context) CacheCheckIntegrity(cache, (context))
172#define CHECK_INTEGRITY(context) \
177@interface OOCache (Private)
179- (void)loadFromArray:(NSArray*)inArray;
195 return [
NSString stringWithFormat:@"<%@ %p>{\"%@\", %u elements, prune threshold=%u, auto-prune=%s dirty=%s}", [
self class],
self, [
self name], CacheGetCount(cache), pruneThreshold, autoPrune ? "yes" : "no", dirty ? "yes" : "no"];
203- (id)initWithPList:(
id)pList
242- (id)objectForKey:(
id)key
253 return [[
result retain] autorelease];
256- (void)setObject:(
id)value forKey:(
id)key
269- (void)removeObjectForKey:(
id)key
279- (void)setPruneThreshold:(
unsigned)threshold
294- (void)setAutoPrune:(BOOL)flag
296 BOOL
prune = (flag != NO);
311 unsigned desiredCount;
323 pruneCount =
count - desiredCount;
325 NSString* logKey = [
NSString stringWithFormat:@"dataCache.prune.%@", CacheGetName(cache)];
350- (void)setName:(NSString*)name
362@implementation OOCache (Private)
364- (void)loadFromArray:(NSArray*)array
366 NSDictionary* entry =
nil;
373 foreach (entry, array) {
374 if ([entry isKindOfClass:[NSDictionary
class]]) {
375 key = [
entry objectForKey:kSerializedEntryKeyKey];
376 value = [
entry objectForKey:kSerializedEntryKeyValue];
377 if ([key isKindOfClass:[NSString
class]] && value !=
nil) {
393#if OOCACHE_PERFORM_INTEGRITY_CHECKS
394static NSString* CacheNodeGetDescription(
OOCacheNode* node);
400#if OOCACHE_PERFORM_INTEGRITY_CHECKS
408#if OOCACHE_PERFORM_INTEGRITY_CHECKS
409static void AgeListCheckIntegrity(
OOCacheImpl* cache, NSString* context);
425 [cache->name autorelease];
433 if (cache == NULL || key ==
nil || value ==
nil)
460 cache->
root = newRoot;
474 if (cache == NULL || cache->
oldest == NULL)
486 if (cache == NULL || key == NULL)
500 NSMutableArray* result =
nil;
502 if (cache == NULL || cache->
count == 0)
505 result = [NSMutableArray arrayWithCapacity:cache->count];
507 for (node = cache->
youngest; node != NULL; node = node->
older) {
508 [result addObject:node->value];
516 NSMutableArray* result =
nil;
518 if (cache == NULL || cache->
count == 0)
521 result = [NSMutableArray arrayWithCapacity:cache->count];
523 for (node = cache->
oldest; node != NULL; node = node->
younger) {
524 [result addObject:[NSDictionary dictionaryWithObjectsAndKeys:node->key, kSerializedEntryKeyKey, node->value, kSerializedEntryKeyValue, nil]];
536 [cache->name autorelease];
537 cache->
name = [name copy];
545#if OOCACHE_PERFORM_INTEGRITY_CHECKS
547static void CacheCheckIntegrity(
OOCacheImpl* cache, NSString* context)
551 cache->
root = TreeCheckIntegrity(cache, cache->
root, NULL, context);
553 trueCount = TreeCountNodes(cache->
root);
555 cache->
count = trueCount;
556 else if (cache->
count != trueCount) {
557 OOLog(kOOLogCacheIntegrityCheck,
@"Integrity check (%@ for \"%@\
"): count is %u, but should be %u.", context, cache->
name, cache->
count, trueCount);
558 cache->
count = trueCount;
561 AgeListCheckIntegrity(cache, context);
573 if (key ==
nil || value ==
nil)
576 result = calloc(
sizeof *result, 1);
577 if (result != NULL) {
578 result->
key = [key copy];
579 result->
value = [value retain];
624 id tmp = node->
value;
625 node->
value = [value retain];
629#if OOCACHE_PERFORM_INTEGRITY_CHECKS
631static NSString* CacheNodeGetDescription(
OOCacheNode* node)
636 return [NSString stringWithFormat:@"%p[\"%@\"]", node, node->key];
651 NSComparisonResult order;
652 OOCacheNode N = { .leftChild = NULL, .rightChild = NULL };
653 OOCacheNode *node = NULL, *temp = NULL, *l = &N, *r = &N;
656 if (root == NULL || *root == NULL || key ==
nil)
664 OOLog(
@"node.error",
@"%@",
@"node is NULL");
665 }
else if (node->
key == NULL) {
666 OOLog(
@"node.error",
@"%@",
@"node->key is NULL");
669 order = [key compare:node->key];
670 if (order == NSOrderedAscending) {
674 if ([key compare:node->
leftChild->
key] == NSOrderedAscending) {
687 }
else if (order == NSOrderedDescending) {
691 if ([key compare:node->
rightChild->
key] == NSOrderedDescending) {
701 l->rightChild = node;
718 return exact ? node : NULL;
725 NSComparisonResult order;
727 if (cache == NULL || key ==
nil || value ==
nil)
730 if (cache->
root == NULL) {
740 closest = cache->
root;
745 order = [key compare:closest->key];
747 if (order == NSOrderedAscending) {
754 }
else if (order == NSOrderedDescending) {
763 OOLog(
@"dataCache.inconsistency",
@"%s() internal inconsistency for cache \"%@\
", insertion failed.", __PRETTY_FUNCTION__, cache->
name);
773#if OOCACHE_PERFORM_INTEGRITY_CHECKS
778 return 1 + TreeCountNodes(node->leftChild) + TreeCountNodes(node->rightChild);
784 NSComparisonResult order;
790 if (OK && node->
key ==
nil) {
791 OOLog(kOOLogCacheIntegrityCheck,
@"Integrity check (%@ for \"%@\
"): node \"%@\" has nil key; deleting subtree.", context, cache->
name, CacheNodeGetDescription(node));
796 OOLog(kOOLogCacheIntegrityCheck,
@"Integrity check (%@ for \"%@\
"): node \"%@\" has nil value, deleting.", context, cache->
name, CacheNodeGetDescription(node));
800 order = [node->key compare:node->leftChild->key];
801 if (order != NSOrderedDescending) {
802 OOLog(kOOLogCacheIntegrityCheck,
@"Integrity check (%@ for \"%@\
"): node %@'s left child %@ is not correctly ordered. Deleting subtree.", context, cache->
name, CacheNodeGetDescription(node), CacheNodeGetDescription(node->
leftChild));
811 order = [node->key compare:node->rightChild->key];
812 if (order != NSOrderedAscending) {
813 OOLog(kOOLogCacheIntegrityCheck,
@"Integrity check (%@ for \"%@\
"): node \"%@\"'s right child \"%@\" is not correctly ordered. Deleting subtree.", context, cache->
name, CacheNodeGetDescription(node), CacheNodeGetDescription(node->
rightChild));
837 if (cache == NULL || node == NULL)
845 if (cache->
oldest == NULL)
863 if (cache->
oldest == node)
870 younger->
older = older;
875#if OOCACHE_PERFORM_INTEGRITY_CHECKS
877static void AgeListCheckIntegrity(
OOCacheImpl* cache, NSString* context)
880 unsigned seenCount = 0;
882 if (cache == NULL || context == NULL)
894 if (next->younger != node) {
895 OOLog(kOOLogCacheIntegrityCheck,
@"Integrity check (%@ for \"%@\
"): node \"%@\" has invalid older link (should be \"%@\", is \"%@\"); repairing.", context, cache->
name, CacheNodeGetDescription(next), CacheNodeGetDescription(node), CacheNodeGetDescription(next->older));
901 if (seenCount != cache->
count) {
903 OOLog(kOOLogCacheIntegrityCheck,
@"Integrity check (%@ for \"%@\
"): expected %u nodes, found %u. Cannot repair; clearing cache.", context, cache->
name, cache->
count, seenCount);
915 if (node->
key != NULL) {
916 OOLog(kOOLogCacheIntegrityCheck,
@"Key is: %@", node->
key);
918 OOLog(kOOLogCacheIntegrityCheck,
@"Key is: NULL");
921 if (node->
value != NULL) {
922 OOLog(kOOLogCacheIntegrityCheck,
@"Value is: %@", node->
value);
924 OOLog(kOOLogCacheIntegrityCheck,
@"Value is: NULL");
940 if (node != cache->
oldest) {
941 OOLog(kOOLogCacheIntegrityCheck,
@"Integrity check (%@ for \"%@\
"): oldest pointer in cache is wrong (should be \"%@\", is \"%@\"); repairing.", context, cache->
name, CacheNodeGetDescription(node), CacheNodeGetDescription(cache->
oldest));
957@implementation OOCache (DebugGraphViz)
959- (void)appendNodesFromSubTree:(
OOCacheNode*)subTree toString:(NSMutableString*)ioString
961 [ioString appendFormat:@"\tn%p [label=\"<f0> | <f1> %@ | <f2>\"];\n", subTree, EscapedGraphVizString([subTree->key description])];
964 [
self appendNodesFromSubTree:subTree->leftChild toString:ioString];
965 [ioString appendFormat:@"\tn%p:f0 -> n%p:f1;\n", subTree, subTree->leftChild];
968 [
self appendNodesFromSubTree:subTree->rightChild toString:ioString];
969 [ioString appendFormat:@"\tn%p:f2 -> n%p:f1;\n", subTree, subTree->rightChild];
973- (NSString*)generateGraphVizBodyWithRootNamed:(NSString*)rootName
975 NSMutableString* result =
nil;
977 result = [NSMutableString string];
980 [result appendFormat:@"\t%@ [label=\"Cache \\\"%@\\\"\" shape=box];\n"
981 "\tnode [shape=record];\n\t\n",
982 rootName, EscapedGraphVizString([
self name])];
988 [
self appendNodesFromSubTree:cache->root toString:result];
991 [result appendString:@"\tedge [color=black constraint=true];\n"];
992 [result appendFormat:@"\t%@ -> n%p:f1;\n", rootName, cache->root];
997 [result appendString:@"\t\n\t// Age-sorted list in blue\n\tedge [color=blue constraint=false];\n"];
999 while (node->
younger != NULL) {
1000 [result appendFormat:@"\tn%p:f2 -> n%p:f0;\n", node, node->younger];
1008- (NSString*)generateGraphViz
1010 NSMutableString* result =
nil;
1012 result = [NSMutableString string];
1015 [result appendFormat:
1016 @"// OOCache dump\n\n"
1019 "\tgraph [charset=\"UTF-8\", label=\"OOCache \"%@\" debug dump\", labelloc=t, labeljust=l];\n\t\n",
1022 [result appendString:[
self generateGraphVizBodyWithRootNamed:@"cache"]];
1024 [result appendString:@"}\n"];
1029- (void)writeGraphVizToURL:(NSURL*)url
1031 NSString* graphViz =
nil;
1034 graphViz = [
self generateGraphViz];
1035 data = [graphViz dataUsingEncoding:NSUTF8StringEncoding];
1038 [data writeToURL:url atomically:YES];
1042- (void)writeGraphVizToPath:(NSString*)path
1044 [
self writeGraphVizToURL:[NSURL fileURLWithPath:path]];
@ kOOCacheDefaultPruneThreshold
@ kOOCacheMinimumPruneThreshold
static void AgeListMakeYoungest(OOCacheImpl *cache, OOCacheNode *node)
static NSString *const kSerializedEntryKeyKey
static NSString * CacheGetName(OOCacheImpl *cache)
static void CacheNodeSetValue(OOCacheNode *node, id value)
static OOCacheNode * CacheNodeAllocate(id< OOCacheComparable > key, id value)
static void CacheFree(OOCacheImpl *cache)
static NSArray * CacheArrayOfContentsByAge(OOCacheImpl *cache)
static unsigned CacheGetCount(OOCacheImpl *cache)
#define CHECK_INTEGRITY(context)
struct OOCacheImpl OOCacheImpl
static OOCacheNode * TreeInsert(OOCacheImpl *cache, id< OOCacheComparable > key, id value)
static NSArray * CacheArrayOfNodesByAge(OOCacheImpl *cache)
static BOOL CacheRemoveOldest(OOCacheImpl *cache, NSString *logKey)
static OOCacheImpl * CacheAllocate(void)
struct OOCacheNode OOCacheNode
static id CacheNodeGetValue(OOCacheNode *node)
static id CacheRetrieve(OOCacheImpl *cache, id key)
static void AgeListRemove(OOCacheImpl *cache, OOCacheNode *node)
static void CacheNodeFree(OOCacheImpl *cache, OOCacheNode *node)
static BOOL CacheRemove(OOCacheImpl *cache, id key)
static NSString *const kSerializedEntryKeyValue
static void CacheSetName(OOCacheImpl *cache, NSString *name)
static BOOL CacheInsert(OOCacheImpl *cache, id key, id value)
static OOCacheNode * TreeSplay(OOCacheNode **root, id< OOCacheComparable > key)
#define OOLogOutdentIf(class)
#define OOLog(class, format,...)
#define OOLogIndentIf(class)
struct OOCacheImpl * cache
id initWithPList:(id pList)
void setObject:forKey:(id value,[forKey] id key)
void loadFromArray:(NSArray *inArray)
id< OOCacheComparable > key