cache

Details

diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/tools/Reducer.java b/src/main/java/br/ufrgs/inf/prosoft/cache/tools/Reducer.java
index 4298fa7..5254586 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/tools/Reducer.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/tools/Reducer.java
@@ -1,29 +1,20 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
 package br.ufrgs.inf.prosoft.cache.tools;
 
 import br.ufrgs.inf.prosoft.cache.CacheEvent;
 import br.ufrgs.inf.prosoft.cache.EventType;
 import com.google.gson.Gson;
+
 import java.io.FileWriter;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
-import java.util.function.Consumer;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
-/**
- *
- * @author romulo
- */
 public class Reducer {
 
     private static final Logger LOGGER = Logger.getLogger(Reducer.class.getName());
@@ -45,9 +36,9 @@ public class Reducer {
             });
 
             try (FileWriter fileWriter = new FileWriter(reducePath, true)) {
-                objectHasHits.entrySet().stream().forEach(entry -> {
+                objectHasHits.forEach((key, value) -> {
                     try {
-                        fileWriter.write(prefix + entry.getKey() + "," + entry.getValue() + "\n");
+                        fileWriter.write(prefix + key + "," + value + "\n");
                     } catch (IOException ex) {
                         LOGGER.log(Level.SEVERE, "IOException {0}", ex);
                     }
@@ -61,45 +52,33 @@ public class Reducer {
     public static void size(String eventsPath, String reducePath, String prefix) {
         try (Stream<String> lines = Files.lines(Paths.get(eventsPath))) {
             Gson gson = new Gson();
-            lines.forEach(new Consumer<String>() {
-                int second = 0;
-
-                @Override
-                public void accept(String line) {
-                    CacheEvent event = gson.fromJson(line, CacheEvent.class);
-                    if (!event.getType().equals(EventType.POPULATION)) {
-                        return;
-                    }
-                    try (FileWriter fileWriter = new FileWriter(reducePath, true)) {
-                        fileWriter.write(prefix + event.getName() + "," + this.second + "," + event.getIdentifier() + "\n");
+            List<CacheEvent> events = lines
+              .map(it -> gson.fromJson(it, CacheEvent.class))
+              .sorted(Comparator.comparingLong(CacheEvent::getTime))
+              .collect(Collectors.toList());
+            long startTime = events.get(0).getTime();
+            try (FileWriter fileWriter = new FileWriter(reducePath, true)) {
+                AtomicLong currentCacheSize = new AtomicLong();
+                fileWriter.write(prefix + events.get(0).getName() + "," + 0 + "," + currentCacheSize + "\n");
+                events.forEach(it -> {
+                    if (it.getType().equals(EventType.ADDITION)) currentCacheSize.getAndIncrement();
+                    else if (it.getType().equals(EventType.INVALIDATION)) currentCacheSize.getAndDecrement();
+                    else return;
+                    try {
+                        long adjustedTime = it.getTime() - startTime;
+                        fileWriter.write(prefix + it.getName() + "," + adjustedTime + "," + currentCacheSize + "\n");
                     } catch (IOException ex) {
-                        LOGGER.log(Level.SEVERE, "output error {0}", eventsPath);
+                        LOGGER.log(Level.SEVERE, "output error {0}", reducePath);
                     }
-                    this.second++;
-                }
-            });
+                });
+                long endTime = events.get(events.size() - 1).getTime() - startTime;
+                fileWriter.write(prefix + events.get(events.size() - 1).getName() + "," + endTime + "," + currentCacheSize + "\n");
+            } catch (IOException ex) {
+                LOGGER.log(Level.SEVERE, "output error {0}", reducePath);
+            }
         } catch (IOException ex) {
-            LOGGER.log(Level.SEVERE, "input not found {0}", eventsPath);
-        }
-    }
-
-    private static class Method {
-
-        String name;
-        String reference;
-        long ttl;
-        double avgexecution;
-        int hits;
-        int additions;
-
-        double getSavedTime() {
-            return avgexecution * hits;
+            LOGGER.log(Level.SEVERE, "output error {0}", eventsPath);
         }
-
-        long getTimeInCache() {
-            return ttl * additions;
-        }
-
     }
 
     public static void savedTimeAndTimeInCache(String eventsPath, String recommendedTTLsPath, String averageExecutionsPath, String reducePath, String prefix) {
@@ -165,4 +144,23 @@ public class Reducer {
         }
     }
 
+    private static class Method {
+
+        String name;
+        String reference;
+        long ttl;
+        double avgexecution;
+        int hits;
+        int additions;
+
+        double getSavedTime() {
+            return avgexecution * hits;
+        }
+
+        long getTimeInCache() {
+            return ttl * additions;
+        }
+
+    }
+
 }