forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractCachedBuildSideMatchDriver.java
173 lines (139 loc) · 7.17 KB
/
AbstractCachedBuildSideMatchDriver.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
169
170
171
172
173
/***********************************************************************************************************************
* Copyright (C) 2010-2013 by the Stratosphere project (http:https://stratosphere.eu)
*
* 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:https://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 eu.stratosphere.pact.runtime.task;
import java.util.List;
import eu.stratosphere.api.common.functions.GenericJoiner;
import eu.stratosphere.api.common.typeutils.TypeComparator;
import eu.stratosphere.api.common.typeutils.TypePairComparatorFactory;
import eu.stratosphere.api.common.typeutils.TypeSerializer;
import eu.stratosphere.core.memory.MemorySegment;
import eu.stratosphere.pact.runtime.hash.MutableHashTable;
import eu.stratosphere.pact.runtime.task.util.TaskConfig;
import eu.stratosphere.pact.runtime.util.EmptyMutableObjectIterator;
import eu.stratosphere.util.Collector;
import eu.stratosphere.util.MutableObjectIterator;
public abstract class AbstractCachedBuildSideMatchDriver<IT1, IT2, OT> extends MatchDriver<IT1, IT2, OT> implements ResettablePactDriver<GenericJoiner<IT1, IT2, OT>, OT> {
/**
* We keep it without generic parameters, because they vary depending on which input is the build side.
*/
protected volatile MutableHashTable<?, ?> hashJoin;
private final int buildSideIndex;
private final int probeSideIndex;
protected AbstractCachedBuildSideMatchDriver(int buildSideIndex, int probeSideIndex) {
this.buildSideIndex = buildSideIndex;
this.probeSideIndex = probeSideIndex;
}
// --------------------------------------------------------------------------------------------
@Override
public boolean isInputResettable(int inputNum) {
if (inputNum < 0 || inputNum > 1) {
throw new IndexOutOfBoundsException();
}
return inputNum == buildSideIndex;
}
@Override
public void initialize() throws Exception {
TaskConfig config = this.taskContext.getTaskConfig();
TypeSerializer<IT1> serializer1 = this.taskContext.getInputSerializer(0);
TypeSerializer<IT2> serializer2 = this.taskContext.getInputSerializer(1);
TypeComparator<IT1> comparator1 = this.taskContext.getInputComparator(0);
TypeComparator<IT2> comparator2 = this.taskContext.getInputComparator(1);
MutableObjectIterator<IT1> input1 = this.taskContext.getInput(0);
MutableObjectIterator<IT2> input2 = this.taskContext.getInput(1);
TypePairComparatorFactory<IT1, IT2> pairComparatorFactory =
this.taskContext.getTaskConfig().getPairComparatorFactory(this.taskContext.getUserCodeClassLoader());
int numMemoryPages = this.taskContext.getMemoryManager().computeNumberOfPages(config.getMemoryDriver());
List<MemorySegment> memSegments = this.taskContext.getMemoryManager().allocatePages(
this.taskContext.getOwningNepheleTask(), numMemoryPages);
if (buildSideIndex == 0 && probeSideIndex == 1) {
MutableHashTable<IT1, IT2> hashJoin = new MutableHashTable<IT1, IT2>(serializer1, serializer2, comparator1, comparator2,
pairComparatorFactory.createComparator21(comparator1, comparator2), memSegments, this.taskContext.getIOManager());
this.hashJoin = hashJoin;
hashJoin.open(input1, EmptyMutableObjectIterator.<IT2>get());
} else if (buildSideIndex == 1 && probeSideIndex == 0) {
MutableHashTable<IT2, IT1> hashJoin = new MutableHashTable<IT2, IT1>(serializer2, serializer1, comparator2, comparator1,
pairComparatorFactory.createComparator12(comparator1, comparator2), memSegments, this.taskContext.getIOManager());
this.hashJoin = hashJoin;
hashJoin.open(input2, EmptyMutableObjectIterator.<IT1>get());
} else {
throw new Exception("Error: Inconcistent setup for repeatable hash join driver.");
}
}
@Override
public void prepare() throws Exception {
// nothing
}
@Override
public void run() throws Exception {
final GenericJoiner<IT1, IT2, OT> matchStub = this.taskContext.getStub();
final Collector<OT> collector = this.taskContext.getOutputCollector();
if (buildSideIndex == 0) {
final TypeSerializer<IT1> buildSideSerializer = taskContext.<IT1> getInputSerializer(0);
final TypeSerializer<IT2> probeSideSerializer = taskContext.<IT2> getInputSerializer(1);
final IT1 buildSideRecordFirst = buildSideSerializer.createInstance();
final IT1 buildSideRecordOther = buildSideSerializer.createInstance();
final IT2 probeSideRecord = probeSideSerializer.createInstance();
final IT2 probeSideRecordCopy = probeSideSerializer.createInstance();
@SuppressWarnings("unchecked")
final MutableHashTable<IT1, IT2> join = (MutableHashTable<IT1, IT2>) this.hashJoin;
final MutableObjectIterator<IT2> probeSideInput = taskContext.<IT2>getInput(1);
while (this.running && probeSideInput.next(probeSideRecord)) {
final MutableHashTable.HashBucketIterator<IT1, IT2> bucket = join.getMatchesFor(probeSideRecord);
if (bucket.next(buildSideRecordFirst)) {
while (bucket.next(buildSideRecordOther)) {
probeSideSerializer.copyTo(probeSideRecord, probeSideRecordCopy);
matchStub.join(buildSideRecordOther, probeSideRecordCopy, collector);
}
matchStub.join(buildSideRecordFirst, probeSideRecord, collector);
}
}
} else if (buildSideIndex == 1) {
final TypeSerializer<IT2> buildSideSerializer = taskContext.<IT2> getInputSerializer(1);
final TypeSerializer<IT1> probeSideSerializer = taskContext.<IT1> getInputSerializer(0);
final IT2 buildSideRecordFirst = buildSideSerializer.createInstance();
final IT2 buildSideRecordOther = buildSideSerializer.createInstance();
final IT1 probeSideRecord = probeSideSerializer.createInstance();
final IT1 probeSideRecordCopy = probeSideSerializer.createInstance();
@SuppressWarnings("unchecked")
final MutableHashTable<IT2, IT1> join = (MutableHashTable<IT2, IT1>) this.hashJoin;
final MutableObjectIterator<IT1> probeSideInput = taskContext.<IT1>getInput(0);
while (this.running && probeSideInput.next(probeSideRecord)) {
final MutableHashTable.HashBucketIterator<IT2, IT1> bucket = join.getMatchesFor(probeSideRecord);
if (bucket.next(buildSideRecordFirst)) {
while (bucket.next(buildSideRecordOther)) {
probeSideSerializer.copyTo(probeSideRecord, probeSideRecordCopy);
matchStub.join(probeSideRecordCopy, buildSideRecordOther, collector);
}
matchStub.join(probeSideRecord, buildSideRecordFirst, collector);
}
}
} else {
throw new Exception();
}
}
@Override
public void cleanup() throws Exception {}
@Override
public void reset() throws Exception {}
@Override
public void teardown() {
this.hashJoin.close();
}
@Override
public void cancel() {
this.running = false;
if (this.hashJoin != null) {
this.hashJoin.close();
}
}
}