Skip to content
This repository was archived by the owner on Jul 7, 2020. It is now read-only.

Commit 290602d

Browse files
committed
Merge remote-tracking branch 'jkff/cleanup-new'
2 parents 4daaa62 + 235c9bc commit 290602d

12 files changed

+136
-782
lines changed

src/main/java/com/clearspring/analytics/stream/StochasticTopper.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ public StochasticTopper(int sampleSize)
4141
this(sampleSize, null);
4242
}
4343

44-
/**
45-
*
46-
*
47-
*/
4844
public StochasticTopper(int sampleSize, Long seed)
4945
{
5046
this.sample = new SampleSet<T>(sampleSize);
@@ -56,21 +52,16 @@ public StochasticTopper(int sampleSize, Long seed)
5652
random = new Random();
5753
}
5854

59-
/**
60-
*
61-
* @param item
62-
* @return
63-
*/
6455
public boolean offer(T item)
6556
{
6657
count++;
6758
boolean taken = false;
68-
if(sample.count() < sampleSize)
59+
if (sample.count() < sampleSize)
6960
{
7061
sample.put(item);
7162
taken = true;
7263
}
73-
else if(random.nextDouble() < sampleSize/(double)count)
64+
else if (random.nextDouble() < sampleSize/(double)count)
7465
{
7566
sample.removeRandom();
7667
sample.put(item);
@@ -82,8 +73,6 @@ else if(random.nextDouble() < sampleSize/(double)count)
8273

8374
/**
8475
* Retrieve top k items
85-
* @param k
86-
* @return
8776
*/
8877
public List<T> peek(int k)
8978
{

src/main/java/com/clearspring/analytics/stream/StreamSummary.java

+4-78
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@
1717
package com.clearspring.analytics.stream;
1818

1919
import java.io.ByteArrayInputStream;
20-
import java.io.ByteArrayOutputStream;
2120
import java.io.Externalizable;
2221
import java.io.IOException;
2322
import java.io.ObjectInput;
2423
import java.io.ObjectInputStream;
2524
import java.io.ObjectOutput;
26-
import java.io.ObjectOutputStream;
27-
import java.util.ArrayList;
28-
import java.util.HashMap;
29-
import java.util.List;
25+
import java.util.*;
3026

3127
import com.clearspring.analytics.util.DoublyLinkedList;
28+
import com.clearspring.analytics.util.ExternalizableUtil;
3229
import com.clearspring.analytics.util.ListNode2;
3330
import com.clearspring.analytics.util.Pair;
3431

@@ -55,52 +52,6 @@ public Bucket(long count)
5552
}
5653
}
5754

58-
/*
59-
public class Counter implements Externalizable
60-
{
61-
private ListNode2<Bucket> bucketNode;
62-
63-
private T item;
64-
private long count;
65-
private long error;
66-
67-
public Counter(ListNode2<Bucket> bucket, T item)
68-
{
69-
this.bucketNode = bucket;
70-
this.count = 0;
71-
this.error = 0;
72-
this.item = item;
73-
}
74-
75-
public T getItem() { return item; }
76-
public long getCount() { return count; }
77-
public long getError() { return error; }
78-
79-
@Override
80-
public String toString()
81-
{
82-
return item+":"+count+':'+error;
83-
}
84-
85-
@SuppressWarnings("unchecked")
86-
@Override
87-
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
88-
{
89-
item = (T)in.readObject();
90-
count = in.readLong();
91-
error = in.readLong();
92-
}
93-
94-
@Override
95-
public void writeExternal(ObjectOutput out) throws IOException
96-
{
97-
out.writeObject(item);
98-
out.writeLong(count);
99-
out.writeLong(error);
100-
}
101-
}
102-
*/
103-
10455
protected int capacity;
10556
private HashMap<T, ListNode2<Counter<T>>> counterMap;
10657
protected DoublyLinkedList<Bucket> bucketList;
@@ -334,36 +285,11 @@ public StreamSummary(byte[] bytes) throws IOException, ClassNotFoundException
334285

335286
public void fromBytes(byte[] bytes) throws IOException, ClassNotFoundException
336287
{
337-
ObjectInput oi = null;
338-
try
339-
{
340-
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
341-
oi = new ObjectInputStream(bais);
342-
readExternal(oi);
343-
}
344-
finally { if(oi != null) oi.close(); }
288+
readExternal(new ObjectInputStream(new ByteArrayInputStream(bytes)));
345289
}
346290

347291
public byte[] toBytes() throws IOException
348292
{
349-
byte[] bytes = null;
350-
ObjectOutput oo = null;
351-
ByteArrayOutputStream baos = null;
352-
353-
try
354-
{
355-
baos = new ByteArrayOutputStream();
356-
oo = new ObjectOutputStream(baos);
357-
this.writeExternal(oo);
358-
}
359-
finally { if(oo != null) oo.close(); }
360-
361-
try
362-
{
363-
bytes = baos.toByteArray();
364-
}
365-
catch (Exception e) { throw new IOException(e); }
366-
367-
return bytes;
293+
return ExternalizableUtil.toBytes(this);
368294
}
369295
}

src/main/java/com/clearspring/analytics/stream/cardinality/AdaptiveCounting.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.clearspring.analytics.stream.cardinality;
1818

1919
import java.io.Serializable;
20+
import java.util.Arrays;
2021

2122
import com.clearspring.analytics.hash.Lookup3Hash;
2223
import com.clearspring.analytics.util.IBuilder;
@@ -109,7 +110,8 @@ protected static byte rho(long x, int k)
109110
@Override
110111
public ICardinality merge(ICardinality... estimators) throws LogLogMergeException
111112
{
112-
return AdaptiveCounting.mergeEstimators(prepMerge(estimators));
113+
LogLog res = (LogLog)super.merge(estimators);
114+
return new AdaptiveCounting(res.M);
113115
}
114116

115117
/**
@@ -120,12 +122,11 @@ public ICardinality merge(ICardinality... estimators) throws LogLogMergeExceptio
120122
*/
121123
public static AdaptiveCounting mergeEstimators(LogLog... estimators) throws LogLogMergeException
122124
{
123-
AdaptiveCounting merged = null;
124-
125-
byte[] mergedBytes = mergeBytes(estimators);
126-
if(mergedBytes != null) merged = new AdaptiveCounting(mergedBytes);
127-
128-
return merged;
125+
if(estimators == null || estimators.length == 0)
126+
{
127+
return null;
128+
}
129+
return (AdaptiveCounting)estimators[0].merge(Arrays.copyOfRange(estimators, 1, estimators.length));
129130
}
130131

131132
public static class Builder implements IBuilder<ICardinality>, Serializable

src/main/java/com/clearspring/analytics/stream/cardinality/CountThenEstimate.java

+12-53
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,16 @@
1616

1717
package com.clearspring.analytics.stream.cardinality;
1818

19-
import java.io.ByteArrayInputStream;
20-
import java.io.ByteArrayOutputStream;
21-
import java.io.Externalizable;
22-
import java.io.IOException;
23-
import java.io.ObjectInput;
24-
import java.io.ObjectInputStream;
25-
import java.io.ObjectOutput;
26-
import java.io.ObjectOutputStream;
19+
import com.clearspring.analytics.util.ExternalizableUtil;
20+
import com.clearspring.analytics.util.IBuilder;
21+
22+
import java.io.*;
2723
import java.util.ArrayList;
24+
import java.util.Arrays;
2825
import java.util.HashSet;
2926
import java.util.List;
3027
import java.util.Set;
3128

32-
import com.clearspring.analytics.util.IBuilder;
33-
3429
/**
3530
* Exact -> Estimator cardinality counting
3631
*
@@ -106,14 +101,7 @@ public CountThenEstimate(int tippingPoint, IBuilder<ICardinality> builder)
106101
*/
107102
public CountThenEstimate(byte[] bytes) throws IOException, ClassNotFoundException
108103
{
109-
ObjectInput in = null;
110-
try
111-
{
112-
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
113-
in = new ObjectInputStream(bais);
114-
readExternal(in);
115-
}
116-
finally { if(in != null) in.close(); }
104+
readExternal(new ObjectInputStream(new ByteArrayInputStream(bytes)));
117105

118106
if(!tipped && builder.sizeof() <= bytes.length) tip();
119107
}
@@ -182,25 +170,7 @@ private void tip()
182170
@Override
183171
public byte[] getBytes() throws IOException
184172
{
185-
byte[] bytes = null;
186-
ObjectOutput out = null;
187-
ByteArrayOutputStream baos = null;
188-
189-
try
190-
{
191-
baos = new ByteArrayOutputStream();
192-
out = new ObjectOutputStream(baos);
193-
this.writeExternal(out);
194-
}
195-
finally { if(out != null) out.close(); }
196-
197-
try
198-
{
199-
bytes = baos.toByteArray();
200-
}
201-
catch (Exception e) { throw new IOException(e); }
202-
203-
return bytes;
173+
return ExternalizableUtil.toBytes(this);
204174
}
205175

206176
@SuppressWarnings("unchecked")
@@ -291,24 +261,13 @@ else if(estimator instanceof LogLog)
291261
@Override
292262
public ICardinality merge(ICardinality... estimators) throws CardinalityMergeException
293263
{
294-
int numEstimators = (estimators == null) ? 0 : estimators.length;
295-
CountThenEstimate[] ces = new CountThenEstimate[numEstimators+1];
296-
if(numEstimators > 0)
264+
if(estimators == null || estimators.length == 0)
297265
{
298-
for(int i=0; i<numEstimators; i++)
299-
{
300-
if(estimators[i] instanceof CountThenEstimate)
301-
{
302-
ces[i] = (CountThenEstimate)estimators[i];
303-
}
304-
else
305-
{
306-
throw new CountThenEstimateMergeException("Unable to merge CountThenEstimate with "+estimators[i].getClass().getName());
307-
}
308-
}
266+
return this;
309267
}
310-
ces[numEstimators] = this;
311-
return CountThenEstimate.mergeEstimators(ces);
268+
CountThenEstimate[] all = Arrays.copyOf(estimators, estimators.length + 1, CountThenEstimate[].class);
269+
all[all.length-1] = this;
270+
return mergeEstimators(all);
312271
}
313272

314273
/**

0 commit comments

Comments
 (0)