Skip to content

Commit 705c981

Browse files
authored
support sync server capacity and negate expr (#288)
1 parent 3a1c4b6 commit 705c981

File tree

7 files changed

+217
-0
lines changed

7 files changed

+217
-0
lines changed

src/main/java/com/alipay/oceanbase/rpc/ObTableClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,6 +4245,15 @@ public ObServerRoute getRoute(boolean readonly) {
42454245
}
42464246
}
42474247

4248+
private ObTableServerCapacity getServerCapacity() {
4249+
if (tableRoster.isEmpty()) {
4250+
throw new IllegalStateException("client is not initialized and obTable is empty");
4251+
}
4252+
Iterator<ObTable> iterator = tableRoster.values().iterator();
4253+
ObTable firstObTable = iterator.next();
4254+
return firstObTable.getServerCapacity();
4255+
}
4256+
42484257
public void setOdpAddr(String odpAddr) {
42494258
this.odpAddr = odpAddr;
42504259
}

src/main/java/com/alipay/oceanbase/rpc/bolt/transport/ObTableConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ private void login() throws Exception {
171171
if (result != null && result.getCredential() != null
172172
&& result.getCredential().length() > 0) {
173173
credential = result.getCredential();
174+
obTable.setServerCapacity(result.getServerCapabilities());
174175
tenantId = result.getTenantId();
175176
// Set version if missing
176177
if (ObGlobal.obVsnMajor() == 0) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*-
2+
* #%L
3+
* OBKV Table Client Framework
4+
* %%
5+
* Copyright (C) 2021 OceanBase
6+
* %%
7+
* OBKV Table Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.rpc.protocol.payload.impl.column;
19+
20+
import com.alipay.oceanbase.rpc.protocol.payload.impl.ObCollationType;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
public class ObGeneratedColumnNegateFunc implements ObGeneratedColumnSimpleFunc {
26+
private final List<String> refColumnNames;
27+
28+
/*
29+
* Ob generated column refer func.
30+
*/
31+
public ObGeneratedColumnNegateFunc(String refColumn) {
32+
List<String> refColumnNameList = new ArrayList<String>(1);
33+
refColumnNameList.add(refColumn);
34+
this.refColumnNames = refColumnNameList;
35+
}
36+
37+
/*
38+
* Set parameters.
39+
*/
40+
@Override
41+
public void setParameters(List<Object> parameters) {
42+
//ignore
43+
}
44+
45+
/*
46+
* Get min parameters.
47+
*/
48+
@Override
49+
public int getMinParameters() {
50+
return 0;
51+
}
52+
53+
/*
54+
* Get max parameters.
55+
*/
56+
@Override
57+
public int getMaxParameters() {
58+
return 0;
59+
}
60+
61+
/*
62+
* Get ref column names.
63+
*/
64+
@Override
65+
public List<String> getRefColumnNames() {
66+
return refColumnNames;
67+
}
68+
69+
/*
70+
* Eval value.
71+
*/
72+
@Override
73+
public Object evalValue(ObCollationType collationType, Object... refs)
74+
throws IllegalArgumentException {
75+
if (refs == null || refs.length == 0) {
76+
throw new IllegalArgumentException("Input references cannot be null or empty");
77+
}
78+
Object ref = refs[0];
79+
if (ref instanceof Long) {
80+
return -(Long)ref;
81+
} else if (ref instanceof Integer) {
82+
Integer value = (Integer) ref;
83+
if (value == Integer.MIN_VALUE) {
84+
throw new IllegalArgumentException("The currently provided parameter is the " +
85+
"minimum value of the Integer type, and its negation will cause an overflow.");
86+
}
87+
return -(Integer) value;
88+
} else {
89+
throw new IllegalArgumentException("Object [" + ref + "] can not evaluate by" +
90+
" ObGeneratedColumnNegateFunc");
91+
}
92+
}
93+
}

src/main/java/com/alipay/oceanbase/rpc/protocol/payload/impl/parser/ObGeneratedColumnExpressParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.alipay.oceanbase.rpc.protocol.payload.impl.parser;
1919

2020
import com.alipay.oceanbase.rpc.exception.GenerateColumnParseException;
21+
import com.alipay.oceanbase.rpc.protocol.payload.impl.column.ObGeneratedColumnNegateFunc;
2122
import com.alipay.oceanbase.rpc.protocol.payload.impl.column.ObGeneratedColumnReferFunc;
2223
import com.alipay.oceanbase.rpc.protocol.payload.impl.column.ObGeneratedColumnSimpleFunc;
2324
import com.alipay.oceanbase.rpc.protocol.payload.impl.column.ObGeneratedColumnSubStrFunc;
@@ -66,6 +67,10 @@ public ObGeneratedColumnSimpleFunc parse() throws GenerateColumnParseException {
6667
default:
6768
throw new GenerateColumnParseException("");
6869
}
70+
case SUB:
71+
// If the first token is a '-' sign, then generate the opposite number expression.
72+
String refColumn = lexer.stringVal();
73+
return new ObGeneratedColumnNegateFunc(refColumn);
6974
default:
7075
throw new GenerateColumnParseException("ERROR. token : " + lexer.token()
7176
+ ", pos : " + lexer.pos());

src/main/java/com/alipay/oceanbase/rpc/table/ObTable.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public class ObTable extends AbstractObTable implements Lifecycle {
6262
private ConnectionFactory connectionFactory;
6363
private ObTableRemoting realClient;
6464
private ObTableConnectionPool connectionPool;
65+
66+
private ObTableServerCapacity serverCapacity = new ObTableServerCapacity();
6567

6668
private Map<String, Object> configs;
6769

@@ -514,6 +516,20 @@ public void setPort(int port) {
514516
this.port = port;
515517
}
516518

519+
/*
520+
* Get server capacity.
521+
*/
522+
public ObTableServerCapacity getServerCapacity() {
523+
return serverCapacity;
524+
}
525+
526+
/*
527+
* Set server capacity.
528+
*/
529+
public void setServerCapacity(int flags) {
530+
serverCapacity.setFlags(flags);
531+
}
532+
517533
/*
518534
* Get tenant name.
519535
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*-
2+
* #%L
3+
* OBKV Table Client Framework
4+
* %%
5+
* Copyright (C) 2021 OceanBase
6+
* %%
7+
* OBKV Table Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.rpc.table;
19+
20+
public class ObTableServerCapacity {
21+
private static final int DISTRIBUTED_EXECUTE = 1 << 0;
22+
private static final int SECONDARY_PARTITION = 1 << 1;
23+
private static final int TIME_SERIES = 1 << 2;
24+
private static final int CAPACITY_MAX = 1 << 31;
25+
private int flags = 0;
26+
27+
public int getFlags() { return flags; }
28+
29+
public void setFlags(int flags) {
30+
this.flags = flags;
31+
}
32+
33+
public boolean isSupportDistributedExecute() {
34+
return (flags & DISTRIBUTED_EXECUTE) != 0;
35+
}
36+
37+
public boolean isSupportSecondaryPartition() {
38+
return (flags & SECONDARY_PARTITION) != 0;
39+
}
40+
41+
public boolean isSupportTimeSeries() {
42+
return (flags & TIME_SERIES) != 0;
43+
}
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*-
2+
* #%L
3+
* OBKV Table Client Framework
4+
* %%
5+
* Copyright (C) 2021 OceanBase
6+
* %%
7+
* OBKV Table Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.rpc.protocol.payload.impl.column;
19+
20+
import com.alipay.oceanbase.rpc.protocol.payload.impl.ObCollationType;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class ObGeneratedColumnNegateFuncTest {
28+
@Test
29+
public void testEval() {
30+
ObGeneratedColumnSimpleFunc func = new ObGeneratedColumnNegateFunc("K");
31+
Assert.assertEquals(-1L, func.evalValue(ObCollationType.CS_TYPE_INVALID, 1L));
32+
Assert.assertEquals(-11L, func.evalValue(ObCollationType.CS_TYPE_INVALID, 11L));
33+
Assert.assertEquals(-111,
34+
func.evalValue(ObCollationType.CS_TYPE_INVALID, 111));
35+
Assert.assertEquals(1111L,
36+
func.evalValue(ObCollationType.CS_TYPE_INVALID, -1111L));
37+
Assert.assertEquals(11111L,
38+
func.evalValue(ObCollationType.CS_TYPE_INVALID, -11111L));
39+
try {
40+
func.evalValue(ObCollationType.CS_TYPE_INVALID, Integer.MIN_VALUE);
41+
} catch (Exception e) {
42+
Assert.assertTrue(e.getMessage().contains("The currently provided parameter is the " +
43+
"minimum value of the Integer type, and its negation will cause an overflow."));
44+
}
45+
List<String> refColumns = new ArrayList<String>();
46+
refColumns.add("K");
47+
Assert.assertEquals(refColumns, func.getRefColumnNames());
48+
}
49+
}

0 commit comments

Comments
 (0)