summaryrefslogtreecommitdiff
path: root/src/io/DataWriter.java
blob: 42d69f8b291684d6eb5a19309a039d1b0c021fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import provider.ResultListener;

/**
 * This class writes the output data into seperate files.
 *
 * @author Maurice Laveaux
 */
public class DataWriter implements ResultListener {

    /**
     * The writer for the tweet stream.
     */
    private final FileWriter m_tweetWriter;

    /**
     * the writer for the profile stream.
     */
    private final FileWriter m_profileWriter;

    /**
     * the buffer of tweet ids that already exist.
     */
    private final Set<Long> m_tweetIdSet;

    /**
     * the buffer of profile ids that already exist.
     */
    private final Set<Long> m_profileIdSet;

    public final static String CFG_PROFILE_FILENAME = "profiles-filename";
    public final static String CFG_TWEETS_FILENAME = "tweets-filename";

    /**
     * Opens a stream to every single file that data will be streamed to.
     *
     * @param profilesName The file to write the profiles to.
     * @param tweetsName The file to write the tweets to.
     * @throws java.io.IOException if the files cannot be read or written.
     */
    public DataWriter(final String profilesName, final String tweetsName)
            throws IOException {
        m_profileIdSet = readIds(profilesName);
        m_profileWriter = new FileWriter(profilesName, true);

        m_tweetIdSet = readIds(tweetsName);
        m_tweetWriter = new FileWriter(tweetsName, true);
    }

    public void close() {
        try {
            m_tweetWriter.close();
            m_profileWriter.close();
        } catch (IOException ex) {
            getLogger().log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void profileGenerated(JSONObject obj) {
        writeObject(obj, m_profileWriter, m_profileIdSet);
    }

    @Override
    public void tweetGenerated(JSONObject obj) {
        writeObject(obj, m_tweetWriter, m_tweetIdSet);
    }

    /**
     * Read the current existing tweetName and profileName filenames and fill
     * the existing id set.
     *
     * @param filename The file to parse.
     * @return The set of ids, may be empty if the fill does not exist.
     */
    private Set readIds(String filename) throws IOException {
        Set<Long> idSet = new HashSet<>();
        try {
            Scanner reader = new Scanner(new File(filename));
            // parse each line into a JSONObject, read the id and add it to
            // the set of ids.
            while (reader.hasNext()) {
                JSONObject obj = new JSONObject(reader.nextLine());
                long id = obj.getLong("id");
                idSet.add(id);
            }
        } catch (FileNotFoundException ex) {
            // ignore, file will be created if necessary.
        } catch (JSONException ex) {
            getLogger().log(Level.WARNING, filename
                    + ": File is only partially processed", ex);
        }
        return idSet;
    }

    /**
     * Writes the JSONObject to a writer and update the idSet.
     *
     * @param obj The object to write.
     * @param writer The writer object to append the object to.
     * @param idSet The id set to add the obj id to.
     */
    private void writeObject(JSONObject obj, FileWriter writer, Set<Long> idSet) {
        try {
            long id = obj.getLong("id");

            if (!idSet.contains(id)) {
                // Write a single profile into the profile file.
                try {
                    writer.write(obj.toString() + "\n");
                    idSet.add(id);
                } catch (IOException ex) {
                    getLogger().log(Level.WARNING, "Cannot write to file", ex);
                }
            }
        } catch (JSONException ex) {
            getLogger().log(Level.WARNING, "ID not found?!", ex);
        }
    }

    private Logger getLogger() {
        return Logger.getLogger(getClass().getName());
    }
}