void
test_bson_cursor_get_int32 (void)
{
  bson *b;
  bson_cursor *c;
  gint d = 12345;

  ok (bson_cursor_get_int32 (NULL, &d) == FALSE,
      "bson_cursor_get_int32() with a NULL cursor fails");

  b = test_bson_generate_full ();
  c = bson_cursor_new (b);

  ok (bson_cursor_get_int32 (c, NULL) == FALSE,
      "bson_cursor_get_int32() with a NULL destination fails");
  ok (bson_cursor_get_int32 (c, &d) == FALSE,
      "bson_cursor_get_int32() at the initial position fails");
  cmp_ok (d, "==", 12345,
          "destination remains unchanged after failed cursor operations");
  bson_cursor_free (c);

  c = bson_find (b, "int32");
  ok (bson_cursor_get_int32 (c, &d),
      "bson_cursor_get_int32() works");
  cmp_ok (d, "==", 32,
          "bson_cursor_get_int32() returns the correct result");

  bson_cursor_next (c);
  ok (bson_cursor_get_int32 (c, &d) == FALSE,
      "bson_cursor_get_int32() should fail when the cursor points to "
      "non-int32 data");

  bson_cursor_free (c);
  bson_free (b);
}
示例#2
0
文件: bson_utils.c 项目: NUOG/ejudge
int
ej_bson_parse_int(
        struct _bson_cursor *bc,
        const unsigned char *field_name,
        int *p_value,
        int check_low,
        int low_value,
        int check_high,
        int high_value)
{
    if (bson_cursor_type(bc) != BSON_TYPE_INT32) {
        err("parse_bson_int: int32 field type expected for '%s'", field_name);
        return -1;
    }
    int value = 0;
    if (!bson_cursor_get_int32(bc, &value)) {
        err("parse_bson_int: failed to fetch int32 value of '%s'", field_name);
        return -1;
    }
    if ((check_low > 0 && value < low_value) || (check_high > 0 && value >= high_value)) {
        err("parse_bson_int: invalid value of '%s': %d", field_name, value);
        return -1;
    }
    *p_value = value;
    return 1;
}
	static void
do_query (mongo_sync_connection *conn)
{
	mongo_sync_cursor *c;
	bson *query;
	gchar *error = NULL;

	query = bson_build
		(BSON_TYPE_TIMESTAMP, "processed", 1294860709000,
		 BSON_TYPE_NONE);
	bson_finish (query);

	c = mongo_sync_cursor_new (conn, "blahblah.plurals",
			mongo_sync_cmd_query (conn, "blahblah.plurals", 0,
				0, 10, query, NULL));
	if (!c)
	{
		fprintf (stderr, "Error creating the query cursor: %s\n",
				strerror (errno));
		exit (1);
	}
	bson_free (query);

	while (mongo_sync_cursor_next (c))
	{
		bson *b = mongo_sync_cursor_get_data (c);
		bson_cursor *bc;
		gint32 w_t;
		gint64 w_i;

		if (!b)
		{
			int e = errno;

			mongo_sync_cmd_get_last_error (conn, "blahblah.plurals", &error);
			fprintf (stderr, "Error retrieving cursor data: %s\n",
					(error) ? error : strerror (e));
			exit (1);
		}

		bc = bson_find (b, "word_type");
		bson_cursor_get_int32 (bc, &w_t);
		printf ("\rWord type: %d\n", w_t);

		bc = bson_find (b, "word_id");
		bson_cursor_get_int64 (bc, &w_i);
		printf ("\rWord id: %d\n", (int)w_i);

		bson_cursor_free (bc);
		bson_free (b);
	}
	printf ("\n");

	mongo_sync_cursor_free (c);
}
示例#4
0
static void
do_query (mongo_sync_connection *conn)
{
  mongo_sync_cursor *c;
  bson *query;
  gchar *error = NULL;

  query = bson_build
    (BSON_TYPE_STRING, "tutorial-program", "tut_hl_client.c", -1,
     BSON_TYPE_NONE);
  bson_finish (query);

  c = mongo_sync_cursor_new (conn, "lmc.tutorial",
			     mongo_sync_cmd_query (conn, "lmc.tutorial", 0,
						   0, 10, query, NULL));
  if (!c)
    {
      fprintf (stderr, "Error creating the query cursor: %s\n",
	       strerror (errno));
      exit (1);
    }
  bson_free (query);

  while (mongo_sync_cursor_next (c))
    {
      bson *b = mongo_sync_cursor_get_data (c);
      bson_cursor *bc;
      gint32 cnt;

      if (!b)
	{
	  int e = errno;

	  mongo_sync_cmd_get_last_error (conn, "lmc", &error);
	  fprintf (stderr, "Error retrieving cursor data: %s\n",
		   (error) ? error : strerror (e));
	  exit (1);
	}

      bc = bson_find (b, "counter");
      bson_cursor_get_int32 (bc, &cnt);
      printf ("\rCounter: %d", cnt);

      bson_cursor_free (bc);
      bson_free (b);
    }
  printf ("\n");

  mongo_sync_cursor_free (c);
}
示例#5
0
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);
}
示例#6
0
void
tut_sync_query_complex (void)
{
  mongo_sync_connection *conn;
  mongo_packet *p;
  mongo_sync_cursor *cursor;
  bson *query, *select;
  gint i = 0;

  conn = mongo_sync_connect ("localhost", 27017, FALSE);
  if (!conn)
    {
      perror ("mongo_sync_connect()");
      exit (1);
    }

  query = bson_build_full (BSON_TYPE_DOCUMENT, "$query", TRUE,
			   bson_build (BSON_TYPE_BOOLEAN, "yes?", FALSE,
				       BSON_TYPE_NONE),
			   BSON_TYPE_DOCUMENT, "$orderby", TRUE,
			   bson_build (BSON_TYPE_INT32, "n", 1,
				       BSON_TYPE_NONE),
			   BSON_TYPE_NONE);
  bson_finish (query);

  select = bson_build (BSON_TYPE_INT32, "hello", 1,
		       BSON_TYPE_INT32, "n", 1,
		       BSON_TYPE_INT32, "yes?", 1,
		       BSON_TYPE_NONE);
  bson_finish (select);

  p = mongo_sync_cmd_query (conn, "tutorial.docs", 0,
			    0, 10, query, select);
  if (!p)
    {
      perror ("mongo_sync_cmd_query()");
      exit (1);
    }
  bson_free (query);
  bson_free (select);

  cursor = mongo_sync_cursor_new (conn, "tutorial.docs", p);
  if (!cursor)
    {
      perror ("mongo_sync_cursor_new()");
      exit (1);
    }

  while (mongo_sync_cursor_next (cursor))
    {
      const char *hello;
      gint32 n;
      gboolean yes;

      bson *result;
      bson_cursor *c;

      result = mongo_sync_cursor_get_data (cursor);
      if (!result)
	{
	  perror ("mongo_sync_cursor_get_data()");
	  exit (1);
	}

      c = bson_find (result, "hello");
      if (!bson_cursor_get_string (c, &hello))
	{
	  perror ("bson_cursor_get_string()");
	  exit (1);
	}
      bson_cursor_free (c);

      c = bson_find (result, "n");
      if (!bson_cursor_get_int32 (c, &n))
	{
	  perror ("bson_cursor_get_int32()");
	  exit (1);
	}
      bson_cursor_free (c);

      c = bson_find (result, "yes?");
      if (!bson_cursor_get_boolean (c, &yes))
	{
	  perror ("bson_cursor_get_boolean()");
	  exit (1);
	}
      bson_cursor_free (c);

      printf ("Document #%d: hello=%s; n=%d; yes?=%s\n",
	      i, hello, n, (yes) ? "TRUE" : "FALSE");

      bson_free (result);
      i++;
    }

  mongo_sync_cursor_free (cursor);
  mongo_sync_disconnect (conn);
}
示例#7
0
文件: bson_utils.c 项目: NUOG/ejudge
void
ej_bson_unparse(
        FILE *out,
        const struct _bson *b,
        int is_array)
{
    if (!b) {
        fprintf(out, "NULL");
        return;
    }
    if (is_array) {
        fprintf(out, "[ ");
    } else {
        fprintf(out, "{ ");
    }
    bson_cursor *cursor = bson_cursor_new(b);
    int first = 1;
    while (bson_cursor_next(cursor)) {
        if (!first) fprintf(out, ", ");
        if (!is_array) {
            fprintf(out, "%s : ", bson_cursor_key(cursor));
        }
        bson_type t = bson_cursor_type(cursor);
        switch (t) {
        case BSON_TYPE_DOUBLE:
            break;
        case BSON_TYPE_STRING:
            {
                const char *value = NULL;
                if (bson_cursor_get_string(cursor, &value)) {
                    fprintf(out, "\"%s\"", value);
                }
            }
            break;
        case BSON_TYPE_DOCUMENT:
            {
                bson *doc = NULL;
                if (bson_cursor_get_document(cursor, &doc)) {
                    ej_bson_unparse(out, doc, 0);
                    bson_free(doc);
                }
            }
            break;
        case BSON_TYPE_ARRAY:
            {
                bson *doc = NULL;
                if (bson_cursor_get_array(cursor, &doc)) {
                    ej_bson_unparse(out, doc, 1);
                    bson_free(doc);
                }
            }
            break;
        case BSON_TYPE_BINARY:
            {
                bson_binary_subtype bt = 0;
                const unsigned char *bd = NULL;
                int bz = 0;
                if (bson_cursor_get_binary(cursor, &bt, &bd, &bz)
                    && bt == BSON_BINARY_SUBTYPE_UUID && bz == sizeof(ej_uuid_t)) {
                    ej_uuid_t value;
                    memcpy(&value, bd, sizeof(value));
                    fprintf(out, "\"%s\"", ej_uuid_unparse(&value, NULL));
                }
            }
            break;
        case BSON_TYPE_OID:
        case BSON_TYPE_BOOLEAN:
            break;
        case BSON_TYPE_UTC_DATETIME:
            {
                gint64 ts = 0;
                if (bson_cursor_get_utc_datetime(cursor, &ts)) {
                    time_t tt = (time_t) (ts / 1000);
                    int ms = (int) (ts % 1000);
                    struct tm *ptm = gmtime(&tt);
                    fprintf(out, "\"%d/%02d/%02d %02d:%02d:%02d.%04d\"",
                            ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
                            ptm->tm_hour, ptm->tm_min, ptm->tm_sec, ms);
                }
            }
            break;
        case BSON_TYPE_NULL:
            break;
        case BSON_TYPE_INT32:
            {
                int value = 0;
                if (bson_cursor_get_int32(cursor, &value)) {
                    fprintf(out, "%d", value);
                }
            }
            break;
        case BSON_TYPE_INT64:
            {
                gint64 value = 0;
                if (bson_cursor_get_int64(cursor, &value)) {
                    fprintf(out, "%lld", (long long) value);
                }
            }
            break;
        default:
            break;
        }
        first = 0;
    }
    bson_cursor_free(cursor); cursor = NULL;
    if (is_array) {
        fprintf(out, " ]");
    } else {
        fprintf(out, " }");
    }
}