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

http://macfuse.googlecode.com/ · C Header · 130 lines · 27 code · 22 blank · 81 comment · 0 complexity · 89e0145914331ffcdd9f626c696dc818 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 <Foundation/Foundation.h>
  15. // KSStatsCollection
  16. //
  17. // A class for managing a collection of "stats" that are persisted to disk. A
  18. // "stat" is simply a key/value pair where the key is any NSString and the value
  19. // is any NSNumber. Simple operations can be performed on stats, such as
  20. // incrementing and decrementing. Since stats values can be any NSNumber, both
  21. // integer and floating point values are acceptable. Internally, integers are
  22. // treated as "long long" types during increment and decrement operations. If
  23. // you increment or decrement a stat, it is up to the you to ensure that you're
  24. // not incrementing NSNumbers that are actually float values.
  25. //
  26. // By default, the stats collection is persisted to disk after every operation
  27. // that mutates the collection. This can be disabled with the
  28. // -setAutoSynchronize: method. If you disable this, you are responsible for
  29. // calling -synchronize, otherwise you may lose data if the application crashes.
  30. //
  31. // To be sure that your stats are correctly persisted to disk, you should make
  32. // sure to call -synchronize before quitting. Do this even if auto synchronizing
  33. // was enabled.
  34. //
  35. // This class is thread safe.
  36. //
  37. // Sample usage:
  38. //
  39. // KSStatsCollection *stats =
  40. // [KSStatsCollection statsCollectionWithPath:@"/tmp/test.stats"];
  41. //
  42. // [stats incrementStat:@"foo"];
  43. // [stats incrementStat:@"bar"];
  44. //
  45. // NSLog(@"Recorded stats are %@", stats);
  46. //
  47. @interface KSStatsCollection : NSObject {
  48. @private
  49. NSString *path_;
  50. NSMutableDictionary *stats_;
  51. BOOL autoSynchronize_;
  52. }
  53. // Returns an autoreleased KSStatsCollection instance that will persiste the
  54. // stats to |path|.
  55. + (id)statsCollectionWithPath:(NSString *)path;
  56. // Returns an autoreleased KSStatsCollection instance that will persiste the
  57. // stats to |path|, and will do so automatically based on the value of
  58. // |autoSync|.
  59. + (id)statsCollectionWithPath:(NSString *)path
  60. autoSynchronize:(BOOL)autoSync;
  61. // Returns a KSStatsCollection instance that will persist the stats to |path|.
  62. // Sets autoSynchronize to YES by default.
  63. - (id)initWithPath:(NSString *)path;
  64. // Designated initializer. Returns a KSStatsCollection instance that will
  65. // persist the stats to |path|, and will do so automatically based on the value
  66. // of |autoSync|.
  67. - (id)initWithPath:(NSString *)path
  68. autoSynchronize:(BOOL)autoSync;
  69. // Returns the path where the stats are persisted.
  70. - (NSString *)path;
  71. // Returns a copy of the internal stats dictionary.
  72. - (NSDictionary *)statsDictionary;
  73. // Returns the number of stats in the internal dictionary.
  74. - (unsigned int)count;
  75. // Removes all stats from this collection.
  76. - (void)removeAllStats;
  77. // Returns YES if the stats collection will automatically write the stats to
  78. // disk after every call that mutates the internal stats dictionary. The default
  79. // is YES.
  80. - (BOOL)autoSynchronize;
  81. // Sets whether stats should be automatically written to disk after mutating
  82. // methods.
  83. - (void)setAutoSynchronize:(BOOL)autoSync;
  84. // Explicitly requests that the stats be written to disk. It is always safe to
  85. // call this method, but it is only necessary if -autoSynchronize is NO.
  86. - (BOOL)synchronize;
  87. //
  88. // Methods for setting, getting, incremeting, and decrementing stats.
  89. //
  90. // Sets the NSNumber value for the given |stat|.
  91. - (void)setNumber:(NSNumber *)num forStat:(NSString *)stat;
  92. // Returns the NSNumber value for the given |stat|.
  93. - (NSNumber *)numberForStat:(NSString *)stat;
  94. // Increments the specified |stat| by 1. If the stat was previously undefined,
  95. // it will count as 0, so the first increment will yield a value of 1.
  96. - (void)incrementStat:(NSString *)stat;
  97. // Increments the specified |stat| by the amount specified by |n|. Setting n = 1
  98. // is equivalent to calling -incrementStat:. A negative value for |n| is
  99. // equivalent to decrementing the stat.
  100. - (void)incrementStat:(NSString *)stat by:(int)n;
  101. // Decrements the specified |stat| by 1. If the stat was previously undefined,
  102. // it will count as 0, so the first decrement will yield a value of -1.
  103. - (void)decrementStat:(NSString *)stat;
  104. // Decrements the specified |stat| by the amount specified by |n|. Setting n = 1
  105. // is equivalent to calling -decrementStat: A negative value for |n| is
  106. // equivalent to incrementing the stat.
  107. - (void)decrementStat:(NSString *)stat by:(int)n;
  108. @end