Skip to content

Commit

Permalink
Merge pull request #2135 from couchbase/merge/3.2_master
Browse files Browse the repository at this point in the history
Merge 3.2.0 into master.
  • Loading branch information
jianminzhao committed Sep 11, 2024
2 parents 8009302 + f00ef2c commit abe9759
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 17 deletions.
15 changes: 8 additions & 7 deletions C/include/c4DatabaseTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ C4API_BEGIN_DECLS

/** Boolean options for C4DatabaseConfig. */
typedef C4_OPTIONS(uint32_t, C4DatabaseFlags){
kC4DB_Create = 0x01, ///< Create the file if it doesn't exist
kC4DB_ReadOnly = 0x02, ///< Open file read-only
kC4DB_AutoCompact = 0x04, ///< Enable auto-compaction [UNIMPLEMENTED]
kC4DB_VersionVectors = 0x08, ///< Upgrade DB to version vectors instead of rev trees
kC4DB_NoUpgrade = 0x20, ///< Disable upgrading an older-version database
kC4DB_NonObservable = 0x40, ///< Disable database/collection observers, for slightly faster writes
kC4DB_FakeVectorClock = 0x80, ///< Use counters instead of timestamps in version vectors (TESTS ONLY)
kC4DB_Create = 0x01, ///< Create the file if it doesn't exist
kC4DB_ReadOnly = 0x02, ///< Open file read-only
kC4DB_AutoCompact = 0x04, ///< Enable auto-compaction [UNIMPLEMENTED]
kC4DB_VersionVectors = 0x08, ///< Upgrade DB to version vectors instead of rev trees
kC4DB_NoUpgrade = 0x20, ///< Disable upgrading an older-version database
kC4DB_NonObservable = 0x40, ///< Disable database/collection observers, for slightly faster writes
kC4DB_DiskSyncFull = 0x80, ///< Flush to disk after each transaction
kC4DB_FakeVectorClock = 0x0100, ///< Use counters instead of timestamps in version vectors (TESTS ONLY)
};


Expand Down
1 change: 1 addition & 0 deletions LiteCore/Database/DatabaseImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ namespace litecore {
options.create = (_config.flags & kC4DB_Create) != 0;
options.writeable = (_config.flags & kC4DB_ReadOnly) == 0;
options.upgradeable = (_config.flags & kC4DB_NoUpgrade) == 0;
options.diskSyncFull = (_config.flags & kC4DB_DiskSyncFull) != 0;
options.useDocumentKeys = true;
options.encryptionAlgorithm = (EncryptionAlgorithm)_config.encryptionKey.algorithm;
if ( options.encryptionAlgorithm != kNoEncryption ) {
Expand Down
8 changes: 3 additions & 5 deletions LiteCore/Storage/DataFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ namespace litecore {


const DataFile::Options DataFile::Options::defaults = {
{true}, // sequences
true,
true,
true,
true // create, writeable, useDocumentKeys, upgradeable
{true}, // sequences
true, true, true, true, // create, writeable, useDocumentKeys, upgradeable
false // diskSyncFull
};

DataFile::DataFile(const FilePath& path, Delegate* delegate, const DataFile::Options* options)
Expand Down
3 changes: 3 additions & 0 deletions LiteCore/Storage/DataFile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace litecore {
bool writeable : 1; ///< If false, db is opened read-only
bool useDocumentKeys : 1; ///< Use SharedKeys for Fleece docs
bool upgradeable : 1; ///< DB schema can be upgraded
bool diskSyncFull : 1; ///< SQLite PRAGMA synchronous
EncryptionAlgorithm encryptionAlgorithm; ///< What encryption (if any)
alloc_slice encryptionKey; ///< Encryption key, if encrypting
DatabaseTag dbTag;
Expand Down Expand Up @@ -273,6 +274,8 @@ namespace litecore {

void setOptions(const Options& o) { _options = o; }

const Options& getOptions() const { return _options; }

void forOpenKeyStores(function_ref<void(KeyStore&)> fn);

virtual Factory& factory() const = 0;
Expand Down
4 changes: 3 additions & 1 deletion LiteCore/Storage/KeyStore.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
//

#pragma once
#define LITECORE_CPP_API 1
#ifndef LITECORE_CPP_API
# define LITECORE_CPP_API 1
#endif
#include "IndexSpec.hh"
#include "RecordEnumerator.hh"
#include <optional>
Expand Down
5 changes: 3 additions & 2 deletions LiteCore/Storage/SQLiteDataFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,12 @@ namespace litecore {
_exec(stringprintf(
"PRAGMA cache_size=%d; " // Memory cache
"PRAGMA mmap_size=%d; " // Memory-mapped reads
"PRAGMA synchronous=normal; " // Speeds up commits
"PRAGMA synchronous=%s; " // Speeds up commits
"PRAGMA journal_size_limit=%lld; " // Limit WAL disk usage
"PRAGMA case_sensitive_like=true; " // Case sensitive LIKE, for N1QL compat
"PRAGMA fullfsync=ON", // Attempt to mitigate damage due to sudden loss of power (iOS / macOS)
-(int)kCacheSize / 1024, kMMapSize, (long long)kJournalSize));
-(int)kCacheSize / 1024, kMMapSize, getOptions().diskSyncFull ? "full" : "normal",
(long long)kJournalSize));

(void)upgradeSchema(SchemaVersion::WithPurgeCount, "Adding purgeCnt column", [&] {
// Schema upgrade: Add the `purgeCnt` column to the kvmeta table.
Expand Down
25 changes: 25 additions & 0 deletions LiteCore/tests/c4BaseTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "c4ExceptionUtils.hh"
#include "fleece/InstanceCounted.hh"
#include "catch.hpp"
#include "DatabaseImpl.hh"
#include "NumConversion.hh"
#include "Actor.hh"
#include "URLTransformer.hh"
Expand All @@ -25,6 +26,7 @@
# include "Error.hh"
# include <winerror.h>
#endif
#include <sstream>

using namespace fleece;
using namespace std;
Expand Down Expand Up @@ -138,6 +140,29 @@ TEST_CASE("C4Error Reporting Macros", "[Errors][C]") {
#endif
}

TEST_CASE_METHOD(C4Test, "Database Flag FullSync", "[Database][C]") {
// Ensure that, by default, diskSyncFull is false.
CHECK(!litecore::asInternal(db)->dataFile()->options().diskSyncFull);

C4DatabaseConfig2 config = *c4db_getConfig2(db);
config.flags |= kC4DB_DiskSyncFull;

std::stringstream ss;
ss << std::string(c4db_getName(db)) << "_" << c4_now();
c4::ref<C4Database> dbWithFullSync = c4db_openNamed(slice(ss.str().c_str()), &config, ERROR_INFO());
// The flag in config is passed to DataFile options.
CHECK(litecore::asInternal(dbWithFullSync)->dataFile()->options().diskSyncFull);

config.flags &= ~kC4DB_DiskSyncFull;
c4::ref<C4Database> otherConnection = c4db_openNamed(c4db_getName(dbWithFullSync), &config, ERROR_INFO());
// The flag applies per connection opened with the config.
CHECK(!litecore::asInternal(otherConnection)->dataFile()->options().diskSyncFull);

c4::ref<C4Database> againConnection = c4db_openAgain(dbWithFullSync, ERROR_INFO());
// The flag is passed to database opened by openAgain.
CHECK(litecore::asInternal(againConnection)->dataFile()->options().diskSyncFull);
}

#pragma mark - INSTANCECOUNTED:

namespace {
Expand Down
4 changes: 2 additions & 2 deletions Networking/HTTP/HTTPLogic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ namespace litecore::net {
// the new type can be handled the same way.
if ( _isWebSocket ) {
Address address = {_address.scheme() == "wss"_sl ? "https"_sl : "http"_sl, _address.hostname(),
_address.port(), _address.url()};
rq << string(Address::toURL(*(C4Address*)&address));
_address.port(), _address.path()};
rq << string(Address::toURL(*(C4Address*)address));
} else {
rq << string(_address.url());
}
Expand Down

0 comments on commit abe9759

Please sign in to comment.