Skip to content

Commit 2c90394

Browse files
committed
CDRIVER-837: Add mongoc_read_concern_t
1 parent 7eb7a07 commit 2c90394

File tree

5 files changed

+273
-0
lines changed

5 files changed

+273
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ set (SOURCES
112112
${SOURCE_DIR}/src/mongoc/mongoc-memcmp.c
113113
${SOURCE_DIR}/src/mongoc/mongoc-opcode.c
114114
${SOURCE_DIR}/src/mongoc/mongoc-queue.c
115+
${SOURCE_DIR}/src/mongoc/mongoc-read-concern.c
115116
${SOURCE_DIR}/src/mongoc/mongoc-read-prefs.c
116117
${SOURCE_DIR}/src/mongoc/mongoc-rpc.c
117118
${SOURCE_DIR}/src/mongoc/mongoc-server-description.c
@@ -159,6 +160,7 @@ set (HEADERS
159160
${SOURCE_DIR}/src/mongoc/mongoc-matcher.h
160161
${SOURCE_DIR}/src/mongoc/mongoc-opcode.h
161162
${SOURCE_DIR}/src/mongoc/mongoc-opcode-private.h
163+
${SOURCE_DIR}/src/mongoc/mongoc-read-concern.h
162164
${SOURCE_DIR}/src/mongoc/mongoc-read-prefs.h
163165
${SOURCE_DIR}/src/mongoc/mongoc-server-description.h
164166
${SOURCE_DIR}/src/mongoc/mongoc-socket.h

src/mongoc/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ INST_H_FILES = \
6868
src/mongoc/mongoc-opcode.h \
6969
src/mongoc/mongoc-opcode-private.h \
7070
src/mongoc/mongoc-queue-private.h \
71+
src/mongoc/mongoc-read-concern-private.h \
72+
src/mongoc/mongoc-read-concern.h \
7173
src/mongoc/mongoc-read-prefs-private.h \
7274
src/mongoc/mongoc-read-prefs.h \
7375
src/mongoc/mongoc-rpc-private.h \
@@ -141,6 +143,7 @@ MONGOC_SOURCES_SHARED += \
141143
src/mongoc/mongoc-memcmp.c \
142144
src/mongoc/mongoc-opcode.c \
143145
src/mongoc/mongoc-queue.c \
146+
src/mongoc/mongoc-read-concern.c \
144147
src/mongoc/mongoc-read-prefs.c \
145148
src/mongoc/mongoc-rpc.c \
146149
src/mongoc/mongoc-server-description.c \
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MONGOC_READ_CONCERN_PRIVATE_H
18+
#define MONGOC_READ_CONCERN_PRIVATE_H
19+
20+
#if !defined (MONGOC_I_AM_A_DRIVER) && !defined (MONGOC_COMPILATION)
21+
#error "Only <mongoc.h> can be included directly."
22+
#endif
23+
24+
#include <bson.h>
25+
#include <mongoc-read-concern.h>
26+
27+
28+
BSON_BEGIN_DECLS
29+
30+
31+
struct _mongoc_read_concern_t
32+
{
33+
char *level;
34+
bool frozen;
35+
bson_t compiled;
36+
};
37+
38+
39+
const bson_t *_mongoc_read_concern_get_bson (mongoc_read_concern_t *read_concern);
40+
41+
BSON_END_DECLS
42+
43+
44+
#endif /* MONGOC_READ_CONCERN_PRIVATE_H */

src/mongoc/mongoc-read-concern.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
#include "mongoc-log.h"
19+
#include "mongoc-read-concern.h"
20+
#include "mongoc-read-concern-private.h"
21+
22+
23+
static BSON_INLINE bool
24+
_mongoc_read_concern_warn_frozen (mongoc_read_concern_t *read_concern)
25+
{
26+
27+
return read_concern->frozen;
28+
}
29+
30+
static void
31+
_mongoc_read_concern_freeze (mongoc_read_concern_t *read_concern);
32+
33+
34+
/**
35+
* mongoc_read_concern_new:
36+
*
37+
* Create a new mongoc_read_concern_t.
38+
*
39+
* Returns: A newly allocated mongoc_read_concern_t. This should be freed
40+
* with mongoc_read_concern_destroy().
41+
*/
42+
mongoc_read_concern_t *
43+
mongoc_read_concern_new (void)
44+
{
45+
mongoc_read_concern_t *read_concern;
46+
47+
read_concern = (mongoc_read_concern_t *)bson_malloc0 (sizeof *read_concern);
48+
49+
return read_concern;
50+
}
51+
52+
53+
mongoc_read_concern_t *
54+
mongoc_read_concern_copy (const mongoc_read_concern_t *read_concern)
55+
{
56+
mongoc_read_concern_t *ret = NULL;
57+
58+
if (read_concern) {
59+
ret = mongoc_read_concern_new ();
60+
ret->level = bson_strdup (read_concern->level);
61+
}
62+
63+
return ret;
64+
}
65+
66+
67+
/**
68+
* mongoc_read_concern_destroy:
69+
* @read_concern: A mongoc_read_concern_t.
70+
*
71+
* Releases a mongoc_read_concern_t and all associated memory.
72+
*/
73+
void
74+
mongoc_read_concern_destroy (mongoc_read_concern_t *read_concern)
75+
{
76+
if (read_concern) {
77+
if (read_concern->compiled.len) {
78+
bson_destroy (&read_concern->compiled);
79+
}
80+
81+
bson_free (read_concern->level);
82+
bson_free (read_concern);
83+
}
84+
}
85+
86+
87+
const char *
88+
mongoc_read_concern_get_level (const mongoc_read_concern_t *read_concern)
89+
{
90+
BSON_ASSERT (read_concern);
91+
92+
return read_concern->level;
93+
}
94+
95+
96+
/**
97+
* mongoc_read_concern_set_level:
98+
* @read_concern: A mongoc_read_concern_t.
99+
* @level: The read concern level
100+
*
101+
* Sets the read concern level. Any string is supported for future compatability
102+
* but MongoDB 3.2 only accepts "local" and "majority", aka:
103+
* - MONGOC_READ_CONCERN_LEVEL_LOCAL
104+
* - MONGOC_READ_CONCERN_LEVEL_MAJORITY
105+
*
106+
* If the @read_concern has already been frozen, calling this function will not
107+
* alter the read concern level.
108+
*
109+
* See the MongoDB docs for more information on readConcernLevel
110+
*/
111+
bool
112+
mongoc_read_concern_set_level (mongoc_read_concern_t *read_concern,
113+
const char *level)
114+
{
115+
BSON_ASSERT (read_concern);
116+
117+
if (read_concern->frozen) {
118+
return false;
119+
}
120+
121+
bson_free (read_concern->level);
122+
read_concern->level = bson_strdup (level);
123+
return true;
124+
}
125+
126+
/**
127+
* mongoc_read_concern_get_bson:
128+
* @read_concern: A mongoc_read_concern_t.
129+
*
130+
* This is an internal function.
131+
*
132+
* Freeze the read concern if necessary and retrieve the encoded bson_t
133+
* representing the read concern.
134+
*
135+
* You may not modify the read concern further after calling this function.
136+
*
137+
* Returns: A bson_t that should not be modified or freed as it is owned by
138+
* the mongoc_read_concern_t instance.
139+
*/
140+
const bson_t *
141+
_mongoc_read_concern_get_bson (mongoc_read_concern_t *read_concern) {
142+
if (!read_concern->frozen) {
143+
_mongoc_read_concern_freeze (read_concern);
144+
}
145+
146+
return &read_concern->compiled;
147+
}
148+
149+
/**
150+
* mongoc_read_concern_freeze:
151+
* @read_concern: A mongoc_read_concern_t.
152+
*
153+
* This is an internal function.
154+
*
155+
* Freeze the read concern if necessary and encode it into a bson_ts which
156+
* represent the raw bson form and the get last error command form.
157+
*
158+
* You may not modify the read concern further after calling this function.
159+
*/
160+
static void
161+
_mongoc_read_concern_freeze (mongoc_read_concern_t *read_concern)
162+
{
163+
bson_t *compiled;
164+
165+
BSON_ASSERT (read_concern);
166+
167+
compiled = &read_concern->compiled;
168+
169+
read_concern->frozen = true;
170+
171+
bson_init (compiled);
172+
173+
BSON_ASSERT (read_concern->level);
174+
BSON_APPEND_UTF8 (compiled, "level", read_concern->level);
175+
}
176+
177+

src/mongoc/mongoc-read-concern.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MONGOC_READ_CONCERN_H
18+
#define MONGOC_READ_CONCERN_H
19+
20+
#if !defined (MONGOC_INSIDE) && !defined (MONGOC_COMPILATION)
21+
# error "Only <mongoc.h> can be included directly."
22+
#endif
23+
24+
#include <bson.h>
25+
26+
27+
BSON_BEGIN_DECLS
28+
29+
30+
#define MONGOC_READ_CONCERN_LEVEL_LOCAL "local"
31+
#define MONGOC_READ_CONCERN_LEVEL_MAJORITY "majority"
32+
33+
typedef struct _mongoc_read_concern_t mongoc_read_concern_t;
34+
35+
36+
mongoc_read_concern_t *mongoc_read_concern_new (void);
37+
mongoc_read_concern_t *mongoc_read_concern_copy (const mongoc_read_concern_t *read_concern);
38+
void mongoc_read_concern_destroy (mongoc_read_concern_t *read_concern);
39+
const char *mongoc_read_concern_get_level (const mongoc_read_concern_t *read_concern);
40+
bool mongoc_read_concern_set_level (mongoc_read_concern_t *read_concern,
41+
const char *level);
42+
43+
44+
BSON_END_DECLS
45+
46+
47+
#endif /* MONGOC_READ_CONCERN_H */

0 commit comments

Comments
 (0)