-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,353 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2013 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 <bson/bson.h> | ||
|
||
#define QUERY(...) ELE_QUERY, __VA_ARGS__, NULL | ||
#define SORT(...) ELE_SORT, __VA_ARGS__, NULL | ||
#define LIMIT(var) ELE_LIMIT, (var) | ||
#define COL_VIEW_CREATE(...) col_view_create ("", __VA_ARGS__, ELE_END) | ||
|
||
typedef enum ele { | ||
ELE_SORT, | ||
ELE_LIMIT, | ||
ELE_QUERY, | ||
ELE_END, | ||
} ele_t; | ||
|
||
bson_t * | ||
col_view_create (const char *stub, ...) | ||
{ | ||
bson_t *bson; | ||
va_list ap; | ||
ele_t type; | ||
int keep_going = 1; | ||
|
||
bcon_append_ctx_t ctx; | ||
bcon_append_ctx_init (&ctx); | ||
|
||
va_start (ap, stub); | ||
|
||
bson = bson_new (); | ||
|
||
while (keep_going) { | ||
type = va_arg (ap, ele_t); | ||
|
||
switch (type) { | ||
case ELE_SORT: | ||
BCON_APPEND_CTX (bson, &ctx, "sort", "{"); | ||
bcon_append_ctx_va (bson, &ctx, &ap); | ||
BCON_APPEND_CTX (bson, &ctx, "}"); | ||
break; | ||
case ELE_LIMIT: { | ||
int i = va_arg (ap, int); | ||
BCON_APPEND_CTX (bson, &ctx, "limit", BCON_INT32 (i)); | ||
break; | ||
} | ||
case ELE_QUERY: | ||
BCON_APPEND_CTX (bson, &ctx, "query", "{"); | ||
bcon_append_ctx_va (bson, &ctx, &ap); | ||
BCON_APPEND_CTX (bson, &ctx, "}"); | ||
break; | ||
case ELE_END: | ||
keep_going = 0; | ||
break; | ||
default: | ||
BSON_ASSERT (0); | ||
break; | ||
} | ||
} | ||
|
||
va_end (ap); | ||
|
||
return bson; | ||
} | ||
|
||
int | ||
main (void) | ||
{ | ||
bson_t *bson; | ||
char *json; | ||
|
||
bson = COL_VIEW_CREATE (SORT ("a", BCON_INT32 (1)), QUERY ("hello", "world"), LIMIT (10)); | ||
|
||
json = bson_as_canonical_extended_json (bson, NULL); | ||
printf ("%s\n", json); | ||
bson_free (json); | ||
|
||
bson_destroy (bson); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2013-2014 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 <bson/bson.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/* | ||
* This is a test for comparing the performance of BCON to regular | ||
* bson_append*() function calls. | ||
* | ||
* Maybe run the following a few times to get an idea of the performance | ||
* implications of using BCON. Generally, it's fast enough to be very | ||
* useful and result in easier to read BSON code. | ||
* | ||
* time ./bcon-speed 100000 y | ||
* time ./bcon-speed 100000 n | ||
*/ | ||
|
||
|
||
int | ||
main (int argc, char *argv[]) | ||
{ | ||
int i; | ||
int n; | ||
int bcon; | ||
bson_t bson, foo, bar, baz; | ||
bson_init (&bson); | ||
|
||
if (argc != 3) { | ||
fprintf (stderr, | ||
"usage: bcon-speed NUM_ITERATIONS [y|n]\n" | ||
"\n" | ||
" y = perform speed tests with bcon\n" | ||
" n = perform speed tests with bson_append\n" | ||
"\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
BSON_ASSERT (argc == 3); | ||
|
||
n = atoi (argv[1]); | ||
bcon = (argv[2][0] == 'y') ? 1 : 0; | ||
|
||
for (i = 0; i < n; i++) { | ||
if (bcon) { | ||
BCON_APPEND ( | ||
&bson, "foo", "{", "bar", "{", "baz", "[", BCON_INT32 (1), BCON_INT32 (2), BCON_INT32 (3), "]", "}", "}"); | ||
} else { | ||
bson_append_document_begin (&bson, "foo", -1, &foo); | ||
bson_append_document_begin (&foo, "bar", -1, &bar); | ||
bson_append_array_begin (&bar, "baz", -1, &baz); | ||
bson_append_int32 (&baz, "0", -1, 1); | ||
bson_append_int32 (&baz, "1", -1, 2); | ||
bson_append_int32 (&baz, "2", -1, 3); | ||
bson_append_array_end (&bar, &baz); | ||
bson_append_document_end (&foo, &bar); | ||
bson_append_document_end (&bson, &foo); | ||
} | ||
|
||
bson_reinit (&bson); | ||
} | ||
|
||
bson_destroy (&bson); | ||
|
||
return 0; | ||
} |
129 changes: 129 additions & 0 deletions
129
source/docs-libbson/includes/examples/bson-check-depth.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2018-present 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. | ||
*/ | ||
|
||
/* -- sphinx-include-start -- */ | ||
/* Reports the maximum nested depth of a BSON document. */ | ||
#include <bson/bson.h> | ||
|
||
#include <assert.h> | ||
#include <inttypes.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct { | ||
uint32_t depth; | ||
uint32_t max_depth; | ||
bool valid; | ||
} check_depth_t; | ||
|
||
bool | ||
_check_depth_document (const bson_iter_t *iter, const char *key, const bson_t *v_document, void *data); | ||
|
||
static const bson_visitor_t check_depth_funcs = { | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
_check_depth_document, | ||
_check_depth_document, | ||
NULL, | ||
}; | ||
|
||
bool | ||
_check_depth_document (const bson_iter_t *iter, const char *key, const bson_t *v_document, void *data) | ||
{ | ||
check_depth_t *state = (check_depth_t *) data; | ||
bson_iter_t child; | ||
|
||
BSON_UNUSED (iter); | ||
BSON_UNUSED (key); | ||
|
||
if (!bson_iter_init (&child, v_document)) { | ||
fprintf (stderr, "corrupt\n"); | ||
return true; /* cancel */ | ||
} | ||
|
||
state->depth++; | ||
if (state->depth > state->max_depth) { | ||
state->valid = false; | ||
return true; /* cancel */ | ||
} | ||
|
||
bson_iter_visit_all (&child, &check_depth_funcs, state); | ||
state->depth--; | ||
return false; /* continue */ | ||
} | ||
|
||
void | ||
check_depth (const bson_t *bson, uint32_t max_depth) | ||
{ | ||
bson_iter_t iter; | ||
check_depth_t state = {0}; | ||
|
||
if (!bson_iter_init (&iter, bson)) { | ||
fprintf (stderr, "corrupt\n"); | ||
} | ||
|
||
state.valid = true; | ||
state.max_depth = max_depth; | ||
_check_depth_document (&iter, NULL, bson, &state); | ||
if (!state.valid) { | ||
printf ("document exceeds maximum depth of %" PRIu32 "\n", state.max_depth); | ||
} else { | ||
char *as_json = bson_as_canonical_extended_json (bson, NULL); | ||
printf ("document %s ", as_json); | ||
printf ("is valid\n"); | ||
bson_free (as_json); | ||
} | ||
} | ||
|
||
int | ||
main (int argc, char **argv) | ||
{ | ||
bson_reader_t *bson_reader; | ||
const bson_t *bson; | ||
bool reached_eof; | ||
bson_error_t error; | ||
|
||
if (argc != 3) { | ||
fprintf (stderr, "usage: %s FILE MAX_DEPTH\n", argv[0]); | ||
fprintf (stderr, "Checks that the depth of the BSON contained in FILE\n"); | ||
fprintf (stderr, "does not exceed MAX_DEPTH\n"); | ||
} | ||
|
||
const char *const filename = argv[1]; | ||
const int max_depth = atoi (argv[2]); | ||
|
||
bson_reader = bson_reader_new_from_file (filename, &error); | ||
if (!bson_reader) { | ||
printf ("could not read %s: %s\n", filename, error.message); | ||
return 1; | ||
} | ||
|
||
BSON_ASSERT (bson_in_range_signed (uint32_t, max_depth)); | ||
|
||
while ((bson = bson_reader_read (bson_reader, &reached_eof))) { | ||
check_depth (bson, (uint32_t) max_depth); | ||
} | ||
|
||
if (!reached_eof) { | ||
printf ("error reading BSON\n"); | ||
} | ||
|
||
bson_reader_destroy (bson_reader); | ||
return 0; | ||
} |
Oops, something went wrong.