All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_cdt_ctx.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2022 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <aerospike/as_cdt_order.h>
20 #include <aerospike/as_vector.h>
21 #include <aerospike/as_val.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /******************************************************************************
28  * TYPES
29  *****************************************************************************/
30 
31 /**
32  * Nested CDT context type.
33  *
34  * @relates as_operations
35  * @ingroup as_operations_object
36  */
37 typedef enum {
46 
47 #define AS_CDT_CTX_VALUE 0x2
48 
49 /**
50  * Nested CDT context level.
51  *
52  * @relates as_operations
53  * @ingroup as_operations_object
54  */
55 typedef struct as_cdt_ctx_item {
56  uint32_t type;
57  union
58  {
59  int64_t ival;
61  } val;
63 
64 /**
65  * List of CDT context level(s).
66  *
67  * @relates as_operations
68  * @ingroup as_operations_object
69  */
70 typedef struct as_cdt_ctx {
72 } as_cdt_ctx;
73 
74 /******************************************************************************
75  * MACROS
76  *****************************************************************************/
77 
78 /**
79  * Initialize a stack allocated nested CDT context list.
80  *
81  * ~~~~~~~~~~{.c}
82  * Lookup last list in list of lists.
83  * as_cdt_ctx ctx;
84  * as_cdt_ctx_inita(&ctx, 1);
85  * as_cdt_ctx_add_list_index(&ctx, -1);
86  * ~~~~~~~~~~
87  *
88  * Call as_cdt_ctx_destroy() when done with the context list if any context levels contain
89  * a heap allocated as_val instance. If in doubt, call as_cdt_ctx_destroy().
90  *
91  * @param __ctx The nested context to initialize.
92  * @param __cap The max number of context levels allowed.
93  *
94  * @relates as_operations
95  * @ingroup as_operations_object
96  */
97 #define as_cdt_ctx_inita(__ctx, __cap) as_vector_inita(&(__ctx)->list, sizeof(as_cdt_ctx_item), __cap)
98 
99 /******************************************************************************
100  * FUNCTIONS
101  *****************************************************************************/
102 
103 /**
104  * Initialize a stack allocated nested CDT context list, with item storage on the heap.
105  * Call as_cdt_ctx_destroy() when done with the context list.
106  *
107  * @relates as_operations
108  * @ingroup as_operations_object
109  */
110 static inline void
111 as_cdt_ctx_init(as_cdt_ctx* ctx, uint32_t capacity)
112 {
113  as_vector_init(&ctx->list, sizeof(as_cdt_ctx_item), capacity);
114 }
115 
116 /**
117  * Initialize a heap allocated nested CDT context list, with item storage on the heap.
118  * Call as_cdt_ctx_destroy() when done with the context list.
119  *
120  * @relates as_operations
121  * @ingroup as_operations_object
122  */
123 static inline as_cdt_ctx*
124 as_cdt_ctx_create(uint32_t capacity)
125 {
126  return (as_cdt_ctx*)as_vector_create(sizeof(as_cdt_ctx_item), capacity);
127 }
128 
129 /**
130  * Destroy nested CDT context list and as_val based context items that were allocated on the heap.
131  *
132  * @relates as_operations
133  * @ingroup as_operations_object
134  */
135 AS_EXTERN void
137 
138 /**
139  * Lookup list by index offset.
140  *
141  * If the index is negative, the resolved index starts backwards from end of list.
142  * If an index is out of bounds, a parameter error will be returned. Examples:
143  * <ul>
144  * <li>0: First item.</li>
145  * <li>4: Fifth item.</li>
146  * <li>-1: Last item.</li>
147  * <li>-3: Third to last item.</li>
148  * </ul>
149  *
150  * @relates as_operations
151  * @ingroup as_operations_object
152  */
153 static inline void
155 {
156  as_cdt_ctx_item item;
158  item.val.ival = index;
159  as_vector_append(&ctx->list, &item);
160 }
161 
162 /**
163  * Create list with given type at index offset.
164  *
165  * @relates as_operations
166  * @ingroup as_operations_object
167  */
168 static inline void
170 {
171  as_cdt_ctx_item item;
172  item.type = AS_CDT_CTX_LIST_INDEX | as_list_order_to_flag(order, pad);
173  item.val.ival = index;
174  as_vector_append(&ctx->list, &item);
175 }
176 
177 /**
178  * Lookup list by rank.
179  * <ul>
180  * <li>0 = smallest value</li>
181  * <li>N = Nth smallest value</li>
182  * <li>-1 = largest value</li>
183  * </ul>
184  *
185  * @relates as_operations
186  * @ingroup as_operations_object
187  */
188 static inline void
190 {
191  as_cdt_ctx_item item;
192  item.type = AS_CDT_CTX_LIST_RANK;
193  item.val.ival = rank;
194  as_vector_append(&ctx->list, &item);
195 }
196 
197 /**
198  * Lookup list by value. The ctx list takes ownership of val.
199  *
200  * @relates as_operations
201  * @ingroup as_operations_object
202  */
203 static inline void
205 {
206  as_cdt_ctx_item item;
208  item.val.pval = val;
209  as_vector_append(&ctx->list, &item);
210 }
211 
212 /**
213  * Lookup map by index offset.
214  * <p>
215  * If the index is negative, the resolved index starts backwards from end of list.
216  * If an index is out of bounds, a parameter error will be returned. Examples:
217  * <ul>
218  * <li>0: First item.</li>
219  * <li>4: Fifth item.</li>
220  * <li>-1: Last item.</li>
221  * <li>-3: Third to last item.</li>
222  * </ul>
223  *
224  * @relates as_operations
225  * @ingroup as_operations_object
226  */
227 static inline void
229 {
230  as_cdt_ctx_item item;
231  item.type = AS_CDT_CTX_MAP_INDEX;
232  item.val.ival = index;
233  as_vector_append(&ctx->list, &item);
234 }
235 
236 /**
237  * Lookup map by rank.
238  * <ul>
239  * <li>0 = smallest value</li>
240  * <li>N = Nth smallest value</li>
241  * <li>-1 = largest value</li>
242  * </ul>
243  *
244  * @relates as_operations
245  * @ingroup as_operations_object
246  */
247 static inline void
249 {
250  as_cdt_ctx_item item;
251  item.type = AS_CDT_CTX_MAP_RANK;
252  item.val.ival = rank;
253  as_vector_append(&ctx->list, &item);
254 }
255 
256 /**
257  * Lookup map by key. The ctx list takes ownership of key.
258  *
259  * @relates as_operations
260  * @ingroup as_operations_object
261  */
262 static inline void
264 {
265  as_cdt_ctx_item item;
266  item.type = AS_CDT_CTX_MAP_KEY;
267  item.val.pval = key;
268  as_vector_append(&ctx->list, &item);
269 }
270 
271 /**
272  * Create map with given type at map key.
273  * The ctx list takes ownership of key.
274  *
275  * @relates as_operations
276  * @ingroup as_operations_object
277  */
278 static inline void
280 {
281  as_cdt_ctx_item item;
283  item.val.pval = key;
284  as_vector_append(&ctx->list, &item);
285 }
286 
287 /**
288  * Lookup map by value. The ctx list takes ownership of val.
289  *
290  * @relates as_operations
291  * @ingroup as_operations_object
292  */
293 static inline void
295 {
296  as_cdt_ctx_item item;
297  item.type = AS_CDT_CTX_MAP_VALUE;
298  item.val.pval = val;
299  as_vector_append(&ctx->list, &item);
300 }
301 
302 /**
303  * Return exact serialized size of ctx. Return zero on error.
304  */
305 AS_EXTERN uint32_t
307 
308 /**
309  * Serialize ctx to bytes. Use as_cdt_ctx_byte_capacity() to determine required bytes capacity.
310  *
311  * @param ctx Source CDT context.
312  * @param bytes Target bytes array which must be allocated before calling this function.
313  * @param capacity Max size of bytes array.
314  * @return Length of serialized bytes on success or zero on error.
315  */
316 AS_EXTERN uint32_t
317 as_cdt_ctx_to_bytes(const as_cdt_ctx* ctx, uint8_t* bytes, uint32_t capacity);
318 
319 /**
320  * Deserialize bytes to ctx.
321  *
322  * @param ctx Target CDT context.
323  * @param bytes Source byte array.
324  * @param size Length of source byte array.
325  * @return true on success, false on error.
326  */
327 AS_EXTERN bool
328 as_cdt_ctx_from_bytes(as_cdt_ctx* ctx, const uint8_t* bytes, uint32_t size);
329 
330 /**
331  * Return estimated base64 encoded size of ctx. Return zero on error.
332  */
333 AS_EXTERN uint32_t
335 
336 /**
337  * Serialize ctx to base64 encoded string. Use as_cdt_ctx_base64_capacity() to determine
338  * required string capacity.
339  *
340  * @param ctx Source CDT context.
341  * @param base64 Target base64 encoded null terminated string which must be allocated before
342  * calling this function.
343  * @param capacity Max size of base64 encoded string.
344  * @return true on success, false on error.
345  */
346 AS_EXTERN bool
347 as_cdt_ctx_to_base64(const as_cdt_ctx* ctx, char* base64, uint32_t capacity);
348 
349 /**
350  * Deserialize base64 encoded string to ctx.
351  *
352  * @param ctx Target CDT context.
353  * @param base64 Source base64 encoded string.
354  * @return true on success, false on error.
355  */
356 AS_EXTERN bool
357 as_cdt_ctx_from_base64(as_cdt_ctx* ctx, const char* base64);
358 
359 #ifdef __cplusplus
360 } // end extern "C"
361 #endif
int64_t ival
Definition: as_cdt_ctx.h:59
static void as_cdt_ctx_add_map_key(as_cdt_ctx *ctx, as_val *key)
Definition: as_cdt_ctx.h:263
static void as_cdt_ctx_add_list_rank(as_cdt_ctx *ctx, int rank)
Definition: as_cdt_ctx.h:189
as_list_order
Definition: as_cdt_order.h:34
static void as_cdt_ctx_add_map_value(as_cdt_ctx *ctx, as_val *val)
Definition: as_cdt_ctx.h:294
static void as_cdt_ctx_add_map_key_create(as_cdt_ctx *ctx, as_val *key, as_map_order order)
Definition: as_cdt_ctx.h:279
as_vector list
Definition: as_cdt_ctx.h:71
AS_EXTERN as_vector * as_vector_create(uint32_t item_size, uint32_t capacity)
AS_EXTERN void as_vector_init(as_vector *vector, uint32_t item_size, uint32_t capacity)
Definition: as_val.h:61
static void as_vector_append(as_vector *vector, void *value)
Definition: as_vector.h:145
static uint32_t as_list_order_to_flag(as_list_order order, bool pad)
Definition: as_cdt_order.h:73
static void as_cdt_ctx_add_list_index_create(as_cdt_ctx *ctx, int index, as_list_order order, bool pad)
Definition: as_cdt_ctx.h:169
as_map_order
Definition: as_cdt_order.h:51
AS_EXTERN bool as_cdt_ctx_from_bytes(as_cdt_ctx *ctx, const uint8_t *bytes, uint32_t size)
#define AS_EXTERN
Definition: as_std.h:25
AS_EXTERN bool as_cdt_ctx_from_base64(as_cdt_ctx *ctx, const char *base64)
static void as_cdt_ctx_init(as_cdt_ctx *ctx, uint32_t capacity)
Definition: as_cdt_ctx.h:111
static void as_cdt_ctx_add_map_rank(as_cdt_ctx *ctx, int rank)
Definition: as_cdt_ctx.h:248
AS_EXTERN uint32_t as_cdt_ctx_byte_capacity(const as_cdt_ctx *ctx)
AS_EXTERN void as_cdt_ctx_destroy(as_cdt_ctx *ctx)
static void as_cdt_ctx_add_list_value(as_cdt_ctx *ctx, as_val *val)
Definition: as_cdt_ctx.h:204
as_val * pval
Definition: as_cdt_ctx.h:60
union as_cdt_ctx_item::@1 val
static as_cdt_ctx * as_cdt_ctx_create(uint32_t capacity)
Definition: as_cdt_ctx.h:124
AS_EXTERN uint32_t as_cdt_ctx_base64_capacity(const as_cdt_ctx *ctx)
uint32_t type
Definition: as_cdt_ctx.h:56
AS_EXTERN uint32_t as_cdt_ctx_to_bytes(const as_cdt_ctx *ctx, uint8_t *bytes, uint32_t capacity)
AS_EXTERN bool as_cdt_ctx_to_base64(const as_cdt_ctx *ctx, char *base64, uint32_t capacity)
static uint32_t as_map_order_to_flag(as_map_order order)
Definition: as_cdt_order.h:79
static void as_cdt_ctx_add_list_index(as_cdt_ctx *ctx, int index)
Definition: as_cdt_ctx.h:154
static void as_cdt_ctx_add_map_index(as_cdt_ctx *ctx, int index)
Definition: as_cdt_ctx.h:228