sf_serializer.[hc]: support id_name_array
parent
97b64c67fb
commit
320f344b3e
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
#define SF_BINLOG_PARSE_INT_SILENCE(var, caption, index, endchr, min_val) \
|
#define SF_BINLOG_PARSE_INT_SILENCE(var, caption, index, endchr, min_val) \
|
||||||
do { \
|
do { \
|
||||||
var = strtol(cols[index].str, &endptr, 10); \
|
var = strtoll(cols[index].str, &endptr, 10); \
|
||||||
if (*endptr != endchr || var < min_val) { \
|
if (*endptr != endchr || var < min_val) { \
|
||||||
sprintf(error_info, "invalid %s: %.*s", \
|
sprintf(error_info, "invalid %s: %.*s", \
|
||||||
caption, cols[index].len, cols[index].str); \
|
caption, cols[index].len, cols[index].str); \
|
||||||
|
|
@ -32,7 +32,7 @@
|
||||||
|
|
||||||
#define SF_BINLOG_PARSE_INT_SILENCE2(var, caption, index, echr1, echr2, min_val) \
|
#define SF_BINLOG_PARSE_INT_SILENCE2(var, caption, index, echr1, echr2, min_val) \
|
||||||
do { \
|
do { \
|
||||||
var = strtol(cols[index].str, &endptr, 10); \
|
var = strtoll(cols[index].str, &endptr, 10); \
|
||||||
if (!(*endptr == echr1 || *endptr == echr2) || (var < min_val)) { \
|
if (!(*endptr == echr1 || *endptr == echr2) || (var < min_val)) { \
|
||||||
sprintf(error_info, "invalid %s: %.*s", \
|
sprintf(error_info, "invalid %s: %.*s", \
|
||||||
caption, cols[index].len, cols[index].str); \
|
caption, cols[index].len, cols[index].str); \
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,8 @@ static SFSerializerTypeConfig value_type_configs[SF_SERIALIZER_VALUE_TYPE_COUNT]
|
||||||
{"int64_array", sizeof(SFSerializerPackFieldArray), 8},
|
{"int64_array", sizeof(SFSerializerPackFieldArray), 8},
|
||||||
{"string_array", sizeof(SFSerializerPackFieldArray),
|
{"string_array", sizeof(SFSerializerPackFieldArray),
|
||||||
sizeof(SFSerializerPackStringValue)},
|
sizeof(SFSerializerPackStringValue)},
|
||||||
|
{"id_name_array", sizeof(SFSerializerPackFieldArray),
|
||||||
|
sizeof(int64_t) + sizeof(SFSerializerPackStringValue)},
|
||||||
{"map", sizeof(SFSerializerPackFieldArray), 2 *
|
{"map", sizeof(SFSerializerPackFieldArray), 2 *
|
||||||
sizeof(SFSerializerPackStringValue)}
|
sizeof(SFSerializerPackStringValue)}
|
||||||
};
|
};
|
||||||
|
|
@ -283,6 +285,54 @@ static int unpack_string_array(SFSerializerIterator *it, const int remain_len)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int unpack_id_name_array(SFSerializerIterator *it, const int remain_len)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
int count;
|
||||||
|
id_name_pair_t *pair;
|
||||||
|
id_name_pair_t *end;
|
||||||
|
|
||||||
|
if ((result=unpack_array_count(it, remain_len, &count)) != 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count > it->id_name_array_alloc) {
|
||||||
|
if ((result=array_expand(it, (void_array_t *)&it->id_name_array,
|
||||||
|
sizeof(id_name_pair_t), count,
|
||||||
|
&it->id_name_array_alloc)) != 0)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it->p += sizeof(SFSerializerPackFieldArray);
|
||||||
|
end = it->id_name_array.elts + count;
|
||||||
|
for (pair=it->id_name_array.elts; pair<end; pair++) {
|
||||||
|
if ((it->end - it->p) < (sizeof(int64_t) +
|
||||||
|
sizeof(SFSerializerPackStringValue)))
|
||||||
|
{
|
||||||
|
snprintf(it->error_info, sizeof(it->error_info),
|
||||||
|
FIELD_ID_AND_TYPE_FORMAT", remain length: %d "
|
||||||
|
"is too small < %d", FIELD_ID_AND_TYPE_PARAMS,
|
||||||
|
(int)(it->end - it->p), (int)(sizeof(int64_t) +
|
||||||
|
sizeof(SFSerializerPackStringValue)));
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
pair->id = buff2long(it->p);
|
||||||
|
it->p += sizeof(int64_t);
|
||||||
|
if ((result=unpack_string(it, it->end - it->p,
|
||||||
|
(SFSerializerPackStringValue *)it->p,
|
||||||
|
&pair->name)) != 0)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
it->id_name_array.count = count;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int unpack_map(SFSerializerIterator *it, const int remain_len)
|
static int unpack_map(SFSerializerIterator *it, const int remain_len)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
@ -402,6 +452,14 @@ const SFSerializerFieldValue *sf_serializer_next(SFSerializerIterator *it)
|
||||||
}
|
}
|
||||||
it->field.value.str_array = it->str_array;
|
it->field.value.str_array = it->str_array;
|
||||||
break;
|
break;
|
||||||
|
case sf_serializer_value_type_id_name_array:
|
||||||
|
if ((it->error_no=unpack_id_name_array(it, remain_len -
|
||||||
|
sizeof(SFSerializerPackFieldArray))) != 0)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
it->field.value.id_name_array = it->id_name_array;
|
||||||
|
break;
|
||||||
case sf_serializer_value_type_map:
|
case sf_serializer_value_type_map:
|
||||||
if ((it->error_no=unpack_map(it, remain_len - sizeof(
|
if ((it->error_no=unpack_map(it, remain_len - sizeof(
|
||||||
SFSerializerPackFieldArray))) != 0)
|
SFSerializerPackFieldArray))) != 0)
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@
|
||||||
#include "fastcommon/fast_buffer.h"
|
#include "fastcommon/fast_buffer.h"
|
||||||
#include "fastcommon/hash.h"
|
#include "fastcommon/hash.h"
|
||||||
|
|
||||||
#define SF_SERIALIZER_VALUE_TYPE_COUNT 11
|
#define SF_SERIALIZER_VALUE_TYPE_COUNT 12
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
sf_serializer_value_type_int8 = 0,
|
sf_serializer_value_type_int8 = 0,
|
||||||
|
|
@ -36,6 +36,7 @@ typedef enum {
|
||||||
sf_serializer_value_type_int32_array,
|
sf_serializer_value_type_int32_array,
|
||||||
sf_serializer_value_type_int64_array,
|
sf_serializer_value_type_int64_array,
|
||||||
sf_serializer_value_type_string_array,
|
sf_serializer_value_type_string_array,
|
||||||
|
sf_serializer_value_type_id_name_array,
|
||||||
sf_serializer_value_type_map
|
sf_serializer_value_type_map
|
||||||
} SFSerializerValueType;
|
} SFSerializerValueType;
|
||||||
|
|
||||||
|
|
@ -94,8 +95,9 @@ typedef struct sf_serializer_field_value {
|
||||||
int64_t n;
|
int64_t n;
|
||||||
string_t s;
|
string_t s;
|
||||||
int64_array_t int_array;
|
int64_array_t int_array;
|
||||||
key_value_array_t kv_array;
|
|
||||||
string_array_t str_array;
|
string_array_t str_array;
|
||||||
|
id_name_array_t id_name_array;
|
||||||
|
key_value_array_t kv_array;
|
||||||
} value;
|
} value;
|
||||||
} SFSerializerFieldValue;
|
} SFSerializerFieldValue;
|
||||||
|
|
||||||
|
|
@ -103,11 +105,13 @@ typedef struct sf_serializer_iterator {
|
||||||
const char *p;
|
const char *p;
|
||||||
const char *end;
|
const char *end;
|
||||||
int64_array_t int_array; //int64_t array holder
|
int64_array_t int_array; //int64_t array holder
|
||||||
key_value_array_t kv_array; //key-value array holder
|
|
||||||
string_array_t str_array; //string_t array holder
|
string_array_t str_array; //string_t array holder
|
||||||
|
id_name_array_t id_name_array; //id name array holder
|
||||||
|
key_value_array_t kv_array; //key value array holder
|
||||||
int int_array_alloc;
|
int int_array_alloc;
|
||||||
int kv_array_alloc;
|
|
||||||
int str_array_alloc;
|
int str_array_alloc;
|
||||||
|
int id_name_array_alloc;
|
||||||
|
int kv_array_alloc;
|
||||||
SFSerializerFieldValue field;
|
SFSerializerFieldValue field;
|
||||||
int error_no;
|
int error_no;
|
||||||
char error_info[256];
|
char error_info[256];
|
||||||
|
|
@ -224,10 +228,9 @@ static inline int sf_serializer_pack_integer(FastBuffer *buffer,
|
||||||
int2buff((value)->len, (ps)->len); \
|
int2buff((value)->len, (ps)->len); \
|
||||||
memcpy((ps)->str, (value)->str, (value)->len)
|
memcpy((ps)->str, (value)->str, (value)->len)
|
||||||
|
|
||||||
#define SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(ps, value) \
|
#define SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(p, value) \
|
||||||
SF_SERIALIZER_PACK_STRING(ps, value); \
|
SF_SERIALIZER_PACK_STRING((SFSerializerPackStringValue *)p, value); \
|
||||||
ps = (SFSerializerPackStringValue *)(((char *)ps) + sizeof( \
|
p += (sizeof(SFSerializerPackStringValue) + (value)->len)
|
||||||
SFSerializerPackStringValue) + (value)->len)
|
|
||||||
|
|
||||||
static inline int sf_serializer_pack_string(FastBuffer *buffer,
|
static inline int sf_serializer_pack_string(FastBuffer *buffer,
|
||||||
const unsigned char fid, const string_t *value)
|
const unsigned char fid, const string_t *value)
|
||||||
|
|
@ -373,7 +376,7 @@ static inline int sf_serializer_pack_string_array(FastBuffer *buffer,
|
||||||
SFSerializerPackFieldArray *obj;
|
SFSerializerPackFieldArray *obj;
|
||||||
const string_t *str;
|
const string_t *str;
|
||||||
const string_t *end;
|
const string_t *end;
|
||||||
SFSerializerPackStringValue *ps;
|
char *p;
|
||||||
|
|
||||||
length = sizeof(SFSerializerPackFieldArray);
|
length = sizeof(SFSerializerPackFieldArray);
|
||||||
end = strings + count;
|
end = strings + count;
|
||||||
|
|
@ -390,9 +393,46 @@ static inline int sf_serializer_pack_string_array(FastBuffer *buffer,
|
||||||
obj->field.type = sf_serializer_value_type_string_array;
|
obj->field.type = sf_serializer_value_type_string_array;
|
||||||
int2buff(count, obj->value.count);
|
int2buff(count, obj->value.count);
|
||||||
|
|
||||||
ps = (SFSerializerPackStringValue *)obj->value.ptr;
|
p = obj->value.ptr;
|
||||||
for (str=strings; str<end; str++) {
|
for (str=strings; str<end; str++) {
|
||||||
SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(ps, str);
|
SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(p, str);
|
||||||
|
}
|
||||||
|
buffer->length += length;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int sf_serializer_pack_id_name_array(FastBuffer *buffer,
|
||||||
|
const unsigned char fid, const id_name_pair_t *in_pairs,
|
||||||
|
const int count)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
int length;
|
||||||
|
SFSerializerPackFieldArray *obj;
|
||||||
|
const id_name_pair_t *pair;
|
||||||
|
const id_name_pair_t *end;
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
length = sizeof(SFSerializerPackFieldArray);
|
||||||
|
end = in_pairs + count;
|
||||||
|
for (pair=in_pairs; pair<end; pair++) {
|
||||||
|
length += sizeof(int64_t) + pair->name.len +
|
||||||
|
sizeof(SFSerializerPackStringValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((result=fast_buffer_check_inc_size(buffer, length)) != 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = (SFSerializerPackFieldArray *)(buffer->data + buffer->length);
|
||||||
|
obj->field.id = fid;
|
||||||
|
obj->field.type = sf_serializer_value_type_id_name_array;
|
||||||
|
int2buff(count, obj->value.count);
|
||||||
|
|
||||||
|
p = obj->value.ptr;
|
||||||
|
for (pair=in_pairs; pair<end; pair++) {
|
||||||
|
long2buff(pair->id, p);
|
||||||
|
p += sizeof(int64_t);
|
||||||
|
SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(p, &pair->name);
|
||||||
}
|
}
|
||||||
buffer->length += length;
|
buffer->length += length;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -407,7 +447,7 @@ static inline int sf_serializer_pack_map(FastBuffer *buffer,
|
||||||
SFSerializerPackFieldArray *obj;
|
SFSerializerPackFieldArray *obj;
|
||||||
const key_value_pair_t *pair;
|
const key_value_pair_t *pair;
|
||||||
const key_value_pair_t *end;
|
const key_value_pair_t *end;
|
||||||
SFSerializerPackStringValue *ps;
|
char *p;
|
||||||
|
|
||||||
length = sizeof(SFSerializerPackFieldArray);
|
length = sizeof(SFSerializerPackFieldArray);
|
||||||
end = kv_pairs + count;
|
end = kv_pairs + count;
|
||||||
|
|
@ -425,10 +465,10 @@ static inline int sf_serializer_pack_map(FastBuffer *buffer,
|
||||||
obj->field.type = sf_serializer_value_type_map;
|
obj->field.type = sf_serializer_value_type_map;
|
||||||
int2buff(count, obj->value.count);
|
int2buff(count, obj->value.count);
|
||||||
|
|
||||||
ps = (SFSerializerPackStringValue *)obj->value.ptr;
|
p = obj->value.ptr;
|
||||||
for (pair=kv_pairs; pair<end; pair++) {
|
for (pair=kv_pairs; pair<end; pair++) {
|
||||||
SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(ps, &pair->key);
|
SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(p, &pair->key);
|
||||||
SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(ps, &pair->value);
|
SF_SERIALIZER_PACK_STRING_AND_MOVE_PTR(p, &pair->value);
|
||||||
}
|
}
|
||||||
buffer->length += length;
|
buffer->length += length;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue