|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include <pulsar/defines.h> |
| 22 | + |
| 23 | +#ifdef __cplusplus |
| 24 | +extern "C" { |
| 25 | +#endif |
| 26 | + |
| 27 | +#include <pulsar/c/message.h> |
| 28 | +#include <pulsar/c/messages.h> |
| 29 | +#include <pulsar/c/result.h> |
| 30 | +#include <stdint.h> |
| 31 | + |
| 32 | +typedef struct _pulsar_table_view pulsar_table_view_t; |
| 33 | + |
| 34 | +typedef void (*pulsar_table_view_action)(const char *key, const void *value, size_t value_size, void *ctx); |
| 35 | +typedef void (*pulsar_result_callback)(pulsar_result, void *); |
| 36 | + |
| 37 | +/** |
| 38 | + * Move the latest value associated with the key. |
| 39 | + * |
| 40 | + * NOTE: |
| 41 | + * 1. Once the value has been retrieved successfully, |
| 42 | + * the associated value will be removed from the table view until next time the value is updated. |
| 43 | + * 2. Once the value has been retrieved successfully, `*value` will point to the memory that is allocated |
| 44 | + * internally. You have to call `free(value)` to free it. |
| 45 | + * |
| 46 | + * Example: |
| 47 | + * |
| 48 | + * ```c |
| 49 | + * pulsar_table_view_t *table_view; |
| 50 | + * void* value; |
| 51 | + * size_t value_size; |
| 52 | + * while (true) { |
| 53 | + * if (pulsar_table_view_retrieve_value(table_view, "key", &value, &value_size)) { |
| 54 | + * for (size_t i = 0; i < value_size; i++) { |
| 55 | + * printf("0x%02x%c", ((char*) value)[i], (i + 1 == value_size) ? '\n': ' '); |
| 56 | + * } |
| 57 | + * } else { |
| 58 | + * // sleep for a while or print the message that value is not updated |
| 59 | + * } |
| 60 | + * } |
| 61 | + * free(value); |
| 62 | + * ``` |
| 63 | + * |
| 64 | + * @param table_view |
| 65 | + * @param key |
| 66 | + * @param value the value associated with the key |
| 67 | + * @return true if there is an associated value of the key, otherwise false |
| 68 | + */ |
| 69 | +PULSAR_PUBLIC bool pulsar_table_view_retrieve_value(pulsar_table_view_t *table_view, const char *key, |
| 70 | + void **value, size_t *value_size); |
| 71 | + |
| 72 | +/** |
| 73 | + * It's similar with `pulsar_table_view_retrieve_value` except the associated value not will be removed from |
| 74 | + * the table view. |
| 75 | + * |
| 76 | + * NOTE: |
| 77 | + * Once the value has been get successfully, `*value` will point to the memory that is allocated internally. |
| 78 | + * You have to call `free(value)` to free it. |
| 79 | + * |
| 80 | + * @param table_view |
| 81 | + * @param key |
| 82 | + * @param value the value associated with the key |
| 83 | + * @return true if there is an associated value of the key, otherwise false |
| 84 | + */ |
| 85 | +PULSAR_PUBLIC bool pulsar_table_view_get_value(pulsar_table_view_t *table_view, const char *key, void **value, |
| 86 | + size_t *value_size); |
| 87 | + |
| 88 | +/** |
| 89 | + * Check if the key exists in the table view. |
| 90 | + * @param table_view |
| 91 | + * @param key |
| 92 | + * @return true if the key exists in the table view |
| 93 | + */ |
| 94 | +PULSAR_PUBLIC bool pulsar_table_view_contain_key(pulsar_table_view_t *table_view, const char *key); |
| 95 | + |
| 96 | +/** |
| 97 | + * Get the size of the elements. |
| 98 | + * @param table_view |
| 99 | + * @return |
| 100 | + */ |
| 101 | +PULSAR_PUBLIC int pulsar_table_view_size(pulsar_table_view_t *table_view); |
| 102 | + |
| 103 | +/** |
| 104 | + * Performs the given action for each entry in this map until all entries have been processed or the |
| 105 | + * action throws an exception. |
| 106 | + */ |
| 107 | +PULSAR_PUBLIC void pulsar_table_view_for_each(pulsar_table_view_t *table_view, |
| 108 | + pulsar_table_view_action action, void *ctx); |
| 109 | + |
| 110 | +/** |
| 111 | + * Performs the given action for each entry in this map until all entries have been processed and |
| 112 | + * register the callback, which will be called each time a key-value pair is updated. |
| 113 | + */ |
| 114 | +PULSAR_PUBLIC void pulsar_table_view_for_each_add_listen(pulsar_table_view_t *table_view, |
| 115 | + pulsar_table_view_action action, void *ctx); |
| 116 | + |
| 117 | +/** |
| 118 | + * Free the table view. |
| 119 | + * @param table_view |
| 120 | + */ |
| 121 | +PULSAR_PUBLIC void pulsar_table_view_free(pulsar_table_view_t *table_view); |
| 122 | + |
| 123 | +/** |
| 124 | + * Close the table view and stop the broker to push more messages |
| 125 | + * @param table_view |
| 126 | + * @return |
| 127 | + */ |
| 128 | +PULSAR_PUBLIC pulsar_result pulsar_table_view_close(pulsar_table_view_t *table_view); |
| 129 | + |
| 130 | +/** |
| 131 | + * Async close the table view and stop the broker to push more messages |
| 132 | + * @param table_view |
| 133 | + * @param callback |
| 134 | + * @param ctx |
| 135 | + */ |
| 136 | +PULSAR_PUBLIC void pulsar_table_view_close_async(pulsar_table_view_t *table_view, |
| 137 | + pulsar_result_callback callback, void *ctx); |
| 138 | + |
| 139 | +#ifdef __cplusplus |
| 140 | +} |
| 141 | +#endif |
0 commit comments