Skip to content

Commit

Permalink
[feat][sdk] Support add table column and add/drop index for exist table
Browse files Browse the repository at this point in the history
  • Loading branch information
wt0530 committed May 9, 2024
1 parent 5a83bef commit a415b05
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@

package io.dingodb.sdk.common.codec;

import io.dingodb.sdk.common.DingoCommonId;
import io.dingodb.sdk.common.KeyValue;
import io.dingodb.sdk.common.serial.RecordDecoder;
import io.dingodb.sdk.common.serial.RecordEncoder;
import io.dingodb.sdk.common.serial.schema.DingoSchema;
import io.dingodb.sdk.common.table.Column;
import io.dingodb.sdk.common.table.Table;
import io.dingodb.sdk.common.utils.Optional;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.IOException;
import java.util.List;

public class DingoKeyValueCodec implements KeyValueCodec {

private List<DingoSchema> schemas;
private final long id;
private final List<DingoSchema> schemas;
RecordEncoder re;
RecordDecoder rd;

Expand All @@ -39,16 +41,25 @@ public DingoKeyValueCodec(long id, List<DingoSchema> schemas) {

public DingoKeyValueCodec(int schemaVersion, long id, List<DingoSchema> schemas) {
this.schemas = schemas;
this.id = id;
re = new RecordEncoder(schemaVersion, schemas, id);
rd = new RecordDecoder(schemaVersion, schemas, id);
}

public static DingoKeyValueCodec of(Table table) {
return of(
table.getVersion(),
Optional.mapOrGet(table.id(), DingoCommonId::entityId, () -> 0L),
table.getColumns()
);
}

public static DingoKeyValueCodec of(long id, Table table) {
return of(table.getVersion(), id, table.getColumns());
return of(table.getVersion(), id, table);
}

public static DingoKeyValueCodec of(long id, List<Column> columns) {
return new DingoKeyValueCodec(1, id, CodecUtils.createSchemaForColumns(columns));
return of(1, id, columns);
}

public static DingoKeyValueCodec of(int schemaVersion,long id, Table table) {
Expand Down Expand Up @@ -94,6 +105,10 @@ public byte[] encodeMaxKeyPrefix() {
return re.encodeMaxKeyPrefix();
}

public byte[] resetPrefix(byte[] key) {
return re.resetKeyPrefix(key, id);
}

@Override
public byte[] resetPrefix(byte[] key, long prefix) {
return re.resetKeyPrefix(key, prefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public interface KeyValueCodec {

byte[] encodeKey(Object[] record);

default byte[] encodeKeyPrefix(Object[] record) {
return encodeKeyPrefix(record, record.length);
}

byte[] encodeKeyPrefix(Object[] record, int columnCount);

byte[] encodeMinKeyPrefix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@

public interface Partition {

@Deprecated
String getFuncName();

default String getStrategy() {
return getFuncName();
}

List<String> getCols();

List<PartitionDetail> getDetails();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@

package io.dingodb.sdk.common.partition;

import io.dingodb.sdk.common.DingoCommonId;

public interface PartitionDetail {

String getPartName();
default DingoCommonId id() {
return null;
}

default String getPartName() {
return null;
}

String getOperator();
default String getOperator() {
return null;
}

Object[] getOperand();
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ private void encodeTag(Buf buf) {
buf.reverseWrite((byte) 0);
}

private void encodeReverseTag(Buf buf) {
buf.reverseWrite(Config.CODEC_VERSION);
buf.reverseWrite((byte) 0);
buf.reverseWrite((byte) 0);
buf.reverseWrite((byte) 0);
}

private void encodeSchemaVersion(Buf buf) {
buf.writeInt(schemaVersion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

public interface Column {

int DEFAULT_PRECISION = -1;
int DEFAULT_SCALE = Integer.MIN_VALUE;
int DISABLE = 1;
int HIDDEN = 1 << 1;

String getName();

String getType();
Expand All @@ -36,10 +41,24 @@ public interface Column {

boolean isAutoIncrement();

int getState();

String getComment();

default int getState() {
return DISABLE;
}

default int getCreateVersion() {
return 1;
}

default int getUpdateVersion() {
return 1;
}

default int getDeleteVersion() {
return -1;
}

default boolean isPrimary() {
return getPrimary() > -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@

package io.dingodb.sdk.common.table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Builder
@ToString
@EqualsAndHashCode
@AllArgsConstructor
public class ColumnDefinition implements Column {

public static final int DEFAULT_PRECISION = -1;
public static final int DEFAULT_SCALE = Integer.MIN_VALUE;

private String name;
private String type;
private String elementType;
@Builder.Default
private int precision = DEFAULT_PRECISION;
private int precision = Column.DEFAULT_PRECISION;
@Builder.Default
private int scale = DEFAULT_SCALE;
private int scale = Column.DEFAULT_SCALE;
@Builder.Default
private boolean nullable = true;
@Builder.Default
Expand All @@ -48,6 +49,9 @@ public class ColumnDefinition implements Column {

@Setter
private String comment;
private int createVersion;
private int updateVersion;
private int deleteVersion;

@Deprecated
public ColumnDefinition(
Expand Down Expand Up @@ -81,45 +85,11 @@ public String getName() {
return name.toUpperCase();
}

@Override
public String getType() {
return type;
}

@Override
public String getElementType() {
return elementType;
}

@Override
public int getPrecision() {
return precision;
}

@Override
public int getScale() {
return scale;
}

@Override
public boolean isNullable() {
return nullable;
}

@Override
public int getPrimary() {
return primary < 0 ? -1 : primary;
}

@Override
public String getDefaultValue() {
return defaultValue;
}

@Override
public boolean isAutoIncrement() {
return isAutoIncrement;
}

@Override
public int getState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.dingodb.sdk.common.table;

import io.dingodb.common.Common;
import io.dingodb.sdk.common.DingoCommonId;
import io.dingodb.sdk.common.index.IndexParameter;
import io.dingodb.sdk.common.partition.Partition;

Expand All @@ -27,6 +27,10 @@

public interface Table {

default DingoCommonId id() {
return null;
}

String getName();

List<Column> getColumns();
Expand Down
4 changes: 4 additions & 0 deletions proto/meta.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ message ColumnDefinition {
int32 state = 20;

string comment = 30;

uint32 create_version = 51;
uint32 update_version = 52;
uint32 delete_version = 53;
}

// Information about Index.
Expand Down

0 comments on commit a415b05

Please sign in to comment.