/* { "type": "record", "name": "FinishRequest", "fields": [ {"name": "succeed", "type": "boolean"}, {"name": "diagnostics", "type": "string"} ] } */ static void parse_finish_request(avro_slice_t *slice, bool *succeed, char **diag) { char filename[FILE_NAME_LEN]; avro_schema_t schema; avro_value_iface_t *iface; avro_value_t record, succeed_value, diag_value; size_t index; avro_reader_t reader; size_t size; sprintf(filename, "%s/%s", SCHEMA_PATH, "FinishRequestRecordAvro.avsc"); init_schema(filename, &schema); iface = avro_generic_class_from_schema(schema); avro_generic_value_new(iface, &record); reader = avro_reader_memory(slice->buffer, slice->len); if (avro_value_read(reader, &record)) { fprintf(stderr, "Unable to read record from memory buffer\n"); fprintf(stderr, "Error: %s\n", avro_strerror()); exit(1); } avro_value_get_by_name(&record, "succeed", &succeed_value, &index); avro_value_get_boolean(&succeed_value, succeed); avro_value_get_by_name(&record, "diagnostics", &diag_value, &index); avro_value_get_string(&diag_value, diag, &size); //avro_generic_value_free(&record); avro_value_iface_decref(iface); avro_schema_decref(schema); }
rkv_error_t r_kv_avro_value_get_string(avro_value_t *avro_value, const char *name, const char **ret_value, int *ret_size) { avro_value_t field; rkv_error_t ret = RKV_SUCCESS; const char *value; size_t size; if (!avro_value || !name || !ret_value) { return RKV_INVALID_ARGUEMENTS; } if (avro_value_get_by_name(avro_value, name, &field, NULL) != 0) { return RKV_INVALID_ARGUEMENTS; } if (avro_value_get_string(&field, &value, &size) == EINVAL) { ret = RKV_INVALID_AVRO_SET_OP; } *ret_value = value; if (ret_size) { *ret_size = size; } //avro_value_decref(&field); return ret; }
int process_frame_table_schema(avro_value_t *record_val, frame_reader_t reader, uint64_t wal_pos) { int err = 0, key_schema_present; avro_value_t relid_val, hash_val, key_schema_val, row_schema_val, branch_val; int64_t relid; const void *hash; const char *key_schema_json = NULL, *row_schema_json; size_t hash_len, key_schema_len = 1, row_schema_len; avro_schema_t key_schema = NULL, row_schema; check(err, avro_value_get_by_index(record_val, 0, &relid_val, NULL)); check(err, avro_value_get_by_index(record_val, 1, &hash_val, NULL)); check(err, avro_value_get_by_index(record_val, 2, &key_schema_val, NULL)); check(err, avro_value_get_by_index(record_val, 3, &row_schema_val, NULL)); check(err, avro_value_get_long(&relid_val, &relid)); check(err, avro_value_get_fixed(&hash_val, &hash, &hash_len)); check(err, avro_value_get_discriminant(&key_schema_val, &key_schema_present)); check(err, avro_value_get_string(&row_schema_val, &row_schema_json, &row_schema_len)); check(err, avro_schema_from_json_length(row_schema_json, row_schema_len - 1, &row_schema)); schema_list_entry *entry = schema_list_replace(reader, relid); entry->relid = relid; entry->hash = *((uint64_t *) hash); entry->row_schema = row_schema; entry->row_iface = avro_generic_class_from_schema(row_schema); avro_generic_value_new(entry->row_iface, &entry->row_value); avro_generic_value_new(entry->row_iface, &entry->old_value); entry->avro_reader = avro_reader_memory(NULL, 0); if (key_schema_present) { check(err, avro_value_get_current_branch(&key_schema_val, &branch_val)); check(err, avro_value_get_string(&branch_val, &key_schema_json, &key_schema_len)); check(err, avro_schema_from_json_length(key_schema_json, key_schema_len - 1, &key_schema)); entry->key_schema = key_schema; entry->key_iface = avro_generic_class_from_schema(key_schema); avro_generic_value_new(entry->key_iface, &entry->key_value); } else { entry->key_schema = NULL; } if (reader->on_table_schema) { check(err, reader->on_table_schema(reader->cb_context, wal_pos, relid, key_schema_json, key_schema_len - 1, key_schema, row_schema_json, row_schema_len - 1, row_schema)); } return err; }
/* returns new reference */ PyObject * avro_to_python(ConvertInfo *info, avro_value_t *value) { avro_type_t type = avro_value_get_type(value); switch (type) { case AVRO_BOOLEAN: { int val; avro_value_get_boolean(value, &val); return PyBool_FromLong(val); } case AVRO_BYTES: { const void *buf; size_t size; avro_value_get_bytes(value, &buf, &size); /* got pointer into underlying value. no need to free */ return PyString_FromStringAndSize(buf, size); } case AVRO_DOUBLE: { double val; avro_value_get_double(value, &val); return PyFloat_FromDouble(val); } case AVRO_FLOAT: { float val; avro_value_get_float(value, &val); return PyFloat_FromDouble(val); } case AVRO_INT32: { int32_t val; avro_value_get_int(value, &val); return PyInt_FromLong(val); } case AVRO_INT64: { int64_t val; avro_value_get_long(value, &val); return PyLong_FromLongLong((PY_LONG_LONG)val); } case AVRO_NULL: { avro_value_get_null(value); Py_INCREF(Py_None); return Py_None; } case AVRO_STRING: { /* TODO: Convert the UTF-8 to the current * locale's character set */ const char *buf; size_t size; avro_value_get_string(value, &buf, &size); /* For strings, size includes the NUL terminator. */ return PyString_FromStringAndSize(buf, size - 1); } case AVRO_ARRAY: return array_to_python(info, value); case AVRO_ENUM: if (info->types == NULL) { /* just an int */ return enum_to_python(info, value); } else { /* a fancy object */ return enum_to_python_object(info, value); } case AVRO_FIXED: { const void *buf; size_t size; avro_value_get_fixed(value, &buf, &size); return PyString_FromStringAndSize((const char *)buf, size); } case AVRO_MAP: return map_to_python(info, value); case AVRO_RECORD: if (info->types == NULL) { /* just a dictionary */ return record_to_python(info, value); } else { /* a fancy object */ return record_to_python_object(info, value); } case AVRO_UNION: return union_to_python(info, value); default: return NULL; } }
int avro_value_equal_fast(avro_value_t *val1, avro_value_t *val2) { avro_type_t type1 = avro_value_get_type(val1); avro_type_t type2 = avro_value_get_type(val2); if (type1 != type2) { return 0; } switch (type1) { case AVRO_BOOLEAN: { int v1; int v2; check_return(0, avro_value_get_boolean(val1, &v1)); check_return(0, avro_value_get_boolean(val2, &v2)); return (v1 == v2); } case AVRO_BYTES: { const void *buf1; const void *buf2; size_t size1; size_t size2; check_return(0, avro_value_get_bytes(val1, &buf1, &size1)); check_return(0, avro_value_get_bytes(val2, &buf2, &size2)); if (size1 != size2) { return 0; } return (memcmp(buf1, buf2, size1) == 0); } case AVRO_DOUBLE: { double v1; double v2; check_return(0, avro_value_get_double(val1, &v1)); check_return(0, avro_value_get_double(val2, &v2)); return (v1 == v2); } case AVRO_FLOAT: { float v1; float v2; check_return(0, avro_value_get_float(val1, &v1)); check_return(0, avro_value_get_float(val2, &v2)); return (v1 == v2); } case AVRO_INT32: { int32_t v1; int32_t v2; check_return(0, avro_value_get_int(val1, &v1)); check_return(0, avro_value_get_int(val2, &v2)); return (v1 == v2); } case AVRO_INT64: { int64_t v1; int64_t v2; check_return(0, avro_value_get_long(val1, &v1)); check_return(0, avro_value_get_long(val2, &v2)); return (v1 == v2); } case AVRO_NULL: { check_return(0, avro_value_get_null(val1)); check_return(0, avro_value_get_null(val2)); return 1; } case AVRO_STRING: { const char *buf1; const char *buf2; size_t size1; size_t size2; check_return(0, avro_value_get_string(val1, &buf1, &size1)); check_return(0, avro_value_get_string(val2, &buf2, &size2)); if (size1 != size2) { return 0; } return (memcmp(buf1, buf2, size1) == 0); } case AVRO_ARRAY: { size_t count1; size_t count2; check_return(0, avro_value_get_size(val1, &count1)); check_return(0, avro_value_get_size(val2, &count2)); if (count1 != count2) { return 0; } size_t i; for (i = 0; i < count1; i++) { avro_value_t child1; avro_value_t child2; check_return(0, avro_value_get_by_index (val1, i, &child1, NULL)); check_return(0, avro_value_get_by_index (val2, i, &child2, NULL)); if (!avro_value_equal_fast(&child1, &child2)) { return 0; } } return 1; } case AVRO_ENUM: { int v1; int v2; check_return(0, avro_value_get_enum(val1, &v1)); check_return(0, avro_value_get_enum(val2, &v2)); return (v1 == v2); } case AVRO_FIXED: { const void *buf1; const void *buf2; size_t size1; size_t size2; check_return(0, avro_value_get_fixed(val1, &buf1, &size1)); check_return(0, avro_value_get_fixed(val2, &buf2, &size2)); if (size1 != size2) { return 0; } return (memcmp(buf1, buf2, size1) == 0); } case AVRO_MAP: { size_t count1; size_t count2; check_return(0, avro_value_get_size(val1, &count1)); check_return(0, avro_value_get_size(val2, &count2)); if (count1 != count2) { return 0; } size_t i; for (i = 0; i < count1; i++) { avro_value_t child1; avro_value_t child2; const char *key1; check_return(0, avro_value_get_by_index (val1, i, &child1, &key1)); check_return(0, avro_value_get_by_name (val2, key1, &child2, NULL)); if (!avro_value_equal_fast(&child1, &child2)) { return 0; } } return 1; } case AVRO_RECORD: { size_t count1; check_return(0, avro_value_get_size(val1, &count1)); size_t i; for (i = 0; i < count1; i++) { avro_value_t child1; avro_value_t child2; check_return(0, avro_value_get_by_index (val1, i, &child1, NULL)); check_return(0, avro_value_get_by_index (val2, i, &child2, NULL)); if (!avro_value_equal_fast(&child1, &child2)) { return 0; } } return 1; } case AVRO_UNION: { int disc1; int disc2; check_return(0, avro_value_get_discriminant(val1, &disc1)); check_return(0, avro_value_get_discriminant(val2, &disc2)); if (disc1 != disc2) { return 0; } avro_value_t branch1; avro_value_t branch2; check_return(0, avro_value_get_current_branch(val1, &branch1)); check_return(0, avro_value_get_current_branch(val2, &branch2)); return avro_value_equal_fast(&branch1, &branch2); } default: return 0; } }
int avro_value_cmp_fast(avro_value_t *val1, avro_value_t *val2) { avro_type_t type1 = avro_value_get_type(val1); avro_type_t type2 = avro_value_get_type(val2); if (type1 != type2) { return -1; } switch (type1) { case AVRO_BOOLEAN: { int v1; int v2; check_return(0, avro_value_get_boolean(val1, &v1)); check_return(0, avro_value_get_boolean(val2, &v2)); return cmp(!!v1, !!v2); } case AVRO_BYTES: { const void *buf1; const void *buf2; size_t size1; size_t size2; size_t min_size; int result; check_return(0, avro_value_get_bytes(val1, &buf1, &size1)); check_return(0, avro_value_get_bytes(val2, &buf2, &size2)); min_size = (size1 < size2)? size1: size2; result = memcmp(buf1, buf2, min_size); if (result != 0) { return result; } else { return cmp(size1, size2); } } case AVRO_DOUBLE: { double v1; double v2; check_return(0, avro_value_get_double(val1, &v1)); check_return(0, avro_value_get_double(val2, &v2)); return cmp(v1, v2); } case AVRO_FLOAT: { float v1; float v2; check_return(0, avro_value_get_float(val1, &v1)); check_return(0, avro_value_get_float(val2, &v2)); return cmp(v1, v2); } case AVRO_INT32: { int32_t v1; int32_t v2; check_return(0, avro_value_get_int(val1, &v1)); check_return(0, avro_value_get_int(val2, &v2)); return cmp(v1, v2); } case AVRO_INT64: { int64_t v1; int64_t v2; check_return(0, avro_value_get_long(val1, &v1)); check_return(0, avro_value_get_long(val2, &v2)); return cmp(v1, v2); } case AVRO_NULL: { check_return(0, avro_value_get_null(val1)); check_return(0, avro_value_get_null(val2)); return 0; } case AVRO_STRING: { const char *buf1; const char *buf2; size_t size1; size_t size2; size_t min_size; int result; check_return(0, avro_value_get_string(val1, &buf1, &size1)); check_return(0, avro_value_get_string(val2, &buf2, &size2)); min_size = (size1 < size2)? size1: size2; result = memcmp(buf1, buf2, min_size); if (result != 0) { return result; } else { return cmp(size1, size2); } } case AVRO_ARRAY: { size_t count1; size_t count2; size_t min_count; size_t i; check_return(0, avro_value_get_size(val1, &count1)); check_return(0, avro_value_get_size(val2, &count2)); min_count = (count1 < count2)? count1: count2; for (i = 0; i < min_count; i++) { avro_value_t child1; avro_value_t child2; int result; check_return(0, avro_value_get_by_index (val1, i, &child1, NULL)); check_return(0, avro_value_get_by_index (val2, i, &child2, NULL)); result = avro_value_cmp_fast(&child1, &child2); if (result != 0) { return result; } } return cmp(count1, count2); } case AVRO_ENUM: { int v1; int v2; check_return(0, avro_value_get_enum(val1, &v1)); check_return(0, avro_value_get_enum(val2, &v2)); return cmp(v1, v2); } case AVRO_FIXED: { const void *buf1; const void *buf2; size_t size1; size_t size2; check_return(0, avro_value_get_fixed(val1, &buf1, &size1)); check_return(0, avro_value_get_fixed(val2, &buf2, &size2)); if (size1 != size2) { return -1; } return memcmp(buf1, buf2, size1); } case AVRO_MAP: { return -1; } case AVRO_RECORD: { size_t count1; check_return(0, avro_value_get_size(val1, &count1)); size_t i; for (i = 0; i < count1; i++) { avro_value_t child1; avro_value_t child2; int result; check_return(0, avro_value_get_by_index (val1, i, &child1, NULL)); check_return(0, avro_value_get_by_index (val2, i, &child2, NULL)); result = avro_value_cmp_fast(&child1, &child2); if (result != 0) { return result; } } return 0; } case AVRO_UNION: { int disc1; int disc2; check_return(0, avro_value_get_discriminant(val1, &disc1)); check_return(0, avro_value_get_discriminant(val2, &disc2)); if (disc1 == disc2) { avro_value_t branch1; avro_value_t branch2; check_return(0, avro_value_get_current_branch(val1, &branch1)); check_return(0, avro_value_get_current_branch(val2, &branch2)); return avro_value_cmp_fast(&branch1, &branch2); } else { return cmp(disc1, disc2); } } default: return 0; } }