Browse Source

Bugfixes in MongoDb datasource

- bugfixes in connection string generator
- bugfixes in garbage collection of opened connections
Yann Weber 8 years ago
parent
commit
79cd9b21b1
2 changed files with 53 additions and 25 deletions
  1. 24
    9
      plugins/mongodb_datasource/datasource.py
  2. 29
    16
      plugins/mongodb_datasource/utils.py

+ 24
- 9
plugins/mongodb_datasource/datasource.py View File

2
 
2
 
3
 import re
3
 import re
4
 import warnings
4
 import warnings
5
-import bson
6
 from bson.son import SON
5
 from bson.son import SON
7
 from collections import OrderedDict
6
 from collections import OrderedDict
8
 import pymongo
7
 import pymongo
9
 from pymongo.errors import BulkWriteError
8
 from pymongo.errors import BulkWriteError
10
-import urllib
11
 
9
 
12
 from lodel import logger
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
     MONGODB_SORT_OPERATORS_MAP, connection_string
14
     MONGODB_SORT_OPERATORS_MAP, connection_string
16
 
15
 
17
 class MongoDbDataSourceError(Exception):
16
 class MongoDbDataSourceError(Exception):
51
         self.__read_only = bool(read_only)
50
         self.__read_only = bool(read_only)
52
         ##@brief Uniq ID for mongodb connection
51
         ##@brief Uniq ID for mongodb connection
53
         self.__conn_hash= None
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
     ##@brief Destructor that attempt to close connection to DB
57
     ##@brief Destructor that attempt to close connection to DB
58
     #
58
     #
64
         if self._connections[self.__conn_hash]['conn_count'] <= 0:
64
         if self._connections[self.__conn_hash]['conn_count'] <= 0:
65
             self._connections[self.__conn_hash]['db'].close()
65
             self._connections[self.__conn_hash]['db'].close()
66
             del(self._connections[self.__conn_hash])
66
             del(self._connections[self.__conn_hash])
67
+            logger.info("Closing connection to database")
67
 
68
 
68
     ##@brief returns a selection of documents from the datasource
69
     ##@brief returns a selection of documents from the datasource
69
     #@param target Emclass
70
     #@param target Emclass
176
     #@param ro bool : If True the Datasource is for read only, else the
177
     #@param ro bool : If True the Datasource is for read only, else the
177
     def __connect(self, username, password, ro):
178
     def __connect(self, username, password, ro):
178
         conn_string = connection_string(
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
         if conn_h in self._connections:
186
         if conn_h in self._connections:
183
             self._connections[conn_h]['conn_count'] += 1
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
     ##@brief Return a pymongo collection given a LeObject child class
201
     ##@brief Return a pymongo collection given a LeObject child class
187
     #@param leobject LeObject child class (no instance)
202
     #@param leobject LeObject child class (no instance)

+ 29
- 16
plugins/mongodb_datasource/utils.py View File

41
     return {'host': 'localhost', 'port': 28015, 'login': 'lodel_admin', 'password': 'lapwd', 'dbname': 'lodel'}
41
     return {'host': 'localhost', 'port': 28015, 'login': 'lodel_admin', 'password': 'lapwd', 'dbname': 'lodel'}
42
 
42
 
43
 
43
 
44
-## @brief Creates a connection to a MongoDb Database
45
-# @param connection_name str
46
-# @return MongoClient
47
-'''def mongodbconnect(connection_name):
48
-    login, password, host, port, dbname = get_connection_args(connection_name)
49
-    return connect(host, port, dbname, login, password)
50
-'''
51
-
52
-
53
-def connection_string(host, port, db_name, username, password):
54
-    return 'mongodb://%s:%s@%s:%s' % (username, password, host, port)
55
-
56
-
44
+def connection_string(host, port, username, password):
45
+    ret = 'mongodb://'
46
+    if username != None:
47
+        ret += username
48
+        if password != None:
49
+            ret += ':'+password
50
+        ret+='@'
51
+    elif password != None:
52
+        raise RuntimeError("Password given but no username given...")
53
+    host = 'localhost' if host is None else host
54
+    ret += host
55
+    if port != None:
56
+        ret += ':'+str(port)
57
+    return ret
58
+
59
+##@brief Return an instanciated MongoClient
60
+#@param host str : hostname or ip
61
+#@param port int : port
62
+#@param username str | None: username
63
+#@param password str|None : password
57
 def connection(host, port, username, password):
64
 def connection(host, port, username, password):
58
-    return MongoClient(connection_string(host, port, '', username, password))
59
-
60
-
65
+    conn_str = connection_string(host, port, username, password)
66
+    return MongoClient(conn_str)
67
+
68
+##@brief Return a database cursor
69
+#@param host str : hostname or ip
70
+#@param port int : port
71
+#@param db_name str : database name
72
+#@param username str | None: username
73
+#@param password str|None : password
61
 def connect(host, port, db_name, username, password):
74
 def connect(host, port, db_name, username, password):
62
     conn = connection(host, port, username, password)
75
     conn = connection(host, port, username, password)
63
     database = conn[db_name]
76
     database = conn[db_name]

Loading…
Cancel
Save