This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
TDBlobStore.h
104 lines (76 loc) · 2.68 KB
/
TDBlobStore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// TDBlobStore.h
// TouchDB
//
// Created by Jens Alfke on 12/10/11.
// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#ifdef GNUSTEP
#import <openssl/md5.h>
#import <openssl/sha.h>
#else
#define COMMON_DIGEST_FOR_OPENSSL
#import <CommonCrypto/CommonDigest.h>
#endif
/** Key identifying a data blob. This happens to be a SHA-1 digest. */
typedef struct TDBlobKey {
uint8_t bytes[SHA_DIGEST_LENGTH];
} TDBlobKey;
/** A persistent content-addressable store for arbitrary-size data blobs.
Each blob is stored as a file named by its SHA-1 digest. */
@interface TDBlobStore : NSObject
{
NSString* _path;
NSString* _tempDir;
}
- (id) initWithPath: (NSString*)dir error: (NSError**)outError;
- (NSData*) blobForKey: (TDBlobKey)key;
- (NSInputStream*) blobInputStreamForKey: (TDBlobKey)key
length: (UInt64*)outLength;
- (BOOL) storeBlob: (NSData*)blob
creatingKey: (TDBlobKey*)outKey;
@property (readonly) NSString* path;
@property (readonly) NSUInteger count;
@property (readonly) NSArray* allKeys;
@property (readonly) UInt64 totalDataSize;
- (NSInteger) deleteBlobsExceptWithKeys: (NSSet*)keysToKeep;
+ (TDBlobKey) keyForBlob: (NSData*)blob;
+ (NSData*) keyDataForBlob: (NSData*)blob;
/** Returns the path of the file storing the attachment with the given key, or nil.
DO NOT MODIFY THIS FILE! */
- (NSString*) pathForKey: (TDBlobKey)key;
@end
typedef struct {
uint8_t bytes[MD5_DIGEST_LENGTH];
} TDMD5Key;
/** Lets you stream a large attachment to a TDBlobStore asynchronously, e.g. from a network download. */
@interface TDBlobStoreWriter : NSObject {
@private
TDBlobStore* _store;
NSString* _tempPath;
NSFileHandle* _out;
UInt64 _length;
SHA_CTX _shaCtx;
MD5_CTX _md5Ctx;
TDBlobKey _blobKey;
TDMD5Key _MD5Digest;
}
- (id) initWithStore: (TDBlobStore*)store;
/** Appends data to the blob. Call this when new data is available. */
- (void) appendData: (NSData*)data;
/** Call this after all the data has been added. */
- (void) finish;
/** Call this to cancel before finishing the data. */
- (void) cancel;
/** Installs a finished blob into the store. */
- (BOOL) install;
/** The number of bytes in the blob. */
@property (readonly) UInt64 length;
/** After finishing, this is the key for looking up the blob through the TDBlobStore. */
@property (readonly) TDBlobKey blobKey;
/** After finishing, this is the MD5 digest of the blob, in base64 with an "md5-" prefix.
(This is useful for compatibility with CouchDB, which stores MD5 digests of attachments.) */
@property (readonly) NSString* MD5DigestString;
@property (readonly) NSString* SHA1DigestString;
@end