Skip to content

Commit 791bcdc

Browse files
committed
added mongodb storage test
1 parent efac54d commit 791bcdc

File tree

4 files changed

+139
-19
lines changed

4 files changed

+139
-19
lines changed

source/annotatorlib/include/AnnotatorLib/Storage/MongoDBStorage.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ class ANNOTATORLIB_API MongoDBStorage : public AbstractStorage {
111111
bool _open = false;
112112
bool _save = true;
113113

114-
Poco::MongoDB::Database getDatabase();
115-
116114
void insertOrUpdateAnnotationAttributes(shared_ptr<Annotation> annotation);
117115
void insertOrUpdateObjectAttributes(shared_ptr<Object> object);
118116
};

source/annotatorlib/source/Storage/MongoDBStorage.cpp

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright 2016-2017 Annotator Team
2-
#define Annotator_AnnotatorLib_Storage_MySQLStorage_BODY
2+
#define Annotator_AnnotatorLib_Storage_MongoDBStorage_BODY
33

44
/************************************************************
55
MongoDBStorage class body
@@ -68,8 +68,9 @@ bool MongoDBStorage::addAnnotation(shared_ptr<Annotation> annotation,
6868
a_.frame = std::to_string(annotation->getFrame()->getId());
6969

7070
try {
71+
Poco::MongoDB::Database db(dbname);
7172
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest =
72-
getDatabase().createInsertRequest("annotations");
73+
db.createInsertRequest("annotations");
7374
insertRequest->addNewDocument()
7475
.add("id", a_.id)
7576
.add("next", a_.next)
@@ -82,11 +83,13 @@ bool MongoDBStorage::addAnnotation(shared_ptr<Annotation> annotation,
8283
.add("height", a_.height)
8384
.add("type", a_.type);
8485

86+
std::cout << insertRequest->documents().size() << std::endl;
87+
8588
connection->sendRequest(*insertRequest);
8689

87-
std::string lastError = getDatabase().getLastError(*connection);
90+
std::string lastError = db.getLastError(*connection);
8891
if (!lastError.empty()) {
89-
std::cout << "Last Error: " << getDatabase().getLastError(*connection)
92+
std::cout << "Last Error: " << db.getLastError(*connection)
9093
<< std::endl;
9194
}
9295
insertOrUpdateAnnotationAttributes(annotation);
@@ -102,11 +105,17 @@ shared_ptr<Annotation> MongoDBStorage::removeAnnotation(unsigned long id,
102105
bool unregister) {
103106
if (_open) {
104107
try {
108+
Poco::MongoDB::Database db(dbname);
105109
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request =
106-
getDatabase().createDeleteRequest("annotations");
110+
db.createDeleteRequest("annotations");
107111
request->selector().add("id", std::to_string(id));
108112

109113
connection->sendRequest(*request);
114+
std::string lastError = db.getLastError(*connection);
115+
if (!lastError.empty()) {
116+
std::cout << "Last Error: " << db.getLastError(*connection)
117+
<< std::endl;
118+
}
110119

111120
} catch (Poco::Exception &e) {
112121
std::cout << e.what() << std::endl;
@@ -150,8 +159,9 @@ void MongoDBStorage::updateAnnotation(shared_ptr<Annotation> annotation) {
150159
a_.frame = std::to_string(annotation->getFrame()->getId());
151160

152161
try {
162+
Poco::MongoDB::Database db(dbname);
153163
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request =
154-
getDatabase().createUpdateRequest("annotations");
164+
db.createUpdateRequest("annotations");
155165
request->selector().add("id", a_.id);
156166

157167
request->update()
@@ -167,6 +177,11 @@ void MongoDBStorage::updateAnnotation(shared_ptr<Annotation> annotation) {
167177
.add("type", a_.type);
168178

169179
connection->sendRequest(*request);
180+
std::string lastError = db.getLastError(*connection);
181+
if (!lastError.empty()) {
182+
std::cout << "Last Error: " << db.getLastError(*connection)
183+
<< std::endl;
184+
}
170185

171186
insertOrUpdateAnnotationAttributes(annotation);
172187
} catch (Poco::Exception &e) {
@@ -186,11 +201,17 @@ bool MongoDBStorage::addClass(shared_ptr<Class> c) {
186201
ClassStruct c_ = {std::to_string(c->getId()), c->getName()};
187202

188203
try {
204+
Poco::MongoDB::Database db(dbname);
189205
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest =
190-
getDatabase().createInsertRequest("classes");
206+
db.createInsertRequest("classes");
191207
insertRequest->addNewDocument().add("id", c_.id).add("name", c_.name);
192208

193209
connection->sendRequest(*insertRequest);
210+
std::string lastError = db.getLastError(*connection);
211+
if (!lastError.empty()) {
212+
std::cout << "Last Error: " << db.getLastError(*connection)
213+
<< std::endl;
214+
}
194215
} catch (Poco::Exception &e) {
195216
std::cout << e.what() << std::endl;
196217
std::cout << e.message() << std::endl;
@@ -202,8 +223,9 @@ bool MongoDBStorage::addClass(shared_ptr<Class> c) {
202223
shared_ptr<Class> MongoDBStorage::removeClass(Class *c) {
203224
if (_open) {
204225
try {
226+
Poco::MongoDB::Database db(dbname);
205227
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request =
206-
getDatabase().createDeleteRequest("classes");
228+
db.createDeleteRequest("classes");
207229
request->selector().add("id", std::to_string(c->getId()));
208230
connection->sendRequest(*request);
209231
} catch (Poco::Exception &e) {
@@ -225,13 +247,19 @@ void MongoDBStorage::updateClass(shared_ptr<Class> theClass) {
225247
ClassStruct c_ = {std::to_string(theClass->getId()), theClass->getName()};
226248

227249
try {
250+
Poco::MongoDB::Database db(dbname);
228251
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request =
229-
getDatabase().createUpdateRequest("classes");
252+
db.createUpdateRequest("classes");
230253
request->selector().add("id", c_.id);
231254

232255
request->update().addNewDocument("$set").add("name", c_.name);
233256

234257
connection->sendRequest(*request);
258+
std::string lastError = db.getLastError(*connection);
259+
if (!lastError.empty()) {
260+
std::cout << "Last Error: " << db.getLastError(*connection)
261+
<< std::endl;
262+
}
235263
} catch (Poco::Exception &e) {
236264
std::cout << e.what() << std::endl;
237265
std::cout << e.message() << std::endl;
@@ -252,14 +280,20 @@ bool MongoDBStorage::addObject(shared_ptr<AnnotatorLib::Object> object,
252280
std::to_string(object->getClass()->getId())};
253281

254282
try {
283+
Poco::MongoDB::Database db(dbname);
255284
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest =
256-
getDatabase().createInsertRequest("objects");
285+
db.createInsertRequest("objects");
257286
insertRequest->addNewDocument()
258287
.add("id", o_.id)
259288
.add("name", o_.name)
260289
.add("class", o_._class);
261290

262291
connection->sendRequest(*insertRequest);
292+
std::string lastError = db.getLastError(*connection);
293+
if (!lastError.empty()) {
294+
std::cout << "Last Error: " << db.getLastError(*connection)
295+
<< std::endl;
296+
}
263297
insertOrUpdateObjectAttributes(object);
264298
} catch (Poco::Exception &e) {
265299
std::cout << e.what() << std::endl;
@@ -273,8 +307,9 @@ shared_ptr<Object> MongoDBStorage::removeObject(unsigned long id,
273307
bool remove_annotations) {
274308
if (_open) {
275309
try {
310+
Poco::MongoDB::Database db(dbname);
276311
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request =
277-
getDatabase().createDeleteRequest("objects");
312+
db.createDeleteRequest("objects");
278313
request->selector().add("id", std::to_string(id));
279314
connection->sendRequest(*request);
280315
} catch (Poco::Exception &e) {
@@ -297,8 +332,9 @@ void MongoDBStorage::updateObject(shared_ptr<Object> object) {
297332
std::to_string(object->getClass()->getId())};
298333

299334
try {
335+
Poco::MongoDB::Database db(dbname);
300336
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request =
301-
getDatabase().createUpdateRequest("objects");
337+
db.createUpdateRequest("objects");
302338
request->selector().add("id", o_.id);
303339

304340
request->update()
@@ -322,8 +358,9 @@ MongoDBStorage::~MongoDBStorage() {
322358

323359
bool MongoDBStorage::open() {
324360
Poco::URI uri(path);
325-
connection = new Poco::MongoDB::Connection(uri.getHost(), uri.getPort());
361+
connection = new Poco::MongoDB::Connection(uri.getHost(), (uri.getPort()==0) ? 27017 : uri.getPort());
326362
dbname = uri.getPath();
363+
dbname.erase(0,1);
327364

328365
this->_open = true;
329366
return _open;
@@ -391,10 +428,6 @@ void MongoDBStorage::insertOrUpdateObjectAttributes(shared_ptr<Object> object) {
391428
}
392429
}
393430

394-
Poco::MongoDB::Database MongoDBStorage::getDatabase() {
395-
return Poco::MongoDB::Database(dbname);
396-
}
397-
398431
// static attributes (if any)
399432

400433
} // of namespace Storage

source/tests/annotatorlib-test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(sources
2626
saver_test.cpp
2727
session_test.cpp
2828
storage_json_test.cpp
29+
storage_mongodb_test.cpp
2930
storage_mysql_test.cpp
3031
storage_sqlite_test.cpp
3132
storage_xml_test.cpp
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2017 Annotator Team
2+
#include <AnnotatorLib/Annotation.h>
3+
#include <AnnotatorLib/Session.h>
4+
#include <AnnotatorLib/Storage/MongoDBStorage.h>
5+
#include <gmock/gmock.h>
6+
#include <memory>
7+
#include <string>
8+
9+
using namespace AnnotatorLib;
10+
using std::shared_ptr;
11+
12+
class storage_mongodb_test : public testing::Test {
13+
public:
14+
};
15+
16+
TEST_F(storage_mongodb_test, createTables) {
17+
AnnotatorLib::Storage::MongoDBStorage storage;
18+
storage.setPath(
19+
"mongodb://localhost:27017/annotatortest");
20+
ASSERT_TRUE(storage.open());
21+
}
22+
23+
TEST_F(storage_mongodb_test, objects) {
24+
AnnotatorLib::Storage::MongoDBStorage storage;
25+
storage.setPath(
26+
"mongodb://localhost:27017/annotatortest");
27+
ASSERT_TRUE(storage.open());
28+
auto c = std::make_shared<Class>("testclass");
29+
30+
storage.addClass(c);
31+
storage.removeClass(c.get());
32+
33+
shared_ptr<Object> o = std::make_shared<Object>();
34+
o->setClass(c);
35+
o->setName("testobject");
36+
storage.addObject(o);
37+
38+
shared_ptr<Object> o2 = storage.getObject(o->getId());
39+
ASSERT_TRUE(o == o2);
40+
ASSERT_TRUE(o2->getName() == "testobject");
41+
/*
42+
AnnotatorLib::Storage::MongoDBStorage storage2;
43+
storage2.setPath(
44+
"mongodb://localhost/annotatortest");
45+
ASSERT_TRUE(storage2.open());
46+
shared_ptr<Object> o3 = storage2.getObject(o->getId());
47+
ASSERT_TRUE(o3->getName() == "testobject");
48+
*/
49+
}
50+
51+
TEST_F(storage_mongodb_test, annotations) {
52+
AnnotatorLib::Storage::MongoDBStorage storage;
53+
storage.setPath(
54+
"mongodb://localhost/annotatortest");
55+
ASSERT_TRUE(storage.open());
56+
auto c = std::make_shared<Class>("testclass");
57+
58+
storage.addClass(c);
59+
storage.removeClass(c.get());
60+
61+
shared_ptr<Object> o = std::make_shared<Object>();
62+
o->setClass(c);
63+
o->setName("testobject");
64+
storage.addObject(o);
65+
66+
shared_ptr<Object> o2 = storage.getObject(o->getId());
67+
ASSERT_TRUE(o == o2);
68+
ASSERT_TRUE(o2->getName() == "testobject");
69+
70+
shared_ptr<Frame> f = std::make_shared<Frame>(1);
71+
shared_ptr<Annotation> a =
72+
Annotation::make_shared(f, o, AnnotationType::RECTANGLE);
73+
a->setPosition(0, 0, 1, 1);
74+
ASSERT_TRUE(storage.addAnnotation(a, true));
75+
/*
76+
AnnotatorLib::Storage::MongoDBStorage storage2;
77+
storage2.setPath(
78+
"mongodb://localhost/annotatortest");
79+
ASSERT_TRUE(storage2.open());
80+
shared_ptr<Object> o3 = storage2.getObject(o->getId());
81+
ASSERT_TRUE(o3->getName() == "testobject");
82+
83+
shared_ptr<Annotation> a2 = storage2.getAnnotation(a->getId());
84+
ASSERT_TRUE(storage2.getAnnotations().size() > 0);
85+
ASSERT_TRUE(a2->getObject()->getName() == "testobject");
86+
ASSERT_TRUE(storage2.getFrames().size() == 1);
87+
*/
88+
}

0 commit comments

Comments
 (0)