
com.alipay.oceanbase.rpc.location.model.partition.ObRangePartDesc Maven / Gradle / Ivy
/*-
* #%L
* OBKV Table Client Framework
* %%
* Copyright (C) 2021 OceanBase
* %%
* OBKV Table Client Framework is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* #L%
*/
package com.alipay.oceanbase.rpc.location.model.partition;
import com.alipay.oceanbase.rpc.exception.ObTablePartitionConsistentException;
import com.alipay.oceanbase.rpc.protocol.payload.impl.ObColumn;
import com.alipay.oceanbase.rpc.protocol.payload.impl.ObObjType;
import com.alipay.oceanbase.rpc.protocol.payload.impl.column.ObGeneratedColumn;
import com.alipay.oceanbase.rpc.protocol.payload.impl.column.ObSimpleColumn;
import com.alipay.oceanbase.rpc.util.RandomUtil;
import com.alipay.oceanbase.rpc.util.TableClientLoggerFactory;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.alipay.oceanbase.rpc.util.TableClientLoggerFactory.LCD;
import static com.alipay.oceanbase.rpc.util.TableClientLoggerFactory.RUNTIME;
public class ObRangePartDesc extends ObPartDesc {
private static final Logger logger = TableClientLoggerFactory.getLogger(ObRangePartDesc.class);
/*
* Ob range part desc.
*/
public ObRangePartDesc() {
setPartFuncType(ObPartFuncType.RANGE);
}
private int partSpace = 0;
private int partNum = 0;
private List orderedCompareColumns = null;
private List orderedCompareColumnTypes = null;
private List> bounds = null;
// save the upper bound of the partition key
// demo:
// PARTITION P0 VALUES LESS THAN (500, 't', 't'),
// PARTITION P0 VALUES LESS THAN (1000, 'T', 'T'),
// -> [[500, t, t], [1000, T, T]]
private List> highBoundValues = null;
/*
* Get ordered compare column types.
*/
public List getOrderedCompareColumnTypes() {
return orderedCompareColumnTypes;
}
/*
* Set ordered compare column types.
*/
public void setOrderedCompareColumnTypes(List orderedPartColumnTypes) {
this.orderedCompareColumnTypes = orderedPartColumnTypes;
}
/*
* Set ordered compare columns.
*/
public void setOrderedCompareColumns(List orderedPartColumn) {
this.orderedCompareColumns = orderedPartColumn;
}
/*
* Get ordered compare columns.
*/
public List getOrderedCompareColumns() {
return orderedCompareColumns;
}
/*
* Set bounds.
*/
public void setBounds(List> bounds) {
this.bounds = bounds;
}
public List> getBounds() {
return bounds;
}
/*
* Set highBoundValues
*/
public void setHighBoundValues(List> highBoundValues) {
this.highBoundValues = highBoundValues;
}
public List> getHighBoundValues() {
return highBoundValues;
}
/*
* Get random part id.
*/
@Override
public Long getRandomPartId() {
if (null == this.bounds) {
return null;
}
int randomIndex = RandomUtil.getRandomNum(0, this.bounds.size());
return this.bounds.get(randomIndex).value;
}
/*
* Prepare.
*/
@Override
public void prepare() throws IllegalArgumentException {
if (bounds == null || bounds.size() == 0) {
throw new IllegalArgumentException(
"prepare ObRangePartDesc failed. partition bounds is empty " + bounds);
}
if (orderedCompareColumnTypes == null || orderedCompareColumnTypes.size() == 0) {
throw new IllegalArgumentException(
"prepare ObRangePartDesc failed. partition orderedCompareColumnTypes is empty "
+ orderedCompareColumnTypes);
}
if (orderedCompareColumns == null || orderedCompareColumns.size() == 0) {
throw new IllegalArgumentException(
"prepare ObRangePartDesc failed. partition orderedCompareColumns is empty "
+ orderedCompareColumnTypes);
}
if (orderedCompareColumns.size() != orderedCompareColumnTypes.size()) {
throw new IllegalArgumentException(
"prepare ObRangePartDesc failed. the size of orderedCompareColumns is as same as the size of orderedCompareColumnTypes"
+ orderedCompareColumnTypes);
}
// Range 类型的type比较特殊 在part_range_type中需要特殊转换一下
List orderedPartColumnTmp = new ArrayList(orderedCompareColumns.size());
for (int i = 0; i < orderedCompareColumns.size(); i++) {
ObColumn obColumn = orderedCompareColumns.get(i);
ObColumn convert;
if (obColumn.getObGeneratedColumnSimpleFunc() != null) {
convert = new ObGeneratedColumn(obColumn.getColumnName(), obColumn.getIndex(),
orderedCompareColumnTypes.get(i), obColumn.getObCollationType(),
obColumn.getObGeneratedColumnSimpleFunc());
} else {
convert = new ObSimpleColumn(obColumn.getColumnName(), obColumn.getIndex(),
orderedCompareColumnTypes.get(i), obColumn.getObCollationType());
}
orderedPartColumnTmp.add(convert);
}
this.orderedCompareColumns = orderedPartColumnTmp;
if (orderedCompareColumnTypes.size() != orderedPartColumnNames.size()) {
throw new IllegalArgumentException(
"prepare ObRangePartDesc failed. the size of orderedCompareColumnTypes is not equal with the size of orderedCompareColumns. types "
+ orderedCompareColumnTypes + " columns" + orderedPartColumnNames);
}
super.prepare();
}
/*
* Get part space.
*/
public int getPartSpace() {
return partSpace;
}
/*
* Set part space.
*/
public void setPartSpace(int partSpace) {
this.partSpace = partSpace;
}
/*
* Get part num.
*/
public int getPartNum() {
return this.partNum;
}
/*
* Set part num.
*/
public void setPartNum(int partNum) {
this.partNum = partNum;
}
/*
* Get part ids.
*/
@Override
public List getPartIds(Object[] start, boolean startInclusive, Object[] end,
boolean endInclusive) {
// can not detail the border effect so that the range is magnified
int startIdx = getBoundsIdx(true, start);
int stopIdx = getBoundsIdx(true, end);
List partIds = new ArrayList();
for (int i = startIdx; i <= stopIdx; i++) {
partIds.add(this.bounds.get(i).value);
}
return partIds;
}
/*
* Get part id.
*/
@Override
public Long getPartId(Object... rowKey) {
try {
return this.bounds.get(getBoundsIdx(false, rowKey)).value;
} catch (IllegalArgumentException e) {
RUNTIME.error(LCD.convert("01-00025"), e);
throw new IllegalArgumentException(
"ObRangePartDesc get part id come across illegal params", e);
}
}
public int getBoundsIdx(boolean isScan, Object... rowKey) {
if (rowKey.length != rowKeyElement.size()) {
throw new IllegalArgumentException("row key is consist of " + rowKeyElement
+ "but found" + Arrays.toString(rowKey));
}
try {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy