std::list<Index*> IndexPage::find(BufferManager* manager, FilterParser* parser) { if (!isLoaded()) { loadPage(manager); } std::list<Index*> result; for (int x = 0; x < size; x++) { BSONObj* key = elements[x]->key; if (key->getString("_id").compare("c597-43e1-ae9b-6f5451b28295") == 0) { cout << "Hey!" << endl; } bool match = false; ExpressionResult* expresult = parser->eval(*key); if (expresult->type() == ExpressionResult::RT_BOOLEAN) { match = *expresult; } delete expresult; if (match) { result.push_back(elements[x]); } } for (int x = 0; x <= size; x++) { IndexPage* innerPage = pointers[x]; if (innerPage != NULL) { std::list<Index*> inner = innerPage->find(manager, parser); result.insert(result.begin(), inner.begin(), inner.end()); } } return result; }
void testAutocasting() { cout << "testAutocasting" << endl; BSONObj o; o.add("long", (__int64)1L); o.add("double", 2.0); o.add("int", 1); o.add("char*", (char*)"Test"); BSONObj inner; inner.add("text", "text"); o.add("inner", inner); TEST_ASSERT(o.getContent("long") != NULL); TEST_ASSERT((__int64)*o.getContent("long") == 1L); TEST_ASSERT(o.getContent("double") != NULL); TEST_ASSERT((double)*o.getContent("double") == 2.0); TEST_ASSERT(o.getContent("int") != NULL); TEST_ASSERT((int)*o.getContent("int") == 1); TEST_ASSERT(o.getContent("inner") != NULL); BSONObj* obj = *o.getContent("inner"); TEST_ASSERT(strcmp(obj->getString("text"), "text") == 0); }
TEST(TestCommand, testUpdateCommand) { cout << "testUpdateCommand" << endl; FileOutputStream* fos = new FileOutputStream("test.dat", "wb"); CommandWriter* commandWriter = new CommandWriter(fos); UpdateCommand cmd; cmd.setDB("testdb"); cmd.setNameSpace("test.namespace.db"); BSONObj obj; std::string* uid = uuid(); obj.add("_id", const_cast<char*>(uid->c_str())); delete uid; obj.add("name", "Cross"); obj.add("age", 18); cmd.setBSON(obj); commandWriter->writeCommand(&cmd); fos->close(); delete fos; delete commandWriter; FileInputStream* fis = new FileInputStream("test.dat", "rb"); CommandReader* reader = new CommandReader(fis); UpdateCommand* rdCmd = (UpdateCommand*) reader->readCommand(); EXPECT_TRUE(rdCmd != NULL); EXPECT_TRUE(rdCmd->nameSpace()->compare("test.namespace.db") == 0); EXPECT_TRUE(rdCmd->DB()->compare("testdb") == 0); BSONObj* objResult = rdCmd->bson(); EXPECT_TRUE(objResult != NULL); EXPECT_TRUE(objResult->has("name")); EXPECT_TRUE(objResult->getString("name").compare("Cross") == 0); }
void testParserArray() { cout << "testParserArray" << endl; BSONArrayObj* array = BSONParser::parseArray("[{age: 1, name: 'John', salary: 3500.25, rel1: {innertext: 'inner text'}}, {age: 2, name: 'John2', salary: 23500.25, rel1: {innertext: 'inner text2'}}]"); TEST_ASSERT(array != NULL); TEST_ASSERT(array->length() == 2); BSONObj* obj = array->get(0); TEST_ASSERT(obj != NULL); TEST_ASSERT(obj->has("age")); TEST_ASSERT(obj->getInt("age") == 1); TEST_ASSERT(obj->has("name")); TEST_ASSERT(strcmp(obj->getString("name"), "John") == 0); TEST_ASSERT(obj->has("salary")); TEST_ASSERT(obj->getDouble("salary") == 3500.25); TEST_ASSERT(obj->getBSON("rel1") != NULL); TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0); BSONArrayObj::iterator i = array->begin(); TEST_ASSERT(i != array->end()); delete array; }
void testCopyBSON() { cout << "testCopyBSON" << endl; BSONObj* objOrig = new BSONObj(); // Add in objOrig->add("int", 1); objOrig->add("string", (char*)"test"); objOrig->add("long", (__int64)1L); objOrig->add("double", 1.1); BSONObj rel; rel.add("innertext", (char*)"inner text"); objOrig->add("rel1", rel); BSONArrayObj array; BSONObj b1; b1.add("b1", "test"); array.add(b1); BSONObj b2; b2.add("b1", "test2"); array.add(b2); objOrig->add("array", array); BSONObj* obj = new BSONObj(*objOrig); delete objOrig; objOrig = NULL; TEST_ASSERT(obj->has("int")); TEST_ASSERT(obj->getInt("int") == 1); TEST_ASSERT(strcmp(obj->getString("string"), "test") == 0); TEST_ASSERT(obj->has("long")); TEST_ASSERT(obj->getLong("long") == 1L); TEST_ASSERT(obj->has("double")); TEST_ASSERT(obj->getDouble("double") == 1.1); BSONObj* temp = obj->getBSON("rel1"); TEST_ASSERT(temp != NULL); TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0); TEST_ASSERT(obj->getBSONArray("array") != NULL); BSONArrayObj* arrayR = obj->getBSONArray("array"); TEST_ASSERT(arrayR != NULL); TEST_ASSERT(arrayR->length() == 2); BSONObj* el1 = arrayR->get(0); TEST_ASSERT(el1 != NULL); BSONObj* el2 = arrayR->get(1); TEST_ASSERT(el2 != NULL); delete obj; }
void testParserTrivial() { cout << "testParserTrivial" << endl; BSONObj* obj = BSONParser::parse("{age: '1'}"); TEST_ASSERT(obj->has("age")); TEST_ASSERT(strcmp(obj->getString("age"), "1") == 0); delete obj; BSONObj* obj2 = BSONParser::parse("{\"type\":\"2\",\"category\":\"1\",\"title\":\"test\",\"price\":\"asdf\",\"place\":\"asdf\",\"description\":\"asdf\"}"); TEST_ASSERT(obj2->has("type")); delete obj2; }
void testBigBSON() { cout << "testBigBSON" << endl; BSONObj* obj = new BSONObj(); int chars = 1000; // Add in obj->add("int", 1); obj->add("long", (__int64)LONG_MAX); obj->add("long long", (__int64)LLONG_MAX); char* temp = (char*)malloc(chars+1); memset(temp, 0, chars+1); memset(temp, 'a', chars); obj->add("char*", temp); BSONObj rel; rel.add("innertext", temp); obj->add("rel1", rel); char* json = obj->toChar(); BSONObj* obj2 = BSONParser::parse(json); free(json); TEST_ASSERT(obj->has("int")); TEST_ASSERT(obj->getInt("int") == 1); TEST_ASSERT(obj->has("long")); TEST_ASSERT(obj->getLong("long") == LONG_MAX); TEST_ASSERT(obj->has("long long")); TEST_ASSERT(obj->getLong("long long") == LLONG_MAX); TEST_ASSERT(strcmp(obj->getString("char*"), temp) == 0); TEST_ASSERT(obj->has("rel1")); TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), temp) == 0); delete obj; delete obj2; free(temp); }
void testParserDoubleRelation() { cout << "testParserDoubleRelation" << endl; BSONObj* obj = BSONParser::parse("{age: 1, name: 'John', salary: 3500.25, rel1: {innertext: 'inner text', innerrel1: {innertext:'text2'}}}"); TEST_ASSERT(obj->has("age")); TEST_ASSERT(obj->getInt("age") == 1); TEST_ASSERT(obj->has("name")); TEST_ASSERT(strcmp(obj->getString("name"), "John") == 0); TEST_ASSERT(obj->has("salary")); TEST_ASSERT(obj->getDouble("salary") == 3500.25); TEST_ASSERT(obj->getBSON("rel1") != NULL); TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0); TEST_ASSERT(obj->getBSON("rel1")->getBSON("innerrel1") != NULL); TEST_ASSERT(strcmp(obj->getBSON("rel1")->getBSON("innerrel1")->getString("innertext"), "text2") == 0); delete obj; }
void testParserCollection() { cout << "testParserCollection" << endl; BSONObj* obj = BSONParser::parse("{age: 1, name: 'John', salary: 3500.25, rel1: [{innertext: 'inner text'}, {innertext: 'inner text'}, {innertext: 'inner text'}, {innertext: 'inner text'} ] }"); TEST_ASSERT(obj->has("age")); TEST_ASSERT(obj->getInt("age") == 1); TEST_ASSERT(obj->has("name")); TEST_ASSERT(strcmp(obj->getString("name"), "John") == 0); TEST_ASSERT(obj->has("salary")); TEST_ASSERT(obj->getDouble("salary") == 3500.25); TEST_ASSERT(obj->getBSONArray("rel1") != NULL); TEST_ASSERT(obj->getBSONArray("rel1")->length() == 4); TEST_ASSERT(obj->getBSONArray("rel1")->get(0)->has("innertext")); TEST_ASSERT(strcmp(obj->getBSONArray("rel1")->get(0)->getString("innertext"), "inner text") == 0); delete obj; }
void testParserRelation() { cout << "testParserRelation" << endl; BSONObj* obj = BSONParser::parse("{age: 1, name: 'John', rel1: {innertext: 'inner text', salary: 150000, rent: 10000}}"); TEST_ASSERT(obj->has("age")); TEST_ASSERT(obj->getInt("age") == 1); TEST_ASSERT(obj->has("name")); TEST_ASSERT(strcmp(obj->getString("name"), "John") == 0); __int32 salary = *obj->getXpath("rel1.salary"); TEST_ASSERT(salary == 150000); __int32 rent = *obj->getXpath("rel1.rent"); TEST_ASSERT(rent == 10000); TEST_ASSERT(obj->getBSON("rel1") != NULL); TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0); delete obj; }
void testParserSimple() { cout << "testParserSimple" << endl; BSONObj* testEmpty = BSONParser::parse("{}"); TEST_ASSERT(testEmpty->length() == 0); BSONObj* obj = BSONParser::parse("{age: 1, name: 'John:test\\'test2\\'', salary: 3500.25, lnumber: 100000000000}"); TEST_ASSERT(obj->has("age")); TEST_ASSERT(obj->getInt("age") == 1); TEST_ASSERT(strcmp(obj->getString("name"), "John:test\\'test2\\'") == 0); TEST_ASSERT(obj->has("salary")); TEST_ASSERT(obj->getDouble("salary") == 3500.25); TEST_ASSERT(obj->has("lnumber")); TEST_ASSERT(obj->getLong("lnumber") == 100000000000); delete obj; delete testEmpty; }
void testBSON() { cout << "testBSON" << endl; BSONObj* obj = new BSONObj(); // Add in obj->add("int", 1); obj->add("string", (const char*)"test"); obj->add("long", (__int64) 10000000000L); obj->add("double", 1.1); BSONObj rel; rel.add("innertext", (char*)"inner text"); obj->add("rel1", rel); BSONArrayObj array; BSONObj b1; b1.add("b1", "test"); array.add(b1); BSONObj b2; b2.add("b1", "test2"); array.add(b2); obj->add("array", array); TEST_ASSERT(obj->has("int")); TEST_ASSERT(obj->getInt("int") == 1); TEST_ASSERT(strcmp(obj->getString("string"), "test") == 0); TEST_ASSERT(obj->has("long")); cout << "long: " << obj->getLong("long") << endl; TEST_ASSERT(obj->getLong("long") == 10000000000L); TEST_ASSERT(obj->has("double")); TEST_ASSERT(obj->getDouble("double") == 1.1); TEST_ASSERT(obj->has("rel1")); TEST_ASSERT(strcmp(obj->getBSON("rel1")->getString("innertext"), "inner text") == 0); TEST_ASSERT(obj->has("array")); BSONArrayObj* arrayR = obj->getBSONArray("array"); TEST_ASSERT(arrayR != NULL); TEST_ASSERT(arrayR->length() == 2); BSONObj* el1 = arrayR->get(0); TEST_ASSERT(el1 != NULL); BSONObj* el2 = arrayR->get(1); TEST_ASSERT(el2 != NULL); // test a non existant attribute try { obj->getLong("xx"); TEST_FAIL("The getLong should throw an exception"); } catch (BSONException e) { } try { obj->getString("xxx"); TEST_FAIL("The getString should throw an exception"); } catch (BSONException e) { } delete obj; }