Quellcode durchsuchen

Bugfixes in MongoDb datasource

- bugfixes in connection string generator
- bugfixes in garbage collection of opened connections
Yann Weber vor 8 Jahren
Ursprung
Commit
79cd9b21b1
2 geänderte Dateien mit 53 neuen und 25 gelöschten Zeilen
  1. 24
    9
      plugins/mongodb_datasource/datasource.py
  2. 29
    16
      plugins/mongodb_datasource/utils.py

+ 24
- 9
plugins/mongodb_datasource/datasource.py Datei anzeigen

@@ -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)

+ 29
- 16
plugins/mongodb_datasource/utils.py Datei anzeigen

@@ -41,23 +41,36 @@ def get_connection_args(connnection_name='default'):
41 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 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 74
 def connect(host, port, db_name, username, password):
62 75
     conn = connection(host, port, username, password)
63 76
     database = conn[db_name]

Laden…
Abbrechen
Speichern