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