@logExceptions(_LOGGER)<EOL><INDENT>def modelInsertAndStart(self, jobID, params, paramsHash, particleHash=None):<DEDENT>
<EOL>if particleHash is None:<EOL><INDENT>particleHash = paramsHash<EOL><DEDENT>paramsHash = self._normalizeHash(paramsHash)<EOL>particleHash = self._normalizeHash(particleHash)<EOL>def findExactMatchNoRetries(conn):<EOL><INDENT>return self._getOneMatchingRowNoRetries(<EOL>self._models, conn,<EOL>{'<STR_LIT>':jobID, '<STR_LIT>':paramsHash,<EOL>'<STR_LIT>':particleHash},<EOL>['<STR_LIT>', '<STR_LIT>'])<EOL><DEDENT>@g_retrySQL<EOL>def findExactMatchWithRetries():<EOL><INDENT>with ConnectionFactory.get() as conn:<EOL><INDENT>return findExactMatchNoRetries(conn)<EOL><DEDENT><DEDENT>row = findExactMatchWithRetries()<EOL>if row is not None:<EOL><INDENT>return (row[<NUM_LIT:0>], False)<EOL><DEDENT>@g_retrySQL<EOL>def insertModelWithRetries():<EOL><INDENT>"""<STR_LIT>"""<EOL>with ConnectionFactory.get() as conn:<EOL><INDENT>query = '<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>'% (self.modelsTableName,)<EOL>sqlParams = (jobID, params, self.STATUS_RUNNING, paramsHash,<EOL>particleHash, self._connectionID)<EOL>try:<EOL><INDENT>numRowsAffected = conn.cursor.execute(query, sqlParams)<EOL><DEDENT>except Exception as e:<EOL><INDENT>if "<STR_LIT>" not in str(e):<EOL><INDENT>raise<EOL><DEDENT>self._logger.info('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>jobID, paramsHash.encode('<STR_LIT>'),<EOL>particleHash.encode('<STR_LIT>'), e)<EOL><DEDENT>else:<EOL><INDENT>if numRowsAffected == <NUM_LIT:1>:<EOL><INDENT>conn.cursor.execute('<STR_LIT>')<EOL>modelID = conn.cursor.fetchall()[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>if modelID != <NUM_LIT:0>:<EOL><INDENT>return (modelID, True)<EOL><DEDENT>else:<EOL><INDENT>self._logger.warn(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>jobID, paramsHash, particleHash)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self._logger.error(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>numRowsAffected, jobID, paramsHash, particleHash)<EOL><DEDENT><DEDENT>row = findExactMatchNoRetries(conn)<EOL>if row is not None:<EOL><INDENT>(modelID, connectionID) = row<EOL>return (modelID, connectionID == self._connectionID)<EOL><DEDENT>query = '<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>'% (self.modelsTableName,)<EOL>sqlParams = [jobID, paramsHash, particleHash]<EOL>numRowsFound = conn.cursor.execute(query, sqlParams)<EOL>assert numRowsFound == <NUM_LIT:1>, (<EOL>'<STR_LIT>'<EOL>'<STR_LIT>') % (jobID, paramsHash, particleHash, numRowsFound)<EOL>(modelID,) = conn.cursor.fetchall()[<NUM_LIT:0>]<EOL>return (modelID, False)<EOL><DEDENT><DEDENT>return insertModelWithRetries()<EOL>
Insert a new unique model (based on params) into the model table in the
        "running" state. This will return two things: whether or not the model was
        actually inserted (i.e. that set of params isn't already in the table) and
        the modelID chosen for that set of params. Even if the model was not
        inserted by this call (it was already there) the modelID of the one already
        inserted is returned.

        Parameters:
        ----------------------------------------------------------------
        jobID:            jobID of the job to add models for
        params:           params for this model
        paramsHash        hash of the params, generated by the worker
        particleHash      hash of the particle info (for PSO). If not provided,
                          then paramsHash will be used.

        retval:           (modelID, wasInserted)
                          modelID: the model ID for this set of params
                          wasInserted: True if this call ended up inserting the
                          new model. False if this set of params was already in
                          the model table.