├── shading_rule ├── src ├── test │ ├── resources │ │ └── log4j.properties │ └── java │ │ └── com │ │ └── pingcap │ │ └── tikv │ │ ├── BUILD │ │ ├── util │ │ ├── ExponentialBackOffTest.java │ │ ├── ReflectionWrapper.java │ │ ├── ZeroBackOff.java │ │ └── ComparablesTest.java │ │ ├── rule.bzl │ │ ├── meta │ │ └── DBInfoTest.java │ │ ├── expression │ │ ├── TiExprTest.java │ │ └── TiConstantTest.java │ │ ├── types │ │ ├── DecimalTypeTest.java │ │ ├── TimestampTest.java │ │ ├── IntegerTypeTest.java │ │ ├── RealTypeTest.java │ │ └── MyDecimalTest.java │ │ ├── predicates │ │ └── PredicateUtilsTest.java │ │ ├── operation │ │ └── ChunkIteratorTest.java │ │ ├── catalog │ │ └── CatalogTransactionTest.java │ │ └── GrpcUtils.java └── main │ └── java │ └── com │ └── pingcap │ └── tikv │ ├── expression │ ├── aggregate │ │ ├── Max.java │ │ ├── Min.java │ │ ├── First.java │ │ ├── Count.java │ │ ├── Sum.java │ │ └── GroupConcat.java │ ├── ExpressionBlacklist.java │ ├── TypeBlacklist.java │ ├── scalar │ │ ├── Plus.java │ │ ├── Divide.java │ │ ├── Minus.java │ │ ├── Multiply.java │ │ ├── If.java │ │ ├── IfNull.java │ │ ├── IsNull.java │ │ ├── Equal.java │ │ ├── LessThan.java │ │ ├── NotEqual.java │ │ ├── LessEqual.java │ │ ├── GreaterThan.java │ │ ├── NullEqual.java │ │ ├── GreaterEqual.java │ │ ├── Coalesce.java │ │ ├── BitXor.java │ │ ├── IsTrue.java │ │ ├── BitOr.java │ │ ├── Case.java │ │ ├── BitAnd.java │ │ ├── In.java │ │ ├── Not.java │ │ ├── Or.java │ │ ├── And.java │ │ ├── LogicalXor.java │ │ ├── Like.java │ │ └── TiScalarFunction.java │ ├── TiUnaryFunctionExpression.java │ ├── TiExpr.java │ ├── Blacklist.java │ ├── TiByItem.java │ └── TiFunctionExpression.java │ ├── operation │ ├── ErrorHandler.java │ ├── transformer │ │ ├── Projection.java │ │ ├── Skip.java │ │ ├── NoOp.java │ │ ├── MultiKeyDecoder.java │ │ └── Cast.java │ ├── PDErrorHandler.java │ └── iterator │ │ ├── ChunkIterator.java │ │ └── IndexScanIterator.java │ ├── exception │ ├── TypeException.java │ ├── DAGRequestException.java │ ├── TiExpressionException.java │ ├── CastingException.java │ ├── TiClientInternalException.java │ ├── GrpcException.java │ ├── KeyException.java │ ├── RegionException.java │ ├── GrpcRegionStaleException.java │ └── SelectException.java │ ├── row │ ├── RowReader.java │ ├── RowReaderFactory.java │ ├── DefaultRowReader.java │ ├── Row.java │ └── ObjectRowImpl.java │ ├── codec │ ├── InvalidCodecFormatException.java │ ├── UnsupportedTypeException.java │ ├── IgnoreUnsupportedTypeException.java │ ├── CodecException.java │ └── KeyUtils.java │ ├── region │ └── RegionErrorReceiver.java │ ├── util │ ├── Pair.java │ ├── Timer.java │ ├── ExponentialBackOff.java │ ├── FutureObserver.java │ └── BackOff.java │ ├── types │ ├── RequestTypes.java │ ├── BitType.java │ ├── DateTimeType.java │ ├── SetType.java │ ├── EnumType.java │ ├── TimeType.java │ ├── RawBytesType.java │ ├── DateType.java │ ├── Types.java │ └── RealType.java │ ├── BUILD │ ├── meta │ ├── CIStr.java │ ├── TiTimestamp.java │ ├── SchemaState.java │ ├── IndexType.java │ ├── TiIndexColumn.java │ └── TiDBInfo.java │ ├── policy │ ├── RetryNTimes.java │ └── RetryPolicy.java │ ├── predicates │ ├── SelectivityCalculator.java │ ├── PredicateUtils.java │ └── IndexMatcher.java │ ├── tools │ └── RegionUtils.java │ ├── ReadOnlyPDClient.java │ ├── streaming │ └── StreamingResponse.java │ ├── Main.java │ └── event │ └── CacheInvalidateEvent.java ├── .gitmodules ├── Makefile ├── scripts ├── version.sh └── format.sh ├── .gitignore ├── BUILD ├── .travis.yml ├── WORKSPACE └── README.md /shading_rule: -------------------------------------------------------------------------------- 1 | rule io.netty.** io.netty.netty4pingcap.@1 2 | -------------------------------------------------------------------------------- /src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=DEBUG, console 2 | 3 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tipb"] 2 | path = tipb 3 | url = https://github.com/pingcap/tipb.git 4 | [submodule "kvproto"] 5 | path = kvproto 6 | url = https://github.com/pingcap/kvproto.git 7 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | run: 2 | bazel run :tikv-java-client 3 | uber_jar: 4 | bazel build :tikv-java-client_deploy.jar 5 | test: 6 | bazel test //src/test/java/com/pingcap/tikv:tikv-client-java-test --test_output=errors --test_timeout=3600 7 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/aggregate/Max.java: -------------------------------------------------------------------------------- 1 | package com.pingcap.tikv.expression.aggregate; 2 | 3 | import com.pingcap.tidb.tipb.ExprType; 4 | import com.pingcap.tikv.expression.TiExpr; 5 | import com.pingcap.tikv.expression.TiUnaryFunctionExpression; 6 | import com.pingcap.tikv.types.DataType; 7 | 8 | public class Max extends TiUnaryFunctionExpression { 9 | 10 | public Max(TiExpr arg) { 11 | super(arg); 12 | } 13 | 14 | @Override 15 | protected ExprType getExprType() { 16 | return ExprType.Max; 17 | } 18 | 19 | @Override 20 | public DataType getType() { 21 | return args.get(0).getType(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/aggregate/Min.java: -------------------------------------------------------------------------------- 1 | package com.pingcap.tikv.expression.aggregate; 2 | 3 | import com.pingcap.tidb.tipb.ExprType; 4 | import com.pingcap.tikv.expression.TiExpr; 5 | import com.pingcap.tikv.expression.TiUnaryFunctionExpression; 6 | import com.pingcap.tikv.types.DataType; 7 | 8 | public class Min extends TiUnaryFunctionExpression { 9 | 10 | public Min(TiExpr arg) { 11 | super(arg); 12 | } 13 | 14 | @Override 15 | protected ExprType getExprType() { 16 | return ExprType.Min; 17 | } 18 | 19 | @Override 20 | public DataType getType() { 21 | return args.get(0).getType(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/aggregate/First.java: -------------------------------------------------------------------------------- 1 | package com.pingcap.tikv.expression.aggregate; 2 | 3 | import com.pingcap.tidb.tipb.ExprType; 4 | import com.pingcap.tikv.expression.TiExpr; 5 | import com.pingcap.tikv.expression.TiUnaryFunctionExpression; 6 | import com.pingcap.tikv.types.DataType; 7 | 8 | public class First extends TiUnaryFunctionExpression { 9 | public First(TiExpr arg) { 10 | super(arg); 11 | } 12 | 13 | @Override 14 | protected ExprType getExprType() { 15 | return ExprType.First; 16 | } 17 | 18 | @Override 19 | public DataType getType() { 20 | return args.get(0).getType(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/aggregate/Count.java: -------------------------------------------------------------------------------- 1 | package com.pingcap.tikv.expression.aggregate; 2 | 3 | import com.pingcap.tidb.tipb.ExprType; 4 | import com.pingcap.tikv.expression.TiExpr; 5 | import com.pingcap.tikv.expression.TiFunctionExpression; 6 | import com.pingcap.tikv.types.DataType; 7 | import com.pingcap.tikv.types.DataTypeFactory; 8 | import com.pingcap.tikv.types.Types; 9 | 10 | public class Count extends TiFunctionExpression { 11 | 12 | public Count(TiExpr... arg) { 13 | super(arg); 14 | } 15 | 16 | @Override 17 | protected ExprType getExprType() { 18 | return ExprType.Count; 19 | } 20 | 21 | @Override 22 | public DataType getType() { 23 | return DataTypeFactory.of(Types.TYPE_LONG); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/aggregate/Sum.java: -------------------------------------------------------------------------------- 1 | package com.pingcap.tikv.expression.aggregate; 2 | 3 | import com.pingcap.tidb.tipb.ExprType; 4 | import com.pingcap.tikv.expression.TiExpr; 5 | import com.pingcap.tikv.expression.TiUnaryFunctionExpression; 6 | import com.pingcap.tikv.types.DataType; 7 | import com.pingcap.tikv.types.DataTypeFactory; 8 | import com.pingcap.tikv.types.Types; 9 | 10 | public class Sum extends TiUnaryFunctionExpression { 11 | 12 | public Sum(TiExpr arg) { 13 | super(arg); 14 | } 15 | 16 | @Override 17 | protected ExprType getExprType() { 18 | return ExprType.Sum; 19 | } 20 | 21 | @Override 22 | public DataType getType() { 23 | return DataTypeFactory.of(Types.TYPE_NEW_DECIMAL); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/aggregate/GroupConcat.java: -------------------------------------------------------------------------------- 1 | package com.pingcap.tikv.expression.aggregate; 2 | 3 | import com.pingcap.tidb.tipb.ExprType; 4 | import com.pingcap.tikv.expression.TiExpr; 5 | import com.pingcap.tikv.expression.TiUnaryFunctionExpression; 6 | import com.pingcap.tikv.types.DataType; 7 | import com.pingcap.tikv.types.DataTypeFactory; 8 | import com.pingcap.tikv.types.Types; 9 | 10 | public class GroupConcat extends TiUnaryFunctionExpression { 11 | 12 | public GroupConcat(TiExpr arg) { 13 | super(arg); 14 | } 15 | 16 | @Override 17 | protected ExprType getExprType() { 18 | return ExprType.GroupConcat; 19 | } 20 | 21 | @Override 22 | public DataType getType() { 23 | return DataTypeFactory.of(Types.TYPE_VARCHAR); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/ErrorHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation; 19 | 20 | @FunctionalInterface 21 | public interface ErrorHandler { 22 | void handle(RespT resp); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/TypeException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | public class TypeException extends RuntimeException { 19 | public TypeException(String msg) { 20 | super(msg); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/row/RowReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.row; 19 | 20 | import com.pingcap.tikv.types.DataType; 21 | 22 | public interface RowReader { 23 | Row readRow(DataType[] dataTypes); 24 | } 25 | -------------------------------------------------------------------------------- /scripts/version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright 2017 PingCAP, Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | SHA1=`git describe --tags` 18 | echo ' 19 | package com.pingcap.tikv; 20 | public final class TiVersion { public final static String CommitVersion = "'${SHA1}'"; }' > src/main/java/com/pingcap/tikv/TiVersion.java 21 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/DAGRequestException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | public class DAGRequestException extends RuntimeException { 19 | public DAGRequestException(String msg) { 20 | super(msg); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/TiExpressionException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | public class TiExpressionException extends RuntimeException { 19 | public TiExpressionException(String msg) { 20 | super(msg); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/codec/InvalidCodecFormatException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.codec; 17 | 18 | public class InvalidCodecFormatException extends RuntimeException { 19 | public InvalidCodecFormatException(String msg) { 20 | super(msg); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/codec/UnsupportedTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.codec; 19 | 20 | public class UnsupportedTypeException extends RuntimeException { 21 | public UnsupportedTypeException(String msg) { 22 | super(msg); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/codec/IgnoreUnsupportedTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.codec; 19 | 20 | public class IgnoreUnsupportedTypeException extends RuntimeException { 21 | public IgnoreUnsupportedTypeException(String msg) { 22 | super(msg); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load(":rule.bzl", "junit_suite_test") 4 | 5 | junit_suite_test( 6 | name = "tikv-client-java-test", 7 | srcs = glob( 8 | ["**/*.java"], 9 | ), 10 | deps = [ 11 | "//src/main/java/com/pingcap/tikv:tikv-java-client-lib", 12 | "//:java", 13 | "//:java_compile_imports", 14 | "@com_fasterxml_jackson_core_jackson_annotations//jar", 15 | "@com_fasterxml_jackson_core_jackson_core//jar", 16 | "@com_fasterxml_jackson_core_jackson_databind//jar", 17 | 18 | "@org_pubref_rules_protobuf//java:grpc_compiletime_deps", 19 | "@org_pubref_rules_protobuf//java:netty_runtime_deps", 20 | "@net_sf_trove4j_trove4j//jar", 21 | "@junit_junit//jar", 22 | "@joda_time//jar", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/region/RegionErrorReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.region; 19 | 20 | 21 | import com.pingcap.tikv.kvproto.Metapb.Store; 22 | 23 | public interface RegionErrorReceiver { 24 | void onNotLeader(TiRegion region, Store store); 25 | void onStoreNotMatch(); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/codec/CodecException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.codec; 17 | 18 | public class CodecException extends RuntimeException { 19 | public CodecException(String msg) { 20 | super(msg); 21 | } 22 | 23 | public CodecException(String msg, Throwable t) { 24 | super(msg, t); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/CastingException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | public class CastingException extends RuntimeException { 19 | public CastingException(Exception e) { 20 | super(e); 21 | } 22 | 23 | public CastingException(String msg) { 24 | super(msg); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/row/RowReaderFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.row; 19 | 20 | import com.pingcap.tikv.codec.CodecDataInput; 21 | 22 | public class RowReaderFactory { 23 | public static RowReader createRowReader(CodecDataInput cdi) { 24 | return new DefaultRowReader(cdi); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #ignore idea configuration 2 | .idea 3 | *.iml 4 | target 5 | proto 6 | src/main/java/com/pingcap/tikv/TiVersion.java 7 | 8 | # ignore eclipse configuration 9 | .project 10 | 11 | # ignore maven related files 12 | dependency-reduced-pom.xml 13 | # ignore emacs configuation 14 | .meghanada 15 | # Compiled class file 16 | *.class 17 | 18 | # Log file 19 | *.log 20 | 21 | # BlueJ files 22 | *.ctxt 23 | 24 | # Mobile Tools for Java (J2ME) 25 | .mtj.tmp/ 26 | 27 | # Package Files # 28 | *.jar 29 | *.war 30 | *.ear 31 | *.zip 32 | *.tar.gz 33 | *.rar 34 | 35 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 36 | hs_err_pid* 37 | 38 | # ignore Mac files 39 | .DS_Store 40 | 41 | # ignore gradle 42 | .gradle 43 | 44 | 45 | bazel-* 46 | 47 | ###ime 48 | .ensime 49 | .ensime_cache/ 50 | scripts/pd/ 51 | scripts/tikv/ 52 | 53 | // jenv configuration 54 | .java-version 55 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/TiClientInternalException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | public class TiClientInternalException extends RuntimeException { 19 | public TiClientInternalException(String msg) { 20 | super(msg); 21 | } 22 | 23 | public TiClientInternalException(String msg, Throwable t) { 24 | super(msg, t); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/ExpressionBlacklist.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression; 17 | 18 | public class ExpressionBlacklist extends Blacklist { 19 | 20 | public ExpressionBlacklist(String exprsString) { 21 | super(exprsString); 22 | } 23 | 24 | public boolean isUnsupportedPushdownExpr(Class cls) { 25 | return isUnsupported(cls); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/TypeBlacklist.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.expression; 19 | 20 | public class TypeBlacklist extends Blacklist { 21 | 22 | public TypeBlacklist(String typesString) { 23 | super(typesString); 24 | } 25 | 26 | public boolean isUnsupportedType(String typeName) { 27 | return isUnsupported(typeName); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/GrpcException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | public class GrpcException extends RuntimeException { 19 | public GrpcException(Exception e) { 20 | super(e); 21 | } 22 | 23 | public GrpcException(String msg) { 24 | super(msg); 25 | } 26 | 27 | public GrpcException(String msg, Exception e) { 28 | super(msg, e); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/transformer/Projection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation.transformer; 19 | 20 | import com.pingcap.tikv.row.Row; 21 | import com.pingcap.tikv.types.DataType; 22 | import java.util.List; 23 | 24 | public interface Projection { 25 | void set(Object value, Row row, int pos); 26 | 27 | int size(); 28 | 29 | List getTypes(); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/util/Pair.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.util; 17 | 18 | import java.io.Serializable; 19 | 20 | public class Pair implements Serializable { 21 | public final F first; 22 | public final S second; 23 | 24 | public Pair(F f, S s) { 25 | first = f; 26 | second = s; 27 | } 28 | 29 | public static Pair create(F f, S s) { 30 | return new Pair<>(f, s); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/KeyException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | import com.pingcap.tikv.kvproto.Kvrpcpb; 19 | 20 | public class KeyException extends RuntimeException { 21 | private final Kvrpcpb.KeyError keyErr; 22 | 23 | public KeyException(Kvrpcpb.KeyError keyErr) { 24 | this.keyErr = keyErr; 25 | } 26 | 27 | public Kvrpcpb.KeyError getKeyErr() { 28 | return keyErr; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/RegionException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | import com.pingcap.tikv.kvproto.Errorpb.Error; 19 | 20 | public class RegionException extends RuntimeException { 21 | private final Error regionErr; 22 | 23 | public RegionException(Error regionErr) { 24 | this.regionErr = regionErr; 25 | } 26 | 27 | public Error getRegionErr() { 28 | return regionErr; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Plus.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | 21 | public class Plus extends TiScalarFunction { 22 | public Plus(TiExpr lhs, TiExpr rhs) { 23 | super(lhs, rhs); 24 | } 25 | 26 | @Override 27 | protected ExprType getExprType() { 28 | return ExprType.Plus; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Divide.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | 21 | public class Divide extends TiScalarFunction { 22 | public Divide(TiExpr lhs, TiExpr rhs) { 23 | super(lhs, rhs); 24 | } 25 | 26 | @Override 27 | protected ExprType getExprType() { 28 | return ExprType.Div; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Minus.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | 21 | public class Minus extends TiScalarFunction { 22 | public Minus(TiExpr lhs, TiExpr rhs) { 23 | super(lhs, rhs); 24 | } 25 | 26 | @Override 27 | protected ExprType getExprType() { 28 | return ExprType.Minus; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Multiply.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | 21 | public class Multiply extends TiScalarFunction { 22 | public Multiply(TiExpr lhs, TiExpr rhs) { 23 | super(lhs, rhs); 24 | } 25 | 26 | @Override 27 | protected ExprType getExprType() { 28 | return ExprType.Mul; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/RequestTypes.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.types; 17 | 18 | public enum RequestTypes { 19 | REQ_TYPE_SELECT(101), 20 | REQ_TYPE_INDEX(102), 21 | REQ_TYPE_DAG(103), 22 | REQ_TYPE_ANALYZE(104), 23 | BATCH_ROW_COUNT(64); 24 | 25 | private final int value; 26 | 27 | RequestTypes(int value) { 28 | this.value = value; 29 | } 30 | 31 | public int getValue() { 32 | return value; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/GrpcRegionStaleException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | 19 | public class GrpcRegionStaleException extends GrpcException { 20 | public GrpcRegionStaleException(Exception e) { 21 | super(e); 22 | } 23 | 24 | public GrpcRegionStaleException(String msg) { 25 | super(msg); 26 | } 27 | 28 | public GrpcRegionStaleException(String msg, Exception e) { 29 | super(msg, e); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/util/Timer.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.util; 17 | 18 | import java.util.concurrent.TimeUnit; 19 | 20 | final public class Timer { 21 | private long startTime; 22 | 23 | public Timer() { 24 | reset(); 25 | } 26 | 27 | public long stop(TimeUnit tu) { 28 | return tu.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS); 29 | } 30 | 31 | public void reset() { 32 | startTime = System.nanoTime(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/BitType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import com.pingcap.tikv.meta.TiColumnInfo; 21 | 22 | public class BitType extends IntegerType { 23 | private BitType(int tp) { 24 | super(tp); 25 | } 26 | 27 | protected BitType(TiColumnInfo.InternalTypeHolder holder) { 28 | super(holder); 29 | } 30 | 31 | static BitType of(int tp) { 32 | return new BitType(tp); 33 | } 34 | 35 | public String simpleTypeName() { return "bit"; } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/exception/SelectException.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.exception; 17 | 18 | import com.pingcap.tidb.tipb.Error; 19 | 20 | public class SelectException extends RuntimeException { 21 | private final Error err; 22 | 23 | public SelectException(Error err, String msg) { 24 | super(msg); 25 | this.err = err; 26 | } 27 | 28 | //TODO: improve this 29 | public SelectException(String msg) { 30 | super(msg); 31 | this.err = null; 32 | } 33 | 34 | public Error getError() { 35 | return err; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/util/ExponentialBackOffTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.util; 19 | 20 | import org.junit.Test; 21 | import static org.junit.Assert.assertEquals; 22 | 23 | public class ExponentialBackOffTest { 24 | @Test 25 | public void nextBackOffMillisTest() { 26 | BackOff backOff = new ExponentialBackOff(10); 27 | for(int i = 1; i < 10; i++) { 28 | long nextBackoffMillis = backOff.nextBackOffMillis(); 29 | int factor = i<<2; 30 | assertEquals(nextBackoffMillis, factor*1000); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /scripts/format.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright 2017 PingCAP, Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | if hash google-java-format 2>/dev/null; then 18 | files_need_format=() 19 | files=$(find ../src -name "*.java") 20 | for file in "${files[@]}";do 21 | echo "formatting..." 22 | #google-java-format -r ("${file}") 23 | google-java-format -r ${file} 24 | done 25 | else 26 | echo "google-java-format is not installed, please install it first" 27 | echo "If you are using macos, type 'brew install google-java-format'" 28 | echo "Otherwise, go to google-java-format github page: 29 | https://github.com/google/google-java-format.git 30 | and download it." 31 | fi 32 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/If.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class If extends TiScalarFunction { 24 | public If(TiExpr... arg) { 25 | super(arg); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.If; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/util/ReflectionWrapper.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.util; 17 | 18 | import java.lang.reflect.Method; 19 | 20 | public class ReflectionWrapper { 21 | public ReflectionWrapper(Object obj) { 22 | this.obj = obj; 23 | } 24 | 25 | private Object obj; 26 | 27 | public Object call(String methodName, Object...args) { 28 | try { 29 | Method method = obj.getClass().getDeclaredMethod(methodName); 30 | method.setAccessible(true); 31 | return method.invoke(obj, args); 32 | } catch (Exception e) { 33 | throw new RuntimeException(e); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/rule.bzl: -------------------------------------------------------------------------------- 1 | def junit_suite_test(name, srcs, deps, size="small", resources=[], classpath_resources=[], jvm_flags=[], tags=[], data=[]): 2 | tests = [] 3 | package = PACKAGE_NAME.replace("src/test/java/", "").replace("/", ".") 4 | for src in srcs: 5 | if src.endswith("Test.java"): 6 | if "/" in src: 7 | src = package + "." + src.replace("/", ".") 8 | tests += [src.replace(".java", ".class")] 9 | 10 | 11 | native.genrule( 12 | name = name + "-AllTests-gen", 13 | outs = ["AllTests.java"], 14 | cmd = """ 15 | cat <> $@ 16 | package %s; 17 | 18 | import org.junit.runner.RunWith; 19 | import org.junit.runners.Suite; 20 | 21 | @RunWith(Suite.class) 22 | @Suite.SuiteClasses({%s}) 23 | public class AllTests {} 24 | EOF 25 | """ % (package, ",".join(tests)) 26 | ) 27 | 28 | native.java_test( 29 | name = name, 30 | srcs = srcs + ["AllTests.java"], 31 | test_class = package + ".AllTests", 32 | resources = resources, 33 | classpath_resources = classpath_resources, 34 | data = data, 35 | size = size, 36 | tags = tags, 37 | jvm_flags = jvm_flags, 38 | deps = deps + [ 39 | ], 40 | ) 41 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/IfNull.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class IfNull extends TiScalarFunction { 24 | public IfNull(TiExpr... arg) { 25 | super(arg); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.IfNull; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/IsNull.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class IsNull extends TiScalarFunction { 24 | public IsNull(TiExpr arg) { 25 | super(arg); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.IsNull; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Equal.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class Equal extends TiScalarFunction { 24 | public Equal(TiExpr lhs, TiExpr rhs) { 25 | super(lhs, rhs); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.EQ; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/LessThan.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class LessThan extends TiScalarFunction { 24 | public LessThan(TiExpr lhs, TiExpr rhs) { 25 | super(lhs, rhs); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.LT; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/NotEqual.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class NotEqual extends TiScalarFunction { 24 | public NotEqual(TiExpr lhs, TiExpr rhs) { 25 | super(lhs, rhs); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.NE; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/LessEqual.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class LessEqual extends TiScalarFunction { 24 | public LessEqual(TiExpr lhs, TiExpr rhs) { 25 | super(lhs, rhs); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.LE; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/GreaterThan.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class GreaterThan extends TiScalarFunction { 24 | public GreaterThan(TiExpr lhs, TiExpr rhs) { 25 | super(lhs, rhs); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.GT; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/NullEqual.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class NullEqual extends TiScalarFunction { 24 | public NullEqual(TiExpr lhs, TiExpr rhs) { 25 | super(lhs, rhs); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.NullEQ; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/GreaterEqual.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class GreaterEqual extends TiScalarFunction { 24 | public GreaterEqual(TiExpr lhs, TiExpr rhs) { 25 | super(lhs, rhs); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.GE; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/transformer/Skip.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation.transformer; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | import com.pingcap.tikv.row.Row; 22 | import com.pingcap.tikv.types.DataType; 23 | import java.util.List; 24 | 25 | public class Skip implements Projection { 26 | public static final Skip SKIP_OP = new Skip(); 27 | 28 | @Override 29 | public void set(Object value, Row row, int pos) {} 30 | 31 | @Override 32 | public int size() { 33 | return 0; 34 | } 35 | 36 | @Override 37 | public List getTypes() { 38 | return ImmutableList.of(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | java_library( 4 | name = "tikv-java-client-lib", 5 | srcs = glob( 6 | ["**/*.java"], 7 | ), 8 | deps = [ 9 | "//:java", 10 | "@com_fasterxml_jackson_core_jackson_annotations//jar", 11 | "@com_fasterxml_jackson_core_jackson_core//jar", 12 | "@com_fasterxml_jackson_core_jackson_databind//jar", 13 | "@com_google_code_findbugs_jsr305//jar", 14 | "@com_google_code_gson_gson//jar", 15 | "@com_google_errorprone_error_prone_annotations//jar", 16 | "@com_google_guava_guava//jar", 17 | "@com_google_protobuf_protobuf_java//jar", 18 | "@joda_time//jar", 19 | # the following are defined in rules_protobuf 20 | "@org_pubref_rules_protobuf//java:grpc_compiletime_deps", 21 | "@org_pubref_rules_protobuf//java:netty_runtime_deps", 22 | 23 | "@org_slf4j_slf4j_api//jar", 24 | "@org_slf4j_jcl_over_slf4j//jar", 25 | "@org_slf4j_jul_to_slf4j//jar", 26 | "@log4j_log4j//jar", 27 | "@net_sf_trove4j_trove4j//jar", 28 | ], 29 | ) 30 | 31 | filegroup( 32 | name = "srcs", 33 | srcs = ["BUILD"] + glob(["**/*.java"]), 34 | ) 35 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Coalesce.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | 22 | public class Coalesce extends TiScalarFunction { 23 | public Coalesce(TiExpr... args) { 24 | super(args); 25 | } 26 | 27 | @Override 28 | protected ExprType getExprType() { 29 | return ExprType.Coalesce; 30 | } 31 | 32 | @Override 33 | protected void validateArguments(TiExpr... args) throws RuntimeException { 34 | } 35 | 36 | @Override 37 | public DataType getType() { 38 | throw new UnsupportedOperationException(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/DateTimeType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import com.pingcap.tikv.meta.TiColumnInfo; 21 | 22 | import java.time.ZoneId; 23 | 24 | public class DateTimeType extends TimestampType { 25 | private static final ZoneId DEFAULT_TIMEZONE = ZoneId.systemDefault(); 26 | static DateTimeType of(int tp) { 27 | return new DateTimeType(tp); 28 | } 29 | 30 | @Override 31 | protected ZoneId getDefaultTimezone() { 32 | return DEFAULT_TIMEZONE; 33 | } 34 | 35 | DateTimeType(int tp) { 36 | super(tp); 37 | } 38 | 39 | DateTimeType(TiColumnInfo.InternalTypeHolder holder) { 40 | super(holder); 41 | } 42 | 43 | public String simpleTypeName() { return "datetime"; } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/BitXor.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tidb.tipb.ScalarFuncSig; 20 | import com.pingcap.tikv.expression.TiExpr; 21 | import com.pingcap.tikv.types.DataType; 22 | 23 | public class BitXor extends TiScalarFunction { 24 | public BitXor(TiExpr lhs, TiExpr rhs) { 25 | super(lhs, rhs); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.BitXor; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | throw new UnsupportedOperationException(); 36 | } 37 | 38 | @Override 39 | ScalarFuncSig getSignature() { 40 | return ScalarFuncSig.BitXorSig; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/meta/CIStr.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.meta; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | 21 | /** This class is mapping TiDB's CIStr/ For internal use only. */ 22 | public class CIStr { 23 | private final String o; // original 24 | private final String l; 25 | 26 | @JsonCreator 27 | private CIStr(@JsonProperty("O") String o, @JsonProperty("L") String l) { 28 | this.o = o; 29 | this.l = l; 30 | } 31 | 32 | public static CIStr newCIStr(String str) { 33 | return new CIStr(str, str.toLowerCase()); 34 | } 35 | 36 | public String getO() { 37 | return o; 38 | } 39 | 40 | public String getL() { 41 | return l; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/IsTrue.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | import com.pingcap.tikv.types.IntegerType; 22 | 23 | public class IsTrue extends TiScalarFunction { 24 | public IsTrue(TiExpr arg) { 25 | super(arg); 26 | } 27 | 28 | @Override 29 | protected ExprType getExprType() { 30 | return ExprType.IsTruth; 31 | } 32 | 33 | @Override 34 | public DataType getType() { 35 | return IntegerType.DEF_BOOLEAN_TYPE; 36 | } 37 | 38 | @Override 39 | protected void validateArguments(TiExpr... args) throws RuntimeException { 40 | super.validateArguments(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/TiUnaryFunctionExpression.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression; 17 | 18 | import static com.google.common.base.Preconditions.checkArgument; 19 | import static com.google.common.base.Preconditions.checkNotNull; 20 | 21 | import com.pingcap.tikv.types.DataType; 22 | 23 | public abstract class TiUnaryFunctionExpression extends TiFunctionExpression { 24 | private DataType dataType; 25 | 26 | protected TiUnaryFunctionExpression(TiExpr... args) { 27 | super(args); 28 | } 29 | 30 | @Override 31 | protected void validateArguments(TiExpr... args) throws RuntimeException { 32 | checkNotNull(args, "Arguments of " + getName() + " cannot be null"); 33 | checkArgument(args.length == 1, getName() + " takes only 1 argument"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/meta/TiTimestamp.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.meta; 17 | 18 | import java.io.Serializable; 19 | 20 | /** TiTimestamp is the timestamp returned by timestamp oracle inside placement driver */ 21 | public class TiTimestamp implements Serializable { 22 | private static final int PHYSICAL_SHIFT_BITS = 18; 23 | 24 | private final long physical; 25 | private final long logical; 26 | 27 | public TiTimestamp(long p, long l) { 28 | this.physical = p; 29 | this.logical = l; 30 | } 31 | 32 | public long getVersion() { 33 | return (physical << PHYSICAL_SHIFT_BITS) + logical; 34 | } 35 | 36 | public long getPhysical() { 37 | return this.physical; 38 | } 39 | 40 | public long getLogical() { 41 | return this.logical; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/row/DefaultRowReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.row; 19 | 20 | import com.pingcap.tikv.codec.CodecDataInput; 21 | import com.pingcap.tikv.types.DataType; 22 | 23 | public class DefaultRowReader implements RowReader { 24 | private final CodecDataInput cdi; 25 | 26 | public static DefaultRowReader create(CodecDataInput cdi) { 27 | return new DefaultRowReader(cdi); 28 | } 29 | 30 | DefaultRowReader(CodecDataInput cdi) { 31 | this.cdi = cdi; 32 | } 33 | 34 | public Row readRow(DataType[] dataTypes) { 35 | int length = dataTypes.length; 36 | Row row = ObjectRowImpl.create(length); 37 | for (int i = 0; i < length; i++) { 38 | dataTypes[i].decodeValueToRow(cdi, row, i); 39 | } 40 | return row; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/BitOr.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tidb.tipb.ScalarFuncSig; 20 | import com.pingcap.tikv.expression.TiExpr; 21 | import com.pingcap.tikv.types.DataType; 22 | import com.pingcap.tikv.types.IntegerType; 23 | 24 | public class BitOr extends TiScalarFunction { 25 | public BitOr(TiExpr lhs, TiExpr rhs) { 26 | super(lhs, rhs); 27 | } 28 | 29 | @Override 30 | protected ExprType getExprType() { 31 | return ExprType.BitOr; 32 | } 33 | 34 | @Override 35 | public DataType getType() { 36 | return IntegerType.DEF_BOOLEAN_TYPE; 37 | } 38 | 39 | @Override 40 | ScalarFuncSig getSignature() { 41 | return ScalarFuncSig.BitOrSig; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Case.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.types.DataType; 21 | 22 | public class Case extends TiScalarFunction { 23 | public Case(TiExpr... arg) { 24 | super(arg); 25 | } 26 | 27 | @Override 28 | protected ExprType getExprType() { 29 | return ExprType.Case; 30 | } 31 | 32 | @Override 33 | protected void validateArguments(TiExpr... args) throws RuntimeException { 34 | } 35 | 36 | /** 37 | * For Case 38 | * 39 | * @return a DataType that Case expression has. 40 | */ 41 | @Override 42 | public DataType getType() { 43 | throw new UnsupportedOperationException(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/TiExpr.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression; 17 | 18 | import com.pingcap.tidb.tipb.Expr; 19 | import com.pingcap.tikv.meta.TiTableInfo; 20 | import com.pingcap.tikv.types.DataType; 21 | import java.io.Serializable; 22 | 23 | public interface TiExpr extends Serializable { 24 | Expr toProto(); 25 | 26 | default boolean isSupportedExpr(ExpressionBlacklist blackList) { 27 | if (blackList != null && blackList.isUnsupportedPushdownExpr(getClass())) { 28 | return false; 29 | } 30 | try { 31 | Expr expr = toProto(); 32 | return expr != null; 33 | } catch (Exception e) { 34 | return false; 35 | } 36 | } 37 | 38 | DataType getType(); 39 | 40 | // TODO: Make it visitor 41 | TiExpr resolve(TiTableInfo table); 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/BitAnd.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.ExprType; 19 | import com.pingcap.tidb.tipb.ScalarFuncSig; 20 | import com.pingcap.tikv.expression.TiExpr; 21 | import com.pingcap.tikv.types.DataType; 22 | import com.pingcap.tikv.types.IntegerType; 23 | 24 | public class BitAnd extends TiScalarFunction { 25 | public BitAnd(TiExpr lhs, TiExpr rhs) { 26 | super(lhs, rhs); 27 | } 28 | 29 | @Override 30 | protected ExprType getExprType() { 31 | return ExprType.BitAnd; 32 | } 33 | 34 | @Override 35 | public DataType getType() { 36 | return IntegerType.DEF_BOOLEAN_TYPE; 37 | } 38 | 39 | @Override 40 | ScalarFuncSig getSignature() { 41 | return ScalarFuncSig.BitAndSig; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/util/ZeroBackOff.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.util; 17 | 18 | 19 | import com.google.common.base.Preconditions; 20 | 21 | public class ZeroBackOff implements BackOff { 22 | private int attempts; 23 | private int counter; 24 | 25 | public ZeroBackOff(int attempts) { 26 | Preconditions.checkArgument(attempts >= 1, "Retry count cannot be less than 1."); 27 | this.counter = 1; 28 | this.attempts = attempts; 29 | } 30 | 31 | @Override 32 | public void reset() { 33 | this.counter = 1; 34 | } 35 | 36 | /** 37 | * produces 0 1 1 2 3 ... fibonacci series number. 38 | */ 39 | @Override 40 | public long nextBackOffMillis() { 41 | if(attempts <= counter) { 42 | return BackOff.STOP; 43 | } 44 | counter++; 45 | return 0; 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/meta/SchemaState.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.meta; 17 | 18 | import com.pingcap.tikv.exception.TiClientInternalException; 19 | 20 | public enum SchemaState { 21 | StateNone(0), 22 | StateDeleteOnly(1), 23 | StateWriteOnly(2), 24 | StateWriteReorganization(3), 25 | StateDeleteReorganization(4), 26 | StatePublic(5); 27 | 28 | private final int state; 29 | 30 | SchemaState(int state) { 31 | this.state = state; 32 | } 33 | 34 | public static SchemaState fromValue(int b) { 35 | for (SchemaState e : SchemaState.values()) { 36 | if (e.state == b) { 37 | return e; 38 | } 39 | } 40 | throw new TiClientInternalException("Invalid SchemaState code: " + b); 41 | } 42 | 43 | public int getStateCode() { 44 | return state; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/PDErrorHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation; 19 | 20 | import com.pingcap.tikv.PDClient; 21 | import com.pingcap.tikv.kvproto.Pdpb; 22 | import java.util.function.Function; 23 | 24 | public class PDErrorHandler implements ErrorHandler { 25 | private final Function getError; 26 | private final PDClient client; 27 | 28 | public PDErrorHandler(Function errorExtractor, PDClient client) { 29 | this.getError = errorExtractor; 30 | this.client = client; 31 | } 32 | 33 | public void handle(RespT resp) { 34 | if (resp == null) { 35 | return; 36 | } 37 | Pdpb.Error error = getError.apply(resp); 38 | if (error != null) { 39 | client.updateLeader(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/SetType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import com.pingcap.tikv.codec.CodecDataInput; 21 | import com.pingcap.tikv.codec.UnsupportedTypeException; 22 | import com.pingcap.tikv.meta.TiColumnInfo; 23 | 24 | public class SetType extends BytesType { 25 | private SetType(int tp) { 26 | super(tp); 27 | } 28 | 29 | protected SetType(TiColumnInfo.InternalTypeHolder holder) { 30 | super(holder); 31 | } 32 | 33 | static SetType of(int tp) { 34 | return new SetType(tp); 35 | } 36 | 37 | public String simpleTypeName() { return "set"; } 38 | 39 | // Set is not supported yet 40 | @Override 41 | public Object decodeNotNull(int flag, CodecDataInput cdi) { 42 | throw new UnsupportedTypeException("Set type is not supported yet"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/EnumType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import com.pingcap.tikv.codec.CodecDataInput; 21 | import com.pingcap.tikv.codec.UnsupportedTypeException; 22 | import com.pingcap.tikv.meta.TiColumnInfo; 23 | 24 | public class EnumType extends BytesType { 25 | private EnumType(int tp) { 26 | super(tp); 27 | } 28 | 29 | protected EnumType(TiColumnInfo.InternalTypeHolder holder) { 30 | super(holder); 31 | } 32 | 33 | static EnumType of(int tp) { 34 | return new EnumType(tp); 35 | } 36 | 37 | public String simpleTypeName() { return "enum"; } 38 | 39 | // Enum is not supported yet 40 | @Override 41 | public Object decodeNotNull(int flag, CodecDataInput cdi) { 42 | throw new UnsupportedTypeException("Enum type is not supported yet"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | load( 2 | "@com_github_zhexuany_bazel_shade//:java_shade.bzl", 3 | "java_shade" 4 | ) 5 | package(default_visibility = ["//visibility:public"]) 6 | 7 | java_binary( 8 | name = "tikv-java-client", 9 | main_class = "com.pingcap.tikv.Main", 10 | runtime_deps = [ 11 | "//src/main/java/com/pingcap/tikv:tikv-java-client-lib", 12 | ":shaded_scalding", 13 | ], 14 | ) 15 | java_shade( 16 | name = "shaded_args", 17 | input_jar = "@io_netty_netty_codec_socks//jar", 18 | rules = "shading_rule" 19 | ) 20 | 21 | java_import( 22 | name = "shaded_scalding", 23 | jars = ["shaded_args.jar"] 24 | ) 25 | 26 | filegroup( 27 | name = "protos", 28 | srcs = glob([ 29 | "kvproto/proto/*.proto", 30 | "kvproto/_vendor/src/github.com/gogo/protobuf/gogoproto/*.proto", 31 | "tipb/proto/*.proto", 32 | ]), 33 | ) 34 | 35 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_library") 36 | 37 | java_proto_library( 38 | name = "java", 39 | imports = [ 40 | "external/com_google_protobuf/src/", 41 | "kvproto/proto", 42 | "kvproto/_vendor/src/github.com/gogo/protobuf", 43 | "tipb/proto", 44 | ], 45 | inputs = ["@com_google_protobuf//:well_known_protos"], 46 | protos = [":protos"], 47 | verbose = 0, # 0=no output, 1=show protoc command, 2+ more... 48 | with_grpc = True, 49 | ) 50 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/meta/DBInfoTest.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.meta; 17 | 18 | import static org.junit.Assert.*; 19 | 20 | import com.fasterxml.jackson.databind.ObjectMapper; 21 | import org.junit.Test; 22 | 23 | public class DBInfoTest { 24 | @Test 25 | public void testSerialize() throws Exception { 26 | ObjectMapper mapper = new ObjectMapper(); 27 | String json = 28 | "{\"id\":1,\"db_name\":{\"O\":\"test\",\"L\":\"test\"},\"charset\":\"utf8\",\"collate\":\"utf8_bin\",\"state\":5}"; 29 | TiDBInfo dbInfo = mapper.readValue(json, TiDBInfo.class); 30 | assertEquals(dbInfo.getId(), 1); 31 | assertEquals(dbInfo.getName(), "test"); 32 | assertEquals(dbInfo.getCharset(), "utf8"); 33 | assertEquals(dbInfo.getCollate(), "utf8_bin"); 34 | assertEquals(dbInfo.getSchemaState(), SchemaState.StatePublic); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/transformer/NoOp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation.transformer; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | import com.pingcap.tikv.row.Row; 22 | import com.pingcap.tikv.types.DataType; 23 | import java.util.List; 24 | 25 | /** Noop is a base type projection, it basically do nothing but copy. */ 26 | public class NoOp implements Projection { 27 | protected DataType targetDataType; 28 | 29 | public NoOp(DataType dataType) { 30 | this.targetDataType = dataType; 31 | } 32 | 33 | @Override 34 | public void set(Object value, Row row, int pos) { 35 | row.set(pos, targetDataType, value); 36 | } 37 | 38 | @Override 39 | public int size() { 40 | return 1; 41 | } 42 | 43 | @Override 44 | public List getTypes() { 45 | return ImmutableList.of(targetDataType); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/util/ExponentialBackOff.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.util; 19 | 20 | import com.google.common.base.Preconditions; 21 | 22 | public class ExponentialBackOff implements BackOff { 23 | private int attempts; 24 | private int counter; 25 | 26 | public ExponentialBackOff(int attempts) { 27 | Preconditions.checkArgument(attempts >= 1, "Retry count cannot be less than 1."); 28 | this.counter = 1; 29 | this.attempts = attempts; 30 | } 31 | 32 | @Override 33 | public void reset() { 34 | this.counter = 1; 35 | } 36 | 37 | /** 38 | * produces 0 1 1 2 3 ... fibonacci series number. 39 | */ 40 | @Override 41 | public long nextBackOffMillis() { 42 | if(attempts <= counter) { 43 | return BackOff.STOP; 44 | } 45 | long millis = (counter<<2) * 1000; 46 | counter++; 47 | return millis; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/TimeType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import com.pingcap.tikv.codec.CodecDataInput; 21 | import com.pingcap.tikv.codec.UnsupportedTypeException; 22 | import com.pingcap.tikv.meta.TiColumnInfo; 23 | 24 | /** 25 | * Type TimeType 26 | * refers to mysql time 27 | */ 28 | public class TimeType extends DateTimeType { 29 | private TimeType(int tp) { 30 | super(tp); 31 | } 32 | 33 | protected TimeType(TiColumnInfo.InternalTypeHolder holder) { 34 | super(holder); 35 | } 36 | 37 | static TimeType of(int tp) { 38 | return new TimeType(tp); 39 | } 40 | 41 | public String simpleTypeName() { return "time"; } 42 | 43 | // Time is not supported yet 44 | @Override 45 | public Object decodeNotNull(int flag, CodecDataInput cdi) { 46 | throw new UnsupportedTypeException("Time type is not supported yet"); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/expression/TiExprTest.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression; 17 | 18 | 19 | import static org.junit.Assert.assertFalse; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import com.pingcap.tikv.expression.scalar.And; 23 | import com.pingcap.tikv.expression.scalar.Not; 24 | import com.pingcap.tikv.expression.scalar.Or; 25 | import org.junit.Test; 26 | 27 | public class TiExprTest { 28 | @Test 29 | public void isSupportedExprTest() { 30 | ExpressionBlacklist blackList = new ExpressionBlacklist("And, , Test"); 31 | Or or = new Or(TiConstant.create(1), TiConstant.create(1)); 32 | And and = new And(TiConstant.create(1), or); 33 | Not not = new Not(or); 34 | Not notFail = new Not(and); 35 | 36 | assertTrue(or.isSupportedExpr(blackList)); 37 | assertFalse(and.isSupportedExpr(blackList)); 38 | assertTrue(not.isSupportedExpr(blackList)); 39 | assertFalse(notFail.isSupportedExpr(blackList)); 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/In.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import static com.google.common.base.Preconditions.checkArgument; 19 | import static java.util.Objects.requireNonNull; 20 | 21 | import com.pingcap.tidb.tipb.ExprType; 22 | import com.pingcap.tikv.expression.TiExpr; 23 | import com.pingcap.tikv.types.DataType; 24 | import com.pingcap.tikv.types.IntegerType; 25 | 26 | public class In extends TiScalarFunction { 27 | public In(TiExpr... args) { 28 | super(args); 29 | } 30 | 31 | @Override 32 | protected ExprType getExprType() { 33 | return ExprType.In; 34 | } 35 | 36 | @Override 37 | protected void validateArguments(TiExpr... args) throws RuntimeException { 38 | requireNonNull(args, "Expressions cannot be null"); 39 | checkArgument(args.length >= 2, "Value list cannot be empty for In Expression"); 40 | } 41 | 42 | @Override 43 | public DataType getType() { 44 | return IntegerType.DEF_BOOLEAN_TYPE; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | sudo: required 3 | language: java 4 | os: 5 | - linux 6 | 7 | env: 8 | - V=0.7.0 9 | 10 | before_install: 11 | - OS=linux 12 | - ARCH=x86_64 13 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then OS=darwin; fi 14 | - GH_BASE="https://github.com/bazelbuild/bazel/releases/download/$V" 15 | - GH_ARTIFACT="bazel-$V-installer-$OS-$ARCH.sh" 16 | - CI_BASE="http://ci.bazel.io/job/Bazel/JAVA_VERSION=1.8,PLATFORM_NAME=$OS-$ARCH/lastSuccessfulBuild/artifact/output/ci" 17 | - CI_ARTIFACT="bazel--installer.sh" 18 | - URL="$GH_BASE/$GH_ARTIFACT" 19 | - if [[ "$V" == "HEAD" ]]; then CI_ARTIFACT="`wget -qO- $CI_BASE | grep -o 'bazel-[-_a-zA-Z0-9\.]*-installer.sh' | uniq`"; fi 20 | - if [[ "$V" == "HEAD" ]]; then URL="$CI_BASE/$CI_ARTIFACT"; fi 21 | - echo $URL 22 | - wget -O install.sh $URL 23 | - chmod +x install.sh 24 | - ./install.sh --user 25 | - rm -f install.sh 26 | 27 | install: 28 | - echo "skip" 29 | script: 30 | - | 31 | bazel \ 32 | --output_base=$HOME/.cache/bazel \ 33 | --batch \ 34 | --host_jvm_args=-Xmx500m \ 35 | --host_jvm_args=-Xms500m \ 36 | test \ 37 | --verbose_failures \ 38 | --test_output=errors \ 39 | --test_strategy=standalone \ 40 | --spawn_strategy=standalone \ 41 | --genrule_strategy=standalone \ 42 | --local_resources=400,2,1.0 \ 43 | --worker_verbose \ 44 | --strategy=Javac=worker \ 45 | --strategy=Closure=worker \ 46 | --deleted_packages=tests/external_proto_library \ 47 | --test_timeout=3600 \ 48 | //src/... \ 49 | $FLAGS \ 50 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/meta/IndexType.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.meta; 17 | 18 | import com.pingcap.tikv.exception.TiClientInternalException; 19 | 20 | // Actually we are not using either real btree or hash index 21 | // TiDB has its own way for indexing as key value pair. 22 | public enum IndexType { 23 | IndexTypeInvalid(0), 24 | IndexTypeBtree(1), 25 | IndexTypeHash(2); 26 | 27 | private final int type; 28 | 29 | IndexType(int type) { 30 | this.type = type; 31 | } 32 | 33 | public static IndexType fromValue(int type) { 34 | for (IndexType e : IndexType.values()) { 35 | if (e.type == type) { 36 | return e; 37 | } 38 | } 39 | throw new TiClientInternalException("Invalid index type code: " + type); 40 | } 41 | 42 | public int getTypeCode() { 43 | return type; 44 | } 45 | 46 | public String toString() { 47 | switch (this.type){ 48 | case 1: 49 | return "BTREE"; 50 | case 2: 51 | return "HASH"; 52 | } 53 | return "Invalid"; 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/policy/RetryNTimes.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.policy; 17 | 18 | import com.pingcap.tikv.exception.TiClientInternalException; 19 | import com.pingcap.tikv.operation.ErrorHandler; 20 | import com.pingcap.tikv.util.BackOff; 21 | 22 | public class RetryNTimes extends RetryPolicy { 23 | private RetryNTimes(ErrorHandler handler, BackOff backOff) { 24 | super(handler); 25 | this.backOff = backOff; 26 | } 27 | 28 | public static class Builder implements RetryPolicy.Builder { 29 | private BackOff backOff; 30 | 31 | public Builder(int n, Class backoffClass) { 32 | try { 33 | this.backOff = backoffClass.getConstructor(int.class).newInstance(n); 34 | } catch (Exception e) { 35 | throw new TiClientInternalException("failed to create backoff object", e); 36 | } 37 | } 38 | 39 | @Override 40 | public RetryPolicy create(ErrorHandler handler) { 41 | return new RetryNTimes<>(handler, backOff); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/Blacklist.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.expression; 19 | 20 | import java.util.HashSet; 21 | import java.util.Set; 22 | import java.util.stream.Collectors; 23 | 24 | import static java.util.Objects.requireNonNull; 25 | 26 | public class Blacklist { 27 | private final Set unsupported = new HashSet<>(); 28 | 29 | Blacklist(String string) { 30 | if (string != null) { 31 | String [] some = string.split(","); 32 | for (String one : some) { 33 | String trimmedExprName = one.trim(); 34 | if (!trimmedExprName.isEmpty()) { 35 | unsupported.add(one.trim()); 36 | } 37 | } 38 | } 39 | } 40 | 41 | boolean isUnsupported(String name) { 42 | return unsupported.contains(name); 43 | } 44 | 45 | boolean isUnsupported(Class cls) { 46 | return isUnsupported(requireNonNull(cls).getSimpleName()); 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return unsupported.stream().collect(Collectors.joining(",")); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Not.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import static com.google.common.base.Preconditions.checkArgument; 19 | 20 | import com.pingcap.tidb.tipb.ExprType; 21 | import com.pingcap.tidb.tipb.ScalarFuncSig; 22 | import com.pingcap.tikv.expression.TiExpr; 23 | import com.pingcap.tikv.types.DataType; 24 | import com.pingcap.tikv.types.IntegerType; 25 | 26 | public class Not extends TiScalarFunction { 27 | public Not(TiExpr arg) { 28 | super(arg); 29 | } 30 | 31 | @Override 32 | protected ExprType getExprType() { 33 | return ExprType.Not; 34 | } 35 | 36 | @Override 37 | public DataType getType() { 38 | return IntegerType.DEF_BOOLEAN_TYPE; 39 | } 40 | 41 | @Override 42 | protected void validateArguments(TiExpr... args) throws RuntimeException { 43 | super.validateArguments(args); 44 | checkArgument(this.args.get(0).getType() instanceof IntegerType); 45 | } 46 | 47 | @Override 48 | ScalarFuncSig getSignature() { 49 | return ScalarFuncSig.UnaryNot; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/types/DecimalTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import com.pingcap.tikv.codec.CodecDataInput; 23 | import com.pingcap.tikv.codec.CodecDataOutput; 24 | import java.math.BigDecimal; 25 | import org.junit.Test; 26 | 27 | public class DecimalTypeTest { 28 | @Test 29 | public void writeDoubleAndReadDoubleTest() { 30 | // issue scientific notation in toBin 31 | CodecDataOutput cdo = new CodecDataOutput(); 32 | DecimalType.writeDouble(cdo, 0.01); 33 | double u = DecimalType.readDouble(new CodecDataInput(cdo.toBytes())); 34 | assertEquals(0.01, u, 0.0001); 35 | 36 | cdo.reset(); 37 | DecimalType.writeDouble(cdo, 206.0); 38 | u = DecimalType.readDouble(new CodecDataInput(cdo.toBytes())); 39 | assertEquals(206.0, u, 0.0001); 40 | 41 | cdo.reset(); 42 | DecimalType.writeDecimal(cdo, BigDecimal.valueOf(206.0)); 43 | u = DecimalType.readDouble(new CodecDataInput(cdo.toBytes())); 44 | assertEquals(206.0, u, 0.0001); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/TiByItem.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression; 17 | 18 | import com.pingcap.tidb.tipb.ByItem; 19 | 20 | import java.io.Serializable; 21 | 22 | import static com.google.common.base.Preconditions.checkNotNull; 23 | 24 | public class TiByItem implements Serializable { 25 | private TiExpr expr; 26 | private boolean desc; 27 | 28 | public static TiByItem create(TiExpr expr, boolean desc) { 29 | return new TiByItem(expr, desc); 30 | } 31 | 32 | private TiByItem(TiExpr expr, boolean desc) { 33 | checkNotNull(expr, "Expr cannot be null for ByItem"); 34 | 35 | this.expr = expr; 36 | this.desc = desc; 37 | } 38 | 39 | public ByItem toProto() { 40 | ByItem.Builder builder = ByItem.newBuilder(); 41 | return builder.setExpr(expr.toProto()) 42 | .setDesc(desc) 43 | .build(); 44 | } 45 | 46 | public TiExpr getExpr() { 47 | return expr; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return String.format("[%s %s]", expr.toString(), desc ? "DESC" : "ASC"); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/predicates/PredicateUtilsTest.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.predicates; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | import com.pingcap.tikv.expression.TiConstant; 22 | import com.pingcap.tikv.expression.TiExpr; 23 | import com.pingcap.tikv.expression.scalar.And; 24 | import java.util.List; 25 | import org.junit.Test; 26 | 27 | public class PredicateUtilsTest { 28 | @Test 29 | public void mergeCNFExpressions() throws Exception { 30 | List exprs = 31 | ImmutableList.of( 32 | TiConstant.create(1), 33 | TiConstant.create(2), 34 | TiConstant.create(3), 35 | TiConstant.create(4), 36 | TiConstant.create(5)); 37 | 38 | TiExpr res = 39 | new And( 40 | TiConstant.create(1), 41 | new And( 42 | TiConstant.create(2), 43 | new And( 44 | TiConstant.create(3), new And(TiConstant.create(4), TiConstant.create(5))))); 45 | 46 | assertEquals(res, PredicateUtils.mergeCNFExpressions(exprs)); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Or.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import static com.google.common.base.Preconditions.checkArgument; 19 | 20 | import com.pingcap.tidb.tipb.ExprType; 21 | import com.pingcap.tidb.tipb.ScalarFuncSig; 22 | import com.pingcap.tikv.expression.TiExpr; 23 | import com.pingcap.tikv.types.IntegerType; 24 | 25 | public class Or extends TiScalarFunction { 26 | public Or(TiExpr lhs, TiExpr rhs) { 27 | super(lhs, rhs); 28 | } 29 | 30 | @Override 31 | protected ExprType getExprType() { 32 | return ExprType.Or; 33 | } 34 | 35 | @Override 36 | public IntegerType getType() { 37 | return IntegerType.DEF_BOOLEAN_TYPE; 38 | } 39 | 40 | @Override 41 | protected void validateArguments(TiExpr... args) throws RuntimeException { 42 | // Validate 2 arguments 43 | super.validateArguments(); 44 | // Validate 2 arguments are strings 45 | checkArgument(this.args.get(0).getType() instanceof IntegerType); 46 | checkArgument(this.args.get(1).getType() instanceof IntegerType); 47 | } 48 | 49 | @Override 50 | ScalarFuncSig getSignature() { 51 | return ScalarFuncSig.LogicalOr; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/transformer/MultiKeyDecoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation.transformer; 19 | 20 | import static java.util.Objects.requireNonNull; 21 | 22 | import com.google.common.collect.ImmutableList; 23 | import com.pingcap.tikv.codec.CodecDataInput; 24 | import com.pingcap.tikv.row.Row; 25 | import com.pingcap.tikv.types.DataType; 26 | import java.util.List; 27 | 28 | public class MultiKeyDecoder implements Projection { 29 | public MultiKeyDecoder(List dataTypes) { 30 | this.resultTypes = requireNonNull(dataTypes).toArray(new DataType[0]); 31 | } 32 | 33 | private DataType[] resultTypes; 34 | 35 | @Override 36 | public void set(Object value, Row row, int pos) { 37 | byte[] rowData = (byte[]) value; 38 | CodecDataInput cdi = new CodecDataInput(rowData); 39 | 40 | for (int i = 0; i < resultTypes.length; i++) { 41 | resultTypes[i].decodeValueToRow(cdi, row, i + pos); 42 | } 43 | } 44 | 45 | @Override 46 | public int size() { 47 | return resultTypes.length; 48 | } 49 | 50 | @Override 51 | public List getTypes() { 52 | return ImmutableList.copyOf(resultTypes); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/util/FutureObserver.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.util; 17 | 18 | import com.google.common.util.concurrent.SettableFuture; 19 | import com.pingcap.tikv.kvproto.Pdpb; 20 | import com.pingcap.tikv.operation.ErrorHandler; 21 | import io.grpc.stub.StreamObserver; 22 | import java.util.concurrent.Future; 23 | 24 | public class FutureObserver implements StreamObserver { 25 | private final SettableFuture resultFuture; 26 | private final Getter getter; 27 | 28 | public interface Getter { 29 | Value getValue(RespT resp); 30 | } 31 | 32 | public FutureObserver(Getter getter) { 33 | this.resultFuture = SettableFuture.create(); 34 | this.getter = getter; 35 | } 36 | 37 | public Value getValue(RespT resp) { 38 | return getter.getValue(resp); 39 | } 40 | 41 | @Override 42 | public void onNext(RespT resp) { 43 | resultFuture.set(getValue(resp)); 44 | } 45 | 46 | @Override 47 | public void onError(Throwable t) { 48 | resultFuture.setException(t); 49 | } 50 | 51 | @Override 52 | public void onCompleted() {} 53 | 54 | public Future getFuture() { 55 | return resultFuture; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/And.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import static com.google.common.base.Preconditions.checkArgument; 19 | 20 | import com.pingcap.tidb.tipb.ExprType; 21 | import com.pingcap.tidb.tipb.ScalarFuncSig; 22 | import com.pingcap.tikv.expression.TiExpr; 23 | import com.pingcap.tikv.types.DataType; 24 | import com.pingcap.tikv.types.IntegerType; 25 | 26 | public class And extends TiScalarFunction { 27 | public And(TiExpr lhs, TiExpr rhs) { 28 | super(lhs, rhs); 29 | } 30 | 31 | @Override 32 | protected ExprType getExprType() { 33 | return ExprType.And; 34 | } 35 | 36 | @Override 37 | public DataType getType() { 38 | return IntegerType.DEF_BOOLEAN_TYPE; 39 | } 40 | 41 | @Override 42 | protected void validateArguments(TiExpr... args) throws RuntimeException { 43 | // Validate 2 arguments 44 | super.validateArguments(); 45 | // Validate 2 arguments are boolean 46 | checkArgument(this.args.get(0).getType() instanceof IntegerType); 47 | checkArgument(this.args.get(1).getType() instanceof IntegerType); 48 | } 49 | 50 | @Override 51 | ScalarFuncSig getSignature() { 52 | return ScalarFuncSig.LogicalAnd; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/LogicalXor.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import static com.google.common.base.Preconditions.checkArgument; 19 | 20 | import com.pingcap.tidb.tipb.ExprType; 21 | import com.pingcap.tidb.tipb.ScalarFuncSig; 22 | import com.pingcap.tikv.expression.TiExpr; 23 | import com.pingcap.tikv.types.DataType; 24 | import com.pingcap.tikv.types.IntegerType; 25 | 26 | public class LogicalXor extends TiScalarFunction { 27 | public LogicalXor(TiExpr lhs, TiExpr rhs) { 28 | super(lhs, rhs); 29 | } 30 | 31 | @Override 32 | protected ExprType getExprType() { 33 | return ExprType.Xor; 34 | } 35 | 36 | @Override 37 | public DataType getType() { 38 | return IntegerType.DEF_BOOLEAN_TYPE; 39 | } 40 | 41 | @Override 42 | protected void validateArguments(TiExpr... args) throws RuntimeException { 43 | // Validate 2 arguments 44 | super.validateArguments(); 45 | // Validate 2 arguments are strings 46 | checkArgument(this.args.get(0).getType() instanceof IntegerType); 47 | checkArgument(this.args.get(1).getType() instanceof IntegerType); 48 | } 49 | 50 | @Override 51 | ScalarFuncSig getSignature() { 52 | return ScalarFuncSig.LogicalXor; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/predicates/SelectivityCalculator.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.predicates; 17 | 18 | 19 | import com.pingcap.tikv.expression.TiExpr; 20 | import com.pingcap.tikv.expression.scalar.Equal; 21 | import com.pingcap.tikv.expression.scalar.GreaterEqual; 22 | import com.pingcap.tikv.expression.scalar.GreaterThan; 23 | import com.pingcap.tikv.expression.scalar.LessEqual; 24 | import com.pingcap.tikv.expression.scalar.LessThan; 25 | import com.pingcap.tikv.expression.scalar.NullEqual; 26 | 27 | public class SelectivityCalculator { 28 | public static final double SELECTION_FACTOR = 100; 29 | public static final double EQUAL_RATE = 0.01; 30 | public static final double LESS_RATE = 0.1; 31 | 32 | public static double calcPseudoSelectivity(Iterable exprs) { 33 | double minFactor = SELECTION_FACTOR; 34 | for (TiExpr expr : exprs) { 35 | if (expr instanceof Equal || expr instanceof NullEqual) { 36 | minFactor *= EQUAL_RATE; 37 | } else if ( 38 | expr instanceof GreaterEqual || 39 | expr instanceof GreaterThan || 40 | expr instanceof LessEqual || 41 | expr instanceof LessThan) { 42 | minFactor *= LESS_RATE; 43 | } 44 | } 45 | return minFactor; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/Like.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import static com.google.common.base.Preconditions.checkArgument; 19 | 20 | import com.pingcap.tidb.tipb.ExprType; 21 | import com.pingcap.tidb.tipb.ScalarFuncSig; 22 | import com.pingcap.tikv.expression.TiExpr; 23 | import com.pingcap.tikv.types.BytesType; 24 | import com.pingcap.tikv.types.DataType; 25 | import com.pingcap.tikv.types.IntegerType; 26 | 27 | public class Like extends TiScalarFunction { 28 | public Like(TiExpr lhs, TiExpr rhs) { 29 | super(lhs, rhs); 30 | } 31 | 32 | @Override 33 | protected ExprType getExprType() { 34 | return ExprType.Like; 35 | } 36 | 37 | @Override 38 | public DataType getType() { 39 | return IntegerType.DEF_BOOLEAN_TYPE; 40 | } 41 | 42 | @Override 43 | protected void validateArguments(TiExpr... args) throws RuntimeException { 44 | // Validate 2 arguments 45 | super.validateArguments(); 46 | // Validate 2 arguments are strings 47 | checkArgument(this.args.get(0).getType() instanceof BytesType); 48 | checkArgument(this.args.get(1).getType() instanceof BytesType); 49 | } 50 | 51 | @Override 52 | ScalarFuncSig getSignature() { 53 | return ScalarFuncSig.LikeSig; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/meta/TiIndexColumn.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.meta; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.pingcap.tikv.types.DataType; 21 | import java.io.Serializable; 22 | 23 | public class TiIndexColumn implements Serializable { 24 | private String name; 25 | private int offset; 26 | private long length; 27 | 28 | @JsonCreator 29 | TiIndexColumn( 30 | @JsonProperty("name") CIStr name, 31 | @JsonProperty("offset") int offset, 32 | @JsonProperty("length") long length) { 33 | this.name = name.getL(); 34 | this.offset = offset; 35 | this.length = length; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public int getOffset() { 43 | return offset; 44 | } 45 | 46 | public long getLength() { 47 | return length; 48 | } 49 | 50 | public boolean isPrefixIndex() { 51 | return length != DataType.UNSPECIFIED_LEN; 52 | } 53 | 54 | public boolean matchName(String otherName) { 55 | return name.equalsIgnoreCase(otherName); 56 | } 57 | 58 | @Override 59 | public String toString() { 60 | return String.format( 61 | "%s {name: %s, offset: %d, length: %d}", getClass().getSimpleName(), name, offset, length); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/predicates/PredicateUtils.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.predicates; 17 | 18 | import static java.util.Objects.requireNonNull; 19 | 20 | import com.pingcap.tikv.expression.TiColumnRef; 21 | import com.pingcap.tikv.expression.TiExpr; 22 | import com.pingcap.tikv.expression.TiFunctionExpression; 23 | import com.pingcap.tikv.expression.scalar.And; 24 | import java.util.HashSet; 25 | import java.util.List; 26 | import java.util.Set; 27 | 28 | public class PredicateUtils { 29 | public static TiExpr mergeCNFExpressions(List exprs) { 30 | requireNonNull(exprs); 31 | if (exprs.size() == 0) return null; 32 | if (exprs.size() == 1) return exprs.get(0); 33 | 34 | return new And(exprs.get(0), mergeCNFExpressions(exprs.subList(1, exprs.size()))); 35 | } 36 | 37 | public static Set extractColumnRefFromExpr(TiExpr expr) { 38 | Set columnRefs = new HashSet<>(); 39 | if (expr instanceof TiFunctionExpression) { 40 | TiFunctionExpression tiF = (TiFunctionExpression) expr; 41 | for (TiExpr arg : tiF.getArgs()) { 42 | if (arg instanceof TiColumnRef) { 43 | TiColumnRef tiCR = (TiColumnRef) arg; 44 | columnRefs.add(tiCR); 45 | } 46 | } 47 | } else if (expr instanceof TiColumnRef) { 48 | columnRefs.add(((TiColumnRef) expr)); 49 | } 50 | return columnRefs; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/row/Row.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.row; 19 | 20 | import com.pingcap.tikv.types.DataType; 21 | import java.sql.Date; 22 | import java.sql.Time; 23 | import java.sql.Timestamp; 24 | 25 | /** 26 | * Even in case of mem-buffer-based row we can ignore field types when en/decoding if we put some 27 | * padding bits for fixed length and use fixed length index for var-length 28 | */ 29 | public interface Row { 30 | void setNull(int pos); 31 | 32 | boolean isNull(int pos); 33 | 34 | void setFloat(int pos, float v); 35 | 36 | float getFloat(int pos); 37 | 38 | void setDouble(int pos, double v); 39 | 40 | double getDouble(int pos); 41 | 42 | void setInteger(int pos, int v); 43 | 44 | int getInteger(int pos); 45 | 46 | void setShort(int pos, short v); 47 | 48 | short getShort(int pos); 49 | 50 | void setLong(int pos, long v); 51 | 52 | long getLong(int pos); 53 | 54 | void setString(int pos, String v); 55 | 56 | String getString(int pos); 57 | 58 | void setTime(int pos, Time v); 59 | 60 | Date getTime(int pos); 61 | 62 | void setTimestamp(int pos, Timestamp v); 63 | 64 | Timestamp getTimestamp(int pos); 65 | 66 | void setDate(int pos, Date v); 67 | 68 | Date getDate(int pos); 69 | 70 | void setBytes(int pos, byte[] v); 71 | 72 | byte[] getBytes(int pos); 73 | 74 | void set(int pos, DataType type, Object v); 75 | 76 | Object get(int pos, DataType type); 77 | 78 | int fieldCount(); 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/util/BackOff.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.util; 19 | 20 | public interface BackOff { 21 | /** Indicates that no more retries should be made for use in {@link #nextBackOffMillis()}. */ 22 | long STOP = -1L; 23 | /** Reset to initial state. */ 24 | void reset(); 25 | 26 | /** 27 | * Gets the number of milliseconds to wait before retrying the operation or {@link #STOP} to 28 | * indicate that no retries should be made. 29 | * 30 | *

31 | * Example usage: 32 | *

33 | * 34 | *
35 |    long backOffMillis = backoff.nextBackOffMillis();
36 |    if (backOffMillis == Backoff.STOP) {
37 |    // do not retry operation
38 |    } else {
39 |    // sleep for backOffMillis milliseconds and retry operation
40 |    }
41 |    * 
42 | */ 43 | long nextBackOffMillis(); 44 | 45 | /** 46 | * Fixed back-off policy whose back-off time is always zero, meaning that the operation is retried 47 | * immediately without waiting. 48 | */ 49 | BackOff ZERO_BACKOFF = new BackOff() { 50 | 51 | public void reset() { 52 | } 53 | 54 | public long nextBackOffMillis() { 55 | return 0; 56 | } 57 | }; 58 | 59 | /** 60 | * Fixed back-off policy that always returns {@code #STOP} for {@link #nextBackOffMillis()}, 61 | * meaning that the operation should not be retried. 62 | */ 63 | BackOff STOP_BACKOFF = new BackOff() { 64 | 65 | public void reset() { 66 | } 67 | 68 | public long nextBackOffMillis() { 69 | return STOP; 70 | } 71 | }; 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/tools/RegionUtils.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.tools; 17 | 18 | 19 | import static java.util.Objects.requireNonNull; 20 | 21 | import com.google.common.collect.ImmutableList; 22 | import com.pingcap.tikv.TiSession; 23 | import com.pingcap.tikv.meta.TiTableInfo; 24 | import com.pingcap.tikv.predicates.ScanBuilder; 25 | import com.pingcap.tikv.predicates.ScanBuilder.ScanPlan; 26 | import com.pingcap.tikv.util.RangeSplitter; 27 | import com.pingcap.tikv.util.RangeSplitter.RegionTask; 28 | import java.util.HashMap; 29 | import java.util.List; 30 | import java.util.Map; 31 | 32 | public class RegionUtils { 33 | static public Map 34 | getRegionDistribution(TiSession session, String databaseName, String tableName) { 35 | requireNonNull(session, "session is null"); 36 | requireNonNull(databaseName, "databaseName is null"); 37 | requireNonNull(tableName, "tableName is null"); 38 | TiTableInfo table = session.getCatalog().getTable(databaseName, tableName); 39 | requireNonNull(table, String.format("Table not found %s.%s", databaseName, tableName)); 40 | ScanBuilder builder = new ScanBuilder(); 41 | ScanPlan scanPlan = builder.buildScan(ImmutableList.of(), table); 42 | List tasks = RangeSplitter 43 | .newSplitter(session.getRegionManager()) 44 | .splitRangeByRegion(scanPlan.getKeyRanges()); 45 | Map regionMap = new HashMap<>(); 46 | for (RegionTask task : tasks) { 47 | regionMap.merge(task.getHost() + "_" + task.getStore().getId(), 1, Integer::sum); 48 | } 49 | return regionMap; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/ReadOnlyPDClient.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv; 17 | 18 | import com.google.protobuf.ByteString; 19 | import com.pingcap.tikv.kvproto.Metapb.Store; 20 | import com.pingcap.tikv.meta.TiTimestamp; 21 | import com.pingcap.tikv.region.TiRegion; 22 | import java.util.concurrent.Future; 23 | 24 | /** Readonly PD client including only reading related interface Supposed for TiDB-like use cases */ 25 | public interface ReadOnlyPDClient { 26 | /** 27 | * Get Timestamp from Placement Driver 28 | * 29 | * @return a timestamp object 30 | */ 31 | TiTimestamp getTimestamp(); 32 | 33 | /** 34 | * Get Region from PD by key specified 35 | * 36 | * @param key key in bytes for locating a region 37 | * @return the region whose startKey and endKey range covers the given key 38 | */ 39 | TiRegion getRegionByKey(ByteString key); 40 | 41 | Future getRegionByKeyAsync(ByteString key); 42 | 43 | /** 44 | * Get Region by Region Id 45 | * 46 | * @param id Region Id 47 | * @return the region corresponding to the given Id 48 | */ 49 | TiRegion getRegionByID(long id); 50 | 51 | Future getRegionByIDAsync(long id); 52 | 53 | /** 54 | * Get Store by StoreId 55 | * 56 | * @param storeId StoreId 57 | * @return the Store corresponding to the given Id 58 | */ 59 | Store getStore(long storeId); 60 | 61 | Future getStoreAsync(long storeId); 62 | 63 | /** 64 | * Close underlining resources 65 | * 66 | */ 67 | void close() throws InterruptedException; 68 | 69 | /** Get associated session * @return the session associated to client */ 70 | TiSession getSession(); 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/streaming/StreamingResponse.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.streaming; 17 | 18 | import com.pingcap.tikv.kvproto.Coprocessor; 19 | import com.pingcap.tikv.kvproto.Errorpb; 20 | 21 | import javax.annotation.Nonnull; 22 | import java.util.ArrayList; 23 | import java.util.Iterator; 24 | import java.util.List; 25 | 26 | import static java.util.Objects.requireNonNull; 27 | 28 | public class StreamingResponse implements Iterable { 29 | private Iterator resultIterator; 30 | private List responseList; 31 | 32 | @SuppressWarnings("unchecked") 33 | public StreamingResponse(Iterator resultIterator) { 34 | requireNonNull(resultIterator, "Streaming result cannot be null!"); 35 | this.resultIterator = resultIterator; 36 | responseList = new ArrayList<>(); 37 | fetchStreamingResult(); 38 | } 39 | 40 | private void fetchStreamingResult() { 41 | while (resultIterator.hasNext()) { 42 | responseList.add(resultIterator.next()); 43 | } 44 | } 45 | 46 | public boolean hasRegionError() { 47 | for (Coprocessor.Response response : responseList) { 48 | if (response.hasRegionError()) { 49 | return true; 50 | } 51 | } 52 | 53 | return false; 54 | } 55 | 56 | public Errorpb.Error getFirstError() { 57 | for (Coprocessor.Response response : responseList) { 58 | if (response.hasRegionError()) { 59 | return response.getRegionError(); 60 | } 61 | } 62 | 63 | return null; 64 | } 65 | 66 | @Override 67 | @Nonnull 68 | public Iterator iterator() { 69 | return responseList.iterator(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/util/ComparablesTest.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.util; 17 | 18 | import static org.junit.Assert.assertTrue; 19 | 20 | import com.google.protobuf.ByteString; 21 | import java.util.function.Function; 22 | import org.junit.Test; 23 | 24 | public class ComparablesTest { 25 | @Test 26 | public void wrapTest() throws Exception { 27 | // compared as unsigned 28 | testBytes(new byte[] {1, 2, -1, 10}, new byte[] {1, 2, 0, 10}, x -> x > 0); 29 | testBytes(new byte[] {1, 2, 0, 10}, new byte[] {1, 2, 0, 10}, x -> x == 0); 30 | testBytes(new byte[] {1, 2, 0, 10}, new byte[] {1, 2, 1, 10}, x -> x < 0); 31 | testBytes(new byte[] {1, 2, 0, 10}, new byte[] {1, 2, 0}, x -> x > 0); 32 | 33 | testComparable(1, 2, x -> x < 0); 34 | testComparable(13, 13, x -> x == 0); 35 | testComparable(13, 2, x -> x > 0); 36 | } 37 | 38 | private void testBytes(byte[] lhs, byte[] rhs, Function tester) { 39 | ByteString lhsBS = ByteString.copyFrom(lhs); 40 | ByteString rhsBS = ByteString.copyFrom(rhs); 41 | 42 | Comparable lhsComp = Comparables.wrap(lhsBS); 43 | Comparable rhsComp = Comparables.wrap(rhsBS); 44 | 45 | assertTrue(tester.apply(lhsComp.compareTo(rhsComp))); 46 | 47 | lhsComp = Comparables.wrap(lhs); 48 | rhsComp = Comparables.wrap(rhs); 49 | 50 | assertTrue(tester.apply(lhsComp.compareTo(rhsComp))); 51 | } 52 | 53 | private void testComparable(Object lhs, Object rhs, Function tester) { 54 | Comparable lhsComp = Comparables.wrap(lhs); 55 | Comparable rhsComp = Comparables.wrap(rhs); 56 | 57 | assertTrue(tester.apply(lhsComp.compareTo(rhsComp))); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/Main.java: -------------------------------------------------------------------------------- 1 | package com.pingcap.tikv; 2 | 3 | 4 | import com.pingcap.tikv.catalog.Catalog; 5 | import com.pingcap.tikv.expression.TiColumnRef; 6 | import com.pingcap.tikv.expression.aggregate.Max; 7 | import com.pingcap.tikv.expression.aggregate.Min; 8 | import com.pingcap.tikv.expression.aggregate.Sum; 9 | 10 | import com.pingcap.tikv.meta.TiDAGRequest; 11 | import com.pingcap.tikv.meta.TiDAGRequest.PushDownType; 12 | import com.pingcap.tikv.meta.TiDBInfo; 13 | import com.pingcap.tikv.meta.TiTableInfo; 14 | import com.pingcap.tikv.operation.SchemaInfer; 15 | import com.pingcap.tikv.predicates.ScanBuilder; 16 | import com.pingcap.tikv.row.Row; 17 | import org.apache.log4j.Logger; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Iterator; 21 | 22 | 23 | public class Main { 24 | private static final Logger logger = Logger.getLogger(Main.class); 25 | 26 | public static void main(String[] args) throws Exception { 27 | TiConfiguration conf = TiConfiguration.createDefault("127.0.0.1:2379"); 28 | TiSession session = TiSession.create(conf); 29 | Catalog cat = session.getCatalog(); 30 | TiDBInfo db = cat.getDatabase("tispark_test"); 31 | TiTableInfo table = cat.getTable(db, "full_data_type_table"); 32 | Snapshot snapshot = session.createSnapshot(); 33 | TiDAGRequest selectRequest = new TiDAGRequest(PushDownType.NORMAL); 34 | ScanBuilder scanBuilder = new ScanBuilder(); 35 | ScanBuilder.ScanPlan scanPlan = scanBuilder.buildScan(new ArrayList<>(), table); 36 | TiColumnRef col = TiColumnRef.create("tp_tinyint", table); 37 | TiColumnRef col2 = TiColumnRef.create("tp_int", table); 38 | selectRequest 39 | .addAggregate(new Sum(col)) 40 | .addAggregate(new Min(col)) 41 | .addAggregate(new Max(col)) 42 | .addRequiredColumn(col) 43 | .setStartTs(snapshot.getVersion()) 44 | .addRanges(scanPlan.getKeyRanges()) 45 | .setTableInfo(table); 46 | System.out.println(selectRequest); 47 | Iterator it = snapshot.tableRead(selectRequest); 48 | while (it.hasNext()) { 49 | Row r = it.next(); 50 | SchemaInfer schemaInfer = SchemaInfer.create(selectRequest); 51 | for (int i = 0; i < r.fieldCount(); i++) { 52 | Object val = r.get(i, schemaInfer.getType(i)); 53 | System.out.print(val); 54 | } 55 | System.out.println(); 56 | } 57 | session.close(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/meta/TiDBInfo.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.meta; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import java.util.List; 21 | 22 | public class TiDBInfo { 23 | private long id; 24 | private String name; 25 | private String charset; 26 | private String collate; 27 | private List tables; 28 | private SchemaState schemaState; 29 | 30 | @JsonCreator 31 | public TiDBInfo( 32 | @JsonProperty("id") long id, 33 | @JsonProperty("db_name") CIStr name, 34 | @JsonProperty("charset") String charset, 35 | @JsonProperty("collate") String collate, 36 | @JsonProperty("-") List tables, 37 | @JsonProperty("state") int schemaState) { 38 | this.id = id; 39 | this.name = name.getL(); 40 | this.charset = charset; 41 | this.collate = collate; 42 | this.tables = tables; 43 | this.schemaState = SchemaState.fromValue(schemaState); 44 | } 45 | 46 | public long getId() { 47 | return id; 48 | } 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public String getCharset() { 55 | return charset; 56 | } 57 | 58 | public String getCollate() { 59 | return collate; 60 | } 61 | 62 | public List getTables() { 63 | return tables; 64 | } 65 | 66 | SchemaState getSchemaState() { 67 | return schemaState; 68 | } 69 | 70 | @Override 71 | public boolean equals(Object other) { 72 | if (other == this) { 73 | return true; 74 | } 75 | if (!(other instanceof TiDBInfo)) { 76 | return false; 77 | } 78 | TiDBInfo otherDB = (TiDBInfo)other; 79 | return otherDB.getId() == getId() && otherDB.getName().equals(getName()); 80 | } 81 | 82 | @Override 83 | public int hashCode() { 84 | final int prime = 31; 85 | int result = prime + Long.hashCode(getId()); 86 | return result * prime + getName().hashCode(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/codec/KeyUtils.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.codec; 17 | 18 | import com.google.common.primitives.UnsignedBytes; 19 | import com.google.protobuf.ByteString; 20 | import java.util.Arrays; 21 | 22 | public class KeyUtils { 23 | private static final ByteString ZERO_BYTE = ByteString.copyFrom(new byte[] {0}); 24 | 25 | public static ByteString getNextKeyInByteOrder(ByteString key) { 26 | return key.concat(ZERO_BYTE); 27 | } 28 | 29 | public static byte[] getNextKeyInByteOrder(byte[] key) { 30 | return Arrays.copyOf(key, key.length + 1); 31 | } 32 | 33 | public static String formatBytes(byte[] bytes) { 34 | if (bytes == null) return "null"; 35 | StringBuilder sb = new StringBuilder(); 36 | for (int i = 0; i < bytes.length; i++) { 37 | int unsignedByte = UnsignedBytes.toInt(bytes[i]); 38 | sb.append(unsignedByte); 39 | if (i != bytes.length - 1) { 40 | sb.append(","); 41 | } 42 | } 43 | return sb.toString(); 44 | } 45 | 46 | public static String formatBytes(ByteString bytes) { 47 | if (bytes == null) return "null"; 48 | return formatBytes(bytes.toByteArray()); 49 | } 50 | 51 | /** 52 | * The next key for bytes domain It first plus one at LSB and if LSB overflows, a zero byte is 53 | * appended at the end Original bytes will be reused if possible 54 | * 55 | * @param key key to encode 56 | * @return encoded results 57 | */ 58 | public static byte[] prefixNext(byte[] key) { 59 | int i; 60 | for (i = key.length - 1; i >= 0; i--) { 61 | if (key[i] != UnsignedBytes.MAX_VALUE) { 62 | key[i]++; 63 | break; 64 | } 65 | } 66 | if (i == -1) { 67 | return getNextKeyInByteOrder(key); 68 | } 69 | return key; 70 | } 71 | 72 | public static boolean hasPrefix(ByteString str, ByteString prefix) { 73 | for (int i = 0; i < prefix.size(); i++) { 74 | if (str.byteAt(i) != prefix.byteAt(i)) { 75 | return false; 76 | } 77 | } 78 | return true; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/RawBytesType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import com.pingcap.tikv.codec.CodecDataInput; 21 | import com.pingcap.tikv.codec.CodecDataOutput; 22 | import com.pingcap.tikv.codec.InvalidCodecFormatException; 23 | import com.pingcap.tikv.meta.TiColumnInfo; 24 | 25 | /** 26 | * TODO: if we need to unify string type and binary types? Indeed they are encoded as the same 27 | * However, decode to string actually going through encoding/decoding by whatever charset.encoding 28 | * format we set, and essentially changed underlying data 29 | */ 30 | public class RawBytesType extends BytesType { 31 | static RawBytesType ofRaw(int tp) { 32 | return new RawBytesType(tp); 33 | } 34 | 35 | private RawBytesType(int tp) { 36 | super(tp); 37 | } 38 | 39 | protected RawBytesType(TiColumnInfo.InternalTypeHolder holder) { 40 | super(holder); 41 | } 42 | 43 | public String simpleTypeName() { return "binary"; } 44 | 45 | @Override 46 | public Object decodeNotNull(int flag, CodecDataInput cdi) { 47 | if (flag == COMPACT_BYTES_FLAG) { 48 | return readCompactBytes(cdi); 49 | } else if (flag == BYTES_FLAG) { 50 | return readBytes(cdi); 51 | } else { 52 | throw new InvalidCodecFormatException("Invalid Flag type for : " + flag); 53 | } 54 | } 55 | 56 | /** 57 | * encode value to cdo per type. If key, then it is memory comparable. If value, no guarantee. 58 | * 59 | * @param cdo destination of data. 60 | * @param encodeType Key or Value. 61 | * @param value need to be encoded. 62 | */ 63 | @Override 64 | public void encodeNotNull(CodecDataOutput cdo, EncodeType encodeType, Object value) { 65 | byte[] bytes; 66 | if (value instanceof byte[]) { 67 | bytes = (byte[]) value; 68 | } else { 69 | throw new UnsupportedOperationException("can not cast non bytes type to bytes array"); 70 | } 71 | if (encodeType == EncodeType.KEY) { 72 | writeBytes(cdo, bytes); 73 | } else { 74 | writeCompactBytes(cdo, bytes); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/transformer/Cast.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation.transformer; 19 | 20 | import com.pingcap.tikv.row.Row; 21 | import com.pingcap.tikv.types.*; 22 | import java.math.BigDecimal; 23 | 24 | public class Cast extends NoOp { 25 | public Cast(DataType type) { 26 | super(type); 27 | } 28 | 29 | @Override 30 | public void set(Object value, Row row, int pos) { 31 | Object casted; 32 | if (value == null) { 33 | row.set(pos, targetDataType, null); 34 | return; 35 | } 36 | if (targetDataType instanceof IntegerType) { 37 | casted = castToLong(value); 38 | } else if (targetDataType instanceof BytesType) { 39 | casted = castToString(value); 40 | } else if (targetDataType instanceof DecimalType) { 41 | casted = castToDecimal(value); 42 | } else if (targetDataType instanceof RealType) { 43 | casted = castToDouble(value); 44 | } else { 45 | throw new UnsupportedOperationException("only support cast to Long, Double and String"); 46 | } 47 | row.set(pos, targetDataType, casted); 48 | } 49 | 50 | public Double castToDouble(Object obj) { 51 | if (obj instanceof Number) { 52 | Number num = (Number) obj; 53 | return num.doubleValue(); 54 | } 55 | throw new UnsupportedOperationException("can not cast un-number to double "); 56 | } 57 | 58 | public BigDecimal castToDecimal(Object obj) { 59 | if (obj instanceof Number) { 60 | Number num = (Number) obj; 61 | return new BigDecimal(num.doubleValue()); 62 | } else if (obj instanceof BigDecimal) { 63 | return (BigDecimal) obj; 64 | } 65 | throw new UnsupportedOperationException( 66 | "can not cast to BigDecimal: " + obj == null ? "null" : obj.getClass().getSimpleName()); 67 | } 68 | 69 | public Long castToLong(Object obj) { 70 | if (obj instanceof Number) { 71 | Number num = (Number) obj; 72 | return num.longValue(); 73 | } 74 | throw new UnsupportedOperationException("can not cast un-number to long "); 75 | } 76 | 77 | public String castToString(Object obj) { 78 | return obj.toString(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/event/CacheInvalidateEvent.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.event; 17 | 18 | import java.io.Serializable; 19 | 20 | public class CacheInvalidateEvent implements Serializable { 21 | public enum CacheType { 22 | REGION_STORE, 23 | REQ_FAILED, 24 | LEADER 25 | } 26 | 27 | private long regionId; 28 | private long storeId; 29 | private boolean invalidateRegion; 30 | private boolean invalidateStore; 31 | private CacheType cacheType; 32 | 33 | public CacheInvalidateEvent(long regionId, long storeId, 34 | boolean updateRegion, boolean updateStore, 35 | CacheType type) { 36 | this.regionId = regionId; 37 | this.storeId = storeId; 38 | this.cacheType = type; 39 | if (updateRegion) { 40 | invalidateRegion(); 41 | } 42 | 43 | if (updateStore) { 44 | invalidateStore(); 45 | } 46 | } 47 | 48 | public long getRegionId() { 49 | return regionId; 50 | } 51 | 52 | public long getStoreId() { 53 | return storeId; 54 | } 55 | 56 | @Override 57 | public boolean equals(Object obj) { 58 | if (obj == this) { 59 | return true; 60 | } else if (obj instanceof CacheInvalidateEvent) { 61 | CacheInvalidateEvent event = (CacheInvalidateEvent) obj; 62 | return event.getRegionId() == getRegionId() && 63 | event.getStoreId() == getStoreId() && 64 | event.getCacheType() == getCacheType(); 65 | } 66 | return false; 67 | } 68 | 69 | @Override 70 | public int hashCode() { 71 | int result = 1106; 72 | result += result * 31 + getStoreId(); 73 | result += result * 31 + getRegionId(); 74 | result += result * 31 + getCacheType().name().hashCode(); 75 | return result; 76 | } 77 | 78 | public void invalidateRegion() { 79 | invalidateRegion = true; 80 | } 81 | 82 | public void invalidateStore() { 83 | invalidateStore = true; 84 | } 85 | 86 | public boolean shouldUpdateRegion() { 87 | return invalidateRegion; 88 | } 89 | 90 | public boolean shouldUpdateStore() { 91 | return invalidateStore; 92 | } 93 | 94 | public CacheType getCacheType() { 95 | return cacheType; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/scalar/TiScalarFunction.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression.scalar; 17 | 18 | import com.pingcap.tidb.tipb.Expr; 19 | import com.pingcap.tidb.tipb.ExprType; 20 | import com.pingcap.tidb.tipb.FieldType; 21 | import com.pingcap.tidb.tipb.ScalarFuncSig; 22 | import com.pingcap.tikv.expression.TiExpr; 23 | import com.pingcap.tikv.exception.TiExpressionException; 24 | import com.pingcap.tikv.expression.TiFunctionExpression; 25 | import com.pingcap.tikv.types.DataType; 26 | import com.pingcap.tikv.util.ScalarFuncInfer; 27 | 28 | /** 29 | * Scalar function 30 | * Used in DAG mode 31 | */ 32 | public abstract class TiScalarFunction extends TiFunctionExpression { 33 | TiScalarFunction(TiExpr... args) { 34 | super(args); 35 | } 36 | 37 | /** 38 | * Gets scalar function PB code representation. 39 | * 40 | * @return the pb code 41 | */ 42 | ScalarFuncSig getSignature() { 43 | return ScalarFuncInfer.of( 44 | getArgTypeCode(), 45 | getExprType() 46 | ); 47 | } 48 | 49 | @Override 50 | public String getName() { 51 | return getSignature().name(); 52 | } 53 | 54 | private DataType getArgType() { 55 | if (args.isEmpty()) { 56 | throw new TiExpressionException( 57 | "Scalar function's argument list cannot be empty!" 58 | ); 59 | } 60 | 61 | return args.get(0).getType(); 62 | } 63 | 64 | /** 65 | * Get scalar function argument type code 66 | * Note:In DAG mode, all the arguments' type should 67 | * be the same 68 | * 69 | * @return the arg type code 70 | */ 71 | public Integer getArgTypeCode() { 72 | return getArgType().getTypeCode(); 73 | } 74 | 75 | @Override 76 | public DataType getType() { 77 | return getArgType(); 78 | } 79 | 80 | @Override 81 | public Expr toProto() { 82 | Expr.Builder builder = Expr.newBuilder(); 83 | // Scalar function type 84 | builder.setTp(ExprType.ScalarFunc); 85 | // Return type 86 | builder.setFieldType( 87 | FieldType.newBuilder() 88 | .setTp( 89 | getType().getTypeCode() 90 | ) 91 | .build() 92 | ); 93 | // Set function signature 94 | builder.setSig(getSignature()); 95 | for (TiExpr arg : args) { 96 | builder.addChildren(arg.toProto()); 97 | } 98 | 99 | return builder.build(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/operation/ChunkIteratorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation; 19 | 20 | import static com.pingcap.tikv.types.Types.TYPE_LONG; 21 | import static com.pingcap.tikv.types.Types.TYPE_VARCHAR; 22 | import static org.junit.Assert.assertEquals; 23 | 24 | import com.google.protobuf.ByteString; 25 | import com.pingcap.tidb.tipb.Chunk; 26 | import com.pingcap.tidb.tipb.RowMeta; 27 | import com.pingcap.tikv.codec.CodecDataInput; 28 | import com.pingcap.tikv.operation.iterator.ChunkIterator; 29 | import com.pingcap.tikv.row.ObjectRowImpl; 30 | import com.pingcap.tikv.row.Row; 31 | import com.pingcap.tikv.types.DataType; 32 | import com.pingcap.tikv.types.DataTypeFactory; 33 | import java.util.ArrayList; 34 | import java.util.List; 35 | import org.junit.Before; 36 | import org.junit.Test; 37 | 38 | public class ChunkIteratorTest { 39 | private List chunks = new ArrayList<>(); 40 | @Before 41 | public void setup() { 42 | // 8 2 2 2 a 8 4 2 2 b 8 6 2 2 c 43 | // 1 a 2 b 3 c 44 | String chunkStr = "\b\u0002\u0002\u0002a\b\u0004\u0002\u0002b\b\u0006\u0002\u0002c"; 45 | Chunk chunk = Chunk.newBuilder(). 46 | setRowsData(ByteString.copyFromUtf8(chunkStr)) 47 | .addRowsMeta(0, RowMeta.newBuilder().setHandle(1).setLength(5)) 48 | .addRowsMeta(1, RowMeta.newBuilder().setHandle(2).setLength(5)) 49 | .addRowsMeta(2, RowMeta.newBuilder().setHandle(3).setLength(5)) 50 | .build(); 51 | chunks.add(chunk); 52 | } 53 | 54 | @Test 55 | public void chunkTest() { 56 | ChunkIterator chunkIterator = ChunkIterator.getRawBytesChunkIterator(chunks); 57 | DataType bytes = DataTypeFactory.of(TYPE_VARCHAR); 58 | DataType ints = DataTypeFactory.of(TYPE_LONG); 59 | Row row = ObjectRowImpl.create(6); 60 | CodecDataInput cdi = new CodecDataInput(chunkIterator.next()); 61 | ints.decodeValueToRow(cdi, row, 0); 62 | bytes.decodeValueToRow(cdi, row, 1); 63 | cdi = new CodecDataInput(chunkIterator.next()); 64 | ints.decodeValueToRow(cdi, row, 2); 65 | bytes.decodeValueToRow(cdi, row, 3); 66 | cdi = new CodecDataInput(chunkIterator.next()); 67 | ints.decodeValueToRow(cdi, row, 4); 68 | bytes.decodeValueToRow(cdi, row, 5); 69 | assertEquals(row.getLong(0), 1); 70 | assertEquals(row.getString(1), "a"); 71 | assertEquals(row.getLong(2), 2); 72 | assertEquals(row.getString(3), "b"); 73 | assertEquals(row.getLong(4), 3); 74 | assertEquals(row.getString(5), "c"); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/policy/RetryPolicy.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.policy; 17 | 18 | import com.google.common.collect.ImmutableSet; 19 | import com.pingcap.tikv.exception.GrpcException; 20 | import com.pingcap.tikv.exception.GrpcRegionStaleException; 21 | import com.pingcap.tikv.operation.ErrorHandler; 22 | import com.pingcap.tikv.util.BackOff; 23 | import io.grpc.Status; 24 | import org.apache.log4j.Logger; 25 | 26 | import java.util.concurrent.Callable; 27 | 28 | public abstract class RetryPolicy { 29 | private static final Logger logger = Logger.getLogger(RetryPolicy.class); 30 | 31 | BackOff backOff = BackOff.ZERO_BACKOFF; 32 | 33 | // handles PD and TiKV's error. 34 | private ErrorHandler handler; 35 | 36 | private ImmutableSet unrecoverableStatus = 37 | ImmutableSet.of( 38 | Status.Code.ALREADY_EXISTS, Status.Code.PERMISSION_DENIED, 39 | Status.Code.INVALID_ARGUMENT, Status.Code.NOT_FOUND, 40 | Status.Code.UNIMPLEMENTED, Status.Code.OUT_OF_RANGE, 41 | Status.Code.UNAUTHENTICATED, Status.Code.CANCELLED); 42 | 43 | RetryPolicy(ErrorHandler handler) { 44 | this.handler = handler; 45 | } 46 | 47 | private void rethrowNotRecoverableException(Exception e) { 48 | if (e instanceof GrpcRegionStaleException) { 49 | throw (GrpcRegionStaleException)e; 50 | } 51 | Status status = Status.fromThrowable(e); 52 | if (unrecoverableStatus.contains(status.getCode())) { 53 | throw new GrpcException(e); 54 | } 55 | } 56 | 57 | private void handleFailure(Exception e, String methodName, long nextBackMills) { 58 | if(nextBackMills == BackOff.STOP) { 59 | throw new GrpcException("retry is exhausted.", e); 60 | } 61 | rethrowNotRecoverableException(e); 62 | doWait(nextBackMills); 63 | } 64 | 65 | private void doWait(long millis) { 66 | try { 67 | Thread.sleep(millis); 68 | } catch (InterruptedException e) { 69 | throw new GrpcException(e); 70 | } 71 | } 72 | 73 | public RespT callWithRetry(Callable proc, String methodName) { 74 | for(;true;) { 75 | try { 76 | RespT result = proc.call(); 77 | if (handler != null) { 78 | handler.handle(result); 79 | } 80 | return result; 81 | } catch (Exception e) { 82 | handleFailure(e, methodName, backOff.nextBackOffMillis()); 83 | } 84 | } 85 | } 86 | 87 | public interface Builder { 88 | RetryPolicy create(ErrorHandler handler); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/types/TimestampTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertNotEquals; 22 | import static org.junit.Assert.assertNull; 23 | 24 | import java.text.ParseException; 25 | import java.text.SimpleDateFormat; 26 | import java.time.LocalDateTime; 27 | import java.time.format.DateTimeFormatter; 28 | import java.util.Date; 29 | import org.junit.Test; 30 | 31 | public class TimestampTest { 32 | @Test 33 | public void fromPackedLongAndToPackedLongTest() throws ParseException { 34 | 35 | LocalDateTime time = LocalDateTime.of(1999, 12, 12, 1, 1, 1, 1000); 36 | LocalDateTime time1 = TimestampType.fromPackedLong(TimestampType.toPackedLong(time)); 37 | assertEquals(time, time1); 38 | 39 | // since precision is microseconds, any nanoseconds is smaller than 1000 will be dropped. 40 | time = LocalDateTime.of(1999, 12, 12, 1, 1, 1, 1); 41 | time1 = TimestampType.fromPackedLong(TimestampType.toPackedLong(time)); 42 | assertNotEquals(time, time1); 43 | 44 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSSSSSS"); 45 | LocalDateTime time2 = LocalDateTime.parse("2010-10-10 10:11:11:0000000", formatter); 46 | LocalDateTime time3 = TimestampType.fromPackedLong(TimestampType.toPackedLong(time2)); 47 | assertEquals(time2, time3); 48 | 49 | // when packedLong is 0, then null is returned 50 | LocalDateTime time4 = TimestampType.fromPackedLong(0); 51 | assertNull(time4); 52 | 53 | LocalDateTime time5 = LocalDateTime.parse("9999-12-31 23:59:59:0000000", formatter); 54 | LocalDateTime time6 = TimestampType.fromPackedLong(TimestampType.toPackedLong(time5)); 55 | assertEquals(time5, time6); 56 | 57 | LocalDateTime time7 = LocalDateTime.parse("1000-01-01 00:00:00:0000000", formatter); 58 | LocalDateTime time8 = TimestampType.fromPackedLong(TimestampType.toPackedLong(time7)); 59 | assertEquals(time7, time8); 60 | 61 | LocalDateTime time9 = LocalDateTime.parse("2017-01-05 23:59:59:5756010", formatter); 62 | LocalDateTime time10 = TimestampType.fromPackedLong(TimestampType.toPackedLong(time9)); 63 | assertEquals(time9, time10); 64 | 65 | SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd"); 66 | Date date1 = formatter1.parse("2099-10-30"); 67 | long time11 = TimestampType.toPackedLong(date1); 68 | LocalDateTime time12 = TimestampType.fromPackedLong(time11); 69 | assertEquals(time12.toLocalDate(), new java.sql.Date(date1.getTime()).toLocalDate()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | maven_jar( 2 | name = "com_fasterxml_jackson_core_jackson_annotations", 3 | artifact = "com.fasterxml.jackson.core:jackson-annotations:2.6.6", 4 | ) 5 | 6 | maven_jar( 7 | name = "com_fasterxml_jackson_core_jackson_databind", 8 | artifact = "com.fasterxml.jackson.core:jackson-databind:2.6.6", 9 | ) 10 | 11 | maven_jar( 12 | name = "com_fasterxml_jackson_core_jackson_core", 13 | artifact = "com.fasterxml.jackson.core:jackson-core:2.6.6", 14 | ) 15 | 16 | maven_jar( 17 | name = "org_slf4j_slf4j_api", 18 | artifact = "org.slf4j:slf4j-api:1.7.16", 19 | ) 20 | 21 | maven_jar( 22 | name = "org_slf4j_jcl_over_slf4j", 23 | artifact = "org.slf4j:jcl-over-slf4j:1.7.16", 24 | ) 25 | 26 | maven_jar( 27 | name = "org_slf4j_jul_to_slf4j", 28 | artifact = "org.slf4j:jul-to-slf4j:1.7.16", 29 | ) 30 | 31 | maven_jar( 32 | name = "log4j_log4j", 33 | artifact = "log4j:log4j:1.2.17", 34 | ) 35 | 36 | maven_jar( 37 | name = "joda_time", 38 | artifact = "joda-time:joda-time:2.9.9", 39 | ) 40 | 41 | maven_jar( 42 | name = "junit_junit", 43 | artifact = "junit:junit:4.12", 44 | ) 45 | 46 | maven_jar( 47 | name = "org_hamcrest_hamcrest_core", 48 | artifact = "org.hamcrest:hamcrest-core:1.3", 49 | ) 50 | 51 | maven_jar( 52 | name = "org_javassist_javassist", 53 | artifact = "org.javassist:javassist:3.21.0-GA", 54 | ) 55 | 56 | maven_jar( 57 | name = "org_powermock_powermock_reflect", 58 | artifact = "org.powermock:powermock-reflect:1.6.6", 59 | ) 60 | 61 | maven_jar( 62 | name = "org_powermock_powermock_api_mockito", 63 | artifact = "org.powermock:powermock-api-mockito:1.6.6", 64 | ) 65 | 66 | maven_jar( 67 | name = "org_mockito_mockito_core", 68 | artifact = "org.mockito:mockito-core:1.10.19", 69 | ) 70 | 71 | maven_jar( 72 | name = "org_objenesis_objenesis", 73 | artifact = "org.objenesis:objenesis:2.1", 74 | ) 75 | 76 | maven_jar( 77 | name = "org_powermock_powermock_api_mockito_common", 78 | artifact = "org.powermock:powermock-api-mockito-common:1.6.6", 79 | ) 80 | 81 | maven_jar( 82 | name = "org_powermock_powermock_api_support", 83 | artifact = "org.powermock:powermock-api-support:1.6.6", 84 | ) 85 | 86 | maven_jar( 87 | name = "net_sf_trove4j_trove4j", 88 | artifact = "net.sf.trove4j:trove4j:3.0.1", 89 | ) 90 | 91 | git_repository( 92 | name = "org_pubref_rules_protobuf", 93 | remote = "https://github.com/pubref/rules_protobuf", 94 | tag = "v0.8.1", 95 | ) 96 | 97 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_repositories") 98 | java_proto_repositories() 99 | 100 | bazel_shade_version = "master" 101 | http_archive( 102 | name = "com_github_zhexuany_bazel_shade", 103 | url = "https://github.com/zhexuany/bazel_shade_plugin/archive/%s.zip"%bazel_shade_version, 104 | type = "zip", 105 | strip_prefix= "bazel_shade_plugin-%s"%bazel_shade_version 106 | ) 107 | load( 108 | "@com_github_zhexuany_bazel_shade//:java_shade.bzl", 109 | "java_shade_repositories", 110 | "java_shade" 111 | ) 112 | java_shade_repositories() 113 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/predicates/IndexMatcher.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.predicates; 17 | 18 | import static com.google.common.base.Preconditions.checkNotNull; 19 | 20 | import com.pingcap.tikv.exception.TiClientInternalException; 21 | import com.pingcap.tikv.expression.TiColumnRef; 22 | import com.pingcap.tikv.expression.TiConstant; 23 | import com.pingcap.tikv.expression.TiExpr; 24 | import com.pingcap.tikv.expression.TiFunctionExpression; 25 | import com.pingcap.tikv.expression.scalar.*; 26 | import com.pingcap.tikv.meta.TiIndexColumn; 27 | 28 | public class IndexMatcher { 29 | private final boolean matchEqualTestOnly; 30 | private final TiIndexColumn indexColumn; 31 | 32 | protected IndexMatcher(TiIndexColumn indexColumn, boolean matchEqualTestOnly) { 33 | this.matchEqualTestOnly = matchEqualTestOnly; 34 | this.indexColumn = indexColumn; 35 | } 36 | 37 | /** 38 | * Test if a filter expression matches an index It does only if it's an comparision-style filter 39 | * 40 | * @param expr to match 41 | * @return if a expr matches the index 42 | */ 43 | public boolean match(TiExpr expr) { 44 | checkNotNull(expr, "Expression should not be null"); 45 | 46 | if (expr instanceof TiFunctionExpression) { 47 | return matchFunction((TiFunctionExpression) expr); 48 | } else if (expr instanceof TiColumnRef) { 49 | return matchColumn((TiColumnRef) expr); 50 | } else if (expr instanceof TiConstant) { 51 | return true; 52 | } 53 | throw new TiClientInternalException( 54 | "Invalid Type for condition checker: " + expr.getClass().getSimpleName()); 55 | } 56 | 57 | private boolean matchFunction(TiFunctionExpression expr) { 58 | try { 59 | if (expr instanceof And || expr instanceof Or) { 60 | // If we pick eq only, AND is not allowed 61 | return !matchEqualTestOnly || !(expr instanceof And) && match(expr.getArg(0)) && match( 62 | expr.getArg(1)); 63 | } else { 64 | if (matchEqualTestOnly) { 65 | if (!(expr instanceof Equal) && !(expr instanceof In)) { 66 | return false; 67 | } 68 | } 69 | AccessConditionNormalizer.NormalizedCondition cond = 70 | AccessConditionNormalizer.normalize(expr); 71 | 72 | return matchColumn(cond.columnRef); 73 | } 74 | } catch (Exception e) { 75 | return false; 76 | } 77 | } 78 | 79 | private boolean matchColumn(TiColumnRef col) { 80 | if (indexColumn != null) { 81 | String indexColumnName = indexColumn.getName(); 82 | return col.getColumnInfo().matchName(indexColumnName); 83 | } 84 | return false; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/DateType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import com.pingcap.tikv.codec.CodecDataInput; 21 | import com.pingcap.tikv.codec.CodecDataOutput; 22 | import com.pingcap.tikv.exception.TiClientInternalException; 23 | import com.pingcap.tikv.meta.TiColumnInfo; 24 | 25 | import java.sql.Date; 26 | import java.text.SimpleDateFormat; 27 | import java.time.LocalDate; 28 | import java.time.LocalDateTime; 29 | import java.time.format.DateTimeFormatter; 30 | 31 | import static com.pingcap.tikv.types.TimestampType.fromPackedLong; 32 | import static com.pingcap.tikv.types.TimestampType.toPackedLong; 33 | 34 | public class DateType extends DataType { 35 | static DateType of(int tp) { 36 | return new DateType(tp); 37 | } 38 | private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 39 | private static final IntegerType codecObject = IntegerType.DEF_LONG_LONG_TYPE; 40 | 41 | private DateType(int tp) { 42 | super(tp); 43 | } 44 | 45 | public String simpleTypeName() { return "date"; } 46 | 47 | @Override 48 | public Object decodeNotNull(int flag, CodecDataInput cdi) { 49 | long val = IntegerType.decodeNotNullPrimitive(flag, cdi); 50 | LocalDateTime localDateTime = fromPackedLong(val); 51 | if (localDateTime == null) { 52 | return null; 53 | } 54 | //TODO revisit this later. 55 | return new Date(localDateTime.getYear() - 1900, 56 | localDateTime.getMonthValue() - 1, 57 | localDateTime.getDayOfMonth()); 58 | } 59 | 60 | @Override 61 | public void encodeNotNull(CodecDataOutput cdo, EncodeType encodeType, Object value) { 62 | Date in; 63 | try { 64 | if (value instanceof Date) { 65 | in = (Date) value; 66 | } else { 67 | // format ensure only date part without time 68 | in = new Date(format.parse(value.toString()).getTime()); 69 | } 70 | } catch (Exception e) { 71 | throw new TiClientInternalException("Can not cast Object to LocalDateTime: " + value, e); 72 | } 73 | long val = toPackedLong(in); 74 | codecObject.encodeNotNull(cdo, encodeType, val); 75 | } 76 | 77 | /** 78 | * get origin default value in string 79 | * @param value a date represents in string in "yyyy-MM-dd" format 80 | * @return a {@link Date} Object 81 | */ 82 | @Override 83 | public Object getOriginDefaultValueNonNull(String value) { 84 | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 85 | LocalDate localDate = LocalDate.parse(value, dateTimeFormatter); 86 | return new Date(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()); 87 | } 88 | 89 | DateType(TiColumnInfo.InternalTypeHolder holder) { 90 | super(holder); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Ti-Client in Java [Under Construction] 2 | 3 | A Java client for [TiDB](https://github.com/pingcap/tidb)/[TiKV](https://github.com/pingcap/tikv). 4 | It is supposed to: 5 | + Communicate via [gRPC](http://www.grpc.io/) 6 | + Talk to Placement Driver searching for a region 7 | + Talk to TiKV for reading/writing data and the resulted data is encoded/decoded just like what we do in TiDB. 8 | + Talk to Coprocessor for calculation pushdown 9 | 10 | ## How to build 11 | 12 | The following command can install dependencies for you. 13 | ``` 14 | mvn package 15 | ``` 16 | 17 | Alternatively, you can use `bazel` for much faster build. When you try this approach, you should run `git submodule update --init --recursive` before you build project. 18 | 19 | Making a uber jar: 20 | ``` 21 | make uber_jar 22 | ``` 23 | run Main class: 24 | ``` 25 | make run 26 | ``` 27 | 28 | run test cases: 29 | ``` 30 | make test 31 | ``` 32 | 33 | this project is designed to hook with `pd` and `tikv` which you can find in `PingCap` github page. 34 | 35 | When you work with this project, you have to communicate with `pd` and `tikv`. There is a script taking care of this. By executing the following commands, `pd` and `tikv` can be executed on background. 36 | ``` 37 | cd scripts 38 | make pd 39 | make tikv 40 | ``` 41 | 42 | ## How to use for now 43 | Since it's not quite complete, a usage sample for now can be given is: 44 | ```java 45 | // Init tidb cluster configuration 46 | TiConfiguration conf = TiConfiguration.createDefault("127.0.0.1:2379"); 47 | TiSession session = TiSession.create(conf); 48 | Catalog cat = session.getCatalog(); 49 | TiDBInfo db = cat.getDatabase("tpch_test"); 50 | TiTableInfo table = cat.getTable(db, "customer"); 51 | Snapshot snapshot = session.createSnapshot(); 52 | 53 | // Generate select ranges 54 | ByteString startKey = TableCodec.encodeRowKeyWithHandle(table.getId(), Long.MIN_VALUE); 55 | ByteString endKey = TableCodec.encodeRowKeyWithHandle(table.getId(), Long.MAX_VALUE); 56 | Coprocessor.KeyRange keyRange = Coprocessor.KeyRange.newBuilder().setStart(startKey).setEnd(endKey).build(); 57 | List ranges = new ArrayList<>(); 58 | ranges.add(keyRange); 59 | 60 | 61 | // Create select request 62 | TiSelectRequest selectRequest = new TiSelectRequest(); 63 | selectRequest.addRanges(ranges); 64 | selectRequest.addField(TiColumnRef.create("c_mktsegment", table)); 65 | selectRequest.setTableInfo(table); 66 | selectRequest.setStartTs(session.getTimestamp().getVersion()); 67 | selectRequest.addWhere(new GreaterEqual(TiConstant.create(5), TiConstant.create(5))); 68 | selectRequest.addGroupByItem(TiByItem.create(TiColumnRef.create("c_mktsegment"), false)); 69 | selectRequest.setLimit(10); 70 | selectRequest.bind(); 71 | 72 | // Fetch data 73 | Iterator iterator = snapshot.select(selectRequest); 74 | System.out.println("Show result:"); 75 | while (iterator.hasNext()) { 76 | Row rowData = iterator.next(); 77 | for (int i = 0; i < rowData.fieldCount(); i++) { 78 | System.out.print(rowData.get(i, null) + "\t"); 79 | } 80 | System.out.println(); 81 | } 82 | 83 | ``` 84 | Result: 85 | ```java 86 | Show result: 87 | BUILDING 88 | AUTOMOBILE 89 | MACHINERY 90 | HOUSEHOLD 91 | FURNITURE 92 | ``` 93 | 94 | ## TODO 95 | Contributions are welcomed. Here is a [TODO](https://github.com/pingcap/tikv-client-java/wiki/TODO-Lists) and you might contact maxiaoyu@pingcap.com if needed. 96 | 97 | ## License 98 | Apache 2.0 license. See the [LICENSE](./LICENSE) file for details. 99 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/iterator/ChunkIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.operation.iterator; 19 | 20 | import com.google.protobuf.ByteString; 21 | import com.pingcap.tidb.tipb.Chunk; 22 | import com.pingcap.tikv.exception.TiClientInternalException; 23 | 24 | import java.util.Iterator; 25 | import java.util.List; 26 | 27 | public abstract class ChunkIterator implements Iterator { 28 | 29 | private final List chunks; 30 | protected int chunkIndex; 31 | protected int metaIndex; 32 | protected int bufOffset; 33 | protected boolean eof; 34 | 35 | public static ChunkIterator getRawBytesChunkIterator(List chunks) { 36 | return new ChunkIterator(chunks) { 37 | @Override 38 | public ByteString next() { 39 | Chunk c = chunks.get(chunkIndex); 40 | long endOffset = c.getRowsMeta(metaIndex).getLength() + bufOffset; 41 | if (endOffset > Integer.MAX_VALUE) { 42 | throw new TiClientInternalException("Offset exceeded MAX_INT."); 43 | } 44 | 45 | ByteString result = c.getRowsData().substring(bufOffset, (int) endOffset); 46 | advance(); 47 | return result; 48 | } 49 | }; 50 | } 51 | 52 | public static ChunkIterator getHandleChunkIterator(List chunks) { 53 | return new ChunkIterator(chunks) { 54 | @Override 55 | public Long next() { 56 | Chunk c = chunks.get(chunkIndex); 57 | long result = c.getRowsMeta(metaIndex).getHandle(); 58 | advance(); 59 | return result; 60 | } 61 | }; 62 | } 63 | 64 | protected ChunkIterator(List chunks) { 65 | // Read and then advance semantics 66 | this.chunks = chunks; 67 | this.chunkIndex = 0; 68 | this.metaIndex = 0; 69 | this.bufOffset = 0; 70 | if (chunks.size() == 0 71 | || chunks.get(0).getRowsMetaCount() == 0 72 | || chunks.get(0).getRowsData().size() == 0) { 73 | eof = true; 74 | } 75 | } 76 | 77 | @Override 78 | public boolean hasNext() { 79 | return !eof; 80 | } 81 | 82 | private boolean seekNextNonEmptyChunk() { 83 | // loop until the end of chunk list or first non empty chunk 84 | do { 85 | chunkIndex += 1; 86 | } while (chunkIndex < chunks.size() && 87 | chunks.get(chunkIndex).getRowsMetaCount() == 0); 88 | // return if remaining things left 89 | return chunkIndex < chunks.size(); 90 | } 91 | 92 | protected void advance() { 93 | if (eof) { 94 | return; 95 | } 96 | Chunk c = chunks.get(chunkIndex); 97 | bufOffset += c.getRowsMeta(metaIndex++).getLength(); 98 | if (metaIndex >= c.getRowsMetaCount()) { 99 | if (seekNextNonEmptyChunk()) { 100 | metaIndex = 0; 101 | bufOffset = 0; 102 | } else { 103 | eof = true; 104 | } 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/Types.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | public class Types { 21 | // The following ints are mysql type. 22 | // TYPE_DECIMAL is not used in MySQL. 23 | // public static final int TYPE_DECIMAL = 0; 24 | public static final int TYPE_TINY = 1; 25 | public static final int TYPE_SHORT = 2; 26 | public static final int TYPE_LONG = 3; 27 | public static final int TYPE_FLOAT = 4; 28 | public static final int TYPE_DOUBLE = 5; 29 | public static final int TYPE_NULL = 6; 30 | public static final int TYPE_TIMESTAMP = 7; 31 | public static final int TYPE_LONG_LONG = 8; 32 | public static final int TYPE_INT24 = 9; 33 | public static final int TYPE_DATE = 10; 34 | // Original name was TypeTime. But TiDB replace Time with 35 | // Duration to avoid name conflict. We just adopt this. 36 | public static final int TYPE_DURATION = 11; 37 | public static final int TYPE_DATETIME = 12; 38 | public static final int TYPE_YEAR = 13; 39 | public static final int TYPE_NEW_DATE = 14; 40 | public static final int TYPE_VARCHAR = 15; 41 | public static final int TYPE_BIT = 16; 42 | 43 | public static final int TYPE_JSON = 0xf5; 44 | public static final int TYPE_NEW_DECIMAL = 0xf6; 45 | public static final int TYPE_ENUM = 0xf7; 46 | public static final int TYPE_SET = 0xf8; 47 | public static final int TYPE_TINY_BLOB = 0xf9; 48 | public static final int TYPE_MEDIUM_BLOB = 0xfa; 49 | public static final int TYPE_LONG_BLOB = 0xfb; 50 | public static final int TYPE_BLOB = 0xfc; 51 | public static final int TYPE_VAR_STRING = 0xfd; 52 | public static final int TYPE_STRING = 0xfe; 53 | public static final int TYPE_GEOMETRY = 0xff; 54 | 55 | // Flag Information for strict mysql type 56 | public static final int NotNullFlag = 1; /* Field can't be NULL */ 57 | public static final int PriKeyFlag = 2; /* Field is part of a primary key */ 58 | public static final int UniqueKeyFlag = 4; /* Field is part of a unique key */ 59 | public static final int MultipleKeyFlag = 8; /* Field is part of a key */ 60 | public static final int BlobFlag = 16; /* Field is a blob */ 61 | public static final int UnsignedFlag = 32; /* Field is unsigned */ 62 | public static final int ZerofillFlag = 64; /* Field is zerofill */ 63 | public static final int BinaryFlag = 128; /* Field is binary */ 64 | 65 | public static final int EnumFlag = 256; /* Field is an enum */ 66 | public static final int AutoIncrementFlag = 512; /* Field is an auto increment field */ 67 | public static final int TimestampFlag = 1024; /* Field is a timestamp */ 68 | public static final int SetFlag = 2048; /* Field is a set */ 69 | public static final int NoDefaultValueFlag = 4096; /* Field doesn't have a default value */ 70 | public static final int OnUpdateNowFlag = 8192; /* Field is set to NOW on UPDATE */ 71 | public static final int NumFlag = 32768; /* Field is a num (for clients) */ 72 | public static final int PartKeyFlag = 16384; /* Intern: Part of some keys */ 73 | public static final int GroupFlag = 32768; /* Intern: Group field */ 74 | public static final int BinCmpFlag = 131072; /* Intern: Used by sql_yacc */ 75 | } 76 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/types/IntegerTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import static org.junit.Assert.*; 21 | 22 | import com.pingcap.tikv.codec.CodecDataInput; 23 | import com.pingcap.tikv.codec.CodecDataOutput; 24 | import org.junit.Test; 25 | 26 | public class IntegerTypeTest { 27 | @Test 28 | public void readNWriteLongTest() throws Exception { 29 | CodecDataOutput cdo = new CodecDataOutput(); 30 | IntegerType.writeLongFull(cdo, 9999L, true); 31 | IntegerType.writeLongFull(cdo, -2333L, false); 32 | assertArrayEquals( 33 | new byte[] { 34 | (byte) 0x3, 35 | (byte) 0x80, 36 | (byte) 0x0, 37 | (byte) 0x0, 38 | (byte) 0x0, 39 | (byte) 0x0, 40 | (byte) 0x0, 41 | (byte) 0x27, 42 | (byte) 0xf, 43 | (byte) 0x8, 44 | (byte) 0xb9, 45 | (byte) 0x24 46 | }, 47 | cdo.toBytes()); 48 | CodecDataInput cdi = new CodecDataInput(cdo.toBytes()); 49 | long value = IntegerType.readLongFully(cdi); 50 | assertEquals(9999L, value); 51 | value = IntegerType.readLongFully(cdi); 52 | assertEquals(-2333L, value); 53 | 54 | byte[] wrongData = new byte[] {(byte) 0x8, (byte) 0xb9}; 55 | cdi = new CodecDataInput(wrongData); 56 | try { 57 | IntegerType.readLongFully(cdi); 58 | fail(); 59 | } catch (Exception e) { 60 | assertTrue(true); 61 | } 62 | } 63 | 64 | @Test 65 | public void readNWriteUnsignedLongTest() throws Exception { 66 | CodecDataOutput cdo = new CodecDataOutput(); 67 | IntegerType.writeULongFull(cdo, 0xffffffffffffffffL, true); 68 | IntegerType.writeULongFull(cdo, Long.MIN_VALUE, false); 69 | assertArrayEquals( 70 | new byte[] { 71 | (byte) 0x4, 72 | (byte) 0xff, 73 | (byte) 0xff, 74 | (byte) 0xff, 75 | (byte) 0xff, 76 | (byte) 0xff, 77 | (byte) 0xff, 78 | (byte) 0xff, 79 | (byte) 0xff, 80 | (byte) 0x9, 81 | (byte) 0x80, 82 | (byte) 0x80, 83 | (byte) 0x80, 84 | (byte) 0x80, 85 | (byte) 0x80, 86 | (byte) 0x80, 87 | (byte) 0x80, 88 | (byte) 0x80, 89 | (byte) 0x80, 90 | (byte) 0x1 91 | }, 92 | cdo.toBytes()); 93 | CodecDataInput cdi = new CodecDataInput(cdo.toBytes()); 94 | long value = IntegerType.readULongFully(cdi); 95 | 96 | assertEquals(0xffffffffffffffffL, value); 97 | value = IntegerType.readULongFully(cdi); 98 | assertEquals(Long.MIN_VALUE, value); 99 | 100 | byte[] wrongData = 101 | new byte[] { 102 | (byte) 0x9, (byte) 0x80, (byte) 0x80, (byte) 0x80, 103 | (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, 104 | (byte) 0x80, (byte) 0x80 105 | }; 106 | cdi = new CodecDataInput(wrongData); 107 | try { 108 | IntegerType.readULongFully(cdi); 109 | fail(); 110 | } catch (Exception e) { 111 | assertTrue(true); 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/expression/TiFunctionExpression.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.expression; 17 | 18 | import static com.google.common.base.Preconditions.checkArgument; 19 | import static java.util.Objects.requireNonNull; 20 | 21 | import com.google.common.base.Joiner; 22 | import com.google.common.collect.ImmutableList; 23 | import com.pingcap.tidb.tipb.Expr; 24 | import com.pingcap.tidb.tipb.ExprType; 25 | import com.pingcap.tikv.meta.TiTableInfo; 26 | import java.util.List; 27 | 28 | 29 | public abstract class TiFunctionExpression implements TiExpr { 30 | 31 | protected final List args; 32 | 33 | protected TiFunctionExpression(TiExpr... args) { 34 | this.args = ImmutableList.copyOf(args); 35 | validateArguments(args); 36 | } 37 | 38 | protected abstract ExprType getExprType(); 39 | 40 | public TiExpr getArg(int i) { 41 | checkArgument(i < args.size(), "Index out of bound for TiExpression Arguments"); 42 | return args.get(i); 43 | } 44 | 45 | public int getArgSize() { 46 | return args.size(); 47 | } 48 | 49 | public List getArgs() { 50 | return args; 51 | } 52 | 53 | @Override 54 | public Expr toProto() { 55 | Expr.Builder builder = Expr.newBuilder(); 56 | 57 | builder.setTp(getExprType()); 58 | 59 | for (TiExpr arg : args) { 60 | builder.addChildren(arg.toProto()); 61 | } 62 | 63 | return builder.build(); 64 | } 65 | 66 | public String getName() { 67 | return getClass().getSimpleName(); 68 | } 69 | 70 | protected void validateArguments(TiExpr... args) throws RuntimeException { 71 | requireNonNull(args, "Expressions cannot be null"); 72 | for (TiExpr expr : args) { 73 | requireNonNull(expr, "Expressions cannot be null."); 74 | } 75 | } 76 | 77 | @Override 78 | public boolean equals(Object other) { 79 | if (other == null) return false; 80 | if (this.getClass().equals(other.getClass())) { 81 | TiFunctionExpression func = (TiFunctionExpression) other; 82 | for (int i = 0; i < func.getArgSize(); i++) { 83 | TiExpr arg = func.getArg(i); 84 | if (!getArg(i).equals(arg)) { 85 | return false; 86 | } 87 | } 88 | return true; 89 | } 90 | return false; 91 | } 92 | 93 | @Override 94 | public boolean isSupportedExpr(ExpressionBlacklist blackList) { 95 | for (TiExpr arg : args) { 96 | if (!arg.isSupportedExpr(blackList)) { 97 | return false; 98 | } 99 | } 100 | return TiExpr.super.isSupportedExpr(blackList); 101 | } 102 | 103 | @Override 104 | public int hashCode() { 105 | int hash = 31 * getClass().hashCode(); 106 | for (TiExpr arg : args) { 107 | hash *= arg.hashCode(); 108 | } 109 | return hash; 110 | } 111 | 112 | @Override 113 | public TiFunctionExpression resolve(TiTableInfo table) { 114 | for (TiExpr arg : args) { 115 | arg.resolve(table); 116 | } 117 | return this; 118 | } 119 | 120 | @Override 121 | public String toString() { 122 | return String.format("%s(%s)", 123 | getName(), 124 | Joiner.on(", ").skipNulls().join(args)); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/types/RealType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import com.pingcap.tikv.codec.CodecDataInput; 21 | import com.pingcap.tikv.codec.CodecDataOutput; 22 | import com.pingcap.tikv.codec.InvalidCodecFormatException; 23 | import com.pingcap.tikv.meta.TiColumnInfo; 24 | 25 | public class RealType extends DataType { 26 | private static final long signMask = 0x8000000000000000L; 27 | 28 | static RealType of(int tp) { 29 | return new RealType(tp); 30 | } 31 | 32 | private RealType(int tp) { 33 | super(tp); 34 | } 35 | 36 | public String simpleTypeName() { return "real"; } 37 | 38 | @Override 39 | public Object decodeNotNull(int flag, CodecDataInput cdi) { 40 | // check flag first and then read. 41 | if (flag != FLOATING_FLAG) { 42 | throw new InvalidCodecFormatException("Invalid Flag type for float type: " + flag); 43 | } 44 | return readDouble(cdi); 45 | } 46 | 47 | RealType(TiColumnInfo.InternalTypeHolder holder) { 48 | super(holder); 49 | } 50 | 51 | /** 52 | * encode a value to cdo. 53 | * 54 | * @param cdo destination of data. 55 | * @param encodeType Key or Value. 56 | * @param value need to be encoded. 57 | */ 58 | @Override 59 | public void encodeNotNull(CodecDataOutput cdo, EncodeType encodeType, Object value) { 60 | double val; 61 | if (value instanceof Double) { 62 | val = (Double) value; 63 | } else { 64 | throw new UnsupportedOperationException("Can not cast Un-number to Float"); 65 | } 66 | 67 | IntegerType.writeULong(cdo, encodeDoubleToCmpLong(val)); 68 | } 69 | 70 | /** 71 | * get origin default value 72 | * @param value a float value represents in string 73 | * @return a {@link Float} Object 74 | */ 75 | @Override 76 | public Object getOriginDefaultValueNonNull(String value) { 77 | return Float.parseFloat(value); 78 | } 79 | 80 | /** 81 | * Decode as float 82 | * 83 | * @param cdi source of data 84 | * @return decoded unsigned long value 85 | */ 86 | public static double readDouble(CodecDataInput cdi) { 87 | long u = IntegerType.readULong(cdi); 88 | if (u < 0) { 89 | u &= Long.MAX_VALUE; 90 | } else { 91 | u = ~u; 92 | } 93 | return Double.longBitsToDouble(u); 94 | } 95 | 96 | private static long encodeDoubleToCmpLong(double val) { 97 | long u = Double.doubleToRawLongBits(val); 98 | if (val >= 0) { 99 | u |= signMask; 100 | } else { 101 | u = ~u; 102 | } 103 | return u; 104 | } 105 | 106 | /** 107 | * Encoding a double value to byte buffer 108 | * 109 | * @param cdo For outputting data in bytes array 110 | * @param val The data to encode 111 | */ 112 | public static void writeDouble(CodecDataOutput cdo, double val) { 113 | IntegerType.writeULong(cdo, encodeDoubleToCmpLong(val)); 114 | } 115 | 116 | /** 117 | * Encoding a float value to byte buffer 118 | * 119 | * @param cdo For outputting data in bytes array 120 | * @param val The data to encode 121 | */ 122 | public static void writeFloat(CodecDataOutput cdo, float val) { 123 | writeDouble(cdo, val); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/catalog/CatalogTransactionTest.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.catalog; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import com.pingcap.tikv.KVMockServer; 21 | import com.pingcap.tikv.PDMockServer; 22 | import com.pingcap.tikv.TiConfiguration; 23 | import com.pingcap.tikv.TiSession; 24 | import com.pingcap.tikv.kvproto.Kvrpcpb.CommandPri; 25 | import com.pingcap.tikv.kvproto.Kvrpcpb.IsolationLevel; 26 | import com.pingcap.tikv.meta.MetaUtils.MetaMockHelper; 27 | import com.pingcap.tikv.meta.TiDBInfo; 28 | import com.pingcap.tikv.meta.TiTableInfo; 29 | import com.pingcap.tikv.region.TiRegion; 30 | import java.util.List; 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | 35 | public class CatalogTransactionTest { 36 | private KVMockServer kvServer; 37 | private PDMockServer pdServer; 38 | private static final long CLUSTER_ID = 1024; 39 | private TiConfiguration conf; 40 | 41 | @Before 42 | public void setUp() throws Exception { 43 | pdServer = new PDMockServer(); 44 | pdServer.start(CLUSTER_ID); 45 | kvServer = new KVMockServer(); 46 | kvServer.start(new TiRegion(MetaMockHelper.region, MetaMockHelper.region.getPeers(0), 47 | IsolationLevel.RC, CommandPri.Low)); 48 | // No PD needed in this test 49 | conf = TiConfiguration.createDefault("127.0.0.1:" + pdServer.port); 50 | } 51 | 52 | @Test 53 | public void getLatestSchemaVersionTest() throws Exception { 54 | MetaMockHelper helper = new MetaMockHelper(pdServer, kvServer); 55 | helper.preparePDForRegionRead(); 56 | helper.setSchemaVersion(666); 57 | TiSession session = TiSession.create(conf); 58 | CatalogTransaction trx = new CatalogTransaction(session.createSnapshot()); 59 | assertEquals(666, trx.getLatestSchemaVersion()); 60 | } 61 | 62 | @Test 63 | public void getDatabasesTest() throws Exception { 64 | MetaMockHelper helper = new MetaMockHelper(pdServer, kvServer); 65 | helper.preparePDForRegionRead(); 66 | helper.addDatabase(130, "global_temp"); 67 | helper.addDatabase(264, "TPCH_001"); 68 | 69 | TiSession session = TiSession.create(conf); 70 | CatalogTransaction trx = new CatalogTransaction(session.createSnapshot()); 71 | List dbs = trx.getDatabases(); 72 | assertEquals(2, dbs.size()); 73 | assertEquals(130, dbs.get(0).getId()); 74 | assertEquals("global_temp", dbs.get(0).getName()); 75 | 76 | assertEquals(264, dbs.get(1).getId()); 77 | assertEquals("tpch_001", dbs.get(1).getName()); 78 | 79 | TiDBInfo db = trx.getDatabase(130); 80 | assertEquals(130, db.getId()); 81 | assertEquals("global_temp", db.getName()); 82 | } 83 | 84 | @Test 85 | public void getTablesTest() throws Exception { 86 | MetaMockHelper helper = new MetaMockHelper(pdServer, kvServer); 87 | helper.preparePDForRegionRead(); 88 | helper.addTable(130, 42, "test"); 89 | helper.addTable(130, 43, "test1"); 90 | 91 | TiSession session = TiSession.create(conf); 92 | CatalogTransaction trx = new CatalogTransaction(session.createSnapshot()); 93 | List tables = trx.getTables(130); 94 | assertEquals(tables.size(), 2); 95 | assertEquals(tables.get(0).getName(), "test"); 96 | assertEquals(tables.get(1).getName(), "test1"); 97 | } 98 | } -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/operation/iterator/IndexScanIterator.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv.operation.iterator; 17 | 18 | import com.pingcap.tikv.Snapshot; 19 | import com.pingcap.tikv.TiConfiguration; 20 | import com.pingcap.tikv.TiSession; 21 | import com.pingcap.tikv.exception.TiClientInternalException; 22 | import com.pingcap.tikv.meta.TiDAGRequest; 23 | import com.pingcap.tikv.row.Row; 24 | import com.pingcap.tikv.util.RangeSplitter; 25 | import com.pingcap.tikv.util.RangeSplitter.RegionTask; 26 | import gnu.trove.list.array.TLongArrayList; 27 | 28 | import java.util.Iterator; 29 | import java.util.List; 30 | import java.util.NoSuchElementException; 31 | import java.util.concurrent.ExecutorCompletionService; 32 | 33 | 34 | public class IndexScanIterator implements Iterator { 35 | private final Iterator handleIterator; 36 | private final TiDAGRequest dagReq; 37 | private final Snapshot snapshot; 38 | private Iterator rowIterator; 39 | private final ExecutorCompletionService> completionService; 40 | 41 | private int batchCount = 0; 42 | private final int batchSize; 43 | 44 | public IndexScanIterator(Snapshot snapshot, TiDAGRequest req, Iterator handleIterator) { 45 | TiSession session = snapshot.getSession(); 46 | TiConfiguration conf = session.getConf(); 47 | this.dagReq = req; 48 | this.handleIterator = handleIterator; 49 | this.snapshot = snapshot; 50 | this.batchSize = conf.getIndexScanBatchSize(); 51 | this.completionService = new ExecutorCompletionService<>(session.getThreadPoolForIndexScan()); 52 | } 53 | 54 | private TLongArrayList feedBatch() { 55 | TLongArrayList handles = new TLongArrayList(512); 56 | while (handleIterator.hasNext()) { 57 | handles.add(handleIterator.next()); 58 | if (batchSize <= handles.size()) { 59 | break; 60 | } 61 | } 62 | return handles; 63 | } 64 | 65 | @Override 66 | public boolean hasNext() { 67 | try { 68 | if (rowIterator == null) { 69 | TiSession session = snapshot.getSession(); 70 | while (handleIterator.hasNext()) { 71 | TLongArrayList handles = feedBatch(); 72 | batchCount++; 73 | completionService.submit(() -> { 74 | List tasks = RangeSplitter 75 | .newSplitter(session.getRegionManager()) 76 | .splitHandlesByRegion(dagReq.getTableInfo().getId(), handles); 77 | return CoprocessIterator.getRowIterator(dagReq, tasks, session); 78 | }); 79 | } 80 | while (batchCount > 0) { 81 | rowIterator = completionService.take().get(); 82 | batchCount--; 83 | 84 | if (rowIterator.hasNext()) { 85 | return true; 86 | } 87 | } 88 | } 89 | if (rowIterator == null) { 90 | return false; 91 | } 92 | } catch (Exception e) { 93 | throw new TiClientInternalException("Error reading rows from handle", e); 94 | } 95 | return rowIterator.hasNext(); 96 | } 97 | 98 | @Override 99 | public Row next() { 100 | if (hasNext()) { 101 | return rowIterator.next(); 102 | } else { 103 | throw new NoSuchElementException(); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/types/RealTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import static org.junit.Assert.*; 21 | 22 | import com.google.common.primitives.UnsignedLong; 23 | import com.pingcap.tikv.codec.CodecDataInput; 24 | import com.pingcap.tikv.codec.CodecDataOutput; 25 | import org.junit.Test; 26 | 27 | public class RealTypeTest { 28 | @Test 29 | public void writeFloatTest() throws Exception { 30 | CodecDataOutput cdo = new CodecDataOutput(); 31 | RealType.writeDouble(cdo, 0.00); 32 | double u = RealType.readDouble(new CodecDataInput(cdo.toBytes())); 33 | assertEquals(0.00, u, 0); 34 | 35 | cdo.reset(); 36 | RealType.writeDouble(cdo, Double.MAX_VALUE); 37 | u = RealType.readDouble(new CodecDataInput(cdo.toBytes())); 38 | assertEquals(Double.MAX_VALUE, u, 0); 39 | 40 | cdo.reset(); 41 | RealType.writeDouble(cdo, Double.MIN_VALUE); 42 | u = RealType.readDouble(new CodecDataInput(cdo.toBytes())); 43 | assertEquals(Double.MIN_VALUE, u, 0); 44 | } 45 | 46 | @Test 47 | public void readFloatTest() throws Exception { 48 | byte[] data = 49 | new byte[] { 50 | (byte) (191 & 0xFF), 51 | (byte) (241 & 0xFF), 52 | (byte) (153 & 0xFF), 53 | (byte) (153 & 0xFF), 54 | (byte) (160 & 0xFF), 55 | 0, 56 | 0, 57 | 0 58 | }; 59 | CodecDataInput cdi = new CodecDataInput(data); 60 | double u = RealType.readDouble(cdi); 61 | assertEquals(1.1, u, 0.0001); 62 | 63 | data = 64 | new byte[] { 65 | (byte) (192 & 0xFF), 66 | (byte) (1 & 0xFF), 67 | (byte) (153 & 0xFF), 68 | (byte) (153 & 0xFF), 69 | (byte) (153 & 0xFF), 70 | (byte) (153 & 0xFF), 71 | (byte) (153 & 0xFF), 72 | (byte) (154 & 0xFF) 73 | }; 74 | cdi = new CodecDataInput(data); 75 | u = RealType.readDouble(cdi); 76 | assertEquals(2.2, u, 0.0001); 77 | 78 | data = 79 | new byte[] { 80 | (byte) (63 & 0xFF), 81 | (byte) (167 & 0xFF), 82 | (byte) (51 & 0xFF), 83 | (byte) (67 & 0xFF), 84 | (byte) (159 & 0xFF), 85 | (byte) (0xFF), 86 | (byte) (0xFF), 87 | (byte) (0xFF) 88 | }; 89 | 90 | cdi = new CodecDataInput(data); 91 | u = RealType.readDouble(cdi); 92 | assertEquals(-99.199, u, 0.0001); 93 | } 94 | 95 | @Test 96 | public void negativeLongTest() throws Exception { 97 | CodecDataOutput cdo = new CodecDataOutput(); 98 | IntegerType.writeULong(cdo, UnsignedLong.valueOf("13831004815617530266").longValue()); 99 | double u = RealType.readDouble(new CodecDataInput(cdo.toBytes())); 100 | assertEquals(1.1, u, 0.001); 101 | 102 | cdo.reset(); 103 | IntegerType.writeULong(cdo, UnsignedLong.valueOf("13835508415244900762").longValue()); 104 | u = RealType.readDouble(new CodecDataInput(cdo.toBytes())); 105 | assertEquals(2.2, u, 0.001); 106 | 107 | cdo.reset(); 108 | IntegerType.writeULong(cdo, UnsignedLong.valueOf("13837985394932580352").longValue()); 109 | u = RealType.readDouble(new CodecDataInput(cdo.toBytes())); 110 | assertEquals(3.3, u, 0.001); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/expression/TiConstantTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.expression; 19 | 20 | import com.fasterxml.jackson.databind.ObjectMapper; 21 | import com.pingcap.tidb.tipb.Expr; 22 | import com.pingcap.tikv.codec.CodecDataInput; 23 | import com.pingcap.tikv.expression.scalar.GreaterThan; 24 | import com.pingcap.tikv.meta.TiTableInfo; 25 | import com.pingcap.tikv.meta.TiTableInfoTest; 26 | import com.pingcap.tikv.types.RealType; 27 | import org.joda.time.DateTimeZone; 28 | import org.joda.time.LocalDate; 29 | import org.junit.Test; 30 | 31 | import java.sql.Timestamp; 32 | 33 | import static com.pingcap.tikv.expression.TiConstant.DateWrapper; 34 | import static org.junit.Assert.assertEquals; 35 | 36 | public class TiConstantTest { 37 | @Test 38 | public void greaterThanTest() throws Exception { 39 | ObjectMapper mapper = new ObjectMapper(); 40 | TiTableInfo tableInfo = mapper.readValue(TiTableInfoTest.tableJson, TiTableInfo.class); 41 | GreaterThan g = new GreaterThan(TiColumnRef.create("c1", tableInfo), TiConstant.create(1.12)); 42 | Expr ge = g.toProto(); 43 | assertEquals(2, ge.getChildrenCount()); 44 | double expected = RealType.readDouble(new CodecDataInput(ge.getChildren(1).getVal())); 45 | assertEquals(1.12, expected, 0.00001); 46 | } 47 | 48 | @Test 49 | public void testEncodeSQLDate() { 50 | DateWrapper[] wrappers = new DateWrapper[]{ 51 | new DateWrapper(904694400000L), 52 | new DateWrapper(946684800000L), 53 | new DateWrapper(1077984000000L), 54 | new DateWrapper(-1019721600000L), 55 | new DateWrapper(1488326400000L), 56 | }; 57 | 58 | DateTimeZone timeZone = DateTimeZone.UTC; 59 | LocalDate[] localDates = new LocalDate[]{ 60 | new LocalDate(904694400000L, timeZone), 61 | new LocalDate(946684800000L, timeZone), 62 | new LocalDate(1077984000000L, timeZone), 63 | new LocalDate(-1019721600000L, timeZone), 64 | new LocalDate(1488326400000L, timeZone), 65 | }; 66 | 67 | String[] encodeAnswers = new String[]{ 68 | "tp: MysqlTime\nval: \"\\031_\\304\\000\\000\\000\\000\\000\"\n", 69 | "tp: MysqlTime\nval: \"\\031dB\\000\\000\\000\\000\\000\"\n", 70 | "tp: MysqlTime\nval: \"\\031q\\270\\000\\000\\000\\000\\000\"\n", 71 | "tp: MysqlTime\nval: \"\\030\\231\\220\\000\\000\\000\\000\\000\"\n", 72 | "tp: MysqlTime\nval: \"\\031\\234\\002\\000\\000\\000\\000\\000\"\n", 73 | }; 74 | 75 | String[][] answers = { 76 | {"1998", "9", "2"}, 77 | {"2000", "1", "1"}, 78 | {"2004", "2", "28"}, 79 | {"1937", "9", "8"}, 80 | {"2017", "3", "1"}, 81 | }; 82 | assertEquals(answers.length, wrappers.length); 83 | 84 | for (int i = 0; i < wrappers.length; i++) { 85 | assertEquals(answers[i][0], localDates[i].getYear() + ""); 86 | assertEquals(answers[i][1], localDates[i].getMonthOfYear() + ""); 87 | assertEquals(answers[i][2], localDates[i].getDayOfMonth() + ""); 88 | assertEquals(encodeAnswers[i], TiConstant.create(wrappers[i]).toProto().toString()); 89 | } 90 | } 91 | 92 | @Test 93 | public void testEncodeTimestamp() { 94 | TiConstant tsDate = TiConstant.create(new Timestamp(1998, 9, 2, 19, 0, 0, 0)); 95 | assertEquals("tp: MysqlTime\nval: \"1\\177\\0050\\000\\000\\000\\000\"\n", 96 | tsDate.toProto().toString()); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/GrpcUtils.java: -------------------------------------------------------------------------------- 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 com.pingcap.tikv; 17 | 18 | import com.google.common.collect.Lists; 19 | import com.google.protobuf.ByteString; 20 | import com.pingcap.tikv.codec.CodecDataOutput; 21 | import com.pingcap.tikv.kvproto.Metapb.*; 22 | import com.pingcap.tikv.kvproto.Pdpb.*; 23 | import com.pingcap.tikv.types.BytesType; 24 | import java.util.Arrays; 25 | 26 | public class GrpcUtils { 27 | private static ResponseHeader makeDefaultHeader(long clusterId) { 28 | return ResponseHeader.newBuilder().setClusterId(clusterId).build(); 29 | } 30 | 31 | public static Member makeMember(long memberId, String... urls) { 32 | return Member.newBuilder().setMemberId(memberId).addAllClientUrls(Arrays.asList(urls)).build(); 33 | } 34 | 35 | public static GetMembersResponse makeGetMembersResponse(long clusterId, Member... members) { 36 | return GetMembersResponse.newBuilder() 37 | .setHeader(makeDefaultHeader(clusterId)) 38 | .setLeader(members[0]) 39 | .addAllMembers(Arrays.asList(members)) 40 | .build(); 41 | } 42 | 43 | public static TsoResponse makeTsoResponse(long clusterId, long physical, long logical) { 44 | Timestamp ts = Timestamp.newBuilder().setPhysical(physical).setLogical(logical).build(); 45 | return TsoResponse.newBuilder() 46 | .setHeader(makeDefaultHeader(clusterId)) 47 | .setCount(1) 48 | .setTimestamp(ts) 49 | .build(); 50 | } 51 | 52 | public static Peer makePeer(long id, long storeId) { 53 | return Peer.newBuilder().setStoreId(storeId).setId(id).build(); 54 | } 55 | 56 | public static ByteString encodeKey(byte[] key) { 57 | CodecDataOutput cdo = new CodecDataOutput(); 58 | BytesType.writeBytes(cdo, key); 59 | return cdo.toByteString(); 60 | } 61 | 62 | public static RegionEpoch makeRegionEpoch(long confVer, long ver) { 63 | return RegionEpoch.newBuilder().setConfVer(confVer).setVersion(ver).build(); 64 | } 65 | 66 | public static Region makeRegion( 67 | long id, ByteString startKey, ByteString endKey, RegionEpoch re, Peer... peers) { 68 | return Region.newBuilder() 69 | .setId(id) 70 | .setStartKey(startKey) 71 | .setEndKey(endKey) 72 | .setRegionEpoch(re) 73 | .addAllPeers(Lists.newArrayList(peers)) 74 | .build(); 75 | } 76 | 77 | public static GetRegionResponse makeGetRegionResponse(long clusterId, Region region) { 78 | return GetRegionResponse.newBuilder() 79 | .setHeader(makeDefaultHeader(clusterId)) 80 | .setRegion(region) 81 | .setLeader(region.getPeers(0)) 82 | .build(); 83 | } 84 | 85 | public static StoreLabel makeStoreLabel(String key, String value) { 86 | return StoreLabel.newBuilder().setKey(key).setValue(value).build(); 87 | } 88 | 89 | public static Store makeStore(long id, String address, StoreState state, StoreLabel... labels) { 90 | return Store.newBuilder() 91 | .setId(id) 92 | .setAddress(address) 93 | .setState(state) 94 | .addAllLabels(Arrays.asList(labels)) 95 | .build(); 96 | } 97 | 98 | public static GetStoreResponse makeGetStoreResponse(long clusterId, Store store) { 99 | return GetStoreResponse.newBuilder() 100 | .setHeader(makeDefaultHeader(clusterId)) 101 | .setStore(store) 102 | .build(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/test/java/com/pingcap/tikv/types/MyDecimalTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.types; 19 | 20 | import static org.junit.Assert.*; 21 | 22 | import com.pingcap.tikv.codec.MyDecimal; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | import org.junit.Test; 26 | 27 | public class MyDecimalTest { 28 | @Test 29 | public void fromStringTest() throws Exception { 30 | List test = new ArrayList<>(); 31 | test.add(new MyDecimalTestStruct("12345", "12345", 5, 0)); 32 | test.add(new MyDecimalTestStruct("12345.", "12345", 5, 0)); 33 | test.add(new MyDecimalTestStruct("123.45", "123.45", 5, 2)); 34 | test.add(new MyDecimalTestStruct("-123.45", "-123.45", 5, 2)); 35 | test.add(new MyDecimalTestStruct(".00012345000098765", "0.00012345000098765", 17, 17)); 36 | test.add(new MyDecimalTestStruct(".12345000098765", "0.12345000098765", 14, 14)); 37 | test.add( 38 | new MyDecimalTestStruct("-.000000012345000098765", "-0.000000012345000098765", 21, 21)); 39 | test.add(new MyDecimalTestStruct("0000000.001", "0.001", 3, 3)); 40 | test.add(new MyDecimalTestStruct("1234500009876.5", "1234500009876.5", 14, 1)); 41 | test.forEach( 42 | (a) -> { 43 | MyDecimal dec = new MyDecimal(); 44 | dec.fromString(a.in); 45 | assertEquals(a.precision, dec.precision()); 46 | assertEquals(a.frac, dec.frac()); 47 | assertEquals(a.out, dec.toString()); 48 | }); 49 | } 50 | 51 | @Test 52 | public void toBinToBinFromBinTest() throws Exception { 53 | List test = new ArrayList<>(); 54 | test.add(new MyDecimalTestStruct("-10.55", "-10.55", 4, 2)); 55 | test.add(new MyDecimalTestStruct("12345", "12345", 5, 0)); 56 | test.add(new MyDecimalTestStruct("-12345", "-12345", 5, 0)); 57 | test.add(new MyDecimalTestStruct("0000000.001", "0.001", 3, 3)); 58 | test.add(new MyDecimalTestStruct("0.00012345000098765", "0.00012345000098765", 17, 17)); 59 | test.add(new MyDecimalTestStruct("-0.00012345000098765", "-0.00012345000098765", 17, 17)); 60 | test.forEach( 61 | (a) -> { 62 | MyDecimal dec = new MyDecimal(); 63 | dec.fromString(a.in); 64 | assertEquals(a.out, dec.toString()); 65 | int[] bin = dec.toBin(dec.precision(), dec.frac()); 66 | dec.clear(); 67 | dec.fromBin(a.precision, a.frac, bin); 68 | assertEquals(a.precision, dec.precision()); 69 | assertEquals(a.frac, dec.frac()); 70 | assertEquals(a.out, dec.toString()); 71 | }); 72 | } 73 | 74 | @Test 75 | public void toBinTest() throws Exception { 76 | MyDecimal dec = new MyDecimal(); 77 | dec.fromDecimal(-1234567890.1234); 78 | int[] data = dec.toBin(dec.precision(), dec.frac()); 79 | int[] expected = 80 | new int[] { 81 | 0x7E, 0xF2, 0x04, 0xC7, 0x2D, 0xFB, 0x2D, 82 | }; 83 | // something wrong with toBin and fromBin 84 | assertArrayEquals(expected, data); 85 | } 86 | 87 | // MyDecimalTestStruct is only used for simplifing testing. 88 | private class MyDecimalTestStruct { 89 | String in; 90 | String out; 91 | int precision; 92 | int frac; 93 | 94 | MyDecimalTestStruct(String in, String out, int precision, int frac) { 95 | this.in = in; 96 | this.out = out; 97 | this.precision = precision; 98 | this.frac = frac; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/com/pingcap/tikv/row/ObjectRowImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2017 PingCAP, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.pingcap.tikv.row; 19 | 20 | import com.pingcap.tikv.types.DataType; 21 | import java.sql.Date; 22 | import java.sql.Time; 23 | import java.sql.Timestamp; 24 | 25 | // A dummy implementation of Row interface 26 | // Using non-memory compact format 27 | public class ObjectRowImpl implements Row { 28 | private final Object[] values; 29 | 30 | public static Row create(int fieldCount) { 31 | return new ObjectRowImpl(fieldCount); 32 | } 33 | 34 | private ObjectRowImpl(int fieldCount) { 35 | values = new Object[fieldCount]; 36 | } 37 | 38 | @Override 39 | public void setNull(int pos) { 40 | values[pos] = null; 41 | } 42 | 43 | @Override 44 | public boolean isNull(int pos) { 45 | return values[pos] == null; 46 | } 47 | 48 | @Override 49 | public void setFloat(int pos, float v) { 50 | values[pos] = v; 51 | } 52 | 53 | @Override 54 | public float getFloat(int pos) { 55 | return (float) values[pos]; 56 | } 57 | 58 | @Override 59 | public void setInteger(int pos, int v) { 60 | values[pos] = v; 61 | } 62 | 63 | @Override 64 | public int getInteger(int pos) { 65 | return (int) values[pos]; 66 | } 67 | 68 | @Override 69 | public void setShort(int pos, short v) { 70 | values[pos] = v; 71 | } 72 | 73 | @Override 74 | public short getShort(int pos) { 75 | return (short) values[pos]; 76 | } 77 | 78 | @Override 79 | public void setDouble(int pos, double v) { 80 | values[pos] = v; 81 | } 82 | 83 | @Override 84 | public double getDouble(int pos) { 85 | // Null should be handled by client code with isNull 86 | // below all get method behave the same 87 | return (double) values[pos]; 88 | } 89 | 90 | @Override 91 | public void setLong(int pos, long v) { 92 | values[pos] = v; 93 | } 94 | 95 | @Override 96 | public long getLong(int pos) { 97 | // Null should be handled by client code with isNull 98 | // below all get method behave the same 99 | return (long) values[pos]; 100 | } 101 | 102 | @Override 103 | public void setString(int pos, String v) { 104 | values[pos] = v; 105 | } 106 | 107 | @Override 108 | public String getString(int pos) { 109 | return (String) values[pos]; 110 | } 111 | 112 | @Override 113 | public void setTime(int pos, Time v) { 114 | values[pos] = v; 115 | } 116 | 117 | @Override 118 | public Date getTime(int pos) { 119 | return (Date) values[pos]; 120 | } 121 | 122 | @Override 123 | public void setTimestamp(int pos, Timestamp v) { 124 | values[pos] = v; 125 | } 126 | 127 | @Override 128 | public Timestamp getTimestamp(int pos) { 129 | return (Timestamp) values[pos]; 130 | } 131 | 132 | @Override 133 | public void setDate(int pos, Date v) { 134 | values[pos] = v; 135 | } 136 | 137 | @Override 138 | public Date getDate(int pos) { 139 | return (Date) values[pos]; 140 | } 141 | 142 | @Override 143 | public void setBytes(int pos, byte[] v) { 144 | values[pos] = v; 145 | } 146 | 147 | @Override 148 | public byte[] getBytes(int pos) { 149 | return (byte[]) values[pos]; 150 | } 151 | 152 | @Override 153 | public void set(int pos, DataType type, Object v) { 154 | // Ignore type for this implementation since no serialization happens 155 | values[pos] = v; 156 | } 157 | 158 | @Override 159 | public Object get(int pos, DataType type) { 160 | // Ignore type for this implementation since no serialization happens 161 | return values[pos]; 162 | } 163 | 164 | @Override 165 | public int fieldCount() { 166 | return values.length; 167 | } 168 | } 169 | --------------------------------------------------------------------------------