-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataConsumer.java
More file actions
100 lines (92 loc) · 3.94 KB
/
Copy pathDataConsumer.java
File metadata and controls
100 lines (92 loc) · 3.94 KB
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
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
public class DataConsumer implements Runnable {
private BlockingQueue<DataRow> queue;
private Map result;
private Map m;
private int arrayThreshhold;
private volatile boolean done = false;
//private static final int SLEEPTIME = 1000;
public DataConsumer(BlockingQueue<DataRow> queue, Map result, int arrayThreshhold) {
this.queue = queue;
this.result = result;
this.arrayThreshhold = arrayThreshhold;
}
@Override
public void run() {
//System.out.println("Start building HashMap: thread " + Thread.currentThread().getId());
//Random r = new Random();
int i = 0;
try {
while (true) {
if(done) {
if (queue.isEmpty())
break;
}
ArrayList<Integer> bArrary;
HashSet<Integer> bHashSet;
DataRow row = queue.poll(1, TimeUnit.SECONDS);
int a,b;
if (row != null) {
a = row.getData()[0];
b = row.getData()[1];
synchronized(result) {
if (!result.containsKey(a)) {
//bHashSet = new HashSet<Integer>();
//bHashSet.add(b);
//result.put(a, bHashSet);
/* Using an ArrayList to store a small set of b values,
* when we got a bunch of groups or distinct values of a,
* but few distinct b values for each a,
* to save memory usage
* (TreeSet is better for search,
* but skill consume too much memory) */
if (arrayThreshhold > 0) {
bArrary = new ArrayList<Integer>();
bArrary.add(b);
result.put(a, bArrary);
}
else {
bHashSet = new HashSet<Integer>();
bHashSet.add(b);
result.put(a, bHashSet);
}
} else if (!(result.get(a) instanceof HashSet)) {
bArrary = (ArrayList<Integer>) result.get(a);
/* Check if bArray contains b can be optimized by SIMD */
if (!bArrary.contains(b)) {
if (bArrary.size() < arrayThreshhold) {
bArrary.add(b);
//result.replace(a, bArrary);
} else {
bHashSet = new HashSet<Integer>();
for (Integer bKey : bArrary) {
bHashSet.add(bKey);
}
bHashSet.add(b);
result.replace(a, bHashSet);
}
}
} else {
bHashSet = (HashSet) result.get(a);
bHashSet.add(b);
//result.put(a, bHashSet);
}
}
i++;
}
}
//System.out.println("Thread "+ Thread.currentThread().getId() + " built: " + i);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
} catch (ConcurrentModificationException e) {
e.printStackTrace();
}
}
public void stop() {
this.done = true;
}
}