<s> import numpy as np <EOL> import cStringIO <EOL> import zlib <EOL> import MySQLdb <EOL> import re <EOL> from collections import defaultdict <EOL> import itertools <EOL> from ocptype import OLDCHANNEL <EOL> import logging <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> """<STR_LIT>""" <EOL> class MySQLKVIO : <EOL> def __init__ ( self , db ) : <EOL> """<STR_LIT>""" <EOL> self . db = db <EOL> self . conn = None <EOL> try : <EOL> self . conn = MySQLdb . connect ( host = self . db . proj . getDBHost ( ) , user = self . db . proj . getDBUser ( ) , passwd = self . db . proj . getDBPasswd ( ) , db = self . db . proj . getDBName ( ) ) <EOL> except MySQLdb . Error , e : <EOL> self . conn = None <EOL> logger . error ( "<STR_LIT>" . format ( self . db . proj . getDBHost ( ) , self . db . proj . getDBName ( ) ) ) <EOL> raise <EOL> self . txncursor = None <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . conn : <EOL> self . conn . close ( ) <EOL> def startTxn ( self ) : <EOL> """<STR_LIT>""" <EOL> self . txncursor = self . conn . cursor ( ) <EOL> sql = "<STR_LIT>" <EOL> self . txncursor . execute ( sql ) <EOL> def commit ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor : <EOL> self . conn . commit ( ) <EOL> self . txncursor . close ( ) <EOL> self . txncursor = None <EOL> def rollback ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor : <EOL> self . conn . rollback ( ) <EOL> self . txncursor . close ( ) <EOL> self . txncursor = None <EOL> def getChannelId ( self , ch ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" <EOL> try : <EOL> cursor . execute ( sql , [ ch . getChannelName ( ) ] ) <EOL> row = cursor . fetchone ( ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if row is None : <EOL> return None <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def getCube ( self , ch , zidx , resolution , update = False ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> if ch . getChannelType ( ) == OLDCHANNEL : <EOL> channel_id = self . getChannelId ( ch ) <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) , channel_id , zidx ) <EOL> else : <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) , zidx ) <EOL> if update : <EOL> sql += "<STR_LIT>" <EOL> try : <EOL> cursor . execute ( sql ) <EOL> row = cursor . fetchone ( ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if row is None : <EOL> return None <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def getTimeCube ( self , ch , zidx , timestamp , resolution , update = False ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) , zidx , timestamp ) <EOL> if update : <EOL> sql += "<STR_LIT>" <EOL> try : <EOL> cursor . execute ( sql ) <EOL> row = cursor . fetchone ( ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if row is None : <EOL> return None <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def getCubes ( self , ch , listofidxs , resolution , neariso = False ) : <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> if ch . getChannelType ( ) == OLDCHANNEL : <EOL> channel_id = self . getChannelId ( ch ) <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) , channel_id ) <EOL> else : <EOL> if neariso : <EOL> sql = "<STR_LIT>" . format ( ch . getNearIsoTable ( resolution ) ) <EOL> else : <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) ) <EOL> in_p = '<STR_LIT:U+002CU+0020>' . join ( map ( lambda x : '<STR_LIT:%s>' , listofidxs ) ) <EOL> sql = sql % in_p <EOL> try : <EOL> rc = cursor . execute ( sql , listofidxs ) <EOL> while ( True ) : <EOL> try : <EOL> retval = cursor . fetchone ( ) <EOL> except : <EOL> break <EOL> if retval is not None : <EOL> yield ( retval ) <EOL> else : <EOL> return <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> def getTimeCubes ( self , ch , idx , listoftimestamps , resolution ) : <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) , idx ) <EOL> in_p = '<STR_LIT:U+002CU+0020>' . join ( map ( lambda x : '<STR_LIT:%s>' , listoftimestamps ) ) <EOL> sql = sql % in_p <EOL> try : <EOL> rc = cursor . execute ( sql , listoftimestamps ) <EOL> while ( True ) : <EOL> try : <EOL> retval = cursor . fetchone ( ) <EOL> except : <EOL> break <EOL> if retval is not None : <EOL> yield ( retval ) <EOL> else : <EOL> return <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> def putCubes ( self , ch , listofidxs , resolution , listofcubes , update = False ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) ) <EOL> try : <EOL> cursor . executemany ( sql , zip ( listofidxs , listofcubes ) ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> def putCube ( self , ch , zidx , resolution , cubestr , update = False ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> if not update : <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) ) <EOL> try : <EOL> cursor . execute ( sql , ( zidx , cubestr ) ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> else : <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) , zidx ) <EOL> try : <EOL> cursor . execute ( sql , ( cubestr , ) ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if self . txncursor is None : <EOL> self . conn . commit ( ) <EOL> def putTimeCube ( self , ch , zidx , timestamp , resolution , cubestr , update = False ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> if not update : <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) ) <EOL> try : <EOL> cursor . execute ( sql , ( zidx , timestamp , cubestr ) ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> else : <EOL> sql = "<STR_LIT>" . format ( ch . getTable ( resolution ) , zidx , timestamp ) <EOL> try : <EOL> cursor . execute ( sql , [ cubestr ] ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if self . txncursor is None : <EOL> self . conn . commit ( ) <EOL> def getAnnotationType ( self , ch , annid ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getAnnoTable ( '<STR_LIT>' ) , annid ) <EOL> try : <EOL> cursor . execute ( sql ) <EOL> row = cursor . fetchone ( ) <EOL> except MySQLdb . Error , e : <EOL> logger . warning ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> except BaseException , e : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if row is None : <EOL> return None <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def getAnnotation ( self , ch , annid , anno_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getAnnoTable ( anno_type ) , annid ) <EOL> try : <EOL> cursor . execute ( sql ) <EOL> row = cursor . fetchone ( ) <EOL> except MySQLdb . Error , e : <EOL> logger . warning ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> except BaseException , e : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if row is None : <EOL> return None <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def getIndex ( self , ch , annid , resolution , update ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getIdxTable ( resolution ) , annid ) <EOL> if update : <EOL> sql += "<STR_LIT>" <EOL> try : <EOL> cursor . execute ( sql ) <EOL> row = cursor . fetchone ( ) <EOL> except MySQLdb . Error , e : <EOL> logger . warning ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> except BaseException , e : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if row is None : <EOL> return [ ] <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def putIndex ( self , ch , zidx , resolution , indexstr , update ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> if not update : <EOL> sql = "<STR_LIT>" . format ( ch . getIdxTable ( resolution ) ) <EOL> try : <EOL> cursor . execute ( sql , ( zidx , indexstr ) ) <EOL> except MySQLdb . Error , e : <EOL> logger . warning ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> except BaseException , e : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> else : <EOL> sql = "<STR_LIT>" . format ( ch . getIdxTable ( resolution ) , zidx ) <EOL> try : <EOL> cursor . execute ( sql , ( indexstr , ) ) <EOL> except MySQLdb . Error , e : <EOL> logger . warning ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> except : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if self . txncursor is None : <EOL> self . conn . commit ( ) <EOL> def deleteIndex ( self , ch , annid , resolution ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getIdxTable ( resolution ) , annid ) <EOL> try : <EOL> cursor . execute ( sql ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> except : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if self . txncursor is None : <EOL> self . conn . commit ( ) <EOL> def getExceptions ( self , ch , zidx , resolution , annid ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getExceptionsTable ( resolution ) , zidx , annid ) <EOL> try : <EOL> cursor . execute ( sql ) <EOL> row = cursor . fetchone ( ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" . format ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> raise <EOL> finally : <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> if row is None : <EOL> return [ ] <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def deleteExceptions ( self , ch , zidx , resolution , annid ) : <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> sql = "<STR_LIT>" . format ( ch . getExceptionsTable ( resolution ) , zidx , annid ) <EOL> try : <EOL> cursor . execute ( sql ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" % ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> raise <EOL> if self . txncursor is None : <EOL> self . conn . commit ( ) <EOL> cursor . close ( ) <EOL> def putExceptions ( self , ch , zidx , resolution , annid , excstr , update = False ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> if self . txncursor is None : <EOL> cursor = self . conn . cursor ( ) <EOL> else : <EOL> cursor = self . txncursor <EOL> if not update : <EOL> sql = "<STR_LIT>" . format ( ch . getExceptionsTable ( resolution ) ) <EOL> try : <EOL> cursor . execute ( sql , ( zidx , annid , excstr ) ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" % ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> raise <EOL> else : <EOL> sql = "<STR_LIT>" . format ( ch . getExceptionsTable ( resolution ) ) <EOL> try : <EOL> cursor . execute ( sql , ( zlib . compress ( excstr ) , zidx , annid ) ) <EOL> except MySQLdb . Error , e : <EOL> logger . error ( "<STR_LIT>" % ( e . args [ <NUM_LIT:0> ] , e . args [ <NUM_LIT:1> ] , sql ) ) <EOL> if self . txncursor is None : <EOL> cursor . close ( ) <EOL> raise <EOL> if self . txncursor is None : <EOL> self . conn . commit ( ) <EOL> cursor . close ( ) </s>
