All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
aerospike.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2020 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  * @mainpage Aerospike C Client
21  *
22  * @section intro_sec Introduction
23  *
24  * The Aerospike C client allows you to build C/C++ applications to store and retrieve data from the
25  * Aerospike cluster. The C client is a smart client that periodically pings nodes for cluster
26  * status and manages interactions with the cluster. The following functionality is supported.
27  *
28  * - Database commands
29  * - Key/Value
30  * - Map/List collections
31  * - Batch read
32  * - Scan
33  * - Secondary index query
34  * - User defined Lua functions
35  * - Expression filters
36  * - Both synchronous and asynchronous command models
37  * - Asynchronous model supports the following event frameworks.
38  * - libev
39  * - libevent
40  * - libuv
41  * - Thread safe API
42  * - Shared memory cluster tend state for multi-process applications
43  * - TLS secure sockets
44  *
45  * See <a href="modules.html">Modules</a> for an API overview.
46  *
47  * See <a href="http://www.aerospike.com/docs/client/c">Developer Guide</a> for installation
48  * instructions and example code.
49  */
50 
51 /**
52  * @defgroup client_objects Client Objects
53  */
54 
55 /**
56  * @defgroup client_operations Client Operations
57  *
58  * Client operations require an initialized aerospike client.
59  */
60 
61 /**
62  * @defgroup aerospike_t Client Types
63  */
64 
65 #include <aerospike/as_error.h>
66 #include <aerospike/as_config.h>
67 #include <aerospike/as_log.h>
68 #include <aerospike/as_status.h>
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 /******************************************************************************
75  * TYPES
76  *****************************************************************************/
77 
78 /**
79  * @private
80  * Forward declaration of a cluster object.
81  */
82 struct as_cluster_s;
83 
84 /**
85  * The aerospike struct is used to connect and execute operations against an
86  * Aerospike database cluster.
87  *
88  * ## Configuration
89  *
90  * A client configuration is required to initialize an aerospike client.
91  * See as_config for details on configuration options.
92  *
93  * At least one seed host must be defined.
94  *
95  * ~~~~~~~~~~{.c}
96  * as_config config;
97  * as_config_init(&config);
98  * as_config_add_host(&config, "127.0.0.1", 3000);
99  * ~~~~~~~~~~
100  *
101  * Once connected to a host in the cluster, then client will gather information
102  * about the cluster, including all other nodes in the cluster. So, all that
103  * is needed is a single valid host. Multiple hosts can still be provided in
104  * case the first host is not currently active.
105  *
106  * ## Initialization
107  *
108  * Initialization requires a configuration to bind to the client instance.
109  *
110  * The aerospike instance can be initialized via either:
111  *
112  * - aerospike_init() — Initialize a stack allocated aerospike instance.
113  * - aerospike_new() — Create and initialize a heap allocated aerospike instance.
114  *
115  * Once initialized, the ownership of the as_config instance fields are transferred
116  * to the aerospike instance. The user should never call as_config_destroy() directly.
117  *
118  * The following uses a stack allocated aerospike instance and initializes it
119  * with aerospike_init():
120  *
121  * ~~~~~~~~~~{.c}
122  * aerospike as;
123  * aerospike_init(&as, &config);
124  * ~~~~~~~~~~
125  *
126  * ## Connecting
127  *
128  * The client will be connected if `aerospike_connect()` completes successfully:
129  *
130  * ~~~~~~~~~~{.c}
131  * if (aerospike_connect(&as, &err) != AEROSPIKE_OK) {
132  * fprintf(stderr, "error(%d) %s at [%s:%d]", err.code, err.message, err.file, err.line);
133  * }
134  * ~~~~~~~~~~
135  *
136  * The `err` parameter will be populated if an error occurs. See as_error for more information
137  * on error handling.
138  *
139  * An aerospike object internally keeps cluster state and maintains connection pools to the cluster.
140  * The same aerospike object should be reused by the application for database operations
141  * to a given cluster.
142  *
143  * If the application requires connecting to multiple Aerospike clusters, the application must
144  * create multiple aerospike objects, each connecting to a different cluster.
145  *
146  * ## Disconnecting
147  *
148  * When the connection to the database is not longer required, then the
149  * connection to the cluster can be closed via `aerospike_close()`:
150  *
151  * ~~~~~~~~~~{.c}
152  * aerospike_close(&as, &err);
153  * ~~~~~~~~~~
154  *
155  * ## Destruction
156  *
157  * When the client is not longer required, the client and its resources should
158  * be releases via `aerospike_destroy()`:
159  *
160  * ~~~~~~~~~~{.c}
161  * aerospike_destroy(&as);
162  * ~~~~~~~~~~
163  *
164  * @ingroup client_objects
165  */
166 typedef struct aerospike_s {
167 
168  /**
169  * @private
170  * Cluster state.
171  */
172  struct as_cluster_s* cluster;
173 
174  /**
175  * Client configuration.
176  */
178 
179  /**
180  * @private
181  * If true, then aerospike_destroy() will free this instance.
182  */
183  bool _free;
184 
185 } aerospike;
186 
187 /******************************************************************************
188  * FUNCTIONS
189  *****************************************************************************/
190 
191 /**
192  * Initialize a stack allocated aerospike instance.
193  *
194  * The config parameter can be an instance of `as_config` or `NULL`. If `NULL`,
195  * then the default configuration will be used.
196  *
197  * Ownership of the as_config instance fields are transferred to the aerospike instance.
198  * The user should never call as_config_destroy() directly.
199  *
200  * ~~~~~~~~~~{.c}
201  * aerospike as;
202  * aerospike_init(&as, &config);
203  * ~~~~~~~~~~
204  *
205  * Once you are finished using the instance, then you should destroy it via the
206  * `aerospike_destroy()` function.
207  *
208  * @param as The aerospike instance to initialize.
209  * @param config The configuration to use for the instance.
210  *
211  * @returns the initialized aerospike instance
212  *
213  * @see config for information on configuring the client.
214  *
215  * @relates aerospike
216  */
218 aerospike_init(aerospike* as, as_config* config);
219 
220 /**
221  * Creates a new heap allocated aerospike instance.
222  *
223  * Ownership of the as_config instance fields are transferred to the aerospike instance.
224  * The user should never call as_config_destroy() directly.
225  *
226  * ~~~~~~~~~~{.c}
227  * aerospike* as = aerospike_new(&config);
228  * ~~~~~~~~~~
229  *
230  * Once you are finished using the instance, then you should destroy it via the
231  * `aerospike_destroy()` function.
232  *
233  * @param config The configuration to use for the instance.
234  *
235  * @returns a new aerospike instance
236  *
237  * @see config for information on configuring the client.
238  *
239  * @relates aerospike
240  */
242 aerospike_new(as_config* config);
243 
244 /**
245  * Initialize global lua configuration.
246  *
247  * @param config The lua configuration to use for all cluster instances.
248  */
249 AS_EXTERN void
251 
252 /**
253  * Destroy the aerospike instance and associated resources.
254  *
255  * ~~~~~~~~~~{.c}
256  * aerospike_destroy(&as);
257  * ~~~~~~~~~~
258  *
259  * @param as The aerospike instance to destroy
260  *
261  * @relates aerospike
262  */
263 AS_EXTERN void
265 
266 /**
267  * Connect an aerospike instance to the cluster.
268  *
269  * ~~~~~~~~~~{.c}
270  * aerospike_connect(&as, &err);
271  * ~~~~~~~~~~
272  *
273  * Once you are finished using the connection, then you must close it via
274  * the `aerospike_close()` function.
275  *
276  * If connect fails, then you do not need to call `aerospike_close()`.
277  *
278  * @param as The aerospike instance to connect to a cluster.
279  * @param err If an error occurs, the err will be populated.
280  *
281  * @returns AEROSPIKE_OK on success. Otherwise an error occurred.
282  *
283  * @relates aerospike
284  */
287 
288 /**
289  * Close connections to the cluster.
290  *
291  * ~~~~~~~~~~{.c}
292  * aerospike_close(&as, &err);
293  * ~~~~~~~~~~
294  *
295  * @param as The aerospike instance to disconnect from a cluster.
296  * @param err If an error occurs, the err will be populated.
297  *
298  * @returns AEROSPIKE_OK on success. Otherwise an error occurred.
299  *
300  * @relates aerospike
301  */
304 
305 /**
306  * Is cluster connected to any server nodes.
307  *
308  * ~~~~~~~~~~{.c}
309  * bool connected = aerospike_cluster_is_connected(&as);
310  * ~~~~~~~~~~
311  *
312  * @param as The aerospike instance to check.
313  *
314  * @returns true when cluster is connected.
315  *
316  * @relates aerospike
317  */
318 AS_EXTERN bool
320 
321 /**
322  * Should stop socket operation if interrupted by a signal. Default is false which means
323  * the socket operation will be retried until timeout.
324  *
325  * @relates aerospike
326  */
327 AS_EXTERN void
328 aerospike_stop_on_interrupt(bool stop);
329 
330 /**
331  * Remove records in specified namespace/set efficiently. This method is many orders of magnitude
332  * faster than deleting records one at a time.
333  *
334  * See <a href="https://www.aerospike.com/docs/reference/info#truncate">https://www.aerospike.com/docs/reference/info#truncate</a>
335  *
336  * This asynchronous server call may return before the truncation is complete. The user can still
337  * write new records after the server returns because new records will have last update times
338  * greater than the truncate cutoff (set at the time of truncate call).
339  *
340  * @param as Aerospike instance.
341  * @param err If an error occurs, the err will be populated.
342  * @param policy Info policy. If NULL, then the default policy will be used.
343  * @param ns Required namespace.
344  * @param set Optional set name. Pass in NULL to delete all sets in namespace.
345  * @param before_nanos Optionally delete records before record last update time.
346  * Units are in nanoseconds since unix epoch (1970-01-01).
347  * If specified, value must be before the current time.
348  * Pass in 0 to delete all records in namespace/set regardless of last update time.
349  * @returns AEROSPIKE_OK on success. Otherwise an error occurred.
350  *
351  * @relates aerospike
352  */
355  aerospike* as, as_error* err, as_policy_info* policy, const char* ns, const char* set,
356  uint64_t before_nanos
357  );
358 
359 /**
360  * Refresh the current TLS configuration by reloading its certificate, key, and blacklist files.
361  *
362  * @param as Aerospike instance whose TLS configuration to refresh.
363  * @param err If an error occurs, this will be populated.
364  *
365  * @returns AEROSPIKE_OK on success. Otherwise an error occurred.
366  *
367  * @relates aerospike
368  */
371 
372 /**
373  * Set XDR filter for given datacenter name and namespace. The expression filter indicates
374  * which records XDR should ship to the datacenter.
375  *
376  * @param as Aerospike instance.
377  * @param err If an error occurs, this will be populated.
378  * @param policy Info policy. If NULL, then the default policy will be used.
379  * @param dc Datacenter name.
380  * @param ns Namespace.
381  * @param filter_b64 expression filter in base64 encoding. Use as_exp_build_b64() to create.
382  *
383  * @returns AEROSPIKE_OK on success. Otherwise an error occurred.
384  *
385  * @relates aerospike
386  */
389  aerospike* as, as_error* err, as_policy_info* policy, const char* dc, const char* ns,
390  const char* filter_b64
391  );
392 
393 #ifdef __cplusplus
394 } // end extern "C"
395 #endif
as_namespace ns
Definition: as_scan.h:267
AS_EXTERN void aerospike_stop_on_interrupt(bool stop)
AS_EXTERN bool aerospike_cluster_is_connected(aerospike *as)
as_status
Definition: as_status.h:30
AS_EXTERN void aerospike_init_lua(as_config_lua *config)
struct as_cluster_s * cluster
Definition: aerospike.h:172
AS_EXTERN aerospike * aerospike_init(aerospike *as, as_config *config)
#define AS_EXTERN
Definition: as_std.h:25
as_config config
Definition: aerospike.h:177
AS_EXTERN as_status aerospike_reload_tls_config(aerospike *as, as_error *err)
AS_EXTERN as_status aerospike_truncate(aerospike *as, as_error *err, as_policy_info *policy, const char *ns, const char *set, uint64_t before_nanos)
AS_EXTERN as_status aerospike_close(aerospike *as, as_error *err)
AS_EXTERN void aerospike_destroy(aerospike *as)
AS_EXTERN aerospike * aerospike_new(as_config *config)
AS_EXTERN as_status aerospike_set_xdr_filter(aerospike *as, as_error *err, as_policy_info *policy, const char *dc, const char *ns, const char *filter_b64)
AS_EXTERN as_status aerospike_connect(aerospike *as, as_error *err)