|
| 1 | +/* |
| 2 | + * Copyright 2017 PingCAP, Inc. |
| 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 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package org.tikv.common; |
| 17 | + |
| 18 | +import static org.tikv.common.util.KeyRangeUtils.makeRange; |
| 19 | + |
| 20 | +import com.google.common.collect.Range; |
| 21 | +import com.google.protobuf.ByteString; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.List; |
| 25 | +import org.tikv.common.exception.TiClientInternalException; |
| 26 | +import org.tikv.common.key.Key; |
| 27 | +import org.tikv.common.meta.TiTimestamp; |
| 28 | +import org.tikv.common.operation.iterator.ConcreteScanIterator; |
| 29 | +import org.tikv.common.region.RegionStoreClient; |
| 30 | +import org.tikv.common.region.TiRegion; |
| 31 | +import org.tikv.common.util.BackOffer; |
| 32 | +import org.tikv.common.util.ConcreteBackOffer; |
| 33 | +import org.tikv.common.util.Pair; |
| 34 | +import org.tikv.kvproto.Kvrpcpb.KvPair; |
| 35 | +import org.tikv.kvproto.Metapb.Store; |
| 36 | + |
| 37 | +public class Snapshot { |
| 38 | + private final TiTimestamp timestamp; |
| 39 | + private final TiSession session; |
| 40 | + private final TiConfiguration conf; |
| 41 | + |
| 42 | + public Snapshot(TiTimestamp timestamp, TiSession session) { |
| 43 | + this.timestamp = timestamp; |
| 44 | + this.session = session; |
| 45 | + this.conf = session.getConf(); |
| 46 | + } |
| 47 | + |
| 48 | + public TiSession getSession() { |
| 49 | + return session; |
| 50 | + } |
| 51 | + |
| 52 | + public long getVersion() { |
| 53 | + return timestamp.getVersion(); |
| 54 | + } |
| 55 | + |
| 56 | + public TiTimestamp getTimestamp() { |
| 57 | + return timestamp; |
| 58 | + } |
| 59 | + |
| 60 | + public byte[] get(byte[] key) { |
| 61 | + ByteString keyString = ByteString.copyFrom(key); |
| 62 | + ByteString value = get(keyString); |
| 63 | + return value.toByteArray(); |
| 64 | + } |
| 65 | + |
| 66 | + public ByteString get(ByteString key) { |
| 67 | + Pair<TiRegion, Store> pair = session.getRegionManager().getRegionStorePairByKey(key); |
| 68 | + RegionStoreClient client = RegionStoreClient.create(pair.first, pair.second, getSession()); |
| 69 | + // TODO: Need to deal with lock error after grpc stable |
| 70 | + return client.get(ConcreteBackOffer.newGetBackOff(), key, timestamp.getVersion()); |
| 71 | + } |
| 72 | + |
| 73 | + public Iterator<KvPair> scan(ByteString startKey) { |
| 74 | + return new ConcreteScanIterator(startKey, session, timestamp.getVersion()); |
| 75 | + } |
| 76 | + |
| 77 | + // TODO: Need faster implementation, say concurrent version |
| 78 | + // Assume keys sorted |
| 79 | + public List<KvPair> batchGet(List<ByteString> keys) { |
| 80 | + TiRegion curRegion = null; |
| 81 | + Range<Key> curKeyRange = null; |
| 82 | + Pair<TiRegion, Store> lastPair; |
| 83 | + List<ByteString> keyBuffer = new ArrayList<>(); |
| 84 | + List<KvPair> result = new ArrayList<>(keys.size()); |
| 85 | + BackOffer backOffer = ConcreteBackOffer.newBatchGetMaxBackOff(); |
| 86 | + for (ByteString key : keys) { |
| 87 | + if (curRegion == null || !curKeyRange.contains(Key.toRawKey(key))) { |
| 88 | + Pair<TiRegion, Store> pair = session.getRegionManager().getRegionStorePairByKey(key); |
| 89 | + lastPair = pair; |
| 90 | + curRegion = pair.first; |
| 91 | + curKeyRange = makeRange(curRegion.getStartKey(), curRegion.getEndKey()); |
| 92 | + |
| 93 | + try (RegionStoreClient client = |
| 94 | + RegionStoreClient.create(lastPair.first, lastPair.second, getSession())) { |
| 95 | + List<KvPair> partialResult = |
| 96 | + client.batchGet(backOffer, keyBuffer, timestamp.getVersion()); |
| 97 | + // TODO: Add lock check |
| 98 | + result.addAll(partialResult); |
| 99 | + } catch (Exception e) { |
| 100 | + throw new TiClientInternalException("Error Closing Store client.", e); |
| 101 | + } |
| 102 | + keyBuffer = new ArrayList<>(); |
| 103 | + keyBuffer.add(key); |
| 104 | + } |
| 105 | + } |
| 106 | + return result; |
| 107 | + } |
| 108 | +} |
0 commit comments