BewareMyPower commented on code in PR #294:
URL: https://github.com/apache/pulsar-client-cpp/pull/294#discussion_r1251654802


##########
lib/c/c_TableView.cc:
##########
@@ -0,0 +1,95 @@
+/**
+ * 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 "c_structs.h"
+#include "cstring"
+
+char *str_malloc_and_copy(const char *s) {

Review Comment:
   ```suggestion
   static char *str_malloc_and_copy(const char *s) {
   ```
   
   Use a static function if it's only used in the current file.



##########
include/pulsar/c/table_view.h:
##########
@@ -0,0 +1,147 @@
+/**
+ * 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 char *value, 
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 `delete 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
+ *     }
+ * }
+ * delete value;

Review Comment:
   It's a C header, `delete` is a C++ keyword. You should not use `delete` 
here. I see the memory that `value` points to is allocated by `malloc` in 
`str_malloc_and_copy`. You should use `free` instead of `delete`.



##########
lib/c/c_TableView.cc:
##########
@@ -0,0 +1,95 @@
+/**
+ * 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 "c_structs.h"
+#include "cstring"

Review Comment:
   ```suggestion
   #include <string.h>
   ```
   
   Use `<>` instead of `""` for system headers. The C++ header `<cstring>` is 
equivalent with the C header `<string.h>` except wrapping all C functions into 
the `std` namespace



##########
tests/c/c_TableViewTest.cc:
##########
@@ -0,0 +1,92 @@
+/**
+ * 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 <gtest/gtest.h>
+#include <pulsar/c/client.h>
+#include <string.h>
+
+#include "pulsar/c/table_view.h"
+
+static const char *lookup_url = "pulsar://localhost:6650";
+
+TEST(c_TableViewTest, testSimpleTableView) {
+    const char *topic_name = "persistent://public/default/test-table_view20";
+    const char *sub_name = "my-sub-name";
+
+    pulsar_client_configuration_t *conf = pulsar_client_configuration_create();
+    pulsar_client_t *client = pulsar_client_create(lookup_url, conf);
+
+    pulsar_producer_configuration_t *producer_conf = 
pulsar_producer_configuration_create();
+    pulsar_producer_t *producer;
+    pulsar_result result = pulsar_client_create_producer(client, topic_name, 
producer_conf, &producer);
+    ASSERT_EQ(pulsar_result_Ok, result);
+
+    // Send messages
+    const int num = 10;
+    const char *key1 = "key1";
+    const char *key2 = "key2";
+    const char *value = "content";
+    for (int i = 0; i < num; i++) {
+        pulsar_message_t *message = pulsar_message_create();
+        if (i % 2 == 0) {
+            pulsar_message_set_partition_key(message, key1);
+        } else {
+            pulsar_message_set_partition_key(message, key2);
+        }
+        pulsar_message_set_content(message, value, strlen(value));
+        pulsar_result res = pulsar_producer_send(producer, message);
+        ASSERT_EQ(pulsar_result_Ok, res);
+        pulsar_message_free(message);
+    }
+
+    // Create table view.
+    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;
+    result = pulsar_client_create_table_view(client, topic_name, 
table_view_conf, &table_view);
+    ASSERT_EQ(pulsar_result_Ok, result);
+
+    char *v1;
+    ASSERT_EQ(pulsar_table_view_size(table_view), 2);
+    ASSERT_TRUE(pulsar_table_view_get_value(table_view, "key1", &v1));
+    ASSERT_STREQ(v1, "content");
+    delete v1;

Review Comment:
   `delete` should not appear in the pure C code.



-- 
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]

Reply via email to