/*********************************************************************** * setPagePosition * * Move cursor position to page id provided in query. * 1. Find item "_id" which is equal to page id iteratively. * 2. If found, set begin/last position and next page. ***********************************************************************/ MojErr MojDbSearchCursor::setPagePosition() { LOG_TRACE("Entering function %s", __FUNCTION__); MojErr err; MojDbStorageItem* item; m_pos = m_items.begin(); ItemVec::ConstIterator last = m_items.end(); m_limitPos = last; // retrieve page id from query MojObject pageKey; err = m_page.toObject(pageKey); MojErrCheck(err); while (m_pos != last) { // retrieve id from query item = m_pos->get(); const MojObject id = item->id(); // If match, set begin/last position and next page if(pageKey.compare(id) == 0) { if (m_limit >= (last-m_pos)) { m_limitPos = m_items.end(); m_page.clear(); } else { m_limitPos = m_pos + m_limit; // set next page MojDbStorageItem* nextItem = m_limitPos->get(); const MojObject nextId = nextItem->id(); m_page.fromObject(nextId); // print log for nextId MojString strOut; err = nextId.toJson(strOut); MojErrCheck(err); LOG_DEBUG("[db_mojodb] nextId : %s \n", strOut.data()); } break; } ++m_pos; } return MojErrNone; }
MojErr MojObjectTest::typeTest() { MojObject obj; MojObject obj2; MojString str1; MojString str2; MojHashMap<MojObject, MojObject> map; // null MojTestAssert(obj.type() == MojObject::TypeUndefined); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); MojErr err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("null")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); MojTestAssert(obj == obj2); MojTestAssert(obj.compare(obj2) == 0); err = obj.coerce(MojObject::TypeNull); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeNull); err = obj.coerce(MojObject::TypeString); MojTestErrCheck(err); MojTestAssert(obj == str1); err = obj.coerce(MojObject::TypeBool); MojTestErrCheck(err); MojTestAssert(obj == true); err = obj.coerce(MojObject::TypeInt); MojTestErrCheck(err); MojTestAssert(obj == 1); err = obj.coerce(MojObject::TypeDecimal); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeDecimal && obj == MojDecimal(1, 0)); err = obj.coerce(MojObject::TypeObject); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeObject); err = obj.coerce(MojObject::TypeArray); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeArray); // object err = obj.put(_T("hello"), 5); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeObject); MojTestAssert(obj.size() == 1); MojTestAssert(!obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("{\"hello\":5}")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); obj.clear(MojObject::TypeObject); MojTestAssert(obj.type() == MojObject::TypeObject); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("{}")); // array for (int i = 0; i < 1000; ++i) { err = obj.push(i); MojTestErrCheck(err); } MojTestAssert(obj.type() == MojObject::TypeArray); MojTestAssert(obj.size() == 1000); MojTestAssert(!obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); for (int i = 0; i < 1000; ++i) { MojTestAssert(obj.at(i, obj2)); MojTestAssert(obj2 == i); } MojTestAssert(!obj.at(1000, obj2)); err = obj.setAt(1001, 1001); MojTestErrCheck(err); MojTestAssert(obj.size() == 1002); MojTestAssert(obj.at(1000, obj2)); MojTestAssert(obj2.type() == MojObject::TypeUndefined); obj.clear(MojObject::TypeArray); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("[]")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); // string err = str1.assign(_T("yo")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.type() == MojObject::TypeString); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str2); MojTestErrCheck(err); MojTestAssert(str1 == str2); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); obj.clear(MojObject::TypeString); MojTestAssert(obj.boolValue() == false); err = str1.assign(_T("12345")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 12345); MojTestAssert(obj.decimalValue() == MojDecimal(12345, 0)); err = str1.assign(_T("-67890")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == -67890); MojTestAssert(obj.decimalValue() == MojDecimal(-67890, 0)); err = str1.assign(_T("12345000000000")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 12345000000000LL); err = str1.assign(_T("12345.6789")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 12345); MojTestAssert(obj.decimalValue() == MojDecimal(12345, 678900)); err = str1.assign(_T("1.0e3")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 1); MojTestAssert(obj.decimalValue() == MojDecimal(1000, 0)); err = str1.assign(_T("45hello")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 45); MojTestAssert(obj.decimalValue() == MojDecimal(45, 0)); // bool obj = true; MojTestAssert(obj.type() == MojObject::TypeBool); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 1); MojTestAssert(obj.decimalValue() == MojDecimal(1, 0)); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("true")); obj.clear(MojObject::TypeBool); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("false")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); // MojDecimal obj = MojDecimal(3, 140000); MojTestAssert(obj.type() == MojObject::TypeDecimal); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 3); MojTestAssert(obj.decimalValue() == MojDecimal(3.14)); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("3.14")); obj.clear(MojObject::TypeDecimal); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("0.0")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); // MojDecimal obj = -987654321; MojTestAssert(obj.type() == MojObject::TypeInt); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == -987654321); MojTestAssert(obj.decimalValue() == MojDecimal(-987654321, 0)); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("-987654321")); obj.clear(MojObject::TypeInt); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("0")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); return MojErrNone; }