2020-03-05 06:13:20 +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/>.
|
|
|
|
|
*/
|
2020-06-12 10:52:48 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
#include "tsdbMain.h"
|
2020-06-16 09:12:21 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
#define TSDB_FILE_OPENED(f) ((f)->fd >= 0)
|
|
|
|
|
#define TSDB_FILE_SET_CLOSED(f) ((f)->fd = -1)
|
2020-06-12 10:52:48 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
// ============== Operations on SMFile
|
|
|
|
|
void tsdbInitMFile(SMFile *pMFile, int vid, int ver, SMFInfo *pInfo) {
|
|
|
|
|
TSDB_FILE_SET_CLOSED(pMFile);
|
|
|
|
|
if (pInfo == NULL) {
|
|
|
|
|
memset(&(pMFile->info), 0, sizeof(pMFile->info));
|
|
|
|
|
pMFile->info.magic = TSDB_FILE_INIT_MAGIC;
|
|
|
|
|
} else {
|
|
|
|
|
pMFile->info = *pInfo;
|
2020-06-12 10:52:48 +00:00
|
|
|
}
|
2021-01-04 12:07:54 +00:00
|
|
|
tfsInitFile(&(pMFile->f), TFS_PRIMARY_LEVEL, TFS_PRIMARY_ID, NULL /*TODO*/);
|
2020-06-12 10:52:48 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return pMFile;
|
2020-06-12 10:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int tsdbOpenMFile(SMFile *pMFile, int flags) {
|
|
|
|
|
ASSERT(!TSDB_FILE_OPENED(pMFile));
|
2020-03-24 09:48:29 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
pMFile->fd = open(pMFile->f.aname, flags);
|
|
|
|
|
if (pMFile->fd < 0) {
|
|
|
|
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
2020-11-26 03:47:11 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2020-11-25 14:20:07 +00:00
|
|
|
|
2020-06-16 02:59:18 +00:00
|
|
|
return 0;
|
2020-03-24 09:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
void tsdbCloseMFile(SMFile *pMFile) {
|
|
|
|
|
if (TSDB_FILE_OPENED(pMFile)) {
|
|
|
|
|
close(pMFile->fd);
|
|
|
|
|
TSDB_FILE_SET_CLOSED(pMFile);
|
2020-05-05 09:18:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-24 09:48:29 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t tsdbSeekMFile(SMFile *pMFile, int64_t offset, int whence) {
|
|
|
|
|
ASSERT(TSDB_FILE_OPENED(pMFile));
|
2020-06-17 04:06:17 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t loffset = taosLSeek(pMFile->fd, offset, whence);
|
|
|
|
|
if (loffset < 0) {
|
|
|
|
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-11-23 08:59:06 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return loffset;
|
|
|
|
|
}
|
2020-11-23 08:59:06 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t tsdbWriteMFile(SMFile *pMFile, void *buf, int64_t nbyte) {
|
|
|
|
|
ASSERT(TSDB_FILE_OPENED(pMFile));
|
2020-11-24 16:06:39 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t nwrite = taosWrite(pMFile->fd, buf, nbyte);
|
|
|
|
|
if (nwrite < nbyte) {
|
|
|
|
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-03-24 09:48:29 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
pMFile->info.size += nbyte;
|
|
|
|
|
return nwrite;
|
|
|
|
|
}
|
2020-11-23 13:31:18 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t tsdbTellMFile(SMFile *pMFile) { return tsdbSeekMFile(pMFile, 0, SEEK_CUR); }
|
2020-11-23 13:31:18 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int tsdbEncodeMFile(void **buf, SMFile *pMFile) {
|
|
|
|
|
int tlen = 0;
|
2020-04-07 10:46:09 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
tlen += tsdbEncodeMFInfo(buf, &(pMFile->info));
|
|
|
|
|
tlen += tfsEncodeFile(buf, &(pMFile->f));
|
2020-04-07 10:46:09 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return tlen;
|
2020-11-23 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
void *tsdbDecodeMFile(void *buf, SMFile *pMFile) {
|
|
|
|
|
buf = tsdbDecodeMFInfo(buf, &(pMFile->info));
|
|
|
|
|
buf = tfsDecodeFile(buf, &(pMFile->f));
|
2020-11-23 13:31:18 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return buf;
|
|
|
|
|
}
|
2020-11-23 13:31:18 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
static int tsdbEncodeMFInfo(void **buf, SMFInfo *pInfo) {
|
|
|
|
|
int tlen = 0;
|
2020-11-23 13:31:18 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
tlen += taosEncodeVariantI64(buf, pInfo->size);
|
|
|
|
|
tlen += taosEncodeVariantI64(buf, pInfo->tombSize);
|
|
|
|
|
tlen += taosEncodeVariantI64(buf, pInfo->nRecords);
|
|
|
|
|
tlen += taosEncodeVariantI64(buf, pInfo->nDels);
|
|
|
|
|
tlen += taosEncodeFixedU32(buf, pInfo->magic);
|
2020-11-23 13:31:18 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return tlen;
|
2020-11-23 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
static void *tsdbDecodeMFInfo(void *buf, SMFInfo *pInfo) {
|
|
|
|
|
buf = taosDecodeVariantI64(buf, &(pInfo->size));
|
|
|
|
|
buf = taosDecodeVariantI64(buf, &(pInfo->tombSize));
|
|
|
|
|
buf = taosDecodeVariantI64(buf, &(pInfo->nRecords));
|
|
|
|
|
buf = taosDecodeVariantI64(buf, &(pInfo->nDels));
|
|
|
|
|
buf = taosDecodeFixedU32(buf, &(pInfo->magic));
|
|
|
|
|
|
|
|
|
|
return buf;
|
2020-11-23 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
// ============== Operations on SDFile
|
|
|
|
|
void tsdbInitDFile(SDFile *pDFile, int vid, int fid, int ver, int level, int id, const SDFInfo *pInfo, TSDB_FILE_T ftype) {
|
|
|
|
|
TSDB_FILE_SET_CLOSED(pDFile);
|
|
|
|
|
if (pInfo == NULL) {
|
|
|
|
|
memset(&(pDFile->info), 0, sizeof(pDFile->info));
|
|
|
|
|
pDFile->info.magic = TSDB_FILE_INIT_MAGIC;
|
2020-11-24 16:06:39 +00:00
|
|
|
} else {
|
2021-01-04 12:07:54 +00:00
|
|
|
pDFile->info = *pInfo;
|
2020-11-24 16:06:39 +00:00
|
|
|
}
|
2021-01-04 12:07:54 +00:00
|
|
|
tfsInitFile(&(pDFile->f), level, id, NULL /*TODO*/);
|
2020-11-24 16:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int tsdbOpenDFile(SDFile *pDFile, int flags) {
|
|
|
|
|
ASSERT(!TSDB_FILE_OPENED(pDFile));
|
2020-11-23 13:31:18 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
pDFile->fd = open(pDFile->f.aname, flags);
|
|
|
|
|
if (pDFile->fd < 0) {
|
|
|
|
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
2020-11-23 13:31:18 +00:00
|
|
|
return -1;
|
2020-10-22 07:26:05 +00:00
|
|
|
}
|
2020-03-24 09:48:29 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return 0;
|
2020-11-23 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
void tsdbCloseDFile(SDFile *pDFile) {
|
|
|
|
|
if (TSDB_FILE_OPENED(pDFile)) {
|
|
|
|
|
close(pDFile->fd);
|
|
|
|
|
TSDB_FILE_SET_CLOSED(pDFile);
|
2020-03-30 08:44:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t tsdbSeekDFile(SDFile *pDFile, int64_t offset, int whence) {
|
|
|
|
|
ASSERT(TSDB_FILE_OPENED(pDFile));
|
2020-07-31 06:02:20 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t loffset = taosLSeek(pDFile->fd, offset, whence);
|
|
|
|
|
if (loffset < 0) {
|
|
|
|
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
|
|
|
return -1;
|
2020-04-04 04:40:51 +00:00
|
|
|
}
|
2020-06-17 02:45:36 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return loffset;
|
2020-03-30 08:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t tsdbWriteDFile(SDFile *pDFile, void *buf, int64_t nbyte) {
|
|
|
|
|
ASSERT(TSDB_FILE_OPENED(pDFile));
|
2020-03-30 08:44:56 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t nwrite = taosWrite(pDFile->fd, buf, nbyte);
|
|
|
|
|
if (nwrite < nbyte) {
|
|
|
|
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
|
|
|
return -1;
|
2020-07-31 06:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
pDFile->info.size += nbyte;
|
|
|
|
|
return nwrite;
|
2020-03-30 08:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int64_t tsdbTellDFile(SDFile *pDFile) { return tsdbSeekDFile(pDFile, 0, SEEK_CUR); }
|
2020-03-24 09:48:29 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int tsdbEncodeDFile(void **buf, SDFile *pDFile) {
|
|
|
|
|
int tlen = 0;
|
2020-08-03 05:16:10 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
tlen += tsdbEncodeDFInfo(buf, &(pDFile->info));
|
|
|
|
|
tlen += tfsEncodeFile(buf, &(pDFile->f));
|
2020-03-24 09:48:29 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return tlen;
|
2020-03-26 15:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
void *tsdbDecodeDFile(void *buf, SDFile *pDFile) {
|
|
|
|
|
buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
|
|
|
|
|
buf = tfsDecodeFile(buf, &(pDFile->f));
|
2020-06-17 04:06:17 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
return buf;
|
2020-06-17 04:06:17 +00:00
|
|
|
}
|
2020-06-17 02:45:36 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
static int tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo) {
|
2020-06-17 09:39:05 +00:00
|
|
|
int tlen = 0;
|
2021-01-04 12:07:54 +00:00
|
|
|
|
2020-07-17 09:32:56 +00:00
|
|
|
tlen += taosEncodeFixedU32(buf, pInfo->magic);
|
2020-06-17 09:39:05 +00:00
|
|
|
tlen += taosEncodeFixedU32(buf, pInfo->len);
|
|
|
|
|
tlen += taosEncodeFixedU32(buf, pInfo->totalBlocks);
|
|
|
|
|
tlen += taosEncodeFixedU32(buf, pInfo->totalSubBlocks);
|
2020-07-20 08:14:55 +00:00
|
|
|
tlen += taosEncodeFixedU32(buf, pInfo->offset);
|
|
|
|
|
tlen += taosEncodeFixedU64(buf, pInfo->size);
|
|
|
|
|
tlen += taosEncodeFixedU64(buf, pInfo->tombSize);
|
2020-06-17 06:28:10 +00:00
|
|
|
|
2020-06-17 09:39:05 +00:00
|
|
|
return tlen;
|
2020-06-17 06:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo) {
|
2020-07-17 09:32:56 +00:00
|
|
|
buf = taosDecodeFixedU32(buf, &(pInfo->magic));
|
2020-06-17 06:28:10 +00:00
|
|
|
buf = taosDecodeFixedU32(buf, &(pInfo->len));
|
|
|
|
|
buf = taosDecodeFixedU32(buf, &(pInfo->totalBlocks));
|
|
|
|
|
buf = taosDecodeFixedU32(buf, &(pInfo->totalSubBlocks));
|
2020-07-20 08:14:55 +00:00
|
|
|
buf = taosDecodeFixedU32(buf, &(pInfo->offset));
|
|
|
|
|
buf = taosDecodeFixedU64(buf, &(pInfo->size));
|
|
|
|
|
buf = taosDecodeFixedU64(buf, &(pInfo->tombSize));
|
2020-06-17 06:28:10 +00:00
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
// ============== Operations on SDFileSet
|
|
|
|
|
void tsdbInitDFileSet(SDFileSet *pSet, int vid, int fid, int ver, int level, int id) {
|
|
|
|
|
pSet->fid = fid;
|
|
|
|
|
pSet->state = 0;
|
2020-11-25 09:11:04 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
|
|
|
|
|
SDFile *pDFile = TSDB_DFILE_IN_SET(pSet, ftype);
|
|
|
|
|
tsdbInitDFile(pDFile, vid, fid, ver, level, id, NULL, ftype);
|
|
|
|
|
// TODO: reset level and id
|
2020-11-25 09:11:04 +00:00
|
|
|
}
|
2020-11-25 14:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int tsdbOpenDFileSet(SDFileSet *pSet, int flags) {
|
|
|
|
|
for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
|
|
|
|
|
SDFile *pDFile = TSDB_DFILE_IN_SET(pSet, ftype);
|
2020-11-25 14:20:07 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
if (tsdbOpenDFile(pDFile, flags) < 0) {
|
|
|
|
|
tsdbCloseDFileSet(pSet);
|
|
|
|
|
return -1;
|
2020-11-25 14:20:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
void tsdbCloseDFileSet(SDFileSet *pSet) {
|
|
|
|
|
for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
|
|
|
|
|
tsdbCloseDFile(pDFile);
|
2020-11-26 03:47:11 +00:00
|
|
|
}
|
2020-11-25 14:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int tsdbUpdateDFileSetHeader(SDFileSet *pSet) {
|
|
|
|
|
// TODO
|
2020-11-26 03:47:11 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-11-25 14:20:07 +00:00
|
|
|
|
2021-01-04 12:07:54 +00:00
|
|
|
int tsdbMoveDFileSet(SDFileSet *pOldSet, int tolevel, SDFileSet *pNewSet) {
|
|
|
|
|
// TODO
|
|
|
|
|
return 0;
|
2020-11-23 08:59:06 +00:00
|
|
|
}
|