Reducer.java

55 lines | 1.938 kB Blame History Raw Download
/*
 * 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);
        }
    }
}