Main Page
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
aerospike
aerospike_query.h
Go to the documentation of this file.
1
/*
2
* Copyright 2008-2015 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
#ifdef __cplusplus
20
extern
"C"
{
21
#endif
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.
52
*
53
* ~~~~~~~~~~{.c}
54
* as_query query;
55
* as_query_init(&query, "test", "demo");
56
*
57
* as_query_where_init(&query, 1);
58
* as_query_where(&query, "bin2", as_integer_equals(100));
59
* ~~~~~~~~~~
60
*
61
* Now that we have a query defined, we want to execute it using
62
* aerospike_query_foreach().
63
*
64
* ~~~~~~~~~~{.c}
65
* if ( aerospike_query_foreach(&as, &err, NULL, &query, callback, NULL) != AEROSPIKE_OK ) {
66
* fprintf(stderr, "error(%d) %s at [%s:%d]", err.code, err.message, err.file, err.line);
67
* }
68
* ~~~~~~~~~~
69
*
70
* The callback provided to the function above is implemented as:
71
*
72
* ~~~~~~~~~~{.c}
73
* bool callback(const as_val * val, void * udata) {
74
* as_record * rec = as_record_fromval(val);
75
* if ( !rec ) return false;
76
* fprintf("record contains %d bins", as_record_numbins(rec));
77
* return true;
78
* }
79
* ~~~~~~~~~~
80
*
81
* An as_query is simply a query definition, so it does not contain any state,
82
* allowing it to be reused for multiple query operations.
83
*
84
* When you are finished with the query, you should destroy the resources
85
* allocated to it:
86
*
87
* ~~~~~~~~~~{.c}
88
* as_query_destroy(&query);
89
* ~~~~~~~~~~
90
*
91
*/
92
93
#include <
aerospike/aerospike.h
>
94
#include <
aerospike/as_error.h
>
95
#include <
aerospike/as_policy.h
>
96
#include <
aerospike/as_query.h
>
97
#include <
aerospike/as_status.h
>
98
#include <
aerospike/as_stream.h
>
99
100
/******************************************************************************
101
* TYPES
102
*****************************************************************************/
103
104
/**
105
* This callback will be called for each value or record returned from
106
* a query.
107
*
108
* The aerospike_query_foreach() function accepts this callback.
109
*
110
* ~~~~~~~~~~{.c}
111
* bool my_callback(as_val * val, void * udata) {
112
* return true;
113
* }
114
* ~~~~~~~~~~
115
*
116
* @param val The value received from the query.
117
* @param udata User-data provided to the calling function.
118
*
119
* @return `true` to continue to the next value. Otherwise, iteration will end.
120
*
121
* @ingroup query_operations
122
*/
123
typedef
bool (*
aerospike_query_foreach_callback
)(
const
as_val
* val,
void
* udata);
124
125
/******************************************************************************
126
* FUNCTIONS
127
*****************************************************************************/
128
129
/**
130
* Execute a query and call the callback function for each result item.
131
*
132
* ~~~~~~~~~~{.c}
133
* as_query query;
134
* as_query_init(&query, "test", "demo");
135
* as_query_select(&query, "bin1");
136
* as_query_where(&query, "bin2", as_integer_equals(100));
137
*
138
* if ( aerospike_query_foreach(&as, &err, NULL, &query, callback, NULL) != AEROSPIKE_OK ) {
139
* fprintf(stderr, "error(%d) %s at [%s:%d]", err.code, err.message, err.file, err.line);
140
* }
141
*
142
* as_query_destroy(&query);
143
* ~~~~~~~~~~
144
*
145
* @param as The aerospike instance to use for this operation.
146
* @param err The as_error to be populated if an error occurs.
147
* @param policy The policy to use for this operation. If NULL, then the default policy will be used.
148
* @param query The query to execute against the cluster.
149
* @param callback The callback function to call for each result value.
150
* @param udata User-data to be passed to the callback.
151
*
152
* @return AEROSPIKE_OK on success, otherwise an error.
153
*
154
* @ingroup query_operations
155
*/
156
as_status
aerospike_query_foreach
(
157
aerospike
* as,
as_error
* err,
const
as_policy_query
* policy,
158
const
as_query
* query,
159
aerospike_query_foreach_callback
callback,
void
* udata
160
);
161
162
#ifdef __cplusplus
163
}
// end extern "C"
164
#endif
as_error
Definition:
as_error.h:96
as_status
as_status
Definition:
as_status.h:30
as_val
Definition:
as_val.h:55
as_policy.h
as_status.h
as_policy_query
Definition:
as_policy.h:536
as_stream.h
aerospike_query_foreach_callback
bool(* aerospike_query_foreach_callback)(const as_val *val, void *udata)
Definition:
aerospike_query.h:123
aerospike_query_foreach
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)
as_error.h
aerospike
Definition:
aerospike.h:162
as_query.h
aerospike.h
as_query
Definition:
as_query.h:466