/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Public Attributes | Static Public Attributes | Properties | Private Member Functions | Private Attributes
DybDbi::physad::AdLogicalPhysical Class Reference

List of all members.

Public Member Functions

def __init__
def write
def lookup_logical2physical
def ksite
def ksubsite
def ksitesubsite
def __call__
def __repr__
def __str__
def check_physical2logical

Public Attributes

 simmask
 seqnos
 over logical to physical

Static Public Attributes

int dbno = 0

Properties

 mappings = property(lambda self:["%s:%s" % (self.ksitesubsite(sitesubsite), physadid ) for sitesubsite,physadid in sorted(self.items())])

Private Member Functions

def _dropzero
def _read
def _update_physical2logical

Private Attributes

 _physical2logical

Detailed Description

Provides access to logical/physical mappings from the DBI table :ref:`gendbi-gphysad`, with 
functionality to read the mappings at particular timestamps and write them with validity time range.

#. logical slots are expressed as tuples ``(site,subsite)`` such as ``(Site.kSAB,DetectorId.kAD1)``
#. physical AD indices ``1,2,...8`` corresponding to ``AD1,AD2,..,AD8``

Mappings are stored within this dict keyed by logical slot.
Reverse ``physical->logical`` lookups are provide by the ``__call__`` method::

    alp = AdLogicalPhysical()
    site, subsite = alp(1)         ## find where AD1 is

An input ``physadid`` of **None** is used to express a vacated slot, and 
results in the writing of a payloadless DBI validity. Such **None** are not read 
back into this dict on reading, it being regarded as a write signal only.

For usage examples see :dybgaudi:`Database/DybDbi/tests/test_physad.py`

Definition at line 7 of file physad.py.


Constructor & Destructor Documentation

def DybDbi::physad::AdLogicalPhysical::__init__ (   self,
  timestamp = None,
  purgecache = False,
  DROP = False 
)
:param timestamp: time at which to lookup mapping, defaults of None is promoted to now (UTC natually)
:param purgecache:  clear cache before reading, needed when reading updates from same process that wrote them
:param DROP: drop the table and zero the LASTUSEDSEQNO (**only use during development**)

Read current mappings from ``PhysAd`` DB table, usage::

    alp = AdLogicalPhysical()
    print alp
    blp = AdLogicalPhysical(timestamp=TimeStamp(2011,10,10,0,0,0))
    print blp

Direct lookup::

    phyadid = alp.get((site,subsite), None)
    if physadid:
print "(%(site)s,%(subsite)s) => %(physadid)s " % locals()

To update mappings in memory::

    alp.update({(Site.kSAB,DetectorId.kAD1):1,(Site.kDayaBay,DetectorId.kAD2):2})

Vacating a formerly occupied slot is done using ``None``::

    alp.update({(Site.kSAB,DetectorId.kAD1):None,(Site.kDayaBay,DetectorId.kAD1):1})

To persist the update to the DB, within a particular timerange::

    alp.write( timestart=TimeStamp.kNow() )

Read back by instanciating a new instance::

    blp = AdLogicalPhysical( timestamp=... ) 

Reverse lookup from physical AD id ``1,2,3..8`` to logical slot::

    sitesubsite = alp(1)          ## invokes the __call__ method for reverse lookup            
    if sitesubsite: 
site, subsite = sitesubsite 
    else:
print "not found" 
 
 

Definition at line 31 of file physad.py.

00032                                                                        :
00033         """
00034         :param timestamp: time at which to lookup mapping, defaults of None is promoted to now (UTC natually)
00035         :param purgecache:  clear cache before reading, needed when reading updates from same process that wrote them
00036         :param DROP: drop the table and zero the LASTUSEDSEQNO (**only use during development**)
00037 
00038         Read current mappings from ``PhysAd`` DB table, usage::
00039 
00040             alp = AdLogicalPhysical()
00041             print alp
00042             blp = AdLogicalPhysical(timestamp=TimeStamp(2011,10,10,0,0,0))
00043             print blp
00044 
00045         Direct lookup::
00046 
00047             phyadid = alp.get((site,subsite), None)
00048             if physadid:
00049                 print "(%(site)s,%(subsite)s) => %(physadid)s " % locals()
00050 
00051         To update mappings in memory::
00052 
00053             alp.update({(Site.kSAB,DetectorId.kAD1):1,(Site.kDayaBay,DetectorId.kAD2):2})
00054 
00055         Vacating a formerly occupied slot is done using ``None``::
00056 
00057             alp.update({(Site.kSAB,DetectorId.kAD1):None,(Site.kDayaBay,DetectorId.kAD1):1})
00058 
00059         To persist the update to the DB, within a particular timerange::
00060 
00061             alp.write( timestart=TimeStamp.kNow() )
00062 
00063         Read back by instanciating a new instance::
00064 
00065             blp = AdLogicalPhysical( timestamp=... ) 
00066 
00067         Reverse lookup from physical AD id ``1,2,3..8`` to logical slot::
00068 
00069             sitesubsite = alp(1)          ## invokes the __call__ method for reverse lookup            
00070             if sitesubsite: 
00071                 site, subsite = sitesubsite 
00072             else:
00073                 print "not found" 
00074  
00075  
00076         """
00077         dict.__init__(self)
00078 
00079         if not timestamp:
00080             timestamp = TimeStamp.kNow()
00081 
00082         self.simmask = SimFlag.kData
00083 
00084         if DROP:
00085             self._dropzero() 
00086         self._read( timestamp=timestamp, purgecache=purgecache )


Member Function Documentation

def DybDbi::physad::AdLogicalPhysical::_dropzero (   cls) [private]
Drops DB table, zeros corresponding LASTUSEDSEQNO, recreates the table 

Definition at line 88 of file physad.py.

00089                       :
00090         """
00091         Drops DB table, zeros corresponding LASTUSEDSEQNO, recreates the table 
00092         """
00093         dbconf = os.environ.get('DBCONF')
00094         assert dbconf
00095         db = DB(dbconf) 
00096         assert db.sect.startswith('tmp_') and db.dbc['database'].startswith('tmp_') , "safety measure prevents dropping DB sections/names that do not start tmp_"
00097         log.warn("_dropzero dropping %s %s " % (db.sect, cls.table) )
00098         tab = db.tab(cls.table)
00099         tab.dropzero() 
00100         cls.kls().CreateDatabaseTables( cls.dbno, cls.table )     

def DybDbi::physad::AdLogicalPhysical::write (   self,
  timestart = None,
  timeend = None 
)
Writes mappings expressed in this dict into DB 

:param timestart:
:param timeend:

Definition at line 101 of file physad.py.

00102                                                     :
00103         """
00104         Writes mappings expressed in this dict into DB 
00105 
00106         :param timestart:
00107         :param timeend:
00108 
00109         """ 
00110         if not timestart:
00111             timestart = TimeStamp.kNow() 
00112         if not timeend:
00113             timeend = TimeStamp.GetEOT() 
00114 
00115         log.info("writing %s " % self )
00116         simmask = self.simmask
00117         seqnos = []
00118         emptys = []
00119 
00120         for sitesubsite,physadid in sorted(self.items()): ## over logical to physical
00121             assert physadid in range(1,9) + [None]
00122             site,subsite = sitesubsite
00123             sitemask = site              
00124             wrt = self.kls.Wrt() 
00125             contextrange = ContextRange( sitemask, simmask, timestart, timeend )   
00126             wrt.ctx( contextrange=contextrange, subsite=subsite )
00127            
00128             if physadid:
00129                 instance = self.kls.Create( PhysAdId=physadid )       
00130                 wrt.Write( instance )
00131             else:
00132                 emptys.append(sitesubsite)  
00133                 log.info("empty slot %s write payloadless validity " % str(sitesubsite) )
00134 
00135             seqno = wrt.Close()
00136             assert seqno, "failed to write %s " % str(sitesubsite) 
00137             seqnos.append(seqno)
00138             pass
00139         self.seqnos = map(int,seqnos)          ## record seqnos from the last read/write
00140 
00141 
00142         log.debug("clearing the None after writing ... regard the None as write signal rather than state")
00143         for sitesubsite in emptys:
00144             self.pop(sitesubsite) 
00145 
00146         return self.seqnos
00147 

def DybDbi::physad::AdLogicalPhysical::lookup_logical2physical (   cls,
  timestamp,
  sitesubsite,
  simflag = SimFlag.kData 
)
:param timestamp:
:param sitesubsite:
:param simflag:

:return physadid, vrec:

Note that a payloadless DBI query result is interpreted to mean an empty logical slot
resulting in the return of a ``physadid`` of **None**

Cannot use ``kAnySubSite = -1`` to avoid querying every slot as DBI 
non-aggregate reads always correspond to a single SEQNO

Definition at line 149 of file physad.py.

00150                                                                                         :
00151         """
00152         :param timestamp:
00153         :param sitesubsite:
00154         :param simflag:
00155 
00156         :return physadid, vrec:
00157 
00158         Note that a payloadless DBI query result is interpreted to mean an empty logical slot
00159         resulting in the return of a ``physadid`` of **None**
00160 
00161         Cannot use ``kAnySubSite = -1`` to avoid querying every slot as DBI 
00162         non-aggregate reads always correspond to a single SEQNO
00163         """
00164         assert type(sitesubsite) == tuple and len(sitesubsite) == 2, "expecting tuple of length 2, unlike %s " % str(sitesubsite)   
00165         site, subsite = sitesubsite
00166         rpt = cls.kls.Rpt()
00167         rpt.ctx( timestamp=timestamp , site=site, simflag=simflag , subsite=subsite )
00168         n = len(rpt)
00169         if n > 0:
00170             assert n == 1, "non-zero payload counts should always be 1 %s %s  " % ( n, str(sitesubsite) )
00171             assert rpt.vrec.contextrange.sitemask == site   , "sitemask mismatch "
00172             assert rpt.vrec.contextrange.simmask == simflag , "simmask/simflag mismatch "
00173             assert rpt.vrec.subsite == subsite
00174             pass
00175             physadid = rpt[0].physadid
00176         else:     
00177             physadid = None
00178         return physadid, rpt.vrec
00179 

def DybDbi::physad::AdLogicalPhysical::_read (   self,
  timestamp = None,
  purgecache = False 
) [private]
:param timestamp: `TimeStamp` instance at which to query, defauls to now (UTC naturally)
:param purgecache: needed for same process reads to notice prior writes

Obtain mappings defined at timestamp provided, by querying DBI in 
all possible logical slots, corresponding to separate DBI contexts, and collecting the mappings:

About AD id of `None`:

#. regarded as an **input only** to express vacating a slot
#. is stored in DB as payloadless validities, but is **not read back** into this dict 

Definition at line 180 of file physad.py.

00181                                                         :
00182         """
00183         :param timestamp: `TimeStamp` instance at which to query, defauls to now (UTC naturally)
00184         :param purgecache: needed for same process reads to notice prior writes
00185 
00186         Obtain mappings defined at timestamp provided, by querying DBI in 
00187         all possible logical slots, corresponding to separate DBI contexts, and collecting the mappings:
00188 
00189         About AD id of `None`:
00190 
00191         #. regarded as an **input only** to express vacating a slot
00192         #. is stored in DB as payloadless validities, but is **not read back** into this dict 
00193 
00194         """
00195 
00196         if not timestamp:
00197             timestamp = TimeStamp.kNow()
00198 
00199         sites = sorted([Site.kDayaBay,Site.kFar,Site.kLingAo,Site.kMid,Site.kSAB]) 
00200         subsites = sorted([DetectorId.kAD1,DetectorId.kAD2,DetectorId.kAD3,DetectorId.kAD4])
00201         slots = [(site,subsite) for site in sites for subsite in subsites]
00202 
00203         if purgecache:
00204             log.info("purging cache")
00205             self.kls.GetTableProxy().GetCache().Purge()
00206 
00207         level = gDbi.level
00208         gDbi.level = "WARNING"
00209         seqnos = []
00210         for sitesubsite in slots:
00211             physadid, vrec = self.lookup_logical2physical( timestamp, sitesubsite, simflag=self.simmask )
00212             seqno = int(vrec.seqno)  
00213             if seqno == 0:
00214                 log.debug("read %s physadid %s seqno %s : skip zero seqno invalid/nonexistant slots " % ( str(sitesubsite), physadid, seqno  ))
00215                 continue
00216             log.debug("read %s physadid %s seqno %s " % ( str(sitesubsite), physadid, seqno  ))
00217             seqnos.append( seqno ) 
00218             if physadid:
00219                 self[sitesubsite] = physadid 
00220             pass
00221         gDbi.level = level
00222         self.seqnos = seqnos          ## record seqnos from the last read/write
00223         log.debug("_read %r %s " % (self,seqnos) )
00224         return seqnos   

def DybDbi::physad::AdLogicalPhysical::ksite (   cls,
  site 
)

Definition at line 226 of file physad.py.

00227                          :
        return "Site.k%s" % Site.AsString(site)
def DybDbi::physad::AdLogicalPhysical::ksubsite (   cls,
  subsite 
)

Definition at line 229 of file physad.py.

00230                                :
        return "DetectorId.k%s" % DetectorId.AsString(subsite)
def DybDbi::physad::AdLogicalPhysical::ksitesubsite (   cls,
  sitesubsite 
)

Definition at line 232 of file physad.py.

00233                                        :
00234         site, subsite = sitesubsite
00235         return "(%s,%s)" % (cls.ksite(site),cls.ksubsite(subsite))

def DybDbi::physad::AdLogicalPhysical::_update_physical2logical (   self) [private]

Definition at line 236 of file physad.py.

00237                                       :
00238         self._physical2logical = {}
00239         for sitesubsite, physadid in self.items():
00240             if physadid:
00241                 self._physical2logical[physadid] = sitesubsite     

def DybDbi::physad::AdLogicalPhysical::__call__ (   self,
  physadid 
)
:param physadid: physical AD id ``1,2,3...8``

Return the logical slot ``(site,subsite)`` tuple corresponding to a physical AD id ``1,2,3...8`` or None
if not yet read from DB or the physical ID is not deployed at the ``timestamp`` specified in the ``read``

As expense of reverse mapping is tiny, update it JIT rather than maintaining it 

Definition at line 242 of file physad.py.

00243                                  :
00244         """
00245         :param physadid: physical AD id ``1,2,3...8``
00246 
00247         Return the logical slot ``(site,subsite)`` tuple corresponding to a physical AD id ``1,2,3...8`` or None
00248         if not yet read from DB or the physical ID is not deployed at the ``timestamp`` specified in the ``read``
00249 
00250         As expense of reverse mapping is tiny, update it JIT rather than maintaining it 
00251         """
00252         self._update_physical2logical()
00253         return self._physical2logical.get(physadid, None)

def DybDbi::physad::AdLogicalPhysical::__repr__ (   self)
Evaluatable string presentation of logical to physical mapping 

Definition at line 256 of file physad.py.

00257                       :
00258         """
00259         Evaluatable string presentation of logical to physical mapping 
00260         """ 
00261         return "%s({%s})" % ( self.__class__.__name__, ",".join(self.mappings) )

def DybDbi::physad::AdLogicalPhysical::__str__ (   self)
Evaluatable string presentation of logical to physical mapping 

Definition at line 262 of file physad.py.

00263                      :
00264         """
00265         Evaluatable string presentation of logical to physical mapping 
00266         """ 
00267         return "%s({%s})" % ( self.__class__.__name__, ",\n".join(self.mappings) )
00268 

def DybDbi::physad::AdLogicalPhysical::check_physical2logical (   self)
Self consistency check
Test that the call returns the expected slot, verifying that the physical2logic dict is in step

Definition at line 269 of file physad.py.

00270                                     :
00271         """
00272         Self consistency check
00273         Test that the call returns the expected slot, verifying that the physical2logic dict is in step
00274         """
00275         for sitesubsite, physadid in sorted(self.items()):
00276             if physadid:          
00277                 sss = self(physadid)
00278                 #log.info("%s =>  %s  ( %s ) " % ( physadid, str(sss), str(sitesubsite) ) )
00279                 assert sss == sitesubsite, "call expected %s by got %s for physadid %s " % ( str(sitesubsite), str(sss), physadid )
00280 
00281 


Member Data Documentation

Definition at line 29 of file physad.py.

Definition at line 74 of file physad.py.

over logical to physical

Definition at line 108 of file physad.py.

Definition at line 236 of file physad.py.


Property Documentation

DybDbi::physad::AdLogicalPhysical::mappings = property(lambda self:["%s:%s" % (self.ksitesubsite(sitesubsite), physadid ) for sitesubsite,physadid in sorted(self.items())]) [static]

Definition at line 254 of file physad.py.


The documentation for this class was generated from the following file:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:57:24 for DybDbi by doxygen 1.7.4