PageRenderTime 59ms CodeModel.GetById 27ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 15#import "KSStatsCollection.h"
 16
 17
 18@implementation KSStatsCollection
 19
 20+ (id)statsCollectionWithPath:(NSString *)path {
 21  return [[[self alloc] initWithPath:path] autorelease];
 22}
 23
 24+ (id)statsCollectionWithPath:(NSString *)path
 25              autoSynchronize:(BOOL)autoSync {
 26  return [[[self alloc] initWithPath:path
 27                     autoSynchronize:autoSync] autorelease];
 28}
 29
 30- (id)init {
 31  return [self initWithPath:nil];
 32}
 33
 34- (id)initWithPath:(NSString *)path {
 35  return [self initWithPath:path autoSynchronize:YES];
 36}
 37
 38- (id)initWithPath:(NSString *)path
 39   autoSynchronize:(BOOL)autoSync {
 40  if ((self = [super init])) {
 41    if (path == nil) {
 42      [self release];
 43      return nil;
 44    }
 45    
 46    path_ = [path copy];
 47    autoSynchronize_ = autoSync;
 48    
 49    // Try to load stats from the specified path. If it doesn't work (maybe the
 50    // file doesn't exist yet), then create an empty dictionary.
 51    stats_ = [[NSMutableDictionary alloc] initWithContentsOfFile:path_];
 52    if (stats_ == nil) stats_ = [[NSMutableDictionary alloc] init];
 53  
 54    // If auto synchronizing is enabled, then do a sync right now to make sure
 55    // we can. If we fail to sync, release self and return nil to indicate an
 56    // error.
 57    if (autoSynchronize_) {
 58      BOOL synced = [self synchronize];
 59      if (!synced) {
 60        [self release];
 61        return nil;
 62      }
 63    }
 64  }
 65  return self;
 66}
 67
 68- (void)dealloc {
 69  [path_ release];
 70  [stats_ release];
 71  [super dealloc];
 72}
 73
 74- (NSString *)path {
 75  return [[path_ copy] autorelease];
 76}
 77
 78- (NSDictionary *)statsDictionary {
 79  return [[stats_ copy] autorelease];
 80}
 81
 82- (unsigned int)count {
 83  unsigned int count = 0;
 84  @synchronized (stats_) {
 85    count = [stats_ count];
 86  }
 87  return count;
 88}
 89
 90- (NSString *)description {
 91  return [NSString stringWithFormat:@"<%@:%p path=\"%@\", count=%d, stats=%@>",
 92          [self class], self, path_, [self count], [self statsDictionary]];
 93}
 94
 95- (void)removeAllStats {
 96  @synchronized (stats_) {
 97    [stats_ removeAllObjects];
 98    if (autoSynchronize_) [self synchronize];
 99  }
100}
101
102- (void)setNumber:(NSNumber *)num forStat:(NSString *)stat {
103  if (num != nil && stat != nil) {
104    @synchronized (stats_) {
105      [stats_ setObject:num forKey:stat];
106      if (autoSynchronize_) [self synchronize];
107    }
108  }
109}
110
111- (NSNumber *)numberForStat:(NSString *)stat {
112  NSNumber *num = nil;
113  if (stat != nil) {
114    @synchronized (stats_) {
115      num = [stats_ objectForKey:stat];
116    }
117  }
118  return num;
119}
120
121- (void)incrementStat:(NSString *)stat {
122  return [self incrementStat:stat by:1];
123}
124
125- (void)incrementStat:(NSString *)stat by:(int)n {
126  if (stat == nil) return;
127  @synchronized (stats_) {
128    NSNumber *num = [stats_ objectForKey:stat];
129    long long val = 0;
130    if (num) val = [num longLongValue];
131    NSNumber *inc = [NSNumber numberWithLongLong:(val + n)];
132    [stats_ setObject:inc forKey:stat];
133    if (autoSynchronize_) [self synchronize];
134  }
135}
136
137- (void)decrementStat:(NSString *)stat {
138  return [self incrementStat:stat by:-1];
139}
140
141- (void)decrementStat:(NSString *)stat by:(int)n {
142  return [self incrementStat:stat by:-n];
143}
144
145- (BOOL)synchronize {
146  BOOL ok = NO;
147  @synchronized (stats_) {
148    ok = [stats_ writeToFile:path_ atomically:YES];
149  }
150  return ok;
151}
152
153- (BOOL)autoSynchronize {
154  return autoSynchronize_;
155}
156
157- (void)setAutoSynchronize:(BOOL)autoSync {
158  autoSynchronize_ = autoSync;
159}
160
161@end