Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bson: Add bson_cursor_find_next ().

A new function to find a key, starting from the current cursor
position.
  • Loading branch information...
commit c17a0f465f7d27f3e44f4fbf1937c423fa97ee48 1 parent 84bb8cf
@algernon authored
View
7 NEWS
@@ -24,6 +24,13 @@ key, and what restrictions make sense in the given context.
The function does NOT do UTF-8 validation, that is completely left up
to the application developer.
+** New function: bson_cursor_find_next()
+When we know that key G always comes after key A, but we do not care
+how far apart they may be, and we do not want to use a separate cursor
+(due to, for example, performance reasons), this new function can
+help: unlike bson_find(), this will start scanning from the current
+cursor position.
+
* 0.1.0 - <2011-05-25 Wed>
** Shared library
View
26 src/bson.c
@@ -893,6 +893,32 @@ bson_cursor_next (bson_cursor *c)
return TRUE;
}
+gboolean
+bson_cursor_find_next (bson_cursor *c, const gchar *name)
+{
+ const gchar *ckey;
+ size_t cpos, cvalue_pos;
+
+ if (!c || !name)
+ return FALSE;
+
+ ckey = c->key;
+ cpos = c->pos;
+ cvalue_pos = c->value_pos;
+
+ while (bson_cursor_next (c))
+ {
+ if (strcmp (bson_cursor_key (c), name) == 0)
+ return TRUE;
+ }
+
+ c->key = ckey;
+ c->pos = cpos;
+ c->value_pos = cvalue_pos;
+
+ return FALSE;
+}
+
bson_cursor *
bson_find (const bson *b, const gchar *name)
{
View
13 src/bson.h
@@ -579,6 +579,19 @@ void bson_cursor_free (bson_cursor *c);
*/
gboolean bson_cursor_next (bson_cursor *c);
+/** Move the cursor to a given key, past the current one.
+ *
+ * Scans the BSON object past the current key, in search for the
+ * specified one, and positions the cursor there if found, leaves it
+ * in place if not.
+ *
+ * @param c is the cursor to move forward.
+ * @param name is the key name to position to.
+ *
+ * @returns TRUE on success, FALSE otherwise.
+ */
+gboolean bson_cursor_find_next (bson_cursor *c, const gchar *name);
+
/** Determine the type of the current element.
*
* @param c is the cursor pointing at the appropriate element.
View
1  tests/Makefile.am
@@ -33,6 +33,7 @@ bson_unit_tests = \
unit/bson/bson_cursor_new \
unit/bson/bson_find \
unit/bson/bson_cursor_next \
+ unit/bson/bson_cursor_find_next \
unit/bson/bson_cursor_type \
unit/bson/bson_cursor_type_as_string \
unit/bson/bson_cursor_key \
View
33 tests/unit/bson/bson_cursor_find_next.c
@@ -0,0 +1,33 @@
+#include "test.h"
+#include "mongo.h"
+
+#include <string.h>
+
+void
+test_bson_cursor_find_next (void)
+{
+ bson *b;
+ bson_cursor *c;
+
+ b = test_bson_generate_full ();
+ c = bson_find (b, "TRUE");
+
+ ok (bson_cursor_find_next (c, NULL) == FALSE,
+ "bson_cursor_find_next() should fail with a NULL key");
+ ok (bson_cursor_find_next (NULL, "int32") == FALSE,
+ "bson_cursor_find_next() should fail with a NULL cursor");
+
+ ok (bson_cursor_find_next (c, "sex") == TRUE,
+ "bson_cursor_find_next() works");
+
+ ok (bson_cursor_find_next (c, "str") == FALSE,
+ "bson_cursor_find_next() should fail when the key is not found");
+
+ ok (bson_cursor_find_next (c, "int64") == TRUE,
+ "bson_cursor_find_next() works, even after a previous failure");
+
+ bson_cursor_free (c);
+ bson_free (b);
+}
+
+RUN_TEST (5, bson_cursor_find_next);
Please sign in to comment.
Something went wrong with that request. Please try again.