def _get_cache(self, cache_filename, cache_pathname): # Remove file on disc if there is one try: if os.path.exists(cache_pathname): os.remove(cache_pathname) except Exception as ex: raise Exception('Removal of old cache file %s failed.' % cache_pathname) from ex if not self.is_postgre_sql: sql_statement = "SELECT PARQUET_FILE FROM %s.%s WHERE ENTITY_TYPE_ID = ? AND PARQUET_NAME = ?" % ( self.quoted_schema, self.quoted_cache_tablename) stmt = ibm_db.prepare(self.db_connection, sql_statement) try: ibm_db.bind_param(stmt, 1, self.entity_type_id) ibm_db.bind_param(stmt, 2, cache_filename) ibm_db.execute(stmt) row = ibm_db.fetch_tuple(stmt) if row is False: row = None except Exception as ex: raise Exception( 'Retrieval of cache %s failed with sql statement "%s"' % (cache_filename, sql_statement)) from ex finally: ibm_db.free_result(stmt) else: sql_statement = 'SELECT parquet_file FROM %s.%s' % (self.quoted_schema, self.quoted_cache_tablename) sql_statement += ' WHERE entity_type_id = %s AND parquet_name = %s' try: row = dbhelper.execute_postgre_sql_select_query(self.db_connection, sql_statement, (self.entity_type_id, cache_filename), fetch_one_only=True) except Exception as ex: raise Exception( 'Retrieval of cache %s failed with sql statement "%s"' % (cache_filename, sql_statement)) from ex cache_found = False if row is not None: cache_found = True parquet = row[0] if parquet is not None and len(parquet) > 0: try: f = open(cache_pathname, "wb") try: f.write(parquet) logger.info('Cache %s has been retrieved from table %s.%s and stored under %s' % ( cache_filename, self.quoted_schema, self.quoted_cache_tablename, cache_pathname)) finally: f.close() except Exception as ex: raise Exception('Writing cache file %s to disc failed.' % cache_pathname) from ex else: logger.info('The cache %s is empty' % cache_filename) else: logger.info('No cache found for %s' % cache_filename) return cache_found
def retrieve_model(self, model_name, deserialize=True): if not self.is_postgre_sql: sql_statement = "SELECT MODEL FROM %s.%s WHERE ENTITY_TYPE_ID = ? AND MODEL_NAME = ?" % ( self.quoted_schema, self.quoted_store_tablename) stmt = ibm_db.prepare(self.db_connection, sql_statement) try: ibm_db.bind_param(stmt, 1, self.entity_type_id) ibm_db.bind_param(stmt, 2, model_name) ibm_db.execute(stmt) row = ibm_db.fetch_tuple(stmt) if row is False: model = None else: model = row[0] except Exception as ex: raise Exception( 'Retrieval of model %s failed with sql statement "%s"' % (model_name, sql_statement)) from ex finally: ibm_db.free_result(stmt) else: sql_statement = 'SELECT model FROM %s.%s' % (self.quoted_schema, self.quoted_store_tablename) sql_statement += ' WHERE entity_type_id = %s AND model_name = %s' try: row = dbhelper.execute_postgre_sql_select_query(self.db_connection, sql_statement, (self.entity_type_id, model_name), fetch_one_only=True) if row is None: model = None else: model = bytes(row[0]) except Exception as ex: raise Exception( 'Retrieval of model %s failed with sql statement "%s"' % (model_name, sql_statement)) from ex if model is not None: logger.info('Model %s of size %d bytes has been retrieved from table %s.%s' % ( model_name, len(model) if model is not None else 0, self.quoted_schema, self.quoted_store_tablename)) else: logger.info('Model %s does not exist in table %s.%s' % ( model_name, self.quoted_schema, self.quoted_store_tablename)) if model is not None and deserialize: try: model = pickle.loads(model) except Exception as ex: raise Exception( 'Deserialization of model %s that has been retrieved from ModelStore failed.' % model_name) from ex return model