Esempio n. 1
0
void
tut_sync_query_simple (void)
{
  mongo_sync_connection *conn;
  mongo_packet *p;
  mongo_sync_cursor *cursor;
  bson *query;
  gint i = 0;

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

  query = bson_new ();
  bson_finish (query);

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

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

  while (mongo_sync_cursor_next (cursor))
    {
      bson *result = mongo_sync_cursor_get_data (cursor);
      bson_cursor *c;

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

      printf ("Keys in document #%d:\n", i);
      c = bson_cursor_new (result);
      while (bson_cursor_next (c))
	printf ("\t%s\n", bson_cursor_key (c));

      i++;
      bson_cursor_free (c);
      bson_free (result);
    }

  mongo_sync_cursor_free (cursor);
  mongo_sync_disconnect (conn);
}
Esempio n. 2
0
struct team_warning *
team_warning_bson_parse(bson *b)
{
    struct team_warning *res = NULL;
    bson_cursor *bc = NULL;

    if (!b) return NULL;

    XCALLOC(res, 1);
    bc = bson_cursor_new(b);
    while (bson_cursor_next(bc)) {
        const unsigned char *key = bson_cursor_key(bc);
        if (!strcmp(key, "date")) {
            if (ej_bson_parse_utc_datetime(bc, "date", &res->date) < 0) goto fail;
        } else if (!strcmp(key, "issuer_id")) {
            if (ej_bson_parse_int(bc, "issuer_id", &res->issuer_id, 1, 1, 0, 0) < 0) goto fail;
        } else if (!strcmp(key, "issuer_ip")) {
            if (ej_bson_parse_ip(bc, "issuer_ip", &res->issuer_ip) < 0) goto fail;
        } else if (!strcmp(key, "text")) {
            if (ej_bson_parse_string(bc, "text", &res->text) < 0) goto fail;
        } else if (!strcmp(key, "comment")) {
            if (ej_bson_parse_string(bc, "comment", &res->comment) < 0) goto fail;
        }
    }
    bson_cursor_free(bc);

    return res;

fail:
    if (res) {
        xfree(res->text);
        xfree(res->comment);
        xfree(res);
    }
    if (bc) bson_cursor_free(bc);
    return NULL;
}
Esempio n. 3
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);
}
Esempio n. 4
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, " }");
    }
}
Esempio n. 5
0
struct team_extra *
team_extra_bson_parse(bson *b)
{
    bson_cursor *bc = NULL;
    bson_cursor *bc2 = NULL;
    struct team_extra *res = NULL;
    bson *arr = NULL;
    bson *doc = NULL;
    struct team_warning *tw = NULL;

    if (!b) return NULL;

    XCALLOC(res, 1);
    bc = bson_cursor_new(b);
    while (bson_cursor_next(bc)) {
        const unsigned char *key = bson_cursor_key(bc);
        if (!strcmp(key, "_id")) {
            if (ej_bson_parse_uuid(bc, "_id", &res->uuid) < 0) goto fail;
        } else if (!strcmp(key, "contest_id")) {
            if (ej_bson_parse_int(bc, "contest_id", &res->contest_id, 1, 1, 0, 0) < 0) goto fail;
        } else if (!strcmp(key, "user_id")) {
            if (ej_bson_parse_int(bc, "user_id", &res->user_id, 1, 1, 0, 0) < 0) goto fail;
        } else if (!strcmp(key, "viewed_clars")) {
            if (ej_bson_parse_array(bc, "viewed_clars", &arr) < 0) goto fail;
            bc2 = bson_cursor_new(arr);
            while (bson_cursor_next(bc2)) {
                int clar_id = 0;
                if (ej_bson_parse_int(bc2, "viewed_clars/clar_id", &clar_id, 1, 0, 0, 0) < 0) goto fail;
                if (clar_id >= res->clar_map_size) team_extra_extend_clar_map(res, clar_id);
                res->clar_map[clar_id / BPE] |= (1UL << clar_id % BPE);
            }
            bson_cursor_free(bc2); bc2 = NULL;
            bson_free(arr); arr = NULL;
        } else if (!strcmp(key, "clar_uuids")) {
            if (ej_bson_parse_array(bc, "clar_uuids", &arr) < 0) goto fail;
            bc2 = bson_cursor_new(arr);
            while (bson_cursor_next(bc2)) {
                ej_uuid_t uuid;
                if (ej_bson_parse_uuid(bc2, "clar_uuids/uuid", &uuid) < 0) goto fail;
                team_extra_add_clar_uuid(res, &uuid);
            }
            bson_cursor_free(bc2); bc2 = NULL;
            bson_free(arr); arr = NULL;
        } else if (!strcmp(key, "disq_comment")) {
            if (ej_bson_parse_string(bc, "disq_comment", &res->disq_comment) < 0) goto fail;
        } else if (!strcmp(key, "warnings")) {
            if (ej_bson_parse_array(bc, "warnings", &arr) < 0) goto fail;
            bc2 = bson_cursor_new(arr);
            while (bson_cursor_next(bc2)) {
                if (ej_bson_parse_document(bc2, "warnings/warning", &doc) < 0) goto fail;
                if (!(tw = team_warning_bson_parse(doc))) goto fail;
                if (res->warn_u == res->warn_a) {
                    if (!(res->warn_a *= 2)) res->warn_a = 16;
                    XREALLOC(res->warns, res->warn_a);
                }
                res->warns[res->warn_u++] = tw; tw = NULL;
                bson_free(doc); doc = NULL;
            }
            bson_cursor_free(bc2); bc2 = NULL;
            bson_free(arr); arr = NULL;
        } else if (!strcmp(key, "status")) {
            if (ej_bson_parse_int(bc, "status", &res->status, 1, 0, 0, 0) < 0) goto fail;
        } else if (!strcmp(key, "run_fields")) {
            if (ej_bson_parse_int(bc, "run_fields", &res->run_fields, 1, 0, 0, 0) < 0) goto fail;
        }
    }
    bson_cursor_free(bc);
    return res;

fail:
    team_extra_free(res);
    if (doc) bson_free(doc);
    if (arr) bson_free(arr);
    if (bc2) bson_cursor_free(bc2);
    if (bc) bson_cursor_free(bc);
    return NULL;
}