Merge branch 'develop' into feature/query
This commit is contained in:
commit
88341598e9
|
@ -104,6 +104,10 @@ pipeline {
|
|||
find pytest -name '*'sql|xargs rm -rf
|
||||
./test-all.sh p2
|
||||
date'''
|
||||
sh '''
|
||||
cd ${WKC}/tests
|
||||
./test-all.sh b4fq
|
||||
'''
|
||||
}
|
||||
}
|
||||
stage('test_b1') {
|
||||
|
|
|
@ -4,7 +4,7 @@ PROJECT(TDengine)
|
|||
IF (DEFINED VERNUMBER)
|
||||
SET(TD_VER_NUMBER ${VERNUMBER})
|
||||
ELSE ()
|
||||
SET(TD_VER_NUMBER "2.0.12.0")
|
||||
SET(TD_VER_NUMBER "2.0.13.0")
|
||||
ENDIF ()
|
||||
|
||||
IF (DEFINED VERCOMPATIBLE)
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
TDengine采用关系型数据模型,需要建库、建表。因此对于一个具体的应用场景,需要考虑库的设计,超级表和普通表的设计。本节不讨论细致的语法规则,只介绍概念。
|
||||
|
||||
关于数据建模请参考<a href="https://www.taosdata.com/blog/2020/11/11/1945.html">视频教程</a>。
|
||||
|
||||
## 创建库
|
||||
|
||||
不同类型的数据采集点往往具有不同的数据特征,包括数据采集频率的高低,数据保留时间的长短,副本的数目,数据块的大小,是否允许更新数据等等。为让各种场景下TDengine都能最大效率的工作,TDengine建议将不同数据特征的表创建在不同的库里,因为每个库可以配置不同的存储策略。创建一个库时,除SQL标准的选项外,应用还可以指定保留时长、副本数、内存块个数、时间精度、文件块里最大最小记录条数、是否压缩、一个数据文件覆盖的天数等多种参数。比如:
|
||||
|
@ -60,4 +62,3 @@ TDengine支持多列模型,只要物理量是一个数据采集点同时采集
|
|||
|
||||
TDengine建议尽可能采用多列模型,因为插入效率以及存储效率更高。但对于有些场景,一个采集点的采集量的种类经常变化,这个时候,如果采用多列模型,就需要频繁修改超级表的结构定义,让应用变的复杂,这个时候,采用单列模型会显得简单。
|
||||
|
||||
关于数据建模请参考<a href="https://www.taosdata.com/blog/2020/11/11/1945.html">视频教程</a>。
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
TDengine的集群管理极其简单,除添加和删除节点需要人工干预之外,其他全部是自动完成,最大程度的降低了运维的工作量。本章对集群管理的操作做详细的描述。
|
||||
|
||||
关于集群搭建请参考<a href="https://www.taosdata.com/blog/2020/11/11/1961.html">视频教程</a>。
|
||||
|
||||
## 准备工作
|
||||
|
||||
**第零步**:规划集群所有物理节点的FQDN,将规划好的FQDN分别添加到每个物理节点的/etc/hostname;修改每个物理节点的/etc/hosts,将所有集群物理节点的IP与FQDN的对应添加好。【如部署了DNS,请联系网络管理员在DNS上做好相关配置】
|
||||
|
@ -227,4 +229,3 @@ SHOW MNODES;
|
|||
|
||||
TDengine提供一个执行程序tarbitrator, 找任何一台Linux服务器运行它即可。请点击[安装包下载](https://www.taosdata.com/cn/all-downloads/),在TDengine Arbitrator Linux一节中,选择适合的版本下载并安装。该程序对系统资源几乎没有要求,只需要保证有网络连接即可。该应用的命令行参数`-p`可以指定其对外服务的端口号,缺省是6042。配置每个taosd实例时,可以在配置文件taos.cfg里将参数arbitrator设置为arbitrator的End Point。如果该参数配置了,当副本数为偶数数,系统将自动连接配置的arbitrator。如果副本数为奇数,即使配置了arbitrator, 系统也不会去建立连接。
|
||||
|
||||
关于集群搭建请参考<a href="https://www.taosdata.com/blog/2020/11/11/1961.html">视频教程</a>。
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: tdengine
|
||||
base: core18
|
||||
version: '2.0.12.0'
|
||||
version: '2.0.13.0'
|
||||
icon: snap/gui/t-dengine.svg
|
||||
summary: an open-source big data platform designed and optimized for IoT.
|
||||
description: |
|
||||
|
@ -72,7 +72,7 @@ parts:
|
|||
- usr/bin/taosd
|
||||
- usr/bin/taos
|
||||
- usr/bin/taosdemo
|
||||
- usr/lib/libtaos.so.2.0.12.0
|
||||
- usr/lib/libtaos.so.2.0.13.0
|
||||
- usr/lib/libtaos.so.1
|
||||
- usr/lib/libtaos.so
|
||||
|
||||
|
|
|
@ -237,21 +237,21 @@ static bool bnCheckVgroupReady(SVgObj *pVgroup, SVnodeGid *pRmVnode) {
|
|||
bool isReady = false;
|
||||
for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) {
|
||||
SVnodeGid *pVnode = pVgroup->vnodeGid + i;
|
||||
SDnodeObj *pDnode = pVnode->pDnode;
|
||||
if (pVnode == pRmVnode) continue;
|
||||
int32_t vver = mnodeGetVgidVer(pVnode->vver);
|
||||
|
||||
mTrace("vgId:%d, check vgroup status, vindex:%d dnode:%d status:%s role:%s vver:%d, rmvver:%d" , pVgroup->vgId, i,
|
||||
pVnode->dnodeId, dnodeStatus[pVnode->pDnode->status], syncRole[pVnode->role], vver, rmVnodeVer);
|
||||
if (pVnode->pDnode->status == TAOS_DN_STATUS_DROPPING) continue;
|
||||
if (pVnode->pDnode->status == TAOS_DN_STATUS_OFFLINE) continue;
|
||||
mTrace("vgId:%d, check vgroup status, vindex:%d dnode:%d status:%s role:%s vver:%d, rmvver:%d", pVgroup->vgId, i,
|
||||
pVnode->dnodeId, dnodeStatus[pDnode->status], syncRole[pVnode->role], vver, rmVnodeVer);
|
||||
if (pDnode->status == TAOS_DN_STATUS_DROPPING) continue;
|
||||
if (pDnode->status == TAOS_DN_STATUS_OFFLINE) continue;
|
||||
if (pVnode->role != TAOS_SYNC_ROLE_SLAVE && pVnode->role != TAOS_SYNC_ROLE_MASTER) continue;
|
||||
|
||||
if (rmVnodeVer == 0 || vver >= rmVnodeVer) {
|
||||
mInfo("vgId:%d, is ready for vindex:%d in dnode:%d status:%s role:%s vver:%d larger than rmvver:%d", pVgroup->vgId, i,
|
||||
pVnode->dnodeId, dnodeStatus[pVnode->pDnode->status], syncRole[pVnode->role], vver, rmVnodeVer);
|
||||
mInfo("vgId:%d, is ready for vindex:%d in dnode:%d status:%s role:%s vver:%d larger than rmvver:%d",
|
||||
pVgroup->vgId, i, pVnode->dnodeId, dnodeStatus[pDnode->status], syncRole[pVnode->role], vver, rmVnodeVer);
|
||||
isReady = true;
|
||||
}
|
||||
|
||||
isReady = true;
|
||||
}
|
||||
|
||||
return isReady;
|
||||
|
|
|
@ -421,7 +421,7 @@ void tscTableMetaCallBack(void *param, TAOS_RES *res, int code) {
|
|||
SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex);
|
||||
|
||||
// check if it is a sub-query of super table query first, if true, enter another routine
|
||||
if (TSDB_QUERY_HAS_TYPE(pQueryInfo->type, (TSDB_QUERY_TYPE_STABLE_SUBQUERY|TSDB_QUERY_TYPE_TAG_FILTER_QUERY))) {
|
||||
if (TSDB_QUERY_HAS_TYPE(pQueryInfo->type, (TSDB_QUERY_TYPE_STABLE_SUBQUERY|TSDB_QUERY_TYPE_SUBQUERY|TSDB_QUERY_TYPE_TAG_FILTER_QUERY))) {
|
||||
tscDebug("%p update local table meta, continue to process sql and send the corresponding query", pSql);
|
||||
|
||||
STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
|
||||
|
|
|
@ -75,11 +75,11 @@ static int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SC
|
|||
static int32_t convertFunctionId(int32_t optr, int16_t* functionId);
|
||||
static uint8_t convertOptr(SStrToken *pToken);
|
||||
|
||||
static int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSelection, bool isSTable, bool joinQuery);
|
||||
static int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSelection, bool isSTable, bool joinQuery, bool intervalQuery);
|
||||
|
||||
static bool validateIpAddress(const char* ip, size_t size);
|
||||
static bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo);
|
||||
static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery);
|
||||
static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool intervalQuery);
|
||||
|
||||
static int32_t parseGroupbyClause(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd);
|
||||
|
||||
|
@ -1475,7 +1475,7 @@ static void addPrimaryTsColIntoResult(SQueryInfo* pQueryInfo) {
|
|||
pQueryInfo->type |= TSDB_QUERY_TYPE_PROJECTION_QUERY;
|
||||
}
|
||||
|
||||
int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSelection, bool isSTable, bool joinQuery) {
|
||||
int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSelection, bool isSTable, bool joinQuery, bool intervalQuery) {
|
||||
assert(pSelection != NULL && pCmd != NULL);
|
||||
|
||||
const char* msg2 = "functions can not be mixed up";
|
||||
|
@ -1531,7 +1531,7 @@ int32_t parseSelectClause(SSqlCmd* pCmd, int32_t clauseIndex, tSQLExprList* pSel
|
|||
addPrimaryTsColIntoResult(pQueryInfo);
|
||||
}
|
||||
|
||||
if (!functionCompatibleCheck(pQueryInfo, joinQuery)) {
|
||||
if (!functionCompatibleCheck(pQueryInfo, joinQuery, intervalQuery)) {
|
||||
return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg2);
|
||||
}
|
||||
|
||||
|
@ -2810,7 +2810,7 @@ bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo)
|
|||
return false;
|
||||
}
|
||||
|
||||
static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery) {
|
||||
static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool intervalQuery) {
|
||||
int32_t startIdx = 0;
|
||||
|
||||
size_t numOfExpr = tscSqlExprNumOfExprs(pQueryInfo);
|
||||
|
@ -2826,6 +2826,10 @@ static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery) {
|
|||
|
||||
int32_t factor = functionCompatList[tscSqlExprGet(pQueryInfo, startIdx)->functionId];
|
||||
|
||||
if (tscSqlExprGet(pQueryInfo, 0)->functionId == TSDB_FUNC_LAST_ROW && (joinQuery || intervalQuery)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// diff function cannot be executed with other function
|
||||
// arithmetic function can be executed with other arithmetic functions
|
||||
size_t size = tscSqlExprNumOfExprs(pQueryInfo);
|
||||
|
@ -2850,7 +2854,7 @@ static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery) {
|
|||
}
|
||||
}
|
||||
|
||||
if (functionId == TSDB_FUNC_LAST_ROW && joinQuery) {
|
||||
if (functionId == TSDB_FUNC_LAST_ROW && (joinQuery || intervalQuery)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -6303,7 +6307,7 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) {
|
|||
}
|
||||
|
||||
bool isSTable = UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo);
|
||||
if (parseSelectClause(&pSql->cmd, 0, pQuerySql->pSelection, isSTable, false) != TSDB_CODE_SUCCESS) {
|
||||
if (parseSelectClause(&pSql->cmd, 0, pQuerySql->pSelection, isSTable, false, false) != TSDB_CODE_SUCCESS) {
|
||||
return TSDB_CODE_TSC_INVALID_SQL;
|
||||
}
|
||||
|
||||
|
@ -6552,7 +6556,9 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) {
|
|||
|
||||
int32_t joinQuery = (pQuerySql->from != NULL && taosArrayGetSize(pQuerySql->from) > 2);
|
||||
|
||||
if (parseSelectClause(pCmd, index, pQuerySql->pSelection, isSTable, joinQuery) != TSDB_CODE_SUCCESS) {
|
||||
int32_t intervalQuery = !(pQuerySql->interval.type == 0 || pQuerySql->interval.n == 0);
|
||||
|
||||
if (parseSelectClause(pCmd, index, pQuerySql->pSelection, isSTable, joinQuery, intervalQuery) != TSDB_CODE_SUCCESS) {
|
||||
return TSDB_CODE_TSC_INVALID_SQL;
|
||||
}
|
||||
|
||||
|
|
|
@ -458,12 +458,13 @@ void tscFreeRegisteredSqlObj(void *pSql) {
|
|||
|
||||
assert(RID_VALID(p->self));
|
||||
|
||||
int32_t num = atomic_sub_fetch_32(&pTscObj->numOfObj, 1);
|
||||
int32_t total = atomic_sub_fetch_32(&tscNumOfObj, 1);
|
||||
|
||||
tscDebug("%p free SqlObj, total in tscObj:%d, total:%d", pSql, num, total);
|
||||
tscFreeSqlObj(p);
|
||||
taosReleaseRef(tscRefId, pTscObj->rid);
|
||||
|
||||
int32_t num = atomic_sub_fetch_32(&pTscObj->numOfObj, 1);
|
||||
int32_t total = atomic_sub_fetch_32(&tscNumOfObj, 1);
|
||||
tscDebug("%p free SqlObj, total in tscObj:%d, total:%d", pSql, num, total);
|
||||
}
|
||||
|
||||
void tscFreeSqlObj(SSqlObj* pSql) {
|
||||
|
|
|
@ -19,136 +19,137 @@ using System.Runtime.InteropServices;
|
|||
|
||||
namespace TDengineDriver
|
||||
{
|
||||
enum TDengineDataType {
|
||||
TSDB_DATA_TYPE_NULL = 0, // 1 bytes
|
||||
TSDB_DATA_TYPE_BOOL = 1, // 1 bytes
|
||||
TSDB_DATA_TYPE_TINYINT = 2, // 1 bytes
|
||||
TSDB_DATA_TYPE_SMALLINT = 3, // 2 bytes
|
||||
TSDB_DATA_TYPE_INT = 4, // 4 bytes
|
||||
TSDB_DATA_TYPE_BIGINT = 5, // 8 bytes
|
||||
TSDB_DATA_TYPE_FLOAT = 6, // 4 bytes
|
||||
TSDB_DATA_TYPE_DOUBLE = 7, // 8 bytes
|
||||
TSDB_DATA_TYPE_BINARY = 8, // string
|
||||
TSDB_DATA_TYPE_TIMESTAMP = 9,// 8 bytes
|
||||
TSDB_DATA_TYPE_NCHAR = 10 // unicode string
|
||||
}
|
||||
|
||||
enum TDengineInitOption
|
||||
{
|
||||
TSDB_OPTION_LOCALE = 0,
|
||||
TSDB_OPTION_CHARSET = 1,
|
||||
TSDB_OPTION_TIMEZONE = 2,
|
||||
TDDB_OPTION_CONFIGDIR = 3,
|
||||
TDDB_OPTION_SHELL_ACTIVITY_TIMER = 4
|
||||
}
|
||||
|
||||
class TDengineMeta
|
||||
{
|
||||
public string name;
|
||||
public short size;
|
||||
public byte type;
|
||||
public string TypeName()
|
||||
enum TDengineDataType
|
||||
{
|
||||
switch ((TDengineDataType)type)
|
||||
{
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BOOL:
|
||||
return "BOOLEAN";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
|
||||
return "BYTE";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
|
||||
return "SHORT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_INT:
|
||||
return "INT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
|
||||
return "LONG";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
|
||||
return "FLOAT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
|
||||
return "DOUBLE";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BINARY:
|
||||
return "STRING";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
return "TIMESTAMP";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
|
||||
return "NCHAR";
|
||||
default:
|
||||
return "undefine";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TDengine
|
||||
{
|
||||
public const int TSDB_CODE_SUCCESS = 0;
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_init", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Init();
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_cleanup", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Cleanup();
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_options", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Options(int option, string value);
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_connect", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr Connect(string ip, string user, string password, string db, short port);
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_errstr", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern private IntPtr taos_errstr(IntPtr res);
|
||||
static public string Error(IntPtr res)
|
||||
{
|
||||
IntPtr errPtr = taos_errstr(res);
|
||||
return Marshal.PtrToStringAnsi(errPtr);
|
||||
TSDB_DATA_TYPE_NULL = 0, // 1 bytes
|
||||
TSDB_DATA_TYPE_BOOL = 1, // 1 bytes
|
||||
TSDB_DATA_TYPE_TINYINT = 2, // 1 bytes
|
||||
TSDB_DATA_TYPE_SMALLINT = 3, // 2 bytes
|
||||
TSDB_DATA_TYPE_INT = 4, // 4 bytes
|
||||
TSDB_DATA_TYPE_BIGINT = 5, // 8 bytes
|
||||
TSDB_DATA_TYPE_FLOAT = 6, // 4 bytes
|
||||
TSDB_DATA_TYPE_DOUBLE = 7, // 8 bytes
|
||||
TSDB_DATA_TYPE_BINARY = 8, // string
|
||||
TSDB_DATA_TYPE_TIMESTAMP = 9,// 8 bytes
|
||||
TSDB_DATA_TYPE_NCHAR = 10 // unicode string
|
||||
}
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_errno", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int ErrorNo(IntPtr res);
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_query", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr Query(IntPtr conn, string sqlstr);
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int AffectRows(IntPtr res);
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_field_count", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int FieldCount(IntPtr res);
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_fetch_fields", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern private IntPtr taos_fetch_fields(IntPtr res);
|
||||
static public List<TDengineMeta> FetchFields(IntPtr res)
|
||||
enum TDengineInitOption
|
||||
{
|
||||
const int fieldSize = 68;
|
||||
|
||||
List<TDengineMeta> metas = new List<TDengineMeta>();
|
||||
if (res == IntPtr.Zero)
|
||||
{
|
||||
return metas;
|
||||
}
|
||||
|
||||
int fieldCount = FieldCount(res);
|
||||
IntPtr fieldsPtr = taos_fetch_fields(res);
|
||||
|
||||
for (int i = 0; i < fieldCount; ++i)
|
||||
{
|
||||
int offset = i * fieldSize;
|
||||
|
||||
TDengineMeta meta = new TDengineMeta();
|
||||
meta.name = Marshal.PtrToStringAnsi(fieldsPtr + offset);
|
||||
meta.type = Marshal.ReadByte(fieldsPtr + offset + 65);
|
||||
meta.size = Marshal.ReadInt16(fieldsPtr + offset + 66);
|
||||
metas.Add(meta);
|
||||
}
|
||||
|
||||
return metas;
|
||||
TSDB_OPTION_LOCALE = 0,
|
||||
TSDB_OPTION_CHARSET = 1,
|
||||
TSDB_OPTION_TIMEZONE = 2,
|
||||
TDDB_OPTION_CONFIGDIR = 3,
|
||||
TDDB_OPTION_SHELL_ACTIVITY_TIMER = 4
|
||||
}
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_fetch_row", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr FetchRows(IntPtr res);
|
||||
class TDengineMeta
|
||||
{
|
||||
public string name;
|
||||
public short size;
|
||||
public byte type;
|
||||
public string TypeName()
|
||||
{
|
||||
switch ((TDengineDataType)type)
|
||||
{
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BOOL:
|
||||
return "BOOLEAN";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
|
||||
return "BYTE";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
|
||||
return "SHORT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_INT:
|
||||
return "INT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
|
||||
return "LONG";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
|
||||
return "FLOAT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
|
||||
return "DOUBLE";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BINARY:
|
||||
return "STRING";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
return "TIMESTAMP";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
|
||||
return "NCHAR";
|
||||
default:
|
||||
return "undefine";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_free_result", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr FreeResult(IntPtr res);
|
||||
class TDengine
|
||||
{
|
||||
public const int TSDB_CODE_SUCCESS = 0;
|
||||
|
||||
[DllImport("taos.dll", EntryPoint = "taos_close", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int Close(IntPtr taos);
|
||||
}
|
||||
}
|
||||
[DllImport("taos", EntryPoint = "taos_init", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Init();
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_cleanup", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Cleanup();
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_options", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Options(int option, string value);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_connect", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr Connect(string ip, string user, string password, string db, short port);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_errstr", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern private IntPtr taos_errstr(IntPtr res);
|
||||
static public string Error(IntPtr res)
|
||||
{
|
||||
IntPtr errPtr = taos_errstr(res);
|
||||
return Marshal.PtrToStringAnsi(errPtr);
|
||||
}
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_errno", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int ErrorNo(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_query", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr Query(IntPtr conn, string sqlstr);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int AffectRows(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_field_count", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int FieldCount(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_fetch_fields", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern private IntPtr taos_fetch_fields(IntPtr res);
|
||||
static public List<TDengineMeta> FetchFields(IntPtr res)
|
||||
{
|
||||
const int fieldSize = 68;
|
||||
|
||||
List<TDengineMeta> metas = new List<TDengineMeta>();
|
||||
if (res == IntPtr.Zero)
|
||||
{
|
||||
return metas;
|
||||
}
|
||||
|
||||
int fieldCount = FieldCount(res);
|
||||
IntPtr fieldsPtr = taos_fetch_fields(res);
|
||||
|
||||
for (int i = 0; i < fieldCount; ++i)
|
||||
{
|
||||
int offset = i * fieldSize;
|
||||
|
||||
TDengineMeta meta = new TDengineMeta();
|
||||
meta.name = Marshal.PtrToStringAnsi(fieldsPtr + offset);
|
||||
meta.type = Marshal.ReadByte(fieldsPtr + offset + 65);
|
||||
meta.size = Marshal.ReadInt16(fieldsPtr + offset + 66);
|
||||
metas.Add(meta);
|
||||
}
|
||||
|
||||
return metas;
|
||||
}
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_fetch_row", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr FetchRows(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_free_result", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr FreeResult(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_close", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int Close(IntPtr taos);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,6 +56,12 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.47</version>
|
||||
</dependency>
|
||||
|
||||
<!-- for restful -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
|
@ -73,7 +79,14 @@
|
|||
<version>1.2.58</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.49</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
|
@ -0,0 +1,808 @@
|
|||
/***************************************************************************
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* This program is free software: you can use, redistribute, and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3
|
||||
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*****************************************************************************/
|
||||
package com.taosdata.jdbc;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractDatabaseMetaData implements DatabaseMetaData {
|
||||
|
||||
private final static String PRODUCT_NAME = "TDengine";
|
||||
private final static String PRODUCT_VESION = "2.0.x.x";
|
||||
private final static String DRIVER_NAME = "taos-jdbcdriver";
|
||||
private final static String DRIVER_VERSION = "2.0.x";
|
||||
private final static int DRIVER_MAJAR_VERSION = 2;
|
||||
private final static int DRIVER_MINOR_VERSION = 0;
|
||||
|
||||
public boolean allProceduresAreCallable() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean allTablesAreSelectable() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public abstract String getURL() throws SQLException;
|
||||
|
||||
public abstract String getUserName() throws SQLException;
|
||||
|
||||
public boolean isReadOnly() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean nullsAreSortedHigh() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean nullsAreSortedLow() throws SQLException {
|
||||
return !nullsAreSortedHigh();
|
||||
}
|
||||
|
||||
public boolean nullsAreSortedAtStart() throws SQLException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean nullsAreSortedAtEnd() throws SQLException {
|
||||
return !nullsAreSortedAtStart();
|
||||
}
|
||||
|
||||
public String getDatabaseProductName() throws SQLException {
|
||||
return PRODUCT_NAME;
|
||||
}
|
||||
|
||||
public String getDatabaseProductVersion() throws SQLException {
|
||||
return PRODUCT_VESION;
|
||||
}
|
||||
|
||||
public String getDriverName() throws SQLException {
|
||||
return DRIVER_NAME;
|
||||
}
|
||||
|
||||
public String getDriverVersion() throws SQLException {
|
||||
return DRIVER_VERSION;
|
||||
}
|
||||
|
||||
public int getDriverMajorVersion() {
|
||||
return DRIVER_MAJAR_VERSION;
|
||||
}
|
||||
|
||||
public int getDriverMinorVersion() {
|
||||
return DRIVER_MINOR_VERSION;
|
||||
}
|
||||
|
||||
public boolean usesLocalFiles() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean usesLocalFilePerTable() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsMixedCaseIdentifiers() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean storesUpperCaseIdentifiers() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean storesLowerCaseIdentifiers() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean storesMixedCaseIdentifiers() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsMixedCaseQuotedIdentifiers() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean storesUpperCaseQuotedIdentifiers() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean storesLowerCaseQuotedIdentifiers() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean storesMixedCaseQuotedIdentifiers() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getIdentifierQuoteString() throws SQLException {
|
||||
return " ";
|
||||
}
|
||||
|
||||
public String getSQLKeywords() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getNumericFunctions() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getStringFunctions() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getSystemFunctions() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getTimeDateFunctions() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getSearchStringEscape() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getExtraNameCharacters() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean supportsAlterTableWithAddColumn() throws SQLException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean supportsAlterTableWithDropColumn() throws SQLException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean supportsColumnAliasing() throws SQLException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean nullPlusNonNullIsNull() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsConvert() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsConvert(int fromType, int toType) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsTableCorrelationNames() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsDifferentTableCorrelationNames() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsExpressionsInOrderBy() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsOrderByUnrelated() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsGroupBy() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsGroupByUnrelated() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsGroupByBeyondSelect() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsLikeEscapeClause() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsMultipleResultSets() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsMultipleTransactions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsNonNullableColumns() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsMinimumSQLGrammar() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsCoreSQLGrammar() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsExtendedSQLGrammar() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsANSI92EntryLevelSQL() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsANSI92IntermediateSQL() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsANSI92FullSQL() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsIntegrityEnhancementFacility() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsOuterJoins() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsFullOuterJoins() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsLimitedOuterJoins() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getSchemaTerm() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getProcedureTerm() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getCatalogTerm() throws SQLException {
|
||||
return "database";
|
||||
}
|
||||
|
||||
public boolean isCatalogAtStart() throws SQLException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getCatalogSeparator() throws SQLException {
|
||||
return ".";
|
||||
}
|
||||
|
||||
public boolean supportsSchemasInDataManipulation() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSchemasInProcedureCalls() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSchemasInTableDefinitions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSchemasInIndexDefinitions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSchemasInPrivilegeDefinitions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsCatalogsInDataManipulation() throws SQLException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean supportsCatalogsInProcedureCalls() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsCatalogsInTableDefinitions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsCatalogsInIndexDefinitions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsPositionedDelete() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsPositionedUpdate() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSelectForUpdate() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsStoredProcedures() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSubqueriesInComparisons() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSubqueriesInExists() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSubqueriesInIns() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsSubqueriesInQuantifieds() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsCorrelatedSubqueries() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsUnion() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsUnionAll() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsOpenCursorsAcrossCommit() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsOpenCursorsAcrossRollback() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsOpenStatementsAcrossCommit() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsOpenStatementsAcrossRollback() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getMaxBinaryLiteralLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxCharLiteralLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxColumnNameLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxColumnsInGroupBy() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxColumnsInIndex() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxColumnsInOrderBy() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxColumnsInSelect() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxColumnsInTable() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxConnections() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxCursorNameLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxIndexLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxSchemaNameLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxProcedureNameLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxCatalogNameLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxRowSize() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean doesMaxRowSizeIncludeBlobs() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getMaxStatementLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxStatements() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxTableNameLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxTablesInSelect() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMaxUserNameLength() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getDefaultTransactionIsolation() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean supportsTransactions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsTransactionIsolationLevel(int level) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsDataManipulationTransactionsOnly() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean dataDefinitionCausesTransactionCommit() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean dataDefinitionIgnoredInTransactions() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern)
|
||||
throws SQLException {
|
||||
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
public ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern,
|
||||
String columnNamePattern) throws SQLException {
|
||||
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
public abstract ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)
|
||||
throws SQLException;
|
||||
|
||||
public ResultSet getSchemas() throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public abstract ResultSet getCatalogs() throws SQLException;
|
||||
|
||||
public ResultSet getTableTypes() throws SQLException {
|
||||
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
|
||||
// set up ColumnMetaDataList
|
||||
List<ColumnMetaData> columnMetaDataList = new ArrayList<ColumnMetaData>(1);
|
||||
ColumnMetaData colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(0);
|
||||
colMetaData.setColName("TABLE_TYPE");
|
||||
colMetaData.setColSize(10);
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_BINARY);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
|
||||
// set up rowDataList
|
||||
List<TSDBResultSetRowData> rowDataList = new ArrayList<TSDBResultSetRowData>(2);
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData();
|
||||
rowData.setString(0, "TABLE");
|
||||
rowDataList.add(rowData);
|
||||
rowData = new TSDBResultSetRowData();
|
||||
rowData.setString(0, "STABLE");
|
||||
rowDataList.add(rowData);
|
||||
|
||||
resultSet.setColumnMetaDataList(columnMetaDataList);
|
||||
resultSet.setRowDataList(rowDataList);
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
public abstract ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException;
|
||||
|
||||
protected int getNullable(int index, String typeName) {
|
||||
if (index == 0 && "TIMESTAMP".equals(typeName))
|
||||
return DatabaseMetaData.columnNoNulls;
|
||||
return DatabaseMetaData.columnNullable;
|
||||
}
|
||||
|
||||
protected int getColumnSize(String typeName, int length) {
|
||||
switch (typeName) {
|
||||
case "TIMESTAMP":
|
||||
return 23;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
protected int getDecimalDigits(String typeName) {
|
||||
switch (typeName) {
|
||||
case "FLOAT":
|
||||
return 5;
|
||||
case "DOUBLE":
|
||||
return 9;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
protected int getDataType(String typeName) {
|
||||
switch (typeName) {
|
||||
case "TIMESTAMP":
|
||||
return Types.TIMESTAMP;
|
||||
case "INT":
|
||||
return Types.INTEGER;
|
||||
case "BIGINT":
|
||||
return Types.BIGINT;
|
||||
case "FLOAT":
|
||||
return Types.FLOAT;
|
||||
case "DOUBLE":
|
||||
return Types.DOUBLE;
|
||||
case "BINARY":
|
||||
return Types.BINARY;
|
||||
case "SMALLINT":
|
||||
return Types.SMALLINT;
|
||||
case "TINYINT":
|
||||
return Types.TINYINT;
|
||||
case "BOOL":
|
||||
return Types.BOOLEAN;
|
||||
case "NCHAR":
|
||||
return Types.NCHAR;
|
||||
default:
|
||||
return Types.NULL;
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern)
|
||||
throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern)
|
||||
throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable)
|
||||
throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getVersionColumns(String catalog, String schema, String table) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable,
|
||||
String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getTypeInfo() throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate)
|
||||
throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public boolean supportsResultSetType(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean ownUpdatesAreVisible(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean ownDeletesAreVisible(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean ownInsertsAreVisible(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean othersUpdatesAreVisible(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean othersDeletesAreVisible(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean othersInsertsAreVisible(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean updatesAreDetected(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean deletesAreDetected(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean insertsAreDetected(int type) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsBatchUpdates() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types)
|
||||
throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean supportsSavepoints() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsNamedParameters() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsMultipleOpenResults() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsGetGeneratedKeys() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern,
|
||||
String attributeNamePattern) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public boolean supportsResultSetHoldability(int holdability) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getResultSetHoldability() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getDatabaseMajorVersion() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getDatabaseMinorVersion() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getJDBCMajorVersion() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getJDBCMinorVersion() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getSQLStateType() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean locatorsUpdateCopy() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean supportsStatementPooling() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public RowIdLifetime getRowIdLifetime() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean supportsStoredFunctionsUsingCallSyntax() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean autoCommitFailureClosesAllResultSets() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
public ResultSet getClientInfoProperties() throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern)
|
||||
throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern,
|
||||
String columnNamePattern) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern,
|
||||
String columnNamePattern) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public boolean generatedKeyAlwaysReturned() throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
private ResultSet getEmptyResultSet() {
|
||||
return new EmptyResultSet();
|
||||
}
|
||||
}
|
|
@ -19,68 +19,71 @@ import java.util.Map;
|
|||
|
||||
public abstract class TSDBConstants {
|
||||
|
||||
public static final String DEFAULT_PORT = "6200";
|
||||
public static final String UNSUPPORT_METHOD_EXCEPTIONZ_MSG = "this operation is NOT supported currently!";
|
||||
public static final String INVALID_VARIABLES = "invalid variables";
|
||||
public static Map<Integer, String> DATATYPE_MAP = null;
|
||||
public static final String DEFAULT_PORT = "6200";
|
||||
public static final String UNSUPPORT_METHOD_EXCEPTIONZ_MSG = "this operation is NOT supported currently!";
|
||||
public static final String INVALID_VARIABLES = "invalid variables";
|
||||
public static Map<Integer, String> DATATYPE_MAP = null;
|
||||
|
||||
public static final long JNI_NULL_POINTER = 0L;
|
||||
public static final long JNI_NULL_POINTER = 0L;
|
||||
|
||||
public static final int JNI_SUCCESS = 0;
|
||||
public static final int JNI_TDENGINE_ERROR = -1;
|
||||
public static final int JNI_CONNECTION_NULL = -2;
|
||||
public static final int JNI_RESULT_SET_NULL = -3;
|
||||
public static final int JNI_NUM_OF_FIELDS_0 = -4;
|
||||
public static final int JNI_SQL_NULL = -5;
|
||||
public static final int JNI_FETCH_END = -6;
|
||||
|
||||
public static final int TSDB_DATA_TYPE_NULL = 0;
|
||||
public static final int TSDB_DATA_TYPE_BOOL = 1;
|
||||
public static final int TSDB_DATA_TYPE_TINYINT = 2;
|
||||
public static final int TSDB_DATA_TYPE_SMALLINT = 3;
|
||||
public static final int TSDB_DATA_TYPE_INT = 4;
|
||||
public static final int TSDB_DATA_TYPE_BIGINT = 5;
|
||||
public static final int TSDB_DATA_TYPE_FLOAT = 6;
|
||||
public static final int TSDB_DATA_TYPE_DOUBLE = 7;
|
||||
public static final int TSDB_DATA_TYPE_BINARY = 8;
|
||||
public static final int TSDB_DATA_TYPE_TIMESTAMP = 9;
|
||||
public static final int TSDB_DATA_TYPE_NCHAR = 10;
|
||||
|
||||
public static String WrapErrMsg(String msg) {
|
||||
return "TDengine Error: " + msg;
|
||||
}
|
||||
public static final int JNI_SUCCESS = 0;
|
||||
public static final int JNI_TDENGINE_ERROR = -1;
|
||||
public static final int JNI_CONNECTION_NULL = -2;
|
||||
public static final int JNI_RESULT_SET_NULL = -3;
|
||||
public static final int JNI_NUM_OF_FIELDS_0 = -4;
|
||||
public static final int JNI_SQL_NULL = -5;
|
||||
public static final int JNI_FETCH_END = -6;
|
||||
|
||||
public static String FixErrMsg(int code) {
|
||||
switch (code) {
|
||||
case JNI_TDENGINE_ERROR:
|
||||
return WrapErrMsg("internal error of database!");
|
||||
case JNI_CONNECTION_NULL:
|
||||
return WrapErrMsg("invalid tdengine connection!");
|
||||
case JNI_RESULT_SET_NULL:
|
||||
return WrapErrMsg("invalid resultset pointer!");
|
||||
case JNI_NUM_OF_FIELDS_0:
|
||||
return WrapErrMsg("invalid num of fields!");
|
||||
case JNI_SQL_NULL:
|
||||
return WrapErrMsg("can't execute empty sql!");
|
||||
case JNI_FETCH_END:
|
||||
return WrapErrMsg("fetch to the end of resultset");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return WrapErrMsg("unkown error!");
|
||||
}
|
||||
public static final int TSDB_DATA_TYPE_NULL = 0;
|
||||
public static final int TSDB_DATA_TYPE_BOOL = 1;
|
||||
public static final int TSDB_DATA_TYPE_TINYINT = 2;
|
||||
public static final int TSDB_DATA_TYPE_SMALLINT = 3;
|
||||
public static final int TSDB_DATA_TYPE_INT = 4;
|
||||
public static final int TSDB_DATA_TYPE_BIGINT = 5;
|
||||
public static final int TSDB_DATA_TYPE_FLOAT = 6;
|
||||
public static final int TSDB_DATA_TYPE_DOUBLE = 7;
|
||||
public static final int TSDB_DATA_TYPE_BINARY = 8;
|
||||
public static final int TSDB_DATA_TYPE_TIMESTAMP = 9;
|
||||
public static final int TSDB_DATA_TYPE_NCHAR = 10;
|
||||
|
||||
static {
|
||||
DATATYPE_MAP = new HashMap<Integer, String>();
|
||||
DATATYPE_MAP.put(1, "BOOL");
|
||||
DATATYPE_MAP.put(2, "TINYINT");
|
||||
DATATYPE_MAP.put(3, "SMALLINT");
|
||||
DATATYPE_MAP.put(4, "INT");
|
||||
DATATYPE_MAP.put(5, "BIGINT");
|
||||
DATATYPE_MAP.put(6, "FLOAT");
|
||||
DATATYPE_MAP.put(7, "DOUBLE");
|
||||
DATATYPE_MAP.put(8, "BINARY");
|
||||
DATATYPE_MAP.put(9, "TIMESTAMP");
|
||||
DATATYPE_MAP.put(10, "NCHAR");
|
||||
}
|
||||
// nchar field's max length
|
||||
public static final int maxFieldSize = 16 * 1024;
|
||||
|
||||
public static String WrapErrMsg(String msg) {
|
||||
return "TDengine Error: " + msg;
|
||||
}
|
||||
|
||||
public static String FixErrMsg(int code) {
|
||||
switch (code) {
|
||||
case JNI_TDENGINE_ERROR:
|
||||
return WrapErrMsg("internal error of database!");
|
||||
case JNI_CONNECTION_NULL:
|
||||
return WrapErrMsg("invalid tdengine connection!");
|
||||
case JNI_RESULT_SET_NULL:
|
||||
return WrapErrMsg("invalid resultset pointer!");
|
||||
case JNI_NUM_OF_FIELDS_0:
|
||||
return WrapErrMsg("invalid num of fields!");
|
||||
case JNI_SQL_NULL:
|
||||
return WrapErrMsg("can't execute empty sql!");
|
||||
case JNI_FETCH_END:
|
||||
return WrapErrMsg("fetch to the end of resultset");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return WrapErrMsg("unkown error!");
|
||||
}
|
||||
|
||||
static {
|
||||
DATATYPE_MAP = new HashMap<Integer, String>();
|
||||
DATATYPE_MAP.put(1, "BOOL");
|
||||
DATATYPE_MAP.put(2, "TINYINT");
|
||||
DATATYPE_MAP.put(3, "SMALLINT");
|
||||
DATATYPE_MAP.put(4, "INT");
|
||||
DATATYPE_MAP.put(5, "BIGINT");
|
||||
DATATYPE_MAP.put(6, "FLOAT");
|
||||
DATATYPE_MAP.put(7, "DOUBLE");
|
||||
DATATYPE_MAP.put(8, "BINARY");
|
||||
DATATYPE_MAP.put(9, "TIMESTAMP");
|
||||
DATATYPE_MAP.put(10, "NCHAR");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,29 @@
|
|||
package com.taosdata.jdbc.rs;
|
||||
|
||||
import com.taosdata.jdbc.TSDBConstants;
|
||||
import com.taosdata.jdbc.TSDBDriver;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
public class RestfulConnection implements Connection {
|
||||
|
||||
private static final String CONNECTION_IS_CLOSED = "connection is closed.";
|
||||
private static final String AUTO_COMMIT_IS_TRUE = "auto commit is true";
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final Properties props;
|
||||
private final String database;
|
||||
private volatile String database;
|
||||
private final String url;
|
||||
/******************************************************/
|
||||
private boolean isClosed;
|
||||
private DatabaseMetaData metadata;
|
||||
private Map<String, Class<?>> typeMap;
|
||||
private Properties clientInfoProps = new Properties();
|
||||
|
||||
public RestfulConnection(String host, String port, Properties props, String database, String url) {
|
||||
this.host = host;
|
||||
|
@ -21,280 +31,424 @@ public class RestfulConnection implements Connection {
|
|||
this.props = props;
|
||||
this.database = database;
|
||||
this.url = url;
|
||||
this.metadata = new RestfulDatabaseMetaData(url, props.getProperty(TSDBDriver.PROPERTY_KEY_USER), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement createStatement() throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("restful TDengine connection is closed."));
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
|
||||
return new RestfulStatement(this, database);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql) throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
//TODO: prepareStatement
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql) throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String nativeSQL(String sql) throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
|
||||
//nothing did
|
||||
return sql;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAutoCommit(boolean autoCommit) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (!autoCommit)
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getAutoCommit() throws SQLException {
|
||||
return false;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commit() throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (getAutoCommit())
|
||||
throw new SQLException(AUTO_COMMIT_IS_TRUE);
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback() throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (getAutoCommit())
|
||||
throw new SQLException(AUTO_COMMIT_IS_TRUE);
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
|
||||
if (isClosed)
|
||||
return;
|
||||
//TODO: release all resources
|
||||
isClosed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() throws SQLException {
|
||||
return false;
|
||||
return isClosed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatabaseMetaData getMetaData() throws SQLException {
|
||||
//TODO: RestfulDatabaseMetaData is not implemented
|
||||
return new RestfulDatabaseMetaData();
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
|
||||
return this.metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadOnly(boolean readOnly) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReadOnly() throws SQLException {
|
||||
return false;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCatalog(String catalog) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
synchronized (RestfulConnection.class) {
|
||||
this.database = catalog;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCatalog() throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
return this.database;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTransactionIsolation(int level) throws SQLException {
|
||||
//transaction is not supported
|
||||
throw new SQLFeatureNotSupportedException("transactions are not supported");
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
switch (level) {
|
||||
case Connection.TRANSACTION_NONE:
|
||||
break;
|
||||
case Connection.TRANSACTION_READ_UNCOMMITTED:
|
||||
case Connection.TRANSACTION_READ_COMMITTED:
|
||||
case Connection.TRANSACTION_REPEATABLE_READ:
|
||||
case Connection.TRANSACTION_SERIALIZABLE:
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
default:
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public int getTransactionIsolation() throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
//Connection.TRANSACTION_NONE specifies that transactions are not supported.
|
||||
return Connection.TRANSACTION_NONE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLWarning getWarnings() throws SQLException {
|
||||
//TODO: getWarnings not implemented
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWarnings() throws SQLException {
|
||||
throw new SQLFeatureNotSupportedException("clearWarnings not supported.");
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
|
||||
if (resultSetType != ResultSet.TYPE_FORWARD_ONLY) {
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY)
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
return createStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (resultSetType != ResultSet.TYPE_FORWARD_ONLY || resultSetConcurrency != ResultSet.CONCUR_READ_ONLY)
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.INVALID_VARIABLES);
|
||||
|
||||
return this.prepareStatement(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (resultSetType != ResultSet.TYPE_FORWARD_ONLY || resultSetConcurrency != ResultSet.CONCUR_READ_ONLY)
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.INVALID_VARIABLES);
|
||||
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Class<?>> getTypeMap() throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
|
||||
synchronized (RestfulConnection.class) {
|
||||
if (this.typeMap == null) {
|
||||
this.typeMap = new HashMap<>();
|
||||
}
|
||||
return this.typeMap;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
|
||||
synchronized (RestfulConnection.class) {
|
||||
this.typeMap = map;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHoldability(int holdability) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (holdability != ResultSet.HOLD_CURSORS_OVER_COMMIT)
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHoldability() throws SQLException {
|
||||
return 0;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
return ResultSet.HOLD_CURSORS_OVER_COMMIT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Savepoint setSavepoint() throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (getAutoCommit())
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
//nothing to do
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Savepoint setSavepoint(String name) throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (getAutoCommit())
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
//nothing to do
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback(Savepoint savepoint) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
if (getAutoCommit())
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
//nothing to do
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
|
||||
return null;
|
||||
if (resultSetHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT)
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
return createStatement(resultSetType, resultSetConcurrency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
|
||||
return null;
|
||||
if (resultSetHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT)
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
return prepareStatement(sql, resultSetType, resultSetConcurrency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
|
||||
return null;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
|
||||
return null;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
|
||||
return null;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
|
||||
return null;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Clob createClob() throws SQLException {
|
||||
//TODO: not supported
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Blob createBlob() throws SQLException {
|
||||
//TODO: not supported
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NClob createNClob() throws SQLException {
|
||||
//TODO: not supported
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLXML createSQLXML() throws SQLException {
|
||||
//TODO: not supported
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(int timeout) throws SQLException {
|
||||
return false;
|
||||
if (timeout < 0)
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
// TODO:
|
||||
/* The driver shall submit a query on the connection or use some other mechanism that positively verifies
|
||||
the connection is still valid when this method is called.*/
|
||||
return !isClosed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClientInfo(String name, String value) throws SQLClientInfoException {
|
||||
|
||||
if (isClosed)
|
||||
throw new SQLClientInfoException();
|
||||
clientInfoProps.setProperty(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClientInfo(Properties properties) throws SQLClientInfoException {
|
||||
if (isClosed)
|
||||
throw new SQLClientInfoException();
|
||||
|
||||
for (Enumeration<Object> enumer = properties.keys(); enumer.hasMoreElements(); ) {
|
||||
String name = (String) enumer.nextElement();
|
||||
clientInfoProps.put(name, properties.getProperty(name));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getClientInfo(String name) throws SQLException {
|
||||
return null;
|
||||
if (isClosed)
|
||||
throw new SQLClientInfoException();
|
||||
|
||||
return clientInfoProps.getProperty(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties getClientInfo() throws SQLException {
|
||||
return null;
|
||||
if (isClosed)
|
||||
throw new SQLClientInfoException();
|
||||
|
||||
return clientInfoProps;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
|
||||
//TODO: not supported
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
|
||||
//TODO: not supported
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSchema(String schema) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
synchronized (RestfulConnection.class) {
|
||||
this.database = schema;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSchema() throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
return this.database;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abort(Executor executor) throws SQLException {
|
||||
if (executor == null) {
|
||||
throw new SQLException("Executor can not be null");
|
||||
}
|
||||
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNetworkTimeout() throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(CONNECTION_IS_CLOSED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -33,7 +33,7 @@ public class RestfulDriver extends AbstractTaosDriver {
|
|||
return null;
|
||||
|
||||
Properties props = parseURL(url, info);
|
||||
String host = props.getProperty(TSDBDriver.PROPERTY_KEY_HOST, "localhost");
|
||||
String host = props.getProperty(TSDBDriver.PROPERTY_KEY_HOST);
|
||||
String port = props.getProperty(TSDBDriver.PROPERTY_KEY_PORT, "6041");
|
||||
String database = props.containsKey(TSDBDriver.PROPERTY_KEY_DBNAME) ? props.getProperty(TSDBDriver.PROPERTY_KEY_DBNAME) : null;
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -2,13 +2,15 @@ package com.taosdata.jdbc.rs;
|
|||
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RestfulResultSetMetaData implements ResultSetMetaData {
|
||||
|
||||
private List<String> fields;
|
||||
private final String database;
|
||||
private ArrayList<RestfulResultSet.Field> fields;
|
||||
|
||||
public RestfulResultSetMetaData(List<String> fields) {
|
||||
public RestfulResultSetMetaData(String database, ArrayList<RestfulResultSet.Field> fields) {
|
||||
this.database = database;
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
|
@ -24,6 +26,7 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
|
|||
|
||||
@Override
|
||||
public boolean isCaseSensitive(int column) throws SQLException {
|
||||
//TODO
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -39,7 +42,7 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
|
|||
|
||||
@Override
|
||||
public int isNullable(int column) throws SQLException {
|
||||
return 0;
|
||||
return ResultSetMetaData.columnNullable;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -54,7 +57,7 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
|
|||
|
||||
@Override
|
||||
public String getColumnLabel(int column) throws SQLException {
|
||||
return fields.get(column - 1);
|
||||
return fields.get(column - 1).name;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -64,7 +67,7 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
|
|||
|
||||
@Override
|
||||
public String getSchemaName(int column) throws SQLException {
|
||||
return null;
|
||||
return this.database;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -84,7 +87,7 @@ public class RestfulResultSetMetaData implements ResultSetMetaData {
|
|||
|
||||
@Override
|
||||
public String getCatalogName(int column) throws SQLException {
|
||||
return null;
|
||||
return this.database;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -7,20 +7,60 @@ import com.taosdata.jdbc.rs.util.HttpClientPoolUtil;
|
|||
import com.taosdata.jdbc.utils.SqlSyntaxValidator;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RestfulStatement implements Statement {
|
||||
|
||||
private static final String STATEMENT_CLOSED = "Statement already closed.";
|
||||
private boolean closed;
|
||||
private String database;
|
||||
private final RestfulConnection conn;
|
||||
|
||||
public RestfulStatement(RestfulConnection c, String database) {
|
||||
this.conn = c;
|
||||
private volatile RestfulResultSet resultSet;
|
||||
private volatile int affectedRows;
|
||||
private volatile boolean closeOnCompletion;
|
||||
|
||||
public RestfulStatement(RestfulConnection conn, String database) {
|
||||
this.conn = conn;
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
private String[] parseTableIdentifier(String sql) {
|
||||
sql = sql.trim().toLowerCase();
|
||||
String[] ret = null;
|
||||
if (sql.contains("where"))
|
||||
sql = sql.substring(0, sql.indexOf("where"));
|
||||
if (sql.contains("interval"))
|
||||
sql = sql.substring(0, sql.indexOf("interval"));
|
||||
if (sql.contains("fill"))
|
||||
sql = sql.substring(0, sql.indexOf("fill"));
|
||||
if (sql.contains("sliding"))
|
||||
sql = sql.substring(0, sql.indexOf("sliding"));
|
||||
if (sql.contains("group by"))
|
||||
sql = sql.substring(0, sql.indexOf("group by"));
|
||||
if (sql.contains("order by"))
|
||||
sql = sql.substring(0, sql.indexOf("order by"));
|
||||
if (sql.contains("slimit"))
|
||||
sql = sql.substring(0, sql.indexOf("slimit"));
|
||||
if (sql.contains("limit"))
|
||||
sql = sql.substring(0, sql.indexOf("limit"));
|
||||
// parse
|
||||
if (sql.contains("from")) {
|
||||
sql = sql.substring(sql.indexOf("from") + 4).trim();
|
||||
return Arrays.asList(sql.split(",")).stream()
|
||||
.map(tableIdentifier -> {
|
||||
tableIdentifier = tableIdentifier.trim();
|
||||
if (tableIdentifier.contains(" "))
|
||||
tableIdentifier = tableIdentifier.substring(0, tableIdentifier.indexOf(" "));
|
||||
return tableIdentifier;
|
||||
}).collect(Collectors.joining(",")).split(",");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet executeQuery(String sql) throws SQLException {
|
||||
if (isClosed())
|
||||
|
@ -29,43 +69,33 @@ public class RestfulStatement implements Statement {
|
|||
throw new SQLException("not a select sql for executeQuery: " + sql);
|
||||
|
||||
final String url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql";
|
||||
// row data
|
||||
String result = HttpClientPoolUtil.execute(url, sql);
|
||||
String fields = "";
|
||||
List<String> words = Arrays.asList(sql.split(" "));
|
||||
if (words.get(0).equalsIgnoreCase("select")) {
|
||||
int index = 0;
|
||||
if (words.contains("from")) {
|
||||
index = words.indexOf("from");
|
||||
JSONObject resultJson = JSON.parseObject(result);
|
||||
if (resultJson.getString("status").equals("error")) {
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + resultJson.getString("desc") + "\n" + "error code: " + resultJson.getString("code")));
|
||||
}
|
||||
|
||||
// parse table name from sql
|
||||
String[] tableIdentifiers = parseTableIdentifier(sql);
|
||||
if (tableIdentifiers != null) {
|
||||
List<JSONObject> fieldJsonList = new ArrayList<>();
|
||||
for (String tableIdentifier : tableIdentifiers) {
|
||||
// field meta
|
||||
String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier);
|
||||
JSONObject fieldJson = JSON.parseObject(fields);
|
||||
if (fieldJson.getString("status").equals("error")) {
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + fieldJson.getString("desc") + "\n" + "error code: " + fieldJson.getString("code")));
|
||||
}
|
||||
fieldJsonList.add(fieldJson);
|
||||
}
|
||||
if (words.contains("FROM")) {
|
||||
index = words.indexOf("FROM");
|
||||
}
|
||||
fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + words.get(index + 1));
|
||||
this.resultSet = new RestfulResultSet(database, this, resultJson, fieldJsonList);
|
||||
} else {
|
||||
this.resultSet = new RestfulResultSet(database, this, resultJson);
|
||||
}
|
||||
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getString("status").equals("error")) {
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " +
|
||||
jsonObject.getString("desc") + "\n" +
|
||||
"error code: " + jsonObject.getString("code")));
|
||||
}
|
||||
String dataStr = jsonObject.getString("data");
|
||||
if ("use".equalsIgnoreCase(fields.split(" ")[0])) {
|
||||
return new RestfulResultSet(dataStr, "");
|
||||
}
|
||||
|
||||
JSONObject jsonField = JSON.parseObject(fields);
|
||||
if (jsonField == null) {
|
||||
return new RestfulResultSet(dataStr, "");
|
||||
}
|
||||
if (jsonField.getString("status").equals("error")) {
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " +
|
||||
jsonField.getString("desc") + "\n" +
|
||||
"error code: " + jsonField.getString("code")));
|
||||
}
|
||||
String fieldData = jsonField.getString("data");
|
||||
|
||||
return new RestfulResultSet(dataStr, fieldData);
|
||||
this.affectedRows = 0;
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -78,77 +108,103 @@ public class RestfulStatement implements Statement {
|
|||
if (this.database == null)
|
||||
throw new SQLException("Database not specified or available");
|
||||
|
||||
final String url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql";
|
||||
HttpClientPoolUtil.execute(url, "use " + conn.getDatabase());
|
||||
final String url = "http://" + conn.getHost().trim() + ":" + conn.getPort() + "/rest/sql";
|
||||
// HttpClientPoolUtil.execute(url, "use " + conn.getDatabase());
|
||||
String result = HttpClientPoolUtil.execute(url, sql);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getString("status").equals("error")) {
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " +
|
||||
jsonObject.getString("desc") + "\n" +
|
||||
"error code: " + jsonObject.getString("code")));
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + jsonObject.getString("desc") + "\n" + "error code: " + jsonObject.getString("code")));
|
||||
}
|
||||
return Integer.parseInt(jsonObject.getString("rows"));
|
||||
this.resultSet = null;
|
||||
this.affectedRows = Integer.parseInt(jsonObject.getString("rows"));
|
||||
return this.affectedRows;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
this.closed = true;
|
||||
synchronized (RestfulStatement.class) {
|
||||
if (!isClosed())
|
||||
this.closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxFieldSize() throws SQLException {
|
||||
return 0;
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return TSDBConstants.maxFieldSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxFieldSize(int max) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
if (max < 0)
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxRows() throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxRows(int max) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
if (max < 0)
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEscapeProcessing(boolean enable) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(RestfulStatement.STATEMENT_CLOSED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getQueryTimeout() throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setQueryTimeout(int seconds) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
if (seconds < 0)
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() throws SQLException {
|
||||
|
||||
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLWarning getWarnings() throws SQLException {
|
||||
//TODO: getWarnings not Implemented
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWarnings() throws SQLException {
|
||||
|
||||
// nothing to do
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCursorName(String name) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(RestfulStatement.STATEMENT_CLOSED);
|
||||
throw new SQLException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -159,133 +215,181 @@ public class RestfulStatement implements Statement {
|
|||
//如果执行了use操作应该将当前Statement的catalog设置为新的database
|
||||
if (SqlSyntaxValidator.isUseSql(sql)) {
|
||||
this.database = sql.trim().replace("use", "").trim();
|
||||
this.conn.setCatalog(this.database);
|
||||
}
|
||||
if (this.database == null)
|
||||
throw new SQLException("Database not specified or available");
|
||||
|
||||
final String url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql";
|
||||
// use database
|
||||
HttpClientPoolUtil.execute(url, "use " + conn.getDatabase());
|
||||
// execute sql
|
||||
String result = HttpClientPoolUtil.execute(url, sql);
|
||||
// parse result
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getString("status").equals("error")) {
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " +
|
||||
jsonObject.getString("desc") + "\n" +
|
||||
"error code: " + jsonObject.getString("code")));
|
||||
if (SqlSyntaxValidator.isSelectSql(sql)) {
|
||||
executeQuery(sql);
|
||||
} else if (SqlSyntaxValidator.isShowSql(sql) || SqlSyntaxValidator.isDescribeSql(sql)) {
|
||||
final String url = "http://" + conn.getHost().trim() + ":" + conn.getPort() + "/rest/sql";
|
||||
if (!SqlSyntaxValidator.isShowDatabaseSql(sql)) {
|
||||
HttpClientPoolUtil.execute(url, "use " + conn.getDatabase());
|
||||
}
|
||||
String result = HttpClientPoolUtil.execute(url, sql);
|
||||
JSONObject resultJson = JSON.parseObject(result);
|
||||
if (resultJson.getString("status").equals("error")) {
|
||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + resultJson.getString("desc") + "\n" + "error code: " + resultJson.getString("code")));
|
||||
}
|
||||
this.resultSet = new RestfulResultSet(database, this, resultJson);
|
||||
} else {
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet getResultSet() throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUpdateCount() throws SQLException {
|
||||
return 0;
|
||||
if (isClosed()) {
|
||||
throw new SQLException("Invalid method call on a closed statement.");
|
||||
}
|
||||
return this.affectedRows;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getMoreResults() throws SQLException {
|
||||
return false;
|
||||
return getMoreResults(CLOSE_CURRENT_RESULT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFetchDirection(int direction) throws SQLException {
|
||||
|
||||
if (direction != ResultSet.FETCH_FORWARD && direction != ResultSet.FETCH_REVERSE && direction != ResultSet.FETCH_UNKNOWN)
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
this.resultSet.setFetchDirection(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFetchDirection() throws SQLException {
|
||||
return 0;
|
||||
return this.resultSet.getFetchDirection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFetchSize(int rows) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
if (rows < 0)
|
||||
throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFetchSize() throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getResultSetConcurrency() throws SQLException {
|
||||
return 0;
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return this.resultSet.getConcurrency();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getResultSetType() throws SQLException {
|
||||
return 0;
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return this.resultSet.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBatch(String sql) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
//TODO:
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearBatch() throws SQLException {
|
||||
|
||||
//TODO:
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] executeBatch() throws SQLException {
|
||||
//TODO:
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return null;
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return this.conn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getMoreResults(int current) throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
if (resultSet == null)
|
||||
return false;
|
||||
|
||||
// switch (current) {
|
||||
// case CLOSE_CURRENT_RESULT:
|
||||
// resultSet.close();
|
||||
// break;
|
||||
// case KEEP_CURRENT_RESULT:
|
||||
// break;
|
||||
// case CLOSE_ALL_RESULTS:
|
||||
// resultSet.close();
|
||||
// break;
|
||||
// default:
|
||||
// throw new SQLException(TSDBConstants.INVALID_VARIABLES);
|
||||
// }
|
||||
// return next;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet getGeneratedKeys() throws SQLException {
|
||||
return null;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
|
||||
return 0;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
|
||||
return 0;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
|
||||
return 0;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
|
||||
return false;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
|
||||
return false;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String sql, String[] columnNames) throws SQLException {
|
||||
return false;
|
||||
throw new SQLFeatureNotSupportedException(TSDBConstants.UNSUPPORT_METHOD_EXCEPTIONZ_MSG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getResultSetHoldability() throws SQLException {
|
||||
return 0;
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return this.resultSet.getHoldability();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -295,22 +399,30 @@ public class RestfulStatement implements Statement {
|
|||
|
||||
@Override
|
||||
public void setPoolable(boolean poolable) throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPoolable() throws SQLException {
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeOnCompletion() throws SQLException {
|
||||
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
this.closeOnCompletion = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCloseOnCompletion() throws SQLException {
|
||||
return false;
|
||||
if (isClosed())
|
||||
throw new SQLException(STATEMENT_CLOSED);
|
||||
return this.closeOnCompletion;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,6 +17,8 @@ import org.apache.http.protocol.HTTP;
|
|||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
|
||||
public class HttpClientPoolUtil {
|
||||
public static PoolingHttpClientConnectionManager cm = null;
|
||||
|
@ -94,7 +96,9 @@ public class HttpClientPoolUtil {
|
|||
initPools();
|
||||
}
|
||||
method = (HttpEntityEnclosingRequestBase) getRequest(uri, HttpPost.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
|
||||
method.setEntity(new StringEntity(data));
|
||||
method.setHeader("Authorization", "Basic cm9vdDp0YW9zZGF0YQ==");
|
||||
method.setHeader("Content-Type", "text/plain");
|
||||
method.setEntity(new StringEntity(data, Charset.forName("UTF-8")));
|
||||
HttpContext context = HttpClientContext.create();
|
||||
CloseableHttpResponse httpResponse = httpClient.execute(method, context);
|
||||
httpEntity = httpResponse.getEntity();
|
||||
|
@ -105,26 +109,13 @@ public class HttpClientPoolUtil {
|
|||
if (method != null) {
|
||||
method.abort();
|
||||
}
|
||||
// e.printStackTrace();
|
||||
// logger.error("execute post request exception, url:" + uri + ", exception:" + e.toString()
|
||||
// + ", cost time(ms):" + (System.currentTimeMillis() - startTime));
|
||||
new Exception("execute post request exception, url:"
|
||||
+ uri + ", exception:" + e.toString() +
|
||||
", cost time(ms):" + (System.currentTimeMillis() - startTime))
|
||||
.printStackTrace();
|
||||
new Exception("execute post request exception, url:" + uri + ", exception:" + e.toString() + ", cost time(ms):" + (System.currentTimeMillis() - startTime)).printStackTrace();
|
||||
} finally {
|
||||
if (httpEntity != null) {
|
||||
try {
|
||||
EntityUtils.consumeQuietly(httpEntity);
|
||||
} catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// logger.error("close response exception, url:" + uri + ", exception:" + e.toString()
|
||||
// + ", cost time(ms):" + (System.currentTimeMillis() - startTime));
|
||||
new Exception(
|
||||
"close response exception, url:" + uri +
|
||||
", exception:" + e.toString()
|
||||
+ ", cost time(ms):" + (System.currentTimeMillis() - startTime))
|
||||
.printStackTrace();
|
||||
new Exception("close response exception, url:" + uri + ", exception:" + e.toString() + ", cost time(ms):" + (System.currentTimeMillis() - startTime)).printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,14 +15,12 @@
|
|||
package com.taosdata.jdbc.utils;
|
||||
|
||||
import com.taosdata.jdbc.TSDBConnection;
|
||||
import com.taosdata.jdbc.TSDBJNIConnector;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SqlSyntaxValidator {
|
||||
|
||||
private static final String[] updateSQL = {"insert", "update", "delete", "create", "alter", "drop", "show", "describe", "use"};
|
||||
private static final String[] updateSQL = {"insert", "update", "delete", "create", "alter", "drop", "show", "describe", "use", "import"};
|
||||
private static final String[] querySQL = {"select"};
|
||||
|
||||
private TSDBConnection tsdbConnection;
|
||||
|
@ -31,22 +29,6 @@ public class SqlSyntaxValidator {
|
|||
this.tsdbConnection = (TSDBConnection) connection;
|
||||
}
|
||||
|
||||
public boolean validateSqlSyntax(String sql) throws SQLException {
|
||||
|
||||
boolean res = false;
|
||||
if (tsdbConnection == null || tsdbConnection.isClosed()) {
|
||||
throw new SQLException("invalid connection");
|
||||
} else {
|
||||
TSDBJNIConnector jniConnector = tsdbConnection.getConnection();
|
||||
if (jniConnector == null) {
|
||||
throw new SQLException("jniConnector is null");
|
||||
} else {
|
||||
res = jniConnector.validateCreateTableSql(sql);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static boolean isValidForExecuteUpdate(String sql) {
|
||||
for (String prefix : updateSQL) {
|
||||
if (sql.trim().toLowerCase().startsWith(prefix))
|
||||
|
@ -56,18 +38,28 @@ public class SqlSyntaxValidator {
|
|||
}
|
||||
|
||||
public static boolean isUseSql(String sql) {
|
||||
return sql.trim().toLowerCase().startsWith(updateSQL[8]) || sql.trim().toLowerCase().matches("create\\s*database.*") || sql.toLowerCase().toLowerCase().matches("drop\\s*database.*");
|
||||
return sql.trim().toLowerCase().startsWith("use") || sql.trim().toLowerCase().matches("create\\s*database.*") || sql.toLowerCase().toLowerCase().matches("drop\\s*database.*");
|
||||
}
|
||||
|
||||
public static boolean isUpdateSql(String sql) {
|
||||
return sql.trim().toLowerCase().startsWith(updateSQL[1]);
|
||||
public static boolean isShowSql(String sql) {
|
||||
return sql.trim().toLowerCase().startsWith("show");
|
||||
}
|
||||
|
||||
public static boolean isDescribeSql(String sql) {
|
||||
return sql.trim().toLowerCase().startsWith("describe");
|
||||
}
|
||||
|
||||
|
||||
public static boolean isInsertSql(String sql) {
|
||||
return sql.trim().toLowerCase().startsWith(updateSQL[0]);
|
||||
return sql.trim().toLowerCase().startsWith("insert") || sql.trim().toLowerCase().startsWith("import");
|
||||
}
|
||||
|
||||
public static boolean isSelectSql(String sql) {
|
||||
return sql.trim().toLowerCase().startsWith(querySQL[0]);
|
||||
return sql.trim().toLowerCase().startsWith("select");
|
||||
}
|
||||
|
||||
|
||||
public static boolean isShowDatabaseSql(String sql) {
|
||||
return sql.trim().toLowerCase().matches("show\\s*databases");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.junit.Test;
|
|||
import java.sql.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DatabaseMetaDataTest extends BaseTest {
|
||||
public class DatabaseMetaDataTest {
|
||||
static Connection connection = null;
|
||||
static PreparedStatement statement = null;
|
||||
static String dbName = "test";
|
||||
|
@ -23,20 +23,21 @@ public class DatabaseMetaDataTest extends BaseTest {
|
|||
}
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_USER, "root");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_PASSWORD, "taosdata");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
|
||||
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
|
||||
|
||||
String sql = "drop database if exists " + dbName;
|
||||
statement = (TSDBPreparedStatement) connection.prepareStatement(sql);
|
||||
statement = connection.prepareStatement(sql);
|
||||
statement.executeUpdate("create database if not exists " + dbName);
|
||||
statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMetaDataTest() throws SQLException {
|
||||
|
||||
DatabaseMetaData databaseMetaData = connection.getMetaData();
|
||||
ResultSet resultSet = databaseMetaData.getTables(dbName, "t*", "t*", new String[]{"t"});
|
||||
while (resultSet.next()) {
|
||||
|
@ -180,7 +181,7 @@ public class DatabaseMetaDataTest extends BaseTest {
|
|||
databaseMetaData.getCatalogs();
|
||||
// databaseMetaData.getTableTypes();
|
||||
|
||||
databaseMetaData.getColumns("", "", "", "");
|
||||
databaseMetaData.getColumns(dbName, "", tName, "");
|
||||
databaseMetaData.getColumnPrivileges("", "", "", "");
|
||||
databaseMetaData.getTablePrivileges("", "", "");
|
||||
databaseMetaData.getBestRowIdentifier("", "", "", 0, false);
|
||||
|
|
|
@ -19,7 +19,7 @@ public class AppMemoryLeakTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
public void testCreateTooManyStatement() throws ClassNotFoundException, SQLException {
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
int stmtCnt = 0;
|
||||
|
@ -30,15 +30,4 @@ public class AppMemoryLeakTest {
|
|||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException, SQLException {
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
int stmtCnt = 0;
|
||||
Connection conn = DriverManager.getConnection("jdbc:TAOS://localhost:6030/?user=root&password=taosdata");
|
||||
while (true) {
|
||||
Statement stmt = conn.createStatement();
|
||||
System.out.println(++stmtCnt + " : " + stmt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package com.taosdata.jdbc.cases;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ConnectWrongDatabaseTest {
|
||||
|
||||
@Test
|
||||
public void connect() {
|
||||
try {
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
DriverManager.getConnection("jdbc:TAOS://localhost:6030/wrong_db?user=root&password=taosdata");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
Assert.assertEquals("TDengine Error: Invalid database name", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.taosdata.jdbc.rs;
|
||||
|
||||
|
||||
import org.junit.*;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
|
|
|
@ -0,0 +1,399 @@
|
|||
package com.taosdata.jdbc.rs;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class SQLTest {
|
||||
private static final String host = "master";
|
||||
|
||||
private static Connection connection;
|
||||
|
||||
@Test
|
||||
public void testCase001() {
|
||||
String sql = "create database if not exists restful_test";
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase002() {
|
||||
String sql = "use restful_test";
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase003() {
|
||||
String sql = "show databases";
|
||||
executeWithResult(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase004() {
|
||||
String sql = "show tables";
|
||||
executeWithResult(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase005() {
|
||||
String sql = "show stables";
|
||||
executeWithResult(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase006() {
|
||||
String sql = "show dnodes";
|
||||
executeWithResult(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase007() {
|
||||
String sql = "show vgroups";
|
||||
executeWithResult(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase008() {
|
||||
String sql = "drop table if exists restful_test.weather";
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase009() {
|
||||
String sql = "create table if not exists restful_test.weather(ts timestamp, temperature float) tags(location nchar(64))";
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase010() {
|
||||
String sql = "create table t1 using restful_test.weather tags('北京')";
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase011() {
|
||||
String sql = "insert into restful_test.t1 values(now, 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase012() {
|
||||
String sql = "insert into restful_test.t1 values('2020-01-01 00:00:00.000', 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase013() {
|
||||
String sql = "insert into restful_test.t1 values('2020-01-01 00:01:00.000', 22.22),('2020-01-01 00:02:00.000', 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase014() {
|
||||
String sql = "insert into restful_test.t2 using weather tags('上海') values('2020-01-01 00:03:00.000', 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase015() {
|
||||
String sql = "insert into restful_test.t2 using weather tags('上海') values('2020-01-01 00:01:00.000', 22.22),('2020-01-01 00:02:00.000', 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase016() {
|
||||
String sql = "insert into t1 values('2020-01-01 01:0:00.000', 22.22),('2020-01-01 02:00:00.000', 22.22) t2 values('2020-01-01 01:0:00.000', 33.33),('2020-01-01 02:00:00.000', 33.33)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase017() {
|
||||
String sql = "Insert into t3 using weather tags('广东') values('2020-01-01 01:0:00.000', 22.22),('2020-01-01 02:00:00.000', 22.22) t4 using weather tags('天津') values('2020-01-01 01:0:00.000', 33.33),('2020-01-01 02:00:00.000', 33.33)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase018() {
|
||||
String sql = "select * from restful_test.t1";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase019() {
|
||||
String sql = "select * from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase020() {
|
||||
String sql = "select ts, temperature from restful_test.t1";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase021() {
|
||||
String sql = "select ts, temperature from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase022() {
|
||||
String sql = "select temperature, ts from restful_test.t1";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase023() {
|
||||
String sql = "select temperature, ts from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase024() {
|
||||
String sql = "import into restful_test.t5 using weather tags('石家庄') values('2020-01-01 00:01:00.000', 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase025() {
|
||||
String sql = "import into restful_test.t6 using weather tags('沈阳') values('2020-01-01 00:01:00.000', 22.22),('2020-01-01 00:02:00.000', 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase026() {
|
||||
String sql = "import into restful_test.t7 using weather tags('长沙') values('2020-01-01 00:01:00.000', 22.22) restful_test.t8 using weather tags('吉林') values('2020-01-01 00:01:00.000', 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase027() {
|
||||
String sql = "import into restful_test.t9 using weather tags('武汉') values('2020-01-01 00:01:00.000', 22.22) ,('2020-01-02 00:01:00.000', 22.22) restful_test.t10 using weather tags('哈尔滨') values('2020-01-01 00:01:00.000', 22.22),('2020-01-02 00:01:00.000', 22.22)";
|
||||
executeUpdate(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase028() {
|
||||
String sql = "select location, temperature, ts from restful_test.weather where temperature > 1";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase029() {
|
||||
String sql = "select location, temperature, ts from restful_test.weather where temperature < 1";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase030() {
|
||||
String sql = "select location, temperature, ts from restful_test.weather where ts > now";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase031() {
|
||||
String sql = "select location, temperature, ts from restful_test.weather where ts < now";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase032() {
|
||||
String sql = "select count(*) from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase033() {
|
||||
String sql = "select first(*) from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase034() {
|
||||
String sql = "select last(*) from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase035() {
|
||||
String sql = "select last_row(*) from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase036() {
|
||||
String sql = "select ts, ts as primary_key from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCase037() {
|
||||
String sql = "select database()";
|
||||
execute("use restful_test");
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase038() {
|
||||
String sql = "select client_version()";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase039() {
|
||||
String sql = "select server_status()";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase040() {
|
||||
String sql = "select server_status() as status";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase041() {
|
||||
String sql = "select tbname, location from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase042() {
|
||||
String sql = "select count(tbname) from restful_test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase043() {
|
||||
String sql = "select * from restful_test.weather where ts < now - 1h";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase044() {
|
||||
String sql = "select * from restful_test.weather where ts < now - 1h and location like '%'";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase045() {
|
||||
String sql = "select * from restful_test.weather where ts < now - 1h order by ts";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase046() {
|
||||
String sql = "select last(*) from restful_test.weather where ts < now - 1h group by tbname order by tbname";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase047() {
|
||||
String sql = "select * from restful_test.weather limit 2";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase048() {
|
||||
String sql = "select * from restful_test.weather limit 2 offset 5";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase049() {
|
||||
String sql = "select * from restful_test.t1, restful_test.t3 where t1.ts = t3.ts ";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase050() {
|
||||
String sql = "select * from restful_test.t1, restful_test.t3 where t1.ts = t3.ts and t1.location = t3.location";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase051() {
|
||||
String sql = "select * from restful_test.t1 tt, restful_test.t3 yy where tt.ts = yy.ts";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void executeUpdate(String sql) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
long start = System.currentTimeMillis();
|
||||
int affectedRows = statement.executeUpdate(sql);
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("[ affected rows : " + affectedRows + " ] time cost: " + (end - start) + " ms, execute statement ====> " + sql);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void executeWithResult(String sql) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute(sql);
|
||||
ResultSet resultSet = statement.getResultSet();
|
||||
printResult(resultSet);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void execute(String sql) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
long start = System.currentTimeMillis();
|
||||
boolean execute = statement.execute(sql);
|
||||
long end = System.currentTimeMillis();
|
||||
printSql(sql, execute, (end - start));
|
||||
} catch (SQLException e) {
|
||||
System.out.println("ERROR execute SQL ===> " + sql);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printSql(String sql, boolean succeed, long cost) {
|
||||
System.out.println("[ " + (succeed ? "OK" : "ERROR!") + " ] time cost: " + cost + " ms, execute statement ====> " + sql);
|
||||
}
|
||||
|
||||
private void executeQuery(String sql) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
long start = System.currentTimeMillis();
|
||||
ResultSet resultSet = statement.executeQuery(sql);
|
||||
long end = System.currentTimeMillis();
|
||||
printSql(sql, true, (end - start));
|
||||
printResult(resultSet);
|
||||
} catch (SQLException e) {
|
||||
System.out.println("ERROR execute SQL ===> " + sql);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printResult(ResultSet resultSet) throws SQLException {
|
||||
ResultSetMetaData metaData = resultSet.getMetaData();
|
||||
while (resultSet.next()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
String columnLabel = metaData.getColumnLabel(i);
|
||||
String value = resultSet.getString(i);
|
||||
sb.append(columnLabel + ": " + value + "\t");
|
||||
}
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void before() throws ClassNotFoundException, SQLException {
|
||||
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
|
||||
connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/restful_test?user=root&password=taosdata");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void after() throws SQLException {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -5,10 +5,6 @@ import org.junit.Test;
|
|||
|
||||
public class SqlSyntaxValidatorTest {
|
||||
|
||||
@Test
|
||||
public void validateSqlSyntax() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSelectSQL() {
|
||||
Assert.assertTrue(SqlSyntaxValidator.isSelectSql("select * from test.weather"));
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
*/
|
||||
|
||||
const ref = require('ref');
|
||||
const os = require('os');
|
||||
const ffi = require('ffi');
|
||||
const ArrayType = require('ref-array');
|
||||
const Struct = require('ref-struct');
|
||||
|
@ -188,7 +189,13 @@ function CTaosInterface (config = null, pass = false) {
|
|||
ref.types.void_ptr2 = ref.refType(ref.types.void_ptr);
|
||||
/*Declare a bunch of functions first*/
|
||||
/* Note, pointers to TAOS_RES, TAOS, are ref.types.void_ptr. The connection._conn buffer is supplied for pointers to TAOS * */
|
||||
this.libtaos = ffi.Library('libtaos', {
|
||||
|
||||
if ('win32' == os.platform()) {
|
||||
taoslibname = 'taos';
|
||||
} else {
|
||||
taoslibname = 'libtaos';
|
||||
}
|
||||
this.libtaos = ffi.Library(taoslibname, {
|
||||
'taos_options': [ ref.types.int, [ ref.types.int , ref.types.void_ptr ] ],
|
||||
'taos_init': [ ref.types.void, [ ] ],
|
||||
//TAOS *taos_connect(char *ip, char *user, char *pass, char *db, int port)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "td2.0-connector",
|
||||
"version": "2.0.4",
|
||||
"version": "2.0.5",
|
||||
"description": "A Node.js connector for TDengine.",
|
||||
"main": "tdengine.js",
|
||||
"scripts": {
|
||||
|
|
|
@ -23,8 +23,7 @@ extern "C" {
|
|||
|
||||
int32_t dnodeInitModules();
|
||||
void dnodeCleanupModules();
|
||||
bool dnodeStartMnode(SMInfos *pMinfos);
|
||||
void dnodeProcessModuleStatus(uint32_t moduleStatus);
|
||||
int32_t dnodeStartMnode(SMInfos *pMinfos);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -184,7 +184,19 @@ void dnodeReprocessMWriteMsg(void *pMsg) {
|
|||
dDebug("msg:%p, app:%p type:%s is redirected for mnode not running, retry times:%d", pWrite, pWrite->rpcMsg.ahandle,
|
||||
taosMsg[pWrite->rpcMsg.msgType], pWrite->retry);
|
||||
|
||||
dnodeSendRedirectMsg(pMsg, true);
|
||||
if (pWrite->pBatchMasterMsg) {
|
||||
++pWrite->pBatchMasterMsg->received;
|
||||
if (pWrite->pBatchMasterMsg->successed + pWrite->pBatchMasterMsg->received
|
||||
>= pWrite->pBatchMasterMsg->expected) {
|
||||
dnodeSendRedirectMsg(&pWrite->pBatchMasterMsg->rpcMsg, true);
|
||||
dnodeFreeMWriteMsg(pWrite->pBatchMasterMsg);
|
||||
}
|
||||
|
||||
mnodeDestroySubMsg(pWrite);
|
||||
|
||||
return;
|
||||
}
|
||||
dnodeSendRedirectMsg(&pWrite->rpcMsg, true);
|
||||
dnodeFreeMWriteMsg(pWrite);
|
||||
} else {
|
||||
dDebug("msg:%p, app:%p type:%s is reput into mwrite queue:%p, retry times:%d", pWrite, pWrite->rpcMsg.ahandle,
|
||||
|
|
|
@ -127,14 +127,16 @@ int32_t dnodeInitModules() {
|
|||
return dnodeStartModules();
|
||||
}
|
||||
|
||||
void dnodeProcessModuleStatus(uint32_t moduleStatus) {
|
||||
int32_t dnodeProcessModuleStatus(uint32_t moduleStatus) {
|
||||
int32_t code = 0;
|
||||
|
||||
for (int32_t module = TSDB_MOD_MNODE; module < TSDB_MOD_HTTP; ++module) {
|
||||
bool enableModule = moduleStatus & (1 << module);
|
||||
if (!tsModule[module].enable && enableModule) {
|
||||
dInfo("module status:%u is set, start %s module", moduleStatus, tsModule[module].name);
|
||||
tsModule[module].enable = true;
|
||||
dnodeSetModuleStatus(module);
|
||||
(*tsModule[module].startFp)();
|
||||
code = (*tsModule[module].startFp)();
|
||||
}
|
||||
|
||||
if (tsModule[module].enable && !enableModule) {
|
||||
|
@ -144,21 +146,29 @@ void dnodeProcessModuleStatus(uint32_t moduleStatus) {
|
|||
(*tsModule[module].stopFp)();
|
||||
}
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
bool dnodeStartMnode(SMInfos *pMinfos) {
|
||||
SMInfos *pMnodes = pMinfos;
|
||||
|
||||
int32_t dnodeStartMnode(SMInfos *pMinfos) {
|
||||
if (tsModuleStatus & (1 << TSDB_MOD_MNODE)) {
|
||||
dDebug("mnode module is already started, module status:%d", tsModuleStatus);
|
||||
return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t moduleStatus = tsModuleStatus | (1 << TSDB_MOD_MNODE);
|
||||
dInfo("start mnode module, module status:%d, new status:%d", tsModuleStatus, moduleStatus);
|
||||
dnodeProcessModuleStatus(moduleStatus);
|
||||
|
||||
sdbUpdateSync(pMnodes);
|
||||
int32_t code = dnodeProcessModuleStatus(moduleStatus);
|
||||
if (code == 0) {
|
||||
code = sdbUpdateSync(pMinfos);
|
||||
}
|
||||
|
||||
return true;
|
||||
if (code != 0) {
|
||||
dError("failed to start mnode module since %s", tstrerror(code));
|
||||
moduleStatus = tsModuleStatus & ~(1 << TSDB_MOD_MNODE);
|
||||
dnodeProcessModuleStatus(moduleStatus);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ int32_t dnodeInitServer() {
|
|||
rpcInit.label = "DND-S";
|
||||
rpcInit.numOfThreads = 1;
|
||||
rpcInit.cfp = dnodeProcessReqMsgFromDnode;
|
||||
rpcInit.sessions = TSDB_MAX_VNODES;
|
||||
rpcInit.sessions = TSDB_MAX_VNODES << 4;
|
||||
rpcInit.connType = TAOS_CONN_SERVER;
|
||||
rpcInit.idleTime = tsShellActivityTimer * 1000;
|
||||
|
||||
|
@ -123,7 +123,7 @@ int32_t dnodeInitClient() {
|
|||
rpcInit.label = "DND-C";
|
||||
rpcInit.numOfThreads = 1;
|
||||
rpcInit.cfp = dnodeProcessRspFromDnode;
|
||||
rpcInit.sessions = TSDB_MAX_VNODES;
|
||||
rpcInit.sessions = TSDB_MAX_VNODES << 4;
|
||||
rpcInit.connType = TAOS_CONN_CLIENT;
|
||||
rpcInit.idleTime = tsShellActivityTimer * 1000;
|
||||
rpcInit.user = "t";
|
||||
|
|
|
@ -214,7 +214,5 @@ static int32_t dnodeProcessCreateMnodeMsg(SRpcMsg *pMsg) {
|
|||
dDebug("mnode index:%d, mnode:%d:%s", i, pCfg->mnodes.mnodeInfos[i].mnodeId, pCfg->mnodes.mnodeInfos[i].mnodeEp);
|
||||
}
|
||||
|
||||
dnodeStartMnode(&pCfg->mnodes);
|
||||
|
||||
return TSDB_CODE_SUCCESS;
|
||||
return dnodeStartMnode(&pCfg->mnodes);
|
||||
}
|
||||
|
|
|
@ -73,7 +73,8 @@ static int32_t dnodeGetVnodeList(int32_t vnodeList[], int32_t *numOfVnodes) {
|
|||
|
||||
if (*numOfVnodes >= TSDB_MAX_VNODES) {
|
||||
dError("vgId:%d, too many vnode directory in disk, exist:%d max:%d", vnode, *numOfVnodes, TSDB_MAX_VNODES);
|
||||
continue;
|
||||
closedir(dir);
|
||||
return TSDB_CODE_DND_TOO_MANY_VNODES;
|
||||
} else {
|
||||
vnodeList[*numOfVnodes - 1] = vnode;
|
||||
}
|
||||
|
@ -288,4 +289,4 @@ void dnodeSendStatusMsgToMnode() {
|
|||
dInfo("force send status msg to mnode");
|
||||
taosTmrReset(dnodeSendStatusMsg, 3, NULL, tsDnodeTmr, &tsStatusTimer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ void dnodeGetClusterId(char *clusterId);
|
|||
|
||||
void dnodeUpdateEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port);
|
||||
bool dnodeCheckEpChanged(int32_t dnodeId, char *epstr);
|
||||
bool dnodeStartMnode(SMInfos *pMinfos);
|
||||
int32_t dnodeStartMnode(SMInfos *pMinfos);
|
||||
|
||||
void dnodeAddClientRspHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg));
|
||||
void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg);
|
||||
|
|
|
@ -42,11 +42,12 @@ typedef struct SMnodeMsg {
|
|||
struct SVgObj * pVgroup;
|
||||
struct STableObj *pTable;
|
||||
struct SSTableObj*pSTable;
|
||||
struct SMnodeMsg *pBatchMasterMsg;
|
||||
SMnodeRsp rpcRsp;
|
||||
int8_t received;
|
||||
int8_t successed;
|
||||
int8_t expected;
|
||||
int8_t retry;
|
||||
int16_t received;
|
||||
int16_t successed;
|
||||
int16_t expected;
|
||||
int16_t retry;
|
||||
int32_t incomingTs;
|
||||
int32_t code;
|
||||
void * pObj;
|
||||
|
@ -57,13 +58,14 @@ typedef struct SMnodeMsg {
|
|||
void * mnodeCreateMsg(SRpcMsg *pRpcMsg);
|
||||
int32_t mnodeInitMsg(SMnodeMsg *pMsg);
|
||||
void mnodeCleanupMsg(SMnodeMsg *pMsg);
|
||||
void mnodeDestroySubMsg(SMnodeMsg *pSubMsg);
|
||||
|
||||
int32_t mnodeInitSystem();
|
||||
int32_t mnodeStartSystem();
|
||||
void mnodeCleanupSystem();
|
||||
void mnodeStopSystem();
|
||||
void sdbUpdateAsync();
|
||||
void sdbUpdateSync(void *pMnodes);
|
||||
int32_t sdbUpdateSync(void *pMnodes);
|
||||
bool mnodeIsRunning();
|
||||
int32_t mnodeProcessRead(SMnodeMsg *pMsg);
|
||||
int32_t mnodeProcessWrite(SMnodeMsg *pMsg);
|
||||
|
|
|
@ -86,6 +86,7 @@ void qDestroyQueryInfo(qinfo_t qHandle);
|
|||
|
||||
void* qOpenQueryMgmt(int32_t vgId);
|
||||
void qQueryMgmtNotifyClosed(void* pExecutor);
|
||||
void qQueryMgmtReOpen(void *pExecutor);
|
||||
void qCleanupQueryMgmt(void* pExecutor);
|
||||
void** qRegisterQInfo(void* pMgmt, uint64_t qInfo);
|
||||
void** qAcquireQInfo(void* pMgmt, uint64_t key);
|
||||
|
|
|
@ -126,6 +126,11 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_SHOWOBJ, 0, 0x030B, "Data expir
|
|||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_QUERY_ID, 0, 0x030C, "Invalid query id")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_STREAM_ID, 0, 0x030D, "Invalid stream id")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_CONN_ID, 0, 0x030E, "Invalid connection id")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_MNODE_IS_RUNNING, 0, 0x0310, "mnode is alreay running")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_CONFIG_SYNC, 0, 0x0311, "failed to config sync")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_START_SYNC, 0, 0x0312, "failed to start sync")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_CREATE_DIR, 0, 0x0313, "failed to create mnode dir")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_INIT_STEP, 0, 0x0314, "failed to init components")
|
||||
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_SDB_OBJ_ALREADY_THERE, 0, 0x0320, "Object already there")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_MND_SDB_ERROR, 0, 0x0321, "Unexpected generic error in sdb")
|
||||
|
@ -194,6 +199,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_DND_OUT_OF_MEMORY, 0, 0x0401, "Dnode out
|
|||
TAOS_DEFINE_ERROR(TSDB_CODE_DND_NO_WRITE_ACCESS, 0, 0x0402, "No permission for disk files in dnode")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_DND_INVALID_MSG_LEN, 0, 0x0403, "Invalid message length")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_DND_ACTION_IN_PROGRESS, 0, 0x0404, "Action in progress")
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_DND_TOO_MANY_VNODES, 0, 0x0405, "Too many vnode directories")
|
||||
|
||||
// vnode
|
||||
TAOS_DEFINE_ERROR(TSDB_CODE_VND_ACTION_IN_PROGRESS, 0, 0x0500, "Action in progress")
|
||||
|
|
|
@ -6,12 +6,12 @@
|
|||
"user": "root",
|
||||
"password": "taosdata",
|
||||
"databases": "db01",
|
||||
"super_table_query":
|
||||
{"rate":1, "concurrent":1,
|
||||
"specified_table_query":
|
||||
{"query_interval":1, "concurrent":1,
|
||||
"sqls": [{"sql": "select count(*) from stb01", "result": "./query_res0.txt"}]
|
||||
},
|
||||
"sub_table_query":
|
||||
{"stblname": "stb01", "rate":1, "threads":1,
|
||||
"sqls": [{"sql": "select count(*) from xxxx", "result": "./query_res1.txt"}]
|
||||
"super_table_query":
|
||||
{"stblname": "stb01", "query_interval":1, "threads":1,
|
||||
"sqls": [{"sql": "select count(*) from xxxx", "result": "./query_res1.txt"}]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1441,11 +1441,12 @@ static int getAllChildNameOfSuperTable(TAOS * taos, char* dbName, char* sTblName
|
|||
strncpy(pTblName, (char *)row[0], TSDB_TABLE_NAME_LEN);
|
||||
//printf("==== sub table name: %s\n", pTblName);
|
||||
count++;
|
||||
if (count == childTblCount) {
|
||||
char *tmp = realloc(childTblName, (size_t)count*1.5*TSDB_TABLE_NAME_LEN);
|
||||
if (count >= childTblCount - 1) {
|
||||
char *tmp = realloc(childTblName, (size_t)childTblCount*1.5*TSDB_TABLE_NAME_LEN+1);
|
||||
if (tmp != NULL) {
|
||||
childTblName = tmp;
|
||||
memset(childTblName + count*TSDB_TABLE_NAME_LEN, 0, (size_t)(count*0.5*TSDB_TABLE_NAME_LEN));
|
||||
childTblCount = (int)(childTblCount*1.5);
|
||||
memset(childTblName + count*TSDB_TABLE_NAME_LEN, 0, (size_t)((childTblCount-count)*TSDB_TABLE_NAME_LEN));
|
||||
} else {
|
||||
// exit, if allocate more memory failed
|
||||
printf("realloc fail for save child table name of %s.%s\n", dbName, sTblName);
|
||||
|
@ -3960,7 +3961,11 @@ void *superQueryProcess(void *sarg) {
|
|||
for (int i = 0; i < g_queryInfo.superQueryInfo.sqlCount; i++) {
|
||||
if (0 == strncasecmp(g_queryInfo.queryMode, "taosc", 5)) {
|
||||
int64_t t1 = taosGetTimestampUs();
|
||||
selectAndGetResult(winfo->taos, g_queryInfo.superQueryInfo.sql[i], g_queryInfo.superQueryInfo.result[i]);
|
||||
char tmpFile[MAX_FILE_NAME_LEN*2] = {0};
|
||||
if (g_queryInfo.superQueryInfo.result[i][0] != 0) {
|
||||
sprintf(tmpFile, "%s-%d", g_queryInfo.superQueryInfo.result[i], winfo->threadID);
|
||||
}
|
||||
selectAndGetResult(winfo->taos, g_queryInfo.superQueryInfo.sql[i], tmpFile);
|
||||
int64_t t2 = taosGetTimestampUs();
|
||||
printf("taosc select sql return, Spent %f s\n", (t2 - t1)/1000000.0);
|
||||
} else {
|
||||
|
@ -4019,7 +4024,11 @@ void *subQueryProcess(void *sarg) {
|
|||
for (int i = 0; i < g_queryInfo.subQueryInfo.sqlCount; i++) {
|
||||
memset(sqlstr,0,sizeof(sqlstr));
|
||||
replaceSubTblName(g_queryInfo.subQueryInfo.sql[i], sqlstr, i);
|
||||
selectAndGetResult(winfo->taos, sqlstr, g_queryInfo.subQueryInfo.result[i]);
|
||||
char tmpFile[MAX_FILE_NAME_LEN*2] = {0};
|
||||
if (g_queryInfo.subQueryInfo.result[i][0] != 0) {
|
||||
sprintf(tmpFile, "%s-%d", g_queryInfo.subQueryInfo.result[i], winfo->threadID);
|
||||
}
|
||||
selectAndGetResult(winfo->taos, sqlstr, tmpFile);
|
||||
}
|
||||
}
|
||||
et = taosGetTimestampMs();
|
||||
|
@ -4193,7 +4202,11 @@ void *subSubscribeProcess(void *sarg) {
|
|||
sprintf(topic, "taosdemo-subscribe-%d", i);
|
||||
memset(subSqlstr,0,sizeof(subSqlstr));
|
||||
replaceSubTblName(g_queryInfo.subQueryInfo.sql[i], subSqlstr, i);
|
||||
g_queryInfo.subQueryInfo.tsub[i] = subscribeImpl(winfo->taos, subSqlstr, topic, g_queryInfo.subQueryInfo.result[i]);
|
||||
char tmpFile[MAX_FILE_NAME_LEN*2] = {0};
|
||||
if (g_queryInfo.subQueryInfo.result[i][0] != 0) {
|
||||
sprintf(tmpFile, "%s-%d", g_queryInfo.subQueryInfo.result[i], winfo->threadID);
|
||||
}
|
||||
g_queryInfo.subQueryInfo.tsub[i] = subscribeImpl(winfo->taos, subSqlstr, topic, tmpFile);
|
||||
if (NULL == g_queryInfo.subQueryInfo.tsub[i]) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -4211,7 +4224,11 @@ void *subSubscribeProcess(void *sarg) {
|
|||
|
||||
TAOS_RES* res = taos_consume(g_queryInfo.subQueryInfo.tsub[i]);
|
||||
if (res) {
|
||||
getResult(res, g_queryInfo.subQueryInfo.result[i]);
|
||||
char tmpFile[MAX_FILE_NAME_LEN*2] = {0};
|
||||
if (g_queryInfo.subQueryInfo.result[i][0] != 0) {
|
||||
sprintf(tmpFile, "%s-%d", g_queryInfo.subQueryInfo.result[i], winfo->threadID);
|
||||
}
|
||||
getResult(res, tmpFile);
|
||||
taos_free_result(res);
|
||||
}
|
||||
}
|
||||
|
@ -4244,7 +4261,11 @@ void *superSubscribeProcess(void *sarg) {
|
|||
char topic[32] = {0};
|
||||
for (int i = 0; i < g_queryInfo.superQueryInfo.sqlCount; i++) {
|
||||
sprintf(topic, "taosdemo-subscribe-%d", i);
|
||||
g_queryInfo.superQueryInfo.tsub[i] = subscribeImpl(winfo->taos, g_queryInfo.superQueryInfo.sql[i], topic, g_queryInfo.superQueryInfo.result[i]);
|
||||
char tmpFile[MAX_FILE_NAME_LEN*2] = {0};
|
||||
if (g_queryInfo.subQueryInfo.result[i][0] != 0) {
|
||||
sprintf(tmpFile, "%s-%d", g_queryInfo.superQueryInfo.result[i], winfo->threadID);
|
||||
}
|
||||
g_queryInfo.superQueryInfo.tsub[i] = subscribeImpl(winfo->taos, g_queryInfo.superQueryInfo.sql[i], topic, tmpFile);
|
||||
if (NULL == g_queryInfo.superQueryInfo.tsub[i]) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -4262,7 +4283,11 @@ void *superSubscribeProcess(void *sarg) {
|
|||
|
||||
TAOS_RES* res = taos_consume(g_queryInfo.superQueryInfo.tsub[i]);
|
||||
if (res) {
|
||||
getResult(res, g_queryInfo.superQueryInfo.result[i]);
|
||||
char tmpFile[MAX_FILE_NAME_LEN*2] = {0};
|
||||
if (g_queryInfo.superQueryInfo.result[i][0] != 0) {
|
||||
sprintf(tmpFile, "%s-%d", g_queryInfo.superQueryInfo.result[i], winfo->threadID);
|
||||
}
|
||||
getResult(res, tmpFile);
|
||||
taos_free_result(res);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -311,6 +311,11 @@ static int32_t mnodeCheckDbCfg(SDbCfg *pCfg) {
|
|||
return TSDB_CODE_MND_INVALID_DB_OPTION;
|
||||
}
|
||||
|
||||
if (pCfg->replications > mnodeGetDnodesNum()) {
|
||||
mError("no enough dnode to config replica: %d, #dnodes: %d", pCfg->replications, mnodeGetDnodesNum());
|
||||
return TSDB_CODE_MND_INVALID_DB_OPTION;
|
||||
}
|
||||
|
||||
if (pCfg->quorum < TSDB_MIN_DB_REPLICA_OPTION || pCfg->quorum > TSDB_MAX_DB_REPLICA_OPTION) {
|
||||
mError("invalid db option quorum:%d valid range: [%d, %d]", pCfg->quorum, TSDB_MIN_DB_REPLICA_OPTION,
|
||||
TSDB_MAX_DB_REPLICA_OPTION);
|
||||
|
|
|
@ -74,13 +74,13 @@ static int32_t mnodeInitComponents() {
|
|||
int32_t mnodeStartSystem() {
|
||||
if (tsMgmtIsRunning) {
|
||||
mInfo("mnode module already started...");
|
||||
return 0;
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
mInfo("starting to initialize mnode ...");
|
||||
if (mkdir(tsMnodeDir, 0755) != 0 && errno != EEXIST) {
|
||||
mError("failed to init mnode dir:%s, reason:%s", tsMnodeDir, strerror(errno));
|
||||
return -1;
|
||||
return TSDB_CODE_MND_FAILED_TO_CREATE_DIR;
|
||||
}
|
||||
|
||||
dnodeAllocMWritequeue();
|
||||
|
@ -88,7 +88,7 @@ int32_t mnodeStartSystem() {
|
|||
dnodeAllocateMPeerQueue();
|
||||
|
||||
if (mnodeInitComponents() != 0) {
|
||||
return -1;
|
||||
return TSDB_CODE_MND_FAILED_TO_INIT_STEP;
|
||||
}
|
||||
|
||||
dnodeReportStep("mnode-grant", "start to set grant infomation", 0);
|
||||
|
@ -99,7 +99,7 @@ int32_t mnodeStartSystem() {
|
|||
|
||||
sdbUpdateSync(NULL);
|
||||
|
||||
return 0;
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t mnodeInitSystem() {
|
||||
|
|
|
@ -318,11 +318,11 @@ void sdbUpdateAsync() {
|
|||
taosTmrReset(sdbUpdateSyncTmrFp, 200, NULL, tsMnodeTmr, &tsSdbTmr);
|
||||
}
|
||||
|
||||
void sdbUpdateSync(void *pMnodes) {
|
||||
int32_t sdbUpdateSync(void *pMnodes) {
|
||||
SMInfos *pMinfos = pMnodes;
|
||||
if (!mnodeIsRunning()) {
|
||||
mDebug("vgId:1, mnode not start yet, update sync config later");
|
||||
return;
|
||||
return TSDB_CODE_MND_MNODE_IS_RUNNING;
|
||||
}
|
||||
|
||||
mDebug("vgId:1, update sync config, pMnodes:%p", pMnodes);
|
||||
|
@ -377,12 +377,12 @@ void sdbUpdateSync(void *pMnodes) {
|
|||
|
||||
if (!hasThisDnode) {
|
||||
sdbDebug("vgId:1, update sync config, this dnode not exist");
|
||||
return;
|
||||
return TSDB_CODE_MND_FAILED_TO_CONFIG_SYNC;
|
||||
}
|
||||
|
||||
if (memcmp(&syncCfg, &tsSdbMgmt.cfg, sizeof(SSyncCfg)) == 0) {
|
||||
sdbDebug("vgId:1, update sync config, info not changed");
|
||||
return;
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
sdbInfo("vgId:1, work as mnode, replica:%d", syncCfg.replica);
|
||||
|
@ -407,12 +407,15 @@ void sdbUpdateSync(void *pMnodes) {
|
|||
tsSdbMgmt.cfg = syncCfg;
|
||||
|
||||
if (tsSdbMgmt.sync) {
|
||||
syncReconfig(tsSdbMgmt.sync, &syncCfg);
|
||||
int32_t code = syncReconfig(tsSdbMgmt.sync, &syncCfg);
|
||||
if (code != 0) return code;
|
||||
} else {
|
||||
tsSdbMgmt.sync = syncStart(&syncInfo);
|
||||
if (tsSdbMgmt.sync <= 0) return TSDB_CODE_MND_FAILED_TO_START_SYNC;
|
||||
}
|
||||
|
||||
sdbUpdateMnodeRoles();
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t sdbInitRef() {
|
||||
|
@ -1051,7 +1054,10 @@ static int32_t sdbWriteFwdToQueue(int32_t vgId, void *wparam, int32_t qtype, voi
|
|||
memcpy(pRow->pHead, pHead, sizeof(SWalHead) + pHead->len);
|
||||
pRow->rowData = pRow->pHead->cont;
|
||||
|
||||
return sdbWriteToQueue(pRow, qtype);
|
||||
int32_t code = sdbWriteToQueue(pRow, qtype);
|
||||
if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) code = 0;
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
static int32_t sdbWriteRowToQueue(SSdbRow *pInputRow, int32_t action) {
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include "tcompare.h"
|
||||
#include "tdataformat.h"
|
||||
#include "tgrant.h"
|
||||
#include "tqueue.h"
|
||||
#include "hash.h"
|
||||
#include "mnode.h"
|
||||
#include "dnode.h"
|
||||
|
@ -720,6 +721,133 @@ static void mnodeExtractTableName(char* tableId, char* name) {
|
|||
}
|
||||
}
|
||||
|
||||
static SMnodeMsg *mnodeCreateSubMsg(SMnodeMsg *pBatchMasterMsg, int32_t contSize) {
|
||||
SMnodeMsg *pSubMsg = taosAllocateQitem(sizeof(*pBatchMasterMsg) + contSize);
|
||||
*pSubMsg = *pBatchMasterMsg;
|
||||
|
||||
//pSubMsg->pCont = (char *) pSubMsg + sizeof(SMnodeMsg);
|
||||
pSubMsg->rpcMsg.pCont = pSubMsg->pCont;
|
||||
pSubMsg->successed = 0;
|
||||
pSubMsg->expected = 0;
|
||||
SCMCreateTableMsg *pCM = pSubMsg->rpcMsg.pCont;
|
||||
pCM->numOfTables = htonl(1);
|
||||
pCM->contLen = htonl(contSize);
|
||||
|
||||
return pSubMsg;
|
||||
}
|
||||
|
||||
void mnodeDestroySubMsg(SMnodeMsg *pSubMsg) {
|
||||
if (pSubMsg) {
|
||||
// pUser is retained in batch master msg
|
||||
if (pSubMsg->pDb) mnodeDecDbRef(pSubMsg->pDb);
|
||||
if (pSubMsg->pVgroup) mnodeDecVgroupRef(pSubMsg->pVgroup);
|
||||
if (pSubMsg->pTable) mnodeDecTableRef(pSubMsg->pTable);
|
||||
if (pSubMsg->pSTable) mnodeDecTableRef(pSubMsg->pSTable);
|
||||
if (pSubMsg->pAcct) mnodeDecAcctRef(pSubMsg->pAcct);
|
||||
if (pSubMsg->pDnode) mnodeDecDnodeRef(pSubMsg->pDnode);
|
||||
|
||||
taosFreeQitem(pSubMsg);
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t mnodeValidateCreateTableMsg(SCreateTableMsg *pCreateTable, SMnodeMsg *pMsg) {
|
||||
if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(pCreateTable->db);
|
||||
if (pMsg->pDb == NULL) {
|
||||
mError("msg:%p, app:%p table:%s, failed to create, db not selected", pMsg, pMsg->rpcMsg.ahandle, pCreateTable->tableId);
|
||||
return TSDB_CODE_MND_DB_NOT_SELECTED;
|
||||
}
|
||||
|
||||
if (pMsg->pDb->status != TSDB_DB_STATUS_READY) {
|
||||
mError("db:%s, status:%d, in dropping", pMsg->pDb->name, pMsg->pDb->status);
|
||||
return TSDB_CODE_MND_DB_IN_DROPPING;
|
||||
}
|
||||
|
||||
if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreateTable->tableId);
|
||||
if (pMsg->pTable != NULL && pMsg->retry == 0) {
|
||||
if (pCreateTable->getMeta) {
|
||||
mDebug("msg:%p, app:%p table:%s, continue to get meta", pMsg, pMsg->rpcMsg.ahandle, pCreateTable->tableId);
|
||||
return mnodeGetChildTableMeta(pMsg);
|
||||
} else if (pCreateTable->igExists) {
|
||||
mDebug("msg:%p, app:%p table:%s, is already exist", pMsg, pMsg->rpcMsg.ahandle, pCreateTable->tableId);
|
||||
return TSDB_CODE_SUCCESS;
|
||||
} else {
|
||||
mError("msg:%p, app:%p table:%s, failed to create, table already exist", pMsg, pMsg->rpcMsg.ahandle,
|
||||
pCreateTable->tableId);
|
||||
return TSDB_CODE_MND_TABLE_ALREADY_EXIST;
|
||||
}
|
||||
}
|
||||
|
||||
if (pCreateTable->numOfTags != 0) {
|
||||
mDebug("msg:%p, app:%p table:%s, create stable msg is received from thandle:%p", pMsg, pMsg->rpcMsg.ahandle,
|
||||
pCreateTable->tableId, pMsg->rpcMsg.handle);
|
||||
return mnodeProcessCreateSuperTableMsg(pMsg);
|
||||
} else {
|
||||
mDebug("msg:%p, app:%p table:%s, create ctable msg is received from thandle:%p", pMsg, pMsg->rpcMsg.ahandle,
|
||||
pCreateTable->tableId, pMsg->rpcMsg.handle);
|
||||
return mnodeProcessCreateChildTableMsg(pMsg);
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t mnodeProcessBatchCreateTableMsg(SMnodeMsg *pMsg) {
|
||||
if (pMsg->pBatchMasterMsg == NULL) { // batch master first round
|
||||
pMsg->pBatchMasterMsg = pMsg;
|
||||
|
||||
SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
|
||||
int32_t numOfTables = htonl(pCreate->numOfTables);
|
||||
int32_t contentLen = htonl(pCreate->contLen);
|
||||
pMsg->expected = numOfTables;
|
||||
|
||||
int32_t code = TSDB_CODE_SUCCESS;
|
||||
SCreateTableMsg *pCreateTable = (SCreateTableMsg*) ((char*) pCreate + sizeof(SCMCreateTableMsg));
|
||||
for (SCreateTableMsg *p = pCreateTable; p < (SCreateTableMsg *) ((char *) pCreate + contentLen); p = (SCreateTableMsg *) ((char *) p + htonl(p->len))) {
|
||||
SMnodeMsg *pSubMsg = mnodeCreateSubMsg(pMsg, sizeof(SCMCreateTableMsg) + htonl(p->len));
|
||||
memcpy(pSubMsg->pCont + sizeof(SCMCreateTableMsg), p, htonl(p->len));
|
||||
code = mnodeValidateCreateTableMsg(p, pSubMsg);
|
||||
|
||||
if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_MND_TABLE_ALREADY_EXIST) {
|
||||
++pSubMsg->pBatchMasterMsg->successed;
|
||||
mnodeDestroySubMsg(pSubMsg);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
|
||||
mnodeDestroySubMsg(pSubMsg);
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
if (pMsg->successed >= pMsg->expected) {
|
||||
return code;
|
||||
} else {
|
||||
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
|
||||
}
|
||||
} else {
|
||||
if (pMsg->pBatchMasterMsg != pMsg) { // batch sub replay
|
||||
SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
|
||||
SCreateTableMsg *pCreateTable = (SCreateTableMsg*) ((char*) pCreate + sizeof(SCMCreateTableMsg));
|
||||
int32_t code = mnodeValidateCreateTableMsg(pCreateTable, pMsg);
|
||||
if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_MND_TABLE_ALREADY_EXIST) {
|
||||
++pMsg->pBatchMasterMsg->successed;
|
||||
mnodeDestroySubMsg(pMsg);
|
||||
} else if (code == TSDB_CODE_MND_ACTION_NEED_REPROCESSED) {
|
||||
return code;
|
||||
} else if (code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
|
||||
++pMsg->pBatchMasterMsg->received;
|
||||
mnodeDestroySubMsg(pMsg);
|
||||
}
|
||||
|
||||
if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
|
||||
>= pMsg->pBatchMasterMsg->expected) {
|
||||
dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, TSDB_CODE_SUCCESS);
|
||||
}
|
||||
|
||||
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
|
||||
} else { // batch master replay, reprocess the whole batch
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg) {
|
||||
SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont;
|
||||
|
||||
|
@ -729,6 +857,11 @@ static int32_t mnodeProcessCreateTableMsg(SMnodeMsg *pMsg) {
|
|||
// todo return error
|
||||
}
|
||||
|
||||
// batch master msg first round or reprocessing and batch sub msg reprocessing
|
||||
if (numOfTables > 1 || pMsg->pBatchMasterMsg != NULL) {
|
||||
return mnodeProcessBatchCreateTableMsg(pMsg);
|
||||
}
|
||||
|
||||
SCreateTableMsg *p = (SCreateTableMsg*)((char*) pCreate + sizeof(SCMCreateTableMsg));
|
||||
if (pMsg->pDb == NULL) pMsg->pDb = mnodeGetDb(p->db);
|
||||
if (pMsg->pDb == NULL) {
|
||||
|
@ -1737,6 +1870,18 @@ static int32_t mnodeDoCreateChildTableCb(SMnodeMsg *pMsg, int32_t code) {
|
|||
mDebug("msg:%p, app:%p table:%s, created in dnode, thandle:%p", pMsg, pMsg->rpcMsg.ahandle, pTable->info.tableId,
|
||||
pMsg->rpcMsg.handle);
|
||||
|
||||
if (pMsg->pBatchMasterMsg) {
|
||||
++pMsg->pBatchMasterMsg->successed;
|
||||
if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
|
||||
>= pMsg->pBatchMasterMsg->expected) {
|
||||
dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, code);
|
||||
}
|
||||
|
||||
mnodeDestroySubMsg(pMsg);
|
||||
|
||||
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
|
||||
}
|
||||
|
||||
dnodeSendRpcMWriteRsp(pMsg, TSDB_CODE_SUCCESS);
|
||||
}
|
||||
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
|
||||
|
@ -2478,6 +2623,19 @@ static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
|
|||
|
||||
mnodeSendDropChildTableMsg(pMsg, false);
|
||||
rpcMsg->code = TSDB_CODE_SUCCESS;
|
||||
|
||||
if (pMsg->pBatchMasterMsg) {
|
||||
++pMsg->pBatchMasterMsg->successed;
|
||||
if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
|
||||
>= pMsg->pBatchMasterMsg->expected) {
|
||||
dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, rpcMsg->code);
|
||||
}
|
||||
|
||||
mnodeDestroySubMsg(pMsg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
dnodeSendRpcMWriteRsp(pMsg, rpcMsg->code);
|
||||
return;
|
||||
}
|
||||
|
@ -2495,6 +2653,19 @@ static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
|
|||
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
|
||||
pMsg->pTable = NULL;
|
||||
mnodeDestroyChildTable(pTable);
|
||||
|
||||
if (pMsg->pBatchMasterMsg) {
|
||||
++pMsg->pBatchMasterMsg->received;
|
||||
if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
|
||||
>= pMsg->pBatchMasterMsg->expected) {
|
||||
dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, code);
|
||||
}
|
||||
|
||||
mnodeDestroySubMsg(pMsg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
dnodeSendRpcMWriteRsp(pMsg, code);
|
||||
}
|
||||
} else {
|
||||
|
@ -2520,6 +2691,19 @@ static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) {
|
|||
//Avoid retry again in client
|
||||
rpcMsg->code = TSDB_CODE_MND_VGROUP_NOT_READY;
|
||||
}
|
||||
|
||||
if (pMsg->pBatchMasterMsg) {
|
||||
++pMsg->pBatchMasterMsg->received;
|
||||
if (pMsg->pBatchMasterMsg->successed + pMsg->pBatchMasterMsg->received
|
||||
>= pMsg->pBatchMasterMsg->expected) {
|
||||
dnodeSendRpcMWriteRsp(pMsg->pBatchMasterMsg, rpcMsg->code);
|
||||
}
|
||||
|
||||
mnodeDestroySubMsg(pMsg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
dnodeSendRpcMWriteRsp(pMsg, rpcMsg->code);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -986,6 +986,19 @@ static void mnodeProcessCreateVnodeRsp(SRpcMsg *rpcMsg) {
|
|||
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) {
|
||||
mnodeMsg->pVgroup = NULL;
|
||||
mnodeDestroyVgroup(pVgroup);
|
||||
|
||||
if (mnodeMsg->pBatchMasterMsg) {
|
||||
++mnodeMsg->pBatchMasterMsg->received;
|
||||
if (mnodeMsg->pBatchMasterMsg->successed + mnodeMsg->pBatchMasterMsg->received
|
||||
>= mnodeMsg->pBatchMasterMsg->expected) {
|
||||
dnodeSendRpcMWriteRsp(mnodeMsg->pBatchMasterMsg, code);
|
||||
}
|
||||
|
||||
mnodeDestroySubMsg(mnodeMsg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
dnodeSendRpcMWriteRsp(mnodeMsg, code);
|
||||
}
|
||||
} else {
|
||||
|
@ -995,6 +1008,19 @@ static void mnodeProcessCreateVnodeRsp(SRpcMsg *rpcMsg) {
|
|||
.pObj = pVgroup
|
||||
};
|
||||
sdbDeleteRow(&row);
|
||||
|
||||
if (mnodeMsg->pBatchMasterMsg) {
|
||||
++mnodeMsg->pBatchMasterMsg->received;
|
||||
if (mnodeMsg->pBatchMasterMsg->successed + mnodeMsg->pBatchMasterMsg->received
|
||||
>= mnodeMsg->pBatchMasterMsg->expected) {
|
||||
dnodeSendRpcMWriteRsp(mnodeMsg->pBatchMasterMsg, mnodeMsg->code);
|
||||
}
|
||||
|
||||
mnodeDestroySubMsg(mnodeMsg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
dnodeSendRpcMWriteRsp(mnodeMsg, mnodeMsg->code);
|
||||
}
|
||||
}
|
||||
|
@ -1192,4 +1218,4 @@ void mnodeSetVgidVer(int8_t *cver, uint64_t iver) {
|
|||
cver[0] = (int8_t)((int32_t)(iver % 1000000) / 10000);
|
||||
cver[1] = (int8_t)((int32_t)(iver % 100000) / 100);
|
||||
cver[2] = (int8_t)(iver % 100);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,12 +89,17 @@ int taosInitTimer(void (*callback)(int), int ms) {
|
|||
if (code != 0) {
|
||||
uError("failed to create timer thread");
|
||||
return -1;
|
||||
} else {
|
||||
uDebug("timer thread:0x%08" PRIx64 " is created", taosGetPthreadId(timerThread));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void taosUninitTimer() {
|
||||
stopTimer = true;
|
||||
|
||||
uDebug("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread));
|
||||
pthread_join(timerThread, NULL);
|
||||
}
|
||||
|
||||
|
|
|
@ -50,9 +50,16 @@ static void httpSendErrorRespImp(HttpContext *pContext, int32_t httpCode, char *
|
|||
char head[512] = {0};
|
||||
char body[512] = {0};
|
||||
|
||||
int8_t httpVersion = 0;
|
||||
int8_t keepAlive = 0;
|
||||
if (pContext->parser != NULL) {
|
||||
httpVersion = pContext->parser->httpVersion;
|
||||
keepAlive = pContext->parser->keepAlive;
|
||||
}
|
||||
|
||||
int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_ERROR], errNo, desc);
|
||||
int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_ERROR], httpVersionStr[pContext->parser->httpVersion],
|
||||
httpCode, httpCodeStr, httpKeepAliveStr[pContext->parser->keepAlive], bodyLen);
|
||||
int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_ERROR], httpVersionStr[httpVersion], httpCode,
|
||||
httpCodeStr, httpKeepAliveStr[keepAlive], bodyLen);
|
||||
|
||||
httpWriteBuf(pContext, head, headLen);
|
||||
httpWriteBuf(pContext, body, bodyLen);
|
||||
|
@ -164,9 +171,16 @@ void httpSendSuccResp(HttpContext *pContext, char *desc) {
|
|||
char head[1024] = {0};
|
||||
char body[1024] = {0};
|
||||
|
||||
int8_t httpVersion = 0;
|
||||
int8_t keepAlive = 0;
|
||||
if (pContext->parser != NULL) {
|
||||
httpVersion = pContext->parser->httpVersion;
|
||||
keepAlive = pContext->parser->keepAlive;
|
||||
}
|
||||
|
||||
int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], TSDB_CODE_SUCCESS, desc);
|
||||
int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OK], httpVersionStr[pContext->parser->httpVersion],
|
||||
httpKeepAliveStr[pContext->parser->keepAlive], bodyLen);
|
||||
int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OK], httpVersionStr[httpVersion],
|
||||
httpKeepAliveStr[keepAlive], bodyLen);
|
||||
|
||||
httpWriteBuf(pContext, head, headLen);
|
||||
httpWriteBuf(pContext, body, bodyLen);
|
||||
|
@ -177,9 +191,16 @@ void httpSendOptionResp(HttpContext *pContext, char *desc) {
|
|||
char head[1024] = {0};
|
||||
char body[1024] = {0};
|
||||
|
||||
int8_t httpVersion = 0;
|
||||
int8_t keepAlive = 0;
|
||||
if (pContext->parser != NULL) {
|
||||
httpVersion = pContext->parser->httpVersion;
|
||||
keepAlive = pContext->parser->keepAlive;
|
||||
}
|
||||
|
||||
int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], TSDB_CODE_SUCCESS, desc);
|
||||
int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OPTIONS], httpVersionStr[pContext->parser->httpVersion],
|
||||
httpKeepAliveStr[pContext->parser->keepAlive], bodyLen);
|
||||
int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OPTIONS], httpVersionStr[httpVersion],
|
||||
httpKeepAliveStr[keepAlive], bodyLen);
|
||||
|
||||
httpWriteBuf(pContext, head, headLen);
|
||||
httpWriteBuf(pContext, body, bodyLen);
|
||||
|
|
|
@ -88,6 +88,7 @@ typedef struct STSBuf {
|
|||
STSList tsData; // uncompressed raw ts data
|
||||
uint64_t numOfTotal;
|
||||
bool autoDelete;
|
||||
bool remainOpen;
|
||||
int32_t tsOrder; // order of timestamp in ts comp buffer
|
||||
STSCursor cur;
|
||||
} STSBuf;
|
||||
|
|
|
@ -3838,8 +3838,10 @@ static void ts_comp_finalize(SQLFunctionCtx *pCtx) {
|
|||
STSBuf * pTSbuf = pInfo->pTSBuf;
|
||||
|
||||
tsBufFlush(pTSbuf);
|
||||
strcpy(pCtx->aOutputBuf, pTSbuf->path);
|
||||
|
||||
*(FILE **)pCtx->aOutputBuf = pTSbuf->f;
|
||||
|
||||
pTSbuf->remainOpen = true;
|
||||
tsBufDestroy(pTSbuf);
|
||||
doFinalizer(pCtx);
|
||||
}
|
||||
|
|
|
@ -758,7 +758,12 @@ static void doUpdateResultRowIndex(SResultRowInfo*pResultRowInfo, TSKEY lastKey,
|
|||
}
|
||||
}
|
||||
|
||||
pResultRowInfo->curIndex = i + 1; // current not closed result object
|
||||
if (i == pResultRowInfo->size - 1) {
|
||||
pResultRowInfo->curIndex = i;
|
||||
} else {
|
||||
pResultRowInfo->curIndex = i + 1; // current not closed result object
|
||||
}
|
||||
|
||||
pResultRowInfo->prevSKey = pResultRowInfo->pResult[pResultRowInfo->curIndex]->win.skey;
|
||||
}
|
||||
}
|
||||
|
@ -1667,7 +1672,7 @@ static void rowwiseApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, SDataStatis *pS
|
|||
|
||||
_end:
|
||||
assert(offset >= 0 && tsCols != NULL);
|
||||
if (prevTs != INT64_MIN) {
|
||||
if (prevTs != INT64_MIN && prevTs != *(int64_t*)pRuntimeEnv->prevRow[0]) {
|
||||
assert(prevRowIndex >= 0);
|
||||
item->lastKey = prevTs + step;
|
||||
}
|
||||
|
@ -2005,6 +2010,7 @@ static void doFreeQueryHandle(SQInfo* pQInfo) {
|
|||
assert(pMemRef->ref == 0 && pMemRef->imem == NULL && pMemRef->mem == NULL);
|
||||
}
|
||||
|
||||
|
||||
static void teardownQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv) {
|
||||
if (pRuntimeEnv->pQuery == NULL) {
|
||||
return;
|
||||
|
@ -2016,6 +2022,16 @@ static void teardownQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv) {
|
|||
qDebug("QInfo:%p teardown runtime env", pQInfo);
|
||||
cleanupResultRowInfo(&pRuntimeEnv->windowResInfo);
|
||||
|
||||
if (isTSCompQuery(pQuery)) {
|
||||
FILE *f = *(FILE **)pQuery->sdata[0]->data;
|
||||
|
||||
if (f) {
|
||||
fclose(f);
|
||||
*(FILE **)pQuery->sdata[0]->data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (pRuntimeEnv->pCtx != NULL) {
|
||||
for (int32_t i = 0; i < pQuery->numOfOutput; ++i) {
|
||||
SQLFunctionCtx *pCtx = &pRuntimeEnv->pCtx[i];
|
||||
|
@ -6944,10 +6960,10 @@ static size_t getResultSize(SQInfo *pQInfo, int64_t *numOfRows) {
|
|||
* TODO handle the case that the file is too large to send back one time
|
||||
*/
|
||||
if (isTSCompQuery(pQuery) && (*numOfRows) > 0) {
|
||||
struct stat fstat;
|
||||
if (stat(pQuery->sdata[0]->data, &fstat) == 0) {
|
||||
*numOfRows = fstat.st_size;
|
||||
return fstat.st_size;
|
||||
struct stat fStat;
|
||||
if (fstat(fileno(*(FILE **)pQuery->sdata[0]->data), &fStat) == 0) {
|
||||
*numOfRows = fStat.st_size;
|
||||
return fStat.st_size;
|
||||
} else {
|
||||
qError("QInfo:%p failed to get file info, path:%s, reason:%s", pQInfo, pQuery->sdata[0]->data, strerror(errno));
|
||||
return 0;
|
||||
|
@ -6963,15 +6979,16 @@ static int32_t doDumpQueryResult(SQInfo *pQInfo, char *data) {
|
|||
|
||||
// load data from file to msg buffer
|
||||
if (isTSCompQuery(pQuery)) {
|
||||
int32_t fd = open(pQuery->sdata[0]->data, O_RDONLY, 0666);
|
||||
|
||||
FILE *f = *(FILE **)pQuery->sdata[0]->data;
|
||||
|
||||
// make sure file exist
|
||||
if (FD_VALID(fd)) {
|
||||
uint64_t s = lseek(fd, 0, SEEK_END);
|
||||
if (f) {
|
||||
off_t s = lseek(fileno(f), 0, SEEK_END);
|
||||
|
||||
qDebug("QInfo:%p ts comp data return, file:%s, size:%"PRId64, pQInfo, pQuery->sdata[0]->data, s);
|
||||
if (lseek(fd, 0, SEEK_SET) >= 0) {
|
||||
size_t sz = read(fd, data, (uint32_t) s);
|
||||
qDebug("QInfo:%p ts comp data return, file:%p, size:%"PRId64, pQInfo, f, s);
|
||||
if (fseek(f, 0, SEEK_SET) >= 0) {
|
||||
size_t sz = fread(data, 1, s, f);
|
||||
if(sz < s) { // todo handle error
|
||||
assert(0);
|
||||
}
|
||||
|
@ -6979,15 +6996,8 @@ static int32_t doDumpQueryResult(SQInfo *pQInfo, char *data) {
|
|||
UNUSED(s);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
unlink(pQuery->sdata[0]->data);
|
||||
} else {
|
||||
// todo return the error code to client and handle invalid fd
|
||||
qError("QInfo:%p failed to open tmp file to send ts-comp data to client, path:%s, reason:%s", pQInfo,
|
||||
pQuery->sdata[0]->data, strerror(errno));
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
}
|
||||
fclose(f);
|
||||
*(FILE **)pQuery->sdata[0]->data = NULL;
|
||||
}
|
||||
|
||||
// all data returned, set query over
|
||||
|
@ -7635,6 +7645,19 @@ void qQueryMgmtNotifyClosed(void* pQMgmt) {
|
|||
taosCacheRefresh(pQueryMgmt->qinfoPool, queryMgmtKillQueryFn);
|
||||
}
|
||||
|
||||
void qQueryMgmtReOpen(void *pQMgmt) {
|
||||
if (pQMgmt == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
SQueryMgmt *pQueryMgmt = pQMgmt;
|
||||
qDebug("vgId:%d, set querymgmt reopen", pQueryMgmt->vgId);
|
||||
|
||||
pthread_mutex_lock(&pQueryMgmt->lock);
|
||||
pQueryMgmt->closed = false;
|
||||
pthread_mutex_unlock(&pQueryMgmt->lock);
|
||||
}
|
||||
|
||||
void qCleanupQueryMgmt(void* pQMgmt) {
|
||||
if (pQMgmt == NULL) {
|
||||
return;
|
||||
|
@ -7715,4 +7738,4 @@ void** qReleaseQInfo(void* pMgmt, void* pQInfo, bool freeHandle) {
|
|||
|
||||
taosCacheRelease(pQueryMgmt->qinfoPool, pQInfo, freeHandle);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ STSBuf* tsBufCreate(bool autoDelete, int32_t order) {
|
|||
if (pTSBuf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pTSBuf->autoDelete = autoDelete;
|
||||
|
||||
taosGetTmpfilePath("join", pTSBuf->path);
|
||||
pTSBuf->f = fopen(pTSBuf->path, "w+");
|
||||
|
@ -26,6 +28,10 @@ STSBuf* tsBufCreate(bool autoDelete, int32_t order) {
|
|||
free(pTSBuf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!autoDelete) {
|
||||
unlink(pTSBuf->path);
|
||||
}
|
||||
|
||||
if (NULL == allocResForTSBuf(pTSBuf)) {
|
||||
return NULL;
|
||||
|
@ -37,8 +43,7 @@ STSBuf* tsBufCreate(bool autoDelete, int32_t order) {
|
|||
|
||||
tsBufResetPos(pTSBuf);
|
||||
pTSBuf->cur.order = TSDB_ORDER_ASC;
|
||||
|
||||
pTSBuf->autoDelete = autoDelete;
|
||||
|
||||
pTSBuf->tsOrder = order;
|
||||
|
||||
return pTSBuf;
|
||||
|
@ -49,6 +54,8 @@ STSBuf* tsBufCreateFromFile(const char* path, bool autoDelete) {
|
|||
if (pTSBuf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pTSBuf->autoDelete = autoDelete;
|
||||
|
||||
tstrncpy(pTSBuf->path, path, sizeof(pTSBuf->path));
|
||||
|
||||
|
@ -129,7 +136,6 @@ STSBuf* tsBufCreateFromFile(const char* path, bool autoDelete) {
|
|||
|
||||
// ascending by default
|
||||
pTSBuf->cur.order = TSDB_ORDER_ASC;
|
||||
pTSBuf->autoDelete = autoDelete;
|
||||
|
||||
// tscDebug("create tsBuf from file:%s, fd:%d, size:%d, numOfGroups:%d, autoDelete:%d", pTSBuf->path, fileno(pTSBuf->f),
|
||||
// pTSBuf->fileSize, pTSBuf->numOfGroups, pTSBuf->autoDelete);
|
||||
|
@ -147,8 +153,10 @@ void* tsBufDestroy(STSBuf* pTSBuf) {
|
|||
|
||||
tfree(pTSBuf->pData);
|
||||
tfree(pTSBuf->block.payload);
|
||||
|
||||
fclose(pTSBuf->f);
|
||||
|
||||
if (!pTSBuf->remainOpen) {
|
||||
fclose(pTSBuf->f);
|
||||
}
|
||||
|
||||
if (pTSBuf->autoDelete) {
|
||||
// ("tsBuf %p destroyed, delete tmp file:%s", pTSBuf, pTSBuf->path);
|
||||
|
|
|
@ -1567,6 +1567,7 @@ static int rpcCheckAuthentication(SRpcConn *pConn, char *msg, int msgLen) {
|
|||
// for response, if code is auth failure, it shall bypass the auth process
|
||||
code = htonl(pHead->code);
|
||||
if (code == TSDB_CODE_RPC_INVALID_TIME_STAMP || code == TSDB_CODE_RPC_AUTH_FAILURE ||
|
||||
code == TSDB_CODE_RPC_INVALID_VERSION ||
|
||||
code == TSDB_CODE_RPC_AUTH_REQUIRED || code == TSDB_CODE_MND_INVALID_USER || code == TSDB_CODE_RPC_NOT_READY) {
|
||||
pHead->msgLen = (int32_t)htonl((uint32_t)pHead->msgLen);
|
||||
// tTrace("%s, dont check authentication since code is:0x%x", pConn->info, code);
|
||||
|
|
|
@ -375,6 +375,8 @@ int32_t syncReconfig(int64_t rid, const SSyncCfg *pNewCfg) {
|
|||
}
|
||||
|
||||
int32_t syncForwardToPeer(int64_t rid, void *data, void *mhandle, int32_t qtype) {
|
||||
if (rid <= 0) return 0;
|
||||
|
||||
SSyncNode *pNode = syncAcquireNode(rid);
|
||||
if (pNode == NULL) return 0;
|
||||
|
||||
|
|
|
@ -90,7 +90,7 @@ static int32_t syncRestoreFile(SSyncPeer *pPeer, uint64_t *fversion) {
|
|||
break;
|
||||
}
|
||||
|
||||
sDebug("%s, file:%s info is received from master, index:%d size:%" PRId64 " fver:%" PRIu64 " magic:%d", pPeer->id,
|
||||
sDebug("%s, file:%s info is received from master, index:%d size:%" PRId64 " fver:%" PRIu64 " magic:%u", pPeer->id,
|
||||
minfo.name, minfo.index, minfo.size, minfo.fversion, minfo.magic);
|
||||
|
||||
// remove extra files on slave between the current and last index
|
||||
|
@ -100,13 +100,13 @@ static int32_t syncRestoreFile(SSyncPeer *pPeer, uint64_t *fversion) {
|
|||
// check the file info
|
||||
sinfo = minfo;
|
||||
sinfo.magic = (*pNode->getFileInfo)(pNode->vgId, sinfo.name, &sinfo.index, TAOS_SYNC_MAX_INDEX, &sinfo.size, &sinfo.fversion);
|
||||
sDebug("%s, local file:%s info, index:%d size:%" PRId64 " fver:%" PRIu64 " magic:%d", pPeer->id, sinfo.name,
|
||||
sDebug("%s, local file:%s info, index:%d size:%" PRId64 " fver:%" PRIu64 " magic:%u", pPeer->id, sinfo.name,
|
||||
sinfo.index, sinfo.size, sinfo.fversion, sinfo.magic);
|
||||
|
||||
// if file not there or magic is not the same, file shall be synced
|
||||
memset(&fileAck, 0, sizeof(SFileAck));
|
||||
syncBuildFileAck(&fileAck, pNode->vgId);
|
||||
fileAck.sync = (sinfo.magic != minfo.magic || sinfo.name[0] == 0) ? 1 : 0;
|
||||
fileAck.sync = (sinfo.magic != minfo.magic || sinfo.size != minfo.size || sinfo.name[0] == 0) ? 1 : 0;
|
||||
|
||||
// send file ack
|
||||
ret = taosWriteMsg(pPeer->syncFd, &fileAck, sizeof(SFileAck));
|
||||
|
@ -195,7 +195,11 @@ static int32_t syncRestoreWal(SSyncPeer *pPeer, uint64_t *wver) {
|
|||
}
|
||||
lastVer = pHead->version;
|
||||
|
||||
(*pNode->writeToCache)(pNode->vgId, pHead, TAOS_QTYPE_WAL, NULL);
|
||||
ret = (*pNode->writeToCache)(pNode->vgId, pHead, TAOS_QTYPE_WAL, NULL);
|
||||
if (ret != 0) {
|
||||
sError("%s, failed to restore record since %s, hver:%" PRIu64, pPeer->id, tstrerror(ret), pHead->version);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (code < 0) {
|
||||
|
|
|
@ -104,7 +104,7 @@ static int32_t syncRetrieveFile(SSyncPeer *pPeer) {
|
|||
fileInfo.magic = (*pNode->getFileInfo)(pNode->vgId, fileInfo.name, &fileInfo.index, TAOS_SYNC_MAX_INDEX,
|
||||
&fileInfo.size, &fileInfo.fversion);
|
||||
syncBuildFileInfo(&fileInfo, pNode->vgId);
|
||||
sDebug("%s, file:%s info is sent, index:%d size:%" PRId64 " fver:%" PRIu64 " magic:%d", pPeer->id, fileInfo.name,
|
||||
sDebug("%s, file:%s info is sent, index:%d size:%" PRId64 " fver:%" PRIu64 " magic:%u", pPeer->id, fileInfo.name,
|
||||
fileInfo.index, fileInfo.size, fileInfo.fversion, fileInfo.magic);
|
||||
|
||||
// send the file info
|
||||
|
@ -143,10 +143,10 @@ static int32_t syncRetrieveFile(SSyncPeer *pPeer) {
|
|||
// if sync is not required, continue
|
||||
if (fileAck.sync == 0) {
|
||||
fileInfo.index++;
|
||||
sDebug("%s, %s is the same", pPeer->id, fileInfo.name);
|
||||
sDebug("%s, %s is the same, fver:%" PRIu64, pPeer->id, fileInfo.name, fileInfo.fversion);
|
||||
continue;
|
||||
} else {
|
||||
sDebug("%s, %s will be sent", pPeer->id, fileInfo.name);
|
||||
sDebug("%s, %s will be sent, fver:%" PRIu64, pPeer->id, fileInfo.name, fileInfo.fversion);
|
||||
}
|
||||
|
||||
// get the full path to file
|
||||
|
@ -328,7 +328,8 @@ static int32_t syncProcessLastWal(SSyncPeer *pPeer, char *wname, int64_t index)
|
|||
// if bytes > 0, file is updated, or fversion is not reached but file still open, read again
|
||||
once = 1;
|
||||
offset += bytes;
|
||||
sDebug("%s, continue retrieve last wal, bytes:%d offset:%" PRId64, pPeer->id, bytes, offset);
|
||||
sDebug("%s, continue retrieve last wal, bytes:%d offset:%" PRId64 " sver:%" PRIu64 " fver:%" PRIu64, pPeer->id,
|
||||
bytes, offset, pPeer->sversion, fversion);
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
@ -503,9 +504,10 @@ void *syncRetrieveData(void *param) {
|
|||
taosClose(pPeer->syncFd);
|
||||
|
||||
// The ref is obtained in both the create thread and the current thread, so it is released twice
|
||||
sInfo("%s, sync retrieve data over, sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]);
|
||||
|
||||
syncReleasePeer(pPeer);
|
||||
syncReleasePeer(pPeer);
|
||||
|
||||
sInfo("%s, sync retrieve data over, sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]);
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ void vnodeCleanupRead(void);
|
|||
int32_t vnodeWriteToRQueue(void *pVnode, void *pCont, int32_t contLen, int8_t qtype, void *rparam);
|
||||
void vnodeFreeFromRQueue(void *pVnode, SVReadMsg *pRead);
|
||||
int32_t vnodeProcessRead(void *pVnode, SVReadMsg *pRead);
|
||||
void vnodeWaitReadCompleted(void *pVnode);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ bool vnodeSetResetStatus(SVnodeObj* pVnode);
|
|||
|
||||
bool vnodeInInitStatus(SVnodeObj* pVnode);
|
||||
bool vnodeInReadyStatus(SVnodeObj* pVnode);
|
||||
bool vnodeInReadyOrUpdatingStatus(SVnodeObj* pVnode);
|
||||
bool vnodeInClosingStatus(SVnodeObj* pVnode);
|
||||
bool vnodeInResetStatus(SVnodeObj* pVnode);
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ void vnodeCleanupWrite(void);
|
|||
int32_t vnodeWriteToWQueue(void *pVnode, void *pHead, int32_t qtype, void *pRpcMsg);
|
||||
void vnodeFreeFromWQueue(void *pVnode, SVWriteMsg *pWrite);
|
||||
int32_t vnodeProcessWrite(void *pVnode, void *pHead, int32_t qtype, void *pRspRet);
|
||||
void vnodeWaitWriteCompleted(void *pVnode);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -153,6 +153,11 @@ static int32_t vnodeAlterImp(SVnodeObj *pVnode, SCreateVnodeMsg *pVnodeCfg) {
|
|||
|
||||
int32_t vnodeAlter(void *vparam, SCreateVnodeMsg *pVnodeCfg) {
|
||||
SVnodeObj *pVnode = vparam;
|
||||
if (pVnode->dbCfgVersion == pVnodeCfg->cfg.dbCfgVersion && pVnode->vgCfgVersion == pVnodeCfg->cfg.vgCfgVersion) {
|
||||
vDebug("vgId:%d, dbCfgVersion:%d and vgCfgVersion:%d not change", pVnode->vgId, pVnode->dbCfgVersion,
|
||||
pVnode->vgCfgVersion);
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
// vnode in non-ready state and still needs to return success instead of TSDB_CODE_VND_INVALID_STATUS
|
||||
// dbCfgVersion can be corrected by status msg
|
||||
|
@ -411,15 +416,12 @@ void vnodeDestroy(SVnodeObj *pVnode) {
|
|||
}
|
||||
|
||||
void vnodeCleanUp(SVnodeObj *pVnode) {
|
||||
if (!vnodeInInitStatus(pVnode)) {
|
||||
// it may be in updateing or reset state, then it shall wait
|
||||
int32_t i = 0;
|
||||
while (!vnodeSetClosingStatus(pVnode)) {
|
||||
if (++i % 1000 == 0) {
|
||||
sched_yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
vDebug("vgId:%d, vnode will cleanup, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode);
|
||||
|
||||
vnodeSetClosingStatus(pVnode);
|
||||
|
||||
// release local resources only after cutting off outside connections
|
||||
qQueryMgmtNotifyClosed(pVnode->qMgmt);
|
||||
|
||||
// stop replication module
|
||||
if (pVnode->sync > 0) {
|
||||
|
@ -428,10 +430,7 @@ void vnodeCleanUp(SVnodeObj *pVnode) {
|
|||
syncStop(sync);
|
||||
}
|
||||
|
||||
vDebug("vgId:%d, vnode will cleanup, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode);
|
||||
|
||||
// release local resources only after cutting off outside connections
|
||||
qQueryMgmtNotifyClosed(pVnode->qMgmt);
|
||||
vDebug("vgId:%d, vnode is cleaned, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode);
|
||||
vnodeRelease(pVnode);
|
||||
}
|
||||
|
||||
|
|
|
@ -88,22 +88,15 @@ void vnodeFreeFromRQueue(void *vparam, SVReadMsg *pRead) {
|
|||
vnodeRelease(pVnode);
|
||||
}
|
||||
|
||||
int32_t vnodeWriteToRQueue(void *vparam, void *pCont, int32_t contLen, int8_t qtype, void *rparam) {
|
||||
SVnodeObj *pVnode = vparam;
|
||||
|
||||
if (qtype == TAOS_QTYPE_RPC || qtype == TAOS_QTYPE_QUERY) {
|
||||
int32_t code = vnodeCheckRead(pVnode);
|
||||
if (code != TSDB_CODE_SUCCESS) return code;
|
||||
}
|
||||
|
||||
static SVReadMsg *vnodeBuildVReadMsg(SVnodeObj *pVnode, void *pCont, int32_t contLen, int8_t qtype, SRpcMsg *pRpcMsg) {
|
||||
int32_t size = sizeof(SVReadMsg) + contLen;
|
||||
SVReadMsg *pRead = taosAllocateQitem(size);
|
||||
if (pRead == NULL) {
|
||||
return TSDB_CODE_VND_OUT_OF_MEMORY;
|
||||
terrno = TSDB_CODE_VND_OUT_OF_MEMORY;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (rparam != NULL) {
|
||||
SRpcMsg *pRpcMsg = rparam;
|
||||
if (pRpcMsg != NULL) {
|
||||
pRead->rpcHandle = pRpcMsg->handle;
|
||||
pRead->rpcAhandle = pRpcMsg->ahandle;
|
||||
pRead->msgType = pRpcMsg->msgType;
|
||||
|
@ -119,13 +112,35 @@ int32_t vnodeWriteToRQueue(void *vparam, void *pCont, int32_t contLen, int8_t qt
|
|||
|
||||
pRead->qtype = qtype;
|
||||
atomic_add_fetch_32(&pVnode->refCount, 1);
|
||||
|
||||
return pRead;
|
||||
}
|
||||
|
||||
int32_t vnodeWriteToRQueue(void *vparam, void *pCont, int32_t contLen, int8_t qtype, void *rparam) {
|
||||
SVReadMsg *pRead = vnodeBuildVReadMsg(vparam, pCont, contLen, qtype, rparam);
|
||||
if (pRead == NULL) {
|
||||
assert(terrno != 0);
|
||||
return terrno;
|
||||
}
|
||||
|
||||
SVnodeObj *pVnode = vparam;
|
||||
|
||||
int32_t code = vnodeCheckRead(pVnode);
|
||||
if (code != TSDB_CODE_SUCCESS) {
|
||||
taosFreeQitem(pRead);
|
||||
vnodeRelease(pVnode);
|
||||
return code;
|
||||
}
|
||||
|
||||
atomic_add_fetch_32(&pVnode->queuedRMsg, 1);
|
||||
|
||||
if (pRead->code == TSDB_CODE_RPC_NETWORK_UNAVAIL || pRead->msgType == TSDB_MSG_TYPE_FETCH) {
|
||||
vTrace("vgId:%d, write into vfetch queue, refCount:%d queued:%d", pVnode->vgId, pVnode->refCount, pVnode->queuedRMsg);
|
||||
if (pRead->code == TSDB_CODE_RPC_NETWORK_UNAVAIL || pRead->msgType == TSDB_MSG_TYPE_FETCH) {
|
||||
vTrace("vgId:%d, write into vfetch queue, refCount:%d queued:%d", pVnode->vgId, pVnode->refCount,
|
||||
pVnode->queuedRMsg);
|
||||
return taosWriteQitem(pVnode->fqueue, qtype, pRead);
|
||||
} else {
|
||||
vTrace("vgId:%d, write into vquery queue, refCount:%d queued:%d", pVnode->vgId, pVnode->refCount, pVnode->queuedRMsg);
|
||||
vTrace("vgId:%d, write into vquery queue, refCount:%d queued:%d", pVnode->vgId, pVnode->refCount,
|
||||
pVnode->queuedRMsg);
|
||||
return taosWriteQitem(pVnode->qqueue, qtype, pRead);
|
||||
}
|
||||
}
|
||||
|
@ -420,3 +435,5 @@ int32_t vnodeNotifyCurrentQhandle(void *handle, void *qhandle, int32_t vgId) {
|
|||
vTrace("QInfo:%p register qhandle to connect:%p", qhandle, handle);
|
||||
return rpcReportProgress(handle, (char *)pMsg, sizeof(SRetrieveTableMsg));
|
||||
}
|
||||
|
||||
void vnodeWaitReadCompleted(void *pVnode) {}
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
#define _DEFAULT_SOURCE
|
||||
#include "os.h"
|
||||
#include "taosmsg.h"
|
||||
#include "query.h"
|
||||
#include "vnodeStatus.h"
|
||||
|
||||
char* vnodeStatus[] = {
|
||||
|
@ -44,11 +46,13 @@ bool vnodeSetReadyStatus(SVnodeObj* pVnode) {
|
|||
vDebug("vgId:%d, cannot set status:ready, old:%s", pVnode->vgId, vnodeStatus[pVnode->status]);
|
||||
}
|
||||
|
||||
qQueryMgmtReOpen(pVnode->qMgmt);
|
||||
|
||||
pthread_mutex_unlock(&pVnode->statusMutex);
|
||||
return set;
|
||||
}
|
||||
|
||||
bool vnodeSetClosingStatus(SVnodeObj* pVnode) {
|
||||
static bool vnodeSetClosingStatusImp(SVnodeObj* pVnode) {
|
||||
bool set = false;
|
||||
pthread_mutex_lock(&pVnode->statusMutex);
|
||||
|
||||
|
@ -63,6 +67,20 @@ bool vnodeSetClosingStatus(SVnodeObj* pVnode) {
|
|||
return set;
|
||||
}
|
||||
|
||||
bool vnodeSetClosingStatus(SVnodeObj* pVnode) {
|
||||
if (!vnodeInInitStatus(pVnode)) {
|
||||
// it may be in updating or reset state, then it shall wait
|
||||
int32_t i = 0;
|
||||
while (!vnodeSetClosingStatusImp(pVnode)) {
|
||||
if (++i % 1000 == 0) {
|
||||
sched_yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool vnodeSetUpdatingStatus(SVnodeObj* pVnode) {
|
||||
bool set = false;
|
||||
pthread_mutex_lock(&pVnode->statusMutex);
|
||||
|
@ -117,6 +135,18 @@ bool vnodeInReadyStatus(SVnodeObj* pVnode) {
|
|||
return in;
|
||||
}
|
||||
|
||||
bool vnodeInReadyOrUpdatingStatus(SVnodeObj* pVnode) {
|
||||
bool in = false;
|
||||
pthread_mutex_lock(&pVnode->statusMutex);
|
||||
|
||||
if (pVnode->status == TAOS_VN_STATUS_READY || pVnode->status == TAOS_VN_STATUS_UPDATING) {
|
||||
in = true;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&pVnode->statusMutex);
|
||||
return in;
|
||||
}
|
||||
|
||||
bool vnodeInClosingStatus(SVnodeObj* pVnode) {
|
||||
bool in = false;
|
||||
pthread_mutex_lock(&pVnode->statusMutex);
|
||||
|
|
|
@ -90,7 +90,10 @@ int32_t vnodeProcessWrite(void *vparam, void *wparam, int32_t qtype, void *rpara
|
|||
|
||||
// write into WAL
|
||||
code = walWrite(pVnode->wal, pHead);
|
||||
if (code < 0) return code;
|
||||
if (code < 0) {
|
||||
vError("vgId:%d, hver:%" PRIu64 " vver:%" PRIu64 " code:0x%x", pVnode->vgId, pHead->version, pVnode->version, code);
|
||||
return code;
|
||||
}
|
||||
|
||||
pVnode->version = pHead->version;
|
||||
|
||||
|
@ -101,8 +104,7 @@ int32_t vnodeProcessWrite(void *vparam, void *wparam, int32_t qtype, void *rpara
|
|||
return syncCode;
|
||||
}
|
||||
|
||||
static int32_t vnodeCheckWrite(void *vparam) {
|
||||
SVnodeObj *pVnode = vparam;
|
||||
static int32_t vnodeCheckWrite(SVnodeObj *pVnode) {
|
||||
if (!(pVnode->accessState & TSDB_VN_WRITE_ACCCESS)) {
|
||||
vDebug("vgId:%d, no write auth, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode);
|
||||
return TSDB_CODE_VND_NO_WRITE_AUTH;
|
||||
|
@ -121,12 +123,6 @@ static int32_t vnodeCheckWrite(void *vparam) {
|
|||
return TSDB_CODE_APP_NOT_READY;
|
||||
}
|
||||
|
||||
if (vnodeInClosingStatus(pVnode)) {
|
||||
vDebug("vgId:%d, vnode status is %s, refCount:%d pVnode:%p", pVnode->vgId, vnodeStatus[pVnode->status],
|
||||
pVnode->refCount, pVnode);
|
||||
return TSDB_CODE_APP_NOT_READY;
|
||||
}
|
||||
|
||||
if (pVnode->isFull) {
|
||||
vDebug("vgId:%d, vnode is full, refCount:%d", pVnode->vgId, pVnode->refCount);
|
||||
return TSDB_CODE_VND_IS_FULL;
|
||||
|
@ -216,29 +212,21 @@ static int32_t vnodeProcessUpdateTagValMsg(SVnodeObj *pVnode, void *pCont, SRspR
|
|||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t vnodeWriteToWQueue(void *vparam, void *wparam, int32_t qtype, void *rparam) {
|
||||
SVnodeObj *pVnode = vparam;
|
||||
SWalHead * pHead = wparam;
|
||||
int32_t code = 0;
|
||||
|
||||
if (qtype == TAOS_QTYPE_RPC) {
|
||||
code = vnodeCheckWrite(pVnode);
|
||||
if (code != TSDB_CODE_SUCCESS) return code;
|
||||
}
|
||||
|
||||
static SVWriteMsg *vnodeBuildVWriteMsg(SVnodeObj *pVnode, SWalHead *pHead, int32_t qtype, SRpcMsg *pRpcMsg) {
|
||||
if (pHead->len > TSDB_MAX_WAL_SIZE) {
|
||||
vError("vgId:%d, wal len:%d exceeds limit, hver:%" PRIu64, pVnode->vgId, pHead->len, pHead->version);
|
||||
return TSDB_CODE_WAL_SIZE_LIMIT;
|
||||
terrno = TSDB_CODE_WAL_SIZE_LIMIT;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int32_t size = sizeof(SVWriteMsg) + sizeof(SWalHead) + pHead->len;
|
||||
SVWriteMsg *pWrite = taosAllocateQitem(size);
|
||||
if (pWrite == NULL) {
|
||||
return TSDB_CODE_VND_OUT_OF_MEMORY;
|
||||
terrno = TSDB_CODE_VND_OUT_OF_MEMORY;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (rparam != NULL) {
|
||||
SRpcMsg *pRpcMsg = rparam;
|
||||
if (pRpcMsg != NULL) {
|
||||
pWrite->rpcMsg = *pRpcMsg;
|
||||
}
|
||||
|
||||
|
@ -248,6 +236,30 @@ int32_t vnodeWriteToWQueue(void *vparam, void *wparam, int32_t qtype, void *rpar
|
|||
|
||||
atomic_add_fetch_32(&pVnode->refCount, 1);
|
||||
|
||||
return pWrite;
|
||||
}
|
||||
|
||||
static int32_t vnodeWriteToWQueueImp(SVWriteMsg *pWrite) {
|
||||
SVnodeObj *pVnode = pWrite->pVnode;
|
||||
|
||||
if (pWrite->qtype == TAOS_QTYPE_RPC) {
|
||||
int32_t code = vnodeCheckWrite(pVnode);
|
||||
if (code != TSDB_CODE_SUCCESS) {
|
||||
vError("vgId:%d, failed to write into vwqueue since %s", pVnode->vgId, tstrerror(code));
|
||||
taosFreeQitem(pWrite);
|
||||
vnodeRelease(pVnode);
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
if (!vnodeInReadyOrUpdatingStatus(pVnode)) {
|
||||
vError("vgId:%d, failed to write into vwqueue, vstatus is %s, refCount:%d pVnode:%p", pVnode->vgId,
|
||||
vnodeStatus[pVnode->status], pVnode->refCount, pVnode);
|
||||
taosFreeQitem(pWrite);
|
||||
vnodeRelease(pVnode);
|
||||
return TSDB_CODE_APP_NOT_READY;
|
||||
}
|
||||
|
||||
int32_t queued = atomic_add_fetch_32(&pVnode->queuedWMsg, 1);
|
||||
if (queued > MAX_QUEUED_MSG_NUM) {
|
||||
int32_t ms = (queued / MAX_QUEUED_MSG_NUM) * 10 + 3;
|
||||
|
@ -256,15 +268,25 @@ int32_t vnodeWriteToWQueue(void *vparam, void *wparam, int32_t qtype, void *rpar
|
|||
taosMsleep(ms);
|
||||
}
|
||||
|
||||
code = vnodePerformFlowCtrl(pWrite);
|
||||
if (code != 0) return 0;
|
||||
|
||||
vTrace("vgId:%d, write into vwqueue, refCount:%d queued:%d", pVnode->vgId, pVnode->refCount, pVnode->queuedWMsg);
|
||||
|
||||
taosWriteQitem(pVnode->wqueue, qtype, pWrite);
|
||||
taosWriteQitem(pVnode->wqueue, pWrite->qtype, pWrite);
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t vnodeWriteToWQueue(void *vparam, void *wparam, int32_t qtype, void *rparam) {
|
||||
SVWriteMsg *pWrite = vnodeBuildVWriteMsg(vparam, wparam, qtype, rparam);
|
||||
if (pWrite == NULL) {
|
||||
assert(terrno != 0);
|
||||
return terrno;
|
||||
}
|
||||
|
||||
int32_t code = vnodePerformFlowCtrl(pWrite);
|
||||
if (code != 0) return 0;
|
||||
|
||||
return vnodeWriteToWQueueImp(pWrite);
|
||||
}
|
||||
|
||||
void vnodeFreeFromWQueue(void *vparam, SVWriteMsg *pWrite) {
|
||||
SVnodeObj *pVnode = vparam;
|
||||
|
||||
|
@ -294,7 +316,10 @@ static void vnodeFlowCtrlMsgToWQueue(void *param, void *tmrId) {
|
|||
vDebug("vgId:%d, msg:%p, write into vwqueue after flowctrl, retry:%d", pVnode->vgId, pWrite,
|
||||
pWrite->processedCount);
|
||||
pWrite->processedCount = 0;
|
||||
taosWriteQitem(pVnode->wqueue, pWrite->qtype, pWrite);
|
||||
code = vnodeWriteToWQueueImp(pWrite);
|
||||
if (code != 0) {
|
||||
dnodeSendRpcVWriteRsp(pWrite->pVnode, pWrite, code);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -319,3 +344,5 @@ static int32_t vnodePerformFlowCtrl(SVWriteMsg *pWrite) {
|
|||
return TSDB_CODE_VND_ACTION_IN_PROGRESS;
|
||||
}
|
||||
}
|
||||
|
||||
void vnodeWaitWriteCompleted(void *pVnode) {}
|
|
@ -0,0 +1,39 @@
|
|||
install build environment
|
||||
===
|
||||
yum/apt install mono-complete
|
||||
|
||||
build C# version taosdemo
|
||||
===
|
||||
mcs -out:taosdemo *.cs
|
||||
|
||||
run C# version taosdemo
|
||||
===
|
||||
Usage: mono taosdemo.exe [OPTION...]
|
||||
|
||||
--help Show usage.
|
||||
|
||||
-h host, The host to connect to TDengine. Default is localhost.
|
||||
-p port, The TCP/IP port number to use for the connection. Default is 0.
|
||||
-u user, The user name to use when connecting to the server. Default is 'root'.
|
||||
-P password, The password to use when connecting to the server. Default is 'taosdata'.
|
||||
-d database, Destination database. Default is 'test'.
|
||||
-a replica, Set the replica parameters of the database, Default 1, min: 1, max: 5.
|
||||
-m table_prefix, Table prefix name. Default is 't'.
|
||||
-s sql file, The select sql file.
|
||||
-M stable, Use super table.
|
||||
-o outputfile, Direct output to the named file. Default is './output.txt'.
|
||||
-q query_mode, Query mode--0: SYNC, 1: ASYNC. Default is SYNC.
|
||||
-b type_of_cols, data_type of columns: 'INT', 'TINYINT', 'SMALLINT', 'BIGINT', 'FLOAT', 'DOUBLE', 'BINARY'. Default is 'INT'.
|
||||
-w length_of_binary, The length of data_type 'BINARY'. Only applicable when type of cols is 'BINARY'. Default is 8
|
||||
-l num_of_cols_per_record, The number of columns per record. Default is 3.
|
||||
-T num_of_threads, The number of threads. Default is 10.
|
||||
-r num_of_records_per_req, The number of records per request. Default is 1000.
|
||||
-t num_of_tables, The number of tables. Default is 1.
|
||||
-n num_of_records_per_table, The number of records per table. Default is 1.
|
||||
-c config_directory, Configuration directory. Default is '/etc/taos/'.
|
||||
-x flag, Insert only flag.
|
||||
-O order, Insert mode--0: In order, 1: Out of order. Default is in order.
|
||||
-R rate, Out of order data's rate--if order=1 Default 10, min: 0, max: 50.
|
||||
-D Delete data methods 0: don't delete, 1: delete by table, 2: delete by stable, 3: delete by database.
|
||||
-v Print verbose output
|
||||
-y Skip read key for continous test, default is not skip
|
|
@ -0,0 +1,155 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* This program is free software: you can use, redistribute, and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3
|
||||
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TDengineDriver
|
||||
{
|
||||
enum TDengineDataType
|
||||
{
|
||||
TSDB_DATA_TYPE_NULL = 0, // 1 bytes
|
||||
TSDB_DATA_TYPE_BOOL = 1, // 1 bytes
|
||||
TSDB_DATA_TYPE_TINYINT = 2, // 1 bytes
|
||||
TSDB_DATA_TYPE_SMALLINT = 3, // 2 bytes
|
||||
TSDB_DATA_TYPE_INT = 4, // 4 bytes
|
||||
TSDB_DATA_TYPE_BIGINT = 5, // 8 bytes
|
||||
TSDB_DATA_TYPE_FLOAT = 6, // 4 bytes
|
||||
TSDB_DATA_TYPE_DOUBLE = 7, // 8 bytes
|
||||
TSDB_DATA_TYPE_BINARY = 8, // string
|
||||
TSDB_DATA_TYPE_TIMESTAMP = 9,// 8 bytes
|
||||
TSDB_DATA_TYPE_NCHAR = 10 // unicode string
|
||||
}
|
||||
|
||||
enum TDengineInitOption
|
||||
{
|
||||
TSDB_OPTION_LOCALE = 0,
|
||||
TSDB_OPTION_CHARSET = 1,
|
||||
TSDB_OPTION_TIMEZONE = 2,
|
||||
TDDB_OPTION_CONFIGDIR = 3,
|
||||
TDDB_OPTION_SHELL_ACTIVITY_TIMER = 4
|
||||
}
|
||||
|
||||
class TDengineMeta
|
||||
{
|
||||
public string name;
|
||||
public short size;
|
||||
public byte type;
|
||||
public string TypeName()
|
||||
{
|
||||
switch ((TDengineDataType)type)
|
||||
{
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BOOL:
|
||||
return "BOOLEAN";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
|
||||
return "BYTE";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
|
||||
return "SHORT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_INT:
|
||||
return "INT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
|
||||
return "LONG";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
|
||||
return "FLOAT";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
|
||||
return "DOUBLE";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BINARY:
|
||||
return "STRING";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
return "TIMESTAMP";
|
||||
case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
|
||||
return "NCHAR";
|
||||
default:
|
||||
return "undefine";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TDengine
|
||||
{
|
||||
public const int TSDB_CODE_SUCCESS = 0;
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_init", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Init();
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_cleanup", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Cleanup();
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_options", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public void Options(int option, string value);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_connect", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr Connect(string ip, string user, string password, string db, short port);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_errstr", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern private IntPtr taos_errstr(IntPtr res);
|
||||
static public string Error(IntPtr res)
|
||||
{
|
||||
IntPtr errPtr = taos_errstr(res);
|
||||
return Marshal.PtrToStringAnsi(errPtr);
|
||||
}
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_errno", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int ErrorNo(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_query", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr Query(IntPtr conn, string sqlstr);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int AffectRows(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_field_count", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int FieldCount(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_fetch_fields", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern private IntPtr taos_fetch_fields(IntPtr res);
|
||||
static public List<TDengineMeta> FetchFields(IntPtr res)
|
||||
{
|
||||
const int fieldSize = 68;
|
||||
|
||||
List<TDengineMeta> metas = new List<TDengineMeta>();
|
||||
if (res == IntPtr.Zero)
|
||||
{
|
||||
return metas;
|
||||
}
|
||||
|
||||
int fieldCount = FieldCount(res);
|
||||
IntPtr fieldsPtr = taos_fetch_fields(res);
|
||||
|
||||
for (int i = 0; i < fieldCount; ++i)
|
||||
{
|
||||
int offset = i * fieldSize;
|
||||
|
||||
TDengineMeta meta = new TDengineMeta();
|
||||
meta.name = Marshal.PtrToStringAnsi(fieldsPtr + offset);
|
||||
meta.type = Marshal.ReadByte(fieldsPtr + offset + 65);
|
||||
meta.size = Marshal.ReadInt16(fieldsPtr + offset + 66);
|
||||
metas.Add(meta);
|
||||
}
|
||||
|
||||
return metas;
|
||||
}
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_fetch_row", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr FetchRows(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_free_result", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public IntPtr FreeResult(IntPtr res);
|
||||
|
||||
[DllImport("taos", EntryPoint = "taos_close", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern public int Close(IntPtr taos);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,782 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* This program is free software: you can use, redistribute, and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3
|
||||
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace TDengineDriver
|
||||
{
|
||||
class TDengineTest
|
||||
{
|
||||
//connect parameters
|
||||
private string host = "127.0.0.1";
|
||||
private string configDir = "C:/TDengine/cfg";
|
||||
private string user = "root";
|
||||
private string password = "taosdata";
|
||||
private short port = 0;
|
||||
|
||||
//sql parameters
|
||||
private string dbName = "db";
|
||||
private string stableName = "st";
|
||||
private string tablePrefix = "t";
|
||||
|
||||
private bool isInsertOnly = false;
|
||||
private int queryMode = 1;
|
||||
|
||||
private long recordsPerTable = 1;
|
||||
private int recordsPerRequest = 1;
|
||||
private int colsPerRecord = 3;
|
||||
private long batchRows = 1000;
|
||||
private long numOfTables = 1;
|
||||
private short replica = 1;
|
||||
|
||||
private IntPtr conn = IntPtr.Zero;
|
||||
// private long rowsInserted = 0;
|
||||
private bool useStable = false;
|
||||
private short methodOfDelete = 0;
|
||||
private long numOfThreads = 1;
|
||||
private long rateOfOutorder = 0;
|
||||
private bool order = true;
|
||||
private bool skipReadKey = false;
|
||||
private bool verbose = false;
|
||||
|
||||
|
||||
static void PrintHelp(String[] argv)
|
||||
{
|
||||
for (int i = 0; i < argv.Length; ++i)
|
||||
{
|
||||
if ("--help" == argv[i])
|
||||
{
|
||||
Console.WriteLine("Usage: mono taosdemo.exe [OPTION...]");
|
||||
Console.WriteLine("");
|
||||
string indent = " ";
|
||||
Console.WriteLine("{0}{1}", indent, "--help Show usage.");
|
||||
Console.WriteLine("");
|
||||
Console.Write("{0}{1}", indent, "-h");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "host, The host to connect to TDengine. Default is localhost.");
|
||||
Console.Write("{0}{1}", indent, "-p");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "port, The TCP/IP port number to use for the connection. Default is 0.");
|
||||
Console.Write("{0}{1}", indent, "-u");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "user, The user name to use when connecting to the server. Default is 'root'.");
|
||||
Console.Write("{0}{1}", indent, "-P");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "password, The password to use when connecting to the server. Default is 'taosdata'.");
|
||||
Console.Write("{0}{1}", indent, "-d");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "database, Destination database. Default is 'test'.");
|
||||
Console.Write("{0}{1}", indent, "-a");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "replica, Set the replica parameters of the database, Default 1, min: 1, max: 5.");
|
||||
Console.Write("{0}{1}", indent, "-m");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "table_prefix, Table prefix name. Default is 't'.");
|
||||
Console.Write("{0}{1}", indent, "-s");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "sql file, The select sql file.");
|
||||
Console.Write("{0}{1}", indent, "-M");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "stable, Use super table.");
|
||||
Console.Write("{0}{1}", indent, "-o");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "outputfile, Direct output to the named file. Default is './output.txt'.");
|
||||
Console.Write("{0}{1}", indent, "-q");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "query_mode, Query mode--0: SYNC, 1: ASYNC. Default is SYNC.");
|
||||
Console.Write("{0}{1}", indent, "-b");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "type_of_cols, data_type of columns: 'INT', 'TINYINT', 'SMALLINT', 'BIGINT', 'FLOAT', 'DOUBLE', 'BINARY'. Default is 'INT'.");
|
||||
Console.Write("{0}{1}", indent, "-w");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "length_of_binary, The length of data_type 'BINARY'. Only applicable when type of cols is 'BINARY'. Default is 8");
|
||||
Console.Write("{0}{1}", indent, "-l");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "num_of_cols_per_record, The number of columns per record. Default is 3.");
|
||||
Console.Write("{0}{1}", indent, "-T");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "num_of_threads, The number of threads. Default is 10.");
|
||||
Console.Write("{0}{1}", indent, "-r");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "num_of_records_per_req, The number of records per request. Default is 1000.");
|
||||
Console.Write("{0}{1}", indent, "-t");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "num_of_tables, The number of tables. Default is 1.");
|
||||
Console.Write("{0}{1}", indent, "-n");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "num_of_records_per_table, The number of records per table. Default is 1.");
|
||||
Console.Write("{0}{1}", indent, "-c");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "config_directory, Configuration directory. Default is '/etc/taos/'.");
|
||||
Console.Write("{0}{1}", indent, "-x");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "flag, Insert only flag.");
|
||||
Console.Write("{0}{1}", indent, "-O");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "order, Insert mode--0: In order, 1: Out of order. Default is in order.");
|
||||
Console.Write("{0}{1}", indent, "-R");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "rate, Out of order data's rate--if order=1 Default 10, min: 0, max: 50.");
|
||||
Console.Write("{0}{1}", indent, "-D");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "Delete data methods 0: don't delete, 1: delete by table, 2: delete by stable, 3: delete by database.");
|
||||
Console.Write("{0}{1}", indent, "-v");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "Print verbose output");
|
||||
Console.Write("{0}{1}", indent, "-y");
|
||||
Console.Write("{0}{1}{2}\n", indent, indent, "Skip read key for continous test, default is not skip");
|
||||
|
||||
System.Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ReadArgument(String[] argv)
|
||||
{
|
||||
host = this.GetArgumentAsString(argv, "-h", "127.0.0.1");
|
||||
port = (short)this.GetArgumentAsLong(argv, "-p", 0, 65535, 6030);
|
||||
user = this.GetArgumentAsString(argv, "-u", "root");
|
||||
password = this.GetArgumentAsString(argv, "-P", "taosdata");
|
||||
dbName = this.GetArgumentAsString(argv, "-d", "db");
|
||||
stableName = this.GetArgumentAsString(argv, "-s", "st");
|
||||
tablePrefix = this.GetArgumentAsString(argv, "-m", "t");
|
||||
isInsertOnly = this.GetArgumentAsFlag(argv, "-x");
|
||||
queryMode = (int)this.GetArgumentAsLong(argv, "-q", 0, 1, 0);
|
||||
numOfTables = this.GetArgumentAsLong(argv, "-t", 1, 1000000000, 1);
|
||||
batchRows = this.GetArgumentAsLong(argv, "-r", 1, 10000, 1000);
|
||||
recordsPerTable = this.GetArgumentAsLong(argv, "-n", 1, 100000000000, 1);
|
||||
recordsPerRequest = (int)this.GetArgumentAsLong(argv, "-r", 1, 10000, 1);
|
||||
colsPerRecord = (int)this.GetArgumentAsLong(argv, "-l", 1, 1024, 3);
|
||||
configDir = this.GetArgumentAsString(argv, "-c", "C:/TDengine/cfg");
|
||||
useStable = this.GetArgumentAsFlag(argv, "-M");
|
||||
|
||||
replica = (short)this.GetArgumentAsLong(argv, "-a", 1, 5, 1);
|
||||
methodOfDelete = (short)this.GetArgumentAsLong(argv, "-D", 0, 3, 0);
|
||||
numOfThreads = (short)this.GetArgumentAsLong(argv, "-T", 1, 10000, 1);
|
||||
order = this.GetArgumentAsFlag(argv, "-O");
|
||||
rateOfOutorder = this.GetArgumentAsLong(argv, "-R", 0, 100, 0);
|
||||
|
||||
skipReadKey = this.GetArgumentAsFlag(argv, "-y");
|
||||
verbose = this.GetArgumentAsFlag(argv, "-v");
|
||||
|
||||
Console.Write("###################################################################\n");
|
||||
Console.Write("# Server IP: {0}\n", host);
|
||||
Console.Write("# User: {0}\n", user);
|
||||
Console.Write("# Password: {0}\n", password);
|
||||
Console.Write("# Number of Columns per record: {0}\n", colsPerRecord);
|
||||
Console.Write("# Number of Threads: {0}\n", numOfThreads);
|
||||
Console.Write("# Number of Tables: {0}\n", numOfTables);
|
||||
Console.Write("# Number of Data per Table: {0}\n", recordsPerTable);
|
||||
Console.Write("# Records/Request: {0}\n", recordsPerRequest);
|
||||
Console.Write("# Database name: {0}\n", dbName);
|
||||
Console.Write("# Replica: {0}\n", replica);
|
||||
Console.Write("# Use STable: {0}\n", useStable);
|
||||
Console.Write("# Table prefix: {0}\n", tablePrefix);
|
||||
Console.Write("# Data order: {0}\n", order);
|
||||
Console.Write("# Data out of order rate: {0}\n", rateOfOutorder);
|
||||
Console.Write("# Delete method: {0}\n", methodOfDelete);
|
||||
Console.Write("# Query Mode: {0}\n", queryMode);
|
||||
Console.Write("# Insert Only: {0}\n", isInsertOnly);
|
||||
Console.Write("# Verbose output {0}\n", verbose);
|
||||
Console.Write("# Test time: {0}\n", DateTime.Now.ToString("h:mm:ss tt"));
|
||||
|
||||
Console.Write("###################################################################\n");
|
||||
|
||||
if (skipReadKey == false)
|
||||
{
|
||||
Console.Write("Press any key to continue..\n");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
||||
public bool GetArgumentAsFlag(String[] argv, String argName)
|
||||
{
|
||||
int argc = argv.Length;
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
if (argName == argv[i])
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public long GetArgumentAsLong(String[] argv, String argName, int minVal, long maxVal, int defaultValue)
|
||||
{
|
||||
int argc = argv.Length;
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
if (argName != argv[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (i < argc - 1)
|
||||
{
|
||||
String tmp = argv[i + 1];
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
Console.WriteLine("option {0:G} requires an argument", tmp);
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
long tmpVal = Convert.ToInt64(tmp);
|
||||
if (tmpVal < minVal || tmpVal > maxVal)
|
||||
{
|
||||
Console.WriteLine("option {0:G} should in range [{1:G}, {2:G}]", argName, minVal, maxVal);
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
return tmpVal;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public String GetArgumentAsString(String[] argv, String argName, String defaultValue)
|
||||
{
|
||||
int argc = argv.Length;
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
if (argName != argv[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (i < argc - 1)
|
||||
{
|
||||
String tmp = argv[i + 1];
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
Console.WriteLine("option {0:G} requires an argument", tmp);
|
||||
ExitProgram();
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
static void ExitProgram()
|
||||
{
|
||||
TDengine.Cleanup();
|
||||
System.Environment.Exit(0);
|
||||
}
|
||||
|
||||
private void DebugPrintFormat(string format, params object[] parameters)
|
||||
{
|
||||
if (verbose == true)
|
||||
{
|
||||
Console.Write(format, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
private void DebugPrint(string str)
|
||||
{
|
||||
if (verbose == true)
|
||||
{
|
||||
Console.Write(str);
|
||||
}
|
||||
}
|
||||
|
||||
public void InitTDengine()
|
||||
{
|
||||
TDengine.Options((int)TDengineInitOption.TDDB_OPTION_CONFIGDIR, this.configDir);
|
||||
TDengine.Options((int)TDengineInitOption.TDDB_OPTION_SHELL_ACTIVITY_TIMER, "60");
|
||||
TDengine.Init();
|
||||
DebugPrint("TDengine Initialization finished\n");
|
||||
}
|
||||
|
||||
public void ConnectTDengine()
|
||||
{
|
||||
string db = "";
|
||||
DebugPrintFormat("host:{0} user:{1}, pass:{2}; db:{3}, port:{4}\n",
|
||||
this.host, this.user, this.password, db, this.port);
|
||||
this.conn = TDengine.Connect(this.host, this.user, this.password, db, this.port);
|
||||
if (this.conn == IntPtr.Zero)
|
||||
{
|
||||
Console.WriteLine("Connect to TDengine failed");
|
||||
ExitProgram();
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugPrint("Connect to TDengine success\n");
|
||||
}
|
||||
}
|
||||
|
||||
public void CreateTablesByThreads()
|
||||
{
|
||||
Thread[] threadArr = new Thread[numOfThreads];
|
||||
|
||||
long quotition = numOfTables / numOfThreads;
|
||||
if (quotition < 1)
|
||||
{
|
||||
numOfThreads = numOfTables;
|
||||
quotition = 1;
|
||||
}
|
||||
|
||||
long remainder = 0;
|
||||
if (numOfThreads != 0)
|
||||
{
|
||||
remainder = numOfTables % numOfThreads;
|
||||
}
|
||||
|
||||
long last = 0;
|
||||
|
||||
for (int i = 0; i < numOfThreads; i++)
|
||||
{
|
||||
CreateTableThread createTableThread = new CreateTableThread();
|
||||
createTableThread.id = i;
|
||||
createTableThread.verbose = verbose;
|
||||
createTableThread.dbName = this.dbName;
|
||||
createTableThread.tablePrefix = this.tablePrefix;
|
||||
createTableThread.useStable = useStable;
|
||||
if (useStable)
|
||||
{
|
||||
createTableThread.stableName = stableName;
|
||||
}
|
||||
createTableThread.conn = conn;
|
||||
|
||||
createTableThread.start = last;
|
||||
if (i < remainder)
|
||||
{
|
||||
createTableThread.end = last + quotition;
|
||||
}
|
||||
else
|
||||
{
|
||||
createTableThread.end = last + quotition - 1;
|
||||
}
|
||||
last = createTableThread.end + 1;
|
||||
|
||||
threadArr[i] = new Thread(createTableThread.ThreadMain);
|
||||
threadArr[i].Start();
|
||||
threadArr[i].Join();
|
||||
}
|
||||
}
|
||||
|
||||
public void dropDatabase()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("DROP DATABASE IF EXISTS ").Append(this.dbName);
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
{
|
||||
DebugPrint(sql.ToString() + " success\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void CreateDb()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("CREATE DATABASE IF NOT EXISTS ").Append(this.dbName).Append(" replica ").Append(this.replica);
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
{
|
||||
DebugPrint(sql.ToString() + " success\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
||||
public void CreateStable()
|
||||
{
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.Clear();
|
||||
sql.Append("CREATE TABLE IF NOT EXISTS ").
|
||||
Append(this.dbName).Append(".").Append(this.stableName).
|
||||
Append("(ts timestamp, v1 bool, v2 tinyint, v3 smallint, v4 int, v5 bigint, v6 float, v7 double, v8 binary(10), v9 nchar(10)) tags(t1 int)");
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
{
|
||||
DebugPrint(sql.ToString() + " success\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
||||
public void InsertByThreads()
|
||||
{
|
||||
Thread[] threadArr = new Thread[numOfThreads];
|
||||
|
||||
long quotition = numOfTables / numOfThreads;
|
||||
if (quotition < 1)
|
||||
{
|
||||
numOfThreads = numOfTables;
|
||||
quotition = 1;
|
||||
}
|
||||
|
||||
long remainder = 0;
|
||||
if (numOfThreads != 0)
|
||||
{
|
||||
remainder = numOfTables % numOfThreads;
|
||||
}
|
||||
|
||||
long last = 0;
|
||||
|
||||
for (int i = 0; i < numOfThreads; i++)
|
||||
{
|
||||
InsertDataThread insertThread = new InsertDataThread();
|
||||
insertThread.id = i;
|
||||
insertThread.recordsPerTable = recordsPerTable;
|
||||
insertThread.batchRows = batchRows;
|
||||
insertThread.numOfTables = numOfTables;
|
||||
insertThread.verbose = verbose;
|
||||
insertThread.dbName = this.dbName;
|
||||
insertThread.tablePrefix = this.tablePrefix;
|
||||
if (useStable)
|
||||
{
|
||||
// insertThread.stableName = stableName;
|
||||
}
|
||||
insertThread.conn = conn;
|
||||
|
||||
insertThread.start = last;
|
||||
if (i < remainder)
|
||||
{
|
||||
insertThread.end = last + quotition;
|
||||
}
|
||||
else
|
||||
{
|
||||
insertThread.end = last + quotition - 1;
|
||||
}
|
||||
last = insertThread.end + 1;
|
||||
|
||||
threadArr[i] = new Thread(insertThread.ThreadMain);
|
||||
threadArr[i].Start();
|
||||
threadArr[i].Join();
|
||||
}
|
||||
}
|
||||
|
||||
public void ExecuteQuery()
|
||||
{
|
||||
// System.DateTime start = new System.DateTime();
|
||||
long queryRows = 0;
|
||||
|
||||
for (int i = 0; i < 1/*this.numOfTables*/; ++i)
|
||||
{
|
||||
String sql = "select * from " + this.dbName + "." + tablePrefix + i;
|
||||
// Console.WriteLine(sql);
|
||||
|
||||
IntPtr res = TDengine.Query(conn, sql);
|
||||
if (res == IntPtr.Zero)
|
||||
{
|
||||
Console.WriteLine(sql + " failure, reason: " + TDengine.Error(res));
|
||||
ExitProgram();
|
||||
}
|
||||
|
||||
int fieldCount = TDengine.FieldCount(res);
|
||||
// Console.WriteLine("field count: " + fieldCount);
|
||||
|
||||
List<TDengineMeta> metas = TDengine.FetchFields(res);
|
||||
for (int j = 0; j < metas.Count; j++)
|
||||
{
|
||||
TDengineMeta meta = (TDengineMeta)metas[j];
|
||||
// Console.WriteLine("index:" + j + ", type:" + meta.type + ", typename:" + meta.TypeName() + ", name:" + meta.name + ", size:" + meta.size);
|
||||
}
|
||||
|
||||
IntPtr rowdata;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
while ((rowdata = TDengine.FetchRows(res)) != IntPtr.Zero)
|
||||
{
|
||||
queryRows++;
|
||||
for (int fields = 0; fields < fieldCount; ++fields)
|
||||
{
|
||||
TDengineMeta meta = metas[fields];
|
||||
int offset = IntPtr.Size * fields;
|
||||
IntPtr data = Marshal.ReadIntPtr(rowdata, offset);
|
||||
|
||||
builder.Append("---");
|
||||
|
||||
if (data == IntPtr.Zero)
|
||||
{
|
||||
builder.Append("NULL");
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ((TDengineDataType)meta.type)
|
||||
{
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BOOL:
|
||||
bool v1 = Marshal.ReadByte(data) == 0 ? false : true;
|
||||
builder.Append(v1);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
|
||||
byte v2 = Marshal.ReadByte(data);
|
||||
builder.Append(v2);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
|
||||
short v3 = Marshal.ReadInt16(data);
|
||||
builder.Append(v3);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_INT:
|
||||
int v4 = Marshal.ReadInt32(data);
|
||||
builder.Append(v4);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
|
||||
long v5 = Marshal.ReadInt64(data);
|
||||
builder.Append(v5);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
|
||||
float v6 = (float)Marshal.PtrToStructure(data, typeof(float));
|
||||
builder.Append(v6);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
|
||||
double v7 = (double)Marshal.PtrToStructure(data, typeof(double));
|
||||
builder.Append(v7);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_BINARY:
|
||||
string v8 = Marshal.PtrToStringAnsi(data);
|
||||
builder.Append(v8);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
long v9 = Marshal.ReadInt64(data);
|
||||
builder.Append(v9);
|
||||
break;
|
||||
case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
|
||||
string v10 = Marshal.PtrToStringAnsi(data);
|
||||
builder.Append(v10);
|
||||
break;
|
||||
}
|
||||
}
|
||||
builder.Append("---");
|
||||
|
||||
if (queryRows <= 10)
|
||||
{
|
||||
Console.WriteLine(builder.ToString());
|
||||
}
|
||||
builder.Clear();
|
||||
}
|
||||
|
||||
if (TDengine.ErrorNo(res) != 0)
|
||||
{
|
||||
Console.Write("Query is not complete, Error {0:G}",
|
||||
TDengine.ErrorNo(res), TDengine.Error(res));
|
||||
}
|
||||
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
/*
|
||||
System.DateTime end = new System.DateTime();
|
||||
TimeSpan ts = end - start;
|
||||
|
||||
Console.Write("Total {0:G} rows inserted, {1:G} rows query, time spend {2:G} seconds.\n"
|
||||
, this.rowsInserted, queryRows, ts.TotalSeconds);
|
||||
*/
|
||||
}
|
||||
|
||||
public void CloseConnection()
|
||||
{
|
||||
if (this.conn != IntPtr.Zero)
|
||||
{
|
||||
TDengine.Close(this.conn);
|
||||
}
|
||||
}
|
||||
|
||||
// Main entry
|
||||
static void Main(string[] args)
|
||||
{
|
||||
PrintHelp(args);
|
||||
|
||||
TDengineTest tester = new TDengineTest();
|
||||
tester.ReadArgument(args);
|
||||
|
||||
tester.InitTDengine();
|
||||
tester.ConnectTDengine();
|
||||
|
||||
if (tester.isInsertOnly == false)
|
||||
{
|
||||
tester.dropDatabase();
|
||||
tester.CreateDb();
|
||||
|
||||
|
||||
if (tester.useStable == true)
|
||||
{
|
||||
tester.CreateStable();
|
||||
}
|
||||
|
||||
tester.CreateTablesByThreads();
|
||||
}
|
||||
|
||||
Stopwatch watch = Stopwatch.StartNew();
|
||||
tester.InsertByThreads();
|
||||
watch.Stop();
|
||||
double elapsedMs = watch.Elapsed.TotalMilliseconds;
|
||||
|
||||
Console.WriteLine("Spent {0} seconds to insert {1} records with {2} record(s) per request: {3} records/second",
|
||||
elapsedMs / 1000,
|
||||
tester.recordsPerTable * tester.numOfTables,
|
||||
tester.batchRows,
|
||||
(tester.recordsPerTable * tester.numOfTables * 1000) / elapsedMs);
|
||||
|
||||
tester.ExecuteQuery();
|
||||
tester.CloseConnection();
|
||||
|
||||
Console.WriteLine("End.");
|
||||
}
|
||||
|
||||
public class InsertDataThread
|
||||
{
|
||||
public long id { set; get; }
|
||||
public long start { set; get; }
|
||||
public long end { set; get; }
|
||||
public string dbName { set; get; }
|
||||
public IntPtr conn { set; get; }
|
||||
public string tablePrefix { set; get; }
|
||||
// public string stableName { set; get; }
|
||||
public long recordsPerTable { set; get; }
|
||||
public long batchRows { set; get; }
|
||||
public long numOfTables { set; get; }
|
||||
public bool verbose { set; get; }
|
||||
|
||||
private void DebugPrintFormat(string format, params object[] parameters)
|
||||
{
|
||||
if (verbose == true)
|
||||
{
|
||||
Console.Write(format, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
private void DebugPrint(string str)
|
||||
{
|
||||
if (verbose == true)
|
||||
{
|
||||
Console.Write(str);
|
||||
}
|
||||
}
|
||||
|
||||
public void ThreadMain()
|
||||
{
|
||||
DebugPrintFormat("InsertDataThread {0} from {1} to {2}\n", id, start, end);
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
int h = now.Hour;
|
||||
int m = now.Minute;
|
||||
int s = now.Second;
|
||||
|
||||
long baseTimestamp = 1609430400000; // 2021/01/01 0:0:0
|
||||
DebugPrintFormat("beginTime is {0} + {1}h:{2}m:{3}s\n", baseTimestamp, h, m, s);
|
||||
long beginTimestamp = baseTimestamp + ((h*60 + m) * 60 + s) * 1000;
|
||||
|
||||
long rowsInserted = 0;
|
||||
|
||||
// System.DateTime startTime = new System.DateTime();
|
||||
long i = 0;
|
||||
while (i < recordsPerTable)
|
||||
{
|
||||
for (long table = start; table <= end; ++table)
|
||||
{
|
||||
long inserted = i;
|
||||
|
||||
sql.Clear();
|
||||
sql.Append("INSERT INTO ").
|
||||
Append(this.dbName).Append(".").Append(this.tablePrefix).Append(table).
|
||||
Append(" VALUES");
|
||||
if (recordsPerTable < batchRows)
|
||||
{
|
||||
batchRows = recordsPerTable;
|
||||
}
|
||||
for (int batch = 0; batch < batchRows; ++batch)
|
||||
{
|
||||
sql.Append("(")
|
||||
.Append(beginTimestamp + i + batch)
|
||||
.Append(", 1, 2, 3,")
|
||||
.Append(i + batch)
|
||||
.Append(", 5, 6, 7, 'abc', 'def')");
|
||||
|
||||
}
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res == IntPtr.Zero)
|
||||
{
|
||||
DebugPrint(sql.ToString() + " failure, reason: " + TDengine.Error(res) + "\n");
|
||||
}
|
||||
|
||||
inserted += this.batchRows;
|
||||
|
||||
int affectRows = TDengine.AffectRows(res);
|
||||
rowsInserted += affectRows;
|
||||
|
||||
TDengine.FreeResult(res);
|
||||
if (table == end)
|
||||
{
|
||||
i = inserted;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateTableThread
|
||||
{
|
||||
public long id { set; get; }
|
||||
public long start { set; get; }
|
||||
public long end { set; get; }
|
||||
public string dbName { set; get; }
|
||||
public IntPtr conn { set; get; }
|
||||
public string tablePrefix { set; get; }
|
||||
public string stableName { set; get; }
|
||||
public bool verbose { set; get; }
|
||||
public bool useStable { set; get; }
|
||||
|
||||
private void DebugPrintFormat(string format, params object[] parameters)
|
||||
{
|
||||
if (verbose == true)
|
||||
{
|
||||
Console.Write(format, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
private void DebugPrint(string str)
|
||||
{
|
||||
if (verbose == true)
|
||||
{
|
||||
Console.Write(str);
|
||||
}
|
||||
}
|
||||
|
||||
public void ThreadMain()
|
||||
{
|
||||
DebugPrintFormat("CreateTable {0} from {1} to {2}\n", id, start, end);
|
||||
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
for (long tableId = start; tableId <= end; tableId++)
|
||||
{
|
||||
sql.Clear();
|
||||
sql = sql.Append("CREATE TABLE IF NOT EXISTS ").
|
||||
Append(this.dbName).Append(".").Append(this.tablePrefix).Append(tableId);
|
||||
if (useStable == true)
|
||||
{
|
||||
sql = sql.Append(" USING ").Append(this.dbName).Append(".").Append(this.stableName).
|
||||
Append(" TAGS(").Append(tableId).Append(")");
|
||||
}
|
||||
else
|
||||
{
|
||||
sql = sql.Append("(ts timestamp, v1 bool, v2 tinyint, v3 smallint, v4 int, v5 bigint, v6 float, v7 double, v8 binary(10), v9 nchar(10))");
|
||||
}
|
||||
IntPtr res = TDengine.Query(this.conn, sql.ToString());
|
||||
if (res != IntPtr.Zero)
|
||||
{
|
||||
DebugPrint(sql.ToString() + " success\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugPrint(sql.ToString() + " failure, reason: " + TDengine.Error(res) + "\n");
|
||||
ExitProgram();
|
||||
}
|
||||
TDengine.FreeResult(res);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,17 +5,12 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>jdbcChecker</artifactId>
|
||||
<artifactId>JDBCDemo</artifactId>
|
||||
<version>SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
|
@ -23,7 +18,7 @@
|
|||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.taosdata.example.JdbcChecker</mainClass>
|
||||
<mainClass>com.taosdata.example.JDBCDemo</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
|
@ -49,6 +44,7 @@
|
|||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
@ -56,18 +52,8 @@
|
|||
<dependency>
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>taos-jdbcdriver</artifactId>
|
||||
<version>2.0.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
<version>2.0.15</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,29 +1,37 @@
|
|||
# How to Run the JDBC Demo Code On A Linux OS
|
||||
# How to Run the JDBC Demo Code On Linux OS
|
||||
TDengine's JDBC demo project is organized in a Maven way so that users can easily compile, package and run the project. If you don't have Maven on your server, you may install it using
|
||||
<pre>sudo apt-get install maven</pre>
|
||||
```
|
||||
sudo apt-get install maven
|
||||
```
|
||||
|
||||
## Install TDengine Client
|
||||
Make sure you have already installed a tdengine client on your current develop environment.
|
||||
Download the tdengine package on our website: ``https://www.taosdata.com/cn/all-downloads/`` and install the client.
|
||||
|
||||
## How to run jdbcChecker
|
||||
<pre>mvn clean compile exec:java -Dexec.mainClass="com.taosdata.example.JdbcChecker" -Dexec.args="-host localhost"</pre>
|
||||
|
||||
## How to run jdbcTaosDemo
|
||||
## Run jdbcDemo using mvn plugin
|
||||
run command:
|
||||
<pre> mvn clean compile exec:java -Dexec.mainClass="com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo"</pre>
|
||||
```
|
||||
mvn clean compile exec:java -Dexec.mainClass="com.taosdata.example.JdbcDemo"
|
||||
```
|
||||
|
||||
and run with your customed args
|
||||
<pre>mvn clean compile exec:java -Dexec.mainClass="com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo" -Dexec.args="-host localhost"</pre>
|
||||
```
|
||||
mvn clean compile exec:java -Dexec.mainClass="com.taosdata.example.JdbcDemo" -Dexec.args="-host [HOSTNAME]"
|
||||
```
|
||||
|
||||
## Compile the Demo Code and Run It
|
||||
To compile taos-jdbcdriver, go to the source directory ``TDengine/src/connector/jdbc`` and execute
|
||||
```
|
||||
mvn clean package -Dmaven.test.skip=true
|
||||
```
|
||||
|
||||
To compile the demo project, go to the source directory ``TDengine/tests/examples/JDBC/JDBCDemo`` and execute
|
||||
|
||||
<pre>
|
||||
```
|
||||
mvn clean package assembly:single
|
||||
</pre>
|
||||
```
|
||||
|
||||
The ``pom.xml`` is configured to package all the dependencies into one executable jar file.
|
||||
To run JDBCDemo.jar, go to ``TDengine/tests/examples/JDBC/JDBCDemo`` and execute
|
||||
```
|
||||
java -Djava.ext.dirs=../../../../src/connector/jdbc/target:$JAVA_HOME/jre/lib/ext -jar target/JDBCDemo-SNAPSHOT-jar-with-dependencies.jar -host [HOSTNAME]
|
||||
```
|
||||
|
||||
To run it, go to ``examples/JDBC/JDBCDemo/target`` and execute
|
||||
<pre>java -jar jdbcChecker-SNAPSHOT-jar-with-dependencies.jar -host localhost</pre>
|
||||
|
|
|
@ -1,56 +1,77 @@
|
|||
package com.taosdata.example;
|
||||
|
||||
import com.taosdata.jdbc.TSDBDriver;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class JdbcChecker {
|
||||
public class JDBCDemo {
|
||||
private static String host;
|
||||
private static String dbName = "test";
|
||||
private static String tbName = "weather";
|
||||
private static String driverType = "jni";
|
||||
private static final String dbName = "test";
|
||||
private static final String tbName = "weather";
|
||||
private Connection connection;
|
||||
|
||||
/**
|
||||
* get connection
|
||||
**/
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1)
|
||||
host = args[++i];
|
||||
if ("-driverType".equalsIgnoreCase(args[i]) && i < args.length - 1) {
|
||||
driverType = args[++i];
|
||||
if (!"jni".equalsIgnoreCase(driverType) && !"restful".equalsIgnoreCase(driverType))
|
||||
printHelp();
|
||||
}
|
||||
}
|
||||
|
||||
if (host == null) {
|
||||
printHelp();
|
||||
}
|
||||
|
||||
JDBCDemo demo = new JDBCDemo();
|
||||
demo.init();
|
||||
demo.createDatabase();
|
||||
demo.useDatabase();
|
||||
demo.dropTable();
|
||||
demo.createTable();
|
||||
demo.insert();
|
||||
demo.select();
|
||||
demo.dropTable();
|
||||
demo.close();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// get connection
|
||||
try {
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
|
||||
if (driverType.equals("restful")) {
|
||||
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
|
||||
url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
|
||||
} else {
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
}
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
|
||||
properties.setProperty("host", host);
|
||||
properties.setProperty("charset", "UTF-8");
|
||||
properties.setProperty("locale", "en_US.UTF-8");
|
||||
properties.setProperty("timezone", "UTC-8");
|
||||
System.out.println("get connection starting...");
|
||||
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
|
||||
connection = DriverManager.getConnection(url, properties);
|
||||
if (connection != null)
|
||||
System.out.println("[ OK ] Connection established.");
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
throw new RuntimeException("connection failed: " + host);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* create database
|
||||
*/
|
||||
private void createDatabase() {
|
||||
String sql = "create database if not exists " + dbName;
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* use database
|
||||
*/
|
||||
private void useDatabase() {
|
||||
String sql = "use " + dbName;
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* select
|
||||
*/
|
||||
private void checkSelect() {
|
||||
private void select() {
|
||||
final String sql = "select * from test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
@ -79,40 +100,21 @@ public class JdbcChecker {
|
|||
}
|
||||
}
|
||||
|
||||
private String formatString(String str) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int blankCnt = (26 - str.length()) / 2;
|
||||
for (int j = 0; j < blankCnt; j++)
|
||||
sb.append(" ");
|
||||
sb.append(str);
|
||||
for (int j = 0; j < blankCnt; j++)
|
||||
sb.append(" ");
|
||||
sb.append("|");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* insert
|
||||
*/
|
||||
private void checkInsert() {
|
||||
private void insert() {
|
||||
final String sql = "insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* create table
|
||||
*/
|
||||
private void createTable() {
|
||||
final String sql = "create table if not exists " + dbName + "." + tbName + " (ts timestamp, temperature float, humidity int)";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
private final void printSql(String sql, boolean succeed, long cost) {
|
||||
private void printSql(String sql, boolean succeed, long cost) {
|
||||
System.out.println("[ " + (succeed ? "OK" : "ERROR!") + " ] time cost: " + cost + " ms, execute statement ====> " + sql);
|
||||
}
|
||||
|
||||
private final void exuete(String sql) {
|
||||
private void exuete(String sql) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
long start = System.currentTimeMillis();
|
||||
boolean execute = statement.execute(sql);
|
||||
|
@ -120,7 +122,7 @@ public class JdbcChecker {
|
|||
printSql(sql, execute, (end - start));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,39 +137,15 @@ public class JdbcChecker {
|
|||
}
|
||||
}
|
||||
|
||||
private void checkDropTable() {
|
||||
private void dropTable() {
|
||||
final String sql = "drop table if exists " + dbName + "." + tbName + "";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1) {
|
||||
host = args[++i];
|
||||
}
|
||||
if ("-db".equalsIgnoreCase(args[i]) && i < args.length - 1) {
|
||||
dbName = args[++i];
|
||||
}
|
||||
if ("-t".equalsIgnoreCase(args[i]) && i < args.length - 1) {
|
||||
tbName = args[++i];
|
||||
}
|
||||
}
|
||||
|
||||
if (host == null) {
|
||||
System.out.println("Usage: java -jar JDBCConnectorChecker.jar -host <hostname>");
|
||||
return;
|
||||
}
|
||||
|
||||
JdbcChecker checker = new JdbcChecker();
|
||||
checker.init();
|
||||
checker.createDatabase();
|
||||
checker.useDatabase();
|
||||
checker.checkDropTable();
|
||||
checker.createTable();
|
||||
checker.checkInsert();
|
||||
checker.checkSelect();
|
||||
checker.checkDropTable();
|
||||
checker.close();
|
||||
private static void printHelp() {
|
||||
System.out.println("Usage: java -jar JdbcDemo.jar -host <hostname> -driverType <jni|restful>");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,357 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo;
|
||||
|
||||
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||
import com.taosdata.example.jdbcTaosdemo.task.CreateTableTask;
|
||||
import com.taosdata.example.jdbcTaosdemo.task.InsertTableDatetimeTask;
|
||||
import com.taosdata.example.jdbcTaosdemo.task.InsertTableTask;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.TimeStampUtil;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class JdbcTaosdemo {
|
||||
|
||||
private static Logger logger = Logger.getLogger(JdbcTaosdemo.class);
|
||||
private final JdbcTaosdemoConfig config;
|
||||
private Connection connection;
|
||||
|
||||
public JdbcTaosdemo(JdbcTaosdemoConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// parse config from args
|
||||
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
|
||||
|
||||
boolean isHelp = Arrays.asList(args).contains("--help");
|
||||
if (isHelp) {
|
||||
JdbcTaosdemoConfig.printHelp();
|
||||
return;
|
||||
}
|
||||
if (config.getHost() == null) {
|
||||
JdbcTaosdemoConfig.printHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
JdbcTaosdemo taosdemo = new JdbcTaosdemo(config);
|
||||
// establish connection
|
||||
taosdemo.init();
|
||||
// drop database
|
||||
taosdemo.dropDatabase();
|
||||
// create database
|
||||
taosdemo.createDatabase();
|
||||
// use db
|
||||
taosdemo.useDatabase();
|
||||
// create super table
|
||||
taosdemo.createSuperTable();
|
||||
// create sub tables
|
||||
taosdemo.createTableMultiThreads();
|
||||
|
||||
boolean infinite = Arrays.asList(args).contains("--infinite");
|
||||
if (infinite) {
|
||||
logger.info("!!! Infinite Insert Mode Started. !!!");
|
||||
taosdemo.insertInfinite();
|
||||
} else {
|
||||
// insert into table
|
||||
taosdemo.insertMultiThreads();
|
||||
// select from sub table
|
||||
taosdemo.selectFromTableLimit();
|
||||
taosdemo.selectCountFromTable();
|
||||
taosdemo.selectAvgMinMaxFromTable();
|
||||
// select last from
|
||||
taosdemo.selectLastFromTable();
|
||||
// select from super table
|
||||
taosdemo.selectFromSuperTableLimit();
|
||||
taosdemo.selectCountFromSuperTable();
|
||||
taosdemo.selectAvgMinMaxFromSuperTable();
|
||||
//select avg ,max from stb where tag
|
||||
taosdemo.selectAvgMinMaxFromSuperTableWhereTag();
|
||||
//select last from stb where location = ''
|
||||
taosdemo.selectLastFromSuperTableWhere();
|
||||
// select group by
|
||||
taosdemo.selectGroupBy();
|
||||
// select like
|
||||
taosdemo.selectLike();
|
||||
// select where ts >= ts<=
|
||||
taosdemo.selectLastOneHour();
|
||||
taosdemo.selectLastOneDay();
|
||||
taosdemo.selectLastOneWeek();
|
||||
taosdemo.selectLastOneMonth();
|
||||
taosdemo.selectLastOneYear();
|
||||
|
||||
// drop super table
|
||||
if (config.isDeleteTable())
|
||||
taosdemo.dropSuperTable();
|
||||
taosdemo.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* establish the connection
|
||||
*/
|
||||
private void init() {
|
||||
try {
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
connection = ConnectionFactory.build(config);
|
||||
if (connection != null)
|
||||
logger.info("[ OK ] Connection established.");
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new RuntimeException("connection failed: " + config.getHost());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* create database
|
||||
*/
|
||||
private void createDatabase() {
|
||||
String sql = SqlSpeller.createDatabaseSQL(config.getDbName(), config.getKeep(), config.getDays());
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* drop database
|
||||
*/
|
||||
private void dropDatabase() {
|
||||
String sql = SqlSpeller.dropDatabaseSQL(config.getDbName());
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* use database
|
||||
*/
|
||||
private void useDatabase() {
|
||||
String sql = SqlSpeller.useDatabaseSQL(config.getDbName());
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* create super table
|
||||
*/
|
||||
private void createSuperTable() {
|
||||
String sql = SqlSpeller.createSuperTableSQL(config.getStbName());
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* create table use super table with multi threads
|
||||
*/
|
||||
private void createTableMultiThreads() {
|
||||
try {
|
||||
final int tableSize = config.getNumberOfTable() / config.getNumberOfThreads();
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
for (int i = 0; i < config.getNumberOfThreads(); i++) {
|
||||
Thread thread = new Thread(new CreateTableTask(config, i * tableSize, tableSize), "Thread-" + i);
|
||||
threads.add(thread);
|
||||
thread.start();
|
||||
}
|
||||
for (Thread thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
logger.info("<<< Multi Threads create table finished.");
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* insert data infinitely
|
||||
*/
|
||||
private void insertInfinite() {
|
||||
try {
|
||||
final long startDatetime = TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000");
|
||||
final long finishDatetime = TimeStampUtil.datetimeToLong("2030-01-01 00:00:00.000");
|
||||
|
||||
final int tableSize = config.getNumberOfTable() / config.getNumberOfThreads();
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
for (int i = 0; i < config.getNumberOfThreads(); i++) {
|
||||
Thread thread = new Thread(new InsertTableDatetimeTask(config, i * tableSize, tableSize, startDatetime, finishDatetime), "Thread-" + i);
|
||||
threads.add(thread);
|
||||
thread.start();
|
||||
}
|
||||
for (Thread thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
logger.info("<<< Multi Threads insert table finished.");
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void insertMultiThreads() {
|
||||
try {
|
||||
final int tableSize = config.getNumberOfTable() / config.getNumberOfThreads();
|
||||
final int numberOfRecordsPerTable = config.getNumberOfRecordsPerTable();
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
for (int i = 0; i < config.getNumberOfThreads(); i++) {
|
||||
Thread thread = new Thread(new InsertTableTask(config, i * tableSize, tableSize, numberOfRecordsPerTable), "Thread-" + i);
|
||||
threads.add(thread);
|
||||
thread.start();
|
||||
}
|
||||
for (Thread thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
logger.info("<<< Multi Threads insert table finished.");
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void selectFromTableLimit() {
|
||||
String sql = SqlSpeller.selectFromTableLimitSQL(config.getDbName(), config.getTbPrefix(), 1, 10, 0);
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectCountFromTable() {
|
||||
String sql = SqlSpeller.selectCountFromTableSQL(config.getDbName(), config.getTbPrefix(), 1);
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectAvgMinMaxFromTable() {
|
||||
String sql = SqlSpeller.selectAvgMinMaxFromTableSQL("current", config.getDbName(), config.getTbPrefix(), 1);
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectLastFromTable() {
|
||||
String sql = SqlSpeller.selectLastFromTableSQL(config.getDbName(), config.getTbPrefix(), 1);
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectFromSuperTableLimit() {
|
||||
String sql = SqlSpeller.selectFromSuperTableLimitSQL(config.getDbName(), config.getStbName(), 10, 0);
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectCountFromSuperTable() {
|
||||
String sql = SqlSpeller.selectCountFromSuperTableSQL(config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectAvgMinMaxFromSuperTable() {
|
||||
String sql = SqlSpeller.selectAvgMinMaxFromSuperTableSQL("current", config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectAvgMinMaxFromSuperTableWhereTag() {
|
||||
String sql = SqlSpeller.selectAvgMinMaxFromSuperTableWhere("current", config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectLastFromSuperTableWhere() {
|
||||
String sql = SqlSpeller.selectLastFromSuperTableWhere("current", config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectGroupBy() {
|
||||
String sql = SqlSpeller.selectGroupBy("current", config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectLike() {
|
||||
String sql = SqlSpeller.selectLike(config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectLastOneHour() {
|
||||
String sql = SqlSpeller.selectLastOneHour(config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectLastOneDay() {
|
||||
String sql = SqlSpeller.selectLastOneDay(config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectLastOneWeek() {
|
||||
String sql = SqlSpeller.selectLastOneWeek(config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectLastOneMonth() {
|
||||
String sql = SqlSpeller.selectLastOneMonth(config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void selectLastOneYear() {
|
||||
String sql = SqlSpeller.selectLastOneYear(config.getDbName(), config.getStbName());
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
|
||||
private void close() {
|
||||
try {
|
||||
if (connection != null) {
|
||||
this.connection.close();
|
||||
logger.info("connection closed.");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* drop super table
|
||||
*/
|
||||
private void dropSuperTable() {
|
||||
String sql = SqlSpeller.dropSuperTableSQL(config.getDbName(), config.getStbName());
|
||||
execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* execute sql, use this method when sql is create, alter, drop..
|
||||
*/
|
||||
private void execute(String sql) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
long start = System.currentTimeMillis();
|
||||
boolean execute = statement.execute(sql);
|
||||
long end = System.currentTimeMillis();
|
||||
printSql(sql, execute, (end - start));
|
||||
} catch (SQLException e) {
|
||||
logger.error("ERROR execute SQL ===> " + sql);
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printSql(String sql, boolean succeed, long cost) {
|
||||
System.out.println("[ " + (succeed ? "OK" : "ERROR!") + " ] time cost: " + cost + " ms, execute statement ====> " + sql);
|
||||
}
|
||||
|
||||
private void executeQuery(String sql) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
long start = System.currentTimeMillis();
|
||||
ResultSet resultSet = statement.executeQuery(sql);
|
||||
long end = System.currentTimeMillis();
|
||||
printSql(sql, true, (end - start));
|
||||
printResult(resultSet);
|
||||
} catch (SQLException e) {
|
||||
logger.error("ERROR execute SQL ===> " + sql);
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printResult(ResultSet resultSet) throws SQLException {
|
||||
ResultSetMetaData metaData = resultSet.getMetaData();
|
||||
while (resultSet.next()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
String columnLabel = metaData.getColumnLabel(i);
|
||||
String value = resultSet.getString(i);
|
||||
sb.append(columnLabel + ": " + value + "\t");
|
||||
}
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,153 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo.domain;
|
||||
|
||||
public final class JdbcTaosdemoConfig {
|
||||
|
||||
//The host to connect to TDengine. Must insert one
|
||||
private String host;
|
||||
//The TCP/IP port number to use for the connection. Default is 6030.
|
||||
private int port = 6030;
|
||||
//The TDengine user name to use when connecting to the server. Default is 'root'
|
||||
private String user = "root";
|
||||
//The password to use when connecting to the server. Default is 'taosdata'
|
||||
private String password = "taosdata";
|
||||
|
||||
//Destination database. Default is 'test'
|
||||
private String dbName = "test";
|
||||
//keep
|
||||
private int keep = 36500;
|
||||
//days
|
||||
private int days = 120;
|
||||
|
||||
//Super table Name. Default is 'meters'
|
||||
private String stbName = "meters";
|
||||
//Table name prefix. Default is 'd'
|
||||
private String tbPrefix = "d";
|
||||
//The number of tables. Default is 10.
|
||||
private int numberOfTable = 10;
|
||||
//The number of records per table. Default is 2
|
||||
private int numberOfRecordsPerTable = 2;
|
||||
//The number of records per request. Default is 100
|
||||
private int numberOfRecordsPerRequest = 100;
|
||||
|
||||
//The number of threads. Default is 1.
|
||||
private int numberOfThreads = 1;
|
||||
//Delete data. Default is false
|
||||
private boolean deleteTable = false;
|
||||
|
||||
public static void printHelp() {
|
||||
System.out.println("Usage: java -jar JdbcTaosDemo.jar [OPTION...]");
|
||||
System.out.println("-h host The host to connect to TDengine. you must input one");
|
||||
System.out.println("-p port The TCP/IP port number to use for the connection. Default is 6030");
|
||||
System.out.println("-u user The TDengine user name to use when connecting to the server. Default is 'root'");
|
||||
System.out.println("-P password The password to use when connecting to the server.Default is 'taosdata'");
|
||||
System.out.println("-d database Destination database. Default is 'test'");
|
||||
System.out.println("-m tablePrefix Table prefix name. Default is 'd'");
|
||||
System.out.println("-t num_of_tables The number of tables. Default is 10");
|
||||
System.out.println("-n num_of_records_per_table The number of records per table. Default is 2");
|
||||
System.out.println("-r num_of_records_per_req The number of records per request. Default is 100");
|
||||
System.out.println("-T num_of_threads The number of threads. Default is 1");
|
||||
System.out.println("-D delete table Delete data methods. Default is false");
|
||||
System.out.println("--help Give this help list");
|
||||
// System.out.println("--infinite infinite insert mode");
|
||||
}
|
||||
|
||||
/**
|
||||
* parse args from command line
|
||||
*
|
||||
* @param args command line args
|
||||
* @return JdbcTaosdemoConfig
|
||||
*/
|
||||
public JdbcTaosdemoConfig(String[] args) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if ("-h".equals(args[i]) && i < args.length - 1) {
|
||||
host = args[++i];
|
||||
}
|
||||
if ("-p".equals(args[i]) && i < args.length - 1) {
|
||||
port = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-u".equals(args[i]) && i < args.length - 1) {
|
||||
user = args[++i];
|
||||
}
|
||||
if ("-P".equals(args[i]) && i < args.length - 1) {
|
||||
password = args[++i];
|
||||
}
|
||||
if ("-d".equals(args[i]) && i < args.length - 1) {
|
||||
dbName = args[++i];
|
||||
}
|
||||
if ("-m".equals(args[i]) && i < args.length - 1) {
|
||||
tbPrefix = args[++i];
|
||||
}
|
||||
if ("-t".equals(args[i]) && i < args.length - 1) {
|
||||
numberOfTable = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-n".equals(args[i]) && i < args.length - 1) {
|
||||
numberOfRecordsPerTable = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-r".equals(args[i]) && i < args.length - 1) {
|
||||
numberOfRecordsPerRequest = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-T".equals(args[i]) && i < args.length - 1) {
|
||||
numberOfThreads = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-D".equals(args[i]) && i < args.length - 1) {
|
||||
deleteTable = Boolean.parseBoolean(args[++i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getDbName() {
|
||||
return dbName;
|
||||
}
|
||||
|
||||
public int getKeep() {
|
||||
return keep;
|
||||
}
|
||||
|
||||
public int getDays() {
|
||||
return days;
|
||||
}
|
||||
|
||||
public String getStbName() {
|
||||
return stbName;
|
||||
}
|
||||
|
||||
public String getTbPrefix() {
|
||||
return tbPrefix;
|
||||
}
|
||||
|
||||
public int getNumberOfTable() {
|
||||
return numberOfTable;
|
||||
}
|
||||
|
||||
public int getNumberOfRecordsPerTable() {
|
||||
return numberOfRecordsPerTable;
|
||||
}
|
||||
|
||||
public int getNumberOfThreads() {
|
||||
return numberOfThreads;
|
||||
}
|
||||
|
||||
public boolean isDeleteTable() {
|
||||
return deleteTable;
|
||||
}
|
||||
|
||||
public int getNumberOfRecordsPerRequest() {
|
||||
return numberOfRecordsPerRequest;
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo.task;
|
||||
|
||||
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class CreateTableTask implements Runnable {
|
||||
|
||||
private static Logger logger = Logger.getLogger(CreateTableTask.class);
|
||||
private final JdbcTaosdemoConfig config;
|
||||
private final int startIndex;
|
||||
private final int tableNumber;
|
||||
|
||||
public CreateTableTask(JdbcTaosdemoConfig config, int startIndex, int tableNumber) {
|
||||
this.config = config;
|
||||
this.startIndex = startIndex;
|
||||
this.tableNumber = tableNumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Connection connection = ConnectionFactory.build(config);
|
||||
for (int i = startIndex; i < startIndex + tableNumber; i++) {
|
||||
Statement statement = connection.createStatement();
|
||||
String sql = SqlSpeller.createTableSQL(i + 1, config.getDbName(), config.getStbName());
|
||||
statement.execute(sql);
|
||||
statement.close();
|
||||
logger.info(">>> " + sql);
|
||||
}
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo.task;
|
||||
|
||||
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class InsertTableDatetimeTask implements Runnable {
|
||||
private static Logger logger = Logger.getLogger(InsertTableDatetimeTask.class);
|
||||
|
||||
private final JdbcTaosdemoConfig config;
|
||||
private final int startTableIndex;
|
||||
private final int tableNumber;
|
||||
private final long startDatetime;
|
||||
private final long finishedDatetime;
|
||||
|
||||
public InsertTableDatetimeTask(JdbcTaosdemoConfig config, int startTableIndex, int tableNumber, long startDatetime, long finishedDatetime) {
|
||||
this.config = config;
|
||||
this.startTableIndex = startTableIndex;
|
||||
this.tableNumber = tableNumber;
|
||||
this.startDatetime = startDatetime;
|
||||
this.finishedDatetime = finishedDatetime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Connection connection = ConnectionFactory.build(config);
|
||||
int valuesCount = config.getNumberOfRecordsPerRequest();
|
||||
for (long ts = startDatetime; ts < finishedDatetime; ts += valuesCount) {
|
||||
for (int i = startTableIndex; i < startTableIndex + tableNumber; i++) {
|
||||
String sql = SqlSpeller.insertBatchSizeRowsSQL(config.getDbName(), config.getTbPrefix(), i + 1, ts, valuesCount);
|
||||
Statement statement = connection.createStatement();
|
||||
statement.execute(sql);
|
||||
statement.close();
|
||||
logger.info(Thread.currentThread().getName() + ">>> " + sql);
|
||||
}
|
||||
}
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo.task;
|
||||
|
||||
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
|
||||
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class InsertTableTask implements Runnable {
|
||||
private static final Logger logger = Logger.getLogger(InsertTableTask.class);
|
||||
|
||||
private final JdbcTaosdemoConfig config;
|
||||
private final int startTbIndex;
|
||||
private final int tableNumber;
|
||||
private final int recordsNumberPerTable;
|
||||
|
||||
public InsertTableTask(JdbcTaosdemoConfig config, int startTbIndex, int tableNumber, int recordsNumberPerTable) {
|
||||
this.config = config;
|
||||
this.startTbIndex = startTbIndex;
|
||||
this.tableNumber = tableNumber;
|
||||
this.recordsNumberPerTable = recordsNumberPerTable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Connection connection = ConnectionFactory.build(config);
|
||||
int keep = config.getKeep();
|
||||
Instant end = Instant.now();
|
||||
Instant start = end.minus(Duration.ofDays(keep - 1));
|
||||
long timeGap = ChronoUnit.MILLIS.between(start, end) / (recordsNumberPerTable - 1);
|
||||
|
||||
// iterate insert
|
||||
for (int j = 0; j < recordsNumberPerTable; j++) {
|
||||
long ts = start.toEpochMilli() + (j * timeGap);
|
||||
// insert data into echo table
|
||||
for (int i = startTbIndex; i < startTbIndex + tableNumber; i++) {
|
||||
String sql = SqlSpeller.insertBatchSizeRowsSQL(config.getDbName(), config.getTbPrefix(), i + 1, ts, config.getNumberOfRecordsPerRequest());
|
||||
logger.info(Thread.currentThread().getName() + ">>> " + sql);
|
||||
Statement statement = connection.createStatement();
|
||||
statement.execute(sql);
|
||||
statement.close();
|
||||
}
|
||||
}
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo.utils;
|
||||
|
||||
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||
import com.taosdata.jdbc.TSDBDriver;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ConnectionFactory {
|
||||
|
||||
public static Connection build(JdbcTaosdemoConfig config) throws SQLException {
|
||||
return build(config.getHost(), config.getPort(), config.getDbName(), config.getUser(), config.getPassword());
|
||||
}
|
||||
|
||||
public static Connection build(String host, int port, String dbName) throws SQLException {
|
||||
return build(host, port, dbName, "root", "taosdata");
|
||||
}
|
||||
|
||||
private static Connection build(String host, int port, String dbName, String user, String password) throws SQLException {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_USER, user);
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_PASSWORD, password);
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
|
||||
return DriverManager.getConnection("jdbc:TAOS://" + host + ":" + port + "/" + dbName + "", properties);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,126 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo.utils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class SqlSpeller {
|
||||
private static final Random random = new Random(System.currentTimeMillis());
|
||||
private static final String[] locations = {
|
||||
"Beijing", "Shanghai", "Guangzhou", "Shenzhen",
|
||||
"HangZhou", "Tianjin", "Wuhan", "Changsha", "Nanjing", "Xian"
|
||||
};
|
||||
|
||||
public static String createDatabaseSQL(String dbName, int keep, int days) {
|
||||
return "create database if not exists " + dbName + " keep " + keep + " days " + days;
|
||||
}
|
||||
|
||||
public static String dropDatabaseSQL(String dbName) {
|
||||
return "drop database if exists " + dbName;
|
||||
}
|
||||
|
||||
public static String useDatabaseSQL(String dbName) {
|
||||
return "use " + dbName;
|
||||
}
|
||||
|
||||
public static String createSuperTableSQL(String superTableName) {
|
||||
return "create table if not exists " + superTableName + "(ts timestamp, current float, voltage int, phase float) tags(location binary(64), groupId int)";
|
||||
}
|
||||
|
||||
public static String dropSuperTableSQL(String dbName, String superTableName) {
|
||||
return "drop table if exists " + dbName + "." + superTableName;
|
||||
}
|
||||
|
||||
public static String createTableSQL(int tableIndex, String dbName, String superTableName) {
|
||||
String location = locations[random.nextInt(locations.length)];
|
||||
return "create table d" + tableIndex + " using " + dbName + "." + superTableName + " tags('" + location + "'," + tableIndex + ")";
|
||||
}
|
||||
|
||||
public static String insertOneRowSQL(String dbName, String tbPrefix, int tableIndex, long ts) {
|
||||
float current = 10 + random.nextFloat();
|
||||
int voltage = 200 + random.nextInt(20);
|
||||
float phase = random.nextFloat();
|
||||
String sql = "insert into " + dbName + "." + tbPrefix + "" + tableIndex + " " + "values(" + ts + ", " + current + ", " + voltage + ", " + phase + ")";
|
||||
return sql;
|
||||
}
|
||||
|
||||
public static String insertBatchSizeRowsSQL(String dbName, String tbPrefix, int tbIndex, long ts, int valuesCount) {
|
||||
float current = 10 + random.nextFloat();
|
||||
int voltage = 200 + random.nextInt(20);
|
||||
float phase = random.nextFloat();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("insert into " + dbName + "." + tbPrefix + "" + tbIndex + " " + "values");
|
||||
for (int i = 0; i < valuesCount; i++) {
|
||||
sb.append("(" + (ts + i) + ", " + current + ", " + voltage + ", " + phase + ") ");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String selectFromTableLimitSQL(String dbName, String tbPrefix, int tbIndex, int limit, int offset) {
|
||||
return "select * from " + dbName + "." + tbPrefix + "" + tbIndex + " limit " + limit + " offset " + offset;
|
||||
}
|
||||
|
||||
public static String selectCountFromTableSQL(String dbName, String tbPrefix, int tbIndex) {
|
||||
return "select count(*) from " + dbName + "." + tbPrefix + "" + tbIndex;
|
||||
}
|
||||
|
||||
public static String selectAvgMinMaxFromTableSQL(String field, String dbName, String tbPrefix, int tbIndex) {
|
||||
return "select avg(" + field + "),min(" + field + "),max(" + field + ") from " + dbName + "." + tbPrefix + "" + tbIndex;
|
||||
}
|
||||
|
||||
public static String selectFromSuperTableLimitSQL(String dbName, String stbName, int limit, int offset) {
|
||||
return "select * from " + dbName + "." + stbName + " limit " + limit + " offset " + offset;
|
||||
}
|
||||
|
||||
public static String selectCountFromSuperTableSQL(String dbName, String stableName) {
|
||||
return "select count(*) from " + dbName + "." + stableName;
|
||||
}
|
||||
|
||||
public static String selectAvgMinMaxFromSuperTableSQL(String field, String dbName, String stbName) {
|
||||
return "select avg(" + field + "),min(" + field + "),max(" + field + ") from " + dbName + "." + stbName + "";
|
||||
}
|
||||
|
||||
public static String selectLastFromTableSQL(String dbName, String tbPrefix, int tbIndex) {
|
||||
return "select last(*) from " + dbName + "." + tbPrefix + "" + tbIndex;
|
||||
}
|
||||
|
||||
//select avg ,max from stb where tag
|
||||
public static String selectAvgMinMaxFromSuperTableWhere(String field, String dbName, String stbName) {
|
||||
return "select avg(" + field + "),min(" + field + "),max(" + field + ") from " + dbName + "." + stbName + " where location = '" + locations[random.nextInt(locations.length)] + "'";
|
||||
}
|
||||
|
||||
//select last from stb where
|
||||
public static String selectLastFromSuperTableWhere(String field, String dbName, String stbName) {
|
||||
return "select last(" + field + ") from " + dbName + "." + stbName + " where location = '" + locations[random.nextInt(locations.length)] + "'";
|
||||
}
|
||||
|
||||
public static String selectGroupBy(String field, String dbName, String stbName) {
|
||||
return "select avg(" + field + ") from " + dbName + "." + stbName + " group by location";
|
||||
}
|
||||
|
||||
public static String selectLike(String dbName, String stbName) {
|
||||
return "select * from " + dbName + "." + stbName + " where location like 'S%'";
|
||||
}
|
||||
|
||||
public static String selectLastOneHour(String dbName, String stbName) {
|
||||
return "select * from " + dbName + "." + stbName + " where ts >= now - 1h";
|
||||
}
|
||||
|
||||
public static String selectLastOneDay(String dbName, String stbName) {
|
||||
return "select * from " + dbName + "." + stbName + " where ts >= now - 1d";
|
||||
}
|
||||
|
||||
public static String selectLastOneWeek(String dbName, String stbName) {
|
||||
return "select * from " + dbName + "." + stbName + " where ts >= now - 1w";
|
||||
}
|
||||
|
||||
public static String selectLastOneMonth(String dbName, String stbName) {
|
||||
return "select * from " + dbName + "." + stbName + " where ts >= now - 1n";
|
||||
}
|
||||
|
||||
public static String selectLastOneYear(String dbName, String stbName) {
|
||||
return "select * from " + dbName + "." + stbName + " where ts >= now - 1y";
|
||||
}
|
||||
|
||||
// select group by
|
||||
// select like
|
||||
// select ts >= ts<=
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo.utils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
|
||||
public class TimeStampUtil {
|
||||
private static final String datetimeFormat = "yyyy-MM-dd HH:mm:ss.SSS";
|
||||
|
||||
public static long datetimeToLong(String dateTime) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(datetimeFormat);
|
||||
try {
|
||||
return sdf.parse(dateTime).getTime();
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String longToDatetime(long time) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(datetimeFormat);
|
||||
return sdf.format(new Date(time));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
|
||||
// Instant now = Instant.now();
|
||||
// System.out.println(now);
|
||||
// Instant years20Ago = now.minus(Duration.ofDays(365));
|
||||
// System.out.println(years20Ago);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
### 设置###
|
||||
log4j.rootLogger=debug,stdout,DebugLog,ErrorLog
|
||||
### 输出信息到控制抬 ###
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
|
||||
### 输出DEBUG 级别以上的日志到=logs/error.log ###
|
||||
log4j.appender.DebugLog=org.apache.log4j.DailyRollingFileAppender
|
||||
log4j.appender.DebugLog.File=logs/debug.log
|
||||
log4j.appender.DebugLog.Append=true
|
||||
log4j.appender.DebugLog.Threshold=DEBUG
|
||||
log4j.appender.DebugLog.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.DebugLog.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
|
||||
### 输出ERROR 级别以上的日志到=logs/error.log ###
|
||||
log4j.appender.ErrorLog=org.apache.log4j.DailyRollingFileAppender
|
||||
log4j.appender.ErrorLog.File=logs/error.log
|
||||
log4j.appender.ErrorLog.Append=true
|
||||
log4j.appender.ErrorLog.Threshold=ERROR
|
||||
log4j.appender.ErrorLog.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.ErrorLog.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
|
|
@ -1,52 +0,0 @@
|
|||
package com.taosdata.example.jdbcTaosdemo.utils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TimeStampUtilTest {
|
||||
|
||||
@Test
|
||||
public void datetimeToLong() {
|
||||
final String startTime = "2005-01-01 00:00:00.000";
|
||||
long start = TimeStampUtil.datetimeToLong(startTime);
|
||||
assertEquals(1104508800000l, start);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void longToDatetime() {
|
||||
String datetime = TimeStampUtil.longToDatetime(1510000000000L);
|
||||
assertEquals("2017-11-07 04:26:40.000", datetime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getStartDateTime() {
|
||||
int keep = 365;
|
||||
|
||||
Instant end = Instant.now();
|
||||
System.out.println(end.toString());
|
||||
System.out.println(end.toEpochMilli());
|
||||
|
||||
Instant start = end.minus(Duration.ofDays(keep));
|
||||
System.out.println(start.toString());
|
||||
System.out.println(start.toEpochMilli());
|
||||
|
||||
int numberOfRecordsPerTable = 10;
|
||||
long timeGap = ChronoUnit.MILLIS.between(start, end) / (numberOfRecordsPerTable - 1);
|
||||
System.out.println(timeGap);
|
||||
|
||||
System.out.println("===========================");
|
||||
for (int i = 0; i < numberOfRecordsPerTable; i++) {
|
||||
long ts = start.toEpochMilli() + (i * timeGap);
|
||||
System.out.println(i + " : " + ts);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -82,12 +82,27 @@ public class ConnectionPoolDemo {
|
|||
|
||||
init(dataSource);
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
|
||||
while (true) {
|
||||
executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
|
||||
if (sleep > 0)
|
||||
TimeUnit.MILLISECONDS.sleep(sleep);
|
||||
try {
|
||||
Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement();
|
||||
String sql = "insert into " + dbName + ".t_1 values('2020-01-01 00:00:00.000',12.12,111)";
|
||||
int affectRows = statement.executeUpdate(sql);
|
||||
System.out.println("affectRows >>> " + affectRows);
|
||||
affectRows = statement.executeUpdate(sql);
|
||||
System.out.println("affectRows >>> " + affectRows);
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
// ExecutorService executor = Executors.newFixedThreadPool(threadCount);
|
||||
// while (true) {
|
||||
// executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
|
||||
// if (sleep > 0)
|
||||
// TimeUnit.MILLISECONDS.sleep(sleep);
|
||||
// }
|
||||
}
|
||||
|
||||
private static void init(DataSource dataSource) {
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
|
||||
@MapperScan(basePackages = {"com.taosdata.jdbc.springbootdemo.dao"})
|
||||
@SpringBootApplication
|
||||
public class SpringbootdemoApplication {
|
||||
public class cd {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringbootdemoApplication.class, args);
|
||||
|
|
|
@ -2,41 +2,87 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.taosdata</groupId>
|
||||
<artifactId>taosdemo</artifactId>
|
||||
<version>2.0</version>
|
||||
<name>taosdemo</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Demo project for TDengine</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<spring.version>5.3.2</spring.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-expression</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- HikariCP -->
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>3.4.5</version>
|
||||
</dependency>
|
||||
<!-- taos jdbc -->
|
||||
<dependency>
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>taos-jdbcdriver</artifactId>
|
||||
<version>2.0.14</version>
|
||||
<version>2.0.15</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/src/main/resources/lib/taos-jdbcdriver-2.0.15-dist.jar</systemPath>
|
||||
</dependency>
|
||||
<!-- mysql -->
|
||||
<!-- fastjson -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.75</version>
|
||||
</dependency>
|
||||
<!-- mysql: just for test -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.47</version>
|
||||
</dependency>
|
||||
<!-- mybatis-plus -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.1.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- log4j -->
|
||||
<dependency>
|
||||
|
@ -44,46 +90,19 @@
|
|||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
|
||||
<!-- springboot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.1.4</version>
|
||||
</dependency>
|
||||
<!-- junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<version>1.18.16</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -94,6 +113,7 @@
|
|||
<includes>
|
||||
<include>**/*.properties</include>
|
||||
<include>**/*.xml</include>
|
||||
<include>**/*.jar</include>
|
||||
</includes>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
|
@ -108,10 +128,41 @@
|
|||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<!-- 指定JdbcChecker为mainClass -->
|
||||
<mainClass>com.taosdata.taosdemo.TaosDemoApplication</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
需求:
|
||||
1. 可以读lowa的配置文件
|
||||
2. 支持对JNI方式和Restful方式的taos-driver
|
|
@ -0,0 +1,112 @@
|
|||
package com.taosdata.taosdemo;
|
||||
|
||||
import com.taosdata.taosdemo.components.DataSourceFactory;
|
||||
import com.taosdata.taosdemo.components.JdbcTaosdemoConfig;
|
||||
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||
import com.taosdata.taosdemo.service.DatabaseService;
|
||||
import com.taosdata.taosdemo.service.SubTableService;
|
||||
import com.taosdata.taosdemo.service.SuperTableService;
|
||||
import com.taosdata.taosdemo.service.data.SuperTableMetaGenerator;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TaosDemoApplication {
|
||||
|
||||
private static Logger logger = Logger.getLogger(TaosDemoApplication.class);
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// 读配置参数
|
||||
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
|
||||
boolean isHelp = Arrays.asList(args).contains("--help");
|
||||
if (isHelp || config.host == null || config.host.isEmpty()) {
|
||||
// if (isHelp) {
|
||||
JdbcTaosdemoConfig.printHelp();
|
||||
System.exit(0);
|
||||
}
|
||||
// 初始化
|
||||
final DataSource dataSource = DataSourceFactory.getInstance(config.host, config.port, config.user, config.password);
|
||||
final DatabaseService databaseService = new DatabaseService(dataSource);
|
||||
final SuperTableService superTableService = new SuperTableService(dataSource);
|
||||
final SubTableService subTableService = new SubTableService(dataSource);
|
||||
// 创建数据库
|
||||
long start = System.currentTimeMillis();
|
||||
Map<String, String> databaseParam = new HashMap<>();
|
||||
databaseParam.put("database", config.database);
|
||||
databaseParam.put("keep", Integer.toString(config.keep));
|
||||
databaseParam.put("days", Integer.toString(config.days));
|
||||
databaseParam.put("replica", Integer.toString(config.replica));
|
||||
//TODO: other database parameters
|
||||
databaseService.createDatabase(databaseParam);
|
||||
databaseService.useDatabase(config.database);
|
||||
long end = System.currentTimeMillis();
|
||||
logger.info(">>> create database time cost : " + (end - start) + " ms.");
|
||||
/**********************************************************************************/
|
||||
// 构造超级表的meta
|
||||
SuperTableMeta superTableMeta;
|
||||
// create super table
|
||||
if (config.superTableSQL != null) {
|
||||
// use a sql to create super table
|
||||
superTableMeta = SuperTableMetaGenerator.generate(config.superTableSQL);
|
||||
if (config.database != null && !config.database.isEmpty())
|
||||
superTableMeta.setDatabase(config.database);
|
||||
} else if (config.numOfFields == 0) {
|
||||
String sql = "create table " + config.database + "." + config.superTable + " (ts timestamp, temperature float, humidity int) tags(location nchar(64), groupId int)";
|
||||
superTableMeta = SuperTableMetaGenerator.generate(sql);
|
||||
} else {
|
||||
// create super table with specified field size and tag size
|
||||
superTableMeta = SuperTableMetaGenerator.generate(config.database, config.superTable, config.numOfFields, config.prefixOfFields, config.numOfTags, config.prefixOfTags);
|
||||
}
|
||||
/**********************************************************************************/
|
||||
// 建表
|
||||
start = System.currentTimeMillis();
|
||||
if (config.doCreateTable) {
|
||||
superTableService.drop(superTableMeta.getDatabase(), superTableMeta.getName());
|
||||
superTableService.create(superTableMeta);
|
||||
if (!config.autoCreateTable) {
|
||||
// 批量建子表
|
||||
subTableService.createSubTable(superTableMeta, config.numOfTables, config.prefixOfTable, config.numOfThreadsForCreate);
|
||||
}
|
||||
}
|
||||
end = System.currentTimeMillis();
|
||||
logger.error(">>> create table time cost : " + (end - start) + " ms.");
|
||||
/**********************************************************************************/
|
||||
// 插入
|
||||
long tableSize = config.numOfTables;
|
||||
int threadSize = config.numOfThreadsForInsert;
|
||||
long startTime = getProperStartTime(config.startTime, config.keep);
|
||||
|
||||
if (tableSize < threadSize)
|
||||
threadSize = (int) tableSize;
|
||||
long gap = (long) Math.ceil((0.0d + tableSize) / threadSize);
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
// multi threads to insert
|
||||
int affectedRows = subTableService.insertMultiThreads(superTableMeta, threadSize, tableSize, startTime, gap, config);
|
||||
end = System.currentTimeMillis();
|
||||
logger.error("insert " + affectedRows + " rows, time cost: " + (end - start) + " ms");
|
||||
/**********************************************************************************/
|
||||
// 删除表
|
||||
if (config.dropTable) {
|
||||
superTableService.drop(config.database, config.superTable);
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private static long getProperStartTime(long startTime, int keep) {
|
||||
Instant now = Instant.now();
|
||||
long earliest = now.minus(Duration.ofDays(keep - 1)).toEpochMilli();
|
||||
if (startTime == 0 || startTime < earliest) {
|
||||
startTime = earliest;
|
||||
}
|
||||
return startTime;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.taosdata.taosdemo;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@MapperScan(basePackages = {"com.taosdata.taosdemo.mapper"})
|
||||
@SpringBootApplication
|
||||
public class TaosdemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TaosdemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.taosdata.taosdemo.components;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
public class DataSourceFactory {
|
||||
|
||||
private static DataSource instance;
|
||||
|
||||
public static DataSource getInstance(String host, int port, String user, String password) throws IOException {
|
||||
if (instance == null) {
|
||||
synchronized (DataSourceFactory.class) {
|
||||
if (instance == null) {
|
||||
InputStream is = DataSourceFactory.class.getClassLoader().getResourceAsStream("application.properties");
|
||||
Properties properties = new Properties();
|
||||
properties.load(is);
|
||||
|
||||
HikariConfig config = new HikariConfig();
|
||||
|
||||
if (properties.containsKey("jdbc.driver")) {
|
||||
// String driverName = properties.getProperty("jdbc.driver");
|
||||
// System.out.println(">>> load driver : " + driverName);
|
||||
// try {
|
||||
// Class.forName(driverName);
|
||||
// } catch (ClassNotFoundException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
config.setDriverClassName(properties.getProperty("jdbc.driver"));
|
||||
} else {
|
||||
config.setDriverClassName("com.taosdata.jdbc.TSDBDriver");
|
||||
}
|
||||
if ("com.taosdata.jdbc.rs.RestfulDriver".equalsIgnoreCase(properties.getProperty("jdbc.driver")))
|
||||
config.setJdbcUrl("jdbc:TAOS-RS://" + host + ":6041/?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8");
|
||||
else
|
||||
config.setJdbcUrl("jdbc:TAOS://" + host + ":" + port + "/?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8");
|
||||
config.setUsername(user);
|
||||
config.setPassword(password);
|
||||
// maximum-pool-size
|
||||
if (properties.containsKey("hikari.maximum-pool-size"))
|
||||
config.setMaximumPoolSize(Integer.parseInt(properties.getProperty("hikari.maximum-pool-size")));
|
||||
else
|
||||
config.setMaximumPoolSize(500);
|
||||
// minimum-idle
|
||||
if (properties.containsKey("hikari.minimum-idle"))
|
||||
config.setMinimumIdle(Integer.parseInt(properties.getProperty("hikari.minimum-idle")));
|
||||
else
|
||||
config.setMinimumIdle(100);
|
||||
config.setMaxLifetime(0);
|
||||
instance = new HikariDataSource(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
package com.taosdata.taosdemo.utils;
|
||||
package com.taosdata.taosdemo.components;
|
||||
|
||||
import com.taosdata.taosdemo.utils.TimeStampUtil;
|
||||
|
||||
public final class JdbcTaosdemoConfig {
|
||||
// instance
|
||||
|
@ -7,10 +9,14 @@ public final class JdbcTaosdemoConfig {
|
|||
public String user = "root"; //user
|
||||
public String password = "taosdata"; //password
|
||||
// database
|
||||
public String database = "test"; //database
|
||||
public String database = "jdbcdb"; //database
|
||||
public int keep = 3650; //keep
|
||||
public int days = 30; //days
|
||||
public int replica = 1; //replica
|
||||
public int blocks = 16;
|
||||
public int cache = 8;
|
||||
public String precision = "ms";
|
||||
|
||||
//super table
|
||||
public boolean doCreateTable = true;
|
||||
public String superTable = "weather"; //super table name
|
||||
|
@ -20,19 +26,19 @@ public final class JdbcTaosdemoConfig {
|
|||
public int numOfTags;
|
||||
public String superTableSQL;
|
||||
//sub table
|
||||
public String tablePrefix = "t";
|
||||
public int numOfTables = 100;
|
||||
public int numOfThreadsForCreate = 1;
|
||||
public String prefixOfTable = "t";
|
||||
// insert task
|
||||
public boolean autoCreateTable;
|
||||
public int numOfRowsPerTable = 100;
|
||||
public long numOfTables = 10;
|
||||
public long numOfRowsPerTable = 10;
|
||||
public int numOfTablesPerSQL = 1;
|
||||
public int numOfValuesPerSQL = 1;
|
||||
public int numOfThreadsForCreate = 1;
|
||||
public int numOfThreadsForInsert = 1;
|
||||
public int numOfTablesPerSQL = 10;
|
||||
public int numOfValuesPerSQL = 10;
|
||||
public long startTime;
|
||||
public long timeGap;
|
||||
public int sleep = 0;
|
||||
public int order = 0;
|
||||
public long timeGap = 1;
|
||||
public int frequency;
|
||||
public int order;
|
||||
public int rate = 10;
|
||||
public long range = 1000l;
|
||||
// select task
|
||||
|
@ -48,10 +54,14 @@ public final class JdbcTaosdemoConfig {
|
|||
System.out.println("-user The TDengine user name to use when connecting to the server. Default is 'root'");
|
||||
System.out.println("-password The password to use when connecting to the server.Default is 'taosdata'");
|
||||
// database
|
||||
System.out.println("-database Destination database. Default is 'test'");
|
||||
System.out.println("-database Destination database. Default is 'jdbcdb'");
|
||||
System.out.println("-keep database keep parameter. Default is 3650");
|
||||
System.out.println("-days database days parameter. Default is 30");
|
||||
System.out.println("-replica database replica parameter. Default 1, min: 1, max: 3");
|
||||
System.out.println("-blocks database blocks parameter. Default is 16");
|
||||
System.out.println("-cache database cache parameter. Default is 8");
|
||||
System.out.println("-precision database precision parameter. Default is ms");
|
||||
|
||||
// super table
|
||||
System.out.println("-doCreateTable do create super table and sub table, true or false, Default true");
|
||||
System.out.println("-superTable super table name. Default 'weather'");
|
||||
|
@ -63,7 +73,7 @@ public final class JdbcTaosdemoConfig {
|
|||
" Default is 'create table weather(ts timestamp, temperature float, humidity int) tags(location nchar(64), groupId int). \n" +
|
||||
" if you use this parameter, the numOfFields and numOfTags will be invalid'");
|
||||
// sub table
|
||||
System.out.println("-tablePrefix The prefix of sub tables. Default is 't'");
|
||||
System.out.println("-prefixOfTable The prefix of sub tables. Default is 't'");
|
||||
System.out.println("-numOfTables The number of tables. Default is 1");
|
||||
System.out.println("-numOfThreadsForCreate The number of thread during create sub table. Default is 1");
|
||||
// insert task
|
||||
|
@ -74,11 +84,10 @@ public final class JdbcTaosdemoConfig {
|
|||
System.out.println("-numOfValuesPerSQL The number of value per SQL. Default is 1");
|
||||
System.out.println("-startTime start time for insert task, The format is \"yyyy-MM-dd HH:mm:ss.SSS\".");
|
||||
System.out.println("-timeGap the number of time gap. Default is 1000 ms");
|
||||
System.out.println("-sleep The number of milliseconds for sleep after each insert. default is 0");
|
||||
System.out.println("-frequency the number of records per second inserted into one table. default is 0, do not control frequency");
|
||||
System.out.println("-order Insert mode--0: In order, 1: Out of order. Default is in order");
|
||||
System.out.println("-rate The proportion of data out of order. effective only if order is 1. min 0, max 100, default is 10");
|
||||
System.out.println("-range The range of data out of order. effective only if order is 1. default is 1000 ms");
|
||||
|
||||
// query task
|
||||
// System.out.println("-sqlFile The select sql file");
|
||||
// drop task
|
||||
|
@ -120,6 +129,15 @@ public final class JdbcTaosdemoConfig {
|
|||
if ("-replica".equals(args[i]) && i < args.length - 1) {
|
||||
replica = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-blocks".equals(args[i]) && i < args.length - 1) {
|
||||
blocks = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-cache".equals(args[i]) && i < args.length - 1) {
|
||||
cache = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-precision".equals(args[i]) && i < args.length - 1) {
|
||||
precision = args[++i];
|
||||
}
|
||||
// super table
|
||||
if ("-doCreateTable".equals(args[i]) && i < args.length - 1) {
|
||||
doCreateTable = Boolean.parseBoolean(args[++i]);
|
||||
|
@ -143,11 +161,11 @@ public final class JdbcTaosdemoConfig {
|
|||
superTableSQL = args[++i];
|
||||
}
|
||||
// sub table
|
||||
if ("-tablePrefix".equals(args[i]) && i < args.length - 1) {
|
||||
tablePrefix = args[++i];
|
||||
if ("-prefixOfTable".equals(args[i]) && i < args.length - 1) {
|
||||
prefixOfTable = args[++i];
|
||||
}
|
||||
if ("-numOfTables".equals(args[i]) && i < args.length - 1) {
|
||||
numOfTables = Integer.parseInt(args[++i]);
|
||||
numOfTables = Long.parseLong(args[++i]);
|
||||
}
|
||||
if ("-autoCreateTable".equals(args[i]) && i < args.length - 1) {
|
||||
autoCreateTable = Boolean.parseBoolean(args[++i]);
|
||||
|
@ -157,7 +175,7 @@ public final class JdbcTaosdemoConfig {
|
|||
}
|
||||
// insert task
|
||||
if ("-numOfRowsPerTable".equals(args[i]) && i < args.length - 1) {
|
||||
numOfRowsPerTable = Integer.parseInt(args[++i]);
|
||||
numOfRowsPerTable = Long.parseLong(args[++i]);
|
||||
}
|
||||
if ("-numOfThreadsForInsert".equals(args[i]) && i < args.length - 1) {
|
||||
numOfThreadsForInsert = Integer.parseInt(args[++i]);
|
||||
|
@ -174,8 +192,8 @@ public final class JdbcTaosdemoConfig {
|
|||
if ("-timeGap".equals(args[i]) && i < args.length - 1) {
|
||||
timeGap = Long.parseLong(args[++i]);
|
||||
}
|
||||
if ("-sleep".equals(args[i]) && i < args.length - 1) {
|
||||
sleep = Integer.parseInt(args[++i]);
|
||||
if ("-frequency".equals(args[i]) && i < args.length - 1) {
|
||||
frequency = Integer.parseInt(args[++i]);
|
||||
}
|
||||
if ("-order".equals(args[i]) && i < args.length - 1) {
|
||||
order = Integer.parseInt(args[++i]);
|
||||
|
@ -197,8 +215,4 @@ public final class JdbcTaosdemoConfig {
|
|||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.taosdata.taosdemo.components;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class JsonConfig {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
JsonConfig config = new JsonConfig();
|
||||
String str = config.read("insert.json");
|
||||
JSONObject jsonObject = JSONObject.parseObject(str);
|
||||
System.out.println(jsonObject);
|
||||
|
||||
}
|
||||
|
||||
private String read(String fileName) {
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(JsonConfig.class.getClassLoader().getResourceAsStream(fileName))
|
||||
);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line = null;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,174 +0,0 @@
|
|||
package com.taosdata.taosdemo.components;
|
||||
|
||||
import com.taosdata.taosdemo.domain.*;
|
||||
import com.taosdata.taosdemo.service.DatabaseService;
|
||||
import com.taosdata.taosdemo.service.SubTableService;
|
||||
import com.taosdata.taosdemo.service.SuperTableService;
|
||||
import com.taosdata.taosdemo.service.data.SubTableMetaGenerator;
|
||||
import com.taosdata.taosdemo.service.data.SubTableValueGenerator;
|
||||
import com.taosdata.taosdemo.service.data.SuperTableMetaGenerator;
|
||||
import com.taosdata.taosdemo.utils.JdbcTaosdemoConfig;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
@Component
|
||||
public class TaosDemoCommandLineRunner implements CommandLineRunner {
|
||||
|
||||
private static Logger logger = Logger.getLogger(TaosDemoCommandLineRunner.class);
|
||||
@Autowired
|
||||
private DatabaseService databaseService;
|
||||
@Autowired
|
||||
private SuperTableService superTableService;
|
||||
@Autowired
|
||||
private SubTableService subTableService;
|
||||
|
||||
private SuperTableMeta superTableMeta;
|
||||
private List<SubTableMeta> subTableMetaList;
|
||||
private List<SubTableValue> subTableValueList;
|
||||
private List<List<SubTableValue>> dataList;
|
||||
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
// 读配置参数
|
||||
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
|
||||
boolean isHelp = Arrays.asList(args).contains("--help");
|
||||
if (isHelp) {
|
||||
JdbcTaosdemoConfig.printHelp();
|
||||
System.exit(0);
|
||||
}
|
||||
// 准备数据
|
||||
prepareData(config);
|
||||
// 创建数据库
|
||||
createDatabaseTask(config);
|
||||
// 建表
|
||||
createTableTask(config);
|
||||
// 插入
|
||||
insertTask(config);
|
||||
// 查询: 1. 生成查询语句, 2. 执行查询
|
||||
// 删除表
|
||||
if (config.dropTable) {
|
||||
superTableService.drop(config.database, config.superTable);
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private void createDatabaseTask(JdbcTaosdemoConfig config) {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
Map<String, String> databaseParam = new HashMap<>();
|
||||
databaseParam.put("database", config.database);
|
||||
databaseParam.put("keep", Integer.toString(config.keep));
|
||||
databaseParam.put("days", Integer.toString(config.days));
|
||||
databaseParam.put("replica", Integer.toString(config.replica));
|
||||
//TODO: other database parameters
|
||||
databaseService.dropDatabase(config.database);
|
||||
databaseService.createDatabase(databaseParam);
|
||||
databaseService.useDatabase(config.database);
|
||||
|
||||
long end = System.currentTimeMillis();
|
||||
logger.info(">>> insert time cost : " + (end - start) + " ms.");
|
||||
}
|
||||
|
||||
// 建超级表,三种方式:1. 指定SQL,2. 指定field和tags的个数,3. 默认
|
||||
private void createTableTask(JdbcTaosdemoConfig config) {
|
||||
long start = System.currentTimeMillis();
|
||||
if (config.doCreateTable) {
|
||||
superTableService.create(superTableMeta);
|
||||
// 批量建子表
|
||||
subTableService.createSubTable(subTableMetaList, config.numOfThreadsForCreate);
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
logger.info(">>> create table time cost : " + (end - start) + " ms.");
|
||||
}
|
||||
|
||||
private void insertTask(JdbcTaosdemoConfig config) {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
int numOfThreadsForInsert = config.numOfThreadsForInsert;
|
||||
int sleep = config.sleep;
|
||||
if (config.autoCreateTable) {
|
||||
// 批量插入,自动建表
|
||||
dataList.stream().forEach(subTableValues -> {
|
||||
subTableService.insertAutoCreateTable(subTableValues, numOfThreadsForInsert);
|
||||
sleep(sleep);
|
||||
});
|
||||
} else {
|
||||
dataList.stream().forEach(subTableValues -> {
|
||||
subTableService.insert(subTableValues, numOfThreadsForInsert);
|
||||
sleep(sleep);
|
||||
});
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
logger.info(">>> insert time cost : " + (end - start) + " ms.");
|
||||
}
|
||||
|
||||
private void prepareData(JdbcTaosdemoConfig config) {
|
||||
long start = System.currentTimeMillis();
|
||||
// 超级表的meta
|
||||
superTableMeta = createSupertable(config);
|
||||
// 子表的meta
|
||||
subTableMetaList = SubTableMetaGenerator.generate(superTableMeta, config.numOfTables, config.tablePrefix);
|
||||
// 子表的data
|
||||
subTableValueList = SubTableValueGenerator.generate(subTableMetaList, config.numOfRowsPerTable, config.startTime, config.timeGap);
|
||||
// 如果有乱序,给数据搞乱
|
||||
if (config.order != 0) {
|
||||
SubTableValueGenerator.disrupt(subTableValueList, config.rate, config.range);
|
||||
}
|
||||
// 分割数据
|
||||
int numOfTables = config.numOfTables;
|
||||
int numOfTablesPerSQL = config.numOfTablesPerSQL;
|
||||
int numOfRowsPerTable = config.numOfRowsPerTable;
|
||||
int numOfValuesPerSQL = config.numOfValuesPerSQL;
|
||||
dataList = SubTableValueGenerator.split(subTableValueList, numOfTables, numOfTablesPerSQL, numOfRowsPerTable, numOfValuesPerSQL);
|
||||
long end = System.currentTimeMillis();
|
||||
logger.info(">>> prepare data time cost : " + (end - start) + " ms.");
|
||||
}
|
||||
|
||||
private SuperTableMeta createSupertable(JdbcTaosdemoConfig config) {
|
||||
SuperTableMeta tableMeta;
|
||||
// create super table
|
||||
logger.info(">>> create super table <<<");
|
||||
if (config.superTableSQL != null) {
|
||||
// use a sql to create super table
|
||||
tableMeta = SuperTableMetaGenerator.generate(config.superTableSQL);
|
||||
} else if (config.numOfFields == 0) {
|
||||
// default sql = "create table test.weather (ts timestamp, temperature float, humidity int) tags(location nchar(64), groupId int)";
|
||||
SuperTableMeta superTableMeta = new SuperTableMeta();
|
||||
superTableMeta.setDatabase(config.database);
|
||||
superTableMeta.setName(config.superTable);
|
||||
List<FieldMeta> fields = new ArrayList<>();
|
||||
fields.add(new FieldMeta("ts", "timestamp"));
|
||||
fields.add(new FieldMeta("temperature", "float"));
|
||||
fields.add(new FieldMeta("humidity", "int"));
|
||||
superTableMeta.setFields(fields);
|
||||
List<TagMeta> tags = new ArrayList<>();
|
||||
tags.add(new TagMeta("location", "nchar(64)"));
|
||||
tags.add(new TagMeta("groupId", "int"));
|
||||
superTableMeta.setTags(tags);
|
||||
return superTableMeta;
|
||||
} else {
|
||||
// create super table with specified field size and tag size
|
||||
tableMeta = SuperTableMetaGenerator.generate(config.database, config.superTable, config.numOfFields, config.prefixOfFields, config.numOfTags, config.prefixOfTags);
|
||||
}
|
||||
return tableMeta;
|
||||
}
|
||||
|
||||
private static void sleep(int sleep) {
|
||||
if (sleep <= 0)
|
||||
return;
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(sleep);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package com.taosdata.taosdemo.controller;
|
||||
|
||||
import com.taosdata.taosdemo.service.DatabaseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping
|
||||
public class DatabaseController {
|
||||
|
||||
@Autowired
|
||||
private DatabaseService databaseService;
|
||||
|
||||
/**
|
||||
* create database
|
||||
***/
|
||||
@PostMapping
|
||||
public int create(@RequestBody Map<String, String> map) {
|
||||
return databaseService.createDatabase(map);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* drop database
|
||||
**/
|
||||
@DeleteMapping("/{dbname}")
|
||||
public int delete(@PathVariable("dbname") String dbname) {
|
||||
return databaseService.dropDatabase(dbname);
|
||||
}
|
||||
|
||||
/**
|
||||
* use database
|
||||
**/
|
||||
@GetMapping("/{dbname}")
|
||||
public int use(@PathVariable("dbname") String dbname) {
|
||||
return databaseService.useDatabase(dbname);
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
package com.taosdata.taosdemo.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class InsertController {
|
||||
|
||||
//TODO:多线程写一张表, thread = 10, table = 1
|
||||
//TODO:一个批次写多张表, insert into t1 using weather values() t2 using weather values()
|
||||
//TODO:插入的频率,
|
||||
//TODO:指定一张表内的records数量
|
||||
//TODO:是否乱序,
|
||||
//TODO:乱序的比例,乱序的范围
|
||||
//TODO:先建表,自动建表
|
||||
//TODO:一个批次写多张表
|
||||
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package com.taosdata.taosdemo.controller;
|
||||
|
||||
import com.taosdata.taosdemo.domain.TableValue;
|
||||
import com.taosdata.taosdemo.service.SuperTableService;
|
||||
import com.taosdata.taosdemo.service.TableService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class SubTableController {
|
||||
|
||||
@Autowired
|
||||
private TableService tableService;
|
||||
@Autowired
|
||||
private SuperTableService superTableService;
|
||||
|
||||
//TODO: 使用supertable创建一个子表
|
||||
|
||||
//TODO:使用supertable创建多个子表
|
||||
|
||||
//TODO:使用supertable多线程创建子表
|
||||
|
||||
//TODO:使用supertable多线程创建子表,指定子表的name_prefix,子表的数量,使用线程的个数
|
||||
|
||||
/**
|
||||
* 创建表,超级表或者普通表
|
||||
**/
|
||||
|
||||
|
||||
/**
|
||||
* 创建超级表的子表
|
||||
**/
|
||||
@PostMapping("/{database}/{superTable}")
|
||||
public int createTable(@PathVariable("database") String database,
|
||||
@PathVariable("superTable") String superTable,
|
||||
@RequestBody TableValue tableMetadta) {
|
||||
tableMetadta.setDatabase(database);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package com.taosdata.taosdemo.controller;
|
||||
|
||||
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||
import com.taosdata.taosdemo.service.SuperTableService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
public class SuperTableController {
|
||||
@Autowired
|
||||
private SuperTableService superTableService;
|
||||
|
||||
|
||||
@PostMapping("/{database}")
|
||||
public int createTable(@PathVariable("database") String database, @RequestBody SuperTableMeta tableMetadta) {
|
||||
tableMetadta.setDatabase(database);
|
||||
return superTableService.create(tableMetadta);
|
||||
}
|
||||
|
||||
//TODO: 删除超级表
|
||||
|
||||
//TODO:查询超级表
|
||||
|
||||
//TODO:统计查询表
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
package com.taosdata.taosdemo.controller;
|
||||
|
||||
public class TableController {
|
||||
|
||||
//TODO:创建普通表,create table(ts timestamp, temperature float)
|
||||
|
||||
//TODO:创建普通表,指定表的列数,包括第一列timestamp
|
||||
|
||||
//TODO:创建普通表,指定表每列的name和type
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.taosdata.taosdemo.dao;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface DatabaseMapper {
|
||||
|
||||
// create database if not exists XXX
|
||||
void createDatabase(String dbname);
|
||||
|
||||
// drop database if exists XXX
|
||||
void dropDatabase(String dbname);
|
||||
|
||||
// create database if not exists XXX keep XX days XX replica XX
|
||||
void createDatabaseWithParameters(Map<String, String> map);
|
||||
|
||||
// use XXX
|
||||
void useDatabase(String dbname);
|
||||
|
||||
//TODO: alter database
|
||||
|
||||
//TODO: show database
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.taosdata.taosdemo.dao;
|
||||
|
||||
import com.taosdata.taosdemo.utils.SqlSpeller;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Map;
|
||||
|
||||
public class DatabaseMapperImpl implements DatabaseMapper {
|
||||
private static final Logger logger = Logger.getLogger(DatabaseMapperImpl.class);
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public DatabaseMapperImpl(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void createDatabase(String dbname) {
|
||||
String sql = "create database if not exists " + dbname;
|
||||
jdbcTemplate.execute(sql);
|
||||
logger.info("SQL >>> " + sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropDatabase(String dbname) {
|
||||
String sql = "drop database if exists " + dbname;
|
||||
jdbcTemplate.update(sql);
|
||||
logger.info("SQL >>> " + sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createDatabaseWithParameters(Map<String, String> map) {
|
||||
String sql = SqlSpeller.createDatabase(map);
|
||||
jdbcTemplate.execute(sql);
|
||||
logger.info("SQL >>> " + sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void useDatabase(String dbname) {
|
||||
String sql = "use " + dbname;
|
||||
jdbcTemplate.execute(sql);
|
||||
logger.info("SQL >>> " + sql);
|
||||
}
|
||||
}
|
|
@ -1,8 +1,7 @@
|
|||
package com.taosdata.taosdemo.mapper;
|
||||
package com.taosdata.taosdemo.dao;
|
||||
|
||||
import com.taosdata.taosdemo.domain.SubTableMeta;
|
||||
import com.taosdata.taosdemo.domain.SubTableValue;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -11,7 +10,7 @@ import java.util.List;
|
|||
public interface SubTableMapper {
|
||||
|
||||
// 创建:子表
|
||||
int createUsingSuperTable(SubTableMeta subTableMeta);
|
||||
void createUsingSuperTable(SubTableMeta subTableMeta);
|
||||
|
||||
// 插入:一张子表多个values
|
||||
int insertOneTableMultiValues(SubTableValue subTableValue);
|
||||
|
@ -20,10 +19,10 @@ public interface SubTableMapper {
|
|||
int insertOneTableMultiValuesUsingSuperTable(SubTableValue subTableValue);
|
||||
|
||||
// 插入:多张表多个values
|
||||
int insertMultiTableMultiValues(@Param("tables") List<SubTableValue> tables);
|
||||
int insertMultiTableMultiValues(List<SubTableValue> tables);
|
||||
|
||||
// 插入:多张表多个values,自动建表
|
||||
int insertMultiTableMultiValuesUsingSuperTable(@Param("tables") List<SubTableValue> tables);
|
||||
int insertMultiTableMultiValuesUsingSuperTable(List<SubTableValue> tables);
|
||||
|
||||
//<!-- TODO:修改子表标签值 alter table ${tablename} set tag tagName=newTagValue-->
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
package com.taosdata.taosdemo.dao;
|
||||
|
||||
import com.taosdata.taosdemo.domain.SubTableMeta;
|
||||
import com.taosdata.taosdemo.domain.SubTableValue;
|
||||
import com.taosdata.taosdemo.utils.SqlSpeller;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
|
||||
public class SubTableMapperImpl implements SubTableMapper {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(SubTableMapperImpl.class);
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public SubTableMapperImpl(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createUsingSuperTable(SubTableMeta subTableMeta) {
|
||||
String sql = SqlSpeller.createTableUsingSuperTable(subTableMeta);
|
||||
logger.info("SQL >>> " + sql);
|
||||
jdbcTemplate.execute(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertOneTableMultiValues(SubTableValue subTableValue) {
|
||||
String sql = SqlSpeller.insertOneTableMultiValues(subTableValue);
|
||||
logger.info("SQL >>> " + sql);
|
||||
|
||||
int affectRows = 0;
|
||||
try {
|
||||
affectRows = jdbcTemplate.update(sql);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return affectRows;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertOneTableMultiValuesUsingSuperTable(SubTableValue subTableValue) {
|
||||
String sql = SqlSpeller.insertOneTableMultiValuesUsingSuperTable(subTableValue);
|
||||
logger.info("SQL >>> " + sql);
|
||||
|
||||
int affectRows = 0;
|
||||
try {
|
||||
affectRows = jdbcTemplate.update(sql);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return affectRows;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertMultiTableMultiValues(List<SubTableValue> tables) {
|
||||
String sql = SqlSpeller.insertMultiSubTableMultiValues(tables);
|
||||
logger.info("SQL >>> " + sql);
|
||||
int affectRows = 0;
|
||||
try {
|
||||
affectRows = jdbcTemplate.update(sql);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return affectRows;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertMultiTableMultiValuesUsingSuperTable(List<SubTableValue> tables) {
|
||||
String sql = SqlSpeller.insertMultiTableMultiValuesUsingSuperTable(tables);
|
||||
logger.info("SQL >>> " + sql);
|
||||
int affectRows = 0;
|
||||
try {
|
||||
affectRows = jdbcTemplate.update(sql);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return affectRows;
|
||||
}
|
||||
}
|
|
@ -1,20 +1,16 @@
|
|||
package com.taosdata.taosdemo.mapper;
|
||||
package com.taosdata.taosdemo.dao;
|
||||
|
||||
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface SuperTableMapper {
|
||||
|
||||
// 创建超级表,使用自己定义的SQL语句
|
||||
int createSuperTableUsingSQL(@Param("createSuperTableSQL") String sql);
|
||||
|
||||
// 创建超级表 create table if not exists xxx.xxx (f1 type1, f2 type2, ... ) tags( t1 type1, t2 type2 ...)
|
||||
int createSuperTable(SuperTableMeta tableMetadata);
|
||||
void createSuperTable(SuperTableMeta tableMetadata);
|
||||
|
||||
// 删除超级表 drop table if exists xxx;
|
||||
int dropSuperTable(@Param("database") String database, @Param("name") String name);
|
||||
void dropSuperTable(String database, String name);
|
||||
|
||||
//<!-- TODO:查询所有超级表信息 show stables -->
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package com.taosdata.taosdemo.dao;
|
||||
|
||||
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||
import com.taosdata.taosdemo.utils.SqlSpeller;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
public class SuperTableMapperImpl implements SuperTableMapper {
|
||||
private static final Logger logger = Logger.getLogger(SuperTableMapperImpl.class);
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public SuperTableMapperImpl(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createSuperTable(SuperTableMeta tableMetadata) {
|
||||
String sql = SqlSpeller.createSuperTable(tableMetadata);
|
||||
logger.info("SQL >>> " + sql);
|
||||
jdbcTemplate.execute(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropSuperTable(String database, String name) {
|
||||
String sql = "drop table if exists " + database + "." + name;
|
||||
logger.info("SQL >>> " + sql);
|
||||
jdbcTemplate.execute(sql);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue