|
@@ -2,16 +2,15 @@
|
2
|
2
|
|
3
|
3
|
import re
|
4
|
4
|
import warnings
|
5
|
|
-import bson
|
6
|
5
|
from bson.son import SON
|
7
|
6
|
from collections import OrderedDict
|
8
|
7
|
import pymongo
|
9
|
8
|
from pymongo.errors import BulkWriteError
|
10
|
|
-import urllib
|
11
|
9
|
|
12
|
10
|
from lodel import logger
|
13
|
11
|
|
14
|
|
-from .utils import object_collection_name, connect, \
|
|
12
|
+from . import utils
|
|
13
|
+from .utils import object_collection_name,\
|
15
|
14
|
MONGODB_SORT_OPERATORS_MAP, connection_string
|
16
|
15
|
|
17
|
16
|
class MongoDbDataSourceError(Exception):
|
|
@@ -51,8 +50,9 @@ class MongoDbDatasource(object):
|
51
|
50
|
self.__read_only = bool(read_only)
|
52
|
51
|
##@brief Uniq ID for mongodb connection
|
53
|
52
|
self.__conn_hash= None
|
54
|
|
- ##@brief Stores the connection to MongoDB
|
55
|
|
- self.database = self.__connect(username, password)
|
|
53
|
+ ##@brief Stores the database cursor
|
|
54
|
+ self.database = self.__connect(
|
|
55
|
+ username, password, ro = self.__read_only)
|
56
|
56
|
|
57
|
57
|
##@brief Destructor that attempt to close connection to DB
|
58
|
58
|
#
|
|
@@ -64,6 +64,7 @@ class MongoDbDatasource(object):
|
64
|
64
|
if self._connections[self.__conn_hash]['conn_count'] <= 0:
|
65
|
65
|
self._connections[self.__conn_hash]['db'].close()
|
66
|
66
|
del(self._connections[self.__conn_hash])
|
|
67
|
+ logger.info("Closing connection to database")
|
67
|
68
|
|
68
|
69
|
##@brief returns a selection of documents from the datasource
|
69
|
70
|
#@param target Emclass
|
|
@@ -176,12 +177,26 @@ class MongoDbDatasource(object):
|
176
|
177
|
#@param ro bool : If True the Datasource is for read only, else the
|
177
|
178
|
def __connect(self, username, password, ro):
|
178
|
179
|
conn_string = connection_string(
|
179
|
|
- username = username, password = password, **self.__db_infos)
|
180
|
|
- conn_string += "__ReadOnly__:"+self.__read_only
|
181
|
|
- self.__conf_hash = conn_h = hash(conn_string)
|
|
180
|
+ username = username, password = password,
|
|
181
|
+ host = self.__db_infos['host'],
|
|
182
|
+ port = self.__db_infos['port'])
|
|
183
|
+
|
|
184
|
+ conn_string += "__ReadOnly__:"+str(self.__read_only)
|
|
185
|
+ self.__conn_hash = conn_h = hash(conn_string)
|
182
|
186
|
if conn_h in self._connections:
|
183
|
187
|
self._connections[conn_h]['conn_count'] += 1
|
184
|
|
- return self._connections[conn_h]['db']
|
|
188
|
+ return self._connections[conn_h]['db'][self.__db_infos['db_name']]
|
|
189
|
+ else:
|
|
190
|
+ logger.info("Opening a new connection to database")
|
|
191
|
+ self._connections[conn_h] = {
|
|
192
|
+ 'conn_count': 1,
|
|
193
|
+ 'db': utils.connection(
|
|
194
|
+ host = self.__db_infos['host'],
|
|
195
|
+ port = self.__db_infos['port'],
|
|
196
|
+ username = username,
|
|
197
|
+ password = password)}
|
|
198
|
+ return self._connections[conn_h]['db'][self.__db_infos['db_name']]
|
|
199
|
+
|
185
|
200
|
|
186
|
201
|
##@brief Return a pymongo collection given a LeObject child class
|
187
|
202
|
#@param leobject LeObject child class (no instance)
|