blob: d4e40293daec4ae72a65b85926f46489b03d0b0d (
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
|
package database;
import analysis.BrandChecker;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Peter Wu
*/
public class BrandAnalyzerQueue implements Runnable {
private final BrandChecker checker;
private final ResultSet data;
private final BlockingQueue<Result> queue;
private volatile boolean last = false;
public BrandAnalyzerQueue(ResultSet data) {
this.checker = new BrandChecker("brandonlyrules.txt");
this.data = data;
this.queue = new ArrayBlockingQueue<>(1000);
}
private Logger getLogger() {
return Logger.getLogger(BrandAnalyzerQueue.class.getName());
}
@Override
public void run() {
try {
fillQueue();
} catch (SQLException ex) {
getLogger().log(Level.SEVERE, "Horrible! Database error", ex);
} catch (InterruptedException ex) {
getLogger().log(Level.SEVERE, "Interrupted!", ex);
}
try {
last = true;
queue.put(new Result(-1, null));
} catch (InterruptedException ex) {
getLogger().log(Level.SEVERE, "Failed to insert suicide pill!");
}
}
private void fillQueue() throws SQLException, InterruptedException {
while (data.next()) {
List<String> brands = checker.getBrands(data.getString("text"));
// if there is no brand, add a dummy so we know it got checked
if (brands.isEmpty()) {
brands.add("no");
}
long tweetid = data.getLong("tweetid");
Result result = new Result(tweetid, brands);
queue.put(result);
}
}
public Result next() {
Result result = null;
try {
if (!last) {
result = queue.take();
if (result.brands == null) {
result = null;
}
}
} catch (InterruptedException ex) {
getLogger().log(Level.SEVERE, "Interrupted!", ex);
}
return result;
}
public static class Result {
public final long tweetid;
public final List<String> brands;
public Result(long tweetid, List<String> brands) {
this.tweetid = tweetid;
this.brands = brands;
}
}
}
|