list = getBinaryDecomposition(0, 16);
135 | for (int i = 0; i < list.size(); i++)
136 | {
137 | System.out.println(list.get(i).getFirst() + ": " + list.get(i).getSecond());
138 | }
139 | // System.out.println(getBinaryLevel(0, 7));
140 | }
141 | }
--------------------------------------------------------------------------------
/src/main/java/edu/ecnu/pbf/util/DataGen.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf.util;
2 |
3 | import java.io.FileWriter;
4 | import java.io.IOException;
5 |
6 | public class DataGen
7 | {
8 | public static void GenStringToFile(String filePrefix, int fileNum,
9 | int strLength, int fileSize)
10 | {
11 | for (int i = 1; i <= fileNum; i++)
12 | {
13 | String outFileName = filePrefix + i;// args[0];
14 | FileWriter writer = null;
15 |
16 | try
17 | {
18 | writer = new FileWriter(outFileName, false);
19 | String tempString = null;
20 | //int recordNum = 2000000; // Integer.parseInt(args[1]);
21 | for (int start = 0; start < fileSize; start++)
22 | {
23 | tempString = RandomGenerator.getRandomString(strLength);
24 | writer.append(tempString);
25 | writer.append("\r\n");
26 | }
27 | }
28 | catch (Exception e)
29 | {
30 | e.printStackTrace();
31 | }
32 | finally
33 | {
34 | if (writer != null)
35 | {
36 | try
37 | {
38 | writer.close();
39 | }
40 | catch (IOException e1)
41 | {
42 | }
43 | }
44 | }
45 | }
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/edu/ecnu/pbf/util/MathUtil.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf.util;
2 |
3 | public class MathUtil
4 | {
5 | public static int gcd(int a, int b)
6 | {
7 | while (b != 0)
8 | {
9 | int temp = a % b;
10 | a = b;
11 | b = temp;
12 | }
13 | return a;
14 | }
15 |
16 | public static int getRandomIndex(int start, int end)
17 | {
18 | return start + (int)(Math.random() * (end - start + 1));
19 | }
20 |
21 | public static boolean isHitPercent(int percent)
22 | {
23 | boolean result = false;
24 | int randomPercent = (int) (Math.random() * 100);
25 | if (randomPercent < percent)
26 | {
27 | result = true;
28 | }
29 |
30 | return result;
31 | }
32 |
33 | public static void main(String[] args)
34 | {
35 | for (int i = 0; i < 100; i++)
36 | {
37 | System.out.println(isHitPercent(2));
38 | }
39 | int[] a = getGeometricSequence(100, 4, 1.4);
40 | for (int i = 0; i < a.length; i++)
41 | {
42 | System.out.println(a[i]);
43 | }
44 | System.out.println(gcd(0, 100));
45 | }
46 |
47 | public static int[] getGeometricSequence(int sum, int length, double radio)
48 | {
49 | int[] result = new int[length];
50 | int a = (int)((double)sum * (1D - radio) / (1D - Math.pow(radio, length)));
51 | for (int i = 0; i < length; i++)
52 | {
53 | result[i] = a;
54 | a = (int)(a * radio);
55 | }
56 | return result;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/edu/ecnu/pbf/util/MurmurHash3.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf.util;
2 |
3 | /**
4 | * The MurmurHash3 algorithm was created by Austin Appleby and placed in the public domain.
5 | * This java port was authored by Yonik Seeley and also placed into the public domain.
6 | * The author hereby disclaims copyright to this source code.
7 | *
8 | * This produces exactly the same hash values as the final C++
9 | * version of MurmurHash3 and is thus suitable for producing the same hash values across
10 | * platforms.
11 | *
12 | * The 32 bit x86 version of this hash should be the fastest variant for relatively short keys like ids.
13 | * murmurhash3_x64_128 is a good choice for longer strings or if you need more than 32 bits of hash.
14 | *
15 | * Note - The x86 and x64 versions do _not_ produce the same results, as the
16 | * algorithms are optimized for their respective platforms.
17 | *
18 | * See http://github.com/yonik/java_util for future updates to this file.
19 | */
20 | public final class MurmurHash3 {
21 |
22 | /** 128 bits of state */
23 | public static final class LongPair {
24 | public long val1;
25 | public long val2;
26 | }
27 |
28 | public static final int fmix32(int h) {
29 | h ^= h >>> 16;
30 | h *= 0x85ebca6b;
31 | h ^= h >>> 13;
32 | h *= 0xc2b2ae35;
33 | h ^= h >>> 16;
34 | return h;
35 | }
36 |
37 | public static final long fmix64(long k) {
38 | k ^= k >>> 33;
39 | k *= 0xff51afd7ed558ccdL;
40 | k ^= k >>> 33;
41 | k *= 0xc4ceb9fe1a85ec53L;
42 | k ^= k >>> 33;
43 | return k;
44 | }
45 |
46 | /** Gets a long from a byte buffer in little endian byte order. */
47 | public static final long getLongLittleEndian(byte[] buf, int offset) {
48 | return ((long)buf[offset+7] << 56) // no mask needed
49 | | ((buf[offset+6] & 0xffL) << 48)
50 | | ((buf[offset+5] & 0xffL) << 40)
51 | | ((buf[offset+4] & 0xffL) << 32)
52 | | ((buf[offset+3] & 0xffL) << 24)
53 | | ((buf[offset+2] & 0xffL) << 16)
54 | | ((buf[offset+1] & 0xffL) << 8)
55 | | ((buf[offset ] & 0xffL)); // no shift needed
56 | }
57 |
58 |
59 | /** Returns the MurmurHash3_x86_32 hash. */
60 | public static int murmurhash3_x86_32(byte[] data, int offset, int len, int seed) {
61 |
62 | final int c1 = 0xcc9e2d51;
63 | final int c2 = 0x1b873593;
64 |
65 | int h1 = seed;
66 | int roundedEnd = offset + (len & 0xfffffffc); // round down to 4 byte block
67 |
68 | for (int i=offset; i>> 17); // ROTL32(k1,15);
73 | k1 *= c2;
74 |
75 | h1 ^= k1;
76 | h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13);
77 | h1 = h1*5+0xe6546b64;
78 | }
79 |
80 | // tail
81 | int k1 = 0;
82 |
83 | switch(len & 0x03) {
84 | case 3:
85 | k1 = (data[roundedEnd + 2] & 0xff) << 16;
86 | // fallthrough
87 | case 2:
88 | k1 |= (data[roundedEnd + 1] & 0xff) << 8;
89 | // fallthrough
90 | case 1:
91 | k1 |= (data[roundedEnd] & 0xff);
92 | k1 *= c1;
93 | k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
94 | k1 *= c2;
95 | h1 ^= k1;
96 | }
97 |
98 | // finalization
99 | h1 ^= len;
100 |
101 | // fmix(h1);
102 | h1 ^= h1 >>> 16;
103 | h1 *= 0x85ebca6b;
104 | h1 ^= h1 >>> 13;
105 | h1 *= 0xc2b2ae35;
106 | h1 ^= h1 >>> 16;
107 |
108 | return h1;
109 | }
110 |
111 |
112 | /** Returns the MurmurHash3_x86_32 hash of the UTF-8 bytes of the String without actually encoding
113 | * the string to a temporary buffer. This is more than 2x faster than hashing the result
114 | * of String.getBytes().
115 | */
116 | public static int murmurhash3_x86_32(CharSequence data, int offset, int len, int seed) {
117 |
118 | final int c1 = 0xcc9e2d51;
119 | final int c2 = 0x1b873593;
120 |
121 | int h1 = seed;
122 |
123 | int pos = offset;
124 | int end = offset + len;
125 | int k1 = 0;
126 | int k2 = 0;
127 | int shift = 0;
128 | int bits = 0;
129 | int nBytes = 0; // length in UTF8 bytes
130 |
131 |
132 | while (pos < end) {
133 | int code = data.charAt(pos++);
134 | if (code < 0x80) {
135 | k2 = code;
136 | bits = 8;
137 |
138 | /***
139 | // optimized ascii implementation (currently slower!!! code size?)
140 | if (shift == 24) {
141 | k1 = k1 | (code << 24);
142 |
143 | k1 *= c1;
144 | k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
145 | k1 *= c2;
146 |
147 | h1 ^= k1;
148 | h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13);
149 | h1 = h1*5+0xe6546b64;
150 |
151 | shift = 0;
152 | nBytes += 4;
153 | k1 = 0;
154 | } else {
155 | k1 |= code << shift;
156 | shift += 8;
157 | }
158 | continue;
159 | ***/
160 |
161 | }
162 | else if (code < 0x800) {
163 | k2 = (0xC0 | (code >> 6))
164 | | ((0x80 | (code & 0x3F)) << 8);
165 | bits = 16;
166 | }
167 | else if (code < 0xD800 || code > 0xDFFF || pos>=end) {
168 | // we check for pos>=end to encode an unpaired surrogate as 3 bytes.
169 | k2 = (0xE0 | (code >> 12))
170 | | ((0x80 | ((code >> 6) & 0x3F)) << 8)
171 | | ((0x80 | (code & 0x3F)) << 16);
172 | bits = 24;
173 | } else {
174 | // surrogate pair
175 | // int utf32 = pos < end ? (int) data.charAt(pos++) : 0;
176 | int utf32 = (int) data.charAt(pos++);
177 | utf32 = ((code - 0xD7C0) << 10) + (utf32 & 0x3FF);
178 | k2 = (0xff & (0xF0 | (utf32 >> 18)))
179 | | ((0x80 | ((utf32 >> 12) & 0x3F))) << 8
180 | | ((0x80 | ((utf32 >> 6) & 0x3F))) << 16
181 | | (0x80 | (utf32 & 0x3F)) << 24;
182 | bits = 32;
183 | }
184 |
185 |
186 | k1 |= k2 << shift;
187 |
188 | // int used_bits = 32 - shift; // how many bits of k2 were used in k1.
189 | // int unused_bits = bits - used_bits; // (bits-(32-shift)) == bits+shift-32 == bits-newshift
190 |
191 | shift += bits;
192 | if (shift >= 32) {
193 | // mix after we have a complete word
194 |
195 | k1 *= c1;
196 | k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
197 | k1 *= c2;
198 |
199 | h1 ^= k1;
200 | h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13);
201 | h1 = h1*5+0xe6546b64;
202 |
203 | shift -= 32;
204 | // unfortunately, java won't let you shift 32 bits off, so we need to check for 0
205 | if (shift != 0) {
206 | k1 = k2 >>> (bits-shift); // bits used == bits - newshift
207 | } else {
208 | k1 = 0;
209 | }
210 | nBytes += 4;
211 | }
212 |
213 | } // inner
214 |
215 | // handle tail
216 | if (shift > 0) {
217 | nBytes += shift >> 3;
218 | k1 *= c1;
219 | k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
220 | k1 *= c2;
221 | h1 ^= k1;
222 | }
223 |
224 | // finalization
225 | h1 ^= nBytes;
226 |
227 | // fmix(h1);
228 | h1 ^= h1 >>> 16;
229 | h1 *= 0x85ebca6b;
230 | h1 ^= h1 >>> 13;
231 | h1 *= 0xc2b2ae35;
232 | h1 ^= h1 >>> 16;
233 |
234 | return h1;
235 | }
236 |
237 |
238 | /** Returns the MurmurHash3_x64_128 hash, placing the result in "out". */
239 | public static void murmurhash3_x64_128(byte[] key, int offset, int len, int seed, LongPair out) {
240 | // The original algorithm does have a 32 bit unsigned seed.
241 | // We have to mask to match the behavior of the unsigned types and prevent sign extension.
242 | long h1 = seed & 0x00000000FFFFFFFFL;
243 | long h2 = seed & 0x00000000FFFFFFFFL;
244 |
245 | final long c1 = 0x87c37b91114253d5L;
246 | final long c2 = 0x4cf5ad432745937fL;
247 |
248 | int roundedEnd = offset + (len & 0xFFFFFFF0); // round down to 16 byte block
249 | for (int i=offset; i 0 && result > kMax)
16 | {
17 | result = kMax;
18 | }
19 | return result;
20 | }
21 |
22 | public static void main(String[] args)
23 | {
24 | System.out.println(getOptimizedK(100000, 12500, 12));
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/edu/ecnu/pbf/util/PbfUtil.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf.util;
2 |
3 | import java.util.BitSet;
4 |
5 | import org.apache.commons.math3.analysis.UnivariateFunction;
6 | import org.apache.commons.math3.analysis.solvers.BisectionSolver;
7 |
8 | import edu.ecnu.pbf.CommonConstants;
9 | import edu.ecnu.pbf.base.impl.TemporalRangeBloomFilterV2;
10 |
11 | public class PbfUtil
12 | {
13 | public static final double INVARIANT = Math.log(2) * Math.log(2);
14 |
15 | public static int[] getOptimizedMForBeta1(int m, int[] d, int[] f, int queryLength)
16 | {
17 | int[] optimizedM = null;
18 | if (d.length != f.length)
19 | {
20 | // TODO
21 | }
22 | else
23 | {
24 | double absoluteAccuracy = 0;
25 | // if (queryLength <= 16)
26 | // {
27 | // absoluteAccuracy = 1E-43;
28 | // }
29 | // else if (queryLength <= 32)
30 | // {
31 | // absoluteAccuracy = 1E-36;
32 | // }
33 | // else if (queryLength <= 64)
34 | // {
35 | // absoluteAccuracy = 1E-32;
36 | // }
37 | // else
38 | // {
39 | // absoluteAccuracy = 1E-30;
40 | // }
41 | absoluteAccuracy = 1E-93; // 93 for beta1, 54 for beta2
42 | optimizedM = new int[d.length];
43 | LamdaFunction lamdaFunc = new LamdaFunction(m, d, f);
44 | BisectionSolver solver = new BisectionSolver(absoluteAccuracy);
45 | double lamda = solver.solve(40000, lamdaFunc, -1, 0);
46 | // System.out.println("lamda: " + lamda);
47 | // System.out.println(lamda);
48 | for (int i = 0; i < optimizedM.length; i++)
49 | {
50 | if (f[i] == 0 || d[i] == 0)
51 | {
52 | optimizedM[i] = 0;
53 | }
54 | else
55 | {
56 | optimizedM[i] = (int) ((double) d[i] / INVARIANT
57 | * Math.log(1.0D - (double) f[i] * PbfUtil.INVARIANT / lamda / d[i]));
58 | }
59 | }
60 | }
61 | return optimizedM;
62 | }
63 |
64 | public static int[] getOptimizedM(int m, int[] d, int[] f, int queryLength, double acc)
65 | {
66 | int[] optimizedM = null;
67 | if (d.length != f.length)
68 | {
69 | // TODO
70 | }
71 | else
72 | {
73 | double absoluteAccuracy = 0;
74 |
75 | absoluteAccuracy = acc; // 93 for beta1, 54 for beta2
76 | optimizedM = new int[d.length];
77 | LamdaFunction lamdaFunc = new LamdaFunction(m, d, f);
78 | BisectionSolver solver = new BisectionSolver(absoluteAccuracy);
79 | double lamda = solver.solve(40000, lamdaFunc, -1, 0);
80 |
81 | for (int i = 0; i < optimizedM.length; i++)
82 | {
83 | if (f[i] == 0 || d[i] == 0)
84 | {
85 | optimizedM[i] = 0;
86 | }
87 | else
88 | {
89 | optimizedM[i] = (int) ((double) d[i] / INVARIANT
90 | * Math.log(1.0D - (double) f[i] * PbfUtil.INVARIANT / lamda / d[i]));
91 | }
92 | }
93 | }
94 | return optimizedM;
95 | }
96 |
97 | public static int[] getOptimizedMForBeta2(int m, int[] d, int[] f, int queryLength)
98 | {
99 | int[] optimizedM = null;
100 | if (d.length != f.length)
101 | {
102 | // TODO
103 | }
104 | else
105 | {
106 | double absoluteAccuracy = 0;
107 |
108 | absoluteAccuracy = 1E-53; // 93 for beta1, 54 for beta2
109 | optimizedM = new int[d.length];
110 | LamdaFunction lamdaFunc = new LamdaFunction(m, d, f);
111 | BisectionSolver solver = new BisectionSolver(absoluteAccuracy);
112 | double lamda = solver.solve(40000, lamdaFunc, -1, 0);
113 |
114 | for (int i = 0; i < optimizedM.length; i++)
115 | {
116 | if (f[i] == 0 || d[i] == 0)
117 | {
118 | optimizedM[i] = 0;
119 | }
120 | else
121 | {
122 | optimizedM[i] = (int) ((double) d[i] / INVARIANT
123 | * Math.log(1.0D - (double) f[i] * PbfUtil.INVARIANT / lamda / d[i]));
124 | }
125 | }
126 | }
127 | return optimizedM;
128 | }
129 |
130 | public static int[] getOptimizedKForBeta1(int[] m, int[] d, int kMax)
131 | {
132 | int[] optimizedK = null;
133 | if (m.length != d.length)
134 | {
135 | // TODO
136 | }
137 | else
138 | {
139 | optimizedK = new int[m.length];
140 | for (int i = 0; i < optimizedK.length; i++)
141 | {
142 | if (m[i] == 0 || d[i] == 0)
143 | {
144 | optimizedK[i] = 0;
145 | }
146 | else
147 | {
148 | optimizedK[i] = (int) ((double) m[i] / d[i] * Math.log(2) + 1) + 1;
149 | if (kMax > 0 && optimizedK[i] > kMax)
150 | {
151 | optimizedK[i] = kMax;
152 | }
153 | }
154 | }
155 | }
156 | return optimizedK;
157 | }
158 |
159 | /**
160 | * Get the total number of levels.
161 | *
162 | * @param maxT
163 | * from 0
164 | * @return
165 | */
166 | public static int getLevelNum(int maxT)
167 | {
168 | int result = 0;
169 | for (int i = 0; i < CommonConstants.g.length; i++)
170 | {
171 | if ((maxT + 1) <= CommonConstants.g[i])
172 | {
173 | result = i + 1;
174 | break;
175 | }
176 | }
177 | return result;
178 | }
179 |
180 | /**
181 | * Merge the BitSets of two consecutive beta2 pbfs into one.
182 | * Note: the structures of the two pbfs should be the same, i.e., hash numbers and level numbers.
183 | * @param b1
184 | * @param b2
185 | * @return
186 | */
187 | public static BitSet mergeBeta2(TemporalRangeBloomFilterV2 rbf1, TemporalRangeBloomFilterV2 rbf2)
188 | {
189 | BitSet bs1 = rbf1.getBitSet();
190 | BitSet bs2 = rbf2.getBitSet();
191 | int bitNumOfRbf1 = rbf1.getSize();
192 | int bitNumOfRbf2 = rbf2.getSize();
193 | int bitNum = MathUtil.gcd(bitNumOfRbf1, bitNumOfRbf2);
194 | BitSet bitSet = new BitSet(bitNum);
195 | for (int i = 0; i < bitNumOfRbf1; i++)
196 | {
197 | if (bs1.get(i) == true)
198 | {
199 | bitSet.set(i % bitNum);
200 | }
201 | }
202 | for (int i = 0; i < bitNumOfRbf2; i++)
203 | {
204 | if (bs2.get(i) == true)
205 | {
206 | bitSet.set(i % bitNum);
207 | }
208 | }
209 | return bitSet;
210 | }
211 |
212 | public static BitSet merge(BitSet bs1, int bitNumOfBs1, BitSet bs2, int bitNumOfBs2)
213 | {
214 | int bitNum = MathUtil.gcd(bitNumOfBs1, bitNumOfBs2);
215 | BitSet bitSet = new BitSet(bitNum);
216 | for (int i = 0; i < bitNumOfBs1; i++)
217 | {
218 | if (bs1.get(i) == true)
219 | {
220 | bitSet.set(i % bitNum);
221 | }
222 | }
223 | for (int i = 0; i < bitNumOfBs2; i++)
224 | {
225 | if (bs2.get(i) == true)
226 | {
227 | bitSet.set(i % bitNum);
228 | }
229 | }
230 | return bitSet;
231 | }
232 |
233 | public static void main(String[] args)
234 | {
235 | System.out.println(getLevelNum(127));
236 | int m = 10000000;
237 | int[] d = {1000, 2000, 4000, 8000, 16000, 32000, 64000, 12800};
238 | int[] f = {2, 2, 2, 2, 2, 2, 2, 1};
239 |
240 | int[] mm = getOptimizedMForBeta1(m, d, f, 128);
241 | int sum = 0;
242 |
243 | for (int i = 0; i < mm.length; i++)
244 | {
245 | System.out.println(mm[i]);
246 | sum += mm[i];
247 | }
248 |
249 | System.out.println(sum);
250 |
251 | }
252 | }
253 |
254 | class LamdaFunction implements UnivariateFunction
255 | {
256 | private int[] d;
257 | private int[] f;
258 | private long m;
259 |
260 | public LamdaFunction(long m, int[] d, int[] f)
261 | {
262 | this.m = m;
263 | this.d = d;
264 | this.f = f;
265 | }
266 |
267 | public double value(double x)
268 | {
269 | double result = 0;
270 | for (int i = 0; i < d.length; i++)
271 | {
272 | if (f[i] == 0 || d[i] == 0)
273 | {
274 |
275 | }
276 | else
277 | {
278 | result = result + (double) d[i]
279 | * Math.log(1.0D - (double) f[i] * PbfUtil.INVARIANT / x / d[i]);
280 | }
281 | }
282 | result = result - (double) m * PbfUtil.INVARIANT;
283 | return result;
284 | }
285 | }
--------------------------------------------------------------------------------
/src/main/java/edu/ecnu/pbf/util/PbfUtil2.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf.util;
2 |
3 | import java.util.BitSet;
4 |
5 | import org.apache.commons.math3.analysis.UnivariateFunction;
6 | import org.apache.commons.math3.analysis.solvers.BisectionSolver;
7 |
8 | import edu.ecnu.pbf.CommonConstants;
9 | import edu.ecnu.pbf.base.impl.TemporalRangeBloomFilterV2;
10 |
11 | public class PbfUtil2
12 | {
13 | public static final double INVARIANT = Math.log(2) * Math.log(2);
14 |
15 | public static int[] getOptimizedMForBeta1(int m, int[] d, int[] f, int queryLength)
16 | {
17 | int[] optimizedM = null;
18 | if (d.length != f.length)
19 | {
20 | // TODO
21 | }
22 | else
23 | {
24 | double absoluteAccuracy = 0;
25 | if (queryLength <= 16)
26 | {
27 | absoluteAccuracy = 1E-43;
28 | }
29 | else if (queryLength <= 32)
30 | {
31 | absoluteAccuracy = 1E-36;
32 | }
33 | else if (queryLength <= 64)
34 | {
35 | absoluteAccuracy = 1E-32;
36 | }
37 | else
38 | {
39 | absoluteAccuracy = 1E-30;
40 | }
41 | optimizedM = new int[d.length];
42 | LamdaFunction lamdaFunc = new LamdaFunction(m, d, f);
43 | BisectionSolver solver = new BisectionSolver(absoluteAccuracy);
44 | double lamda = solver.solve(20000, lamdaFunc, -1, 0);
45 | System.out.println("lamda: " + lamda);
46 | System.out.println(lamda);
47 | for (int i = 0; i < optimizedM.length; i++)
48 | {
49 | if (f[i] == 0 || d[i] == 0)
50 | {
51 | optimizedM[i] = 0;
52 | }
53 | else
54 | {
55 | optimizedM[i] = (int) ((double) d[i] / INVARIANT
56 | * Math.log(1.0D - (double) f[i] * PbfUtil2.INVARIANT / lamda / d[i]));
57 | }
58 | }
59 | }
60 | return optimizedM;
61 | }
62 |
63 | public static int[] getOptimizedKForBeta1(int[] m, int[] d, int kMax)
64 | {
65 | int[] optimizedK = null;
66 | if (m.length != d.length)
67 | {
68 | // TODO
69 | }
70 | else
71 | {
72 | optimizedK = new int[m.length];
73 | for (int i = 0; i < optimizedK.length; i++)
74 | {
75 | if (m[i] == 0 || d[i] == 0)
76 | {
77 | optimizedK[i] = 0;
78 | }
79 | else
80 | {
81 | optimizedK[i] = (int) ((double) m[i] / d[i] * Math.log(2) + 1) + 1;
82 | if (kMax > 0 && optimizedK[i] > kMax)
83 | {
84 | optimizedK[i] = kMax;
85 | }
86 | }
87 | }
88 | }
89 | return optimizedK;
90 | }
91 |
92 | /**
93 | * Get the total number of levels.
94 | *
95 | * @param maxT
96 | * from 0
97 | * @return
98 | */
99 | public static int getLevelNum(int maxT)
100 | {
101 | int result = 0;
102 | for (int i = 0; i < CommonConstants.g.length; i++)
103 | {
104 | if ((maxT + 1) <= CommonConstants.g[i])
105 | {
106 | result = i + 1;
107 | break;
108 | }
109 | }
110 | return result;
111 | }
112 |
113 | /**
114 | * Merge the BitSets of two consecutive beta2 pbfs into one.
115 | * Note: the structures of the two pbfs should be the same, i.e., hash numbers and level numbers.
116 | * @param b1
117 | * @param b2
118 | * @return
119 | */
120 | public static BitSet mergeBeta2(TemporalRangeBloomFilterV2 rbf1, TemporalRangeBloomFilterV2 rbf2)
121 | {
122 | BitSet bs1 = rbf1.getBitSet();
123 | BitSet bs2 = rbf2.getBitSet();
124 | int bitNumOfRbf1 = rbf1.getSize();
125 | int bitNumOfRbf2 = rbf2.getSize();
126 | int bitNum = MathUtil.gcd(bitNumOfRbf1, bitNumOfRbf2);
127 | BitSet bitSet = new BitSet(bitNum);
128 | for (int i = 0; i < bitNumOfRbf1; i++)
129 | {
130 | if (bs1.get(i) == true)
131 | {
132 | bitSet.set(i % bitNum);
133 | }
134 | }
135 | for (int i = 0; i < bitNumOfRbf2; i++)
136 | {
137 | if (bs2.get(i) == true)
138 | {
139 | bitSet.set(i % bitNum);
140 | }
141 | }
142 | return bitSet;
143 | }
144 |
145 | public static BitSet merge(BitSet bs1, int bitNumOfBs1, BitSet bs2, int bitNumOfBs2)
146 | {
147 | int bitNum = MathUtil.gcd(bitNumOfBs1, bitNumOfBs2);
148 | BitSet bitSet = new BitSet(bitNum);
149 | for (int i = 0; i < bitNumOfBs1; i++)
150 | {
151 | if (bs1.get(i) == true)
152 | {
153 | bitSet.set(i % bitNum);
154 | }
155 | }
156 | for (int i = 0; i < bitNumOfBs2; i++)
157 | {
158 | if (bs2.get(i) == true)
159 | {
160 | bitSet.set(i % bitNum);
161 | }
162 | }
163 | return bitSet;
164 | }
165 |
166 | public static void main(String[] args)
167 | {
168 | System.out.println(getLevelNum(127));
169 | int m = 10000000;
170 | int[] d = {1000, 2000, 4000, 8000, 16000, 32000, 64000, 12800};
171 | int[] f = {2, 2, 2, 2, 2, 2, 2, 1};
172 |
173 | int[] mm = getOptimizedMForBeta1(m, d, f, 128);
174 | int sum = 0;
175 |
176 | for (int i = 0; i < mm.length; i++)
177 | {
178 | System.out.println(mm[i]);
179 | sum += mm[i];
180 | }
181 |
182 | System.out.println(sum);
183 |
184 | }
185 | }
186 |
187 | //class LamdaFunction implements UnivariateFunction
188 | //{
189 | // private int[] d;
190 | // private int[] f;
191 | // private long m;
192 | //
193 | // public LamdaFunction(long m, int[] d, int[] f)
194 | // {
195 | // this.m = m;
196 | // this.d = d;
197 | // this.f = f;
198 | // }
199 | //
200 | // public double value(double x)
201 | // {
202 | // double result = 0;
203 | // for (int i = 0; i < d.length; i++)
204 | // {
205 | // if (f[i] == 0 || d[i] == 0)
206 | // {
207 | //
208 | // }
209 | // else
210 | // {
211 | // result = result + (double) d[i]
212 | // * Math.log(1.0D - (double) f[i] * PbfUtil2.INVARIANT / x / d[i]);
213 | // }
214 | // }
215 | // result = result - (double) m * PbfUtil2.INVARIANT;
216 | // return result;
217 | // }
218 | //}
--------------------------------------------------------------------------------
/src/main/java/edu/ecnu/pbf/util/RandomGenerator.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf.util;
2 |
3 | import java.util.Random;
4 |
5 | public class RandomGenerator
6 | {
7 | public static String getRandomString(int length)
8 | {
9 | String base = "abcdefghijklmnopqrstuvwxyz";
10 | Random random = new Random();
11 | StringBuffer sb = new StringBuffer();
12 | for (int i = 0; i < length; i++)
13 | {
14 | int number = random.nextInt(base.length());
15 | sb.append(base.charAt(number));
16 | }
17 | return sb.toString();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/edu/ecnu/pbf/util/ResultUtil.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf.util;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.StringTokenizer;
6 |
7 | public class ResultUtil
8 | {
9 | public static double handleResult(ArrayList resultArray, int start, int number)
10 | {
11 | double result = 0;
12 | Collections.sort(resultArray);
13 | int s = start > 0 ? start : 0;
14 | number = number > 0 ? number : 1;
15 | int e = s + number - 1;
16 | e = e < resultArray.size() ? e : resultArray.size() - 1;
17 | double sum = 0;
18 | for (int i = s; i <= e; i++)
19 | {
20 | sum = sum + resultArray.get(i);
21 | }
22 | result = sum / number;
23 | return result;
24 | }
25 |
26 | public static String handleDouble(double input, int decimalPlace)
27 | {
28 | String result = "";
29 | StringTokenizer tokenizer = new StringTokenizer(String.valueOf(input), ".");
30 | if (tokenizer.hasMoreTokens())
31 | {
32 | result = tokenizer.nextToken();
33 | }
34 | if (tokenizer.hasMoreTokens())
35 | {
36 | decimalPlace = decimalPlace > 0 ? decimalPlace : 0;
37 | String decimalStr = tokenizer.nextToken();
38 | decimalPlace = decimalPlace <= decimalStr.length() ? decimalPlace : decimalStr.length();
39 | result = result + "." + decimalStr.substring(0, decimalPlace);
40 | }
41 | return result;
42 | }
43 |
44 | public static void main(String[] args)
45 | {
46 | double a = 12.234567812;
47 | System.out.println(handleDouble(a, 0));
48 | System.out.println(handleDouble(a, 1));
49 | System.out.println(handleDouble(a, 2));
50 | System.out.println(handleDouble(a, 3));
51 | System.out.println(handleDouble(a, 4));
52 | System.out.println(handleDouble(a, 8));
53 | System.out.println(handleDouble(a, 9));
54 | System.out.println(handleDouble(a, 10));
55 | System.out.println(handleDouble(a, 20));
56 |
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/test/java/edu/ecnu/pbf/AppTest.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | /**
8 | * Unit test for simple App.
9 | */
10 | public class AppTest
11 | extends TestCase
12 | {
13 | /**
14 | * Create the test case
15 | *
16 | * @param testName name of the test case
17 | */
18 | public AppTest( String testName )
19 | {
20 | super( testName );
21 | }
22 |
23 | /**
24 | * @return the suite of tests being tested
25 | */
26 | public static Test suite()
27 | {
28 | return new TestSuite( AppTest.class );
29 | }
30 |
31 | /**
32 | * Rigourous Test :-)
33 | */
34 | public void testApp()
35 | {
36 | assertTrue( true );
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/test/java/edu/ecnu/pbf/Beta1Test.java:
--------------------------------------------------------------------------------
1 | package edu.ecnu.pbf;
2 |
3 | public class Beta1Test
4 | {
5 |
6 | }
7 |
--------------------------------------------------------------------------------