MongoDB C++ Driver legacy-1.1.1
Loading...
Searching...
No Matches
write_concern.h
1/* Copyright 2014 MongoDB Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#pragma once
17
18#include <bitset>
19#include <string>
20
21#include "mongo/client/export_macros.h"
22#include "mongo/db/jsobj.h"
23
24namespace mongo {
25
35class MONGO_CLIENT_API WriteConcern {
36public:
39
45 static const char kMajority[];
46
47 //
48 // Standard write concern levels as defined in the MongoDB manual:
49 // http://docs.mongodb.org/manual/core/write-concern/
50 //
51
54
57
59 static const WriteConcern journaled;
60
63
65 static const WriteConcern majority;
66
75 int32_t nodes() const;
76
84 const std::string& mode() const;
85
91 bool journal() const;
92
101 bool fsync() const;
102
108 int32_t timeout() const;
109
112
115
118
125 WriteConcern& fsync(bool fsync);
126
128 WriteConcern& timeout(int timeout);
129
132
134 bool hasMode() const;
135
137 BSONObj obj() const;
138
139private:
140 // Enabled option book keeping
141 static const size_t kNumOptions = 5;
142 enum Options { kW, kWStr, kJ, kFsync, kTimeout };
143 std::bitset<kNumOptions> _enabled;
144
145 // Actual option values
146 int32_t _w;
147 std::string _w_str;
148 bool _j;
149 bool _fsync;
150 int32_t _timeout;
151};
152
153} // namespace mongo
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents.
Definition string_data.h:43
Class to encapsulate client side "Write Concern" concept.
Definition write_concern.h:35
static const WriteConcern acknowledged
A single node acknowledges the write, equivalent to default constructor.
Definition write_concern.h:56
int32_t nodes() const
Returns an integer representing the number of nodes required for write to be considered successful.
const std::string & mode() const
Returns a string representing the write concern mode.
bool journal() const
If write will only be considered successful when committed to journal.
BSONObj obj() const
Turn write concern into an object for inclusion in GetLastError or write command.
WriteConcern()
Default write concern: equivalent to acknowledged.
static const WriteConcern replicated
Two nodes have acknowledged receipt of the write operation.
Definition write_concern.h:62
int32_t timeout() const
Length of time to block waiting for nodes, journal, or fsync.
WriteConcern & timeout(int timeout)
Sets timeout to wait for write to be successful.
bool hasMode() const
Whether the write concern currently reflects a mode.
bool fsync() const
If fsync is set and the server is running without journaling, the write will only be considered succe...
static const WriteConcern unacknowledged
Fire and forget.
Definition write_concern.h:53
WriteConcern & mode(const StringData &w)
Sets the type of nodes required for write to be successful.
WriteConcern & nodes(int w)
Sets the number of nodes required for write to be successful.
bool requiresConfirmation() const
Whether we need to send getLastError for this WriteConcern.
static const WriteConcern majority
A majority of nodes acknowledges (replica set)
Definition write_concern.h:65
WriteConcern & fsync(bool fsync)
Sets the value of the fsync parameter.
WriteConcern & journal(bool j)
Sets whether journal is required for write to be successful.
static const WriteConcern journaled
A single node acknowledges the write operation was committed to journal.
Definition write_concern.h:59
BSON classes.
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:32