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#pragma once
19
20#include "config.h"
21
22#include "configuration.h"
23#include "logger.h"
24
25#include <string>
26
27class Logger;
28
29class KVStoreConfig {
30public:
31    /**
32     * This constructor intialises the object from a central
33     * ep-engine Configuration instance.
34     */
35    KVStoreConfig(Configuration& config, uint16_t shardId);
36
37    /**
38     * This constructor sets the mandatory config options
39     *
40     * Optional config options are set using a separate method
41     */
42    KVStoreConfig(uint16_t _maxVBuckets,
43                  uint16_t _maxShards,
44                  const std::string& _dbname,
45                  const std::string& _backend,
46                  uint16_t _shardId,
47                  bool persistDocNamespace);
48
49    virtual ~KVStoreConfig();
50
51    uint16_t getMaxVBuckets() const {
52        return maxVBuckets;
53    }
54
55    uint16_t getMaxShards() const {
56        return maxShards;
57    }
58
59    std::string getDBName() const {
60        return dbname;
61    }
62
63    const std::string& getBackend() const {
64        return backend;
65    }
66
67    uint16_t getShardId() const {
68        return shardId;
69    }
70
71    Logger& getLogger() {
72        return *logger;
73    }
74
75    /**
76     * Indicates whether or not underlying file operations will be
77     * buffered by the storage engine used.
78     *
79     * Only recognised by CouchKVStore
80     */
81    bool getBuffered() const {
82        return buffered;
83    }
84
85    /**
86     * Used to override the default logger object
87     */
88    KVStoreConfig& setLogger(Logger& _logger);
89
90    /**
91     * Used to override the default buffering behaviour.
92     *
93     * Only recognised by CouchKVStore
94     */
95    KVStoreConfig& setBuffered(bool _buffered);
96
97    bool shouldPersistDocNamespace() const {
98        return persistDocNamespace;
99    }
100
101    void setPersistDocNamespace(bool value) {
102        persistDocNamespace = value;
103    }
104
105    uint64_t getPeriodicSyncBytes() const {
106        return periodicSyncBytes;
107    }
108
109    void setPeriodicSyncBytes(uint64_t bytes) {
110        periodicSyncBytes = bytes;
111    }
112    void setCouchstoreTracingEnabled(bool value) {
113        couchstoreTracingEnabled = value;
114    }
115
116    bool getCouchstoreTracingEnabled() const {
117        return couchstoreTracingEnabled;
118    }
119    void setCouchstoreWriteValidationEnabled(bool value) {
120        couchstoreWriteValidationEnabled = value;
121    }
122
123    bool getCouchstoreWriteValidationEnabled() const {
124        return couchstoreWriteValidationEnabled;
125    }
126
127    void setCouchstoreMprotectEnabled(bool value) {
128        couchstoreMprotectEnabled = value;
129    }
130
131    bool getCouchstoreMprotectEnabled() const {
132        return couchstoreMprotectEnabled;
133    }
134
135private:
136    class ConfigChangeListener;
137
138    uint16_t maxVBuckets;
139    uint16_t maxShards;
140    std::string dbname;
141    std::string backend;
142    uint16_t shardId;
143    Logger* logger;
144    bool buffered;
145    bool persistDocNamespace;
146
147    /**
148     * If non-zero, tell storage layer to issue a sync() operation after every
149     * N bytes written.
150     */
151    uint64_t periodicSyncBytes;
152
153    /* enable tracing for couchstore */
154    bool couchstoreTracingEnabled;
155    /* enable write verification for couchstore */
156    bool couchstoreWriteValidationEnabled;
157    /* enbale mprotect of couchstore internal io buffer */
158    bool couchstoreMprotectEnabled;
159};
160