Skip to content

Commit 3d05def

Browse files
committed
fix pair equality check (tikv#724)
Signed-off-by: shiyuhang <1136742008@qq.com>
1 parent 59e2dae commit 3d05def

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

src/main/java/org/tikv/common/util/Pair.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.tikv.common.util;
1919

2020
import java.io.Serializable;
21+
import java.util.Objects;
2122

2223
public class Pair<F, S> implements Serializable {
2324
public final F first;
@@ -36,4 +37,21 @@ public static <F, S> Pair<F, S> create(F f, S s) {
3637
public String toString() {
3738
return String.format("[%s:%s]", first, second);
3839
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) {
44+
return true;
45+
}
46+
if (o == null || getClass() != o.getClass()) {
47+
return false;
48+
}
49+
Pair<?, ?> pair = (Pair<?, ?>) o;
50+
return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hash(first, second);
56+
}
3957
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2023 TiKV Project Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.tikv.common.util;
19+
20+
import com.google.common.collect.ImmutableList;
21+
import com.google.protobuf.ByteString;
22+
import java.util.ArrayList;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.stream.Collectors;
27+
import org.junit.Test;
28+
import org.tikv.common.PDMockServerTest;
29+
import org.tikv.common.region.TiRegion;
30+
import org.tikv.common.region.TiStore;
31+
import org.tikv.kvproto.Metapb;
32+
import org.tikv.kvproto.Metapb.Peer;
33+
34+
public class PairTest extends PDMockServerTest {
35+
36+
@Test
37+
public void testPair() {
38+
Metapb.Region r =
39+
Metapb.Region.newBuilder()
40+
.setRegionEpoch(Metapb.RegionEpoch.newBuilder().setConfVer(1).setVersion(2))
41+
.setId(233)
42+
.setStartKey(ByteString.EMPTY)
43+
.setEndKey(ByteString.EMPTY)
44+
.addPeers(Peer.getDefaultInstance())
45+
.build();
46+
List<Metapb.Store> s =
47+
ImmutableList.of(
48+
Metapb.Store.newBuilder()
49+
.setAddress(LOCAL_ADDR + ":" + 4000)
50+
.setVersion("5.0.0")
51+
.setId(1)
52+
.build());
53+
54+
TiRegion region =
55+
new TiRegion(
56+
session.getConf(),
57+
r,
58+
r.getPeers(0),
59+
r.getPeersList(),
60+
s.stream().map(TiStore::new).collect(Collectors.toList()));
61+
TiStore store = new TiStore(s.get(0));
62+
63+
Map<Pair<TiRegion, TiStore>, List<ByteString>> groupKeyMap = new HashMap<>();
64+
65+
for (int i = 0; i < 10; i++) {
66+
Pair<TiRegion, TiStore> pair = Pair.create(region, store);
67+
groupKeyMap
68+
.computeIfAbsent(pair, e -> new ArrayList<>())
69+
.add(ByteString.copyFromUtf8("test"));
70+
}
71+
Pair<TiRegion, TiStore> pair = Pair.create(region, store);
72+
assert (groupKeyMap.get(pair).size() == 10);
73+
}
74+
}

0 commit comments

Comments
 (0)