Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "CBL-6238: DataFile::documentKeys() is not thread-safe. (#2147)" #2154

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions LiteCore/Storage/DataFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace litecore {
return ret;
}


#pragma mark - DATAFILE:


Expand Down Expand Up @@ -190,7 +190,7 @@ namespace litecore {
// getKeyStore will reopen them on demand
i.second->close();
}

_shared->addDataFile(this);
}

Expand Down Expand Up @@ -299,7 +299,7 @@ namespace litecore {
std::this_thread::sleep_for(100ms);
}
}

if (file)
file->close(true);
bool result = factory._deleteFile(FilePath(shared->path), options);
Expand Down Expand Up @@ -350,7 +350,7 @@ namespace litecore {
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
// Never remove a KeyStore from _keyStores: there may be objects pointing to it
i->second->close();
}
}
Expand All @@ -369,15 +369,13 @@ namespace litecore {


fleece::impl::SharedKeys* DataFile::documentKeys() const {
std::call_once(_documentKeysOnce, [this] {
auto keys = _documentKeys.get();
if ( !keys && _options.useDocumentKeys ) {
auto mutableThis = const_cast<DataFile*>(this);
keys = new DocumentKeys(*mutableThis);
_documentKeys = keys;
}
});
return _documentKeys.get();
auto keys = _documentKeys.get();
if (!keys && _options.useDocumentKeys) {
auto mutableThis = const_cast<DataFile*>(this);
keys = new DocumentKeys(*mutableThis);
_documentKeys = keys;
}
return keys;
}

#pragma mark - QUERIES:
Expand Down Expand Up @@ -405,7 +403,7 @@ namespace litecore {

#pragma mark - TRANSACTION:


void DataFile::beginTransactionScope(ExclusiveTransaction* t) {
Assert(!_inTransaction);
checkOpen();
Expand All @@ -432,7 +430,7 @@ namespace litecore {
ks.transactionWillEnd(committing);
});
}

void DataFile::endTransactionScope(ExclusiveTransaction* t) {
_shared->unsetTransaction(t);
_inTransaction = false;
Expand Down
7 changes: 3 additions & 4 deletions LiteCore/Storage/DataFile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace litecore {
void forOtherDataFiles(function_ref<void(DataFile*)> fn);

//////// QUERIES:

/** Creates a database query object. */
virtual Retained<Query> compileQuery(slice expr,
QueryLanguage =QueryLanguage::kJSON,
Expand Down Expand Up @@ -203,7 +203,7 @@ namespace litecore {

/** Does a file exist at this path? */
virtual bool fileExists(const FilePath &path);

protected:
/** Deletes a non-open file. Returns false if it doesn't exist. */
virtual bool _deleteFile(const FilePath &path, const Options* =nullptr) =0;
Expand Down Expand Up @@ -274,7 +274,7 @@ namespace litecore {

static bool deleteDataFile(DataFile *file, const Options *options,
Shared *shared, Factory &factory);

KeyStore& addKeyStore(const std::string &name, KeyStore::Capabilities);
void closeAllQueries();
void beginTransactionScope(ExclusiveTransaction*);
Expand All @@ -297,7 +297,6 @@ namespace litecore {
std::mutex _queriesMutex; // Thread-safe access to _queries
bool _inTransaction {false}; // Am I in a Transaction?
std::atomic_bool _closeSignaled {false}; // Have I been asked to close?
mutable std::once_flag _documentKeysOnce{}; // Thread-safe init of documentKeys
};


Expand Down
Loading