|
| 1 | +// Copyright 2016-2017 Annotator Team |
| 2 | +#define Annotator_AnnotatorLib_Loader_MongoDBLoader_BODY |
| 3 | + |
| 4 | +/************************************************************ |
| 5 | + MongoDBLoader class body |
| 6 | + ************************************************************/ |
| 7 | + |
| 8 | +// include associated header file |
| 9 | +#include <AnnotatorLib/Loader/MongoDBLoader.h> |
| 10 | +#include <AnnotatorLib/Object.h> |
| 11 | +#include <AnnotatorLib/Session.h> |
| 12 | + |
| 13 | +#include <Poco/MongoDB/Array.h> |
| 14 | +#include <Poco/MongoDB/Cursor.h> |
| 15 | +#include <Poco/MongoDB/Database.h> |
| 16 | +#include <Poco/URI.h> |
| 17 | + |
| 18 | +using std::shared_ptr; |
| 19 | + |
| 20 | +namespace AnnotatorLib { |
| 21 | +namespace Loader { |
| 22 | + |
| 23 | +void MongoDBLoader::setPath(std::string path) { this->path = path; } |
| 24 | + |
| 25 | +StorageType MongoDBLoader::getType() { |
| 26 | + return AnnotatorLib::StorageType::MONGODB; |
| 27 | +} |
| 28 | + |
| 29 | +void MongoDBLoader::loadSession(Session *session) { |
| 30 | + Poco::URI uri(path); |
| 31 | + Poco::MongoDB::Connection connection( |
| 32 | + uri.getHost(), (uri.getPort() == 0) ? 27017 : uri.getPort()); |
| 33 | + dbname = uri.getPath(); |
| 34 | + dbname.erase(0, 1); |
| 35 | + |
| 36 | + loadAttributes(connection, session); |
| 37 | + loadClasses(connection, session); |
| 38 | + loadObjects(connection, session); |
| 39 | + loadFrames(connection, session); |
| 40 | + loadAnnotations(connection, session); |
| 41 | +} |
| 42 | + |
| 43 | +void MongoDBLoader::loadAttributes(Poco::MongoDB::Connection &connection, |
| 44 | + Session *session) {} |
| 45 | + |
| 46 | +void MongoDBLoader::loadAnnotations(Poco::MongoDB::Connection &connection, |
| 47 | + Session *session) { |
| 48 | + Poco::MongoDB::Cursor cursor(dbname, "annotations"); |
| 49 | + |
| 50 | + Poco::MongoDB::ResponseMessage &response = cursor.next(connection); |
| 51 | + while (1) { |
| 52 | + for (Poco::MongoDB::Document::Vector::const_iterator it = |
| 53 | + response.documents().begin(); |
| 54 | + it != response.documents().end(); ++it) { |
| 55 | + unsigned long object_id = (*it)->get<long>("object"); |
| 56 | + unsigned long frame_id = (*it)->get<long>("frame"); |
| 57 | + |
| 58 | + float x = (*it)->get<double>("x"); |
| 59 | + float y = (*it)->get<double>("y"); |
| 60 | + float width = (*it)->get<double>("width"); |
| 61 | + float height = (*it)->get<double>("height"); |
| 62 | + AnnotationType type = |
| 63 | + AnnotationTypeFromString((*it)->get<std::string>("type")); |
| 64 | + |
| 65 | + shared_ptr<Object> o = session->getObject(object_id); |
| 66 | + shared_ptr<Frame> f = session->getFrame(frame_id); |
| 67 | + |
| 68 | + if (o && f) { |
| 69 | + shared_ptr<Annotation> a = Annotation::make_shared(f, o, type); |
| 70 | + a->setPosition(x, y, width, height); |
| 71 | + session->addAnnotation(a); |
| 72 | + loadAnnotationAttributes(connection, a); |
| 73 | + } |
| 74 | + } |
| 75 | + if (response.cursorID() == 0) { |
| 76 | + break; |
| 77 | + } |
| 78 | + response = cursor.next(connection); |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +void MongoDBLoader::loadAnnotationAttributes( |
| 83 | + Poco::MongoDB::Connection &connection, |
| 84 | + std::shared_ptr<Annotation> annotation) {} |
| 85 | + |
| 86 | +void MongoDBLoader::loadClasses(Poco::MongoDB::Connection &connection, |
| 87 | + Session *session) { |
| 88 | + Poco::MongoDB::Cursor cursor(dbname, "classes"); |
| 89 | + |
| 90 | + Poco::MongoDB::ResponseMessage &response = cursor.next(connection); |
| 91 | + while (1) { |
| 92 | + for (Poco::MongoDB::Document::Vector::const_iterator it = |
| 93 | + response.documents().begin(); |
| 94 | + it != response.documents().end(); ++it) { |
| 95 | + unsigned long class_id = (*it)->get<long>("id"); |
| 96 | + AnnotatorLib::Class *c_ = |
| 97 | + new AnnotatorLib::Class(class_id, (*it)->get<std::string>("name")); |
| 98 | + session->addClass(shared_ptr<AnnotatorLib::Class>(c_)); |
| 99 | + } |
| 100 | + if (response.cursorID() == 0) { |
| 101 | + break; |
| 102 | + } |
| 103 | + response = cursor.next(connection); |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +void MongoDBLoader::loadObjectAttributes(Poco::MongoDB::Connection &connection, |
| 108 | + std::shared_ptr<Object> object) {} |
| 109 | + |
| 110 | +void MongoDBLoader::loadObjects(Poco::MongoDB::Connection &connection, |
| 111 | + Session *session) { |
| 112 | + Poco::MongoDB::Cursor cursor(dbname, "objects"); |
| 113 | + |
| 114 | + Poco::MongoDB::ResponseMessage &response = cursor.next(connection); |
| 115 | + while (1) { |
| 116 | + for (Poco::MongoDB::Document::Vector::const_iterator it = |
| 117 | + response.documents().begin(); |
| 118 | + it != response.documents().end(); ++it) { |
| 119 | + unsigned long object_id = (*it)->get<long>("id"); |
| 120 | + std::string object_name = (*it)->get<std::string>("name"); |
| 121 | + unsigned long class_id = (*it)->get<long>("class"); |
| 122 | + |
| 123 | + std::shared_ptr<Object> object = std::make_shared<Object>(object_id); |
| 124 | + object->setName(object_name); |
| 125 | + |
| 126 | + object->setClass(session->getClass(class_id)); |
| 127 | + session->addObject(object); |
| 128 | + loadObjectAttributes(connection, object); |
| 129 | + } |
| 130 | + if (response.cursorID() == 0) { |
| 131 | + break; |
| 132 | + } |
| 133 | + response = cursor.next(connection); |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +void MongoDBLoader::loadFrames(Poco::MongoDB::Connection &connection, |
| 138 | + Session *session) { |
| 139 | + Poco::MongoDB::Database db(dbname); |
| 140 | + Poco::SharedPtr<Poco::MongoDB::QueryRequest> command = db.createCommand(); |
| 141 | + |
| 142 | + command->selector().add("distinct", "annotations").add("key", "frame"); |
| 143 | + |
| 144 | + Poco::MongoDB::ResponseMessage response; |
| 145 | + connection.sendRequest(*command, response); |
| 146 | + if (response.hasDocuments()) { |
| 147 | + Poco::MongoDB::Array::Ptr values = |
| 148 | + response.documents()[0]->get<Poco::MongoDB::Array::Ptr>("values"); |
| 149 | + for (unsigned int i = 0; i < values->size(); ++i) { |
| 150 | + unsigned long nmb = values->get<long>(i); |
| 151 | + session->addFrame(std::make_shared<Frame>(nmb)); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +} // of namespace Loader |
| 157 | +} // of namespace AnnotatorLib |
| 158 | + |
| 159 | +/************************************************************ |
| 160 | + End of MongoDBLoader class body |
| 161 | + ************************************************************/ |
0 commit comments