cache

added cache events reducer tool

5/22/2019 5:23:01 AM

Details

pom.xml 47(+47 -0)

diff --git a/pom.xml b/pom.xml
index 2995c37..7b99582 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,4 +10,51 @@
         <maven.compiler.source>1.8</maven.compiler.source>
         <maven.compiler.target>1.8</maven.compiler.target>
     </properties>
+    <dependencies>
+        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.8.5</version>
+        </dependency>
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>copy</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>copy-dependencies</goal>
+                        </goals>
+                        <configuration>
+                            <outputDirectory>
+                                ${project.build.directory}/lib
+                            </outputDirectory>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <version>3.1.0</version>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addClasspath>true</addClasspath>
+                            <classpathPrefix>lib</classpathPrefix>
+                            <mainClass>br.ufrgs.inf.prosoft.cache.tools.Main</mainClass>
+                        </manifest>
+                        <manifestEntries>
+                            <Class-Path>lib/</Class-Path>
+                        </manifestEntries>
+                    </archive>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 </project>
\ No newline at end of file
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java b/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java
index c96fdc3..016d8b3 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java
@@ -91,6 +91,22 @@ public class CacheEvent {
         return new CacheEvent("INVALIDATION", identifier, size);
     }
 
+    public long getTime() {
+        return time;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public String getIdentifier() {
+        return identifier;
+    }
+
+    public int getSize() {
+        return size;
+    }
+
     @Override
     public String toString() {
         StringBuilder stringBuilder = new StringBuilder();
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/tools/Main.java b/src/main/java/br/ufrgs/inf/prosoft/cache/tools/Main.java
new file mode 100644
index 0000000..d6a4779
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/tools/Main.java
@@ -0,0 +1,26 @@
+/*
+ * 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;
+
+/**
+ *
+ * @author romulo
+ */
+public class Main {
+
+    public static void main(String[] args) {
+        System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT+%1$tL] [%4$-7s] [Cache] %5$s %n");
+
+        if (args.length < 3) {
+            System.err.println("<EventsPath> <ReducePath> <prefix>");
+            System.exit(1);
+        }
+        String eventsPath = args[0];
+        String reducePath = args[1];
+        String prefix = args[2];
+        Reducer.reduce(eventsPath, reducePath, prefix);
+    }
+}
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
new file mode 100644
index 0000000..1447fdc
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/tools/Reducer.java
@@ -0,0 +1,54 @@
+/*
+ * 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 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.List;
+import java.util.Map;
+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());
+
+    public static void reduce(String eventsPath, String reducePath, String prefix) {
+        try (Stream<String> lines = Files.lines(Paths.get(eventsPath))) {
+            Gson gson = new Gson();
+            List<CacheEvent> events = lines.map(line -> gson.fromJson(line, CacheEvent.class))
+                    .filter(event -> event.getType().equals("HIT") || event.getType().equals("ADDITION"))
+                    .collect(Collectors.toList());
+
+            Map<String, Long> objectHasEvent = events.stream()
+                    .collect(Collectors.groupingBy(
+                            event -> event.getIdentifier() + "," + event.getType().toLowerCase(),
+                            Collectors.counting()));
+
+            try (FileWriter fileWriter = new FileWriter(reducePath, true)) {
+                objectHasEvent.forEach((key, value) -> {
+                    try {
+                        fileWriter.write(prefix + key + "," + value + "\n");
+                    } catch (IOException ex) {
+                        LOGGER.log(Level.SEVERE, "IOException {0}", ex);
+                    }
+                });
+            }
+        } catch (IOException ex) {
+            LOGGER.log(Level.SEVERE, "file not found {0}", eventsPath);
+        }
+    }
+}