static void test_reader_tell (void) { bson_reader_t *reader; const bson_t *b; uint32_t i; bson_iter_t iter; bool eof; int fd; fd = bson_open(BINARY_DIR"/stream.bson", O_RDONLY); assert(-1 != fd); reader = bson_reader_new_from_handle ((void *)&fd, &test_reader_from_handle_read, &test_reader_from_handle_destroy); for (i = 0; i < 1000; i++) { if (i) { assert_cmpint(5 * i, ==, bson_reader_tell(reader)); } else { assert_cmpint(0, ==, bson_reader_tell(reader)); } eof = false; b = bson_reader_read(reader, &eof); assert(b); assert(bson_iter_init(&iter, b)); assert(!bson_iter_next(&iter)); }
int main (int argc, char *argv[]) { mongoc_matcher_t *matcher; bson_reader_t *reader; const bson_t *bson; bson_t *spec; char *str; reader = bson_reader_new_from_handle ((void *)&MONGOC_STDIN_FILENO, &_read_cb, &_destroy_cb); spec = BCON_NEW ("hello", "world"); matcher = mongoc_matcher_new (spec, NULL); while ((bson = bson_reader_read (reader, NULL))) { if (mongoc_matcher_match (matcher, bson)) { str = bson_as_json (bson, NULL); printf ("%s\n", str); bson_free (str); } } bson_reader_destroy (reader); bson_destroy (spec); return 0; }
bson_reader_t * bson_reader_new_from_fd (int fd, /* IN */ bool close_on_destroy) /* IN */ { bson_reader_handle_fd_t *handle; bson_return_val_if_fail (fd != -1, NULL); handle = bson_malloc0 (sizeof *handle); handle->fd = fd; handle->do_close = close_on_destroy; return bson_reader_new_from_handle (handle, _bson_reader_handle_fd_read, _bson_reader_handle_fd_destroy); }
SEXP R_mongo_restore(SEXP con, SEXP ptr_col, SEXP verb) { bool verbose = Rf_asLogical(verb); mongoc_collection_t *col = r2col(ptr_col); bson_reader_t *reader = bson_reader_new_from_handle(con, bson_reader_feed, bson_reader_finalize); mongoc_bulk_operation_t *bulk = NULL; const bson_t *b; bson_error_t err; int count = 0; int i = 0; bool done = false; bson_t reply; while(!done) { //note: default opts uses {ordered:true} bulk = mongoc_collection_create_bulk_operation_with_opts(col, NULL); for(i = 0; i < 1000; i++){ if(!(b = bson_reader_read (reader, &done))) break; mongoc_bulk_operation_insert (bulk, b); count++; } if(i == 0) break; if(!mongoc_bulk_operation_execute (bulk, &reply, &err)){ bson_reader_destroy(reader); mongoc_bulk_operation_destroy (bulk); Rf_error(err.message); } if(verbose) Rprintf("\rRestored %d records...", count); } if(verbose) Rprintf("\rDone! Inserted total of %d records.\n", count); if (!done) Rf_warning("Failed to read all documents.\n"); bson_reader_destroy(reader); mongoc_bulk_operation_destroy (bulk); return Rf_ScalarInteger(count); }