コード例 #1
0
void
test_mongo_sync_gridfs_get_set_chunk_size (void)
{
  mongo_sync_gridfs *gfs;

  ok (mongo_sync_gridfs_get_chunk_size (NULL) == -1,
      "mongo_sync_gridfs_get_chunk_size() fails with a NULL gfs");
  ok (mongo_sync_gridfs_set_chunk_size (NULL, 16 * 1024) == FALSE,
      "mongo_sync_gridfs_set_chunk_size() fails with a NULL gfs");

  begin_network_tests (3);

  gfs = mongo_sync_gridfs_new (mongo_sync_connect (config.primary_host,
                                                   config.primary_port,
                                                   FALSE),
                               config.gfs_prefix);

  ok (mongo_sync_gridfs_set_chunk_size (gfs, -1) == FALSE,
      "mongo_sync_gridfs_set_chunk_size() fails if the size is invalid");
  ok (mongo_sync_gridfs_set_chunk_size (gfs, 12345),
      "mongo_sync_gridfs_set_chunk_size() works");
  cmp_ok (mongo_sync_gridfs_get_chunk_size (gfs), "==", 12345,
          "mongo_sync_gridfs_get_chunk_size() works");

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();
}
コード例 #2
0
ファイル: gridfs.c プロジェクト: algernon/libmongo-client
void
mongo_gridfs_remove (config_t *config, gint argc, gchar *argv[])
{
  mongo_sync_gridfs *gfs;
  bson *query;
  gchar *fn;

  if (argc < 3)
    {
      fprintf (stderr, "Usage: %s remove GRIDFS_FILENAME\n", argv[0]);
      exit (1);
    }
  fn = argv[2];

  gfs = mongo_gridfs_connect (config);

  VLOG ("Deleting file: '%s'...\n", fn);

  query = bson_build (BSON_TYPE_STRING, "filename", fn, -1,
                      BSON_TYPE_NONE);
  bson_finish (query);

  if (mongo_sync_gridfs_remove (gfs, query))
    {
      VLOG ("\tDeleted\n");
    }
  else
    {
      VLOG ("\tFailed: %s\n", strerror (errno));
    }
  bson_free (query);

  mongo_sync_gridfs_free (gfs, TRUE);
}
コード例 #3
0
void
test_mongo_sync_gridfs_chunked_find (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  bson *query;

  query = bson_build (BSON_TYPE_STRING, "filename", "bogus-fn", -1,
                      BSON_TYPE_NONE);
  bson_finish (query);

  ok (mongo_sync_gridfs_chunked_find (NULL, query) == NULL,
      "mongo_sync_gridfs_chunked_find() fails with a NULL GridFS");

  begin_network_tests (2);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  ok (mongo_sync_gridfs_chunked_find (gfs, NULL) == NULL,
      "mongo_sync_gridfs_chunked_find() fails with a NULL query");

  ok (mongo_sync_gridfs_chunked_find (gfs, query) == NULL,
      "mongo_sync_gridfs_chunked_find() fails when the file is not found");

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();

  bson_free (query);
}
コード例 #4
0
void
test_mongo_sync_gridfs_stream_seek (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;

  mongo_util_oid_init (0);

  ok (mongo_sync_gridfs_stream_seek (NULL, 0, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with a NULL stream");

  begin_network_tests (8);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, NULL);

  ok (mongo_sync_gridfs_stream_seek (stream, 0, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with a write stream");

  stream->file.type = LMC_GRIDFS_FILE_STREAM_READER;

  ok (mongo_sync_gridfs_stream_seek (stream, -1, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_SET and a negative "
      "position");

  ok (mongo_sync_gridfs_stream_seek (stream, 10, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_SET and a position "
      "past EOF");

  ok (mongo_sync_gridfs_stream_seek (stream, -1, SEEK_CUR) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_CUR and a position "
      "before the start");

  ok (mongo_sync_gridfs_stream_seek (stream, 10, SEEK_CUR) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_CUR and a position "
      "past EOF");

  ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_END) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_END and a position "
      "past EOF");

  ok (mongo_sync_gridfs_stream_seek (stream, -1, SEEK_END) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_END and a position "
      "before the start");

  ok (mongo_sync_gridfs_stream_seek (stream, 0, 42) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with an invalid whence");

  mongo_sync_gridfs_stream_close (stream);
  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();
}
コード例 #5
0
void
test_mongo_sync_gridfs_new (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  gchar *f, *c;

  conn = test_make_fake_sync_conn (4, TRUE);

  ok (mongo_sync_gridfs_new (NULL, "test.fs") == NULL,
      "mongo_sync_gridfs_new() should fail with a NULL connection");

  ok (mongo_sync_gridfs_new (conn, "test.fs") == NULL,
      "mongo_sync_gridfs_new() should fail with a bogus connection");

  ok (mongo_sync_gridfs_new (conn, NULL) == NULL,
      "mongo_sync_gridfs_new() should fail with a NULL ns prefix");

  ok (mongo_sync_gridfs_new (conn, "bogus") == NULL,
      "mongo_sync_gridfs_new() should fail with a bogus ns prefix");

  mongo_sync_disconnect (conn);

  begin_network_tests (4);

  f = g_strconcat (config.gfs_prefix, ".files", NULL);
  c = g_strconcat (config.gfs_prefix, ".chunks", NULL);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);

  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
  ok (gfs != NULL,
      "mongo_sync_gridfs_new() works");
  is (gfs->ns.prefix, config.gfs_prefix,
      "The namespace prefix is as specified");
  is (gfs->ns.files, f,
      "The files namespace is correct");
  is (gfs->ns.chunks, c,
      "The chunks namespace is correct");
  mongo_sync_gridfs_free (gfs, FALSE);

  mongo_sync_disconnect (conn);

  g_free (f);
  g_free (c);
  end_network_tests ();
}
コード例 #6
0
void
test_mongo_sync_gridfs_stream_write (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;
  guint8 buffer[4096];

  mongo_util_oid_init (0);

  meta = bson_build (BSON_TYPE_STRING, "my-id", "sync_gridfs_stream_write", -1,
                     BSON_TYPE_NONE);
  bson_finish (meta);

  ok (mongo_sync_gridfs_stream_write (NULL, buffer, sizeof (buffer)) == FALSE,
      "mongo_sync_gridfs_stream_write() should fail with a NULL connection");

  begin_network_tests (4);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, meta);

  ok (mongo_sync_gridfs_stream_write (stream, NULL, sizeof (buffer)) == FALSE,
      "mongo_sync_gridfs_stream_write() should fail with a NULL buffer");
  ok (mongo_sync_gridfs_stream_write (stream, buffer, 0) == FALSE,
      "mongo_sync_gridfs_stream_write() should fail with 0 size");
  ok (mongo_sync_gridfs_stream_write (stream, buffer, sizeof (buffer)) == TRUE,
      "mongo_sync_gridfs_stream_write() works");

  stream->file.type = LMC_GRIDFS_FILE_STREAM_READER;
  ok (mongo_sync_gridfs_stream_write (stream, buffer, sizeof (buffer)) == FALSE,
      "mongo_sync_gridfs_stream_write() should fail with a read stream");

  mongo_sync_gridfs_stream_close (stream);
  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();

  bson_free (meta);
}
コード例 #7
0
void
test_mongo_sync_gridfs_stream_new (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;

  mongo_util_oid_init (0);

  meta = bson_build (BSON_TYPE_STRING, "my-id", "sync_gridfs_stream_new", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  ok (mongo_sync_gridfs_stream_new (NULL, meta) == FALSE,
      "mongo_sync_gridfs_stream_new() should fail with a NULL connection");

  begin_network_tests (2);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, NULL);
  ok (stream != NULL,
      "mongo_sync_gridfs_stream_new() works with NULL metadata");
  mongo_sync_gridfs_stream_close (stream);

  stream = mongo_sync_gridfs_stream_new (gfs, meta);
  ok (stream != NULL,
      "mongo_sync_gridfs_stream_new() works with metadata");
  mongo_sync_gridfs_stream_close (stream);

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();

  bson_free (meta);
}
コード例 #8
0
void
test_mongo_sync_gridfs_stream_read (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  guint8 buffer[4096];

  mongo_util_oid_init (0);

  ok (mongo_sync_gridfs_stream_read (NULL, buffer, sizeof (buffer)) == -1,
      "mongo_sync_gridfs_stream_read() should fail with a NULL connection");

  begin_network_tests (3);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, NULL);

  ok (mongo_sync_gridfs_stream_read (stream, buffer, sizeof (buffer)) == -1,
      "mongo-sync_gridfs_stream_read() should fail when the stream is "
      "write-only");

  stream->file.type = LMC_GRIDFS_FILE_STREAM_READER;

  ok (mongo_sync_gridfs_stream_read (stream, NULL, sizeof (buffer)) == -1,
      "mongo_sync_gridfs_stream_read() should fail with a NULL buffer");
  ok (mongo_sync_gridfs_stream_read (stream, buffer, 0) == -1,
      "mongo_sync_gridfs_stream_read() should fail with a 0 size");

  mongo_sync_gridfs_stream_close (stream);

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();
}
コード例 #9
0
ファイル: gridfs.c プロジェクト: algernon/libmongo-client
void
mongo_gridfs_get (config_t *config, gint argc, gchar *argv[])
{
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_chunked_file *gfile;
  mongo_sync_cursor *cursor;
  gint64 n = 0;
  bson *query;
  int fd;

  gchar *gfn, *ofn;

  if (argc < 4)
    {
      fprintf (stderr, "Usage: %s get GRIDFS_FILENAME OUTPUT_FILENAME\n",
               argv[0]);
      exit (1);
    }
  gfn = argv[2];
  ofn = argv[3];

  gfs = mongo_gridfs_connect (config);

  VLOG ("Trying to find '%s'...\n", gfn);

  query = bson_build (BSON_TYPE_STRING, "filename", gfn, -1,
                      BSON_TYPE_NONE);
  bson_finish (query);
  gfile = mongo_sync_gridfs_chunked_find (gfs, query);
  if (!gfile)
    mongo_gridfs_error (errno);
  bson_free (query);

  VLOG ("Opening output file '%s'...\n", ofn);
  fd = open (ofn, O_RDWR | O_CREAT | O_TRUNC, 0600);
  if (fd == -1)
    {
      fprintf (stderr, "Error opening output file '%s': %s\n",
               ofn, strerror (errno));
      exit (1);
    }

  VLOG ("Writing '%s' -> '%s' (%" G_GINT64_FORMAT " bytes in %" G_GINT64_FORMAT
        " chunks)\n", gfn, ofn,
        mongo_sync_gridfs_file_get_length (gfile),
        mongo_sync_gridfs_file_get_chunks (gfile));

  cursor = mongo_sync_gridfs_chunked_file_cursor_new (gfile, 0, 0);
  if (!cursor)
    mongo_gridfs_error (errno);

  while (mongo_sync_cursor_next (cursor))
    {
      gint32 size;
      guint8 *data;

      VLOG ("\rWriting chunk %" G_GINT64_FORMAT "...", n++);

      data = mongo_sync_gridfs_chunked_file_cursor_get_chunk (cursor, &size);
      if (!data)
        mongo_gridfs_error (errno);

      if (write (fd, data, size) != size)
        {
          perror ("write()");
          exit (1);
        }
      g_free (data);
    }
  mongo_sync_cursor_free (cursor);
  mongo_sync_gridfs_chunked_file_free (gfile);

  close (fd);

  mongo_sync_gridfs_free (gfs, TRUE);

  VLOG("\n");
}
コード例 #10
0
ファイル: gridfs.c プロジェクト: algernon/libmongo-client
void
mongo_gridfs_list (config_t *config)
{
  mongo_sync_cursor *cursor;
  mongo_sync_gridfs *gfs;

  gfs = mongo_gridfs_connect (config);

  cursor = mongo_sync_gridfs_list (gfs, NULL);

  while (mongo_sync_cursor_next (cursor))
    {
      bson *meta = mongo_sync_cursor_get_data (cursor);
      bson_cursor *c;
      const guint8 oid[12];
      gint32 i32, chunk_size;
      gint64 length, date;
      const gchar *md5, *filename = NULL;
      gchar *oid_s;

      c = bson_find (meta, "_id");
      if (!bson_cursor_get_oid (c, (const guint8 **)&oid))
        mongo_gridfs_error (errno);

      bson_cursor_find (c, "length");
      if (!bson_cursor_get_int32 (c, &i32))
        {
          if (!bson_cursor_get_int64 (c, &length))
            mongo_gridfs_error (errno);
        }
      else
        length = i32;

      bson_cursor_find (c, "chunkSize");
      if (!bson_cursor_get_int32 (c, &chunk_size))
        mongo_gridfs_error (errno);

      bson_cursor_find (c, "uploadDate");
      if (!bson_cursor_get_utc_datetime (c, &date))
        mongo_gridfs_error (errno);

      bson_cursor_find (c, "md5");
      if (!bson_cursor_get_string (c, &md5))
        mongo_gridfs_error (errno);

      bson_cursor_find (c, "filename");
      bson_cursor_get_string (c, &filename);

      bson_cursor_free (c);

      oid_s = mongo_util_oid_as_string (oid);
      printf ("{ _id: ObjectID(\"%s\"), length: %" G_GINT64_FORMAT
              ", chunkSize: %i, uploadDate: %"
              G_GINT64_FORMAT ", md5: \"%s\"",

              oid_s, length, chunk_size, date, md5);
      g_free (oid_s);

      if (filename)
        printf (", filename: \"%s\"", filename);
      printf (" }\n");

      if (config->verbose)
        {
          c = bson_cursor_new (meta);
          printf ("\tExtra metadata: [ ");
          while (bson_cursor_next (c))
            {
              if (strcmp (bson_cursor_key (c), "_id") &&
                  strcmp (bson_cursor_key (c), "length") &&
                  strcmp (bson_cursor_key (c), "chunkSize") &&
                  strcmp (bson_cursor_key (c), "uploadDate") &&
                  strcmp (bson_cursor_key (c), "md5") &&
                  strcmp (bson_cursor_key (c), "filename"))
                {
                  printf ("%s (%s), ", bson_cursor_key (c),
                          bson_cursor_type_as_string (c));
                }
            }
          bson_cursor_free (c);
          printf ("]\n");
        }
    }

  mongo_sync_gridfs_free (gfs, TRUE);
}
コード例 #11
0
ファイル: gridfs.c プロジェクト: algernon/libmongo-client
void
mongo_gridfs_put (config_t *config, gint argc, gchar *argv[])
{
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_chunked_file *gfile;
  bson *meta;
  int fd;
  guint8 *data;
  struct stat st;

  gchar *gfn, *ifn, *oid_s;

  if (argc < 4)
    {
      fprintf (stderr, "Usage: %s put INPUT_FILENAME GRIDFS_FILENAME\n",
               argv[0]);
      exit (1);
    }
  ifn = argv[2];
  gfn = argv[3];

  mongo_util_oid_init (0);

  gfs = mongo_gridfs_connect (config);

  VLOG ("Opening input file: '%s'...\n", ifn);
  fd = open (ifn, O_RDONLY);
  if (!fd)
    {
      fprintf (stderr, "Error opening input file: %s\n",
               strerror (errno));
      exit (1);
    }
  if (fstat (fd, &st) != 0)
    {
      fprintf (stderr, "Error stat'ing the input file: %s\n",
               strerror (errno));
      exit (1);
    }

  data = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (data == MAP_FAILED)
    {
      fprintf (stderr, "Error mmapping the input file: %s\n",
               strerror (errno));
    }

  meta = bson_build (BSON_TYPE_STRING, "filename", gfn, -1,
                     BSON_TYPE_NONE);
  bson_finish (meta);

  VLOG ("Uploading '%s' -> '%s'...\n", ifn, gfn);

  gfile = mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, meta,
                                                          data, st.st_size);
  if (!gfile)
    mongo_gridfs_error (errno);
  bson_free (meta);
  munmap (data, st.st_size);

  oid_s = mongo_util_oid_as_string (mongo_sync_gridfs_file_get_id (gfile));
  printf ("Uploaded file: %s (_id: %s; md5 = %s)\n", gfn,
          oid_s,
          mongo_sync_gridfs_file_get_md5 (gfile));

  g_free (oid_s);
  mongo_sync_gridfs_chunked_file_free (gfile);
  mongo_sync_gridfs_free (gfs, TRUE);
}
void
test_mongo_sync_gridfs_chunked_file_new_from_buffer (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  bson *metadata;
  guint8 *buffer;
  mongo_sync_gridfs_chunked_file *gfile;

  buffer = g_malloc (BUFFER_SIZE);
  memset (buffer, 'a', BUFFER_SIZE);

  conn = test_make_fake_sync_conn (4, TRUE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  metadata = bson_build (BSON_TYPE_STRING, "filename",
                         "gridfs_file_new_from_buffer", -1,
                         BSON_TYPE_NONE);
  bson_finish (metadata);

  ok (mongo_sync_gridfs_chunked_file_new_from_buffer (NULL, metadata,
                                                      buffer, BUFFER_SIZE) == FALSE,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() fails with a NULL GridFS");

  mongo_sync_gridfs_free (gfs, TRUE);

  begin_network_tests (5);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  ok (mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, metadata,
                                                      NULL, BUFFER_SIZE) == FALSE,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() fails with NULL data");

  ok (mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, metadata,
                                                      buffer, 0) == FALSE,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() fails with an invalid data size");

  ok (mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, metadata, buffer,
                                                      BUFFER_SIZE) == FALSE,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() fails with uninitialized OID");

  mongo_util_oid_init (0);

  gfile = mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, metadata,
                                                          buffer, BUFFER_SIZE);
  ok (gfile != NULL,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() works with metadata");
  mongo_sync_gridfs_chunked_file_free (gfile);

  gfile = mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, NULL,
                                                          buffer, BUFFER_SIZE);
  ok (gfile != NULL,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() works without metadata");
  mongo_sync_gridfs_chunked_file_free (gfile);

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();

  bson_free (metadata);
  g_free (buffer);
}