static void _upload_big_file (gridfs_test_t *gridfs_test, bson_oid_t *oid /* OUT */) { mongoc_gridfs_file_t *file; mongoc_iovec_t iov; bson_error_t error; const bson_value_t *value; file = mongoc_gridfs_create_file (gridfs_test->gridfs, NULL); iov.iov_base = (void *) gridfs_test->data; iov.iov_len = gridfs_test->data_sz; if (iov.iov_len != mongoc_gridfs_file_writev (file, &iov, 1, 0)) { if (mongoc_gridfs_file_error (file, &error)) { MONGOC_ERROR ("file_writev: %s\n", error.message); } else { MONGOC_ERROR ("file_writev: unknown error\n"); } abort (); } mongoc_gridfs_file_save (file); value = mongoc_gridfs_file_get_id (file); assert (value->value_type == BSON_TYPE_OID); bson_oid_copy (&value->value.v_oid, oid); mongoc_gridfs_file_destroy (file); }
static ssize_t _mongoc_stream_gridfs_writev (mongoc_stream_t *stream, mongoc_iovec_t *iov, size_t iovcnt, int32_t timeout_msec) { mongoc_stream_gridfs_t *file = (mongoc_stream_gridfs_t *) stream; ssize_t ret = 0; ENTRY; BSON_ASSERT (stream); BSON_ASSERT (iov); BSON_ASSERT (iovcnt); /* timeout_msec is unused by mongoc_gridfs_file_writev */ ret = mongoc_gridfs_file_writev (file->file, iov, iovcnt, 0); if (!ret) { RETURN (ret); } mongoc_counter_streams_egress_add (ret); RETURN (ret); }
static ssize_t _mongoc_stream_gridfs_writev (mongoc_stream_t *stream, struct iovec *iov, size_t iovcnt, bson_int32_t timeout_msec) { mongoc_stream_gridfs_t *file = (mongoc_stream_gridfs_t *)stream; ssize_t ret = 0; ENTRY; BSON_ASSERT (stream); BSON_ASSERT (iov); BSON_ASSERT (iovcnt); ret = mongoc_gridfs_file_writev (file->file, iov, iovcnt, timeout_msec); if (!ret) { RETURN (ret); } mongoc_counter_streams_egress_add (ret); RETURN (ret); }
static void test_write (void) { mongoc_gridfs_t *gridfs; mongoc_gridfs_file_t *file; mongoc_client_t *client; bson_error_t error; ssize_t r; char buf[] = "foo bar"; char buf2[] = " baz"; char buf3[1000]; mongoc_gridfs_file_opt_t opt = { 0 }; mongoc_iovec_t iov[2]; mongoc_iovec_t riov; int len = sizeof buf + sizeof buf2 - 2; iov [0].iov_base = buf; iov [0].iov_len = sizeof (buf) - 1; iov [1].iov_base = buf2; iov [1].iov_len = sizeof (buf2) - 1; riov.iov_base = buf3; riov.iov_len = sizeof (buf3); opt.chunk_size = 2; client = mongoc_client_new (gTestUri); assert (client); gridfs = get_test_gridfs (client, "write", &error); assert (gridfs); mongoc_gridfs_drop (gridfs, &error); file = mongoc_gridfs_create_file (gridfs, &opt); assert (file); assert (mongoc_gridfs_file_save (file)); r = mongoc_gridfs_file_writev (file, iov, 2, 0); assert (r == len); assert (mongoc_gridfs_file_save (file)); r = mongoc_gridfs_file_seek (file, 0, SEEK_SET); assert (!r); r = mongoc_gridfs_file_tell (file); assert (r == 0); r = mongoc_gridfs_file_readv (file, &riov, 1, len, 0); assert (r == len); assert (memcmp (buf3, "foo bar baz", len) == 0); mongoc_gridfs_file_destroy (file); drop_collections (gridfs, &error); mongoc_gridfs_destroy (gridfs); mongoc_client_destroy (client); }
/** create a gridfs file from a stream * * The stream is fully consumed in creating the file */ mongoc_gridfs_file_t * mongoc_gridfs_create_file_from_stream (mongoc_gridfs_t *gridfs, mongoc_stream_t *stream, mongoc_gridfs_file_opt_t *opt) { mongoc_gridfs_file_t *file; ssize_t r; uint8_t buf[MONGOC_GRIDFS_STREAM_CHUNK]; mongoc_iovec_t iov; int timeout; ENTRY; BSON_ASSERT (gridfs); BSON_ASSERT (stream); iov.iov_base = (void *)buf; iov.iov_len = 0; file = _mongoc_gridfs_file_new (gridfs, opt); timeout = gridfs->client->cluster.sockettimeoutms; for (;; ) { r = mongoc_stream_read (stream, iov.iov_base, MONGOC_GRIDFS_STREAM_CHUNK, 0, timeout); if (r > 0) { iov.iov_len = r; mongoc_gridfs_file_writev (file, &iov, 1, timeout); } else if (r == 0) { break; } else { mongoc_gridfs_file_destroy (file); RETURN (NULL); } } mongoc_stream_failed (stream); mongoc_gridfs_file_seek (file, 0, SEEK_SET); RETURN (file); }
static void * background_mongoc_gridfs_file_writev (void *data) { future_t *future = (future_t *) data; future_value_t return_value; return_value.type = future_value_ssize_t_type; future_value_set_ssize_t ( &return_value, mongoc_gridfs_file_writev ( future_value_get_mongoc_gridfs_file_t_ptr (future_get_param (future, 0)), future_value_get_mongoc_iovec_t_ptr (future_get_param (future, 1)), future_value_get_size_t (future_get_param (future, 2)), future_value_get_uint32_t (future_get_param (future, 3)) )); future_resolve (future, return_value); return NULL; }
/** create a gridfs file from a stream * * The stream is fully consumed in creating the file */ mongoc_gridfs_file_t * mongoc_gridfs_create_file_from_stream (mongoc_gridfs_t *gridfs, mongoc_stream_t *stream, mongoc_gridfs_file_opt_t *opt) { mongoc_gridfs_file_t *file; ssize_t r; bson_uint8_t buf[MONGOC_GRIDFS_STREAM_CHUNK]; struct iovec iov = { buf, 0 }; ENTRY; BSON_ASSERT (gridfs); BSON_ASSERT (stream); file = _mongoc_gridfs_file_new (gridfs, opt); for (;; ) { r = mongoc_stream_read (stream, iov.iov_base, MONGOC_GRIDFS_STREAM_CHUNK, 0, 0); if (r > 0) { iov.iov_len = r; mongoc_gridfs_file_writev (file, &iov, 1, 0); } else if (r == 0) { break; } else { mongoc_gridfs_file_destroy (file); RETURN (NULL); } } mongoc_stream_destroy (stream); mongoc_gridfs_file_seek (file, 0, SEEK_SET); RETURN (file); }