This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 557
/
ConcurrentStreamSummary.java
130 lines (115 loc) · 4.56 KB
/
ConcurrentStreamSummary.java
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
/*
* Copyright (C) 2011 Clearspring Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.clearspring.analytics.stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
/**
* Based on the <i>Space-Saving</i> algorithm and the <i>Stream-Summary</i>
* data structure as described in:
* <i>Efficient Computation of Frequent and Top-k Elements in Data Streams</i>
* by Metwally, Agrawal, and Abbadi
* <p/>
* Ideally used in multithreaded applications, otherwise see {@link StreamSummary}
*
* @param <T> type of data in the stream to be summarized
* @author Eric Vlaanderen
*/
public class ConcurrentStreamSummary<T> implements ITopK<T> {
private final int capacity;
private final ConcurrentHashMap<T, ScoredItem<T>> itemMap;
private final AtomicReference<ScoredItem<T>> minVal;
private final AtomicLong size;
private final AtomicBoolean reachCapacity;
public ConcurrentStreamSummary(final int capacity) {
this.capacity = capacity;
this.minVal = new AtomicReference<ScoredItem<T>>();
this.size = new AtomicLong(0);
this.itemMap = new ConcurrentHashMap<T, ScoredItem<T>>(capacity);
this.reachCapacity = new AtomicBoolean(false);
}
@Override
public boolean offer(final T element) {
return offer(element, 1);
}
@Override
public boolean offer(final T element, final int incrementCount) {
long val = incrementCount;
ScoredItem<T> value = new ScoredItem<T>(element, incrementCount);
ScoredItem<T> oldVal = itemMap.putIfAbsent(element, value);
if (oldVal != null) {
val = oldVal.addAndGetCount(incrementCount);
} else if (reachCapacity.get() || size.incrementAndGet() > capacity) {
reachCapacity.set(true);
ScoredItem<T> oldMinVal = minVal.getAndSet(value);
itemMap.remove(oldMinVal.getItem());
while (oldMinVal.isNewItem()) {
// Wait for the oldMinVal so its error and value are completely up to date.
// no thread.sleep here due to the overhead of calling it - the waiting time will be microseconds.
}
long count = oldMinVal.getCount();
value.addAndGetCount(count);
value.setError(count);
}
value.setNewItem(false);
minVal.set(getMinValue());
return val != incrementCount;
}
private ScoredItem<T> getMinValue() {
ScoredItem<T> minVal = null;
for (ScoredItem<T> entry : itemMap.values()) {
if (minVal == null || (!entry.isNewItem() && entry.getCount() < minVal.getCount())) {
minVal = entry;
}
}
return minVal;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (ScoredItem entry : itemMap.values()) {
sb.append("(" + entry.getCount() + ": " + entry.getItem() + ", e: " + entry.getError() + "),");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("]");
return sb.toString();
}
@Override
public List<T> peek(final int k) {
List<T> toReturn = new ArrayList<T>(k);
List<ScoredItem<T>> values = peekWithScores(k);
for (ScoredItem<T> value : values) {
toReturn.add(value.getItem());
}
return toReturn;
}
public List<ScoredItem<T>> peekWithScores(final int k) {
List<ScoredItem<T>> values = new ArrayList<ScoredItem<T>>();
for (Map.Entry<T, ScoredItem<T>> entry : itemMap.entrySet()) {
ScoredItem<T> value = entry.getValue();
values.add(new ScoredItem<T>(value.getItem(), value.getCount(), value.getError()));
}
Collections.sort(values);
values = values.size() > k ? values.subList(0, k) : values;
return values;
}
}