Skip to content

Commit

Permalink
bson subselects fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
crosstantine committed Sep 10, 2012
1 parent f372a49 commit 5d3488b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
4 changes: 2 additions & 2 deletions db/bson/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ AM_CPPFLAGS += $(all_includes)
lib_LTLIBRARIES = libdjon-bson.la
libdjon_bson_la_LDFLAGS = -static
libdjon_bson_la_SOURCES = bsonobj.cpp \
bsonobj.h \
bsonarrayobj.cpp \
bsonutil.cpp \
bsoncontent.cpp \
bsonparser.cpp

library_includedir=$(includedir)/djondb
library_include_HEADERS = bson.h bsonobj.h bsonparser.h bsondefs.h bsoncontent.h bsonarrayobj.h
library_include_HEADERS = bson.h bsonobj.h bsonutil.h bsonparser.h bsondefs.h bsoncontent.h bsonarrayobj.h

TESTS = test-bson
bin_PROGRAMS = test-bson test-performance
Expand All @@ -28,6 +27,7 @@ if !MAC
test_bson_LDADD += -lrt -luuid
else
endif

test_bson_SOURCES = tests/main.cpp

test_performance_SOURCES = tests/testperformance.cpp
Expand Down
26 changes: 20 additions & 6 deletions db/bson/bsonutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/

#include "bsonutil.h"
#include "util.h"
#include <string.h>
#include <vector>
#include <stdlib.h>


std::set<std::string> bson_splitSelect(const char* select) {
std::vector<std::string> elements = split(std::string(select), ",");
Expand All @@ -41,28 +46,37 @@ std::set<std::string> bson_splitSelect(const char* select) {
}

char* bson_subselect(const char* select, const char* name) {
std::set<std::string> elements = splitSelect(select);
MemoryStream ms(2048);
std::vector<std::string> elements = split(std::string(select), ", ");
char* result = (char*)malloc(strlen(select) + 1);
memset(result, 0, strlen(select) + 1);
int pos = 0;

std::string startXpath = format("%s.", name);
int lenStartXpath = startXpath.length();
bool first = true;
for (std::set<std::string>::const_iterator i = elements.begin(); i != elements.end(); i++) {
for (std::vector<std::string>::const_iterator i = elements.begin(); i != elements.end(); i++) {
std::string selement = *i;
char* element = const_cast<char*>(selement.c_str());
element = trim(element, strlen(element));
if (startsWith(element, "$")) {
// Remvoes the $" " from the element
element = strcpy(element, 2, strlen(element) - 3);
if (startsWith(element, startXpath.c_str())) {
char* suffix = strcpy(element, lenStartXpath, strlen(element) - lenStartXpath);
ms.writeChars(suffix, strlen(suffix));
if (!first) {
ms.writeChars(", ", 2);
memcpy(result + pos, ", ", 2);
pos += 2;
}
char* suffix = strcpy(element, lenStartXpath, strlen(element) - lenStartXpath);
memcpy(result + pos, "$\"", 2);
pos+=2;
memcpy(result + pos, suffix, strlen(suffix));
pos += strlen(suffix);
memcpy(result + pos, "\"", 1);
pos++;
first = false;
free(suffix);
}
}
}
return result;
}
3 changes: 3 additions & 0 deletions db/bson/bsonutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#ifndef BSONUTIL_INCLUDE_H
#define BSONUTIL_INCLUDE_H

#include <string>
#include <set>

std::set<std::string> bson_splitSelect(const char* select);
char* bson_subselect(const char* select, const char* elment);

Expand Down
39 changes: 38 additions & 1 deletion db/bson/tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
// *********************************************************************************************************************

#include <iostream>
#include "../bson.h"
#include "bson.h"
#include "bsonutil.h"
#include <string>
#include <string.h>
#include <cpptest.h>
Expand All @@ -43,6 +44,7 @@ class TestBSONSuite : public Test::Suite
TEST_ADD(TestBSONSuite::testComparison);
TEST_ADD(TestBSONSuite::testAutocasting);
TEST_ADD(TestBSONSuite::testXPath);
TEST_ADD(TestBSONSuite::testBSONUtil);
}

private:
Expand Down Expand Up @@ -453,6 +455,41 @@ class TestBSONSuite : public Test::Suite
delete obj1;
}

void testBSONUtil() {
cout << "\ntestBSONUtil" << endl;

char* selectsimple = "$\"test\"";
std::set<std::string> result = bson_splitSelect("$\"test\"");
TEST_ASSERT(result.size() == 1);
TEST_ASSERT(result.find("test") != result.end());

selectsimple = "$\"test\", $\"test2\"";
result = bson_splitSelect("$\"test\"");
TEST_ASSERT(result.size() == 1);
TEST_ASSERT(result.find("test") != result.end());
result = bson_splitSelect("$\"test2\"");
TEST_ASSERT(result.size() == 1);
TEST_ASSERT(result.find("test2") != result.end());

selectsimple = "$\"test\", $\"test2.inner1\"";
result = bson_splitSelect("$\"test\"");
TEST_ASSERT(result.size() == 1);
TEST_ASSERT(result.find("test") != result.end());
result = bson_splitSelect("$\"test\", $\"test2\"");
TEST_ASSERT(result.size() == 2);
TEST_ASSERT(result.find("test") != result.end());
TEST_ASSERT(result.find("test2") != result.end());

selectsimple = "$\"test2.inner\"";
char* subresult = bson_subselect(selectsimple, "test2");
char* expected = "$\"inner\"";
TEST_ASSERT(strcmp(subresult, expected) == 0);

selectsimple = "$\"test1\", $\"test2.inner\", $\"test1.testinner\", $\"test2.inner2\", $\"test2.inner2.testii\"";
subresult = bson_subselect(selectsimple, "test2");
expected = "$\"inner\", $\"inner2\", $\"inner2.testii\"";
TEST_ASSERT(strcmp(subresult, expected) == 0);
}
};

enum OutputType
Expand Down

0 comments on commit 5d3488b

Please sign in to comment.