summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Adamansky <adamansky@gmail.com>2015-04-20 12:07:50 +0600
committerAnton Adamansky <adamansky@gmail.com>2015-04-20 12:07:50 +0600
commitb6a6110323412d906fbb0da4d2ae28932155cedd (patch)
treee0dbc122856467db0bb927909bcd7f3fda7e6230
parent8fbadcfd83a457fdf18226c2e59b38736d7a7cce (diff)
parente5428ff3d1e364192e436b4fe24cafd0e1333a35 (diff)
downloadejdb-b6a6110323412d906fbb0da4d2ae28932155cedd.tar.gz
ejdb-b6a6110323412d906fbb0da4d2ae28932155cedd.tar.bz2
ejdb-b6a6110323412d906fbb0da4d2ae28932155cedd.zip
Merge branch 'achoy-symbols'
-rw-r--r--src/bson/bson.h2
-rw-r--r--src/ejdb/ejdb.c5
-rw-r--r--src/ejdb/tests/ejdbtest2.c112
3 files changed, 116 insertions, 3 deletions
diff --git a/src/bson/bson.h b/src/bson/bson.h
index de5aab7..a741336 100644
--- a/src/bson/bson.h
+++ b/src/bson/bson.h
@@ -32,7 +32,7 @@
#include "tcutil.h"
#define BSON_IS_NUM_TYPE(atype) (atype == BSON_INT || atype == BSON_LONG || atype == BSON_DOUBLE)
-#define BSON_IS_STRING_TYPE(atype) (atype == BSON_STRING || atype == BSON_SYMBOL)
+#define BSON_IS_STRING_TYPE(atype) ((atype) == BSON_STRING || (atype) == BSON_SYMBOL)
EJDB_EXTERN_C_START
diff --git a/src/ejdb/ejdb.c b/src/ejdb/ejdb.c
index 5eabbfc..d568a57 100644
--- a/src/ejdb/ejdb.c
+++ b/src/ejdb/ejdb.c
@@ -1794,10 +1794,11 @@ static bool _qrybsvalmatch(const EJQF *qf, bson_iterator *it, bool expandarrays,
int sp;
const char *fval;
+ // Feature #129: Handle BSON_SYMBOL like BSON_STRING
#define _FETCHSTRFVAL() \
do { \
- fvalsz = (bt == BSON_STRING) ? bson_iterator_string_len(it) : 1; \
- fval = (bt == BSON_STRING) ? bson_iterator_string(it) : ""; \
+ fvalsz = (BSON_IS_STRING_TYPE(bt)) ? bson_iterator_string_len(it) : 1; \
+ fval = (BSON_IS_STRING_TYPE(bt)) ? bson_iterator_string(it) : ""; \
if (bt == BSON_OID) { \
bson_oid_to_string(bson_iterator_oid(it), oidbuf); \
fvalsz = 25; \
diff --git a/src/ejdb/tests/ejdbtest2.c b/src/ejdb/tests/ejdbtest2.c
index ffaed5f..beea331 100644
--- a/src/ejdb/tests/ejdbtest2.c
+++ b/src/ejdb/tests/ejdbtest2.c
@@ -63,6 +63,7 @@ void testAddData(void) {
bson_append_finish_object(&a1);
bson_append_finish_array(&a1); //EOF complexarr
CU_ASSERT_FALSE_FATAL(a1.err);
+ bson_append_symbol(&a1, "symbol", "apple");
bson_finish(&a1);
ejdbsavebson(ccoll, &a1, &oid);
bson_destroy(&a1);
@@ -100,6 +101,7 @@ void testAddData(void) {
bson_append_long(&a1, "1", 556667);
bson_append_double(&a1, "2", 77676.22);
bson_append_finish_array(&a1);
+ bson_append_symbol(&a1, "symbol", "application");
bson_finish(&a1);
CU_ASSERT_FALSE_FATAL(a1.err);
@@ -122,6 +124,7 @@ void testAddData(void) {
bson_append_long(&a1, "1", 222334);
bson_append_double(&a1, "2", 77676.22);
bson_append_finish_array(&a1);
+ bson_append_symbol(&a1, "symbol", "bison");
bson_finish(&a1);
CU_ASSERT_FALSE_FATAL(a1.err);
@@ -2573,6 +2576,7 @@ void testQuery28(void) { // $gte: 64 bit number
bson_init_as_query(&bsq1);
bson_append_start_object(&bsq1, "longscore");
bson_append_long(&bsq1, "$gte", int64value);
+ bson_append_finish_object(&bsq1);
bson_finish(&bsq1);
CU_ASSERT_FALSE_FATAL(bsq1.err);
@@ -2602,6 +2606,111 @@ void testQuery28(void) { // $gte: 64 bit number
ejdbquerydel(q1);
}
+void testQuery29(void) {
+ // #129: Test $begin Query with Symbols
+ EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(contacts);
+
+ bson bsq1;
+ bson_init_as_query(&bsq1);
+ bson_append_start_object(&bsq1, "symbol");
+ bson_append_string(&bsq1, "$begin", "app");
+ bson_append_finish_object(&bsq1);
+ bson_finish(&bsq1);
+ CU_ASSERT_FALSE_FATAL(bsq1.err);
+
+ EJQ *q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+
+ uint32_t count = 0;
+ TCXSTR *log = tcxstrnew();
+ TCLIST *q1res = ejdbqryexecute(contacts, q1, &count, 0, log);
+ //fprintf(stderr, "%s", TCXSTRPTR(log));
+
+ CU_ASSERT_EQUAL(count, 2); // should match symbol_info: apple, application
+ CU_ASSERT_TRUE(TCLISTNUM(q1res) == 2);
+
+ for (int i = 0; i < TCLISTNUM(q1res); ++i) {
+ if (i == 0) CU_ASSERT_FALSE(bson_compare_string("apple", TCLISTVALPTR(q1res, i), "symbol"));
+ if (i == 1) CU_ASSERT_FALSE(bson_compare_string("application", TCLISTVALPTR(q1res, i), "symbol"));
+ }
+
+ bson_destroy(&bsq1);
+ tclistdel(q1res);
+ tcxstrdel(log);
+ ejdbquerydel(q1);
+}
+
+void testQuery30(void) {
+ // #129: Test equal with Symbols
+ EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(contacts);
+
+ bson bsq1;
+ bson_init_as_query(&bsq1);
+ bson_append_string(&bsq1, "symbol", "bison");
+ bson_finish(&bsq1);
+ CU_ASSERT_FALSE_FATAL(bsq1.err);
+
+ EJQ *q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+
+ uint32_t count = 0;
+ TCXSTR *log = tcxstrnew();
+ TCLIST *q1res = ejdbqryexecute(contacts, q1, &count, 0, log);
+ //fprintf(stderr, "%s", TCXSTRPTR(log));
+
+ CU_ASSERT_EQUAL(count, 1); // should match symbol_info: bison
+ CU_ASSERT_TRUE(TCLISTNUM(q1res) == 1);
+
+ for (int i = 0; i < TCLISTNUM(q1res); ++i) {
+ if (i == 0) CU_ASSERT_FALSE(bson_compare_string("bison", TCLISTVALPTR(q1res, i), "symbol"));
+ }
+
+ bson_destroy(&bsq1);
+ tclistdel(q1res);
+ tcxstrdel(log);
+ ejdbquerydel(q1);
+}
+
+void testQuery31(void) {
+ // #129: Test $in array Query with Symbols
+ EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(contacts);
+
+ bson bsq1;
+ bson_init_as_query(&bsq1);
+ bson_append_start_object(&bsq1, "symbol");
+ bson_append_start_array(&bsq1, "$in");
+ bson_append_string(&bsq1, "0", "apple");
+ bson_append_string(&bsq1, "1", "bison");
+ bson_append_finish_array(&bsq1);
+ bson_append_finish_object(&bsq1);
+ bson_finish(&bsq1);
+ CU_ASSERT_FALSE_FATAL(bsq1.err);
+
+ EJQ *q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, NULL);
+ CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+
+ uint32_t count = 0;
+ TCXSTR *log = tcxstrnew();
+ TCLIST *q1res = ejdbqryexecute(contacts, q1, &count, 0, log);
+ //fprintf(stderr, "%s", TCXSTRPTR(log));
+
+ CU_ASSERT_EQUAL(count, 2); // should match symbol_info: apple, bison
+ CU_ASSERT_TRUE(TCLISTNUM(q1res) == 2);
+
+ for (int i = 0; i < TCLISTNUM(q1res); ++i) {
+ if (i == 0) CU_ASSERT_FALSE(bson_compare_string("apple", TCLISTVALPTR(q1res, i), "symbol"));
+ if (i == 1) CU_ASSERT_FALSE(bson_compare_string("bison", TCLISTVALPTR(q1res, i), "symbol"));
+ }
+
+ bson_destroy(&bsq1);
+ tclistdel(q1res);
+ tcxstrdel(log);
+ ejdbquerydel(q1);
+}
+
void testOIDSMatching(void) { //OID matching
EJCOLL *contacts = ejdbcreatecoll(jb, "contacts", NULL);
CU_ASSERT_PTR_NOT_NULL_FATAL(contacts);
@@ -5772,6 +5881,9 @@ int main() {
(NULL == CU_add_test(pSuite, "testQuery26", testQuery26)) ||
(NULL == CU_add_test(pSuite, "testQuery27", testQuery27)) ||
(NULL == CU_add_test(pSuite, "testQuery28", testQuery28)) ||
+ (NULL == CU_add_test(pSuite, "testQuery29", testQuery29)) ||
+ (NULL == CU_add_test(pSuite, "testQuery30", testQuery30)) ||
+ (NULL == CU_add_test(pSuite, "testQuery31", testQuery31)) ||
(NULL == CU_add_test(pSuite, "testOIDSMatching", testOIDSMatching)) ||
(NULL == CU_add_test(pSuite, "testEmptyFieldIndex", testEmptyFieldIndex)) ||
(NULL == CU_add_test(pSuite, "testICaseIndex", testICaseIndex)) ||