summaryrefslogtreecommitdiff
path: root/src/database/BrandAnalyzerInserter.java
blob: d42dac2e2ad86e3bf66b101b099dafd2d4745b8b (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
package database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.Callable;
import main.Watcher;

/**
 * Inserts queued brand analyses into the database.
 *
 * @author Peter Wu
 */
public class BrandAnalyzerInserter implements Callable<Integer>,
        Watcher.Progressable {

    private final Connection connection;
    private final BrandAnalyzerQueue analyzer;
    private final NamedPreparedStatement insertBrand;
    private volatile int count;

    public BrandAnalyzerInserter(Connection connection,
            BrandAnalyzerQueue analyzer) throws SQLException {
        this.connection = connection;
        this.analyzer = analyzer;
        this.insertBrand = new NamedPreparedStatement(connection,
                "INSERT INTO mentionsbrand"
                + " SELECT :tweetid AS tweetid, :brand AS brand"
                + " WHERE NOT EXISTS (SELECT 1 FROM mentionsbrand WHERE"
                + " tweetid=:tweetid AND brand=:brand)");
        connection.setAutoCommit(false);
    }

    @Override
    public Integer call() throws SQLException {
        try {
            runInserter();
        } finally {
            connection.close();
        }
        return count;
    }

    private void runInserter() throws SQLException {
        BrandAnalyzerQueue.Result result;
        while ((result = analyzer.next()) != null) {
            for (String brand : result.brands) {
                QueryUtils.setInsertBrandParams(insertBrand, result.tweetid, brand);
                insertBrand.executeUpdate();
            }

            count++;
            // save results every 1000 analyses
            if (count % 1000 == 0) {
                connection.commit();
            }
        }
    }

    @Override
    public int getCount() {
        return count;
    }
}