提交 de30e247 编写于 作者: Z zyyang

Merge branch 'feature/TD-5992' of github.com:taosdata/TDengine into feature/TD-5992

......@@ -442,12 +442,13 @@ int taos_options(TSDB_OPTION option, const void *arg, ...) {
static int taos_set_config_imp(const char *config){
static bool setConfFlag = false;
if (setConfFlag) {
tscError("already set config");
printf("already set config");
return 0;
}
taosInitGlobalCfg();
cJSON *root = cJSON_Parse(config);
if (root == NULL) {
tscError("failed to set config, invalid json format: %s", config);
printf("failed to set config, invalid json format: %s", config);
return -1;
}
......@@ -456,30 +457,25 @@ static int taos_set_config_imp(const char *config){
for(int i = 0; i < size; i++){
cJSON *item = cJSON_GetArrayItem(root, i);
if (!item) {
tscError("failed to read index:%d", i);
printf("failed to read index:%d", i);
ret = -2;
continue;
}
if(!taosReadConfigOption(item->string, item->valuestring, NULL, NULL)){
if(!taosReadConfigOption(item->string, item->valuestring, NULL, NULL, TAOS_CFG_CSTATUS_OPTION)){
printf("set failed:%s->%s", item->string, item->valuestring);
ret = -2;
}
}
taosPrintGlobalCfg();
setConfFlag = true;
return ret;
}
int taos_set_config(const char *config){
if(taos_init()){
tscError("failed to call taos_init");
return -1;
}
static int32_t lock = 0;
for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) {
if (i % 1000 == 0) {
tscInfo("haven't acquire lock after spin %d times.", i);
printf("haven't acquire lock after spin %d times.", i);
sched_yield();
}
}
......
......@@ -259,12 +259,12 @@ void validateResultFields() {
}
/* test parse time function */
TEST(testCase, result_field_test) {
const char *config = "{\"debugFlag\":\"135\"}";
taos_set_config(config);
taos_set_config(config);
taos_options(TSDB_OPTION_CONFIGDIR, "~/first/cfg");
taos_init();
const char *config = "{\"tempDir\":\"/root/tmp/\",\"serverPort\":\"3333\"}";
taos_set_config(config);
taos_set_config(config);
validateResultFields();
stmtInsertTest();
}
......@@ -57,13 +57,17 @@ public class TSDBJNIConnector {
}
}
public static native void initImp(String configDir);
private static native void initImp(String configDir);
public static native int setOptions(int optionIndex, String optionValue);
private static native int setOptions(int optionIndex, String optionValue);
public static native String getTsCharset();
private static native String getTsCharset();
public static native int setConfig(String config);
public static void setConfig(String config) {
setConfigImp(config);
}
private static native int setConfigImp(String config);
public boolean connect(String host, int port, String dbName, String user, String password) throws SQLException {
if (this.taos != TSDBConstants.JNI_NULL_POINTER) {
......
package com.taosdata.jdbc;
import org.junit.After;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import static org.junit.Assert.*;
import java.sql.Statement;
public class SetConfigurationInJNITest {
private String host = "127.0.0.1";
private String dbname = "test_jni";
private String debugFlagJSON = "{ \"debugFlag\": \"135\"}";
private long maxSQLLength = 1024000;
private String debugFlagJSON = "{ \"debugFlag\": 135}";
private String maxSqlLengthJSON = "{ \"maxSQLLength\": " + maxSQLLength + "}";
@Test
public void testDebugFlag() {
public void setConfigBeforeConnectIsValid() {
try {
// init
TSDBJNIConnector.initImp(null);
TSDBJNIConnector.setOptions(0, null);
TSDBJNIConnector.setOptions(1, null);
TSDBJNIConnector.setOptions(2, null);
String tsCharset = TSDBJNIConnector.getTsCharset();
assertEquals("", tsCharset);
TSDBJNIConnector.setConfig(debugFlagJSON);
// connect
TSDBJNIConnector jniConnector = new TSDBJNIConnector();
boolean connected = jniConnector.connect(host, 0, null, "root", "taosdata");
assertTrue(connected);
Connection conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata");
Statement stmt = conn.createStatement();
String[] setupSqls = {
"drop database if exists " + dbname,
"create database if not exists " + dbname,
"use " + dbname,
"create table weather(ts timestamp, f1 int) tags(loc nchar(10))",
"insert into t1 using weather tags('beijing') values(now, 1)",
"drop database if exists " + dbname
};
Arrays.asList(setupSqls).forEach(sql -> {
try {
long setupSql = jniConnector.executeQuery(sql);
if (jniConnector.isUpdateQuery(setupSql)) {
jniConnector.freeResultSet(setupSql);
}
} catch (SQLException e) {
e.printStackTrace();
}
});
stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists " + dbname);
stmt.execute("use " + dbname);
stmt.execute("create table weather(ts timestamp, f1 int) tags(loc nchar(10))");
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void testMaxSQLLength() {
public void setConfigAfterConnectIsInvalid() {
try {
Connection conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata");
TSDBJNIConnector.setConfig(debugFlagJSON);
Statement stmt = conn.createStatement();
stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists " + dbname);
stmt.execute("use " + dbname);
stmt.execute("create table weather(ts timestamp, f1 int) tags(loc nchar(10))");
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
......@@ -89,7 +89,7 @@ void taosDumpGlobalCfg();
void taosInitConfigOption(SGlobalCfg cfg);
SGlobalCfg * taosGetConfigOption(const char *option);
bool taosReadConfigOption(const char *option, char *value, char *value2, char *value3);
bool taosReadConfigOption(const char *option, char *value, char *value2, char *value3, int8_t cfgStatus);
#ifdef __cplusplus
}
......
......@@ -293,7 +293,9 @@ SGlobalCfg *taosGetConfigOption(const char *option) {
return NULL;
}
bool taosReadConfigOption(const char *option, char *value, char *value2, char *value3) {
bool taosReadConfigOption(const char *option, char *value, char *value2, char *value3,
int8_t cfgStatus) {
bool ret = false;
for (int i = 0; i < tsGlobalConfigNum; ++i) {
SGlobalCfg *cfg = tsGlobalConfig + i;
if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_CONFIG)) continue;
......@@ -301,35 +303,48 @@ bool taosReadConfigOption(const char *option, char *value, char *value2, char *v
switch (cfg->valType) {
case TAOS_CFG_VTYPE_INT8:
return taosReadInt8Config(cfg, value);
ret = taosReadInt8Config(cfg, value);
break;
case TAOS_CFG_VTYPE_INT16:
return taosReadInt16Config(cfg, value);
ret = taosReadInt16Config(cfg, value);
break;
case TAOS_CFG_VTYPE_INT32:
return taosReadInt32Config(cfg, value);
ret = taosReadInt32Config(cfg, value);
break;
case TAOS_CFG_VTYPE_UINT16:
return taosReadUInt16Config(cfg, value);
ret = taosReadUInt16Config(cfg, value);
break;
case TAOS_CFG_VTYPE_FLOAT:
return taosReadFloatConfig(cfg, value);
ret = taosReadFloatConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_DOUBLE:
return taosReadDoubleConfig(cfg, value);
ret = taosReadDoubleConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_STRING:
return taosReadStringConfig(cfg, value);
ret = taosReadStringConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_IPSTR:
return taosReadIpStrConfig(cfg, value);
ret = taosReadIpStrConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_DIRECTORY:
return taosReadDirectoryConfig(cfg, value);
ret = taosReadDirectoryConfig(cfg, value);
break;
case TAOS_CFG_VTYPE_DATA_DIRCTORY:
if (taosReadDirectoryConfig(cfg, value)) {
taosReadDataDirCfg(value, value2, value3);
return true;
ret = true;
}
return false;
ret = false;
break;
default:
uError("config option:%s, input value:%s, can't be recognized", option, value);
return false;
ret = false;
}
if(ret && cfgStatus == TAOS_CFG_CSTATUS_OPTION){
cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
}
}
return false;
return ret;
}
void taosInitConfigOption(SGlobalCfg cfg) {
......@@ -464,7 +479,7 @@ bool taosReadGlobalCfg() {
if (vlen3 != 0) value3[vlen3] = 0;
}
taosReadConfigOption(option, value, value2, value3);
taosReadConfigOption(option, value, value2, value3, TAOS_CFG_CSTATUS_FILE);
}
fclose(fp);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册