Package pulp :: Package server :: Package db :: Module connection
[hide private]
[frames] | no frames]

Source Code for Module pulp.server.db.connection

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  # 
 4  # Copyright © 2010 Red Hat, Inc. 
 5  # 
 6  # This software is licensed to you under the GNU General Public License, 
 7  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 8  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 9  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
10  # along with this software; if not, see 
11  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
12  # 
13  # Red Hat trademarks are not licensed under GPLv2. No permission is 
14  # granted to use or replicate Red Hat trademarks that are incorporated 
15  # in this software or its documentation. 
16   
17  import logging 
18   
19  import pymongo 
20  from pymongo.son_manipulator import AutoReference, NamespaceInjector 
21   
22   
23  _connection = None 
24  _database = None 
25   
26  _log = logging.getLogger(__name__) 
27   
28  # connection api -------------------------------------------------------------- 
29   
30 -def _initialize():
31 """ 32 Initialize the connection pool and top-level database for pulp. 33 """ 34 global _connection, _database 35 try: 36 _connection = pymongo.Connection() 37 _database = _connection._database 38 _database.add_son_manipulator(NamespaceInjector()) 39 _database.add_son_manipulator(AutoReference(_database)) 40 except Exception: 41 _log.critical('Database initialization failed') 42 _connection = None 43 _database = None 44 raise
45 46
47 -def get_object_db(name, unique_indexes=[], other_indexes=[], order=pymongo.DESCENDING):
48 """ 49 Get an object database (read MongoDB Document Collection) for the given name. 50 Build in indexes in the given order. 51 @type name: basestring instance or derived instance 52 @param name: name of the object database to get 53 @type unique_indexes: iterable of str's 54 @param unique_indexes: unique indexes of the database 55 @type other_indexes: iterable of str's 56 @param other_indexes: non-unique indexes of the database 57 @type order: either pymongo.ASCENDING or pymongo.DESCENDING 58 @param order: order of the database indexes 59 """ 60 if _database is None: 61 raise RuntimeError('Database is not initialized') 62 objdb = getattr(_database, name) 63 for index in unique_indexes: 64 #_log.debug('Object DB %s: adding unique index: %s' % (objdb.name, index)) 65 objdb.ensure_index([(index, order)], unique=True, background=True) 66 for index in other_indexes: 67 #_log.debug('Object DB %s: adding non-unique index: %s' % (objdb.name, index)) 68 objdb.ensure_index([(index, order)], unique=False, background=True) 69 return objdb
70 71 # initialize on import -------------------------------------------------------- 72 73 _initialize() 74