1 /*
2  * Copyright 2014 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_WRITE_COMMAND_PRIVATE_H
18 #define MONGOC_WRITE_COMMAND_PRIVATE_H
19 
20 #if !defined(MONGOC_COMPILATION)
21 #error "Only <mongoc.h> can be included directly."
22 #endif
23 
24 #include <bson.h>
25 
26 #include "mongoc-client.h"
27 #include "mongoc-error.h"
28 #include "mongoc-write-concern.h"
29 #include "mongoc-server-stream-private.h"
30 
31 
32 BSON_BEGIN_DECLS
33 
34 
35 #define MONGOC_WRITE_COMMAND_DELETE 0
36 #define MONGOC_WRITE_COMMAND_INSERT 1
37 #define MONGOC_WRITE_COMMAND_UPDATE 2
38 
39 
40 typedef enum {
41    MONGOC_BYPASS_DOCUMENT_VALIDATION_FALSE = 0,
42    MONGOC_BYPASS_DOCUMENT_VALIDATION_TRUE = 1 << 0,
43    MONGOC_BYPASS_DOCUMENT_VALIDATION_DEFAULT = 1 << 1,
44 } mongoc_write_bypass_document_validation_t;
45 
46 struct _mongoc_bulk_write_flags_t {
47    bool ordered;
48    mongoc_write_bypass_document_validation_t bypass_document_validation;
49    bool has_collation;
50 };
51 
52 
53 typedef struct {
54    int type;
55    bson_t *documents;
56    uint32_t n_documents;
57    mongoc_bulk_write_flags_t flags;
58    int64_t operation_id;
59    union {
60       struct {
61          bool allow_bulk_op_insert;
62       } insert;
63    } u;
64 } mongoc_write_command_t;
65 
66 
67 typedef struct {
68    /* true after a legacy update prevents us from calculating nModified */
69    bool omit_nModified;
70    uint32_t nInserted;
71    uint32_t nMatched;
72    uint32_t nModified;
73    uint32_t nRemoved;
74    uint32_t nUpserted;
75    /* like [{"index": int, "_id": value}, ...] */
76    bson_t writeErrors;
77    /* like [{"index": int, "code": int, "errmsg": str}, ...] */
78    bson_t upserted;
79    /* like [{"code": 64, "errmsg": "duplicate"}, ...] */
80    uint32_t n_writeConcernErrors;
81    bson_t writeConcernErrors;
82    bool failed;    /* The command failed */
83    bool must_stop; /* The stream may have been disonnected */
84    bson_error_t error;
85    uint32_t upsert_append_count;
86 } mongoc_write_result_t;
87 
88 
89 void
90 _mongoc_write_command_destroy (mongoc_write_command_t *command);
91 void
92 _mongoc_write_command_init_insert (mongoc_write_command_t *command,
93                                    const bson_t *document,
94                                    mongoc_bulk_write_flags_t flags,
95                                    int64_t operation_id,
96                                    bool allow_bulk_op_insert);
97 void
98 _mongoc_write_command_init_delete (mongoc_write_command_t *command,
99                                    const bson_t *selectors,
100                                    const bson_t *opts,
101                                    mongoc_bulk_write_flags_t flags,
102                                    int64_t operation_id);
103 void
104 _mongoc_write_command_init_update (mongoc_write_command_t *command,
105                                    const bson_t *selector,
106                                    const bson_t *update,
107                                    const bson_t *opts,
108                                    mongoc_bulk_write_flags_t flags,
109                                    int64_t operation_id);
110 void
111 _mongoc_write_command_insert_append (mongoc_write_command_t *command,
112                                      const bson_t *document);
113 void
114 _mongoc_write_command_update_append (mongoc_write_command_t *command,
115                                      const bson_t *selector,
116                                      const bson_t *update,
117                                      const bson_t *opts);
118 
119 void
120 _mongoc_write_command_delete_append (mongoc_write_command_t *command,
121                                      const bson_t *selector,
122                                      const bson_t *opts);
123 
124 void
125 _mongoc_write_command_execute (mongoc_write_command_t *command,
126                                mongoc_client_t *client,
127                                mongoc_server_stream_t *server_stream,
128                                const char *database,
129                                const char *collection,
130                                const mongoc_write_concern_t *write_concern,
131                                uint32_t offset,
132                                mongoc_write_result_t *result);
133 void
134 _mongoc_write_result_init (mongoc_write_result_t *result);
135 void
136 _mongoc_write_result_merge (mongoc_write_result_t *result,
137                             mongoc_write_command_t *command,
138                             const bson_t *reply,
139                             uint32_t offset);
140 void
141 _mongoc_write_result_merge_legacy (mongoc_write_result_t *result,
142                                    mongoc_write_command_t *command,
143                                    const bson_t *reply,
144                                    int32_t error_api_version,
145                                    mongoc_error_code_t default_code,
146                                    uint32_t offset);
147 bool
148 _mongoc_write_result_complete (mongoc_write_result_t *result,
149                                int32_t error_api_version,
150                                const mongoc_write_concern_t *wc,
151                                mongoc_error_domain_t err_domain_override,
152                                bson_t *reply,
153                                bson_error_t *error);
154 void
155 _mongoc_write_result_destroy (mongoc_write_result_t *result);
156 
157 
158 BSON_END_DECLS
159 
160 
161 #endif /* MONGOC_WRITE_COMMAND_PRIVATE_H */
162