BewareMyPower commented on code in PR #294:
URL: https://github.com/apache/pulsar-client-cpp/pull/294#discussion_r1253029851
##########
include/pulsar/c/client.h:
##########
@@ -172,6 +175,54 @@ PULSAR_PUBLIC void
pulsar_client_create_reader_async(pulsar_client_t *client, co
const pulsar_message_id_t
*startMessageId,
pulsar_reader_configuration_t *conf,
pulsar_reader_callback
callback, void *ctx);
+/**
+ * Create a table view with given {@code table_view_configuration} for
specified topic.
+ *
+ * The TableView provides a key-value map view of a compacted topic. Messages
without keys will
+ * be ignored.
+ *
+ * NOTE:
+ * When the result in the callback is `ResultOk`, `*c_tableView` will point to
the memory that
+ * is allocated internally. You have to call `pulsar_table_view_free` to free
it.
+ *
+ * Example:
+ * ```c
+ * pulsar_table_view_configuration_t *table_view_conf =
pulsar_table_view_configuration_create();
+ * pulsar_table_view_configuration_set_subscription_name(table_view_conf,
sub_name);
+ * pulsar_table_view_t *table_view;
+ * pulsar_result result = pulsar_client_create_table_view(client, topic_name,
table_view_conf, &table_view);
+ *
+ * // do something...
+ *
+ * pulsar_table_view_close(table_view);
+ * pulsar_table_view_free(table_view);
+ * pulsar_table_view_configuration_free(table_view_conf);
+ *
+ * ```
+ *
+ * @param topic The name of the topic.
+ * @param conf The {@code table_view_configuration} pointer.
+ * @param c_tableView The pointer of the table_view pointer
+ * @return Returned when the table_view is successfully linked to the topic
and the map is built from a
+ * message that already exists.
+ */
+PULSAR_PUBLIC pulsar_result pulsar_client_create_table_view(pulsar_client_t
*client, const char *topic,
+
pulsar_table_view_configuration_t *conf,
+
pulsar_table_view_t **c_tableView);
+
+/**
+ * Async Create a table view with given {@code table_view_configuration} for
specified topic.
+ * @param topic The name of the topic.
+ * @param conf The {@code table_view_configuration} pointer.
+ * @param callback
+ * 1. When the result in the callback is `ResultOk`, `tableView` in the
callback will point to the memory that
+ * is allocated internally. You have to call `pulsar_table_view_free` to free
it.
+ * 2. If the result in the callback is not `ResultOk`, `tableView` in the
callback will be nullptr.
+ * @param ctx
+ */
+PULSAR_PUBLIC void pulsar_client_create_table_view_async(pulsar_client_t
*client, const char *topic,
Review Comment:
Could you add a test for this method?
##########
lib/c/c_TableView.cc:
##########
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <pulsar/c/table_view.h>
+#include <string.h>
+
+#include "c_structs.h"
+
+static void *malloc_and_copy(const char *s, size_t slen) {
+ void *result = (void *)malloc(slen);
+ if (result == NULL) {
+ return NULL;
Review Comment:
You should perform the null check everywhere if you returned `NULL` here.
For example,
```c++
*value = malloc_and_copy(v.c_str(), v.size());
if (!*value) {
return false;
}
```
But I think it's better to fail fast for failed memory allocation. i.e.
```c++
if (result == NULL) {
abort(); // It will create a core dump
}
```
##########
include/pulsar/c/table_view.h:
##########
@@ -0,0 +1,139 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#pragma once
+
+#include <pulsar/defines.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <pulsar/c/message.h>
+#include <pulsar/c/messages.h>
+#include <pulsar/c/result.h>
+#include <stdint.h>
+
+typedef struct _pulsar_table_view pulsar_table_view_t;
+
+typedef void (*pulsar_table_view_action)(const char *key, const void *value,
const size_t value_size,
+ void *ctx);
+typedef void (*pulsar_result_callback)(pulsar_result, void *);
+
+/**
+ * Move the latest value associated with the key.
+ *
+ * NOTE:
+ * 1. Once the value has been retrieved successfully,
+ * the associated value will be removed from the table view until next time
the value is updated.
+ * 2. Once the value has been retrieved successfully, `*value` will point to
the memory that is allocated
+ * internally. You have to call `free(value)` to free it.
+ *
+ * Example:
+ *
+ * ```c
+ * pulsar_table_view_t *table_view;
+ * char *value;
+ * while (true) {
+ * if (pulsar_table_view_retrieve_value(table_view, "key", &value)) {
+ * printf("value is update to: %s", value);
+ * } else {
+ * // sleep for a while or print the message that value is not updated
+ * }
+ * }
+ * free(value);
+ * ```
+ *
+ * @param table_view
+ * @param key
+ * @param value the value associated with the key
+ * @return true if there is an associated value of the key, otherwise false
+ */
+PULSAR_PUBLIC bool pulsar_table_view_retrieve_value(pulsar_table_view_t
*table_view, const char *key,
+ void **value, size_t
*value_size);
+
+/**
+ * It's similar with `pulsar_table_view_retrieve_value` except the associated
value not will be removed from
+ * the table view.
+ *
+ * NOTE:
+ * Once the value has been get successfully, `*value` will point to the memory
that is allocated internally.
+ * You have to call `free(value)` to free it.
+ *
+ * @param table_view
+ * @param key
+ * @param value the value associated with the key
+ * @return true if there is an associated value of the key, otherwise false
+ */
+PULSAR_PUBLIC bool pulsar_table_view_get_value(pulsar_table_view_t
*table_view, const char *key, void **value,
+ size_t *value_size);
+
+/**
+ * Check if the key exists in the table view.
+ * @param table_view
+ * @param key
+ * @return true if the key exists in the table view
+ */
+PULSAR_PUBLIC bool pulsar_table_view_contain_key(pulsar_table_view_t
*table_view, const char *key);
+
+/**
+ * Get the size of the elements.
+ * @param table_view
+ * @return
+ */
+PULSAR_PUBLIC int pulsar_table_view_size(pulsar_table_view_t *table_view);
+
+/**
+ * Performs the given action for each entry in this map until all entries have
been processed or the
+ * action throws an exception.
+ */
+PULSAR_PUBLIC void pulsar_table_view_for_each(pulsar_table_view_t *table_view,
+ pulsar_table_view_action action,
void *ctx);
+
+/**
+ * Performs the given action for each entry in this map until all entries have
been processed and
+ * register the callback, which will be called each time a key-value pair is
updated.
+ */
+PULSAR_PUBLIC void pulsar_table_view_for_each_add_listen(pulsar_table_view_t
*table_view,
+
pulsar_table_view_action action, void *ctx);
+
+/**
+ * Free the table view.
+ * @param table_view
+ */
+PULSAR_PUBLIC void pulsar_table_view_free(pulsar_table_view_t *table_view);
+
+/**
+ * Close the table view and stop the broker to push more messages
+ * @param table_view
+ * @return
+ */
+PULSAR_PUBLIC pulsar_result pulsar_table_view_close(pulsar_table_view_t
*table_view);
+
+/**
+ * Async close the table view and stop the broker to push more messages
+ * @param table_view
+ * @param callback
+ * @param ctx
+ */
+PULSAR_PUBLIC void pulsar_table_view_close_async(pulsar_table_view_t
*table_view,
+ pulsar_result_callback
callback, void *ctx);
Review Comment:
Could you also add a test for this? If you added a new API, please cover it
in tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]