.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "MONGOC_GRIDFS_T" "3" "May 07, 2024" "1.27.1" "libmongoc" .sp \fBWARNING:\fP .INDENT 0.0 .INDENT 3.5 This GridFS implementation does not conform to the \fI\%MongoDB GridFS specification\fP\&. For a spec compliant implementation, use \fI\%mongoc_gridfs_bucket_t\fP\&. .UNINDENT .UNINDENT .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .EX #include typedef struct _mongoc_gridfs_t mongoc_gridfs_t; .EE .UNINDENT .UNINDENT .SH DESCRIPTION .sp \fBmongoc_gridfs_t\fP provides a MongoDB gridfs implementation. The system as a whole is made up of \fBgridfs\fP objects, which contain \fBgridfs_files\fP and \fBgridfs_file_lists\fP\&. Essentially, a basic file system API. .sp There are extensive caveats about the kind of use cases gridfs is practical for. In particular, any writing after initial file creation is likely to both break any concurrent readers and be quite expensive. That said, this implementation does allow for arbitrary writes to existing gridfs object, just use them with caution. .sp mongoc_gridfs also integrates tightly with the \fI\%mongoc_stream_t\fP abstraction, which provides some convenient wrapping for file creation and reading/writing. It can be used without, but its worth looking to see if your problem can fit that model. .sp \fBWARNING:\fP .INDENT 0.0 .INDENT 3.5 \fBmongoc_gridfs_t\fP does not support read preferences. In a replica set, GridFS queries are always routed to the primary. .UNINDENT .UNINDENT .SH THREAD SAFETY .sp \fBmongoc_gridfs_t\fP is NOT thread\-safe and should only be used in the same thread as the owning \fI\%mongoc_client_t\fP\&. .SH LIFECYCLE .sp It is an error to free a \fBmongoc_gridfs_t\fP before freeing all related instances of \fI\%mongoc_gridfs_file_t\fP and \fI\%mongoc_gridfs_file_list_t\fP\&. .SH EXAMPLE .sp example\-gridfs.c .INDENT 0.0 .INDENT 3.5 .sp .EX #include #include #include #include #include int main (int argc, char *argv[]) { mongoc_gridfs_t *gridfs; mongoc_gridfs_file_t *file; mongoc_gridfs_file_list_t *list; mongoc_gridfs_file_opt_t opt = {0}; mongoc_client_t *client; const char *uri_string = \(dqmongodb://127.0.0.1:27017/?appname=gridfs\-example\(dq; mongoc_uri_t *uri; mongoc_stream_t *stream; bson_t filter; bson_t opts; bson_t child; bson_error_t error; ssize_t r; char buf[4096]; mongoc_iovec_t iov; const char *filename; const char *command; bson_value_t id; if (argc < 2) { fprintf (stderr, \(dqusage \- %s command ...\en\(dq, argv[0]); return EXIT_FAILURE; } mongoc_init (); iov.iov_base = (void *) buf; iov.iov_len = sizeof buf; /* connect to localhost client */ uri = mongoc_uri_new_with_error (uri_string, &error); if (!uri) { fprintf (stderr, \(dqfailed to parse URI: %s\en\(dq \(dqerror message: %s\en\(dq, uri_string, error.message); return EXIT_FAILURE; } client = mongoc_client_new_from_uri (uri); assert (client); mongoc_client_set_error_api (client, 2); /* grab a gridfs handle in test prefixed by fs */ gridfs = mongoc_client_get_gridfs (client, \(dqtest\(dq, \(dqfs\(dq, &error); assert (gridfs); command = argv[1]; filename = argv[2]; if (strcmp (command, \(dqread\(dq) == 0) { if (argc != 3) { fprintf (stderr, \(dqusage \- %s read filename\en\(dq, argv[0]); return EXIT_FAILURE; } file = mongoc_gridfs_find_one_by_filename (gridfs, filename, &error); assert (file); stream = mongoc_stream_gridfs_new (file); assert (stream); for (;;) { r = mongoc_stream_readv (stream, &iov, 1, \-1, 0); assert (r >= 0); if (r == 0) { break; } if (fwrite (iov.iov_base, 1, r, stdout) != r) { MONGOC_ERROR (\(dqFailed to write to stdout. Exiting.\en\(dq); exit (1); } } mongoc_stream_destroy (stream); mongoc_gridfs_file_destroy (file); } else if (strcmp (command, \(dqlist\(dq) == 0) { bson_init (&filter); bson_init (&opts); bson_append_document_begin (&opts, \(dqsort\(dq, \-1, &child); BSON_APPEND_INT32 (&child, \(dqfilename\(dq, 1); bson_append_document_end (&opts, &child); list = mongoc_gridfs_find_with_opts (gridfs, &filter, &opts); bson_destroy (&filter); bson_destroy (&opts); while ((file = mongoc_gridfs_file_list_next (list))) { const char *name = mongoc_gridfs_file_get_filename (file); printf (\(dq%s\en\(dq, name ? name : \(dq?\(dq); mongoc_gridfs_file_destroy (file); } mongoc_gridfs_file_list_destroy (list); } else if (strcmp (command, \(dqwrite\(dq) == 0) { if (argc != 4) { fprintf (stderr, \(dqusage \- %s write filename input_file\en\(dq, argv[0]); return EXIT_FAILURE; } stream = mongoc_stream_file_new_for_path (argv[3], O_RDONLY, 0); assert (stream); opt.filename = filename; /* the driver generates a file_id for you */ file = mongoc_gridfs_create_file_from_stream (gridfs, stream, &opt); assert (file); id.value_type = BSON_TYPE_INT32; id.value.v_int32 = 1; /* optional: the following method specifies a file_id of any BSON type */ if (!mongoc_gridfs_file_set_id (file, &id, &error)) { fprintf (stderr, \(dq%s\en\(dq, error.message); return EXIT_FAILURE; } if (!mongoc_gridfs_file_save (file)) { mongoc_gridfs_file_error (file, &error); fprintf (stderr, \(dqCould not save: %s\en\(dq, error.message); return EXIT_FAILURE; } mongoc_gridfs_file_destroy (file); } else { fprintf (stderr, \(dqUnknown command\(dq); return EXIT_FAILURE; } mongoc_gridfs_destroy (gridfs); mongoc_uri_destroy (uri); mongoc_client_destroy (client); mongoc_cleanup (); return EXIT_SUCCESS; } .EE .UNINDENT .UNINDENT .sp \fBSEE ALSO:\fP .INDENT 0.0 .INDENT 3.5 .nf The \fI\%MongoDB GridFS specification\fP\&. .fi .sp .nf The spec\-compliant \fI\%mongoc_gridfs_bucket_t\fP\&. .fi .sp .UNINDENT .UNINDENT .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .