-
Notifications
You must be signed in to change notification settings - Fork 77
feat: Support table view for C client. #294
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /** | ||
| * 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, 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; | ||
| * void* value; | ||
| * size_t value_size; | ||
| * while (true) { | ||
| * if (pulsar_table_view_retrieve_value(table_view, "key", &value, &value_size)) { | ||
| * for (size_t i = 0; i < value_size; i++) { | ||
| * printf("0x%02x%c", ((char*) value)[i], (i + 1 == value_size) ? '\n': ' '); | ||
| * } | ||
| * } 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); | ||
shibd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /** | ||
| * 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> | ||
|
|
||
| #include "producer_configuration.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| typedef struct _pulsar_table_view_configuration pulsar_table_view_configuration_t; | ||
|
|
||
| PULSAR_PUBLIC pulsar_table_view_configuration_t *pulsar_table_view_configuration_create(); | ||
|
|
||
| PULSAR_PUBLIC void pulsar_table_view_configuration_free(pulsar_table_view_configuration_t *conf); | ||
|
|
||
| PULSAR_PUBLIC void pulsar_table_view_configuration_set_schema_info( | ||
| pulsar_table_view_configuration_t *table_view_configuration_t, pulsar_schema_type schemaType, | ||
| const char *name, const char *schema, pulsar_string_map_t *properties); | ||
|
|
||
| PULSAR_PUBLIC void pulsar_table_view_configuration_set_subscription_name( | ||
| pulsar_table_view_configuration_t *table_view_configuration_t, const char *subscription_name); | ||
|
|
||
| PULSAR_PUBLIC const char *pulsar_table_view_configuration_get_subscription_name( | ||
| pulsar_table_view_configuration_t *table_view_configuration_t); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
| abort(); | ||
| } | ||
| memcpy(result, s, slen); | ||
| return result; | ||
| } | ||
|
|
||
| bool pulsar_table_view_retrieve_value(pulsar_table_view_t *table_view, const char *key, void **value, | ||
| size_t *value_size) { | ||
| std::string v; | ||
| bool result = table_view->tableView.retrieveValue(key, v); | ||
| if (result) { | ||
| *value = malloc_and_copy(v.c_str(), v.size()); | ||
| *value_size = v.size(); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| bool pulsar_table_view_get_value(pulsar_table_view_t *table_view, const char *key, void **value, | ||
| size_t *value_size) { | ||
| std::string v; | ||
| bool result = table_view->tableView.getValue(key, v); | ||
| if (result) { | ||
| *value = malloc_and_copy(v.c_str(), v.size()); | ||
| *value_size = v.size(); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| bool pulsar_table_view_contain_key(pulsar_table_view_t *table_view, const char *key) { | ||
| return table_view->tableView.containsKey(key); | ||
| } | ||
|
|
||
| int pulsar_table_view_size(pulsar_table_view_t *table_view) { return table_view->tableView.size(); } | ||
|
|
||
| void pulsar_table_view_for_each(pulsar_table_view_t *table_view, pulsar_table_view_action action, void *ctx) { | ||
| table_view->tableView.forEach([action, ctx](const std::string &key, const std::string &value) { | ||
| if (action) { | ||
| action(key.c_str(), value.c_str(), value.size(), ctx); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void pulsar_table_view_for_each_add_listen(pulsar_table_view_t *table_view, pulsar_table_view_action action, | ||
| void *ctx) { | ||
| table_view->tableView.forEachAndListen([action, ctx](const std::string &key, const std::string &value) { | ||
| if (action) { | ||
| action(key.c_str(), value.c_str(), value.size(), ctx); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void pulsar_table_view_free(pulsar_table_view_t *table_view) { delete table_view; } | ||
|
|
||
| pulsar_result pulsar_table_view_close(pulsar_table_view_t *table_view) { | ||
| return (pulsar_result)table_view->tableView.close(); | ||
| } | ||
|
|
||
| void pulsar_table_view_close_async(pulsar_table_view_t *table_view, pulsar_result_callback callback, | ||
| void *ctx) { | ||
| table_view->tableView.closeAsync( | ||
| [callback, ctx](pulsar::Result result) { return handle_result_callback(result, callback, ctx); }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.