3 * @brief BSON Declarations
6 /* Copyright 2009-2012 10gen Inc.
7 * Copyright (C) 2012-2015 Softmotions Ltd <info@softmotions.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
28 #if ! defined(__cplusplus)
34 #define BSON_IS_NUM_TYPE(atype) (atype == BSON_INT || atype == BSON_LONG || atype == BSON_DOUBLE)
35 #define BSON_IS_STRING_TYPE(atype) ((atype) == BSON_STRING || (atype) == BSON_SYMBOL)
42 //Maximum field path length allocated on stack
43 #define BSON_MAX_FPATH_LEN (255)
46 BSON_SIZE_OVERFLOW = 1 /**< Trying to create a BSON object larger than INT_MAX. */
49 enum bson_validity_t {
50 BSON_VALID = 0, /**< BSON is valid and UTF-8 compliant. */
51 BSON_NOT_UTF8 = (1 << 1), /**< A key or a string is not valid UTF-8. */
52 BSON_FIELD_HAS_DOT = (1 << 2), /**< Warning: key contains '.' character. */
53 BSON_FIELD_INIT_DOLLAR = (1 << 3), /**< Warning: key starts with '$' character. */
54 BSON_ALREADY_FINISHED = (1 << 4), /**< Trying to modify a finished BSON object. */
55 BSON_ERROR_ANY = (1 << 5) /**< Unspecified error */
58 enum bson_binary_subtype_t {
61 BSON_BIN_BINARY_OLD = 2,
68 BSON_FLAG_QUERY_MODE = 1,
69 BSON_FLAG_STACK_ALLOCATED = 1 << 1 /**< If it set BSON data is allocated on stack and realloc should deal with this case */
85 BSON_DBREF = 12, /**< Deprecated. */
94 typedef int bson_bool_t;
102 char *data; /**< Pointer to a block of data in this BSON object. */
103 char *cur; /**< Pointer to the current position. */
104 int dataSize; /**< The number of bytes allocated to char *data. */
105 bson_bool_t finished; /**< When finished, the BSON object can no longer be modified. */
106 int stack[32]; /**< A stack used to keep track of nested BSON elements. */
107 int stackPos; /**< Index of current stack position. */
108 int err; /**< Bitfield representing errors or warnings on this buffer */
109 char *errstr; /**< A string representation of the most recent error or warning. */
121 typedef int64_t bson_date_t; /* milliseconds since epoch UTC */
124 int i; /* increment */
125 int t; /* time in seconds */
129 EJDB_EXPORT const char* bson_first_errormsg(bson *bson);
132 #define BSON_ITERATOR_FROM_BUFFER(_bs_I, _bs_B) \
133 (_bs_I)->cur = ((char*) (_bs_B)) + 4; \
136 #define BSON_ITERATOR_SUBITERATOR(_bs_I, _bs_S) \
137 BSON_ITERATOR_FROM_BUFFER((_bs_S), bson_iterator_value(_bs_I))
139 #define BSON_ITERATOR_TYPE(_bs_I) \
140 ((bson_type) (_bs_I)->cur[0])
142 #define BSON_ITERATOR_KEY(_bs_I) \
145 #define BSON_ITERATOR_INIT(_bs_I, _bs) \
146 (_bs_I)->cur = (_bs)->data + 4; \
149 /* ----------------------------
151 ------------------------------ */
153 EJDB_EXPORT bson* bson_create(void);
154 EJDB_EXPORT void bson_dispose(bson* b);
157 * Size of a BSON object.
159 * @param b the BSON object.
163 EJDB_EXPORT int bson_size(const bson *b);
164 EJDB_EXPORT int bson_size2(const void *bsdata);
165 EJDB_EXPORT int bson_buffer_size(const bson *b);
168 * Return a pointer to the raw buffer stored by this bson object.
170 * @param b a BSON object
172 EJDB_EXPORT const char *bson_data(const bson *b);
173 EJDB_EXPORT const char* bson_data2(const bson *b, int *bsize);
176 * Print a string representation of a BSON object.
178 * @param bson the raw data to print.
179 * @param depth the depth to recurse the object.x
181 EJDB_EXPORT void bson_print_raw(const char *bson, int depth);
184 * Advance a bson_iterator to the named field.
186 * @param it the bson_iterator to use.
187 * @param obj the BSON object to use.
188 * @param name the name of the field to find.
190 * @return the type of the found object or BSON_EOO if it is not found.
192 EJDB_EXPORT bson_type bson_find(bson_iterator *it, const bson *obj, const char *name);
194 EJDB_EXPORT bson_type bson_find_from_buffer(bson_iterator *it, const char *buffer, const char *name);
196 typedef struct { /**< Find field path context */
199 bson_iterator *input;
202 int mpos; /**< Array index of the first matched array field */
203 int dpos; /**< Position of `$` in array projection fieldpath. */
208 * Advance specified iterator 'it' to field value pointing by 'fieldpath'/
209 * Field path string format: 'field1.nestedfield1.nestedfield.2'.
210 * If specified field not found BSON_EOO will be returned.
212 * @param fieldpath Path specification to the BSON field.
213 * @param it the bson_iterator to use.
214 * @return the type of the found object or BSON_EOO if it is not found.
216 EJDB_EXPORT bson_type bson_find_fieldpath_value(const char *fieldpath, bson_iterator *it);
217 EJDB_EXPORT bson_type bson_find_fieldpath_value2(const char *fpath, int fplen, bson_iterator *it);
218 EJDB_EXPORT bson_type bson_find_fieldpath_value3(FFPCTX* ffctx);
221 * BSON object visitor
222 * @param it bson iterator to traverse
223 * @param visitor Visitor function
224 * @param op Opaque data for visitor
227 BSON_TRAVERSE_ARRAYS_EXCLUDED = 1,
228 BSON_TRAVERSE_OBJECTS_EXCLUDED = 1 << 1
229 } bson_traverse_flags_t;
233 BSON_VCMD_TERMINATE = 1,
234 BSON_VCMD_SKIP_NESTED = 1 << 1,
235 BSON_VCMD_SKIP_AFTER = 1 << 2
236 } bson_visitor_cmd_t;
238 typedef bson_visitor_cmd_t(*BSONVISITOR)(const char *ipath, int ipathlen,
239 const char *key, int keylen,
240 const bson_iterator *it,
241 bool after, void *op);
243 EJDB_EXPORT void bson_visit_fields(bson_iterator *it,
244 bson_traverse_flags_t flags,
245 BSONVISITOR visitor, void *op);
248 EJDB_EXPORT bson_iterator* bson_iterator_create(void);
249 EJDB_EXPORT void bson_iterator_dispose(bson_iterator*);
251 * Initialize a bson_iterator.
253 * @param i the bson_iterator to initialize.
254 * @param bson the BSON object to associate with the iterator.
256 EJDB_EXPORT void bson_iterator_init(bson_iterator *i, const bson *b);
259 * Initialize a bson iterator from a const char* buffer. Note
260 * that this is mostly used internally.
262 * @param i the bson_iterator to initialize.
263 * @param buffer the buffer to point to.
265 EJDB_EXPORT void bson_iterator_from_buffer(bson_iterator *i, const char *buffer);
267 /* more returns true for eoo. best to loop with bson_iterator_next(&it) */
269 * Check to see if the bson_iterator has more data.
271 * @param i the iterator.
273 * @return returns true if there is more data.
275 EJDB_EXPORT bson_bool_t bson_iterator_more(const bson_iterator *i);
278 * Point the iterator at the next BSON object.
280 * @param i the bson_iterator.
282 * @return the type of the next BSON object.
284 EJDB_EXPORT bson_type bson_iterator_next(bson_iterator *i);
287 * Get the type of the BSON object currently pointed to by the iterator.
289 * @param i the bson_iterator
291 * @return the type of the current BSON object.
293 EJDB_EXPORT bson_type bson_iterator_type(const bson_iterator *i);
296 * Get the key of the BSON object currently pointed to by the iterator.
298 * @param i the bson_iterator
300 * @return the key of the current BSON object.
302 EJDB_EXPORT const char *bson_iterator_key(const bson_iterator *i);
305 * Get the value of the BSON object currently pointed to by the iterator.
307 * @param i the bson_iterator
309 * @return the value of the current BSON object.
311 EJDB_EXPORT const char *bson_iterator_value(const bson_iterator *i);
313 /* these convert to the right type (return 0 if non-numeric) */
315 * Get the double value of the BSON object currently pointed to by the
318 * @param i the bson_iterator
320 * @return the value of the current BSON object.
322 EJDB_EXPORT double bson_iterator_double(const bson_iterator *i);
325 * Get the int value of the BSON object currently pointed to by the iterator.
327 * @param i the bson_iterator
329 * @return the value of the current BSON object.
331 EJDB_EXPORT int bson_iterator_int(const bson_iterator *i);
334 * Get the long value of the BSON object currently pointed to by the iterator.
336 * @param i the bson_iterator
338 * @return the value of the current BSON object.
340 EJDB_EXPORT int64_t bson_iterator_long(const bson_iterator *i);
342 /* return the bson timestamp as a whole or in parts */
344 * Get the timestamp value of the BSON object currently pointed to by
347 * @param i the bson_iterator
349 * @return the value of the current BSON object.
351 EJDB_EXPORT bson_timestamp_t bson_iterator_timestamp(const bson_iterator *i);
352 EJDB_EXPORT int bson_iterator_timestamp_time(const bson_iterator *i);
353 EJDB_EXPORT int bson_iterator_timestamp_increment(const bson_iterator *i);
356 * Get the boolean value of the BSON object currently pointed to by
359 * @param i the bson_iterator
361 * @return the value of the current BSON object.
363 /* false: boolean false, 0 in any type, or null */
364 /* true: anything else (even empty strings and objects) */
365 EJDB_EXPORT bson_bool_t bson_iterator_bool(const bson_iterator *i);
368 * Get the double value of the BSON object currently pointed to by the
369 * iterator. Assumes the correct type is used.
371 * @param i the bson_iterator
373 * @return the value of the current BSON object.
375 /* these assume you are using the right type */
376 EJDB_EXPORT double bson_iterator_double_raw(const bson_iterator *i);
379 * Get the int value of the BSON object currently pointed to by the
380 * iterator. Assumes the correct type is used.
382 * @param i the bson_iterator
384 * @return the value of the current BSON object.
386 EJDB_EXPORT int bson_iterator_int_raw(const bson_iterator *i);
389 * Get the long value of the BSON object currently pointed to by the
390 * iterator. Assumes the correct type is used.
392 * @param i the bson_iterator
394 * @return the value of the current BSON object.
396 EJDB_EXPORT int64_t bson_iterator_long_raw(const bson_iterator *i);
399 * Get the bson_bool_t value of the BSON object currently pointed to by the
400 * iterator. Assumes the correct type is used.
402 * @param i the bson_iterator
404 * @return the value of the current BSON object.
406 EJDB_EXPORT bson_bool_t bson_iterator_bool_raw(const bson_iterator *i);
409 * Get the bson_oid_t value of the BSON object currently pointed to by the
412 * @param i the bson_iterator
414 * @return the value of the current BSON object.
416 EJDB_EXPORT bson_oid_t *bson_iterator_oid(const bson_iterator *i);
419 * Get the string value of the BSON object currently pointed to by the
422 * @param i the bson_iterator
424 * @return the value of the current BSON object.
426 /* these can also be used with bson_code and bson_symbol*/
427 EJDB_EXPORT const char *bson_iterator_string(const bson_iterator *i);
430 * Get the string length of the BSON object currently pointed to by the
433 * @param i the bson_iterator
435 * @return the length of the current BSON object.
437 EJDB_EXPORT int bson_iterator_string_len(const bson_iterator *i);
440 * Get the code value of the BSON object currently pointed to by the
441 * iterator. Works with bson_code, bson_codewscope, and BSON_STRING
442 * returns NULL for everything else.
444 * @param i the bson_iterator
446 * @return the code value of the current BSON object.
448 /* works with bson_code, bson_codewscope, and BSON_STRING */
449 /* returns NULL for everything else */
450 EJDB_EXPORT const char *bson_iterator_code(const bson_iterator *i);
453 * Calls bson_empty on scope if not a bson_codewscope
455 * @param i the bson_iterator.
456 * @param scope the bson scope.
458 /* calls bson_empty on scope if not a bson_codewscope */
459 EJDB_EXPORT void bson_iterator_code_scope(const bson_iterator *i, bson *scope);
462 * Get the date value of the BSON object currently pointed to by the
465 * @param i the bson_iterator
467 * @return the date value of the current BSON object.
469 /* both of these only work with bson_date */
470 EJDB_EXPORT bson_date_t bson_iterator_date(const bson_iterator *i);
473 * Get the time value of the BSON object currently pointed to by the
476 * @param i the bson_iterator
478 * @return the time value of the current BSON object.
480 EJDB_EXPORT time_t bson_iterator_time_t(const bson_iterator *i);
483 * Get the length of the BSON binary object currently pointed to by the
486 * @param i the bson_iterator
488 * @return the length of the current BSON binary object.
490 EJDB_EXPORT int bson_iterator_bin_len(const bson_iterator *i);
493 * Get the type of the BSON binary object currently pointed to by the
496 * @param i the bson_iterator
498 * @return the type of the current BSON binary object.
500 EJDB_EXPORT char bson_iterator_bin_type(const bson_iterator *i);
503 * Get the value of the BSON binary object currently pointed to by the
506 * @param i the bson_iterator
508 * @return the value of the current BSON binary object.
510 EJDB_EXPORT const char *bson_iterator_bin_data(const bson_iterator *i);
513 * Get the value of the BSON regex object currently pointed to by the
516 * @param i the bson_iterator
518 * @return the value of the current BSON regex object.
520 EJDB_EXPORT const char *bson_iterator_regex(const bson_iterator *i);
523 * Get the options of the BSON regex object currently pointed to by the
526 * @param i the bson_iterator.
528 * @return the options of the current BSON regex object.
530 EJDB_EXPORT const char *bson_iterator_regex_opts(const bson_iterator *i);
532 /* these work with BSON_OBJECT and BSON_ARRAY */
534 * Get the BSON subobject currently pointed to by the
537 * @param i the bson_iterator.
538 * @param sub the BSON subobject destination.
540 EJDB_EXPORT void bson_iterator_subobject(const bson_iterator *i, bson *sub);
543 * Get a bson_iterator that on the BSON subobject.
545 * @param i the bson_iterator.
546 * @param sub the iterator to point at the BSON subobject.
548 EJDB_EXPORT void bson_iterator_subiterator(const bson_iterator *i, bson_iterator *sub);
550 /* str must be at least 24 hex chars + null byte */
552 * Create a bson_oid_t from a string.
554 * @param oid the bson_oid_t destination.
555 * @param str a null terminated string comprised of at least 24 hex chars.
557 EJDB_EXPORT void bson_oid_from_string(bson_oid_t *oid, const char *str);
560 * Create a string representation of the bson_oid_t.
562 * @param oid the bson_oid_t source.
563 * @param str the string representation destination.
565 EJDB_EXPORT void bson_oid_to_string(const bson_oid_t *oid, char *str);
568 * Create a bson_oid object.
570 * @param oid the destination for the newly created bson_oid_t.
572 EJDB_EXPORT void bson_oid_gen(bson_oid_t *oid);
575 * Set a function to be used to generate the second four bytes
578 * @param func a pointer to a function that returns an int.
580 EJDB_EXPORT void bson_set_oid_fuzz(int ( *func)(void));
583 * Set a function to be used to generate the incrementing part
584 * of an object id (last four bytes). If you need thread-safety
585 * in generating object ids, you should set this function.
587 * @param func a pointer to a function that returns an int.
589 EJDB_EXPORT void bson_set_oid_inc(int ( *func)(void));
592 * Get the time a bson_oid_t was created.
594 * @param oid the bson_oid_t.
596 EJDB_EXPORT time_t bson_oid_generated_time(bson_oid_t *oid); /* Gives the time the OID was created */
598 /* ----------------------------
600 ------------------------------ */
603 EJDB_EXPORT void bson_append(bson *b, const void *data, int len);
606 * Initialize a new bson object. If not created
607 * with bson_new, you must initialize each new bson
608 * object using this function.
610 * @note When finished, you must pass the bson object to
613 EJDB_EXPORT void bson_init(bson *b);
617 * Intialize a new bson object. In query contruction mode allowing
618 * dot and dollar chars in field names.
621 EJDB_EXPORT void bson_init_as_query(bson *b);
624 * Initialize a BSON object, and point its data
625 * pointer to the provided char*.
627 * @param b the BSON object to initialize.
628 * @param data the raw BSON data.
630 * @return BSON_OK or BSON_ERROR.
632 EJDB_EXPORT int bson_init_data(bson *b, char *data);
633 EJDB_EXPORT int bson_init_finished_data(bson *b, const char *data);
636 * Initialize a BSON object, and set its
637 * buffer to the given size.
639 * @param b the BSON object to initialize.
640 * @param size the initial size of the buffer.
642 * @return BSON_OK or BSON_ERROR.
644 EJDB_EXPORT void bson_init_size(bson *b, int size);
646 EJDB_EXPORT void bson_init_on_stack(bson *b, char *bstack, int mincapacity, int maxonstack);
649 * Grow a bson object.
651 * @param b the bson to grow.
652 * @param bytesNeeded the additional number of bytes needed.
654 * @return BSON_OK or BSON_ERROR with the bson error object set.
655 * Exits if allocation fails.
657 EJDB_EXPORT int bson_ensure_space(bson *b, const int bytesNeeded);
660 * Finalize a bson object.
662 * @param b the bson object to finalize.
664 * @return the standard error code. To deallocate memory,
665 * call bson_del on the bson object.
667 EJDB_EXPORT int bson_finish(bson *b);
670 * Destroy a bson object.
671 * Clears bson object and frees internal memory buffers held by bson
672 * object BUT does not delete bson object itself
673 * @param b the bson object to destroy.
675 EJDB_EXPORT void bson_destroy(bson *b);
678 * The bson_del() performs bson_destroy() then frees bson object itself.
681 EJDB_EXPORT void bson_del(bson *b);
683 EJDB_EXPORT void bson_reset(bson *b);
686 * Returns a pointer to a static empty BSON object.
688 * @param obj the BSON object to initialize.
690 * @return the empty initialized BSON object.
692 /* returns pointer to static empty bson object */
693 EJDB_EXPORT bson *bson_empty(bson *obj);
696 * Make a complete copy of the a BSON object.
697 * The source bson object must be in a finished
698 * state; otherwise, the copy will fail.
700 * @param out the copy destination BSON object.
701 * @param in the copy source BSON object.
703 EJDB_EXPORT int bson_copy(bson *out, const bson *in); /* puts data in new buffer. NOOP if out==NULL */
706 * Append a previously created bson_oid_t to a bson object.
708 * @param b the bson to append to.
709 * @param name the key for the bson_oid_t.
710 * @param oid the bson_oid_t to append.
712 * @return BSON_OK or BSON_ERROR.
714 EJDB_EXPORT int bson_append_oid(bson *b, const char *name, const bson_oid_t *oid);
717 * Append a bson_oid_t to a bson.
719 * @param b the bson to append to.
720 * @param name the key for the bson_oid_t.
722 * @return BSON_OK or BSON_ERROR.
724 EJDB_EXPORT int bson_append_new_oid(bson *b, const char *name);
727 * Append an int to a bson.
729 * @param b the bson to append to.
730 * @param name the key for the int.
731 * @param i the int to append.
733 * @return BSON_OK or BSON_ERROR.
735 EJDB_EXPORT int bson_append_int(bson *b, const char *name, const int i);
738 * Append an long to a bson.
740 * @param b the bson to append to.
741 * @param name the key for the long.
742 * @param i the long to append.
744 * @return BSON_OK or BSON_ERROR.
746 EJDB_EXPORT int bson_append_long(bson *b, const char *name, const int64_t i);
749 * Append an double to a bson.
751 * @param b the bson to append to.
752 * @param name the key for the double.
753 * @param d the double to append.
755 * @return BSON_OK or BSON_ERROR.
757 EJDB_EXPORT int bson_append_double(bson *b, const char *name, const double d);
760 * Append a string to a bson.
762 * @param b the bson to append to.
763 * @param name the key for the string.
764 * @param str the string to append.
766 * @return BSON_OK or BSON_ERROR.
768 EJDB_EXPORT int bson_append_string(bson *b, const char *name, const char *str);
771 * Append len bytes of a string to a bson.
773 * @param b the bson to append to.
774 * @param name the key for the string.
775 * @param str the string to append.
776 * @param len the number of bytes from str to append.
778 * @return BSON_OK or BSON_ERROR.
780 EJDB_EXPORT int bson_append_string_n(bson *b, const char *name, const char *str, int len);
783 * Append a symbol to a bson.
785 * @param b the bson to append to.
786 * @param name the key for the symbol.
787 * @param str the symbol to append.
789 * @return BSON_OK or BSON_ERROR.
791 EJDB_EXPORT int bson_append_symbol(bson *b, const char *name, const char *str);
794 * Append len bytes of a symbol to a bson.
796 * @param b the bson to append to.
797 * @param name the key for the symbol.
798 * @param str the symbol to append.
799 * @param len the number of bytes from str to append.
801 * @return BSON_OK or BSON_ERROR.
803 EJDB_EXPORT int bson_append_symbol_n(bson *b, const char *name, const char *str, int len);
806 * Append code to a bson.
808 * @param b the bson to append to.
809 * @param name the key for the code.
810 * @param str the code to append.
811 * @param len the number of bytes from str to append.
813 * @return BSON_OK or BSON_ERROR.
815 EJDB_EXPORT int bson_append_code(bson *b, const char *name, const char *str);
818 * Append len bytes of code to a bson.
820 * @param b the bson to append to.
821 * @param name the key for the code.
822 * @param str the code to append.
823 * @param len the number of bytes from str to append.
825 * @return BSON_OK or BSON_ERROR.
827 EJDB_EXPORT int bson_append_code_n(bson *b, const char *name, const char *str, int len);
830 * Append code to a bson with scope.
832 * @param b the bson to append to.
833 * @param name the key for the code.
834 * @param str the string to append.
835 * @param scope a BSON object containing the scope.
837 * @return BSON_OK or BSON_ERROR.
839 EJDB_EXPORT int bson_append_code_w_scope(bson *b, const char *name, const char *code, const bson *scope);
842 * Append len bytes of code to a bson with scope.
844 * @param b the bson to append to.
845 * @param name the key for the code.
846 * @param str the string to append.
847 * @param len the number of bytes from str to append.
848 * @param scope a BSON object containing the scope.
850 * @return BSON_OK or BSON_ERROR.
852 EJDB_EXPORT int bson_append_code_w_scope_n(bson *b, const char *name, const char *code, int size, const bson *scope);
855 * Append binary data to a bson.
857 * @param b the bson to append to.
858 * @param name the key for the data.
859 * @param type the binary data type.
860 * @param str the binary data.
861 * @param len the length of the data.
863 * @return BSON_OK or BSON_ERROR.
865 EJDB_EXPORT int bson_append_binary(bson *b, const char *name, char type, const char *str, int len);
868 * Append a bson_bool_t to a bson.
870 * @param b the bson to append to.
871 * @param name the key for the boolean value.
872 * @param v the bson_bool_t to append.
874 * @return BSON_OK or BSON_ERROR.
876 EJDB_EXPORT int bson_append_bool(bson *b, const char *name, const bson_bool_t v);
879 * Append a null value to a bson.
881 * @param b the bson to append to.
882 * @param name the key for the null value.
884 * @return BSON_OK or BSON_ERROR.
886 EJDB_EXPORT int bson_append_null(bson *b, const char *name);
889 * Append an undefined value to a bson.
891 * @param b the bson to append to.
892 * @param name the key for the undefined value.
894 * @return BSON_OK or BSON_ERROR.
896 EJDB_EXPORT int bson_append_undefined(bson *b, const char *name);
899 * Append a regex value to a bson.
901 * @param b the bson to append to.
902 * @param name the key for the regex value.
903 * @param pattern the regex pattern to append.
904 * @param the regex options.
906 * @return BSON_OK or BSON_ERROR.
908 EJDB_EXPORT int bson_append_regex(bson *b, const char *name, const char *pattern, const char *opts);
911 * Append bson data to a bson.
913 * @param b the bson to append to.
914 * @param name the key for the bson data.
915 * @param bson the bson object to append.
917 * @return BSON_OK or BSON_ERROR.
919 EJDB_EXPORT int bson_append_bson(bson *b, const char *name, const bson *bson);
922 * Append a BSON element to a bson from the current point of an iterator.
924 * @param b the bson to append to.
925 * @param name_or_null the key for the BSON element, or NULL.
926 * @param elem the bson_iterator.
928 * @return BSON_OK or BSON_ERROR.
930 EJDB_EXPORT int bson_append_element(bson *b, const char *name_or_null, const bson_iterator *elem);
933 * Append a bson_timestamp_t value to a bson.
935 * @param b the bson to append to.
936 * @param name the key for the timestampe value.
937 * @param ts the bson_timestamp_t value to append.
939 * @return BSON_OK or BSON_ERROR.
941 EJDB_EXPORT int bson_append_timestamp(bson *b, const char *name, bson_timestamp_t *ts);
942 EJDB_EXPORT int bson_append_timestamp2(bson *b, const char *name, int time, int increment);
944 /* these both append a bson_date */
946 * Append a bson_date_t value to a bson.
948 * @param b the bson to append to.
949 * @param name the key for the date value.
950 * @param millis the bson_date_t to append.
952 * @return BSON_OK or BSON_ERROR.
954 EJDB_EXPORT int bson_append_date(bson *b, const char *name, bson_date_t millis);
957 * Append a time_t value to a bson.
959 * @param b the bson to append to.
960 * @param name the key for the date value.
961 * @param secs the time_t to append.
963 * @return BSON_OK or BSON_ERROR.
965 EJDB_EXPORT int bson_append_time_t(bson *b, const char *name, time_t secs);
968 * Start appending a new object to a bson.
970 * @param b the bson to append to.
971 * @param name the name of the new object.
973 * @return BSON_OK or BSON_ERROR.
975 EJDB_EXPORT int bson_append_start_object(bson *b, const char *name);
976 EJDB_EXPORT int bson_append_start_object2(bson *b, const char *name, int namelen);
979 * Start appending a new array to a bson.
981 * @param b the bson to append to.
982 * @param name the name of the new array.
984 * @return BSON_OK or BSON_ERROR.
986 EJDB_EXPORT int bson_append_start_array(bson *b, const char *name);
987 EJDB_EXPORT int bson_append_start_array2(bson *b, const char *name, int namelen);
990 * Finish appending a new object or array to a bson.
992 * @param b the bson to append to.
994 * @return BSON_OK or BSON_ERROR.
996 EJDB_EXPORT int bson_append_finish_object(bson *b);
999 * Finish appending a new object or array to a bson. This
1000 * is simply an alias for bson_append_finish_object.
1002 * @param b the bson to append to.
1004 * @return BSON_OK or BSON_ERROR.
1006 EJDB_EXPORT int bson_append_finish_array(bson *b);
1008 EJDB_EXPORT void bson_numstr(char *str, int64_t i);
1009 EJDB_EXPORT int bson_numstrn(char *str, int maxbuf, int64_t i);
1011 //void bson_incnumstr(char *str);
1013 /* Error handling and standard library function over-riding. */
1014 /* -------------------------------------------------------- */
1016 /* bson_err_handlers shouldn't return!!! */
1017 typedef void( *bson_err_handler)(const char *errmsg);
1018 typedef int (*bson_printf_func)(const char *, ...);
1020 extern void *(*bson_malloc_func)(size_t);
1021 extern void *(*bson_realloc_func)(void *, size_t);
1022 extern void ( *bson_free_func)(void *);
1024 extern bson_printf_func bson_errprintf;
1026 void bson_free(void *ptr);
1029 * Allocates memory and checks return value, exiting fatally if malloc() fails.
1031 * @param size bytes to allocate.
1033 * @return a pointer to the allocated memory.
1037 void *bson_malloc(int size);
1040 * Changes the size of allocated memory and checks return value,
1041 * exiting fatally if realloc() fails.
1043 * @param ptr pointer to the space to reallocate.
1044 * @param size bytes to allocate.
1046 * @return a pointer to the allocated memory.
1050 void *bson_realloc(void *ptr, int size);
1053 * Set a function for error handling.
1055 * @param func a bson_err_handler function.
1057 * @return the old error handling function, or NULL.
1059 EJDB_EXPORT bson_err_handler set_bson_err_handler(bson_err_handler func);
1061 /* does nothing if ok != 0 */
1065 * @param ok exits if ok is equal to 0.
1067 void bson_fatal(int ok);
1070 * Exit fatally with an error message.
1072 * @param ok exits if ok is equal to 0.
1073 * @param msg prints to stderr before exiting.
1075 void bson_fatal_msg(int ok, const char *msg);
1078 * Invoke the error handler, but do not exit.
1080 * @param b the buffer object.
1082 void bson_builder_error(bson *b);
1085 * Cast an int64_t to double. This is necessary for embedding in
1086 * certain environments.
1089 EJDB_EXPORT double bson_int64_to_double(int64_t i64);
1090 EJDB_EXPORT void bson_swap_endian32(void *outp, const void *inp);
1091 EJDB_EXPORT void bson_swap_endian64(void *outp, const void *inp);
1095 * Append current field from iterator into bson object.
1099 * @return BSON_OK or BSON_ERROR.
1101 EJDB_EXPORT int bson_append_field_from_iterator(const bson_iterator *from, bson *into);
1104 * Append current field value from iterator into bson object under specified string key.
1106 * @param key Key name.
1107 * @param from Source iterator value
1108 * @param into Target bsob object
1109 * @return BSON_OK or BSON_ERROR.
1111 EJDB_EXPORT int bson_append_field_from_iterator2(const char *key, const bson_iterator *from, bson *into);
1112 EJDB_EXPORT int bson_append_object_from_iterator(const char *key, bson_iterator *from, bson *into);
1113 EJDB_EXPORT int bson_append_array_from_iterator(const char *key, bson_iterator *from, bson *into);
1117 * Merge bson `b2` into `b1` saving result the 'out' object.
1118 * `b1` & `b2` bson must be finished BSONS.
1119 * Resulting 'out' bson must be allocated and not finished.
1121 * Nested object skipped and usupported.
1123 * @param b1 BSON to to be merged in `out`
1124 * @param b2 Second BSON to to be merged in `out`
1125 * @param overwrite if `true` all `b1` fields will be overwriten by corresponding `b2` fields
1128 * @return BSON_OK or BSON_ERROR.
1130 EJDB_EXPORT int bson_merge(const bson *b1, const bson *b2, bson_bool_t overwrite, bson *out);
1131 EJDB_EXPORT int bson_merge2(const void *b1data, const void *b2data, bson_bool_t overwrite, bson *out);
1134 * Recursive merge bson `b2` into `b1` saving result the 'out' object.
1135 * `b1` & `b2` bson must be finished BSONS.
1136 * Resulting 'out' bson must be allocated and not finished.
1138 * NOTE. Arrays with same paths joined.
1140 * @param b1 BSON to to be merged in `out`
1141 * @param b2 Second BSON to to be merged in `out`
1142 * @param overwrite if `true` all `b1` fields will be overwriten by corresponding `b2` fields
1145 * @return BSON_OK or BSON_ERROR.
1147 EJDB_EXPORT int bson_merge_recursive(const bson *b1, const bson *b2, bson_bool_t overwrite, bson *out);
1148 EJDB_EXPORT int bson_merge_recursive2(const void *b1data, const void *b2data, bson_bool_t overwrite, bson *out);
1152 * `bsdata2` may contain field path keys (eg: 'foo.bar').
1153 * @param bsdata1 BSON data to to be merged in `out`
1154 * @param bsdata2 Second BSON data to to be merged in `out`
1155 * @param out Resulting `out` bson must be allocated and not finished.
1157 * @return BSON_OK or BSON_ERROR.
1159 EJDB_EXPORT int bson_merge_fieldpaths(const void *bsdata1, const void *bsdata2, bson *out);
1161 EJDB_EXPORT int bson_inplace_set_bool(bson_iterator *pos, bson_bool_t val);
1162 EJDB_EXPORT int bson_inplace_set_long(bson_iterator *pos, int64_t val);
1163 EJDB_EXPORT int bson_inplace_set_double(bson_iterator *pos, double val);
1166 TCMAP *ifields; //Required Map of fieldpaths. Map values are a simple boolean bufs.
1167 bool imode; //Required If true fpaths will be included. Otherwise fpaths will be excluded from bson.
1168 const void *bsbuf; //Required BSON buffer to process.
1169 bson *bsout; //Required Allocated output not finished bson* object.
1170 TCMAP *fkfields; //Optional: Map (fpath => bson key) used to force specific bson keys for selected fpaths.
1171 int matched; //Output: number of matched fieldpaths
1172 bson *collector; //Optional: Collector for excluded data (fieldpath -> excluded value)
1176 * Include or exclude fpaths in the specified BSON and put resulting data into `bsout`.
1177 * On completion it finishes `bsout` object.
1179 * @param ifields Map of fieldpaths. Map values are a simple boolean bufs.
1180 * @param imode If true fpaths will be included. Otherwise fpaths will be excluded from bson.
1181 * @param bsbuf BSON buffer to process.
1182 * @param bsout Allocated and not finished output bson* object
1183 * @param matched[out] Number of matched include/exclude fieldpaths.
1184 * @return BSON error code
1186 EJDB_EXPORT int bson_strip(TCMAP *ifields, bool imode, const void *bsbuf, bson *bsout, int *matched);
1187 EJDB_EXPORT int bson_strip2(BSONSTRIPCTX *sctx);
1190 * @brief Rename a fields specified by `fields` rename mapping.
1192 * This operation unsets both all and new fieldpaths and then sets
1193 * new fieldpath values.
1195 * @param fields Rename mapping old `fieldpath` to new `fieldpath`.
1196 * @param bsbuf BSON buffer to process.
1197 * @param bsout Allocated and not finished output bson* object
1198 * @param rencnt A number of fieldpaths actually renamed.
1200 EJDB_EXPORT int bson_rename(TCMAP *fields, const void *bsbuf, bson *bsout, int *rencnt);
1204 * Compares field path primitive values of two BSONs
1205 * @param bsdata1 BSON raw data
1206 * @param bsdata2 BSON raw data
1207 * @param fpath Field path to the field
1208 * @param fplen Length of fpath value
1210 EJDB_EXPORT int bson_compare(const void *bsdata1, const void *bsdata2, const char* fpath, int fplen);
1211 EJDB_EXPORT int bson_compare_fpaths(const void *bsdata1, const void *bsdata2,
1212 const char *fpath1, int fplen1,
1213 const char *fpath2, int fplen2);
1214 EJDB_EXPORT int bson_compare_it_current(const bson_iterator *it1, const bson_iterator *it2);
1215 EJDB_EXPORT int bson_compare_string(const char* cv, const void *bsdata, const char *fpath);
1216 EJDB_EXPORT int bson_compare_long(const int64_t cv, const void *bsdata, const char *fpath);
1217 EJDB_EXPORT int bson_compare_double(double cv, const void *bsdata, const char *fpath);
1218 EJDB_EXPORT int bson_compare_bool(bson_bool_t cv, const void *bsdata, const char *fpath);
1222 * Duplicates BSON object.
1224 * @return Finished copy of src
1226 EJDB_EXPORT bson* bson_dup(const bson *src);
1229 EJDB_EXPORT bson* bson_create_from_iterator(bson_iterator *from);
1230 EJDB_EXPORT bson* bson_create_from_buffer(const void *buf, int bufsz);
1231 EJDB_EXPORT bson* bson_create_from_buffer2(bson *bs, const void *buf, int bufsz);
1232 EJDB_EXPORT void bson_init_with_data(bson *bs, const void *bsdata);
1235 BSON_MERGE_ARRAY_ADDSET = 0,
1236 BSON_MERGE_ARRAY_PULL = 1,
1237 BSON_MERGE_ARRAY_PUSH = 2
1238 } bson_merge_array_mode;
1240 EJDB_EXPORT int bson_merge_arrays(const void *mbuf, const void *inbuf,
1241 bson_merge_array_mode mode,
1242 bool expandall, bson *bsout);
1244 EJDB_EXPORT bool bson_find_unmerged_arrays(const void *mbuf, const void *inbuf);
1245 EJDB_EXPORT bool bson_find_merged_arrays(const void *mbuf, const void *inbuf, bool expandall);
1248 * Convert BSON into JSON buffer.
1250 * A caller should free an allocated `*buf`
1251 * if value of `*buf` is not `NULL` after function completion.
1253 * @param src BSON data
1254 * @param buf Allocated buffer with resulting JSON data
1255 * @param sp JSON data length will be stored into
1256 * @return BSON_OK or BSON_ERROR
1258 EJDB_EXPORT int bson2json(const char *bsdata, char **buf, int *sp);
1261 * Convert JSON into BSON object.
1262 * @param jsonstr NULL terminated JSON string
1263 * @return Allocated BSON object filled with given JSON data or NULL on error
1265 EJDB_EXPORT bson* json2bson(const char *jsonstr);