cache

Details

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 016d8b3..a5ab1e9 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java
@@ -12,99 +12,53 @@ package br.ufrgs.inf.prosoft.cache;
 public class CacheEvent {
 
     private final long time;
-    private final String type;
+    private final EventType type;
     private final int size;
+    private final String name;
     private final String identifier;
 
-    private CacheEvent(String type) {
-        this(type, 0);
+    protected CacheEvent(EventType type, String name) {
+        this(type, name, 0);
     }
 
-    private CacheEvent(String type, String identifier) {
-        this(type, identifier, 0);
+    protected CacheEvent(EventType type, String name, String identifier) {
+        this(type, name, identifier, 0);
     }
 
-    private CacheEvent(String type, int size) {
-        this(type, null, size);
+    protected CacheEvent(EventType type, String name, int size) {
+        this(type, null, null, size);
     }
 
-    private CacheEvent(String type, String identifier, int size) {
-        this(System.currentTimeMillis(), type, identifier, size);
+    protected CacheEvent(EventType type, String name, String identifier, int size) {
+        this(System.currentTimeMillis(), type, name, identifier, size);
     }
 
-    private CacheEvent(long time, String type, String identifier, int size) {
+    private CacheEvent(long time, EventType type, String name, String identifier, int size) {
         this.time = time;
         this.type = type;
         this.identifier = identifier;
+        this.name = name;
         this.size = size;
     }
 
-    public static CacheEvent miss() {
-        return new CacheEvent("MISS");
-    }
-
-    public static CacheEvent addition() {
-        return new CacheEvent("ADDITION");
-    }
-
-    public static CacheEvent addition(String identifier) {
-        return new CacheEvent("ADDITION", identifier);
-    }
-
-    public static CacheEvent addition(int size) {
-        return new CacheEvent("ADDITION", size);
-    }
-
-    public static CacheEvent addition(String identifier, int size) {
-        return new CacheEvent("ADDITION", identifier, size);
-    }
-
-    public static CacheEvent hit() {
-        return new CacheEvent("HIT");
-    }
-
-    public static CacheEvent hit(String identifier) {
-        return new CacheEvent("HIT", identifier);
-    }
-
-    public static CacheEvent hit(int size) {
-        return new CacheEvent("HIT", size);
-    }
-
-    public static CacheEvent hit(String identifier, int size) {
-        return new CacheEvent("HIT", identifier, size);
-    }
-
-    public static CacheEvent invalidation() {
-        return new CacheEvent("INVALIDATION");
-    }
-
-    public static CacheEvent invalidation(String identifier) {
-        return new CacheEvent("INVALIDATION", identifier);
-    }
-
-    public static CacheEvent invalidation(int size) {
-        return new CacheEvent("INVALIDATION", size);
+    public long getTime() {
+        return this.time;
     }
 
-    public static CacheEvent invalidation(String identifier, int size) {
-        return new CacheEvent("INVALIDATION", identifier, size);
+    public EventType getType() {
+        return this.type;
     }
 
-    public long getTime() {
-        return time;
-    }
-
-    public String getType() {
-        return type;
+    public String getName() {
+        return this.name;
     }
 
     public String getIdentifier() {
-        return identifier;
+        return this.identifier;
     }
 
     public int getSize() {
-        return size;
+        return this.size;
     }
 
     @Override
@@ -113,11 +67,15 @@ public class CacheEvent {
         stringBuilder.append("{");
         stringBuilder.append("\"time\":").append(this.time);
         stringBuilder.append(",");
-        stringBuilder.append("\"type\":").append("\"").append(this.type).append("\"");
+        stringBuilder.append("\"type\":").append("\"").append(this.type.name()).append("\"");
         if (this.size > 0) {
             stringBuilder.append(",");
             stringBuilder.append("\"size\":").append(this.size);
         }
+        if (this.name != null) {
+            stringBuilder.append(",");
+            stringBuilder.append("\"name\":").append("\"").append(this.name).append("\"");
+        }
         if (this.identifier != null) {
             stringBuilder.append(",");
             stringBuilder.append("\"identifier\":").append("\"").append(this.identifier).append("\"");
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/CachePerformance.java b/src/main/java/br/ufrgs/inf/prosoft/cache/CachePerformance.java
index 31c39f5..122f4e3 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/CachePerformance.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/CachePerformance.java
@@ -39,9 +39,7 @@ public class CachePerformance {
             @Override
             public void run() {
                 Logger.getLogger(CachePerformance.class.getName()).log(Level.INFO, "printing caching metrics");
-                System.out.print(CachePerformance.this.name);
-                System.out.print(": ");
-                System.out.println(CachePerformance.this.toLongString());
+                System.out.println(CachePerformance.this.name + ": " + CachePerformance.this.toLongString());
             }
         });
     }
@@ -57,75 +55,40 @@ public class CachePerformance {
         this.maximumSize = Math.max(this.maximumSize, cachingPerformance.maximumSize);
     }
 
-    public void registerMiss() {
-        this.misses++;
-        logCacheEvent(CacheEvent.miss());
-    }
-
-    public void registerAddition() {
-        this.additions++;
-        logCacheEvent(CacheEvent.addition());
-    }
-
-    public void registerAddition(String identifier) {
-        this.additions++;
-        logCacheEvent(CacheEvent.addition(identifier));
-    }
-
-    public void registerAddition(int size) {
-        this.additions++;
-        this.bytesAdded += size;
-        logCacheEvent(CacheEvent.addition(size));
-    }
-
-    public void registerAddition(String identifier, int size) {
-        this.additions++;
-        this.bytesAdded += size;
-        logCacheEvent(CacheEvent.addition(identifier, size));
-    }
-
-    public void registerHit() {
-        this.hits++;
-        logCacheEvent(CacheEvent.hit());
-    }
-
-    public void registerHit(String identifier) {
-        this.hits++;
-        logCacheEvent(CacheEvent.hit(identifier));
-    }
-
-    public void registerHit(int size) {
-        this.hits++;
-        this.bytesHit += size;
-        logCacheEvent(CacheEvent.hit(size));
-    }
-
-    public void registerHit(String identifier, int size) {
-        this.hits++;
-        this.bytesHit += size;
-        logCacheEvent(CacheEvent.hit(identifier, size));
-    }
-
-    public void registerInvalidation() {
-        this.invalidations++;
-        logCacheEvent(CacheEvent.invalidation());
+    public void registerEvent(EventType type, String identifier, int size) {
+        switch (type) {
+            case HIT:
+                this.hits++;
+                this.bytesHit += size;
+                break;
+            case MISS:
+                this.misses++;
+                break;
+            case ADDITION:
+                this.additions++;
+                this.bytesAdded += size;
+                break;
+            case INVALIDATION:
+                this.invalidations++;
+                this.bytesInvalidated += size;
+                break;
+            default:
+                throw new AssertionError(type.name());
+        }
+        CacheEvent cacheEvent = new CacheEvent(type, this.name, identifier, size);
+        logCacheEvent(cacheEvent);
     }
 
-    public void registerInvalidation(String identifier) {
-        this.invalidations++;
-        logCacheEvent(CacheEvent.invalidation(identifier));
+    public void registerEvent(EventType event) {
+        registerEvent(event, null, 0);
     }
 
-    public void registerInvalidation(int size) {
-        this.invalidations++;
-        this.bytesInvalidated += size;
-        logCacheEvent(CacheEvent.invalidation(size));
+    public void registerEvent(EventType event, int size) {
+        registerEvent(event, null, size);
     }
 
-    public void registerInvalidation(String identifier, int size) {
-        this.invalidations++;
-        this.bytesInvalidated += size;
-        logCacheEvent(CacheEvent.invalidation(identifier, size));
+    public void registerEvent(EventType event, String identifier) {
+        registerEvent(event, identifier, 0);
     }
 
     public void registerEntry() {
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/EventType.java b/src/main/java/br/ufrgs/inf/prosoft/cache/EventType.java
new file mode 100644
index 0000000..4507fb5
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/EventType.java
@@ -0,0 +1,14 @@
+/*
+ * 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;
+
+/**
+ *
+ * @author romulo
+ */
+public enum EventType {
+    HIT, MISS, ADDITION, INVALIDATION;
+}
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java b/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
index 9c86e56..6c225c1 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
@@ -58,9 +58,9 @@ public class MultiCache<K, V> implements Cache<K, V> {
         invalidate(key);
         this.map.put(key, value);
         if (CACHE_REGISTER_SIZE) {
-            this.cachingPerformance.registerAddition(String.valueOf(value.hashCode()), CachePerformance.calculateObjectSize(value));
+            this.cachingPerformance.registerEvent(EventType.ADDITION, String.valueOf(value.hashCode()), CachePerformance.calculateObjectSize(value));
         } else {
-            this.cachingPerformance.registerAddition(String.valueOf(value.hashCode()));
+            this.cachingPerformance.registerEvent(EventType.ADDITION, String.valueOf(value.hashCode()));
         }
         this.cachingPerformance.registerSize(this.map.size());
     }
@@ -69,12 +69,12 @@ public class MultiCache<K, V> implements Cache<K, V> {
     public V get(K key) {
         V get = this.map.get(key);
         if (get == null) {
-            this.cachingPerformance.registerMiss();
+            this.cachingPerformance.registerEvent(EventType.MISS);
         } else {
             if (CACHE_REGISTER_SIZE) {
-                this.cachingPerformance.registerHit(String.valueOf(get.hashCode()), CachePerformance.calculateObjectSize(get));
+                this.cachingPerformance.registerEvent(EventType.HIT, String.valueOf(get.hashCode()), CachePerformance.calculateObjectSize(get));
             } else {
-                this.cachingPerformance.registerHit(String.valueOf(get.hashCode()));
+                this.cachingPerformance.registerEvent(EventType.HIT, String.valueOf(get.hashCode()));
             }
         }
         return get;
@@ -85,9 +85,9 @@ public class MultiCache<K, V> implements Cache<K, V> {
         V remove = this.map.remove(key);
         if (remove != null) {
             if (CACHE_REGISTER_SIZE) {
-                this.cachingPerformance.registerInvalidation(String.valueOf(remove.hashCode()), CachePerformance.calculateObjectSize(remove));
+                this.cachingPerformance.registerEvent(EventType.INVALIDATION, String.valueOf(remove.hashCode()), CachePerformance.calculateObjectSize(remove));
             } else {
-                this.cachingPerformance.registerInvalidation(String.valueOf(remove.hashCode()));
+                this.cachingPerformance.registerEvent(EventType.INVALIDATION, String.valueOf(remove.hashCode()));
             }
         }
     }
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 f8771d0..3ac305f 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
@@ -6,6 +6,7 @@
 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;
@@ -30,18 +31,18 @@ public class Reducer {
         try (Stream<String> lines = Files.lines(Paths.get(eventsPath))) {
             Gson gson = new Gson();
             List<CacheEvent> hits = lines.map(line -> gson.fromJson(line, CacheEvent.class))
-                    .filter(event -> event.getType().equals("HIT"))
+                    .filter(event -> event.getType().equals(EventType.HIT) || event.getType().equals(EventType.ADDITION))
                     .collect(Collectors.toList());
 
             Map<String, Long> objectHasHits = hits.stream()
                     .collect(Collectors.groupingBy(
-                            event -> event.getIdentifier() + "," + event.getType().toLowerCase(),
+                            event -> event.getName() + "," + event.getIdentifier() + "," + event.getType().name().toLowerCase(),
                             Collectors.counting()));
 
             try (FileWriter fileWriter = new FileWriter(reducePath, true)) {
-                objectHasHits.values().stream().forEach(value -> {
+                objectHasHits.entrySet().stream().forEach(entry -> {
                     try {
-                        fileWriter.write(prefix + value + "\n");
+                        fileWriter.write(prefix + entry.getKey() + "," + entry.getValue() + "\n");
                     } catch (IOException ex) {
                         LOGGER.log(Level.SEVERE, "IOException {0}", ex);
                     }