1
1
// Copyright 2016-2017 Annotator Team
2
- #define Annotator_AnnotatorLib_Storage_MySQLStorage_BODY
2
+ #define Annotator_AnnotatorLib_Storage_MongoDBStorage_BODY
3
3
4
4
/* ***********************************************************
5
5
MongoDBStorage class body
@@ -68,8 +68,9 @@ bool MongoDBStorage::addAnnotation(shared_ptr<Annotation> annotation,
68
68
a_.frame = std::to_string (annotation->getFrame ()->getId ());
69
69
70
70
try {
71
+ Poco::MongoDB::Database db (dbname);
71
72
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest =
72
- getDatabase () .createInsertRequest (" annotations" );
73
+ db .createInsertRequest (" annotations" );
73
74
insertRequest->addNewDocument ()
74
75
.add (" id" , a_.id )
75
76
.add (" next" , a_.next )
@@ -82,11 +83,13 @@ bool MongoDBStorage::addAnnotation(shared_ptr<Annotation> annotation,
82
83
.add (" height" , a_.height )
83
84
.add (" type" , a_.type );
84
85
86
+ std::cout << insertRequest->documents ().size () << std::endl;
87
+
85
88
connection->sendRequest (*insertRequest);
86
89
87
- std::string lastError = getDatabase () .getLastError (*connection);
90
+ std::string lastError = db .getLastError (*connection);
88
91
if (!lastError.empty ()) {
89
- std::cout << " Last Error: " << getDatabase () .getLastError (*connection)
92
+ std::cout << " Last Error: " << db .getLastError (*connection)
90
93
<< std::endl;
91
94
}
92
95
insertOrUpdateAnnotationAttributes (annotation);
@@ -102,11 +105,17 @@ shared_ptr<Annotation> MongoDBStorage::removeAnnotation(unsigned long id,
102
105
bool unregister) {
103
106
if (_open) {
104
107
try {
108
+ Poco::MongoDB::Database db (dbname);
105
109
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request =
106
- getDatabase () .createDeleteRequest (" annotations" );
110
+ db .createDeleteRequest (" annotations" );
107
111
request->selector ().add (" id" , std::to_string (id));
108
112
109
113
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
+ }
110
119
111
120
} catch (Poco::Exception &e) {
112
121
std::cout << e.what () << std::endl;
@@ -150,8 +159,9 @@ void MongoDBStorage::updateAnnotation(shared_ptr<Annotation> annotation) {
150
159
a_.frame = std::to_string (annotation->getFrame ()->getId ());
151
160
152
161
try {
162
+ Poco::MongoDB::Database db (dbname);
153
163
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request =
154
- getDatabase () .createUpdateRequest (" annotations" );
164
+ db .createUpdateRequest (" annotations" );
155
165
request->selector ().add (" id" , a_.id );
156
166
157
167
request->update ()
@@ -167,6 +177,11 @@ void MongoDBStorage::updateAnnotation(shared_ptr<Annotation> annotation) {
167
177
.add (" type" , a_.type );
168
178
169
179
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
+ }
170
185
171
186
insertOrUpdateAnnotationAttributes (annotation);
172
187
} catch (Poco::Exception &e) {
@@ -186,11 +201,17 @@ bool MongoDBStorage::addClass(shared_ptr<Class> c) {
186
201
ClassStruct c_ = {std::to_string (c->getId ()), c->getName ()};
187
202
188
203
try {
204
+ Poco::MongoDB::Database db (dbname);
189
205
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest =
190
- getDatabase () .createInsertRequest (" classes" );
206
+ db .createInsertRequest (" classes" );
191
207
insertRequest->addNewDocument ().add (" id" , c_.id ).add (" name" , c_.name );
192
208
193
209
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
+ }
194
215
} catch (Poco::Exception &e) {
195
216
std::cout << e.what () << std::endl;
196
217
std::cout << e.message () << std::endl;
@@ -202,8 +223,9 @@ bool MongoDBStorage::addClass(shared_ptr<Class> c) {
202
223
shared_ptr<Class> MongoDBStorage::removeClass (Class *c) {
203
224
if (_open) {
204
225
try {
226
+ Poco::MongoDB::Database db (dbname);
205
227
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request =
206
- getDatabase () .createDeleteRequest (" classes" );
228
+ db .createDeleteRequest (" classes" );
207
229
request->selector ().add (" id" , std::to_string (c->getId ()));
208
230
connection->sendRequest (*request);
209
231
} catch (Poco::Exception &e) {
@@ -225,13 +247,19 @@ void MongoDBStorage::updateClass(shared_ptr<Class> theClass) {
225
247
ClassStruct c_ = {std::to_string (theClass->getId ()), theClass->getName ()};
226
248
227
249
try {
250
+ Poco::MongoDB::Database db (dbname);
228
251
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request =
229
- getDatabase () .createUpdateRequest (" classes" );
252
+ db .createUpdateRequest (" classes" );
230
253
request->selector ().add (" id" , c_.id );
231
254
232
255
request->update ().addNewDocument (" $set" ).add (" name" , c_.name );
233
256
234
257
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
+ }
235
263
} catch (Poco::Exception &e) {
236
264
std::cout << e.what () << std::endl;
237
265
std::cout << e.message () << std::endl;
@@ -252,14 +280,20 @@ bool MongoDBStorage::addObject(shared_ptr<AnnotatorLib::Object> object,
252
280
std::to_string (object->getClass ()->getId ())};
253
281
254
282
try {
283
+ Poco::MongoDB::Database db (dbname);
255
284
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest =
256
- getDatabase () .createInsertRequest (" objects" );
285
+ db .createInsertRequest (" objects" );
257
286
insertRequest->addNewDocument ()
258
287
.add (" id" , o_.id )
259
288
.add (" name" , o_.name )
260
289
.add (" class" , o_._class );
261
290
262
291
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
+ }
263
297
insertOrUpdateObjectAttributes (object);
264
298
} catch (Poco::Exception &e) {
265
299
std::cout << e.what () << std::endl;
@@ -273,8 +307,9 @@ shared_ptr<Object> MongoDBStorage::removeObject(unsigned long id,
273
307
bool remove_annotations) {
274
308
if (_open) {
275
309
try {
310
+ Poco::MongoDB::Database db (dbname);
276
311
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request =
277
- getDatabase () .createDeleteRequest (" objects" );
312
+ db .createDeleteRequest (" objects" );
278
313
request->selector ().add (" id" , std::to_string (id));
279
314
connection->sendRequest (*request);
280
315
} catch (Poco::Exception &e) {
@@ -297,8 +332,9 @@ void MongoDBStorage::updateObject(shared_ptr<Object> object) {
297
332
std::to_string (object->getClass ()->getId ())};
298
333
299
334
try {
335
+ Poco::MongoDB::Database db (dbname);
300
336
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> request =
301
- getDatabase () .createUpdateRequest (" objects" );
337
+ db .createUpdateRequest (" objects" );
302
338
request->selector ().add (" id" , o_.id );
303
339
304
340
request->update ()
@@ -322,8 +358,9 @@ MongoDBStorage::~MongoDBStorage() {
322
358
323
359
bool MongoDBStorage::open () {
324
360
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 ());
326
362
dbname = uri.getPath ();
363
+ dbname.erase (0 ,1 );
327
364
328
365
this ->_open = true ;
329
366
return _open;
@@ -391,10 +428,6 @@ void MongoDBStorage::insertOrUpdateObjectAttributes(shared_ptr<Object> object) {
391
428
}
392
429
}
393
430
394
- Poco::MongoDB::Database MongoDBStorage::getDatabase () {
395
- return Poco::MongoDB::Database (dbname);
396
- }
397
-
398
431
// static attributes (if any)
399
432
400
433
} // of namespace Storage
0 commit comments