All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
aerospike_query.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2008-2013 by Aerospike.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  *****************************************************************************/
22 
23 /**
24  * @defgroup query_operations Query Operations (3.0 only)
25  * @ingroup client_operations
26  *
27  * The Aerospike Query Operations provide the ability to query data in the
28  * Aerospike database. The queries can only be performed on secondary indexes,
29  * which have been created in the database. To scan all the records in the
30  * database, then you must use the @ref scan_operations.
31  *
32  * ## Usage
33  *
34  * Before you can execute a query, you first need to build a query using
35  * as_query. See as_query for details on building queries.
36  *
37  * Once you have a query defined, then you can execute the query :
38  *
39  * - aerospike_query_foreach() - Executes a query and invokes a callback
40  * function for each result returned.
41  *
42  * When aerospike_query_foreach() is executed, it will process the results
43  * and create records on the stack. Because the records are on the stack,
44  * they will only be available within the context of the callback function.
45  *
46  *
47  * ## Walk-through
48  *
49  * First, we define a query using as_query. The query will be for the "test"
50  * namespace and "demo" set. We will add a where predicate on "bin2", on which
51  * we have already created a secondary index. Also, we will limit
52  * the results to 100 records.
53  *
54  * ~~~~~~~~~~{.c}
55  * as_query query;
56  * as_query_init(&query, "test", "demo");
57  * as_query_limit(&query, 100);
58  *
59  * as_query_where_init(&query, 1);
60  * as_query_where(&query, "bin2", integer_equals(100));
61  * ~~~~~~~~~~
62  *
63  * Now that we have a query defined, we want to execute it using
64  * aerospike_query_foreach().
65  *
66  * ~~~~~~~~~~{.c}
67  * if ( aerospike_query_foreach(&as, &err, NULL, &query, callback, NULL) != AEROSPIKE_OK ) {
68  * fprintf(stderr, "error(%d) %s at [%s:%d]", err.code, err.message, err.file, err.line);
69  * }
70  * ~~~~~~~~~~
71  *
72  * The callback provided to the function above is implemented as:
73  *
74  * ~~~~~~~~~~{.c}
75  * bool callback(const as_val * val, void * udata) {
76  * as_record * rec = as_record_fromval(val);
77  * if ( !rec ) return false;
78  * fprintf("record contains %d bins", as_record_numbins(rec));
79  * return true;
80  * }
81  * ~~~~~~~~~~
82  *
83  * An as_query is simply a query definition, so it does not contain any state,
84  * allowing it to be reused for multiple query operations.
85  *
86  * When you are finished with the query, you should destroy the resources
87  * allocated to it:
88  *
89  * ~~~~~~~~~~{.c}
90  * as_query_destroy(&query);
91  * ~~~~~~~~~~
92  *
93  */
94 
95 #pragma once
96 
97 #include <aerospike/aerospike.h>
98 #include <aerospike/as_error.h>
99 #include <aerospike/as_policy.h>
100 #include <aerospike/as_query.h>
101 #include <aerospike/as_status.h>
102 #include <aerospike/as_stream.h>
103 
104 /******************************************************************************
105  * TYPES
106  *****************************************************************************/
107 
108 /**
109  * This callback will be called for each value or record returned from
110  * a query.
111  *
112  * The aerospike_query_foreach() function accepts this callback.
113  *
114  * ~~~~~~~~~~{.c}
115  * bool my_callback(as_val * val, void * udata) {
116  * return true;
117  * }
118  * ~~~~~~~~~~
119  *
120  * @param val The value received from the query.
121  * @param udata User-data provided to the calling function.
122  *
123  * @return `true` to continue to the next value. Otherwise, iteration will end.
124  *
125  * @ingroup query_operations
126  */
127 typedef bool (* aerospike_query_foreach_callback)(const as_val * val, void * udata);
128 
129 /******************************************************************************
130  * FUNCTIONS
131  *****************************************************************************/
132 
133 /**
134  * Execute a query and call the callback function for each result item.
135  *
136  * ~~~~~~~~~~{.c}
137  * as_query query;
138  * as_query_init(&query, "test", "demo");
139  * as_query_select(&query, "bin1");
140  * as_query_where(&query, "bin2", integer_equals(100));
141  *
142  * if ( aerospike_query_foreach(&as, &err, NULL, &query, callback, NULL) != AEROSPIKE_OK ) {
143  * fprintf(stderr, "error(%d) %s at [%s:%d]", err.code, err.message, err.file, err.line);
144  * }
145  *
146  * as_query_destroy(&query);
147  * ~~~~~~~~~~
148  *
149  * @param as The aerospike instance to use for this operation.
150  * @param err The as_error to be populated if an error occurs.
151  * @param policy The policy to use for this operation. If NULL, then the default policy will be used.
152  * @param query The query to execute against the cluster.
153  * @param callback The callback function to call for each result value.
154  * @param udata User-data to be passed to the callback.
155  *
156  * @return AEROSPIKE_OK on success, otherwise an error.
157  *
158  * @ingroup query_operations
159  */
161  aerospike * as, as_error * err, const as_policy_query * policy,
162  const as_query * query,
163  aerospike_query_foreach_callback callback, void * udata
164  );
as_status
Definition: as_status.h:32
Definition: as_val.h:51
bool(* aerospike_query_foreach_callback)(const as_val *val, void *udata)
as_status aerospike_query_foreach(aerospike *as, as_error *err, const as_policy_query *policy, const as_query *query, aerospike_query_foreach_callback callback, void *udata)