This repository has been archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.cpp
383 lines (313 loc) · 14.7 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* Copyright 2015 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hphp/runtime/ext/extension.h"
#include "hphp/runtime/base/array-init.h"
#include "hphp/runtime/base/execution-context.h"
#include "hphp/runtime/vm/native-data.h"
#include "bson.h"
#include "utils.h"
#include "mongodb.h"
#include "src/MongoDB/Driver/BulkWrite.h"
#include "src/MongoDB/Driver/Command.h"
#include "src/MongoDB/Driver/Cursor.h"
#include "src/MongoDB/Driver/Manager.h"
#include "src/MongoDB/Driver/Query.h"
#include "src/MongoDB/Driver/ReadConcern.h"
#include "src/MongoDB/Driver/ReadPreference.h"
#include "src/MongoDB/Driver/WriteResult.h"
namespace MongoDriver
{
bool Utils::splitNamespace(HPHP::String ns, char **db, char **col)
{
const char *input = ns.c_str();
const char *dot = strchr(input, '.');
if (!dot) {
return false;
}
if (col) {
*col = strdup(input + (dot - input) + 1);
}
if (db) {
*db = strndup(input, dot - input);
}
return true;
}
HPHP::Object Utils::CreateAndConstruct(HPHP::StaticString classname, const HPHP::Variant &message, const HPHP::Variant &code)
{
static HPHP::Class* c_class;
c_class = HPHP::Unit::lookupClass(classname.get());
assert(c_class);
HPHP::Object inst = HPHP::Object{c_class};
#if HIPPO_HHVM_VERSION >= 31700
HPHP::g_context->invokeFunc(c_class->getCtor(), HPHP::make_packed_array(message, code), inst.get());
#else
HPHP::TypedValue ret;
HPHP::g_context->invokeFunc(&ret, c_class->getCtor(), HPHP::make_packed_array(message, code), inst.get());
HPHP::tvRefcountedDecRef(&ret);
#endif
return inst;
}
const HPHP::StaticString s_MongoDriverExceptionAuthenticationException_className("MongoDB\\Driver\\Exception\\AuthenticationException");
const HPHP::StaticString s_MongoDriverExceptionBulkWriteException_className("MongoDB\\Driver\\Exception\\BulkWriteException");
const HPHP::StaticString s_MongoDriverExceptionConnectionException_className("MongoDB\\Driver\\Exception\\ConnectionException");
const HPHP::StaticString s_MongoDriverExceptionConnectionTimeoutException_className("MongoDB\\Driver\\Exception\\ConnectionTimeoutException");
const HPHP::StaticString s_MongoDriverExceptionExecutionTimeoutException_className("MongoDB\\Driver\\Exception\\ExecutionTimeoutException");
const HPHP::StaticString s_MongoDriverExceptionInvalidArgumentException_className("MongoDB\\Driver\\Exception\\InvalidArgumentException");
const HPHP::StaticString s_MongoDriverExceptionLogicException_className("MongoDB\\Driver\\Exception\\LogicException");
const HPHP::StaticString s_MongoDriverExceptionRuntimeException_className("MongoDB\\Driver\\Exception\\RuntimeException");
const HPHP::StaticString s_MongoDriverExceptionUnexpectedValueException_className("MongoDB\\Driver\\Exception\\UnexpectedValueException");
const HPHP::StaticString s_MongoDriverExceptionWriteException_className("MongoDB\\Driver\\Exception\\WriteException");
HPHP::Object Utils::throwInvalidArgumentException(char *errormessage)
{
return Utils::CreateAndConstruct(s_MongoDriverExceptionInvalidArgumentException_className, HPHP::Variant(errormessage), HPHP::Variant((uint64_t) 0));
}
HPHP::Object Utils::throwInvalidArgumentException(HPHP::String errormessage)
{
return Utils::CreateAndConstruct(s_MongoDriverExceptionInvalidArgumentException_className, HPHP::Variant(errormessage), HPHP::Variant((uint64_t) 0));
}
HPHP::Object Utils::throwBulkWriteException(HPHP::String errormessage)
{
return Utils::CreateAndConstruct(s_MongoDriverExceptionBulkWriteException_className, HPHP::Variant(errormessage), HPHP::Variant((uint64_t) 0));
}
HPHP::Object Utils::throwLogicException(char *errormessage)
{
return Utils::CreateAndConstruct(s_MongoDriverExceptionLogicException_className, HPHP::Variant(errormessage), HPHP::Variant((uint64_t) 0));
}
HPHP::Object Utils::throwRunTimeException(char *errormessage)
{
return Utils::CreateAndConstruct(s_MongoDriverExceptionRuntimeException_className, HPHP::Variant(errormessage), HPHP::Variant((uint64_t) 0));
}
HPHP::Object Utils::throwRunTimeException(HPHP::String errormessage)
{
return Utils::CreateAndConstruct(s_MongoDriverExceptionRuntimeException_className, HPHP::Variant(errormessage), HPHP::Variant((uint64_t) 0));
}
HPHP::Object Utils::throwUnexpectedValueException(char *errormessage)
{
return Utils::CreateAndConstruct(s_MongoDriverExceptionUnexpectedValueException_className, HPHP::Variant(errormessage), HPHP::Variant((uint64_t) 0));
}
HPHP::Object Utils::throwUnexpectedValueException(HPHP::String errormessage)
{
return Utils::CreateAndConstruct(s_MongoDriverExceptionUnexpectedValueException_className, HPHP::Variant(errormessage), HPHP::Variant((uint64_t) 0));
}
HPHP::Object Utils::throwExceptionFromBsonError(bson_error_t *error)
{
switch (error->code) {
case 50: /* ExceededTimeLimit */
return Utils::CreateAndConstruct(s_MongoDriverExceptionExecutionTimeoutException_className, HPHP::Variant(error->message), HPHP::Variant((uint64_t) error->code));
case MONGOC_ERROR_STREAM_SOCKET:
case MONGOC_ERROR_SERVER_SELECTION_FAILURE:
return Utils::CreateAndConstruct(s_MongoDriverExceptionConnectionTimeoutException_className, HPHP::Variant(error->message), HPHP::Variant((uint64_t) error->code));
case MONGOC_ERROR_CLIENT_AUTHENTICATE:
return Utils::CreateAndConstruct(s_MongoDriverExceptionAuthenticationException_className, HPHP::Variant(error->message), HPHP::Variant((uint64_t) error->code));
case MONGOC_ERROR_STREAM_INVALID_TYPE:
case MONGOC_ERROR_STREAM_INVALID_STATE:
case MONGOC_ERROR_STREAM_NAME_RESOLUTION:
case MONGOC_ERROR_STREAM_CONNECT:
case MONGOC_ERROR_STREAM_NOT_ESTABLISHED:
return Utils::CreateAndConstruct(s_MongoDriverExceptionConnectionException_className, HPHP::Variant(error->message), HPHP::Variant((uint64_t) error->code));
case MONGOC_ERROR_CLIENT_NOT_READY:
case MONGOC_ERROR_CLIENT_TOO_BIG:
case MONGOC_ERROR_CLIENT_TOO_SMALL:
case MONGOC_ERROR_CLIENT_GETNONCE:
case MONGOC_ERROR_CLIENT_NO_ACCEPTABLE_PEER:
case MONGOC_ERROR_CLIENT_IN_EXHAUST:
case MONGOC_ERROR_PROTOCOL_INVALID_REPLY:
case MONGOC_ERROR_PROTOCOL_BAD_WIRE_VERSION:
case MONGOC_ERROR_CURSOR_INVALID_CURSOR:
case MONGOC_ERROR_QUERY_FAILURE:
/*case MONGOC_ERROR_PROTOCOL_ERROR:*/
case MONGOC_ERROR_BSON_INVALID:
case MONGOC_ERROR_MATCHER_INVALID:
case MONGOC_ERROR_NAMESPACE_INVALID:
case MONGOC_ERROR_COLLECTION_INSERT_FAILED:
case MONGOC_ERROR_GRIDFS_INVALID_FILENAME:
case MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND:
case MONGOC_ERROR_QUERY_NOT_TAILABLE:
return Utils::CreateAndConstruct(s_MongoDriverExceptionRuntimeException_className, HPHP::Variant(error->message), HPHP::Variant((uint64_t) error->code));
case MONGOC_ERROR_COMMAND_INVALID_ARG:
return Utils::CreateAndConstruct(s_MongoDriverExceptionInvalidArgumentException_className, HPHP::Variant(error->message), HPHP::Variant((uint64_t) error->code));
}
switch (error->domain) {
case MONGOC_ERROR_CLIENT:
case MONGOC_ERROR_STREAM:
case MONGOC_ERROR_PROTOCOL:
case MONGOC_ERROR_CURSOR:
case MONGOC_ERROR_QUERY:
case MONGOC_ERROR_INSERT:
case MONGOC_ERROR_SASL:
case MONGOC_ERROR_BSON:
case MONGOC_ERROR_MATCHER:
case MONGOC_ERROR_NAMESPACE:
case MONGOC_ERROR_COMMAND:
case MONGOC_ERROR_COLLECTION:
case MONGOC_ERROR_GRIDFS:
/* FIXME: We don't have the Exceptions mocked yet.. */
#if 0
return phongo_ce_mongo_connection_exception;
#endif
default:
return Utils::CreateAndConstruct(s_MongoDriverExceptionRuntimeException_className, HPHP::Variant(error->message), HPHP::Variant((uint64_t) error->code));
}
}
HPHP::Object Utils::doExecuteBulkWrite(const HPHP::String ns, mongoc_client_t *client, int server_id, const HPHP::Object bulk, const mongoc_write_concern_t *write_concern)
{
HPHP::MongoDBDriverBulkWriteData* bulk_data = HPHP::Native::data<HPHP::MongoDBDriverBulkWriteData>(bulk.get());
bson_error_t error;
int success;
bson_t reply = BSON_INITIALIZER;
if (bulk_data->m_executed == true) {
throw throwBulkWriteException("BulkWrite objects may only be executed once and this instance has already been executed");
}
if (bulk_data->m_database) {
free(bulk_data->m_database);
}
if (bulk_data->m_collection) {
free(bulk_data->m_collection);
}
/* Prepare */
if (!MongoDriver::Utils::splitNamespace(ns, &bulk_data->m_database, &bulk_data->m_collection)) {
throw throwInvalidArgumentException("Invalid namespace provided: " + ns);
}
/* Setup operation */
mongoc_bulk_operation_set_database(bulk_data->m_bulk, bulk_data->m_database);
mongoc_bulk_operation_set_collection(bulk_data->m_bulk, bulk_data->m_collection);
mongoc_bulk_operation_set_client(bulk_data->m_bulk, client);
/* Deal with write concerns */
if (write_concern) {
mongoc_bulk_operation_set_write_concern(bulk_data->m_bulk, write_concern);
} else {
write_concern = mongoc_client_get_write_concern(client);
}
/* Handle server hint */
if (server_id > 0) {
mongoc_bulk_operation_set_hint(bulk_data->m_bulk, server_id);
}
/* Run operation */
success = mongoc_bulk_operation_execute(bulk_data->m_bulk, &reply, &error);
bulk_data->m_executed = true;
/* Prepare result */
HPHP::Object obj = HPHP::hippo_write_result_init(&reply, &error, client, mongoc_bulk_operation_get_hint(bulk_data->m_bulk), success, write_concern);
bson_destroy(&reply);
return obj;
}
/* Advance the cursor and return whether there is an error. On error, the
* cursor will be destroyed and an exception will be thrown. */
static void hippo_advance_cursor_and_check_for_error(mongoc_cursor_t *cursor)
{
const bson_t *doc;
/* Check for errors */
if (!mongoc_cursor_next(cursor, &doc)) {
bson_error_t error;
/* Could simply be no docs, which is not an error */
if (mongoc_cursor_error(cursor, &error)) {
mongoc_cursor_destroy(cursor);
throw Utils::throwExceptionFromBsonError(&error);
}
}
}
const HPHP::StaticString
s_filter("filter"),
s_opts("opts"),
s_readConcernLevel("readConcernLevel");
HPHP::Object Utils::doExecuteCommand(const char *db, mongoc_client_t *client, int server_id, const HPHP::Object &command, const HPHP::Variant &readPreference)
{
mongoc_cursor_t *cursor;
bson_iter_t iter;
mongoc_read_prefs_t *read_preference = NULL;
bson_t *bson;
auto zcommand = command->o_get(HPHP::s_MongoDBDriverManager_command, false, HPHP::s_MongoDriverCommand_className);
HPHP::VariantToBsonConverter converter(zcommand, HIPPO_BSON_NO_FLAGS);
bson = bson_new();
converter.convert(bson);
if (!readPreference.isNull()) {
HPHP::Object o_rp = readPreference.toObject();
HPHP::MongoDBDriverReadPreferenceData* data = HPHP::Native::data<HPHP::MongoDBDriverReadPreferenceData>(o_rp);
read_preference = data->m_read_preference;
}
/* Run operation */
cursor = mongoc_client_command(client, db, MONGOC_QUERY_NONE, 0, 1, 0, bson, NULL, read_preference);
/* Handle server hint */
if (server_id > 0 && !mongoc_cursor_set_hint(cursor, server_id)) {
throw throwRunTimeException("Could not set cursor server_id");
}
/* This throws an exception upon error */
hippo_advance_cursor_and_check_for_error(cursor);
if (bson_iter_init_find(&iter, mongoc_cursor_current(cursor), "cursor") && BSON_ITER_HOLDS_DOCUMENT(&iter)) {
mongoc_cursor_t *cmd_cursor;
/* According to mongoc_cursor_new_from_command_reply(), the reply bson_t
* is ultimately destroyed on both success and failure. Use bson_copy()
* to create a writable copy of the const bson_t we fetched above. */
cmd_cursor = mongoc_cursor_new_from_command_reply(client, bson_copy(mongoc_cursor_current(cursor)), mongoc_cursor_get_hint(cursor));
mongoc_cursor_destroy(cursor);
/* This throws an exception upon error */
hippo_advance_cursor_and_check_for_error(cmd_cursor);
return HPHP::hippo_cursor_init_for_command(cmd_cursor, client, db, command, readPreference);
}
/* Prepare result */
HPHP::Object obj = HPHP::hippo_cursor_init_for_command(cursor, client, db, command, readPreference);
/* Destroy */
bson_destroy(bson);
return obj;
}
HPHP::Object Utils::doExecuteQuery(const HPHP::String ns, mongoc_client_t *client, int server_id, const HPHP::Object &query, const HPHP::Variant &readPreference)
{
bson_t *bfilter = NULL, *bopts = NULL;
mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
char *dbname;
char *collname;
mongoc_read_prefs_t *read_preference = NULL;
/* Prepare */
if (!MongoDriver::Utils::splitNamespace(ns, &dbname, &collname)) {
throw throwInvalidArgumentException("Invalid namespace provided: " + ns);
}
/* Get query properties */
auto zfilter = query->o_get(s_filter, false, HPHP::s_MongoDriverQuery_className);
auto zopts = query->o_get(s_opts, false, HPHP::s_MongoDriverQuery_className);
auto zrc = query->o_get(s_readConcernLevel, false, HPHP::s_MongoDriverQuery_className);
HPHP::VariantToBsonConverter filter_converter(zfilter, HIPPO_BSON_NO_FLAGS);
bfilter = bson_new();
filter_converter.convert(bfilter);
HPHP::VariantToBsonConverter opts_converter(zopts, HIPPO_BSON_NO_FLAGS);
bopts = bson_new();
opts_converter.convert(bopts);
/* Get collection */
collection = mongoc_client_get_collection(client, dbname, collname);
/* Process options to set on collection */
if (!zrc.isNull()) {
mongoc_read_concern_t *rc;
rc = mongoc_read_concern_new();
mongoc_read_concern_set_level(rc, zrc.toString().c_str());
mongoc_collection_set_read_concern(collection, rc);
}
if (!readPreference.isNull()) {
HPHP::Object o_rp = readPreference.toObject();
HPHP::MongoDBDriverReadPreferenceData* data = HPHP::Native::data<HPHP::MongoDBDriverReadPreferenceData>(o_rp);
read_preference = data->m_read_preference;
}
/* Run query and get cursor */
cursor = mongoc_collection_find_with_opts(collection, bfilter, bopts, read_preference);
mongoc_collection_destroy(collection);
/* Handle server hint */
if (server_id > 0 && !mongoc_cursor_set_hint(cursor, server_id)) {
throw throwRunTimeException("Could not set cursor server_id");
}
/* This throws an exception upon error */
hippo_advance_cursor_and_check_for_error(cursor);
/* Prepare result */
return HPHP::hippo_cursor_init_for_query(cursor, client, ns, query, readPreference);
}
}