1 /*-
2  * Public Domain 2014-2018 MongoDB, Inc.
3  * Public Domain 2008-2014 WiredTiger, Inc.
4  *
5  * This is free and unencumbered software released into the public domain.
6  *
7  * Anyone is free to copy, modify, publish, use, compile, sell, or
8  * distribute this software, either in source code form or as a compiled
9  * binary, for any purpose, commercial or non-commercial, and by any
10  * means.
11  *
12  * In jurisdictions that recognize copyright laws, the author or authors
13  * of this software dedicate any and all copyright interest in the
14  * software to the public domain. We make this dedication for the benefit
15  * of the public at large and to the detriment of our heirs and
16  * successors. We intend this dedication to be an overt act of
17  * relinquishment in perpetuity of all present and future rights to this
18  * software under copyright law.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  *
28  * ex_cursor.c
29  *	This is an example demonstrating some cursor types and operations.
30  */
31 #include <test_util.h>
32 
33 int cursor_reset(WT_CURSOR *cursor);
34 int cursor_forward_scan(WT_CURSOR *cursor);
35 int cursor_reverse_scan(WT_CURSOR *cursor);
36 int cursor_search(WT_CURSOR *cursor);
37 int cursor_search_near(WT_CURSOR *cursor);
38 int cursor_insert(WT_CURSOR *cursor);
39 int cursor_update(WT_CURSOR *cursor);
40 int cursor_remove(WT_CURSOR *cursor);
41 
42 static const char *home;
43 
44 /*! [cursor next] */
45 int
cursor_forward_scan(WT_CURSOR * cursor)46 cursor_forward_scan(WT_CURSOR *cursor)
47 {
48 	const char *key, *value;
49 	int ret;
50 
51 	while ((ret = cursor->next(cursor)) == 0) {
52 		error_check(cursor->get_key(cursor, &key));
53 		error_check(cursor->get_value(cursor, &value));
54 	}
55 	scan_end_check(ret == WT_NOTFOUND);
56 
57 	return (0);
58 }
59 /*! [cursor next] */
60 
61 /*! [cursor prev] */
62 int
cursor_reverse_scan(WT_CURSOR * cursor)63 cursor_reverse_scan(WT_CURSOR *cursor)
64 {
65 	const char *key, *value;
66 	int ret;
67 
68 	while ((ret = cursor->prev(cursor)) == 0) {
69 		error_check(cursor->get_key(cursor, &key));
70 		error_check(cursor->get_value(cursor, &value));
71 	}
72 	scan_end_check(ret == WT_NOTFOUND);
73 
74 	return (0);
75 }
76 /*! [cursor prev] */
77 
78 /*! [cursor reset] */
79 int
cursor_reset(WT_CURSOR * cursor)80 cursor_reset(WT_CURSOR *cursor)
81 {
82 	return (cursor->reset(cursor));
83 }
84 /*! [cursor reset] */
85 
86 /*! [cursor search] */
87 int
cursor_search(WT_CURSOR * cursor)88 cursor_search(WT_CURSOR *cursor)
89 {
90 	const char *value;
91 
92 	cursor->set_key(cursor, "foo");
93 
94 	error_check(cursor->search(cursor));
95 	error_check(cursor->get_value(cursor, &value));
96 
97 	return (0);
98 }
99 /*! [cursor search] */
100 
101 /*! [cursor search near] */
102 int
cursor_search_near(WT_CURSOR * cursor)103 cursor_search_near(WT_CURSOR *cursor)
104 {
105 	const char *key, *value;
106 	int exact;
107 
108 	cursor->set_key(cursor, "foo");
109 
110 	error_check(cursor->search_near(cursor, &exact));
111 	switch (exact) {
112 	case -1:	/* Returned key smaller than search key */
113 		error_check(cursor->get_key(cursor, &key));
114 		break;
115 	case 0:		/* Exact match found */
116 		break;
117 	case 1:		/* Returned key larger than search key */
118 		error_check(cursor->get_key(cursor, &key));
119 		break;
120 	}
121 	error_check(cursor->get_value(cursor, &value));
122 
123 	return (0);
124 }
125 /*! [cursor search near] */
126 
127 /*! [cursor insert] */
128 int
cursor_insert(WT_CURSOR * cursor)129 cursor_insert(WT_CURSOR *cursor)
130 {
131 	cursor->set_key(cursor, "foo");
132 	cursor->set_value(cursor, "bar");
133 
134 	return (cursor->insert(cursor));
135 }
136 /*! [cursor insert] */
137 
138 /*! [cursor update] */
139 int
cursor_update(WT_CURSOR * cursor)140 cursor_update(WT_CURSOR *cursor)
141 {
142 	cursor->set_key(cursor, "foo");
143 	cursor->set_value(cursor, "newbar");
144 
145 	return (cursor->update(cursor));
146 }
147 /*! [cursor update] */
148 
149 /*! [cursor remove] */
150 int
cursor_remove(WT_CURSOR * cursor)151 cursor_remove(WT_CURSOR *cursor)
152 {
153 	cursor->set_key(cursor, "foo");
154 	return (cursor->remove(cursor));
155 }
156 /*! [cursor remove] */
157 
158 int
main(int argc,char * argv[])159 main(int argc, char *argv[])
160 {
161 	WT_CONNECTION *conn;
162 	WT_CURSOR *cursor;
163 	WT_SESSION *session;
164 
165 	home = example_setup(argc, argv);
166 
167 	/* Open a connection to the database, creating it if necessary. */
168 	error_check(wiredtiger_open(
169 	    home, NULL, "create,statistics=(fast)", &conn));
170 
171 	/* Open a session for the current thread's work. */
172 	error_check(conn->open_session(conn, NULL, NULL, &session));
173 
174 	error_check(session->create(session, "table:world",
175 	    "key_format=r,value_format=5sii,"
176 	    "columns=(id,country,population,area)"));
177 
178 	/*! [open cursor #1] */
179 	error_check(session->open_cursor(
180 	    session, "table:world", NULL, NULL, &cursor));
181 	/*! [open cursor #1] */
182 
183 	/*! [open cursor #2] */
184 	error_check(session->open_cursor(session,
185 	    "table:world(country,population)", NULL, NULL, &cursor));
186 	/*! [open cursor #2] */
187 
188 	/*! [open cursor #3] */
189 	error_check(session->open_cursor(
190 	    session, "statistics:", NULL, NULL, &cursor));
191 	/*! [open cursor #3] */
192 
193 	/* Create a simple string table to illustrate basic operations. */
194 	error_check(session->create(
195 	    session, "table:map", "key_format=S,value_format=S"));
196 	error_check(session->open_cursor(
197 	    session, "table:map", NULL, NULL, &cursor));
198 	error_check(cursor_insert(cursor));
199 	error_check(cursor_reset(cursor));
200 	error_check(cursor_forward_scan(cursor));
201 	error_check(cursor_reset(cursor));
202 	error_check(cursor_reverse_scan(cursor));
203 	error_check(cursor_search_near(cursor));
204 	error_check(cursor_update(cursor));
205 	error_check(cursor_remove(cursor));
206 	error_check(cursor->close(cursor));
207 
208 	/* Note: closing the connection implicitly closes open session(s). */
209 	error_check(conn->close(conn, NULL));
210 
211 	return (EXIT_SUCCESS);
212 }
213