Example #1
0
BSONObj* DjondbConnection::findByKey(const char* db, const char* ns, const char* select, const char* id) {
	if (_logger->isDebug()) _logger->debug("executing findByKey db: %s, ns: %s, select: %s, id: %s", db, ns, select, id);

	if (!isOpen()) {
		throw DjondbException(D_ERROR_CONNECTION, "Not connected to any server");
	}
	std::string filter = format("$'_id' == '%s'", id);

	BSONArrayObj* result = find(const_cast<char*>(db), const_cast<char*>(ns), const_cast<char*>(select), const_cast<char*>(filter.c_str()));

	BSONObj* res = NULL;
	if (result->length() == 1) {
		if (_logger->isDebug()) _logger->debug(2, "findByKey found 1 result");
		res = *result->begin();
	} else {
		if (result->length() > 1) {
			throw DjondbException(D_ERROR_TOO_MANY_RESULTS, "The result contains more than 1 result");
		}
	}
   BSONObj* bsonresult = NULL;
	if (res != NULL) {
		// creates a copy of the result before deleting the temporal objects
		bsonresult = new BSONObj(*res);
	}

	delete result;
	return bsonresult;
}
Example #2
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;
		}
Example #3
0
		void testFind(int port, int top = 10000000) {
			DjondbConnection* conn = DjondbConnectionManager::getConnection("localhost", port);

			if (!conn->open()) {
				cout << "Not connected" << endl;
				exit(0);
			}

			Logger* log = getLogger(NULL);

			log->startTimeRecord();
			log->info("Testing find performance over: %d finds.", top);
			for (int x = 0; x < top; x++) {
				BSONArrayObj* arr = conn->executeQuery("select top 1 * from db:testperformance");
				if (arr->length() == 0) {
					log->info("Error an id was not found");
					exit(1);
				}
				delete arr;
			}
			log->stopTimeRecord();

			DTime time = log->recordedTime();

			cout << "Total find secs: " << time.totalSecs() << endl;
			if (time.totalSecs() > 0) {
				log->info("Found %d: throughtput: %d.", top, (top / time.totalSecs()));
			} else {
				log->info("Found :%d, throughtput too high to be measured", top);
			}
			if ((time.totalSecs() > 0) && ((top / time.totalSecs()) < 16000))  {
				log->info("Performance is not good enough");
			}
		}
Example #4
0
		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;
		}
Example #5
0
void testTransactionMergedData()
{
	printf("%s\n", "testTransactionMergedData");
	BaseTransaction* tx = new BaseTransaction(_controller);
	tx->dropNamespace("db", "testcommit");

	BSONObj ooutTX;
	std::string* idOut = uuid();
	ooutTX.add("_id", const_cast<char*>(idOut->c_str()));
	ooutTX.add("name", "JohnOut");
	tx->insert("db", "testcommit", &ooutTX);

	// Insert out of the transaction
	std::string* tuid = uuid();
	StdTransaction* stx = new StdTransaction(tx, *tuid);

	BSONObj o;
	std::string* id = uuid();
	o.add("_id", const_cast<char*>(id->c_str()));
	o.add("name", "John");
	stx->insert("db", "testcommit", &o);

	BSONArrayObj* res = stx->find("db", "testcommit", "*", "");
	TEST_ASSERT(res->length() == 2);

	BSONArrayObj* resOut = tx->find("db", "testcommit", "*", "");
	TEST_ASSERT(resOut->length() == 1);

	stx->commit();
	delete stx;

	BSONArrayObj* resOut2 = tx->find("db", "testcommit", "*", "");
	TEST_ASSERT(resOut2->length() == 2);

	delete tx;
	delete res;
	delete resOut;
	delete resOut2;
	delete tuid;
	printf("%s\n", "~testTransactionMergedData");
}
Example #6
0
void testTransaction()
{
	printf("%s\n", "testTransaction");
	DummyController* controller = new DummyController();

	BaseTransaction* tx = new BaseTransaction(controller);

	tx->dropNamespace("db", "txns");

	BSONObj o;
	std::string* id = uuid();
	o.add("_id", const_cast<char*>(id->c_str()));
	o.add("name", "John");
	tx->insert("db", "txns", &o);

	BSONArrayObj* res = tx->find("db", "txns", "*", "");

	TEST_ASSERT(res->length() == 1);
	BSONObj* test1 = *res->begin();
	TEST_ASSERT(test1->getString("name").compare("John") == 0);
	test1->add("name", "Peter");
	tx->update("db", "txns", test1);

	delete res;

	res = tx->find("db", "txns", "*", "");

	TEST_ASSERT(res->length() == 1);
	BSONObj* test2 = *res->begin();
	TEST_ASSERT(test2->getString("name").compare("Peter") == 0);

	delete res;
	printf("%s\n", "Deleting tx");
	delete tx;
	delete controller;
	delete id;
	printf("%s\n", "~testTransaction");
}
Example #7
0
		void testXPath() {
			cout << "testXPath" << endl;

			BSONObj* obj1 = BSONParser::parse("{ name: 'John', age: 35, one: { data: 1 }, children: [ { name: 'Joshua', age: 15}, { name: 'Mary', age: 30}] }");

			__int32 age = *obj1->getXpath("age");

			TEST_ASSERT(age == 35);

			BSONArrayObj* children = *obj1->getXpath("children");
			TEST_ASSERT(children->length() == 2);

			__int32 data = *obj1->getXpath("one.data");
			TEST_ASSERT(data == 1);

			delete obj1;
		}
Example #8
0
		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;
		}