Skip to content

Commit

Permalink
[dingo-exec] Use the new update, delete and drop interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jwcxs-m committed Aug 29, 2023
1 parent da6a9b8 commit abc2cf5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.dingodb.calcite.schema;

import io.dingodb.calcite.DingoParserContext;
import io.dingodb.common.CommonId;
import io.dingodb.common.partition.PartitionDetailDefinition;
import io.dingodb.common.table.Index;
import io.dingodb.common.table.TableDefinition;
Expand All @@ -26,6 +27,8 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DingoSchema extends AbstractSchema {

Expand All @@ -46,7 +49,12 @@ public void createTables(@NonNull TableDefinition tableDefinition,
}

public boolean dropTable(@NonNull String tableName) {
if (metaService.dropTable(tableName)) {
// Get all index table commonIds
CommonId tableId = metaService.getTableId(tableName);
List<CommonId> tableIds = Stream
.concat(Stream.of(tableId), metaService.getTableIndexDefinitions(tableId).keySet().stream())
.collect(Collectors.toList());
if (metaService.dropTables(tableIds)) {
tableCache.remove(tableName);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ public boolean update(@NonNull KeyValue keyValue) {

@Override
public boolean update(@NonNull Object[] newTuple, @NonNull Object[] oldTuple) {
try {
return update(codec.encode(newTuple), codec.encode(oldTuple));
} catch (IOException e) {
throw new RuntimeException(e);
if (store.insertIndex(newTuple)) {
if (store.updateWithIndex(newTuple, oldTuple)) {
return store.deleteIndex(newTuple, oldTuple);
}
}
return false;
}

@Override
Expand All @@ -139,11 +140,10 @@ public boolean update(@NonNull KeyValue newKeyValue, @NonNull KeyValue oldKeyVal

@Override
public boolean remove(@NonNull Object[] tuple) {
try {
return remove(codec.encode(tuple).getKey());
} catch (IOException e) {
throw new RuntimeException(e);
if (store.deleteWithIndex(tuple)) {
return store.deleteIndex(tuple);
}
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
import io.dingodb.common.CommonId;
import io.dingodb.common.partition.PartitionDetailDefinition;
import io.dingodb.common.partition.RangeDistribution;
import io.dingodb.common.table.ColumnDefinition;
import io.dingodb.common.table.TableDefinition;
import io.dingodb.common.type.DingoTypeFactory;
import io.dingodb.common.type.TupleMapping;
import io.dingodb.common.type.TupleType;
import io.dingodb.common.util.ByteArrayUtils.ComparableByteArray;
import io.dingodb.common.util.Optional;
import io.dingodb.expr.core.TypeCode;
Expand All @@ -39,6 +37,8 @@
import io.dingodb.server.executor.common.Mapping;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -139,6 +139,13 @@ public boolean dropTable(@NonNull String tableName) {
return metaServiceClient.dropTable(tableName);
}

@Override
public boolean dropTables(@NonNull Collection<CommonId> tableIds) {
return metaServiceClient.dropTables(
tableIds.stream().map(Mapping::mapping).collect(Collectors.toCollection(ArrayList::new))
);
}

@Override
public CommonId getTableId(@NonNull String tableName) {
return Optional.mapOrNull(metaServiceClient.getTableId(tableName), Mapping::mapping);
Expand Down
3 changes: 3 additions & 0 deletions dingo-meta-api/src/main/java/io/dingodb/meta/MetaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.dingodb.common.util.ByteArrayUtils.ComparableByteArray;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
Expand Down Expand Up @@ -111,6 +112,8 @@ default boolean isRoot() {
*/
boolean dropTable(@NonNull String tableName);

boolean dropTables(@NonNull Collection<CommonId> tableIds);

/**
* Get table id by table name.
* Notice: check the table name case, because by default, the table names are converted to uppercase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.dingodb.meta.TableStatistic;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -137,6 +138,12 @@ public boolean dropTable(@NonNull String tableName) {
return false;
}

@Override
public boolean dropTables(@NonNull Collection<CommonId> tableIds) {
tableIds.forEach(tableDefinitions::remove);
return true;
}

@Override
public CommonId getTableId(@NonNull String tableName) {
String tableNameU = tableName.toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,42 @@ public boolean update(KeyValue row, KeyValue old) {
throw new IllegalArgumentException();
}

@Override
public boolean updateWithIndex(Object[] newRecord, Object[] oldRecord) {
KeyValue newKeyValue = convertRow(newRecord);
KeyValue oldKeyValue = convertRow(oldRecord);
if (Arrays.equals(newKeyValue.getKey(), oldKeyValue.getKey())) {
if (Arrays.equals(db.get(newKeyValue.getKey()), oldKeyValue.getValue())) {
db.put(newKeyValue.getKey(), newKeyValue.getValue());
return true;
} else {
return false;
}
}
throw new IllegalArgumentException();
}

@Override
public boolean delete(byte[] key) {
db.remove(key);
return true;
}

@Override
public boolean deleteIndex(Object[] key) {
return true;
}

public boolean deleteIndex(Object[] newRecord, Object[] oldRecord) {
return true;
}

@Override
public boolean deleteWithIndex(Object[] key) {
db.remove(convertRow(key).getKey());
return true;
}

private KeyValue convertRow(Object[] row) {
KeyValue keyValue = null;
try {
Expand Down

0 comments on commit abc2cf5

Please sign in to comment.