/core/externals/update-engine/Common/KSStatsCollection.m

http://macfuse.googlecode.com/ · Objective C · 161 lines · 116 code · 27 blank · 18 comment · 19 complexity · 3ff37f45044a231784ffbffbd9b46c49 MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "KSStatsCollection.h"
  15. @implementation KSStatsCollection
  16. + (id)statsCollectionWithPath:(NSString *)path {
  17. return [[[self alloc] initWithPath:path] autorelease];
  18. }
  19. + (id)statsCollectionWithPath:(NSString *)path
  20. autoSynchronize:(BOOL)autoSync {
  21. return [[[self alloc] initWithPath:path
  22. autoSynchronize:autoSync] autorelease];
  23. }
  24. - (id)init {
  25. return [self initWithPath:nil];
  26. }
  27. - (id)initWithPath:(NSString *)path {
  28. return [self initWithPath:path autoSynchronize:YES];
  29. }
  30. - (id)initWithPath:(NSString *)path
  31. autoSynchronize:(BOOL)autoSync {
  32. if ((self = [super init])) {
  33. if (path == nil) {
  34. [self release];
  35. return nil;
  36. }
  37. path_ = [path copy];
  38. autoSynchronize_ = autoSync;
  39. // Try to load stats from the specified path. If it doesn't work (maybe the
  40. // file doesn't exist yet), then create an empty dictionary.
  41. stats_ = [[NSMutableDictionary alloc] initWithContentsOfFile:path_];
  42. if (stats_ == nil) stats_ = [[NSMutableDictionary alloc] init];
  43. // If auto synchronizing is enabled, then do a sync right now to make sure
  44. // we can. If we fail to sync, release self and return nil to indicate an
  45. // error.
  46. if (autoSynchronize_) {
  47. BOOL synced = [self synchronize];
  48. if (!synced) {
  49. [self release];
  50. return nil;
  51. }
  52. }
  53. }
  54. return self;
  55. }
  56. - (void)dealloc {
  57. [path_ release];
  58. [stats_ release];
  59. [super dealloc];
  60. }
  61. - (NSString *)path {
  62. return [[path_ copy] autorelease];
  63. }
  64. - (NSDictionary *)statsDictionary {
  65. return [[stats_ copy] autorelease];
  66. }
  67. - (unsigned int)count {
  68. unsigned int count = 0;
  69. @synchronized (stats_) {
  70. count = [stats_ count];
  71. }
  72. return count;
  73. }
  74. - (NSString *)description {
  75. return [NSString stringWithFormat:@"<%@:%p path=\"%@\", count=%d, stats=%@>",
  76. [self class], self, path_, [self count], [self statsDictionary]];
  77. }
  78. - (void)removeAllStats {
  79. @synchronized (stats_) {
  80. [stats_ removeAllObjects];
  81. if (autoSynchronize_) [self synchronize];
  82. }
  83. }
  84. - (void)setNumber:(NSNumber *)num forStat:(NSString *)stat {
  85. if (num != nil && stat != nil) {
  86. @synchronized (stats_) {
  87. [stats_ setObject:num forKey:stat];
  88. if (autoSynchronize_) [self synchronize];
  89. }
  90. }
  91. }
  92. - (NSNumber *)numberForStat:(NSString *)stat {
  93. NSNumber *num = nil;
  94. if (stat != nil) {
  95. @synchronized (stats_) {
  96. num = [stats_ objectForKey:stat];
  97. }
  98. }
  99. return num;
  100. }
  101. - (void)incrementStat:(NSString *)stat {
  102. return [self incrementStat:stat by:1];
  103. }
  104. - (void)incrementStat:(NSString *)stat by:(int)n {
  105. if (stat == nil) return;
  106. @synchronized (stats_) {
  107. NSNumber *num = [stats_ objectForKey:stat];
  108. long long val = 0;
  109. if (num) val = [num longLongValue];
  110. NSNumber *inc = [NSNumber numberWithLongLong:(val + n)];
  111. [stats_ setObject:inc forKey:stat];
  112. if (autoSynchronize_) [self synchronize];
  113. }
  114. }
  115. - (void)decrementStat:(NSString *)stat {
  116. return [self incrementStat:stat by:-1];
  117. }
  118. - (void)decrementStat:(NSString *)stat by:(int)n {
  119. return [self incrementStat:stat by:-n];
  120. }
  121. - (BOOL)synchronize {
  122. BOOL ok = NO;
  123. @synchronized (stats_) {
  124. ok = [stats_ writeToFile:path_ atomically:YES];
  125. }
  126. return ok;
  127. }
  128. - (BOOL)autoSynchronize {
  129. return autoSynchronize_;
  130. }
  131. - (void)setAutoSynchronize:(BOOL)autoSync {
  132. autoSynchronize_ = autoSync;
  133. }
  134. @end