|
| 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