【源码阅读】2. Catalog和Database

发布时间 2023-06-25 18:39:04作者: xutao_ustc
 

Catalog

创建

    | KW_CREATE KW_CATALOG opt_if_not_exists:ifNotExists ident:catalogName opt_properties:properties
    {:
         RESULT = new CreateCatalogStmt(ifNotExists, catalogName, null, properties);
    :}
    | KW_CREATE KW_CATALOG opt_if_not_exists:ifNotExists ident:catalogName KW_WITH KW_RESOURCE ident:resourceName opt_properties:properties
    {:
         RESULT = new CreateCatalogStmt(ifNotExists, catalogName, resourceName, properties);
    :}
 可以通过两种方式建立Catalog
-- 方式一 
CREATE RESOURCE mysql_resource PROPERTIES (
    "type"="jdbc",
    "user"="root",
    "password"="123456",
    "jdbc_url" = "jdbc:mysql://127.0.0.1:3316/doris_test?useSSL=false",
    "driver_url" = "https://doris-community-test-1308700295.cos.ap-hongkong.myqcloud.com/jdbc_driver/mysql-connector-java-8.0.25.jar",
    "driver_class" = "com.mysql.cj.jdbc.Driver"
);
CREATE CATALOG jdbc WITH RESOURCE mysql_resource;

-- 方式二
CREATE CATALOG jdbc PROPERTIES (
    "type"="jdbc",
    "user"="root",
    "password"="123456",
    "jdbc_url" = "jdbc:mysql://127.0.0.1:3316/doris_test?useSSL=false",
    "driver_url" = "https://doris-community-test-1308700295.cos.ap-hongkong.myqcloud.com/jdbc_driver/mysql-connector-java-8.0.25.jar",
    "driver_class" = "com.mysql.cj.jdbc.Driver"
);

 

展示

Show Catalog有两种写法,分别展示所有Catalog列表和单Catalog的属性

 

展示创建

ShowCreateCatalogStmt在CatalogMgr.showCreateCatalog中拼接show create catalog语句

 

switch

最终设置ConnectContext.defaultCatalog属性

 

refresh

刷新Catalog时:

● 重置objectCreated - 因为每一步操作都会先去makeSureInitialized,其中会判断objectCreated,这里设置了之后,后面会触发客户端创建

● 重置initialized - 因为每一步操作都会先去makeSureInitialized,其中会判断initialized,这里设置了之后,后面会触发ExternalDatabase的生成(重置)

● 清空Cache

    public void setUninitialized(boolean invalidCache) {
        this.objectCreated = false;
        this.initialized = false;
        this.invalidCacheInInit = invalidCache;
        if (invalidCache) {
            Env.getCurrentEnv().getExtMetaCacheMgr().invalidateCatalogCache(id);
        }
    }
 

Catalog接口调用

Catalog在实际进行接口调用时,会首先makeSureInitialized,判断objectCreated和initialized,保证以下事情是完成的:

● 构建Client

● 刷新DB列表

 

DB

创建

    KW_CREATE KW_DATABASE opt_if_not_exists:ifNotExists ident:db opt_properties:properties
    {:
        RESULT = new CreateDbStmt(ifNotExists, db, properties);
    :}
 

主要是InternalCatalog.createDb:创建Database对象并注册到内存

 

展示

    | KW_DATABASES opt_wild_where
    {:
        RESULT = new ShowDbStmt(parser.wild, parser.where);
    :}
    | KW_DATABASES KW_FROM ident:catalogName
    {:
        RESULT = new ShowDbStmt(null, null, catalogName);
    :}
 

 

展示创建

 

use

这个语句比较特殊,没有走一般的Query子流程,而是在dispatch时,直接走到了handleInitDb里面。而且调用它时,还会顺带发送如下子句

● SELECT DATABASE()

● show databases

● show tables

最终调用ConnectContext.setDatabase设置ConnectContext.currentDb属性

 

refresh Database

刷新Database时:

● 重置initialized - 因为每一步操作都会先去makeSureInitialized,其中会判断initialized,这里设置了之后,后面会触发ExternalTable的生成

● 清空DB Cache

    public void setUnInitialized(boolean invalidCache) {
        this.initialized = false;
        this.invalidCacheInInit = invalidCache;
        if (invalidCache) {
            Env.getCurrentEnv().getExtMetaCacheMgr().invalidateDbCache(extCatalog.getId(), name);
        }
    }

 

 

Database接口调用

Database在实际进行接口调用时,会首先makeSureInitialized,判断Catalog.makeSureInitialized和initialized,保证以下事情是完成的:

● Catalog初始化完成

  • 构建Client
  • 刷新DB列表

● 刷新Table列表

 

InternalCatalog之内部默认information_schema

InfoSchemaDb是内部Database的实现类。SchemaTable是其中加入的Table类型。