All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_record_iterator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 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_bytes.h>
21 #include <aerospike/as_integer.h>
22 #include <aerospike/as_key.h>
23 #include <aerospike/as_list.h>
24 #include <aerospike/as_map.h>
25 #include <aerospike/as_rec.h>
26 #include <aerospike/as_record.h>
27 #include <aerospike/as_string.h>
28 #include <aerospike/as_util.h>
29 #include <aerospike/as_val.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /******************************************************************************
36  * TYPES
37  *****************************************************************************/
38 
39 /**
40  * Iterator over bins of a record.
41  *
42  * ## Initialization
43  *
44  * The as_record_iterator can be initialized via:
45  *
46  * - as_record_iterator_init() — initializes a stack allocated
47  * as_record_iterator.
48  * - as_record_iterator_new() — allocated and initializes an
49  * as_record_iterator on the heap.
50  *
51  * Both of the function require the record on which it will iterate.
52  *
53  * To initialize an as_record_iterator on the stack:
54  *
55  * ~~~~~~~~~~{.c}
56  * as_record_iterator it;
57  * as_record_iterator_init(&it, record);
58  * ~~~~~~~~~~
59  *
60  * To initialize an as_record_iterator on the heap:
61  *
62  * ~~~~~~~~~~{.c}
63  * as_record_iterator * it as_record_iterator_new(record);
64  * ~~~~~~~~~~
65  *
66  * ## Destruction
67  *
68  * When you no longer require the iterator, you should release it and
69  * associated resource via as_record_iterator_destroy():
70  *
71  * ~~~~~~~~~~{.c}
72  * as_record_iterator_destroy(it);
73  * ~~~~~~~~~~
74  *
75  * ## Usage
76  *
77  * With an initialized as_record_iterator, you can traverse the bins of
78  * a record.
79  *
80  * Traversal is usually performed by first checking to see if
81  * the there are any bins available to traverse to via
82  * as_record_iterator_has_next(), which returns true if there are more bins,
83  * or false if there are no more bins.
84  *
85  * ~~~~~~~~~~{.c}
86  * as_record_iterator_has_next(&it);
87  * ~~~~~~~~~~
88  *
89  * When you are sure there are more bins, then you will use
90  * as_record_iterator_next() to read the next bin. If there are no bins
91  * available, then NULL is returned.
92  *
93  * ~~~~~~~~~~{.c}
94  * as_bin* bin = as_record_iterator_next(&it);
95  * ~~~~~~~~~~
96  *
97  * If as_record_iterator_next() returns a bin, then you can use the following
98  * functions to get information about the bin:
99  *
100  * - as_bin_get_name() — Get the bin's name.
101  * - as_bin_get_value() — Get the bin's value.
102  * - as_bin_get_type() — Get the bin's values' types.
103  *
104  * Most often, a traversal is performed in a while loop. The following is a
105  * simple example:
106  *
107  * ~~~~~~~~~~{.c}
108  * while ( as_record_iterator_has_next(&it) ) {
109  * as_bin* bin = as_record_iterator_next(&it);
110  * char* name = as_bin_get_name(bin);
111  * as_val* value = (as_val*) as_bin_get_value(bin);
112  * }
113  * ~~~~~~~~~~
114  *
115  * @ingroup as_record_object
116  */
117 typedef struct as_record_iterator_s {
118 
119  /**
120  * @private
121  * If true, then as_record_iterator_destroy() will free this object.
122  */
123  bool _free;
124 
125  /**
126  * The record being iterated over.
127  */
129 
130  /**
131  * Current position of the iterator
132  */
133  uint32_t pos;
134 
136 
137 /******************************************************************************
138  * FUNCTIONS
139  *****************************************************************************/
140 
141 /**
142  * Create and initialize a heap allocated as_record_iterator for the
143  * specified record.
144  *
145  * ~~~~~~~~~~{.c}
146  * as_record_iterator * it = as_record_iterator_new(rec);
147  *
148  * while (as_record_iterator_has_next(&it)) {
149  * as_bin* bin = as_record_iterator_next(&it);
150  * }
151  *
152  * as_record_iterator_destroy(&it);
153  * ~~~~~~~~~~
154  *
155  * @param record The record to iterate over.
156  *
157  * @return On success, a new as_record_iterator. Otherwise an error occurred.
158  *
159  * @relates as_record_iterator
160  * @ingroup as_record_object
161  */
163 as_record_iterator_new(const as_record* record);
164 
165 /**
166  * Initializes a stack allocated as_record_iterator for the specified record.
167  *
168  * ~~~~~~~~~~{.c}
169  * as_record_iterator it;
170  * as_record_iterator_init(&it, rec);
171  *
172  * while (as_record_iterator_has_next(&it)) {
173  * as_bin* bin = as_record_iterator_next(&it);
174  * }
175  *
176  * as_record_iterator_destroy(&it);
177  * ~~~~~~~~~~
178  *
179  * When you are finished using the `as_record` instance, you should release the
180  * resources allocated to it by calling `as_record_destroy()`.
181  *
182  * @param iterator The iterator to initialize.
183  * @param record The record to iterate over
184  *
185  * @return On success, a new as_record_iterator. Otherwise an error occurred.
186  *
187  * @relates as_record_iterator
188  * @ingroup as_record_object
189  */
191 as_record_iterator_init(as_record_iterator* iterator, const as_record* record);
192 
193 /**
194  * Destroy the as_record_iterator and associated resources.
195  *
196  * @param iterator The iterator to destroy.
197  *
198  * @relates as_record_iterator
199  * @ingroup as_record_object
200  */
201 AS_EXTERN void
203 
204 /**
205  * Test if there are more bins in the iterator.
206  *
207  * @param iterator The iterator to test.
208  *
209  * @return the number of bins in the record.
210  *
211  * @relates as_record_iterator
212  * @ingroup as_record_object
213  */
214 AS_EXTERN bool
216 
217 /**
218  * Read the next bin from the iterator.
219  *
220  * @param iterator The iterator to read from.
221  *
222  * @return The next bin from the iterator.
223  *
224  * @relates as_record_iterator
225  * @ingroup as_record_object
226  */
229 
230 #ifdef __cplusplus
231 } // end extern "C"
232 #endif
Definition: as_bin.h:79
AS_EXTERN as_record_iterator * as_record_iterator_new(const as_record *record)
#define AS_EXTERN
Definition: as_std.h:25
AS_EXTERN void as_record_iterator_destroy(as_record_iterator *iterator)
const as_record * record
AS_EXTERN as_record_iterator * as_record_iterator_init(as_record_iterator *iterator, const as_record *record)
AS_EXTERN as_bin * as_record_iterator_next(as_record_iterator *iterator)
AS_EXTERN bool as_record_iterator_has_next(const as_record_iterator *iterator)