|
| 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 | + |
0 commit comments