TDengine/source/dnode/vnode/src/tsdb/dev/tsdbFile.c

245 lines
7.4 KiB
C
Raw Normal View History

2023-03-23 09:15:51 +00:00
/*
* 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/>.
*/
2023-05-08 02:27:55 +00:00
#include "inc/tsdbFile.h"
2023-03-23 09:15:51 +00:00
2023-05-08 10:15:31 +00:00
// to_json
2023-05-08 08:16:45 +00:00
static int32_t head_to_json(const STFile *file, cJSON *json);
static int32_t data_to_json(const STFile *file, cJSON *json);
static int32_t sma_to_json(const STFile *file, cJSON *json);
static int32_t tomb_to_json(const STFile *file, cJSON *json);
static int32_t stt_to_json(const STFile *file, cJSON *json);
2023-05-08 10:15:31 +00:00
// from_json
static int32_t head_from_json(const cJSON *json, STFile *file);
static int32_t data_from_json(const cJSON *json, STFile *file);
static int32_t sma_from_json(const cJSON *json, STFile *file);
static int32_t tomb_from_json(const cJSON *json, STFile *file);
static int32_t stt_from_json(const cJSON *json, STFile *file);
2023-05-08 08:16:45 +00:00
static const struct {
const char *suffix;
int32_t (*to_json)(const STFile *file, cJSON *json);
2023-05-08 10:15:31 +00:00
int32_t (*from_json)(const cJSON *json, STFile *file);
2023-05-08 08:16:45 +00:00
} g_tfile_info[] = {
2023-05-08 10:15:31 +00:00
[TSDB_FTYPE_HEAD] = {"head", head_to_json, head_from_json},
[TSDB_FTYPE_DATA] = {"data", data_to_json, data_from_json},
[TSDB_FTYPE_SMA] = {"sma", sma_to_json, sma_from_json},
[TSDB_FTYPE_TOMB] = {"tomb", tomb_to_json, tomb_from_json},
[TSDB_FTYPE_STT] = {"stt", stt_to_json, stt_from_json},
2023-03-23 10:40:44 +00:00
};
2023-05-08 08:16:45 +00:00
static int32_t tfile_to_json(const STFile *file, cJSON *json) {
2023-05-08 10:15:31 +00:00
/* did.level */
2023-05-08 08:16:45 +00:00
if (cJSON_AddNumberToObject(json, "did.level", file->did.level) == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2023-05-08 10:15:31 +00:00
/* did.id */
2023-05-08 08:16:45 +00:00
if (cJSON_AddNumberToObject(json, "did.id", file->did.id) == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2023-05-08 10:15:31 +00:00
/* fid */
2023-05-08 08:16:45 +00:00
if (cJSON_AddNumberToObject(json, "fid", file->fid) == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2023-05-08 10:15:31 +00:00
/* cid */
2023-05-08 08:16:45 +00:00
if (cJSON_AddNumberToObject(json, "cid", file->cid) == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2023-05-08 10:15:31 +00:00
/* size */
2023-05-08 08:16:45 +00:00
if (cJSON_AddNumberToObject(json, "size", file->size) == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
return 0;
}
2023-05-08 10:15:31 +00:00
static int32_t tfile_from_json(const cJSON *json, STFile *file) {
const cJSON *item;
2023-05-08 08:16:45 +00:00
2023-05-08 10:15:31 +00:00
/* did.level */
item = cJSON_GetObjectItem(json, "did.level");
if (cJSON_IsNumber(item)) {
file->did.level = item->valuedouble;
} else {
return TSDB_CODE_FILE_CORRUPTED;
}
2023-05-08 08:16:45 +00:00
2023-05-08 10:15:31 +00:00
/* did.id */
item = cJSON_GetObjectItem(json, "did.id");
if (cJSON_IsNumber(item)) {
file->did.id = item->valuedouble;
} else {
return TSDB_CODE_FILE_CORRUPTED;
}
2023-05-08 08:16:45 +00:00
2023-05-08 10:15:31 +00:00
/* fid */
item = cJSON_GetObjectItem(json, "fid");
if (cJSON_IsNumber(item)) {
file->fid = item->valuedouble;
} else {
return TSDB_CODE_FILE_CORRUPTED;
}
2023-05-08 08:16:45 +00:00
2023-05-08 10:15:31 +00:00
/* cid */
item = cJSON_GetObjectItem(json, "cid");
if (cJSON_IsNumber(item)) {
file->cid = item->valuedouble;
} else {
return TSDB_CODE_FILE_CORRUPTED;
}
/* size */
item = cJSON_GetObjectItem(json, "size");
if (cJSON_IsNumber(item)) {
file->size = item->valuedouble;
} else {
return TSDB_CODE_FILE_CORRUPTED;
}
return 0;
}
static int32_t head_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
static int32_t data_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
static int32_t sma_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
static int32_t tomb_to_json(const STFile *file, cJSON *json) { return tfile_to_json(file, json); }
2023-05-08 08:16:45 +00:00
static int32_t stt_to_json(const STFile *file, cJSON *json) {
int32_t code = tfile_to_json(file, json);
if (code) return code;
2023-05-08 10:15:31 +00:00
/* lvl */
2023-05-08 08:16:45 +00:00
if (cJSON_AddNumberToObject(json, "lvl", file->stt.lvl) == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2023-05-08 10:15:31 +00:00
/* nseg */
2023-05-08 08:16:45 +00:00
if (cJSON_AddNumberToObject(json, "nseg", file->stt.nseg) == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
return 0;
}
2023-05-08 10:15:31 +00:00
static int32_t head_from_json(const cJSON *json, STFile *file) { return tfile_from_json(json, file); }
static int32_t data_from_json(const cJSON *json, STFile *file) { return tfile_from_json(json, file); }
static int32_t sma_from_json(const cJSON *json, STFile *file) { return tfile_from_json(json, file); }
static int32_t tomb_from_json(const cJSON *json, STFile *file) { return tfile_from_json(json, file); }
static int32_t stt_from_json(const cJSON *json, STFile *file) {
int32_t code = tfile_from_json(json, file);
if (code) return code;
const cJSON *item;
/* lvl */
item = cJSON_GetObjectItem(json, "lvl");
if (cJSON_IsNumber(item)) {
file->stt.lvl = item->valuedouble;
} else {
return TSDB_CODE_FILE_CORRUPTED;
}
/* nseg */
item = cJSON_GetObjectItem(json, "nseg");
if (cJSON_IsNumber(item)) {
file->stt.nseg = item->valuedouble;
} else {
return TSDB_CODE_FILE_CORRUPTED;
}
return 0;
}
2023-05-08 02:27:55 +00:00
int32_t tsdbTFileInit(STsdb *pTsdb, STFile *pFile) {
2023-03-28 08:43:22 +00:00
SVnode *pVnode = pTsdb->pVnode;
STfs *pTfs = pVnode->pTfs;
if (pTfs) {
2023-05-08 08:16:45 +00:00
snprintf(pFile->fname, //
TSDB_FILENAME_LEN, //
"%s%s%s%sv%df%dver%" PRId64 ".%s", //
tfsGetDiskPath(pTfs, pFile->did), //
TD_DIRSEP, //
pTsdb->path, //
TD_DIRSEP, //
TD_VID(pVnode), //
pFile->fid, //
pFile->cid, //
g_tfile_info[pFile->type].suffix);
2023-03-28 08:43:22 +00:00
} else {
2023-05-08 08:16:45 +00:00
snprintf(pFile->fname, //
TSDB_FILENAME_LEN, //
"%s%sv%df%dver%" PRId64 ".%s", //
pTsdb->path, //
TD_DIRSEP, //
TD_VID(pVnode), //
pFile->fid, //
pFile->cid, //
g_tfile_info[pFile->type].suffix);
2023-03-28 08:43:22 +00:00
}
2023-05-15 09:14:45 +00:00
// pFile->ref = 1;
2023-03-28 07:38:55 +00:00
return 0;
}
2023-05-08 02:27:55 +00:00
int32_t tsdbTFileClear(STFile *pFile) {
// TODO
return 0;
2023-05-08 08:16:45 +00:00
}
int32_t tsdbTFileToJson(const STFile *file, cJSON *json) {
cJSON *tjson = cJSON_AddObjectToObject(json, g_tfile_info[file->type].suffix);
if (tjson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
return g_tfile_info[file->type].to_json(file, tjson);
2023-05-08 10:15:31 +00:00
}
int32_t tsdbTFileFromJson(const cJSON *json, tsdb_ftype_t ftype, STFile **f) {
const cJSON *item = cJSON_GetObjectItem(json, g_tfile_info[ftype].suffix);
if (cJSON_IsObject(item)) {
f[0] = (STFile *)taosMemoryMalloc(sizeof(*f[0]));
if (f[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
int32_t code = g_tfile_info[ftype].from_json(item, f[0]);
if (code) {
taosMemoryFree(f[0]);
f[0] = NULL;
return code;
}
tsdbTFileInit(NULL /* TODO */, f[0]);
} else {
f[0] = NULL;
}
2023-05-15 07:47:06 +00:00
return 0;
}
int32_t tsdbTFileCreate(const STFile *pFile, STFile **f) {
f[0] = taosMemoryMalloc(sizeof(*f[0]));
if (f[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
*f[0] = *pFile;
2023-05-15 09:14:45 +00:00
// f[0]->ref = 1;
2023-05-15 07:47:06 +00:00
return 0;
}
int32_t tsdbTFileDestroy(STFile *pFile) {
taosMemoryFree(pFile);
2023-05-08 10:15:31 +00:00
return 0;
2023-05-08 02:27:55 +00:00
}