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);
}
static void *
_multi_download_thread (void *p)
{
   multi_download_thread_context_t *ctx;
   FILE *fp;
   mongoc_gridfs_file_t *file;
   bson_error_t error;
   char buf[BUF_SZ];
   mongoc_iovec_t iov;
   ssize_t sz;

   ctx = (multi_download_thread_context_t *) p;

   fp = fopen (ctx->path, "w+");
   if (!fp) {
      perror ("fopen");
      abort ();
   }

   file = mongoc_gridfs_find_one_by_filename (ctx->gridfs,
                                              ctx->filename,
                                              &error);

   if (!file) {
      MONGOC_ERROR ("find_one_by_filename: %s\n", error.message);
      abort ();
   }

   iov.iov_base = buf;
   iov.iov_len = BUF_SZ;

   for (;;) {
      /* a 255k chunk at a time */
      sz = mongoc_gridfs_file_readv (file,
                                     &iov,
                                     1, /* iov count */
                                     1, /* min bytes */
                                     0  /* timeout   */);

      if (!sz) {
         break;
      }

      assert (sz > 0);
      fwrite (iov.iov_base, sizeof (char), (size_t) sz, fp);
   }

   if (mongoc_gridfs_file_error (file, &error)) {
      MONGOC_ERROR ("gridfs_file_readv: %s\n", error.message);
      abort ();
   }

   mongoc_gridfs_file_destroy (file);
   fclose (fp);

   return (void *) 1;
}
static void
test_read (void)
{
   mongoc_gridfs_t *gridfs;
   mongoc_gridfs_file_t *file;
   mongoc_stream_t *stream;
   mongoc_client_t *client;
   bson_error_t error;
   ssize_t r;
   char buf[10], buf2[10];
   mongoc_iovec_t iov[2];

   iov[0].iov_base = buf;
   iov[0].iov_len = 10;

   iov[1].iov_base = buf2;
   iov[1].iov_len = 10;

   client = mongoc_client_new (gTestUri);
   assert (client);

   gridfs = get_test_gridfs (client, "read", &error);
   assert (gridfs);

   mongoc_gridfs_drop (gridfs, &error);

   stream = mongoc_stream_file_new_for_path (BINARY_DIR"/gridfs.dat", O_RDONLY, 0);

   file = mongoc_gridfs_create_file_from_stream (gridfs, stream, NULL);
   assert (file);
   assert (mongoc_gridfs_file_save (file));

   r = mongoc_gridfs_file_readv (file, iov, 2, 20, 0);
   assert (r == 20);
   assert (memcmp (iov[0].iov_base, "Bacon ipsu", 10) == 0);
   assert (memcmp (iov[1].iov_base, "m dolor si", 10) == 0);

   mongoc_gridfs_file_destroy (file);

   drop_collections (gridfs, &error);
   mongoc_gridfs_destroy (gridfs);

   mongoc_client_destroy (client);
}
Ejemplo n.º 4
0
static void *
background_mongoc_gridfs_file_readv (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_readv (
         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_size_t (future_get_param (future, 3)),
         future_value_get_uint32_t (future_get_param (future, 4))
      ));

   future_resolve (future, return_value);

   return NULL;
}
static void
download_task (perf_test_t *test)
{
   download_test_t *gridfs_test;
   bson_t query = BSON_INITIALIZER;
   mongoc_gridfs_file_t *file;
   mongoc_iovec_t iov;
   bson_error_t error;
   ssize_t read_sz;

   gridfs_test = (download_test_t *) test;

   bson_append_oid (&query, "_id", 3, &gridfs_test->file_id);
   file = mongoc_gridfs_find_one (gridfs_test->base.gridfs, &query, &error);
   if (!file) {
      MONGOC_ERROR ("gridfs_find_one: %s\n", error.message);
      abort ();
   }

   /* overwrite the buffer we used for _upload_big_file */
   iov.iov_base = (void *) gridfs_test->base.data;
   iov.iov_len = gridfs_test->base.data_sz;
   read_sz = mongoc_gridfs_file_readv (file, &iov, 1, gridfs_test->base.data_sz,
                                       0);

   if (read_sz != gridfs_test->base.data_sz) {
      if (mongoc_gridfs_file_error (file, &error)) {
         MONGOC_ERROR ("file_readv: %s\n", error.message);
      } else {
         MONGOC_ERROR ("file_readv: unknown error\n");
      }

      abort ();
   }

   mongoc_gridfs_file_destroy (file);
   bson_destroy (&query);
}
Ejemplo n.º 6
0
static ssize_t
_mongoc_stream_gridfs_readv (mongoc_stream_t *stream,
                             mongoc_iovec_t *iov,
                             size_t iovcnt,
                             size_t min_bytes,
                             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_readv */
   ret = mongoc_gridfs_file_readv (file->file, iov, iovcnt, min_bytes, 0);

   mongoc_counter_streams_ingress_add (ret);

   RETURN (ret);
}
static ssize_t
_mongoc_stream_gridfs_readv (mongoc_stream_t *stream,
                             struct iovec    *iov,
                             size_t           iovcnt,
                             size_t           min_bytes,
                             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);
   BSON_ASSERT (timeout_msec <= INT_MAX);

   ret = mongoc_gridfs_file_readv (file->file, iov, iovcnt, min_bytes,
                                   timeout_msec);

   mongoc_counter_streams_ingress_add (ret);

   RETURN (ret);
}