Loading...
Searching...
No Matches
as_batch.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_bin.h>
20#include <aerospike/as_key.h>
21#include <aerospike/as_record.h>
22#include <aerospike/as_status.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28//---------------------------------
29// Types
30//---------------------------------
31
32/**
33 * A collection of keys to be batch processed.
34 */
35typedef 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 */
71typedef 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 */
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// Macros
104//---------------------------------
105
106/**
107 * Initializes `as_batch` with specified capacity using alloca().
108 *
109 * For heap allocation, use `as_batch_new()`.
110 *
111 * ~~~~~~~~~~{.c}
112 * as_batch batch;
113 * as_batch_inita(&batch, 2);
114 * as_key_init(as_batch_get(&batch, 0), "ns", "set", "key1");
115 * as_key_init(as_batch_get(&batch, 1), "ns", "set", "key2");
116 * ~~~~~~~~~~
117 *
118 * When the batch is no longer needed, then use as_batch_destroy() to
119 * release the batch and associated resources.
120 *
121 * @param __batch The query to initialize.
122 * @param __size The number of keys to allocate.
123 *
124 * @relates as_batch
125 * @ingroup batch_operations
126 */
127#define as_batch_inita(__batch, __size) \
128 do {\
129 (__batch)->keys.entries = (as_key*) alloca(sizeof(as_key) * (__size));\
130 if ((__batch)->keys.entries) { \
131 (__batch)->keys.size = (__size);\
132 (__batch)->keys._free = false;\
133 }\
134 (__batch)->_free = false;\
135 } while(0)
136
137//---------------------------------
138// Functions
139//---------------------------------
140
141/**
142 * Create and initialize a heap allocated as_batch capable of storing
143 * `capacity` keys.
144 *
145 * ~~~~~~~~~~{.c}
146 * as_batch* batch = as_batch_new(2);
147 * as_key_init(as_batch_get(batch, 0), "ns", "set", "key1");
148 * as_key_init(as_batch_get(batch, 1), "ns", "set", "key2");
149 * ~~~~~~~~~~
150 *
151 * When the batch is no longer needed, then use as_batch_destroy() to
152 * release the batch and associated resources.
153 *
154 * @param size The number of keys to allocate.
155 *
156 * @relates as_batch
157 * @ingroup batch_operations
158 */
160as_batch_new(uint32_t size);
161
162/**
163 * Initialize a stack allocated as_batch capable of storing `capacity` keys.
164 *
165 * ~~~~~~~~~~{.c}
166 * as_batch batch;
167 * as_batch_init(&batch, 2);
168 * as_key_init(as_batch_get(&batch, 0), "ns", "set", "key1");
169 * as_key_init(as_batch_get(&batch, 1), "ns", "set", "key2");
170 * ~~~~~~~~~~
171 *
172 * When the batch is no longer needed, then use as_batch_destroy() to
173 * release the batch and associated resources.
174 *
175 * @param batch The batch to initialize.
176 * @param size The number of keys to allocate.
177 *
178 * @relates as_batch
179 * @ingroup batch_operations
180 */
182as_batch_init(as_batch* batch, uint32_t size);
183
184/**
185 * Destroy the batch of keys.
186 *
187 * ~~~~~~~~~~{.c}
188 * as_batch_destroy(batch);
189 * ~~~~~~~~~~
190 *
191 * @param batch The batch to release.
192 *
193 * @relates as_batch
194 * @ingroup batch_operations
195 */
196AS_EXTERN void
198
199/**
200 * Get the key at given position of the batch. If the position is not
201 * within the allocated capacity for the batchm then NULL is returned.
202 *
203 * @param batch The batch to get the key from.
204 * @param i The position of the key.
205 *
206 * @return On success, the key at specified position. If position is invalid, then NULL.
207 *
208 * @relates as_batch
209 * @ingroup batch_operations
210 */
211static inline as_key*
212as_batch_keyat(const as_batch* batch, uint32_t i)
213{
214 return (batch != NULL && batch->keys.entries != NULL && batch->keys.size > i) ? &batch->keys.entries[i] : NULL;
215}
216
217#ifdef __cplusplus
218} // end extern "C"
219#endif
as_batch_result as_batch_read
Definition as_batch.h:100
as_status
Definition as_status.h:30
#define AS_EXTERN
Definition as_std.h:25
static as_key * as_batch_keyat(const as_batch *batch, uint32_t i)
Definition as_batch.h:212
AS_EXTERN void as_batch_destroy(as_batch *batch)
AS_EXTERN as_batch * as_batch_new(uint32_t size)
AS_EXTERN as_batch * as_batch_init(as_batch *batch, uint32_t size)
as_status result
Definition as_batch.h:86
as_record record
Definition as_batch.h:81
const as_key * key
Definition as_batch.h:75
uint32_t size
Definition as_batch.h:50
as_key * entries
Definition as_batch.h:45
struct as_batch::@0 keys