Single encode function

pull/4750/head
Alistair Veitch 9 years ago
parent d409e3bf41
commit c45d088ae7
  1. 29
      include/grpc/census.h
  2. 23
      src/core/census/tag_set.c
  3. 20
      test/core/census/tag_set_test.c

@ -433,17 +433,24 @@ int census_tag_set_get_tag_by_key(const census_tag_set *tags, const char *key,
for use by RPC systems only, for purposes of transmitting/receiving tag
sets. */
/* Encode to-be-propagated non-binary tags from a tag set into a memory
buffer. The total number of bytes used in the buffer is returned. If the
buffer is too small to contain the encoded tag set, then 0 is returned. */
size_t census_tag_set_encode_propagated(const census_tag_set *tags,
char *buffer, size_t buf_size);
/* Encode to-be-propagated binary tags from a tag set into a memory
buffer. The total number of bytes used in the buffer is returned. If the
buffer is too small to contain the encoded tag set, then 0 is returned. */
size_t census_tag_set_encode_propagated_binary(const census_tag_set *tags,
char *buffer, size_t buf_size);
/* Encode a tag set into a buffer. The propagated tags are encoded into the
buffer in two regions: one for printable tags, and one for binary tags.
@param tags tag set to be encoded
@param buffer pointer to buffer. This address will be used to encode the
printable tags.
@param buf_size On input, will be a pointer to total buffer size. On output,
will be set to total number of bytes consumed by printable
tags.
@param bin_buf_size on output, will be set to the number of bytes used to
encode the binary tags.
@return A pointer to the binary tag's encoded, or NULL if the buffer was
insufficiently large to hold the encoded tags. Thus, if successful,
printable tags are encoded into
[buffer, buffer + *buf_size) and binary tags into
[returned-ptr, returned-ptr + *bin_buf_size) (and the return value
should be buffer + *buf_size) */
char *census_tag_set_encode(const census_tag_set *tags, char *buffer,
size_t *buf_size, size_t *bin_buf_size);
/* Decode tag set buffers encoded with census_tag_set_encode_*(). Returns NULL
if there is an error in parsing either buffer. */

@ -453,14 +453,21 @@ static size_t tag_set_encode(const struct tag_set *tags, char *buffer,
return ENCODED_HEADER_SIZE + tags->kvm_used;
}
size_t census_tag_set_encode_propagated(const census_tag_set *tags,
char *buffer, size_t buf_size) {
return tag_set_encode(&tags->tags[PROPAGATED_TAGS], buffer, buf_size);
}
size_t census_tag_set_encode_propagated_binary(const census_tag_set *tags,
char *buffer, size_t buf_size) {
return tag_set_encode(&tags->tags[PROPAGATED_BINARY_TAGS], buffer, buf_size);
char *census_tag_set_encode(const census_tag_set *tags, char *buffer,
size_t *buf_size, size_t *bin_buf_size) {
size_t p_buf_size =
tag_set_encode(&tags->tags[PROPAGATED_TAGS], buffer, *buf_size);
if (p_buf_size == 0) {
return NULL;
}
char *b_buffer = buffer + p_buf_size;
*bin_buf_size = tag_set_encode(&tags->tags[PROPAGATED_BINARY_TAGS], b_buffer,
*buf_size - p_buf_size);
if (*bin_buf_size == 0) {
return NULL;
}
*buf_size = p_buf_size;
return b_buffer;
}
// Decode a tag set.

@ -321,17 +321,19 @@ static void replace_add_delete_test(void) {
// test encode/decode.
static void encode_decode_test(void) {
char buf1[1000];
char buf2[1000];
const size_t BUF_SIZE = 200;
char buffer[BUF_SIZE];
struct census_tag_set *cts =
census_tag_set_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
GPR_ASSERT(census_tag_set_encode_propagated(cts, buf1, 1) == 0);
size_t b1 = census_tag_set_encode_propagated(cts, buf1, 1000);
GPR_ASSERT(b1 != 0);
GPR_ASSERT(census_tag_set_encode_propagated_binary(cts, buf2, 1) == 0);
size_t b2 = census_tag_set_encode_propagated_binary(cts, buf2, 1000);
GPR_ASSERT(b2 != 0);
census_tag_set *cts2 = census_tag_set_decode(buf1, b1, buf2, b2);
size_t bsize = 2; // buffer size too small
size_t bin_bsize = 0;
GPR_ASSERT(census_tag_set_encode(cts, buffer, &bsize, &bin_bsize) == NULL);
bsize = BUF_SIZE;
char *b_buffer = census_tag_set_encode(cts, buffer, &bsize, &bin_bsize);
GPR_ASSERT(b_buffer != NULL && bsize > 0 && bin_bsize > 0 &&
bsize + bin_bsize <= BUF_SIZE && b_buffer == buffer + bsize);
census_tag_set *cts2 =
census_tag_set_decode(buffer, bsize, b_buffer, bin_bsize);
GPR_ASSERT(cts2 != NULL);
const census_tag_set_create_status *status =
census_tag_set_get_create_status(cts2);

Loading…
Cancel
Save