Skip to content

Instantly share code, notes, and snippets.

@junwen12221
Created April 19, 2021 07:23
Show Gist options
  • Save junwen12221/e2433d3b26a19dab5df81d8ddbe47904 to your computer and use it in GitHub Desktop.
Save junwen12221/e2433d3b26a19dab5df81d8ddbe47904 to your computer and use it in GitHub Desktop.
//package io.mycat.drdsrunner;
//
//import com.alibaba.druid.sql.SQLUtils;
//import com.alibaba.druid.sql.ast.SQLStatement;
//import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
//import io.mycat.DrdsRunner;
//import io.mycat.MetaClusterCurrent;
//import io.mycat.MetadataManager;
//import io.mycat.MycatCore;
//import io.mycat.assemble.MycatTest;
//import io.mycat.calcite.MycatCalciteSupport;
//import io.mycat.calcite.physical.MycatInsertRel;
//import io.mycat.calcite.physical.MycatUpdateRel;
//import io.mycat.calcite.rewriter.IndexCondition;
//import io.mycat.config.*;
//import io.mycat.hint.CreateClusterHint;
//import io.mycat.hint.CreateDataSourceHint;
//import io.mycat.router.ShardingTableHandler;
//import io.mycat.util.JsonUtil;
//import org.apache.calcite.persistor.MycatUpdateRelPersistor;
//import org.apache.calcite.persistor.RelOptCostPersistor;
//import org.apache.calcite.plan.RelOptCost;
//import org.junit.Assert;
//import org.junit.Test;
//
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.Map;
//
//import static io.mycat.assemble.MycatTest.DB1;
//import static io.mycat.assemble.MycatTest.DB2;
//import static org.apache.calcite.persistor.RelOptCostPersistor.toRelOptCost;
//
//public class PersistTest {
//
//
// @Test
// public void testMycaUpdateRel() {
// initRuntime();
// MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
// MycatUpdateRel mycatUpdateRel = MycatUpdateRel.create(IndexCondition.EMPTY ,
// (SQLStatement) SQLUtils.parseSingleMysqlStatement(
// "INSERT INTO db1.sharding (id) VALUES (?)"),"db1","sharding");
// String persist = MycatUpdateRelPersistor.persist(mycatUpdateRel);
// Assert.assertEquals(persist, MycatUpdateRelPersistor.persist(MycatUpdateRelPersistor.unPersist(persist)));
// }
// @Test
// public void testMycatInsertRel() {
// initRuntime();
// MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
// MycatInsertRel mycatInsertRel = MycatInsertRel.create(-1, Arrays.asList(1),
// (MySqlInsertStatement) SQLUtils.parseSingleMysqlStatement(
// "INSERT INTO db1.sharding (id) VALUES (?)"), (ShardingTableHandler) metadataManager.getTable("db1", "sharding"));
// String persist = MycatInsertRelPersistor.persist(mycatInsertRel);
// Assert.assertEquals(persist, MycatInsertRelPersistor.persist(MycatInsertRelPersistor.unPersist(persist)));
// }
// public static void main(String[] args) {
// initRuntime();
// MetadataManager wrapper = MetaClusterCurrent.wrapper(MetadataManager.class);
// System.out.println();
// }
//
// public static void initRuntime() {
// MycatCore mycatCore = new MycatCore();
// FileMetadataStorageManager fileMetadataStorageManager = MetaClusterCurrent.wrapper(FileMetadataStorageManager.class);
// MycatRouterConfig mycatRouterConfig = new MycatRouterConfig();
// LogicSchemaConfig logicSchemaConfig = new LogicSchemaConfig();
// mycatRouterConfig.getSchemas().add(logicSchemaConfig);
// logicSchemaConfig.setSchemaName("db1");
//
// ShardingTableConfig mainSharding = new ShardingTableConfig();
// mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" +
// " `id` bigint NOT NULL AUTO_INCREMENT,\n" +
// " `user_id` varchar(100) DEFAULT NULL,\n" +
// " `traveldate` date DEFAULT NULL,\n" +
// " `fee` decimal(10,0) DEFAULT NULL,\n" +
// " `days` int DEFAULT NULL,\n" +
// " `blob` longblob,\n" +
// " PRIMARY KEY (`id`),\n" +
// " KEY `id` (`id`)\n" +
// ") ENGINE=InnoDB DEFAULT CHARSET=utf8"
// + " dbpartition by hash(id) tbpartition by hash(id) tbpartitions 2 dbpartitions 2;");
// mainSharding.setFunction(ShardingFuntion.builder().properties(JsonUtil.from("{\n" +
// "\t\t\t\t\t\"dbNum\":\"2\",\n" +
// "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" +
// "\t\t\t\t\t\"tableNum\":\"2\",\n" +
// "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\n" +
// "\t\t\t\t\t\"storeNum\":2,\n" +
// "\t\t\t\t\t\"dbMethod\":\"hash(id)\"\n" +
// "\t\t\t\t}", Map.class)).build());
// logicSchemaConfig.getShadingTables().put("sharding", mainSharding);
// mycatRouterConfig.getClusters().add(CreateClusterHint.createConfig("c0", Arrays.asList("ds0"), Collections.emptyList()));
// mycatRouterConfig.getDatasources().add(CreateDataSourceHint.createConfig("ds0", MycatTest.DB1));
// mycatRouterConfig.getDatasources().add(CreateDataSourceHint.createConfig("prototype", MycatTest.DB1));
// fileMetadataStorageManager.start(mycatRouterConfig);
// }
//
//
// @Test
// public void testVolcanoCostPersist() {
// String text = "{\n" +
// "\t\"cpu\":2.0,\n" +
// "\t\"io\":3.0,\n" +
// "\t\"rowCount\":1.0,\n" +
// "\t\"className\":\"org.apache.calcite.plan.volcano.VolcanoCost\"\n" +
// "}";
// RelOptCost relOptCost = toRelOptCost(text);
// Assert.assertEquals(text, RelOptCostPersistor. persist(relOptCost));
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment