Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-slice bytes support #439

Merged
merged 27 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
58a73de
wip
DenisBiryukov91 Jun 20, 2024
eaff4a2
bytes tests and fixes
DenisBiryukov91 Jun 21, 2024
99abc68
merge
DenisBiryukov91 Jun 24, 2024
61afacc
warning fixes
DenisBiryukov91 Jun 24, 2024
ed88be7
use new zz_bytes on user side
DenisBiryukov91 Jun 24, 2024
38cbee0
rename zz_bytes -> z_bytes
DenisBiryukov91 Jun 24, 2024
f04f702
merge
DenisBiryukov91 Jun 24, 2024
b955bd0
Merge branch 'dev/fix_bytes' into dev/multi_slice_bytes
DenisBiryukov91 Jun 24, 2024
efdad63
fmt
DenisBiryukov91 Jun 24, 2024
b175b35
fixes
DenisBiryukov91 Jun 25, 2024
ea30ca1
fixes
DenisBiryukov91 Jun 25, 2024
5f8e54f
fixed svec implementation;
DenisBiryukov91 Jun 25, 2024
13d2224
fixe _z_sample_create signature
DenisBiryukov91 Jun 25, 2024
cd95ad2
fix _z_trigger_local_subscriptions signature
DenisBiryukov91 Jun 25, 2024
c673cd4
fix attachment examples
DenisBiryukov91 Jun 25, 2024
acc3f59
fixed uninitialized owned_bytes pointer
DenisBiryukov91 Jun 26, 2024
c33bf76
added z_bytes_len and z_bytes_is_empty functions; updated docs
DenisBiryukov91 Jun 26, 2024
1c20290
exposed z_bytes_reader_t
DenisBiryukov91 Jun 26, 2024
00f9577
remove _Z_DO_AND_RETURN_IF_ERR since _Z_CLEAN_RETURN_IF_ERR does the …
DenisBiryukov91 Jun 27, 2024
57e2022
serialization fixes
DenisBiryukov91 Jun 27, 2024
baf0d00
ensure that decoded z_bytes never contanin non-_is_alloc slice
DenisBiryukov91 Jun 27, 2024
f36e2e4
added z_bytes_writer implementation
DenisBiryukov91 Jun 27, 2024
f0c17b7
format
DenisBiryukov91 Jun 27, 2024
1bb55e5
fixed attachments
DenisBiryukov91 Jun 27, 2024
6c1cc65
use _ZP_UNUSED instead of (void)(...)
DenisBiryukov91 Jun 27, 2024
8cb3ef0
fixed _z_bytes_reader_read return value;
DenisBiryukov91 Jun 27, 2024
4ac44f8
Merge branch 'dev/1.0.0' into dev/multi_slice_bytes
DenisBiryukov91 Jun 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Data Structures
.. autoctype:: types.h::zp_send_keep_alive_options_t
.. autoctype:: types.h::zp_send_join_options_t
.. autoctype:: types.h::z_qos_t
.. autoctype:: types.h::z_bytes_reader_t
.. autoctype:: types.h::z_bytes_iterator_t


Owned Types
~~~~~~~~~~~
Expand Down Expand Up @@ -320,6 +323,15 @@ Primitives
.. autocfunction:: primitives.h::z_bytes_serialize_from_slice_copy
.. autocfunction:: primitives.h::z_bytes_serialize_from_string
.. autocfunction:: primitives.h::z_bytes_serialize_from_string_copy
.. autocfunction:: primitives.h::z_bytes_empty
.. autocfunction:: primitives.h::z_bytes_len
.. autocfunction:: primitives.h::z_bytes_is_empty
.. autocfunction:: primitives.h::z_bytes_get_iterator
.. autocfunction:: primitives.h::z_bytes_iterator_next
.. autocfunction:: primitives.h::z_bytes_get_reader
.. autocfunction:: primitives.h::z_bytes_reader_read
.. autocfunction:: primitives.h::z_bytes_reader_seek
.. autocfunction:: primitives.h::z_bytes_reader_tell
.. autocfunction:: primitives.h::z_timestamp_check
.. autocfunction:: primitives.h::z_query_target_default
.. autocfunction:: primitives.h::z_query_consolidation_auto
Expand Down
26 changes: 8 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,26 @@ 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_pair(z_loan(kv), &first, &second);
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 +210,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
15 changes: 8 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,26 @@ 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_pair(z_loan(kv), &first, &second);
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 +143,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
9 changes: 5 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,11 @@ 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_pair(z_loan(kv), &first, &second);
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
131 changes: 120 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,140 @@ 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);

/**
* Parameters:
* bytes: Pointer to an unitialized :c:type:`z_lowned_bytes_t` instance.
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_empty(z_owned_bytes_t *bytes);

/**
* Returns total number of bytes in the container.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* Return:
* Number of bytes in the container.
*/
size_t z_bytes_len(const z_loaned_bytes_t *bytes);

/**
* Checks if container is empty
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* Return:
* ``true`` if conainer is empty, ``false`` otherwise.
*/
_Bool z_bytes_is_empty(const z_loaned_bytes_t *bytes);

/**
* 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);

/**
* Returns a reader for the `bytes`.
*
* The `bytes` should outlive the reader and should not be modified, while reader is in use.
*
* Parameters:
* bytes: Data to read.
*
* Return:
* The constructed :c:type:`z_bytes_reader_t`.
*/
z_bytes_reader_t z_bytes_get_reader(const z_loaned_bytes_t *bytes);

/**
* Reads data into specified destination.
*
* Parameters:
* reader: Data reader to read from.
* dst: Buffer where the read data is written.
* len: Maximum number of bytes to read.
*
* Return:
* Number of bytes read. If return value is smaller than `len`, it means that the end of the data was reached.
*/
size_t z_bytes_reader_read(z_bytes_reader_t *reader, uint8_t *dst, size_t len);
/**
* Sets the `reader` position indicator for the payload to the value pointed to by offset.
* The new position is exactly `offset` bytes measured from the beginning of the payload if origin is `SEEK_SET`,
* from the current reader position if origin is `SEEK_CUR`, and from the end of the payload if origin is `SEEK_END`.
*
* Parameters:
* reader: Data reader to reposition.
* offset: New position ffset in bytes.
* origin: Origin for the new position.
*
* Return:
* ​0​ upon success, negative error code otherwise.
*/
int8_t z_bytes_reader_seek(z_bytes_reader_t *reader, int64_t offset, int origin);
/**
* Gets the read position indicator.
*
* Parameters:
* reader: Data reader to get position of.
*
* Return:
* Read position indicator on success or -1L if failure occurs.
*/
int64_t z_bytes_reader_tell(z_bytes_reader_t *reader);

/**
* 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
11 changes: 11 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,16 @@ _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;

/**
* A reader for serialized data.
*/
typedef _z_bytes_reader_t z_bytes_reader_t;

/**
* Represents a string without null-terminator.
*
Expand Down
Loading
Loading