MojErr MojService::CategoryHandler::invoke(const MojChar* method, MojServiceMessage* msg, MojObject& payload) { MojAssert(method && msg); MojLogTrace(s_log); MojTime startTime; MojErr err = MojGetCurrentTime(startTime); MojErrCheck(err); // lookup callback CallbackInfo cb; if (!m_callbackMap.get(method, cb)) { MojErrThrow(MojErrMethodNotFound); } // validate schema if (cb.m_schema.get()) { MojSchema::Result res; err = cb.m_schema->validate(payload, res); MojErrCheck(err); if (!res.valid()) { MojErrThrowMsg(MojErrInvalidArg, _T("invalid parameters: caller='%s' error='%s'"), msg->senderName(), res.msg().data()); } } // invoke method err = invoke(cb.m_callback, msg, payload); MojErrCheck(err); // log timing MojTime endTime; err = MojGetCurrentTime(endTime); MojErrCheck(err); MojLogInfo(s_log, _T("%s invoked: %.3fms"), method, (double) (endTime.microsecs() - startTime.microsecs()) / 1000); return MojErrNone; }
MojErr MojDbKind::update(MojObject* newObj, const MojObject* oldObj, MojDbOp op, MojDbReq& req, bool checkSchema) { MojLogTrace(s_log); MojErr err = checkPermission(op, req); MojErrCheck(err); err = req.curKind(this); MojErrCheck(err); #if defined(TESTDBKIND) MojString s; MojErr e2; if (oldObj) { e2 = oldObj->toJson(s); MojLogInfo(s_log, _T("Kind_Update_OldObj: %s ;\n"), s.data()); } if (newObj) { e2 = newObj->toJson(s); MojLogInfo(s_log, _T("Kind_Update_NewObj: %s ;\n"), s.data()); } #endif if (newObj) { // add the _backup property if not set if (m_backup && !newObj->contains(MojDb::SyncKey)) { err = newObj->putBool(MojDb::SyncKey, true); MojErrCheck(err); } // TEMPORARY!!! This should be done in pre-update to also check parent kinds // warning message comes from preUpdate if(checkSchema) { MojSchema::Result res; err = m_schema.validate(*newObj, res); MojErrCheck(err); if (!res.valid()) { MojErrThrowMsg(MojErrSchemaValidation, _T("schema validation failed for kind '%s': %s"), m_id.data(), res.msg().data()); } } } // update revSets and validate schema err = preUpdate(newObj, oldObj, req); MojErrCheck(err); // update indexes MojVector<MojDbKind*> kindVec; MojInt32 idxcount = 0; err = updateIndexes(newObj, oldObj, req, op, kindVec, idxcount); MojLogInfo(s_log, _T("Kind_UpdateIndexes_End: %s; supers = %zu; indexcount = %zu; updated = %d \n"), this->id().data(), m_supers.size(), m_indexes.size(), idxcount); MojErrCheck(err); return MojErrNone; }
MojErr MojDbKind::preUpdate(MojObject* newObj, const MojObject* oldObj, MojDbReq& req) { // update supers for (KindVec::ConstIterator i = m_supers.begin(); i != m_supers.end(); ++i) { MojErr err = (*i)->preUpdate(newObj, oldObj, req); MojErrCheck(err); } // process new obj if (newObj) { // update revSets for (RevSetVec::ConstIterator i = m_revSets.begin(); i != m_revSets.end(); ++i) { MojErr err = (*i)->update(newObj, oldObj); MojErrCheck(err); } // validate schemas MojSchema::Result res; MojErr err = m_schema.validate(*newObj, res); MojErrCheck(err); if (!res.valid()) { //MojErrThrowMsg(MojErrSchemaValidation, _T("schema validation failed for kind '%s': %s"), m_id.data(), res.msg().data()); MojLogWarning(s_log, _T("schema validation failed for kind '%s': %s"), m_id.data(), res.msg().data()); } } return MojErrNone; }
MojErr MojSchemaTest::checkValid(const MojChar* schemaJson, const MojChar* instanceJson, bool expected) { MojObject schemaObj; MojErr err = schemaObj.fromJson(schemaJson); MojTestErrCheck(err); MojSchema schema; err = schema.fromObject(schemaObj); MojTestErrCheck(err); MojObject instance; err = instance.fromJson(instanceJson); MojTestErrCheck(err); MojSchema::Result res; err = schema.validate(instance, res); MojTestErrCheck(err); MojTestAssert(res.valid() == expected); return MojErrNone; }