All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_batch.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_bin.h>
20 #include <aerospike/as_key.h>
21 #include <aerospike/as_record.h>
22 #include <aerospike/as_status.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /*****************************************************************************
29  * STRUCTURES
30  *****************************************************************************/
31 
32 /**
33  * A collection of keys to be batch processed.
34  */
35 typedef struct as_batch_s {
36 
37  /**
38  * Sequence of keys in the batch.
39  */
40  struct {
41 
42  /**
43  * The keys contained by this batch.
44  */
46 
47  /**
48  * The number of keys this structure contains.
49  */
50  uint32_t size;
51 
52  /**
53  * If true, then this structure will be freed when as_batch_destroy()
54  * is called.
55  */
56  bool _free;
57 
58  } keys;
59 
60  /**
61  * If true, then this structure will be freed when as_batch_destroy()
62  * is called.
63  */
64  bool _free;
65 
66 } as_batch;
67 
68 /**
69  * Batch key and record result.
70  */
71 typedef struct as_batch_result_s {
72  /**
73  * The requested key.
74  */
75  const as_key* key;
76 
77  /**
78  * Record result after batch command has completed. Will be null if record was not found
79  * or an error occurred.
80  */
82 
83  /**
84  * Result code for this returned record. If not AEROSPIKE_OK, the record will be null.
85  */
87 
88  /**
89  * Is it possible that the write transaction may have completed even though an error
90  * occurred for this record. This may be the case when a client error occurs (like timeout)
91  * after the command was sent to the server.
92  */
93  bool in_doubt;
95 
96 /**
97  * The (key, result, record) for an entry in a batch read.
98  * @deprecated Use as_batch_result instead.
99  */
101 
102 /*********************************************************************************
103  * INSTANCE MACROS
104  *********************************************************************************/
105 
106 
107 /**
108  * Initializes `as_batch` with specified capacity using alloca().
109  *
110  * For heap allocation, use `as_batch_new()`.
111  *
112  * ~~~~~~~~~~{.c}
113  * as_batch batch;
114  * as_batch_inita(&batch, 2);
115  * as_key_init(as_batch_get(&batch, 0), "ns", "set", "key1");
116  * as_key_init(as_batch_get(&batch, 1), "ns", "set", "key2");
117  * ~~~~~~~~~~
118  *
119  * When the batch is no longer needed, then use as_batch_destroy() to
120  * release the batch and associated resources.
121  *
122  * @param __batch The query to initialize.
123  * @param __size The number of keys to allocate.
124  *
125  * @relates as_batch
126  * @ingroup batch_object
127  */
128 #define as_batch_inita(__batch, __size) \
129  do {\
130  (__batch)->keys.entries = (as_key*) alloca(sizeof(as_key) * (__size));\
131  if ((__batch)->keys.entries) { \
132  (__batch)->keys.size = (__size);\
133  (__batch)->keys._free = false;\
134  }\
135  (__batch)->_free = false;\
136  } while(0)
137 
138 /*********************************************************************************
139  * INSTANCE FUNCTIONS
140  *********************************************************************************/
141 
142 /**
143  * Create and initialize a heap allocated as_batch capable of storing
144  * `capacity` keys.
145  *
146  * ~~~~~~~~~~{.c}
147  * as_batch* batch = as_batch_new(2);
148  * as_key_init(as_batch_get(batch, 0), "ns", "set", "key1");
149  * as_key_init(as_batch_get(batch, 1), "ns", "set", "key2");
150  * ~~~~~~~~~~
151  *
152  * When the batch is no longer needed, then use as_batch_destroy() to
153  * release the batch and associated resources.
154  *
155  * @param size The number of keys to allocate.
156  *
157  * @relates as_batch
158  * @ingroup batch_object
159  */
161 as_batch_new(uint32_t size);
162 
163 /**
164  * Initialize a stack allocated as_batch capable of storing `capacity` keys.
165  *
166  * ~~~~~~~~~~{.c}
167  * as_batch batch;
168  * as_batch_init(&batch, 2);
169  * as_key_init(as_batch_get(&batch, 0), "ns", "set", "key1");
170  * as_key_init(as_batch_get(&batch, 1), "ns", "set", "key2");
171  * ~~~~~~~~~~
172  *
173  * When the batch is no longer needed, then use as_batch_destroy() to
174  * release the batch and associated resources.
175  *
176  * @param batch The batch to initialize.
177  * @param size The number of keys to allocate.
178  *
179  * @relates as_batch
180  * @ingroup batch_object
181  */
183 as_batch_init(as_batch* batch, uint32_t size);
184 
185 /**
186  * Destroy the batch of keys.
187  *
188  * ~~~~~~~~~~{.c}
189  * as_batch_destroy(batch);
190  * ~~~~~~~~~~
191  *
192  * @param batch The batch to release.
193  *
194  * @relates as_batch
195  * @ingroup batch_object
196  */
197 AS_EXTERN void
198 as_batch_destroy(as_batch* batch);
199 
200 /**
201  * Get the key at given position of the batch. If the position is not
202  * within the allocated capacity for the batchm then NULL is returned.
203  *
204  * @param batch The batch to get the key from.
205  * @param i The position of the key.
206  *
207  * @return On success, the key at specified position. If position is invalid, then NULL.
208  *
209  * @relates as_batch
210  * @ingroup batch_object
211  */
212 static inline as_key*
213 as_batch_keyat(const as_batch* batch, uint32_t i)
214 {
215  return (batch != NULL && batch->keys.entries != NULL && batch->keys.size > i) ? &batch->keys.entries[i] : NULL;
216 }
217 
218 #ifdef __cplusplus
219 } // end extern "C"
220 #endif
AS_EXTERN as_batch * as_batch_init(as_batch *batch, uint32_t size)
as_status
Definition: as_status.h:30
as_status result
Definition: as_batch.h:86
uint32_t size
Definition: as_batch.h:50
as_key * entries
Definition: as_batch.h:45
#define AS_EXTERN
Definition: as_std.h:25
AS_EXTERN void as_batch_destroy(as_batch *batch)
as_record record
Definition: as_batch.h:81
const as_key * key
Definition: as_batch.h:75
AS_EXTERN as_batch * as_batch_new(uint32_t size)
as_batch_result as_batch_read
Definition: as_batch.h:100
Definition: as_key.h:196
static as_key * as_batch_keyat(const as_batch *batch, uint32_t i)
Definition: as_batch.h:213
struct as_batch::@0 keys