std::string collection::insert(nlohmann::json::object_t const& document) throw(std::runtime_error) { bson_oid_t oid; std::shared_ptr<bson> bson_doc = convert_to_bson(document); if(document.find("_id") != document.end()) { std::string const &id = document.find("_id")->second; if(!ejdbisvalidoidstr(id.c_str())) { throw ejdb_exception(JBEINVALIDBSONPK); } bson_oid_from_string(&oid, id.c_str()); std::shared_ptr<bson> bson_oid(bson_create(), bson_del); bson_init(bson_oid.get()); bson_append_oid(bson_oid.get(), "_id", &oid); bson_finish(bson_oid.get()); std::shared_ptr<bson> bson_doc_with_oid(bson_create(), bson_del); bson_init(bson_doc_with_oid.get()); bson_merge(bson_oid.get(), bson_doc.get(), true, bson_doc_with_oid.get()); bson_finish(bson_doc_with_oid.get()); bson_doc.swap(bson_doc_with_oid); } if(!ejdbsavebson(_coll.get(), bson_doc.get(), &oid)) { throw_last_ejdb_exception(); } std::string id(24, '\0'); bson_oid_to_string(&oid, &id[0]); document_added(id, document); return id; }
void deserializeGraph(const nlohmann::json::object_t& graph, IntentStoryModel& intentStoryModel) { nlohmann::json::object_t::const_iterator sourceEdgeIt = graph.begin(); nlohmann::json::object_t::const_iterator sourceEdgeItEnd = graph.end(); for (; sourceEdgeIt != sourceEdgeItEnd; ++sourceEdgeIt) { const std::string& sourceId = sourceEdgeIt->first; const nlohmann::json::object_t& edgeTarget = sourceEdgeIt->second; nlohmann::json::object_t::const_iterator edgeTargetIt = edgeTarget.begin(); nlohmann::json::object_t::const_iterator edgeTargetItEnd = edgeTarget.end(); for (; edgeTargetIt != edgeTargetItEnd; ++edgeTargetIt) { const std::string& intentAndAction = edgeTargetIt->first; std::vector<std::string> tokens; SingleCharacterDelimiterTokenizer::tokenize(intentAndAction, ":", tokens); std::string intentName; std::string actionId; intentName = tokens[0]; if (tokens.size() == 2) actionId = tokens[1]; const std::string& targetId = edgeTargetIt->second; IntentStoryModel::EdgeInfo edgeInfo; edgeInfo.intent.intentId = intentName; edgeInfo.actionId = actionId; IntentStoryModel::StoryGraph::Vertex source; IntentStoryModel::StoryGraph::Vertex target; IntentStoryModel::VertexByStateIdIndex::const_iterator it = intentStoryModel.vertexByStateId.find(sourceId); if (it != intentStoryModel.vertexByStateId.end()) { source = it->second; } else // Adding vertex { IntentStoryModel::VertexInfo vertexInfo; vertexInfo.stateId = sourceId; source = intentStoryModel.graph.addVertex(vertexInfo); intentStoryModel.vertexByStateId[sourceId] = source; } it = intentStoryModel.vertexByStateId.find(targetId); if (it != intentStoryModel.vertexByStateId.end()) { target = it->second; } else // Adding vertex { IntentStoryModel::VertexInfo vertexInfo; vertexInfo.stateId = targetId; target = intentStoryModel.graph.addVertex(vertexInfo); intentStoryModel.vertexByStateId[targetId] = target; } intentStoryModel.graph.addEdge(source, target, edgeInfo); } } }
void deserializeReplies(const nlohmann::json::object_t& replies, ChatbotActionModel& chatbotActionModel) { nlohmann::json::object_t::const_iterator replyIt = replies.begin(); nlohmann::json::object_t::const_iterator replyItEnd = replies.end(); for (; replyIt != replyItEnd; ++replyIt) { const std::string& replyId = replyIt->first; const std::string& reply = replyIt->second; chatbotActionModel.replyContentByReplyIdIndex[replyId] = reply; } }
void deserializeReplyIdsByActionId( const nlohmann::json::object_t& repliesByAction, const std::string& state, ChatbotActionModel& chatbotActionModel) { nlohmann::json::object_t::const_iterator repliesByActionIt = repliesByAction.begin(); nlohmann::json::object_t::const_iterator repliesByActionItEnd = repliesByAction.end(); for (; repliesByActionIt != repliesByActionItEnd; ++repliesByActionIt) { const std::string& actionId = repliesByActionIt->first; const nlohmann::json::array_t& repliesList = repliesByActionIt->second; appendRepliesListByStateAndActionId(state, actionId, repliesList, chatbotActionModel); } }
void deserializeReplyIdsByStateAndActionId( const nlohmann::json::object_t& repliesByStatesAndActions, ChatbotActionModel& chatbotActionModel) { nlohmann::json::object_t::const_iterator repliesByStatesAndActionsIt = repliesByStatesAndActions.begin(); nlohmann::json::object_t::const_iterator repliesByStatesAndActionsItEnd = repliesByStatesAndActions.end(); for (; repliesByStatesAndActionsIt != repliesByStatesAndActionsItEnd; ++repliesByStatesAndActionsIt) { const std::string& state = repliesByStatesAndActionsIt->first; const nlohmann::json::object_t& repliesByActions = repliesByStatesAndActionsIt->second; deserializeReplyIdsByActionId(repliesByActions, state, chatbotActionModel); } }
void deserializeDictionary(const nlohmann::json::object_t& dictionary, DictionaryModel& dictionaryModel) { nlohmann::json::object_t::const_iterator entitiesIt = dictionary.begin(); nlohmann::json::object_t::const_iterator entitiesItEnd = dictionary.end(); unsigned int termId = 0; for (unsigned int i = 0; entitiesIt != entitiesItEnd; ++entitiesIt, i++) { dictionaryModel.entitiesByEntityId[i] = entitiesIt->first; const nlohmann::json::object_t& termsAndAlias = entitiesIt->second; nlohmann::json::object_t::const_iterator termsAndAliasIt = termsAndAlias.begin(); nlohmann::json::object_t::const_iterator termsAndAliasItEnd = termsAndAlias.end(); for (; termsAndAliasIt != termsAndAliasItEnd; ++termsAndAliasIt) { if (isTermRegex(termsAndAliasIt)) { dictionaryModel.regexesByEntityId[termsAndAliasIt->second] = i; } else { Term term; term.term = termsAndAliasIt->first; term.termId = termId; term.entityId = i; nlohmann::json::array_t alias = termsAndAliasIt->second; nlohmann::json::array_t::const_iterator aliasIt = alias.begin(); nlohmann::json::array_t::const_iterator aliasItEnd = alias.end(); for (unsigned int k = 0; aliasIt != aliasItEnd; aliasIt++, ++k) { term.alias.push_back(*aliasIt); } dictionaryModel.dictionary.pushTerm(term); } termId++; } } }
nlohmann::json collection::modified_fields(nlohmann::json::object_t const& a, nlohmann::json::object_t const& b) { nlohmann::json::object_t diff; std::set_difference(b.begin(), b.end(), a.begin(), a.end(), std::inserter(diff, diff.begin())); std::vector<std::string> cleared_fields; for(auto const& field: a) { if(b.find(field.first) == b.end()) { cleared_fields.push_back(field.first); } } return {{ "fields", diff }, { "cleared", cleared_fields }}; }