-
Notifications
You must be signed in to change notification settings - Fork 72
/
c4BlobStore.cc
235 lines (183 loc) · 6.47 KB
/
c4BlobStore.cc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// c4BlobStore.cc
// LiteCore
//
// Created by Jens Alfke on 9/1/16.
// Copyright © 2016 Couchbase. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
#include "c4Internal.hh"
#include "c4BlobStore.h"
#include "BlobStore.hh"
#include "Database.hh"
#include <libb64/decode.h>
// This is a no-op class that just serves to make c4BlobStore type-compatible with BlobStore.
struct c4BlobStore : public BlobStore {
public:
c4BlobStore(const FilePath &dirPath, const Options *options)
:BlobStore(dirPath, options)
{ }
};
static inline const blobKey& internal(const C4BlobKey &key) {return *(blobKey*)&key;}
static inline const C4BlobKey& external(const blobKey &key) {return *(C4BlobKey*)&key;}
static SeekableReadStream* internal(C4ReadStream* s) {return (SeekableReadStream*)s;}
static inline C4ReadStream* external(SeekableReadStream* s) {return (C4ReadStream*)s;}
static BlobWriteStream* internal(C4WriteStream* s) {return (BlobWriteStream*)s;}
static inline C4WriteStream* external(BlobWriteStream* s) {return (C4WriteStream*)s;}
bool c4blob_keyFromString(C4Slice str, C4BlobKey* outKey) noexcept {
try {
blobKey key(string((char*)str.buf, str.size));
*outKey = external(key);
return true;
} catchExceptions()
return false;
}
C4SliceResult c4blob_keyToString(C4BlobKey key) noexcept {
string str = internal(key).base64String();
return sliceResult(str);
}
C4BlobStore* c4blob_openStore(C4Slice dirPath,
C4DatabaseFlags flags,
const C4EncryptionKey *key,
C4Error* outError) noexcept
{
try {
BlobStore::Options options = {};
options.create = (flags & kC4DB_Create) != 0;
options.writeable = !(flags & kC4DB_ReadOnly);
if (key) {
options.encryptionAlgorithm = (EncryptionAlgorithm)key->algorithm;
options.encryptionKey = alloc_slice(key->bytes, sizeof(key->bytes));
}
return new c4BlobStore(FilePath((string)dirPath), &options);
} catchError(outError)
return nullptr;
}
C4BlobStore* c4db_getBlobStore(C4Database *db, C4Error* outError) noexcept {
try {
return (C4BlobStore*) db->blobStore();
} catchError(outError)
return nullptr;
}
void c4blob_freeStore(C4BlobStore *store) noexcept {
delete store;
}
bool c4blob_deleteStore(C4BlobStore* store, C4Error *outError) noexcept {
try {
store->deleteStore();
delete store;
return true;
} catchError(outError)
return false;
}
int64_t c4blob_getSize(C4BlobStore* store, C4BlobKey key) noexcept {
try {
return store->get(internal(key)).contentLength();
} catchExceptions()
return -1;
}
C4SliceResult c4blob_getContents(C4BlobStore* store, C4BlobKey key, C4Error* outError) noexcept {
try {
return sliceResult(store->get(internal(key)).contents());
} catchError(outError)
return {nullptr, 0};
}
C4StringResult c4blob_getFilePath(C4BlobStore* store, C4BlobKey key, C4Error* outError) noexcept {
try {
auto path = store->get(internal(key)).path();
if (!path.exists()) {
recordError(LiteCoreDomain, kC4ErrorNotFound, outError);
return {nullptr, 0};
} else if (store->isEncrypted()) {
recordError(LiteCoreDomain, kC4ErrorWrongFormat, outError);
return {nullptr, 0};
}
return sliceResult((string)path);
} catchError(outError)
return {nullptr, 0};
}
bool c4blob_create(C4BlobStore* store, C4Slice contents, C4BlobKey *outKey, C4Error* outError) noexcept {
try {
Blob blob = store->put(contents);
if (outKey)
*outKey = external(blob.key());
return true;
} catchError(outError)
return false;
}
bool c4blob_delete(C4BlobStore* store, C4BlobKey key, C4Error* outError) noexcept {
try {
store->get(internal(key)).del();
return true;
} catchError(outError)
return false;
}
#pragma mark - STREAMING READS:
C4ReadStream* c4blob_openReadStream(C4BlobStore* store, C4BlobKey key, C4Error* outError) noexcept {
try {
unique_ptr<SeekableReadStream> stream = store->get(internal(key)).read();
return external(stream.release());
} catchError(outError)
return nullptr;
}
size_t c4stream_read(C4ReadStream* stream, void *buffer, size_t maxBytes, C4Error* outError) noexcept {
try {
clearError(outError);
return internal(stream)->read(buffer, maxBytes);
} catchError(outError)
return 0;
}
int64_t c4stream_getLength(C4ReadStream* stream, C4Error* outError) noexcept {
try {
return internal(stream)->getLength();
} catchError(outError)
return -1;
}
bool c4stream_seek(C4ReadStream* stream, uint64_t position, C4Error* outError) noexcept {
try {
internal(stream)->seek(position);
return true;
} catchError(outError)
return false;
}
void c4stream_close(C4ReadStream* stream) noexcept {
delete internal(stream);
}
#pragma mark - STREAMING WRITES:
C4WriteStream* c4blob_openWriteStream(C4BlobStore* store, C4Error* outError) noexcept {
try {
return external(new BlobWriteStream(*store));
} catchError(outError)
return nullptr;
}
bool c4stream_write(C4WriteStream* stream, const void *bytes, size_t length, C4Error* outError) noexcept {
try {
internal(stream)->write(slice(bytes, length));
return true;
} catchError(outError)
return false;
}
C4BlobKey c4stream_computeBlobKey(C4WriteStream* stream) noexcept {
return external( internal(stream)->computeKey() );
}
bool c4stream_install(C4WriteStream* stream, C4Error *outError) noexcept {
try {
internal(stream)->install();
return true;
} catchError(outError)
return false;
}
void c4stream_closeWriter(C4WriteStream* stream) noexcept {
if (!stream)
return;
try {
internal(stream)->close();
delete internal(stream);
} catchExceptions()
}