1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2017 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#include "kvstore_config.h"
19
20#include <platform/make_unique.h>
21
22/// A listener class to update KVStore related configs at runtime.
23class KVStoreConfig::ConfigChangeListener : public ValueChangedListener {
24public:
25    ConfigChangeListener(KVStoreConfig& c) : config(c) {
26    }
27
28    void sizeValueChanged(const std::string& key, size_t value) override {
29        if (key == "fsync_after_every_n_bytes_written") {
30            config.setPeriodicSyncBytes(value);
31        }
32    }
33    void booleanValueChanged(const std::string& key, bool value) override {
34        if (key == "couchstore_tracing") {
35            config.setCouchstoreTracingEnabled(value);
36        }
37        if (key == "couchstore_write_validation") {
38            config.setCouchstoreWriteValidationEnabled(value);
39        }
40        if (key == "couchstore_mprotect") {
41            config.setCouchstoreMprotectEnabled(value);
42        }
43    }
44
45private:
46    KVStoreConfig& config;
47};
48
49KVStoreConfig::KVStoreConfig(Configuration& config, uint16_t shardid)
50    : KVStoreConfig(config.getMaxVbuckets(),
51                    config.getMaxNumShards(),
52                    config.getDbname(),
53                    config.getBackend(),
54                    shardid,
55                    config.isCollectionsPrototypeEnabled()) {
56    setPeriodicSyncBytes(config.getFsyncAfterEveryNBytesWritten());
57    config.addValueChangedListener(
58            "fsync_after_every_n_bytes_written",
59            std::make_unique<ConfigChangeListener>(*this));
60    setCouchstoreTracingEnabled(config.isCouchstoreTracing());
61    config.addValueChangedListener(
62            "couchstore_tracing",
63            std::make_unique<ConfigChangeListener>(*this));
64    setCouchstoreWriteValidationEnabled(config.isCouchstoreWriteValidation());
65    config.addValueChangedListener(
66            "couchstore_write_validation",
67            std::make_unique<ConfigChangeListener>(*this));
68    setCouchstoreMprotectEnabled(config.isCouchstoreMprotect());
69    config.addValueChangedListener(
70            "couchstore_mprotect",
71            std::make_unique<ConfigChangeListener>(*this));
72}
73
74KVStoreConfig::KVStoreConfig(uint16_t _maxVBuckets,
75                             uint16_t _maxShards,
76                             const std::string& _dbname,
77                             const std::string& _backend,
78                             uint16_t _shardId,
79                             bool _persistDocNamespace)
80    : maxVBuckets(_maxVBuckets),
81      maxShards(_maxShards),
82      dbname(_dbname),
83      backend(_backend),
84      shardId(_shardId),
85      logger(&global_logger),
86      buffered(true),
87      persistDocNamespace(_persistDocNamespace),
88      couchstoreTracingEnabled(false),
89      couchstoreWriteValidationEnabled(false),
90      couchstoreMprotectEnabled(false) {
91}
92
93KVStoreConfig::~KVStoreConfig() = default;
94
95KVStoreConfig& KVStoreConfig::setLogger(Logger& _logger) {
96    logger = &_logger;
97    return *this;
98}
99
100KVStoreConfig& KVStoreConfig::setBuffered(bool _buffered) {
101    buffered = _buffered;
102    return *this;
103}
104