MongoDB C++ Driver  legacy-1.1.2
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 
24 namespace mongo {
25 
35 class MONGO_CLIENT_API WriteConcern {
36 public:
38  WriteConcern();
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 
56  static const WriteConcern acknowledged;
57 
59  static const WriteConcern journaled;
60 
62  static const WriteConcern replicated;
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 
111  WriteConcern& nodes(int w);
112 
114  WriteConcern& mode(const StringData& w);
115 
117  WriteConcern& journal(bool j);
118 
125  WriteConcern& fsync(bool fsync);
126 
128  WriteConcern& timeout(int timeout);
129 
131  bool requiresConfirmation() const;
132 
134  bool hasMode() const;
135 
137  BSONObj obj() const;
138 
139 private:
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
static const WriteConcern journaled
A single node acknowledges the write operation was committed to journal.
Definition: write_concern.h:59
static const WriteConcern unacknowledged
Fire and forget.
Definition: write_concern.h:53
static const WriteConcern acknowledged
A single node acknowledges the write, equivalent to default constructor.
Definition: write_concern.h:56
static const WriteConcern replicated
Two nodes have acknowledged receipt of the write operation.
Definition: write_concern.h:62
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents...
Definition: string_data.h:43
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
static const WriteConcern majority
A majority of nodes acknowledges (replica set)
Definition: write_concern.h:65
BSON classes.
Class to encapsulate client side "Write Concern" concept.
Definition: write_concern.h:35
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78