Exemplo n.º 1
0
void
test_bson_cursor_get_string (void)
{
  bson *b;
  bson_cursor *c;
  const gchar *s = "deadbeef";

  ok (bson_cursor_get_string (NULL, &s) == FALSE,
      "bson_cursor_get_string() with a NULL cursor fails");

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

  ok (bson_cursor_get_string (c, NULL) == FALSE,
      "bson_cursor_get_string() with a NULL destination fails");
  ok (bson_cursor_get_string (c, &s) == FALSE,
      "bson_cursor_get_string() at the initial position fails");
  is (s, "deadbeef",
      "destination remains unchanged after failed cursor operations");
  bson_cursor_free (c);

  c = bson_find (b, "str");
  ok (bson_cursor_get_string (c, &s),
      "bson_cursor_get_string() works");
  is (s, "hello world",
      "bson_cursor_get_string() returns the correct result");

  bson_cursor_next (c);
  ok (bson_cursor_get_string (c, &s) == FALSE,
      "bson_cursor_get_string() should fail when the cursor points to "
      "non-string data");

  bson_cursor_free (c);
  bson_free (b);
}
Exemplo n.º 2
0
struct ofields* get_data(struct results *res)
{
	struct ofields *fields;
	const char *msg;
	const char *prog;
	const char *syslog_tag;
	gint64 date_r;
	bson_cursor *c;

	fields = malloc(sizeof(struct ofields));
		
	c = bson_find (res->result, "msg");		 
	if (!bson_cursor_get_string (c, &msg))
     	{
		perror ("bson_cursor_get_string()");
		exit (1);
        }	
      	bson_cursor_free (c); 

	c = bson_find (res->result, "sys");
	if (!bson_cursor_get_string (c, &prog))
        {	 	
         	perror ("bson_cursor_get_string()");
         	exit (1);
        }
      	bson_cursor_free (c);   

	c = bson_find (res->result, "syslog_tag");
     	if (!bson_cursor_get_string (c, &syslog_tag))
        {		
          	perror ("bson_cursor_get_string()");
          	exit (1);
        }
      	bson_cursor_free (c); 

	c = bson_find (res->result, "time_rcvd");
     	if (!bson_cursor_get_utc_datetime (c, &date_r))
        {
          	perror ("bson_cursor_get_utc_datetime()");
          	exit (1);
        }

 	bson_cursor_free (c);  
	fields->msg = msg;
	fields->prog = prog;
	fields->syslog_tag = syslog_tag;
	fields->date_r = date_r;

	return fields;
		
}
static void
print_coll_info (bson *info)
{
  bson_cursor *c = NULL;
  bson *options = NULL;

  const gchar *name;
  gboolean capped = FALSE;
  gint64 size = -1;
  gint64 max = -1;

  c = bson_find (info, "name");
  bson_cursor_get_string (c, &name);
  bson_cursor_find (c, "options");

  bson_cursor_get_document (c, &options);

  printf ("Options for %s:\n", name);

  bson_cursor_free (c);
  bson_free (info);

  c = bson_find (options, "capped");
  bson_cursor_get_boolean (c, &capped);
  bson_cursor_free (c);

  c = bson_find (options, "size");
  bson_cursor_get_int64 (c, &size);
  bson_cursor_free (c);

  c = bson_find (options, "max");
  bson_cursor_get_int64 (c, &max);
  bson_cursor_free (c);

  bson_free (options);

  printf ("\tCapped: %s\n", (capped) ? "yes" : "no");
  if (size > 0)
    printf ("\tSize  : %lu\n", size);
  if (max > 0)
    printf ("\tMax   : %lu\n", max);
  printf ("\n");
}
Exemplo n.º 4
0
int
ej_bson_parse_ip(
        struct _bson_cursor *bc,
        const unsigned char *field_name,
        ej_ip_t *p_value)
{
    if (bson_cursor_type(bc) != BSON_TYPE_STRING) {
        err("parse_bson_ip: string field type expected for '%s'", field_name);
        return -1;
    }
    const char *data = NULL;
    if (!bson_cursor_get_string(bc, &data)) {
        err("parse_bson_ip: failed to fetch string for '%s'", field_name);
        return -1;
    }
    if (!data) {
        err("parse_bson_ip: invalid string for in '%s'", field_name);
        return -1;
    }
    if (xml_parse_ipv6(NULL, 0, 0, 0, data, p_value) < 0) return -1;
    return 1;
}
Exemplo n.º 5
0
int
ej_bson_parse_string(
        struct _bson_cursor *bc,
        const unsigned char *field_name,
        unsigned char **p_value)
{
    if (bson_cursor_type(bc) != BSON_TYPE_STRING) {
        err("parse_bson_string: string field type expected for '%s'", field_name);
        return -1;
    }
    const char *data = NULL;
    if (!bson_cursor_get_string(bc, &data)) {
        err("parse_bson_string: failed to fetch string for '%s'", field_name);
        return -1;
    }
    if (!data) {
        err("parse_bson_string: invalid string for in '%s'", field_name);
        return -1;
    }
    if (p_value) {
        *p_value = xstrdup(data);
    }
    return 1;
}
Exemplo n.º 6
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);
}
Exemplo n.º 7
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);
}
Exemplo n.º 8
0
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, " }");
    }
}