/
DataFile.cc
406 lines (318 loc) · 11.5 KB
/
DataFile.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
// DataFile.cc
//
// Copyright (c) 2014 Couchbase, Inc 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 "DataFile.hh"
#include "DataFile+Shared.hh"
#include "Record.hh"
#include "DocumentKeys.hh"
#include "FilePath.hh"
#include "Logging.hh"
#include "Endian.hh"
#include "RefCounted.hh"
#include "c4Private.h"
#include "PlatformIO.hh"
#include "Stopwatch.hh"
#include "Instrumentation.hh"
#include <errno.h>
#include <dirent.h>
#include <algorithm>
#include <thread>
#include "SQLiteDataFile.hh"
using namespace std;
namespace litecore {
// How long deleteDataFile() should wait for other threads to close their connections
static const unsigned kOtherDBCloseTimeoutSecs = 3;
LogDomain DBLog("DB");
unordered_map<string, DataFile::Shared*> DataFile::Shared::sFileMap;
mutex DataFile::Shared::sFileMapMutex;
#pragma mark - FACTORY:
void DataFile::Factory::moveFile(const FilePath &from, const FilePath &to) {
auto fromBaseLen = from.fileName().size();
from.forEachMatch([&](const FilePath &f) {
string toFile = to.fileName() + from.fileName().substr(fromBaseLen);
f.moveTo(to.dirName() + toFile);
});
}
bool DataFile::Factory::fileExists(const FilePath &path) {
return path.exists();
}
std::vector<DataFile::Factory*> DataFile::factories() {
return {&SQLiteDataFile::sqliteFactory()};
}
DataFile::Factory* DataFile::factoryNamed(const std::string &name) {
auto facs = factories();
if (name.empty())
return facs[0];
for (auto factory : facs)
if (name == factory->name())
return factory;
return nullptr;
}
DataFile::Factory* DataFile::factoryNamed(const char *name) {
if (!name)
name = "";
return factoryNamed(string(name));
}
DataFile::Factory* DataFile::factoryForFile(const FilePath &path) {
auto ext = path.extension();
for (auto factory : factories())
if (ext == factory->filenameExtension())
return factory;
return nullptr;
}
#pragma mark - DATAFILE:
const DataFile::Options DataFile::Options::defaults = DataFile::Options {
{true}, // sequences
true, true, true, true // create, writeable, useDocumentKeys, upgradeable
};
DataFile::DataFile(const FilePath &path, Delegate *delegate, const DataFile::Options *options)
:Logging(DBLog)
,_delegate(delegate)
,_path(path)
,_options(options ? *options : Options::defaults)
{
// Do this last so I'm fully constructed before other threads can see me (#425)
_shared = Shared::forPath(path, this);
}
DataFile::~DataFile() {
logDebug("destructing (~DataFile)");
Assert(!_inTransaction);
if (_shared)
_shared->removeDataFile(this);
}
string DataFile::loggingIdentifier() const {
return _path.path();
}
void DataFile::close() {
for (auto& i : _keyStores) {
i.second->close();
}
_close();
if (_shared->removeDataFile(this))
logInfo("Closing database");
}
void DataFile::reopen() {
logInfo("Opening database");
_shared->addDataFile(this);
}
void DataFile::checkOpen() const {
if (!isOpen())
error::_throw(error::NotOpen);
}
void DataFile::rekey(EncryptionAlgorithm alg, slice newKey) {
if (alg != kNoEncryption)
error::_throw(error::UnsupportedEncryption);
}
void DataFile::forOtherDataFiles(function_ref<void(DataFile*)> fn) {
_shared->forOpenDataFiles(this, fn);
}
Retained<RefCounted> DataFile::sharedObject(const string &key) {
return _shared->sharedObject(key);
}
Retained<RefCounted> DataFile::addSharedObject(const string &key, Retained<RefCounted> object) {
return _shared->addSharedObject(key, object);
}
#pragma mark - DELETION:
void DataFile::deleteDataFile() {
deleteDataFile(this, nullptr, _shared, factory());
}
bool DataFile::Factory::deleteFile(const FilePath &path, const Options *options) {
Retained<Shared> shared = Shared::forPath(path, nullptr);
return DataFile::deleteDataFile(nullptr, options, shared, *this);
}
bool DataFile::deleteDataFile(DataFile *file, const Options *options,
Shared *shared, Factory &factory)
{
shared->condemn(true);
try {
// Wait for other connections to close -- in multithreaded setups there may be races where
// another thread takes a bit longer to close its connection.
int n = 0;
fleece::Stopwatch st;
for(;;) {
auto otherConnections = (long)shared->openCount();
if (file && file->isOpen())
--otherConnections;
Assert(otherConnections >= 0);
if (otherConnections == 0)
break;
if (n++ == 0)
LogTo(DBLog, "Waiting for %ld other connection(s) to close before deleting %s",
otherConnections, shared->path.c_str());
if (st.elapsed() > kOtherDBCloseTimeoutSecs)
error::_throw(error::Busy, "Can't delete db file while other connections are open");
else
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (file)
file->close();
bool result = factory._deleteFile(shared->path, options);
shared->condemn(false);
return result;
} catch (...) {
shared->condemn(false);
throw;
}
}
#pragma mark - KEY-STORES:
const string DataFile::kDefaultKeyStoreName{"default"};
const string DataFile::kInfoKeyStoreName{"info"};
KeyStore& DataFile::getKeyStore(const string &name) const {
return getKeyStore(name, _options.keyStores);
}
KeyStore& DataFile::getKeyStore(const string &name, KeyStore::Capabilities options) const {
checkOpen();
auto i = _keyStores.find(name);
if (i != _keyStores.end()) {
KeyStore &store = *i->second;
store.reopen();
return store;
} else {
return const_cast<DataFile*>(this)->addKeyStore(name, options);
}
}
KeyStore& DataFile::addKeyStore(const string &name, KeyStore::Capabilities options) {
logDebug("open KVS '%s'", name.c_str());
checkOpen();
Assert(!(options.sequences && !_options.keyStores.sequences),
"KeyStore can't have sequences if Database doesn't");
KeyStore *store = newKeyStore(name, options);
_keyStores[name] = unique_ptr<KeyStore>(store);
return *store;
}
void DataFile::closeKeyStore(const string &name) {
logDebug("close KVS '%s'", name.c_str());
auto i = _keyStores.find(name);
if (i != _keyStores.end()) {
// Never remove a KeyStore from _keyStores: there may be objects pointing to it
i->second->close();
}
}
KeyStore& DataFile::defaultKeyStore(KeyStore::Capabilities options) const {
checkOpen();
if (!_defaultKeyStore)
const_cast<DataFile*>(this)->_defaultKeyStore = &getKeyStore(kDefaultKeyStoreName,
options);
return *_defaultKeyStore;
}
void DataFile::forOpenKeyStores(function_ref<void(KeyStore&)> fn) {
for (auto& ks : _keyStores)
fn(*ks.second);
}
fleece::impl::SharedKeys* DataFile::documentKeys() const {
auto keys = _documentKeys.get();
if (!keys && _options.useDocumentKeys) {
auto mutableThis = const_cast<DataFile*>(this);
keys = new DocumentKeys(*mutableThis);
mutableThis->_documentKeys = keys;
}
return keys;
}
#pragma mark - TRANSACTION:
void DataFile::beginTransactionScope(Transaction* t) {
Assert(!_inTransaction);
checkOpen();
_shared->setTransaction(t);
_inTransaction = true;
}
void DataFile::transactionBegan(Transaction*) {
if (documentKeys())
_documentKeys->transactionBegan();
}
void DataFile::transactionEnding(Transaction*, bool committing) {
if (_documentKeys) {
if (committing)
_documentKeys->save();
else
_documentKeys->revert();
}
}
void DataFile::endTransactionScope(Transaction* t) {
_shared->unsetTransaction(t);
_inTransaction = false;
if (_documentKeys)
_documentKeys->transactionEnded();
}
Transaction& DataFile::transaction() {
Assert(_inTransaction);
return *_shared->transaction();
}
void DataFile::withFileLock(function_ref<void(void)> fn) {
if (_inTransaction) {
fn();
} else {
Transaction t(this, false);
fn();
}
}
Transaction::Transaction(DataFile* db)
:Transaction(db, true)
{ }
Transaction::Transaction(DataFile* db, bool active)
:_db(*db),
_active(false)
{
_db.beginTransactionScope(this);
if (active) {
_db._logVerbose("begin transaction");
Signpost::begin(Signpost::transaction, uintptr_t(this));
_db._beginTransaction(this);
_active = true;
_db.transactionBegan(this);
}
}
void Transaction::commit() {
Assert(_active, "Transaction is not active");
_db.transactionEnding(this, true);
_active = false;
_db._logVerbose("commit transaction");
Stopwatch st;
_db._endTransaction(this, true);
auto elapsed = st.elapsed();
Signpost::end(Signpost::transaction, uintptr_t(this));
if (elapsed >= 0.1)
_db._logInfo("Committing transaction took %.3f sec", elapsed);
}
void Transaction::abort() {
Assert(_active, "Transaction is not active");
_db.transactionEnding(this, false);
_active = false;
_db._logVerbose("abort transaction");
_db._endTransaction(this, false);
Signpost::end(Signpost::transaction, uintptr_t(this));
}
Transaction::~Transaction() {
if (_active) {
_db._logInfo("Transaction exiting scope without explicit commit; aborting");
abort();
}
_db.endTransactionScope(this);
}
ReadOnlyTransaction::ReadOnlyTransaction(DataFile *db) {
db->beginReadOnlyTransaction();
_db = db;
}
ReadOnlyTransaction::~ReadOnlyTransaction() {
if (_db) {
try {
_db->endReadOnlyTransaction();
} catch (...) {
_db->warn("~ReadOnlyTransaction caught C++ exception in endReadOnlyTransaction");
}
}
}
}