private List<String> readFileContentAsList(File jobFile) { List<String> lines = new ArrayList<String>(); FileTouchPoint fileTouchPoint = this.fileTouchPointMap.get(jobFile.getAbsolutePath()); boolean needToReadFile = false; long lastReadPoint = -1; if (fileTouchPoint == null) { needToReadFile = true; } else if (fileTouchPoint.shouldReadFile(jobFile.lastModified())) { needToReadFile = true; lastReadPoint = fileTouchPoint.lastReadPoint; } if (needToReadFile) { RandomAccessFile raf = null; try { raf = FileHelper.randomAccessFile(jobFile, "r"); if (lastReadPoint != -1) raf.seek(lastReadPoint); String line = null; while ((line = raf.readLine()) != null) { lines.add(line); } lastReadPoint = raf.getFilePointer(); fileTouchPointMap.put( jobFile.getAbsolutePath(), new FileTouchPoint(jobFile.lastModified(), lastReadPoint)); } catch (FileNotFoundException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (IOException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } finally { if (raf != null) try { FileHelper.close(raf); } catch (IOException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } } } return lines; }
public void crunchJobFileCounter(String jobId, File jobFile) { List<String> fileContentLines = readFileContentAsList(jobFile); if (fileContentLines.size() > 0) { String newFile = jobFile.getAbsolutePath().replace("cumulative", FILE_EXTENSION); BufferedWriter bw = null; try { bw = FileHelper.bufferedWriter(newFile, true); LastPoint lastPoint = this.fileLastCrunchPointMap.get(jobFile.getAbsolutePath()); if (lastPoint == null) { String firstContentLine = fileContentLines.remove(0); String[] tokens = firstContentLine.split(","); lastPoint = new LastPoint(Long.parseLong(tokens[0]), Long.parseLong(tokens[1])); } while (fileContentLines.size() > 0) { String currentContentLine = fileContentLines.remove(0); String[] tokens = currentContentLine.split(","); long currentContentTimeMS = Long.parseLong(tokens[0]); long currentContentCount = Long.parseLong(tokens[1]); long opsDone = currentContentCount - lastPoint.count; long timeTakenMS = currentContentTimeMS - lastPoint.time; float timeTakenSec = (float) timeTakenMS / MathConstant.THOUSAND; float tps = opsDone / timeTakenSec; CounterStatsInstance counterStatsInstance = new CounterStatsInstance() .setTime(Clock.dateFromMS(currentContentTimeMS)) .setCount(currentContentCount) .setThroughput(tps); bw.write(objectMapper.writeValueAsString(counterStatsInstance) + "\n"); bw.flush(); BufferedWriter bwLast = FileHelper.bufferedWriter(newFile + ".last", false); bwLast.write(objectMapper.writeValueAsString(counterStatsInstance) + "\n"); bwLast.flush(); lastPoint = new LastPoint(currentContentTimeMS, currentContentCount); this.fileLastCrunchPointMap.put(jobFile.getAbsolutePath(), lastPoint); } } catch (FileNotFoundException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (IOException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } finally { try { FileHelper.close(bw); } catch (IOException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } } } }