/
HashTables.java
168 lines (147 loc) · 6.12 KB
/
HashTables.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.datasketches.tuple;
import static java.lang.Math.ceil;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static org.apache.datasketches.HashOperations.hashInsertOnly;
import static org.apache.datasketches.HashOperations.hashSearch;
import static org.apache.datasketches.Util.MIN_LG_NOM_LONGS;
import static org.apache.datasketches.Util.ceilingPowerOf2;
import java.lang.reflect.Array;
@SuppressWarnings("unchecked")
class HashTables<S extends Summary> {
long[] hashTable_ = null;
S[] summaryTable_ = null;
int lgTableSize_ = 0;
int count_ = 0;
HashTables() { }
//must have valid entries
void fromSketch(final Sketch<S> sketch) {
count_ = sketch.getRetainedEntries();
lgTableSize_ = getLgTableSize(count_);
hashTable_ = new long[1 << lgTableSize_];
final SketchIterator<S> it = sketch.iterator();
while (it.next()) {
final long hash = it.getHash();
final int index = hashInsertOnly(hashTable_, lgTableSize_, hash);
final S mySummary = (S)it.getSummary().copy();
if (summaryTable_ == null) {
summaryTable_ = (S[]) Array.newInstance(mySummary.getClass(), 1 << lgTableSize_);
}
summaryTable_[index] = mySummary;
}
}
//must have valid entries
void fromSketch(final org.apache.datasketches.theta.Sketch sketch, final S summary) {
count_ = sketch.getRetainedEntries(true);
lgTableSize_ = getLgTableSize(count_);
hashTable_ = new long[1 << lgTableSize_];
final org.apache.datasketches.theta.HashIterator it = sketch.iterator();
while (it.next()) {
final long hash = it.get();
final int index = hashInsertOnly(hashTable_, lgTableSize_, hash);
final S mySummary = (S)summary.copy();
if (summaryTable_ == null) {
summaryTable_ = (S[]) Array.newInstance(mySummary.getClass(), 1 << lgTableSize_);
}
summaryTable_[index] = mySummary;
}
}
private void fromArrays(final long[] hashArr, final S[] summaryArr, final int count) {
count_ = count;
lgTableSize_ = getLgTableSize(count);
summaryTable_ = null;
hashTable_ = new long[1 << lgTableSize_];
for (int i = 0; i < count; i++) {
final long hash = hashArr[i];
final int index = hashInsertOnly(hashTable_, lgTableSize_, hash);
final S mySummary = summaryArr[i];
if (summaryTable_ == null) {
summaryTable_ = (S[]) Array.newInstance(mySummary.getClass(), 1 << lgTableSize_);
}
summaryTable_[index] = summaryArr[i];
}
}
//For Tuple Sketches
HashTables<S> getIntersectHashTables(
final Sketch<S> nextTupleSketch,
final long thetaLong,
final SummarySetOperations<S> summarySetOps) {
//Match nextSketch data with local instance data, filtering by theta
final int maxMatchSize = min(count_, nextTupleSketch.getRetainedEntries());
final long[] matchHashArr = new long[maxMatchSize];
final S[] matchSummariesArr = Util.newSummaryArray(summaryTable_, maxMatchSize);
int matchCount = 0;
final SketchIterator<S> it = nextTupleSketch.iterator();
while (it.next()) {
final long hash = it.getHash();
if (hash >= thetaLong) { continue; }
final int index = hashSearch(hashTable_, lgTableSize_, hash);
if (index < 0) { continue; }
//Copy the intersecting items from local hashTables_
// sequentially into local matchHashArr_ and matchSummaries_
matchHashArr[matchCount] = hash;
matchSummariesArr[matchCount] = summarySetOps.intersection(summaryTable_[index], it.getSummary());
matchCount++;
}
final HashTables<S> resultHT = new HashTables<>();
resultHT.fromArrays(matchHashArr, matchSummariesArr, matchCount);
return resultHT;
}
//For Theta Sketches
HashTables<S> getIntersectHashTables(
final org.apache.datasketches.theta.Sketch nextThetaSketch,
final long thetaLong,
final SummarySetOperations<S> summarySetOps,
final S summary) {
final Class<S> summaryType = (Class<S>) summary.getClass();
//Match nextSketch data with local instance data, filtering by theta
final int maxMatchSize = min(count_, nextThetaSketch.getRetainedEntries());
final long[] matchHashArr = new long[maxMatchSize];
final S[] matchSummariesArr = (S[]) Array.newInstance(summaryType, maxMatchSize);
int matchCount = 0;
final org.apache.datasketches.theta.HashIterator it = nextThetaSketch.iterator();
//scan B & search A(hashTable) for match
while (it.next()) {
final long hash = it.get();
if (hash >= thetaLong) { continue; }
final int index = hashSearch(hashTable_, lgTableSize_, hash);
if (index < 0) { continue; }
//Copy the intersecting items from local hashTables_
// sequentially into local matchHashArr_ and matchSummaries_
matchHashArr[matchCount] = hash;
matchSummariesArr[matchCount] = summarySetOps.intersection(summaryTable_[index], summary);
matchCount++;
}
final HashTables<S> resultHT = new HashTables<>();
resultHT.fromArrays(matchHashArr, matchSummariesArr, matchCount);
return resultHT;
}
void clear() {
hashTable_ = null;
summaryTable_ = null;
lgTableSize_ = 0;
count_ = 0;
}
static int getLgTableSize(final int count) {
final int tableSize = max(ceilingPowerOf2((int) ceil(count / 0.75)), 1 << MIN_LG_NOM_LONGS);
return Integer.numberOfTrailingZeros(tableSize);
}
}