Skip to content

Commit

Permalink
Merge branch 'dev/fix_bytes' into dev/multi_slice_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Jun 24, 2024
2 parents d67ea84 + f04f702 commit b955bd0
Show file tree
Hide file tree
Showing 45 changed files with 1,462 additions and 527 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ if(UNIX OR MSVC)
add_executable(z_test_fragment_rx ${PROJECT_SOURCE_DIR}/tests/z_test_fragment_rx.c)
add_executable(z_perf_tx ${PROJECT_SOURCE_DIR}/tests/z_perf_tx.c)
add_executable(z_perf_rx ${PROJECT_SOURCE_DIR}/tests/z_perf_rx.c)
add_executable(z_bytes_test ${PROJECT_SOURCE_DIR}/tests/z_bytes_test.c)

target_link_libraries(z_data_struct_test ${Libname})
target_link_libraries(z_channels_test ${Libname})
Expand All @@ -361,6 +362,7 @@ if(UNIX OR MSVC)
target_link_libraries(z_test_fragment_rx ${Libname})
target_link_libraries(z_perf_tx ${Libname})
target_link_libraries(z_perf_rx ${Libname})
target_link_libraries(z_bytes_test ${Libname})

configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY)
configure_file(${PROJECT_SOURCE_DIR}/tests/raweth.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/raweth.py COPYONLY)
Expand All @@ -378,6 +380,7 @@ if(UNIX OR MSVC)
add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test)
add_test(z_api_null_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_null_drop_test)
add_test(z_api_double_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_double_drop_test)
add_test(z_bytes_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_bytes_test)
endif()

if(BUILD_MULTICAST)
Expand Down
25 changes: 7 additions & 18 deletions examples/unix/c11/z_get_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,25 @@ typedef struct kv_pairs_rx_t {
static z_condvar_t cond;
static z_mutex_t mutex;

size_t kv_pairs_size(kv_pairs_tx_t *kvp) {
size_t ret = 0;
for (size_t i = 0; i < kvp->len; i++) {
// Size fields
ret += 2 * sizeof(uint32_t);
// Data size
ret += strlen(kvp->data[i].key) + strlen(kvp->data[i].value);
}
return ret;
}

_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *curr_idx) {
_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) {
kv_pairs_tx_t *kvs = (kv_pairs_tx_t *)(context);
z_owned_bytes_t k, v;
if (kvs->current_idx >= kvs->len) {
return false;
} else {
z_bytes_serialize_from_string(&k, kvs->data[kvs->current_idx].key);
z_bytes_serialize_from_string(&v, kvs->data[kvs->current_idx].value);
zp_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v), curr_idx);
z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v));
kvs->current_idx++;
return true;
}
}

void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) {
size_t curr_idx = 0;
z_owned_bytes_t first, second;
while ((kvp->current_idx < kvp->len) &&
(zp_bytes_deserialize_into_pair(attachment, &first, &second, &curr_idx) == 0)) {
z_owned_bytes_t kv, first, second;
z_bytes_iterator_t iter = z_bytes_get_iterator(attachment);

while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) {
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
Expand Down Expand Up @@ -220,7 +209,7 @@ int main(int argc, char **argv) {
kvs[0] = (kv_pair_t){.key = "test_key", .value = "test_value"};
kv_pairs_tx_t ctx = (kv_pairs_tx_t){.data = kvs, .current_idx = 0, .len = 1};
z_owned_bytes_t attachment;
zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx, kv_pairs_size(&ctx));
z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx);
opts.attachment = z_move(attachment);

z_owned_closure_reply_t callback;
Expand Down
18 changes: 3 additions & 15 deletions examples/unix/c11/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,15 @@ typedef struct kv_pairs_t {

#if Z_FEATURE_PUBLICATION == 1

// Return the total serialized size of the key value pairs
size_t kv_pairs_size(kv_pairs_t *kvp) {
size_t ret = 0;
for (size_t i = 0; i < kvp->len; i++) {
// Size fields
ret += 2 * sizeof(uint32_t);
// Data size
ret += strlen(kvp->data[i].key) + strlen(kvp->data[i].value);
}
return ret;
}

_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *curr_idx) {
_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) {
kv_pairs_t *kvs = (kv_pairs_t *)(context);
z_owned_bytes_t k, v;
if (kvs->current_idx >= kvs->len) {
return false;
} else {
z_bytes_serialize_from_string(&k, kvs->data[kvs->current_idx].key);
z_bytes_serialize_from_string(&v, kvs->data[kvs->current_idx].value);
zp_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v), curr_idx);
z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v));
kvs->current_idx++;
return true;
}
Expand Down Expand Up @@ -169,7 +157,7 @@ int main(int argc, char **argv) {
sprintf(buf_ind, "%d", idx);
kvs[1] = (kv_pair_t){.key = "index", .value = buf_ind};
kv_pairs_t ctx = (kv_pairs_t){.data = kvs, .current_idx = 0, .len = 2};
zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx, kv_pairs_size(&ctx));
z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx);
options.attachment = z_move(attachment);

z_publisher_put(z_loan(pub), z_move(payload), &options);
Expand Down
14 changes: 7 additions & 7 deletions examples/unix/c11/z_queryable_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ size_t kv_pairs_size(kv_pairs_tx_t *kvp) {
return ret;
}

_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *curr_idx) {
_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) {
kv_pairs_tx_t *kvs = (kv_pairs_tx_t *)(context);
z_owned_bytes_t k, v;
if (kvs->current_idx >= kvs->len) {
return false;
} else {
z_bytes_serialize_from_string(&k, kvs->data[kvs->current_idx].key);
z_bytes_serialize_from_string(&v, kvs->data[kvs->current_idx].value);
zp_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v), curr_idx);
z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v));
kvs->current_idx++;
return true;
}
}

void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) {
size_t curr_idx = 0;
z_owned_bytes_t first, second;
while ((kvp->current_idx < kvp->len) &&
(zp_bytes_deserialize_into_pair(attachment, &first, &second, &curr_idx) == 0)) {
z_owned_bytes_t kv, first, second;
z_bytes_iterator_t iter = z_bytes_get_iterator(attachment);

while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) {
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
Expand Down Expand Up @@ -142,7 +142,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
kvs[0] = (kv_pair_t){.key = "reply_key", .value = "reply_value"};
kv_pairs_tx_t kv_ctx = (kv_pairs_tx_t){.data = kvs, .current_idx = 0, .len = 1};
z_owned_bytes_t attachment;
zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&kv_ctx, kv_pairs_size(&kv_ctx));
z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&kv_ctx);
options.attachment = z_move(attachment);

z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options);
Expand Down
8 changes: 4 additions & 4 deletions examples/unix/c11/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ typedef struct kv_pairs_t {
static int msg_nb = 0;

void parse_attachment(kv_pairs_t *kvp, const z_loaned_bytes_t *attachment) {
size_t curr_idx = 0;
z_owned_bytes_t first, second;
while ((kvp->current_idx < kvp->len) &&
(zp_bytes_deserialize_into_pair(attachment, &first, &second, &curr_idx) == 0)) {
z_owned_bytes_t kv, first, second;
z_bytes_iterator_t iter = z_bytes_get_iterator(attachment);

while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) {
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
Expand Down
40 changes: 29 additions & 11 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,11 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* first: Pointer to an uninitialized :c:type:`z_owned_bytes_t` to contain the first element.
* second: Pointer to an uninitialized :c:type:`z_owned_bytes_t` to contain the second element.
* curr_idx: Pointer to the current decoding index.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t zp_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second,
size_t *curr_idx);
int8_t z_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second);

/**
* Encodes a signed integer into a :c:type:`z_owned_bytes_t`
Expand Down Expand Up @@ -798,29 +796,49 @@ int8_t z_bytes_serialize_from_string_copy(z_owned_bytes_t *bytes, const char *s)
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded payload.
* iterator_body: Iterator body function, providing data items. Returning false is treated as iteration end.
* context: Arbitrary context that will be passed to iterator_body.
* total_len: The length of all the items to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t zp_bytes_serialize_from_iter(z_owned_bytes_t *bytes,
_Bool (*iterator_body)(z_owned_bytes_t *data, void *context, size_t *curr_idx),
void *context, size_t total_len);
int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context),
void *context);

/**
* Append a pair of `z_owned_bytes` objects which are consumed in the process.
*
* Parameters:
* bytes: An pre-initialized :c:type:`z_owned_bytes_t` to contain the encoded pair.
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded pair.
* first: Pointer to the first `z_owned_bytes` to encode.
* second: Pointer to the second `z_owned_bytes` to encode.
* curr_idx: Pointer to the current encoding index value.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t zp_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second,
size_t *curr_idx);
int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second);

/**
* Returns an iterator for multi-element serialized data.
*
* Parameters:
* bytes: Data to iterate over.
*
* Return:
* The constructed :c:type:`z_bytes_iterator_t`.
*/
z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes);

/**
* Constructs :c:type:`z_owned_bytes_t` object corresponding to the next element of serialized data.
*
* Will construct null-state `z_owned_bytes_t` when iterator reaches the end (or in case of error).
*
* Parameters:
* iter: An iterator over multi-element serialized data.
* out: An uninitialized :c:type:`z_owned_bytes_t` that will contained next serialized element.
* Return:
* ``false`` when iterator reaches the end, ``true`` otherwise
*/
_Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *out);

/**
* Checks validity of a timestamp
Expand Down
6 changes: 6 additions & 0 deletions include/zenoh-pico/api/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define INCLUDE_ZENOH_PICO_API_TYPES_H

#include "olv_macros.h"
#include "zenoh-pico/collections/bytes.h"
#include "zenoh-pico/collections/element.h"
#include "zenoh-pico/collections/list.h"
#include "zenoh-pico/collections/slice.h"
Expand Down Expand Up @@ -74,6 +75,11 @@ _Z_LOANED_TYPE(_z_slice_t, slice)
_Z_OWNED_TYPE_PTR(_z_bytes_t, bytes)
_Z_LOANED_TYPE(_z_bytes_t, bytes)

/**
* An iterator over multi-element serialized data
*/
typedef _z_bytes_iterator_t z_bytes_iterator_t;

/**
* Represents a string without null-terminator.
*
Expand Down
55 changes: 55 additions & 0 deletions include/zenoh-pico/collections/arc_slice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

#ifndef ZENOH_PICO_COLLECTIONS_ARC_SLICE_H
#define ZENOH_PICO_COLLECTIONS_ARC_SLICE_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include "refcount.h"
#include "slice.h"
#include "zenoh-pico/system/platform-common.h"

_Z_REFCOUNT_DEFINE(_z_slice, _z_slice)

/*-------- ArcSlice --------*/
/**
* An atomically reference counted subslice.
*
* Members:
* _z_slice_rc_t len: Rc counted slice.
* size_t start: Offset to the subslice start.
* size_t len: Length of the subslice.
*/

typedef struct {
_z_slice_rc_t slice;
size_t start;
size_t len;
} _z_arc_slice_t;

_z_arc_slice_t _z_arc_slice_empty(void);
_z_arc_slice_t _z_arc_slice_wrap(_z_slice_t s, size_t offset, size_t len);
_z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, size_t len);
size_t _z_arc_slice_len(const _z_arc_slice_t* s);
_Bool _z_arc_slice_is_empty(const _z_arc_slice_t* s);
const uint8_t* _z_arc_slice_data(const _z_arc_slice_t* s);
int8_t _z_arc_slice_copy(_z_arc_slice_t* dst, const _z_arc_slice_t* src);
int8_t _z_arc_slice_move(_z_arc_slice_t* dst, _z_arc_slice_t* src);
int8_t _z_arc_slice_drop(_z_arc_slice_t* s);

#endif /* ZENOH_PICO_COLLECTIONS_ARC_SLICE_H */
Loading

0 comments on commit b955bd0

Please sign in to comment.