Skip to content

Commit d3e4018

Browse files
committed
implemented attributes into mongodb storage and loader, refs #2
1 parent 47aeebd commit d3e4018

File tree

2 files changed

+110
-4
lines changed

2 files changed

+110
-4
lines changed

source/annotatorlib/source/Loader/MongoDBLoader.cpp

+86-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,49 @@ void MongoDBLoader::loadAnnotations(Poco::MongoDB::Connection &connection,
8181

8282
void MongoDBLoader::loadAnnotationAttributes(
8383
Poco::MongoDB::Connection &connection,
84-
std::shared_ptr<Annotation> annotation) {}
84+
std::shared_ptr<Annotation> annotation) {
85+
Poco::MongoDB::Cursor cursor(dbname, "annotation_attributes");
86+
cursor.query().selector().add("annotation", (long)annotation->getId());
87+
88+
Poco::MongoDB::ResponseMessage &response = cursor.next(connection);
89+
while (1) {
90+
for (Poco::MongoDB::Document::Vector::const_iterator it =
91+
response.documents().begin();
92+
it != response.documents().end(); ++it) {
93+
unsigned long id = (*it)->get<long>("id");
94+
std::string name = (*it)->get<std::string>("name");
95+
std::string type = (*it)->get<std::string>("type");
96+
std::string value = (*it)->get<std::string>("value");
97+
98+
AttributeType t = AttributeTypeFromString(type);
99+
std::shared_ptr<Attribute> a = std::make_shared<Attribute>(id, t, name);
100+
std::shared_ptr<AttributeValue> av;
101+
switch (t) {
102+
case AttributeType::STRING:
103+
av = std::make_shared<AttributeValue>(value);
104+
break;
105+
case AttributeType::INTEGER:
106+
av = std::make_shared<AttributeValue>(std::stol(value));
107+
break;
108+
case AttributeType::FLOAT:
109+
av = std::make_shared<AttributeValue>(std::stod(value));
110+
break;
111+
case AttributeType::BOOLEAN:
112+
av = std::make_shared<AttributeValue>(value == "true" ||
113+
value == "True");
114+
break;
115+
default:
116+
av = std::make_shared<AttributeValue>(value);
117+
};
118+
a->setValue(av);
119+
annotation->addAttribute(a);
120+
}
121+
if (response.cursorID() == 0) {
122+
break;
123+
}
124+
response = cursor.next(connection);
125+
};
126+
}
85127

86128
void MongoDBLoader::loadClasses(Poco::MongoDB::Connection &connection,
87129
Session *session) {
@@ -105,7 +147,49 @@ void MongoDBLoader::loadClasses(Poco::MongoDB::Connection &connection,
105147
}
106148

107149
void MongoDBLoader::loadObjectAttributes(Poco::MongoDB::Connection &connection,
108-
std::shared_ptr<Object> object) {}
150+
std::shared_ptr<Object> object) {
151+
Poco::MongoDB::Cursor cursor(dbname, "object_attributes");
152+
cursor.query().selector().add("object", (long)object->getId());
153+
154+
Poco::MongoDB::ResponseMessage &response = cursor.next(connection);
155+
while (1) {
156+
for (Poco::MongoDB::Document::Vector::const_iterator it =
157+
response.documents().begin();
158+
it != response.documents().end(); ++it) {
159+
unsigned long id = (*it)->get<long>("id");
160+
std::string name = (*it)->get<std::string>("name");
161+
std::string type = (*it)->get<std::string>("type");
162+
std::string value = (*it)->get<std::string>("value");
163+
164+
AttributeType t = AttributeTypeFromString(type);
165+
std::shared_ptr<Attribute> a = std::make_shared<Attribute>(id, t, name);
166+
std::shared_ptr<AttributeValue> av;
167+
switch (t) {
168+
case AttributeType::STRING:
169+
av = std::make_shared<AttributeValue>(value);
170+
break;
171+
case AttributeType::INTEGER:
172+
av = std::make_shared<AttributeValue>(std::stol(value));
173+
break;
174+
case AttributeType::FLOAT:
175+
av = std::make_shared<AttributeValue>(std::stod(value));
176+
break;
177+
case AttributeType::BOOLEAN:
178+
av = std::make_shared<AttributeValue>(value == "true" ||
179+
value == "True");
180+
break;
181+
default:
182+
av = std::make_shared<AttributeValue>(value);
183+
};
184+
a->setValue(av);
185+
object->addAttribute(a);
186+
}
187+
if (response.cursorID() == 0) {
188+
break;
189+
}
190+
response = cursor.next(connection);
191+
};
192+
}
109193

110194
void MongoDBLoader::loadObjects(Poco::MongoDB::Connection &connection,
111195
Session *session) {

source/annotatorlib/source/Storage/MongoDBStorage.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,18 @@ void MongoDBStorage::insertOrUpdateAnnotationAttributes(
388388
(long)annotation->getId()};
389389

390390
try {
391-
// TODO insert or update
391+
Poco::MongoDB::Database db(dbname);
392+
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request =
393+
db.createUpdateRequest("annotation_attributes");
394+
request->selector().add("id", a_.id);
395+
396+
request->update()
397+
.addNewDocument("$set")
398+
.add("name", a_.name)
399+
.add("type", a_.type)
400+
.add("value", a_.value)
401+
.add("annotation", a_.annotation_id);
402+
connection->sendRequest(*request);
392403
} catch (Poco::Exception &e) {
393404
std::cout << e.what() << std::endl;
394405
std::cout << e.message() << std::endl;
@@ -413,7 +424,18 @@ void MongoDBStorage::insertOrUpdateObjectAttributes(shared_ptr<Object> object) {
413424
(long)object->getId()};
414425

415426
try {
416-
// TODO insert or update
427+
Poco::MongoDB::Database db(dbname);
428+
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request =
429+
db.createUpdateRequest("object_attributes");
430+
request->selector().add("id", a_.id);
431+
432+
request->update()
433+
.addNewDocument("$set")
434+
.add("name", a_.name)
435+
.add("type", a_.type)
436+
.add("value", a_.value)
437+
.add("object", a_.object_id);
438+
connection->sendRequest(*request);
417439
} catch (Poco::Exception &e) {
418440
std::cout << e.what() << std::endl;
419441
std::cout << e.message() << std::endl;

0 commit comments

Comments
 (0)