Loading...
Searching...
No Matches
as_key.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 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_bytes.h>
21#include <aerospike/as_error.h>
22#include <aerospike/as_string.h>
23#include <aerospike/as_status.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29//---------------------------------
30// Macros
31//---------------------------------
32
33/**
34 * The size of as_digest.value
35 */
36#define AS_DIGEST_VALUE_SIZE 20
37
38/**
39 * The maxium size of as_namespace.
40 */
41#define AS_NAMESPACE_MAX_SIZE 32
42
43/**
44 * The maxium size of as_set.
45 */
46#define AS_SET_MAX_SIZE 64
47
48//---------------------------------
49// Types
50//---------------------------------
51
52/**
53 * Namespace Name
54 */
56
57/**
58 * Set Name
59 */
60typedef char as_set[AS_SET_MAX_SIZE];
61
62/**
63 * Digest value
64 */
66
67/**
68 * The digest is the value used to locate a record based on the
69 * set and digest of the record. The digest is calculated using RIPEMD-160.
70 * Keys for digests can be either a string or integer.
71 */
72typedef struct as_digest_s {
73
74 /**
75 * Indicates whether the digest was calculated.
76 */
77 bool init;
78
79 /**
80 * The digest value.
81 */
83
84} as_digest;
85
86/**
87 * Key value
88 */
89typedef union as_key_value_u {
90
91 /**
92 * Integer value.
93 */
95
96 /**
97 * String value.
98 */
100
101 /**
102 * Raw value.
103 */
105
107
108
109/**
110 * A key is used for locating records in the database.
111 *
112 * ## Initialization
113 *
114 * A key can either be stack or heap allocated. Use one of the following
115 * functions to properly initialize an as_key.
116 *
117 * Each function requires a namespace, set and key value. The set can be
118 * and empty string.
119 *
120 * For stack allocated as_key, you should you the following functions to
121 * initialize the value:
122 *
123 * - as_key_init() - Initialize the key with a string value.
124 * - as_key_init_int64() - Initialize the key with an int64_t value.
125 * - as_key_init_str() - Same as as_key_init().
126 * - as_key_init_raw() - Initialize the key with byte array.
127 * - as_key_init_value() - Initialize the key with an as_key_value.
128 *
129 * ~~~~~~~~~~{.c}
130 * as_key key;
131 * as_key_init(&key, "ns", "set", "key");
132 * ~~~~~~~~~~
133 *
134 * For heap allocated as_key, you should use the following functions
135 * to allocate and initialize the value on the heap.
136 *
137 * - as_key_new() - Initialize the key with a string value.
138 * - as_key_new_int64() - Initialize the key with an int64_t value.
139 * - as_key_new_str() - Same as as_key_new().
140 * - as_key_new_raw() - Initialize the key with byte array.
141 * - as_key_new_value() - Initialize the key with an as_key_value.
142 *
143 * ~~~~~~~~~~{.c}
144 * as_key* key = as_key_new("ns", "set", "key");
145 * ~~~~~~~~~~
146 *
147 * ## Destruction
148 *
149 * When you no longer require an instance of as_key, you should release the
150 * key and associated resources via as_key_destroy().
151 *
152 * ~~~~~~~~~~{.c}
153 * as_key_destroy(key);
154 * ~~~~~~~~~~
155 *
156 * This function should be used on both stack and heap allocated keys.
157 *
158 * ## Operations
159 *
160 * The following are operations which require a key.
161 *
162 * - aerospike_key_get()
163 * - aerospike_key_select()
164 * - aerospike_key_exists()
165 * - aerospike_key_put()
166 * - aerospike_key_operate()
167 * - aerospike_key_remove()
168 * - aerospike_key_apply()
169 *
170 * ## Digest
171 *
172 * Each operation that requires a key, internally generates a digest for the
173 * key. The digest is a hash value used to locate a record in the cluster. Once
174 * calculated, the digest will be reused.
175 *
176 * To get the digest value of a key, use as_key_digest().
177 *
178 * @ingroup client_objects
179 */
180typedef struct as_key_s {
181
182 /**
183 * @private
184 * If true, then as_key_destroy() will free this instance.
185 */
186 bool _free;
187
188 /**
189 * The namespace the key belongs to.
190 */
192
193 /**
194 * The set the key belongs to.
195 */
197
198 /**
199 * The key value.
200 */
202
203 /**
204 * The key value pointer.
205 * If NULL, then there is no value.
206 * It can point to as_key.value or a different value.
207 */
209
210 /**
211 * Digest for the key.
212 */
214
215} as_key;
216
217//---------------------------------
218// Functions
219//---------------------------------
220
221/**
222 * Initialize a stack allocated as_key to a NULL-terminated string value.
223 *
224 * ~~~~~~~~~~{.c}
225 * as_key key;
226 * as_key_init(&key, "ns", "set", "key");
227 * ~~~~~~~~~~
228 *
229 * Use as_key_destroy() to release resources allocated to as_key via
230 * this function.
231 *
232 * @param key The key to initialize.
233 * @param ns The namespace for the key.
234 * @param set The set for the key.
235 * @param value The key's value.
236 *
237 * @return The initialized as_key on success. Otherwise NULL.
238 *
239 * @relates as_key
240 */
242as_key_init(as_key* key, const char* ns, const char* set, const char* value);
243
244/**
245 * Initialize a stack allocated as_key to a int64_t value.
246 *
247 * ~~~~~~~~~~{.c}
248 * as_key key;
249 * as_key_init_int64(&key, "ns", "set", 123);
250 * ~~~~~~~~~~
251 *
252 * Use as_key_destroy() to release resources allocated to as_key.
253 *
254 * @param key The key to initialize.
255 * @param ns The namespace for the key.
256 * @param set The set for the key.
257 * @param value The key's value.
258 *
259 * @return The initialized as_key on success. Otherwise NULL.
260 *
261 * @relates as_key
262 */
264as_key_init_int64(as_key* key, const char* ns, const char* set, int64_t value);
265
266/**
267 * Initialize a stack allocated as_key to a NULL-terminated string value.
268 *
269 * ~~~~~~~~~~{.c}
270 * as_key key;
271 * as_key_init_strp(&key, "ns", "set", stdup("key"), true);
272 * ~~~~~~~~~~
273 *
274 * Use as_key_destroy() to release resources allocated to as_key.
275 *
276 * @param key The key to initialize.
277 * @param ns The namespace for the key.
278 * @param set The set for the key.
279 * @param value The key's value.
280 * @param free If true, then the key's value can be freed when the key is destroyed.
281 *
282 * @return The initialized as_key on success. Otherwise NULL.
283 *
284 * @relates as_key
285 */
287as_key_init_strp(as_key* key, const char* ns, const char* set, const char* value, bool free);
288
289/**
290 * Initialize a stack allocated as_key to a NULL-terminated string value.
291 *
292 * ~~~~~~~~~~{.c}
293 * as_key key;
294 * as_key_init_str(&key, "ns", "set", "key");
295 * ~~~~~~~~~~
296 *
297 * Use as_key_destroy() to release resources allocated to as_key.
298 *
299 * @param key The key to initialize.
300 * @param ns The namespace for the key.
301 * @param set The set for the key.
302 * @param value The key's value. Must last for the lifetime of the key.
303 *
304 * @return The initialized as_key on success. Otherwise NULL.
305 *
306 * @relates as_key
307 */
308static inline as_key*
309as_key_init_str(as_key* key, const char* ns, const char* set, const char* value)
310{
311 return as_key_init_strp(key, ns, set, value, false);
312}
313
314/**
315 * Initialize a stack allocated as_key to bytes array.
316 *
317 * ~~~~~~~~~~{.c}
318 * uint8_t * rgb = (uint8_t *) malloc(3);
319 * rgb[0] = 255;
320 * rgb[1] = 255;
321 * rgb[3] = 255;
322 *
323 * as_key key;
324 * as_key_init_rawp(&key, "ns", "set", rgb, 3, true);
325 * ~~~~~~~~~~
326 *
327 * Use as_key_destroy() to release resources allocated to as_key.
328 *
329 * @param key The key to initialize.
330 * @param ns The namespace for the key.
331 * @param set The set for the key.
332 * @param value The key's value.
333 * @param size The number of bytes in value.
334 * @param free If true, then the key's value can be freed when the key is destroyed.
335 *
336 * @return The initialized as_key on success. Otherwise NULL.
337 *
338 * @relates as_key
339 */
341as_key_init_rawp(as_key* key, const char* ns, const char* set, const uint8_t* value, uint32_t size, bool free);
342
343/**
344 * Initialize a stack allocated as_key to bytes array.
345 *
346 * ~~~~~~~~~~{.c}
347 * uint8_t rgb[3] = {254,254,120};
348 *
349 * as_key key;
350 * as_key_init_raw(&key, "ns", "set", rgb, 3);
351 * ~~~~~~~~~~
352 *
353 * Use as_key_destroy() to release resources allocated to as_key.
354 *
355 * @param key The key to initialize.
356 * @param ns The namespace for the key.
357 * @param set The set for the key.
358 * @param value The key's value.
359 * @param size The number of bytes in value. Must last for the lifetime of the key.
360 *
361 * @return The initialized as_key on success. Otherwise NULL.
362 *
363 * @relates as_key
364 */
365static inline as_key*
366as_key_init_raw(as_key* key, const char* ns, const char* set, const uint8_t* value, uint32_t size)
367{
368 return as_key_init_rawp(key, ns, set, value, size, false);
369}
370
371/**
372 * Initialize a stack allocated as_key with a digest.
373 *
374 * ~~~~~~~~~~{.c}
375 * as_digest_value digest = {0};
376 *
377 * as_key key;
378 * as_key_init_digest(&key, "ns", "set", digest);
379 * ~~~~~~~~~~
380 *
381 * Use as_key_destroy() to release resources allocated to as_key.
382 *
383 * @param key The key to initialize.
384 * @param ns The namespace for the key.
385 * @param set The set for the key.
386 * @param digest The digest for the key.
387 *
388 * @return The initialized as_key on success. Otherwise NULL.
389 *
390 * @relates as_key
391 */
393as_key_init_digest(as_key* key, const char* ns, const char* set, const as_digest_value digest);
394
395/**
396 * Initialize a stack allocated as_key to an as_key_value.
397 *
398 * ~~~~~~~~~~{.c}
399 * as_string str;
400 * as_string_init(&str, "abc", false);
401 *
402 * as_key key;
403 * as_key_init_value(&key, "ns", "set", (as_key_value *) str);
404 * ~~~~~~~~~~
405 *
406 * Use as_key_destroy() to release resources allocated to as_key.
407 *
408 * @param key The key to initialize.
409 * @param ns The namespace for the key.
410 * @param set The set for the key.
411 * @param value The key's value.
412 *
413 * @return The initialized as_key on success. Otherwise NULL.
414 *
415 * @relates as_key
416 */
418as_key_init_value(as_key* key, const char* ns, const char* set, const as_key_value* value);
419
420/**
421 * Creates and initializes a heap allocated as_key to a NULL-terminated string value.
422 *
423 * ~~~~~~~~~~{.c}
424 * as_key* key = as_key_new("ns", "set", "key");
425 * ~~~~~~~~~~
426 *
427 * Use as_key_destroy() to release resources allocated to as_key via
428 * this function.
429 *
430 * @param ns The namespace for the key.
431 * @param set The set for the key.
432 * @param value The key's value.
433 *
434 * @return A new as_key on success. Otherwise NULL.
435 *
436 * @relates as_key
437 */
439as_key_new(const char* ns, const char* set, const char* value);
440
441/**
442 * Creates and initializes a heap allocated as_key to a int64_t value.
443 *
444 * ~~~~~~~~~~{.c}
445 * as_key* key = as_key_new_int64("ns", "set", 123);
446 * ~~~~~~~~~~
447 *
448 * Use as_key_destroy() to release resources allocated to as_key via
449 * this function.
450 *
451 * @param ns The namespace for the key.
452 * @param set The set for the key.
453 * @param value The key's value.
454 *
455 * @return A new as_key on success. Otherwise NULL.
456 *
457 * @relates as_key
458 */
460as_key_new_int64(const char* ns, const char* set, int64_t value);
461
462/**
463 * Creates and initializes a heap allocated as_key to a NULL-terminated string value.
464 *
465 * ~~~~~~~~~~{.c}
466 * as_key* key = as_key_new_strp("ns", "set", strdup("key"), true);
467 * ~~~~~~~~~~
468 *
469 * Use as_key_destroy() to release resources allocated to as_key via
470 * this function.
471 *
472 * @param ns The namespace for the key.
473 * @param set The set for the key.
474 * @param value The key's value.
475 * @param free If true, then the key's value can be freed when the key is destroyed.
476 *
477 * @return A new as_key on success. Otherwise NULL.
478 *
479 * @relates as_key
480 */
482as_key_new_strp(const char* ns, const char* set, const char* value, bool free);
483
484/**
485 * Creates and initializes a heap allocated as_key to a NULL-terminated string value.
486 *
487 * ~~~~~~~~~~{.c}
488 * as_key* key = as_key_new_str("ns", "set", "key");
489 * ~~~~~~~~~~
490 *
491 * Use as_key_destroy() to release resources allocated to as_key via
492 * this function.
493 *
494 * @param ns The namespace for the key.
495 * @param set The set for the key.
496 * @param value The key's value. Must last for the lifetime of the key.
497 *
498 * @return A new as_key on success. Otherwise NULL.
499 *
500 * @relates as_key
501 */
502static inline as_key*
503as_key_new_str(const char* ns, const char* set, const char* value)
504{
505 return as_key_new_strp(ns, set, value, false);
506}
507
508/**
509 * Creates and initializes a heap allocated as_key to a byte array.
510 *
511 * ~~~~~~~~~~{.c}
512 * uint8_t * rgb = (uint8_t *) malloc(3);
513 * rgb[0] = 255;
514 * rgb[1] = 255;
515 * rgb[3] = 255;
516 *
517 * as_key* key = as_key_new_rawp("ns", "set", rgb, 3, true);
518 * ~~~~~~~~~~
519 *
520 * Use as_key_destroy() to release resources allocated to as_key via
521 * this function.
522 *
523 * @param ns The namespace for the key.
524 * @param set The set for the key.
525 * @param value The key's value.
526 * @param size The number of bytes in the value.
527 * @param free If true, then the key's value can be freed when the key is destroyed.
528 *
529 * @return A new as_key on success. Otherwise NULL.
530 *
531 * @relates as_key
532 */
534as_key_new_rawp(const char* ns, const char* set, const uint8_t* value, uint32_t size, bool free);
535
536/**
537 * Creates and initializes a heap allocated as_key to a byte array.
538 *
539 * ~~~~~~~~~~{.c}
540 * uint8_t rgb[3] = {254,254,120};
541 *
542 * as_key* key = as_key_new_raw("ns", "set", rgb, 3);
543 * ~~~~~~~~~~
544 *
545 * Use as_key_destroy() to release resources allocated to as_key via
546 * this function.
547 *
548 * @param ns The namespace for the key.
549 * @param set The set for the key.
550 * @param value The key's value. Must last for the lifetime of the key.
551 * @param size The number of bytes in the value.
552 *
553 * @return A new as_key on success. Otherwise NULL.
554 *
555 * @relates as_key
556 */
557static inline as_key*
558as_key_new_raw(const char* ns, const char* set, const uint8_t* value, uint32_t size)
559{
560 return as_key_new_rawp(ns, set, value, size, false);
561}
562
563/**
564 * Creates and initializes a heap allocated as_key with a digest.
565 *
566 * ~~~~~~~~~~{.c}
567 * as_digest_value digest = {0};
568 *
569 * as_key* key = as_key_new_digest("ns", "set", digest);
570 * ~~~~~~~~~~
571 *
572 * Use as_key_destroy() to release resources allocated to as_key via
573 * this function.
574 *
575 * @param ns The namespace for the key.
576 * @param set The set for the key.
577 * @param digest The key's digest.
578 *
579 * @return A new as_key on success. Otherwise NULL.
580 *
581 * @relates as_key
582 */
584as_key_new_digest(const char* ns, const char* set, const as_digest_value digest);
585
586/**
587 * Creates and initializes a heap allocated as_key to a an as_key_value.
588 *
589 * ~~~~~~~~~~{.c}
590 * as_string str;
591 * as_string_init(&str, "abc", false);
592 *
593 * as_key* key = as_key_new_value("ns", "set", (as_key_value *) str);
594 * ~~~~~~~~~~
595 *
596 * Use as_key_destroy() to release resources allocated to as_key via
597 * this function.
598 *
599 * @param ns The namespace for the key.
600 * @param set The set for the key.
601 * @param value The key's value.
602 *
603 * @return A new as_key on success. Otherwise NULL.
604 *
605 * @relates as_key
606 */
608as_key_new_value(const char* ns, const char* set, const as_key_value* value);
609
610/**
611 * Destory the as_key, releasing resources.
612 *
613 * ~~~~~~~~~~{.c}
614 * as_key_destroy(key);
615 * ~~~~~~~~~~
616 *
617 * @param key The as_key to destroy.
618 *
619 * @relates as_key
620 */
621AS_EXTERN void
623
624/**
625 * Get the digest for the given key.
626 *
627 * The digest is computed the first time function is called. Subsequent calls
628 * will return the previously calculated value.
629 *
630 * ~~~~~~~~~~{.c}
631 * as_digest * digest = as_key_digest(key);
632 * ~~~~~~~~~~
633 *
634 * @param key The key to get the digest for.
635 *
636 * @return The digest for the key.
637 *
638 * @relates as_key
639 */
642
643/**
644 * Set the digest value in the key structure. Keys must be integer, string or blob.
645 * Otherwise, an error is returned.
646 *
647 * @param err Error message that is populated on error.
648 * @param key The key to get the digest for.
649 *
650 * @return Status code.
651 *
652 * @relates as_key
653 */
656
657#ifdef __cplusplus
658} // end extern "C"
659#endif
#define AS_DIGEST_VALUE_SIZE
Definition as_key.h:36
char as_namespace[AS_NAMESPACE_MAX_SIZE]
Definition as_key.h:55
char as_set[AS_SET_MAX_SIZE]
Definition as_key.h:60
#define AS_SET_MAX_SIZE
Definition as_key.h:46
uint8_t as_digest_value[AS_DIGEST_VALUE_SIZE]
Definition as_key.h:65
#define AS_NAMESPACE_MAX_SIZE
Definition as_key.h:41
as_status
Definition as_status.h:30
#define AS_EXTERN
Definition as_std.h:25
as_digest_value value
Definition as_key.h:82
bool init
Definition as_key.h:77
AS_EXTERN as_digest * as_key_digest(as_key *key)
static as_key * as_key_init_raw(as_key *key, const char *ns, const char *set, const uint8_t *value, uint32_t size)
Definition as_key.h:366
AS_EXTERN as_key * as_key_init_digest(as_key *key, const char *ns, const char *set, const as_digest_value digest)
AS_EXTERN as_key * as_key_new_value(const char *ns, const char *set, const as_key_value *value)
as_digest digest
Definition as_key.h:213
as_set set
Definition as_key.h:196
AS_EXTERN as_key * as_key_new(const char *ns, const char *set, const char *value)
static as_key * as_key_new_str(const char *ns, const char *set, const char *value)
Definition as_key.h:503
AS_EXTERN as_status as_key_set_digest(as_error *err, as_key *key)
AS_EXTERN as_key * as_key_new_rawp(const char *ns, const char *set, const uint8_t *value, uint32_t size, bool free)
AS_EXTERN as_key * as_key_init_strp(as_key *key, const char *ns, const char *set, const char *value, bool free)
AS_EXTERN as_key * as_key_init(as_key *key, const char *ns, const char *set, const char *value)
AS_EXTERN as_key * as_key_init_int64(as_key *key, const char *ns, const char *set, int64_t value)
AS_EXTERN as_key * as_key_new_int64(const char *ns, const char *set, int64_t value)
as_key_value value
Definition as_key.h:201
static as_key * as_key_init_str(as_key *key, const char *ns, const char *set, const char *value)
Definition as_key.h:309
AS_EXTERN as_key * as_key_new_strp(const char *ns, const char *set, const char *value, bool free)
static as_key * as_key_new_raw(const char *ns, const char *set, const uint8_t *value, uint32_t size)
Definition as_key.h:558
as_key_value * valuep
Definition as_key.h:208
AS_EXTERN as_key * as_key_init_value(as_key *key, const char *ns, const char *set, const as_key_value *value)
AS_EXTERN void as_key_destroy(as_key *key)
AS_EXTERN as_key * as_key_init_rawp(as_key *key, const char *ns, const char *set, const uint8_t *value, uint32_t size, bool free)
AS_EXTERN as_key * as_key_new_digest(const char *ns, const char *set, const as_digest_value digest)
as_namespace ns
Definition as_key.h:191
as_string string
Definition as_key.h:99
as_integer integer
Definition as_key.h:94
as_bytes bytes
Definition as_key.h:104