APLevelDB.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // APLevelDB.h
  3. //
  4. // Created by Adam Preble on 1/23/12.
  5. // Copyright (c) 2012 Adam Preble. All rights reserved.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import <Foundation/Foundation.h>
  25. extern NSString * const APLevelDBErrorDomain;
  26. @class APLevelDBIterator;
  27. @protocol APLevelDBWriteBatch;
  28. @interface APLevelDB : NSObject
  29. @property (nonatomic, readonly, strong) NSString *path;
  30. + (APLevelDB *)levelDBWithPath:(NSString *)path error:(NSError *__autoreleasing*)errorOut;
  31. - (void)close;
  32. - (BOOL)setData:(NSData *)data forKey:(NSString *)key;
  33. - (BOOL)setString:(NSString *)str forKey:(NSString *)key;
  34. - (NSData *)dataForKey:(NSString *)key;
  35. - (NSString *)stringForKey:(NSString *)key;
  36. - (BOOL)removeKey:(NSString *)key;
  37. - (NSArray *)allKeys;
  38. - (void)enumerateKeys:(void (^)(NSString *key, BOOL *stop))block;
  39. - (void)enumerateKeysWithPrefix:(NSString *)prefix usingBlock:(void (^)(NSString *key, BOOL *stop))block;
  40. - (void)enumerateKeysAndValuesAsStrings:(void (^)(NSString *key, NSString *value, BOOL *stop))block;
  41. - (void)enumerateKeysWithPrefix:(NSString *)prefix asStrings:(void (^)(NSString *key, NSString *value, BOOL *stop))block;
  42. - (void)enumerateKeysAndValuesAsData:(void (^)(NSString *key, NSData *value, BOOL *stop))block;
  43. - (void)enumerateKeysWithPrefix:(NSString *)prefix asData:(void (^)(NSString *key, NSData *value, BOOL *stop))block;
  44. - (NSUInteger)approximateSizeFrom:(NSString *)from to:(NSString *)to;
  45. - (NSUInteger)exactSizeFrom:(NSString *)from to:(NSString *)to;
  46. // Objective-C Subscripting Support:
  47. // The database object supports subscripting for string-string and string-data key-value access and assignment.
  48. // Examples:
  49. // db[@"key"] = @"value";
  50. // db[@"key"] = [NSData data];
  51. // NSString *s = db[@"key"];
  52. // An NSInvalidArgumentException is raised if the key is not an NSString, or if the assigned object is not an
  53. // instance of NSString or NSData.
  54. - (id)objectForKeyedSubscript:(id)key;
  55. - (void)setObject:(id)object forKeyedSubscript:(id<NSCopying>)key;
  56. // Batch write/atomic update support:
  57. - (id<APLevelDBWriteBatch>)beginWriteBatch;
  58. @end
  59. @interface APLevelDBIterator : NSObject
  60. + (id)iteratorWithLevelDB:(APLevelDB *)db;
  61. // Designated initializer:
  62. - (id)initWithLevelDB:(APLevelDB *)db;
  63. - (BOOL)seekToKey:(NSString *)key;
  64. - (NSString *)nextKey;
  65. - (NSString *)key;
  66. - (NSString *)valueAsString;
  67. - (NSData *)valueAsData;
  68. @end
  69. @protocol APLevelDBWriteBatch <NSObject>
  70. - (void)setData:(NSData *)data forKey:(NSString *)key;
  71. - (void)setString:(NSString *)str forKey:(NSString *)key;
  72. - (void)removeKey:(NSString *)key;
  73. // Remove all of the buffered sets and removes:
  74. - (void)clear;
  75. - (BOOL)commit;
  76. @end