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


##########
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,

Review Comment:
   ```suggestion
   typedef void (*pulsar_table_view_action)(const char *key, const void *value, 
size_t value_size,
   ```
   
   Don't use `const` for value parameters.



##########
tests/c/c_TableViewTest.cc:
##########
@@ -0,0 +1,97 @@
+/**
+ * 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";
+    size_t data_size = 4;
+    int *data = (int *)malloc(data_size);
+    data[0] = 0x01;
+    data[1] = 0x00;
+    data[2] = 0x02;
+    data[3] = 0x00;

Review Comment:
   The pointer you used is wrong. You allocated 4 (`data_size`) bytes. However, 
you casted the `void*` to `int*`, so the access to `data[1]` to `data[3]` is 
invalid.
   
   Assuming `p` is the address returned by `malloc` and the machine is 
little-endian, the value stored are actually :
   
   | p | p + 1 | p + 2 | p + 3 |
   | :- | :- | :- | :- |
   | 0x01 | 0x00 | 0x00 | 0x00 |
   
   `data[1]` is `p+4`.
   
   Take the following example:
   
   ```c
   #include <stdio.h>
   #include <stdlib.h>
   
   int main(int argc, char *argv[]) {
     int *data = (int *)malloc(4);
     data[0] = 0x01;
     data[1] = 0x02;
     printf("%p\n%p\n", &data[0], &data[1]);
     char *p = (char *)data;
     for (int i = 0; i < 4; i++) {
       printf("0x%02x ", p[i]);
     }
     printf("\n");
     free(data);
     return 0;
   }
   ```
   
   ```bash
   $ gcc 1.c
   $ ./a.out
   0x600001838030
   0x600001838034
   0x01 0x00 0x00 0x00 
   ```



##########
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);

Review Comment:
   This code example is wrong.



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