All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
aerospike_index.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2023 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 /**
20  * @defgroup index_operations Index Operations
21  * @ingroup client_operations
22  *
23  * The Index API provides the ability to create and remove secondary indexes.
24  *
25  * Aerospike currently supports indexing of strings and integers.
26  *
27  * ## String Indexes
28  *
29  * A string index allows for equality lookups. An equality lookup means that
30  * if you query for an indexed bin with value "abc", then only the records
31  * containing bins with "abc" will be returned.
32  *
33  * ## Integer Indexes
34  *
35  * An integer index allows for either equality or range lookups. An equality
36  * lookup means that if you query for an indexed bin with value 123, then only
37  * the records containing bins with the value 123 will be returned. A range
38  * lookup means that you can query bins within a range. So, if your range is
39  * (1...100), then all records containing the a value in that range will
40  * be returned.
41  */
42 
43 #include <aerospike/aerospike.h>
44 #include <aerospike/as_bin.h>
45 #include <aerospike/as_error.h>
46 #include <aerospike/as_key.h>
47 #include <aerospike/as_policy.h>
48 #include <aerospike/as_status.h>
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /******************************************************************************
55  * TYPES
56  *****************************************************************************/
57 
58 /**
59  * Index Type
60  *
61  * @ingroup index_operations
62  */
63 typedef enum as_index_type_s {
69 
70 /*
71  * Type of data which is going to indexed
72  */
73 typedef enum as_index_datatype_s {
77  AS_INDEX_BLOB // Requires server version 7.0+.
79 
80 /**
81  * Index Task
82  *
83  * Task used to poll for long running create index completion.
84  *
85  * @ingroup index_operations
86  */
87 typedef struct as_index_task_s {
88  /**
89  * The aerospike instance to use for this operation.
90  */
92 
93  /**
94  * The namespace to be indexed.
95  */
97 
98  /**
99  * The name of the index.
100  */
101  char name[64];
102 
103  /**
104  * Maximum time in milliseconds to wait for info command to return create index status.
105  * Defaults to "as_policy_info.timeout" that is passed to the original create index function.
106  */
107  uint32_t socket_timeout;
108 
109  /**
110  * Maximum time in milliseconds to wait for create index to complete.
111  * Default: 30000 ms (30 seconds)
112  */
113  uint32_t total_timeout;
114 
115  /**
116  * Has operation completed.
117  */
118  bool done;
119 } as_index_task;
120 
121 struct as_cdt_ctx;
122 
123 /******************************************************************************
124  * FUNCTIONS
125  *****************************************************************************/
126 
127 /**
128  * Create secondary index given collection type, data type and context.
129  *
130  * This asynchronous server call will return before the command is complete.
131  * The user can optionally wait for command completion by using a task instance.
132  *
133  * ~~~~~~~~~~{.c}
134  * as_cdt_ctx ctx;
135  * as_cdt_ctx_init(&ctx, 1);
136  * as_cdt_ctx_add_list_rank(&ctx, -1);
137  * as_index_task task;
138  * if (aerospike_index_create_ctx(&as, &err, &task, NULL, "test", "demo", "bin1",
139  * "idx_test_demo_bin1", AS_INDEX_TYPE_DEFAULT, AS_INDEX_NUMERIC, &ctx) == AEROSPIKE_OK) {
140  * aerospike_index_create_wait(&err, &task, 0);
141  * }
142  * ~~~~~~~~~~
143  *
144  * @param as The aerospike instance to use for this operation.
145  * @param err The as_error to be populated if an error occurs.
146  * @param task The optional task data used to poll for completion.
147  * @param policy The policy to use for this operation. If NULL, then the default policy will be used.
148  * @param ns The namespace to be indexed.
149  * @param set The set to be indexed.
150  * @param bin_name The bin name to be indexed.
151  * @param index_name The name of the index.
152  * @param itype The type of index, default or complex type.
153  * @param dtype The data type of index, string or integer.
154  * @param ctx Optional CDT context describing the path to locate the data to be indexed.
155  *
156  * @return AEROSPIKE_OK if successful. Return AEROSPIKE_ERR_INDEX_FOUND if index exists. Otherwise an error.
157  *
158  * @ingroup index_operations
159  */
162  aerospike* as, as_error* err, as_index_task* task, const as_policy_info* policy, const char* ns,
163  const char* set, const char* bin_name, const char* index_name, as_index_type itype,
164  as_index_datatype dtype, struct as_cdt_ctx* ctx
165  );
166 
167 /**
168  * Create secondary index given collection type and data type.
169  *
170  * This asynchronous server call will return before the command is complete.
171  * The user can optionally wait for command completion by using a task instance.
172  *
173  * ~~~~~~~~~~{.c}
174  * as_index_task task;
175  * if (aerospike_index_create_complex(&as, &err, &task, NULL, "test", "demo", "bin1",
176  * "idx_test_demo_bin1", AS_INDEX_TYPE_DEFAULT, AS_INDEX_NUMERIC) == AEROSPIKE_OK) {
177  * aerospike_index_create_wait(&err, &task, 0);
178  * }
179 * ~~~~~~~~~~
180  *
181  * @param as The aerospike instance to use for this operation.
182  * @param err The as_error to be populated if an error occurs.
183  * @param task The optional task data used to poll for completion.
184  * @param policy The policy to use for this operation. If NULL, then the default policy will be used.
185  * @param ns The namespace to be indexed.
186  * @param set The set to be indexed.
187  * @param bin_name The bin name to be indexed.
188  * @param index_name The name of the index.
189  * @param itype The type of index, default or complex type.
190  * @param dtype The data type of index, string or integer.
191  *
192  * @return AEROSPIKE_OK if successful. Return AEROSPIKE_ERR_INDEX_FOUND if index exists. Otherwise an error.
193  *
194  * @ingroup index_operations
195  */
196 static inline as_status
198  aerospike* as, as_error* err, as_index_task* task,
199  const as_policy_info* policy, const char* ns, const char* set,
200  const char* bin_name, const char* index_name, as_index_type itype,
201  as_index_datatype dtype
202  )
203 {
204  return aerospike_index_create_ctx(as, err, task, policy, ns, set, bin_name,
205  index_name, itype, dtype, NULL);
206 }
207 
208 /**
209  * Create secondary index given data type.
210  *
211  * This asynchronous server call will return before the command is complete.
212  * The user can optionally wait for command completion by using a task instance.
213  *
214  * ~~~~~~~~~~{.c}
215  * as_index_task task;
216  * if (aerospike_index_create(&as, &err, &task, NULL, "test", "demo", "bin1",
217  * "idx_test_demo_bin1", AS_INDEX_NUMERIC) == AEROSPIKE_OK) {
218  * aerospike_index_create_wait(&err, &task, 0);
219  * }
220  * ~~~~~~~~~~
221  *
222  * @param as The aerospike instance to use for this operation.
223  * @param err The as_error to be populated if an error occurs.
224  * @param task The optional task data used to poll for completion.
225  * @param policy The policy to use for this operation. If NULL, then the default policy will be used.
226  * @param ns The namespace to be indexed.
227  * @param set The set to be indexed.
228  * @param bin_name The bin name to be indexed.
229  * @param index_name The name of the index.
230  * @param dtype The data type of index, string or integer.
231  *
232  * @return AEROSPIKE_OK if successful. Return AEROSPIKE_ERR_INDEX_FOUND if index exists. Otherwise an error.
233  *
234  * @ingroup index_operations
235  */
236 static inline as_status
238  aerospike* as, as_error* err, as_index_task* task, const as_policy_info* policy,
239  const char* ns, const char* set, const char* bin_name, const char* index_name,
240  as_index_datatype dtype
241  )
242 {
243  return aerospike_index_create_ctx(as, err, task, policy, ns, set, bin_name, index_name,
244  AS_INDEX_TYPE_DEFAULT, dtype, NULL);
245 }
246 
247 /**
248  * Wait for asynchronous task to complete using given polling interval.
249  *
250  * @param err The as_error to be populated if an error occurs.
251  * @param task The task data used to poll for completion.
252  * @param interval_ms The polling interval in milliseconds. If zero, 1000 ms is used.
253  *
254  * @return AEROSPIKE_OK if successful. Otherwise an error.
255  *
256  * @ingroup index_operations
257  */
259 aerospike_index_create_wait(as_error* err, as_index_task* task, uint32_t interval_ms);
260 
261 /**
262  * Removes (drops) a secondary index.
263  *
264  * ~~~~~~~~~~{.c}
265  * if (aerospike_index_remove(&as, &err, NULL, "test", idx_test_demo_bin1") != AEROSPIKE_OK) {
266  * fprintf(stderr, "error(%d) %s at [%s:%d]", err.code, err.message, err.file, err.line);
267  * }
268  * ~~~~~~~~~~
269  *
270  * @param as The aerospike instance to use for this operation.
271  * @param err The as_error to be populated if an error occurs.
272  * @param policy The policy to use for this operation. If NULL, then the default policy will be used.
273  * @param ns The namespace containing the index to be removed.
274  * @param index_name The name of the index to be removed.
275  *
276  * @return AEROSPIKE_OK if successful or index does not exist. Otherwise an error.
277  *
278  * @ingroup index_operations
279  */
282  aerospike* as, as_error* err, const as_policy_info* policy,
283  const char* ns, const char* index_name
284  );
285 
286 /******************************************************************************
287  * DEPRECATED FUNCTIONS
288  *****************************************************************************/
289 
290 /**
291  * Create a new secondary index on an integer bin.
292  *
293  * @deprecated Use aerospike_index_create() instead.
294  *
295  * @ingroup index_operations
296  */
297 static inline as_status
299  aerospike* as, as_error* err, const as_policy_info* policy,
300  const char* ns, const char* set, const char* bin, const char* name
301  )
302 {
303  return aerospike_index_create_complex(as, err, 0, policy, ns, set, bin, name, AS_INDEX_TYPE_DEFAULT, AS_INDEX_NUMERIC);
304 }
305 
306 /**
307  * Create a new secondary index on a string bin.
308  *
309  * @deprecated Use aerospike_index_create() instead.
310  *
311  * @ingroup index_operations
312  */
313 static inline as_status
315  aerospike* as, as_error* err, const as_policy_info* policy,
316  const char* ns, const char* set, const char* bin, const char* name
317  )
318 {
319  return aerospike_index_create_complex(as, err, 0, policy, ns, set, bin, name, AS_INDEX_TYPE_DEFAULT, AS_INDEX_STRING);
320 }
321 
322 #ifdef __cplusplus
323 } // end extern "C"
324 #endif
uint32_t total_timeout
static as_status aerospike_index_create(aerospike *as, as_error *err, as_index_task *task, const as_policy_info *policy, const char *ns, const char *set, const char *bin_name, const char *index_name, as_index_datatype dtype)
as_namespace ns
Definition: as_scan.h:267
as_status
Definition: as_status.h:30
uint32_t socket_timeout
char as_namespace[AS_NAMESPACE_MAX_SIZE]
Definition: as_key.h:63
static as_status aerospike_index_integer_create(aerospike *as, as_error *err, const as_policy_info *policy, const char *ns, const char *set, const char *bin, const char *name)
#define AS_EXTERN
Definition: as_std.h:25
static as_status aerospike_index_string_create(aerospike *as, as_error *err, const as_policy_info *policy, const char *ns, const char *set, const char *bin, const char *name)
static as_status aerospike_index_create_complex(aerospike *as, as_error *err, as_index_task *task, const as_policy_info *policy, const char *ns, const char *set, const char *bin_name, const char *index_name, as_index_type itype, as_index_datatype dtype)
as_index_type
as_index_datatype
aerospike * as
as_namespace ns
AS_EXTERN as_status aerospike_index_remove(aerospike *as, as_error *err, const as_policy_info *policy, const char *ns, const char *index_name)
AS_EXTERN as_status aerospike_index_create_ctx(aerospike *as, as_error *err, as_index_task *task, const as_policy_info *policy, const char *ns, const char *set, const char *bin_name, const char *index_name, as_index_type itype, as_index_datatype dtype, struct as_cdt_ctx *ctx)
AS_EXTERN as_status aerospike_index_create_wait(as_error *err, as_index_task *task, uint32_t interval_ms)