@Test public void insert_and_select_data_stream() throws Exception { when(system.now()).thenReturn(NOW); InputStream report = IOUtils.toInputStream(SOME_DATA); underTest.insert(dbTester.getSession(), A_UUID, report); Optional<CeTaskInputDao.DataStream> result = underTest.selectData(dbTester.getSession(), A_UUID); assertThat(result).isPresent(); try { assertThat(IOUtils.toString(result.get().getInputStream())).isEqualTo(SOME_DATA); } finally { result.get().close(); } }
public Optional<DataStream> selectData(DbSession dbSession, String taskUuid) { PreparedStatement stmt = null; ResultSet rs = null; DataStream result = null; try { stmt = dbSession.getConnection().prepareStatement("SELECT input_data FROM ce_task_input WHERE task_uuid=? AND input_data IS NOT NULL"); stmt.setString(1, taskUuid); rs = stmt.executeQuery(); if (rs.next()) { result = new DataStream(stmt, rs, rs.getBinaryStream(1)); return Optional.of(result); } return Optional.empty(); } catch (SQLException e) { throw new IllegalStateException("Fail to select data of CE task " + taskUuid, e); } finally { if (result == null) { DatabaseUtils.closeQuietly(rs); DatabaseUtils.closeQuietly(stmt); } } }
public Optional<DataStream> selectData(DbSession dbSession, String taskUuid) { PreparedStatement stmt = null; ResultSet rs = null; DataStream result = null; try { stmt = dbSession.getConnection().prepareStatement("SELECT input_data FROM ce_task_input WHERE task_uuid=? AND input_data IS NOT NULL"); stmt.setString(1, taskUuid); rs = stmt.executeQuery(); if (rs.next()) { result = new DataStream(stmt, rs, rs.getBinaryStream(1)); return Optional.of(result); } return Optional.empty(); } catch (SQLException e) { throw new IllegalStateException("Fail to select data of CE task " + taskUuid, e); } finally { if (result == null) { DatabaseUtils.closeQuietly(rs); DatabaseUtils.closeQuietly(stmt); } } }
@Override public void execute() { try (DbSession dbSession = dbClient.openSession(false)) { Optional<CeTaskInputDao.DataStream> opt = dbClient.ceTaskInputDao().selectData(dbSession, task.getUuid()); if (opt.isPresent()) { File unzippedDir = tempFolder.newDir(); try (CeTaskInputDao.DataStream reportStream = opt.get(); InputStream zipStream = new BufferedInputStream(reportStream.getInputStream())) { ZipUtils.unzip(zipStream, unzippedDir); } catch (IOException e) { throw new IllegalStateException("Fail to extract report " + task.getUuid() + " from database", e); } reportDirectoryHolder.setDirectory(unzippedDir); } else { throw MessageException.of("Analysis report " + task.getUuid() + " is missing in database"); } } }
@Override public void execute(ComputationStep.Context context) { try (DbSession dbSession = dbClient.openSession(false)) { Optional<CeTaskInputDao.DataStream> opt = dbClient.ceTaskInputDao().selectData(dbSession, task.getUuid()); if (opt.isPresent()) { File unzippedDir = tempFolder.newDir(); try (CeTaskInputDao.DataStream reportStream = opt.get(); InputStream zipStream = new BufferedInputStream(reportStream.getInputStream())) { ZipUtils.unzip(zipStream, unzippedDir); } catch (IOException e) { throw new IllegalStateException("Fail to extract report " + task.getUuid() + " from database", e); } reportDirectoryHolder.setDirectory(unzippedDir); if (LOGGER.isDebugEnabled()) { // size is not added to context statistics because computation // can take time. It's enabled only if log level is DEBUG. try { String dirSize = FileUtils.byteCountToDisplaySize(FileUtils2.sizeOf(unzippedDir.toPath())); LOGGER.debug("Analysis report is {} uncompressed", dirSize); } catch (IOException e) { LOGGER.warn("Fail to compute size of directory " + unzippedDir, e); } } } else { throw MessageException.of("Analysis report " + task.getUuid() + " is missing in database"); } } }
@Test public void start_deletes_orphan_report_files() { // analysis reports are persisted but the associated // task is not in the queue insertInQueue("TASK_1", CeQueueDto.Status.PENDING); insertTaskData("TASK_1"); insertTaskData("TASK_2"); runCleaner(); CeTaskInputDao dataDao = dbTester.getDbClient().ceTaskInputDao(); Optional<CeTaskInputDao.DataStream> task1Data = dataDao.selectData(dbTester.getSession(), "TASK_1"); assertThat(task1Data).isPresent(); task1Data.get().close(); assertThat(dataDao.selectData(dbTester.getSession(), "TASK_2")).isNotPresent(); }