示例#1
0
static int test_string(void)
{
    unsigned int i;
    const char *strings[] = { "Four score and seven years ago",
                              "our father brought forth on this continent",
                              "a new nation", "conceived in Liberty",
                              "and dedicated to the proposition that all men are created equal."
                            };
    avro_schema_t writer_schema = avro_schema_string();
    for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) {
        avro_datum_t datum = avro_givestring(strings[i], NULL);
        write_read_check(writer_schema, datum, NULL, NULL, "string");
        avro_datum_decref(datum);
    }

    avro_datum_t  datum = avro_givestring(strings[0], NULL);
    test_json(datum, "\"Four score and seven years ago\"");
    avro_datum_decref(datum);

    // The following should bork if we don't copy the string value
    // correctly (since we'll try to free a static string).

    datum = avro_string("this should be copied");
    avro_string_set(datum, "also this");
    avro_datum_decref(datum);

    avro_schema_decref(writer_schema);
    return 0;
}
示例#2
0
/*添加学生记录*/
void add_student(avro_file_writer_t db, const char *name, const char *dept, const char *phone, int32_t age)
{
    avro_datum_t student = avro_record("Student", NULL);

    avro_datum_t sid_datum = avro_int64(++id);
    avro_datum_t name_datum = avro_string(name);
    avro_datum_t dept_datum = avro_string(dept);
    avro_datum_t age_datum = avro_int32(age);
    avro_datum_t phone_datum = avro_string(phone);

    /*创建学生记录*/
    if (avro_record_set(student, "SID", sid_datum)
            || avro_record_set(student, "Name", name_datum)
            || avro_record_set(student, "Dept", dept_datum)
            || avro_record_set(student, "Age", age_datum)
            || avro_record_set(student, "Phone", phone_datum)) {
        fprintf(stderr, "Failed to create student datum structure");
        exit(EXIT_FAILURE);
    }

    /*将记录添加到数据库文件中*/
    if (avro_file_writer_append(db, student)) {
        fprintf(stderr, "Failed to add student datum to database");
        exit(EXIT_FAILURE);
    }

    /*解除引用,释放内存空间*/
    avro_datum_decref(sid_datum);
    avro_datum_decref(name_datum);
    avro_datum_decref(dept_datum);
    avro_datum_decref(age_datum);
    avro_datum_decref(phone_datum);
    avro_datum_decref(student);

    fprintf(stdout, "Successfully added %s\n", name);
}