server/pool.py
changeset 9456 a79e88aad555
parent 8696 0bb18407c053
child 9463 d62e13eba033
equal deleted inserted replaced
9455:62e89e696a3b 9456:a79e88aad555
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    27 class ConnectionsSet(object):
    27 class ConnectionsSet(object):
    28     """handle connections on a set of sources, at some point associated to a
    28     """handle connections on a set of sources, at some point associated to a
    29     :class:`Session`
    29     :class:`Session`
    30     """
    30     """
    31 
    31 
    32     def __init__(self, sources):
    32     # since 3.19, we only have to manage the system source connection
       
    33     def __init__(self, system_source):
    33         # dictionary of (source, connection), indexed by sources'uri
    34         # dictionary of (source, connection), indexed by sources'uri
    34         self.source_cnxs = {}
    35         self.source_cnxs = {}
    35         for source in sources:
    36         self.source_cnxs['system'] = (system_source,
    36             self.add_source(source)
    37                                       system_source.get_connection())
    37         if not 'system' in self.source_cnxs:
       
    38             self.source_cnxs['system'] = self.source_cnxs[sources[0].uri]
       
    39         self._cursors = {}
    38         self._cursors = {}
    40 
    39 
    41     def __getitem__(self, uri):
    40     def __getitem__(self, uri):
    42         """subscription notation provide access to sources'cursors"""
    41         """subscription notation provide access to sources'cursors"""
       
    42         assert uri == 'system'
    43         try:
    43         try:
    44             cursor = self._cursors[uri]
    44             cursor = self._cursors[uri]
    45         except KeyError:
    45         except KeyError:
    46             cursor = self.source_cnxs[uri][1].cursor()
    46             cursor = self.source_cnxs[uri][1].cursor()
    47             if cursor is not None:
    47             if cursor is not None:
    48                 # None possible on sources without cursor support such as ldap
    48                 # None possible on sources without cursor support such as ldap
    49                 self._cursors[uri] = cursor
    49                 self._cursors[uri] = cursor
    50         return cursor
    50         return cursor
    51 
       
    52     def add_source(self, source):
       
    53         assert not source.uri in self.source_cnxs
       
    54         self.source_cnxs[source.uri] = (source, source.get_connection())
       
    55 
       
    56     def remove_source(self, source):
       
    57         source, cnx = self.source_cnxs.pop(source.uri)
       
    58         cnx.close()
       
    59         self._cursors.pop(source.uri, None)
       
    60 
    51 
    61     def commit(self):
    52     def commit(self):
    62         """commit the current transaction for this user"""
    53         """commit the current transaction for this user"""
    63         # FIXME: what happends if a commit fail
    54         # FIXME: what happends if a commit fail
    64         # would need a two phases commit or like, but I don't know how to do
    55         # would need a two phases commit or like, but I don't know how to do
   108     def sources(self):
    99     def sources(self):
   109         """return the source objects handled by this connections set"""
   100         """return the source objects handled by this connections set"""
   110         # implementation details of flying insert requires the system source
   101         # implementation details of flying insert requires the system source
   111         # first
   102         # first
   112         yield self.source_cnxs['system'][0]
   103         yield self.source_cnxs['system'][0]
   113         for uri, (source, cnx) in self.source_cnxs.items():
       
   114             if uri == 'system':
       
   115                 continue
       
   116             yield source
       
   117         #return [source_cnx[0] for source_cnx in self.source_cnxs.itervalues()]
       
   118 
   104 
   119     def source(self, uid):
   105     def source(self, uid):
   120         """return the source object with the given uri"""
   106         """return the source object with the given uri"""
   121         return self.source_cnxs[uid][0]
   107         return self.source_cnxs[uid][0]
   122 
   108