コード例 #1
0
void
test_bson_cursor_get_document (void)
{
  bson *b, *d = NULL;
  bson_cursor *c;

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

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

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

  c = bson_find (b, "doc");
  ok (bson_cursor_get_document (c, &d),
      "bson_cursor_get_document() works");
  cmp_ok (bson_size (d), ">", 0,
	  "the returned document is finished");
  bson_free (d);

  bson_cursor_next (c);
  ok (bson_cursor_get_document (c, &d) == FALSE,
      "bson_cursor_get_document() fails if the cursor points to "
      "non-document data");

  bson_cursor_free (c);
  bson_free (b);
}
コード例 #2
0
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");
}
コード例 #3
0
ファイル: bson_utils.c プロジェクト: NUOG/ejudge
int
ej_bson_parse_document(
        bson_cursor *bc,
        const unsigned char *field_name,
        bson **p_value)
{
    if (bson_cursor_type(bc) != BSON_TYPE_DOCUMENT) {
        err("parse_bson_document: document field type expected for '%s', got %s", field_name, bson_cursor_type_as_string(bc));
        return -1;
    }
    bson *data = NULL;
    if (!bson_cursor_get_document(bc, &data) || !data) {
        err("parse_bson_document: failed to fetch document for '%s'", field_name);
        return -1;
    }
    if (p_value) {
        *p_value = data;
    } else {
        bson_free(data);
    }
    return 1;
}
コード例 #4
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, " }");
    }
}