tsdbFile2.c 8.9 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "tsdbFile2.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
// to_json
H
Hongze Cheng 已提交
19 20 21 22 23 24
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);

H
Hongze Cheng 已提交
25 26 27 28 29 30 31
// 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);

H
Hongze Cheng 已提交
32 33 34
static const struct {
  const char *suffix;
  int32_t (*to_json)(const STFile *file, cJSON *json);
H
Hongze Cheng 已提交
35
  int32_t (*from_json)(const cJSON *json, STFile *file);
H
Hongze Cheng 已提交
36
} g_tfile_info[] = {
H
Hongze Cheng 已提交
37 38 39 40 41
    [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},
H
Hongze Cheng 已提交
42 43
};

H
Hongze Cheng 已提交
44 45 46 47 48
static void remove_file(const char *fname) {
  taosRemoveFile(fname);
  tsdbInfo("file:%s is removed", fname);
}

H
Hongze Cheng 已提交
49
static int32_t tfile_to_json(const STFile *file, cJSON *json) {
H
Hongze Cheng 已提交
50
  /* did.level */
H
Hongze Cheng 已提交
51 52 53 54
  if (cJSON_AddNumberToObject(json, "did.level", file->did.level) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
55
  /* did.id */
H
Hongze Cheng 已提交
56 57 58 59
  if (cJSON_AddNumberToObject(json, "did.id", file->did.id) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
60
  /* fid */
H
Hongze Cheng 已提交
61 62 63 64
  if (cJSON_AddNumberToObject(json, "fid", file->fid) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
65
  /* cid */
H
Hongze Cheng 已提交
66 67 68 69
  if (cJSON_AddNumberToObject(json, "cid", file->cid) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
70
  /* size */
H
Hongze Cheng 已提交
71 72 73 74 75 76 77
  if (cJSON_AddNumberToObject(json, "size", file->size) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  return 0;
}

H
Hongze Cheng 已提交
78 79
static int32_t tfile_from_json(const cJSON *json, STFile *file) {
  const cJSON *item;
H
Hongze Cheng 已提交
80

H
Hongze Cheng 已提交
81 82 83 84 85 86 87
  /* did.level */
  item = cJSON_GetObjectItem(json, "did.level");
  if (cJSON_IsNumber(item)) {
    file->did.level = item->valuedouble;
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }
H
Hongze Cheng 已提交
88

H
Hongze Cheng 已提交
89 90 91 92 93 94 95
  /* did.id */
  item = cJSON_GetObjectItem(json, "did.id");
  if (cJSON_IsNumber(item)) {
    file->did.id = item->valuedouble;
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }
H
Hongze Cheng 已提交
96

H
Hongze Cheng 已提交
97 98 99 100 101 102 103
  /* fid */
  item = cJSON_GetObjectItem(json, "fid");
  if (cJSON_IsNumber(item)) {
    file->fid = item->valuedouble;
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }
H
Hongze Cheng 已提交
104

H
Hongze Cheng 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  /* 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); }
H
Hongze Cheng 已提交
128 129 130 131
static int32_t stt_to_json(const STFile *file, cJSON *json) {
  int32_t code = tfile_to_json(file, json);
  if (code) return code;

H
Hongze Cheng 已提交
132
  /* lvl */
H
Hongze Cheng 已提交
133
  if (cJSON_AddNumberToObject(json, "level", file->stt->level) == NULL) {
H
Hongze Cheng 已提交
134 135 136 137 138 139
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  return 0;
}

H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148 149 150
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 */
H
Hongze Cheng 已提交
151
  item = cJSON_GetObjectItem(json, "level");
H
Hongze Cheng 已提交
152
  if (cJSON_IsNumber(item)) {
H
Hongze Cheng 已提交
153
    file->stt->level = item->valuedouble;
H
Hongze Cheng 已提交
154 155 156 157 158 159 160
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }

  return 0;
}

H
Hongze Cheng 已提交
161
int32_t tsdbTFileToJson(const STFile *file, cJSON *json) {
H
Hongze Cheng 已提交
162 163 164 165 166 167 168
  if (file->type == TSDB_FTYPE_STT) {
    return g_tfile_info[file->type].to_json(file, json);
  } else {
    cJSON *item = cJSON_AddObjectToObject(json, g_tfile_info[file->type].suffix);
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
    return g_tfile_info[file->type].to_json(file, item);
  }
H
Hongze Cheng 已提交
169 170
}

H
Hongze Cheng 已提交
171 172 173 174 175 176
int32_t tsdbJsonToTFile(const cJSON *json, tsdb_ftype_t ftype, STFile *f) {
  f[0] = (STFile){.type = ftype};

  if (ftype == TSDB_FTYPE_STT) {
    int32_t code = g_tfile_info[ftype].from_json(json, f);
    if (code) return code;
H
Hongze Cheng 已提交
177
  } else {
H
Hongze Cheng 已提交
178 179 180 181 182 183 184
    const cJSON *item = cJSON_GetObjectItem(json, g_tfile_info[ftype].suffix);
    if (cJSON_IsObject(item)) {
      int32_t code = g_tfile_info[ftype].from_json(item, f);
      if (code) return code;
    } else {
      return TSDB_CODE_NOT_FOUND;
    }
H
Hongze Cheng 已提交
185 186
  }

H
Hongze Cheng 已提交
187 188 189
  return 0;
}

H
Hongze Cheng 已提交
190
int32_t tsdbTFileObjInit(STsdb *pTsdb, const STFile *f, STFileObj **fobj) {
H
Hongze Cheng 已提交
191
  fobj[0] = taosMemoryMalloc(sizeof(*fobj[0]));
H
Hongze Cheng 已提交
192
  if (!fobj[0]) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
193

H
Hongze Cheng 已提交
194
  taosThreadMutexInit(&fobj[0]->mutex, NULL);
H
Hongze Cheng 已提交
195
  fobj[0]->f[0] = f[0];
H
Hongze Cheng 已提交
196
  fobj[0]->state = TSDB_FSTATE_LIVE;
H
Hongze Cheng 已提交
197
  fobj[0]->ref = 1;
H
Hongze Cheng 已提交
198
  tsdbTFileName(pTsdb, f, fobj[0]->fname);
H
Hongze Cheng 已提交
199 200 201
  return 0;
}

H
Hongze Cheng 已提交
202
int32_t tsdbTFileObjRef(STFileObj *fobj) {
H
Hongze Cheng 已提交
203 204
  int32_t nRef;
  taosThreadMutexLock(&fobj->mutex);
H
Hongze Cheng 已提交
205
  ASSERT(fobj->ref > 0 && fobj->state == TSDB_FSTATE_LIVE);
H
Hongze Cheng 已提交
206 207
  nRef = fobj->ref++;
  taosThreadMutexUnlock(&fobj->mutex);
H
Hongze Cheng 已提交
208 209 210 211
  return 0;
}

int32_t tsdbTFileObjUnref(STFileObj *fobj) {
H
Hongze Cheng 已提交
212
  taosThreadMutexLock(&fobj->mutex);
H
Hongze Cheng 已提交
213
  int32_t nRef = --fobj->ref;
H
Hongze Cheng 已提交
214
  taosThreadMutexUnlock(&fobj->mutex);
H
Hongze Cheng 已提交
215
  ASSERT(nRef >= 0);
H
Hongze Cheng 已提交
216
  if (nRef == 0) {
H
Hongze Cheng 已提交
217 218
    if (fobj->state == TSDB_FSTATE_DEAD) {
      remove_file(fobj->fname);
H
Hongze Cheng 已提交
219 220 221
    }
    taosMemoryFree(fobj);
  }
H
Hongze Cheng 已提交
222

H
Hongze Cheng 已提交
223 224 225
  return 0;
}

H
Hongze Cheng 已提交
226
int32_t tsdbTFileObjRemove(STFileObj *fobj) {
H
Hongze Cheng 已提交
227
  taosThreadMutexLock(&fobj->mutex);
H
Hongze Cheng 已提交
228 229
  ASSERT(fobj->state == TSDB_FSTATE_LIVE && fobj->ref > 0);
  fobj->state = TSDB_FSTATE_DEAD;
H
Hongze Cheng 已提交
230 231
  int32_t nRef = --fobj->ref;
  taosThreadMutexUnlock(&fobj->mutex);
H
Hongze Cheng 已提交
232
  if (nRef == 0) {
H
Hongze Cheng 已提交
233
    remove_file(fobj->fname);
H
Hongze Cheng 已提交
234 235
    taosMemoryFree(fobj);
  }
H
Hongze Cheng 已提交
236
  return 0;
H
Hongze Cheng 已提交
237
}
H
Hongze Cheng 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

int32_t tsdbTFileName(STsdb *pTsdb, const STFile *f, char fname[]) {
  SVnode *pVnode = pTsdb->pVnode;
  STfs   *pTfs = pVnode->pTfs;

  if (pTfs) {
    snprintf(fname,                              //
             TSDB_FILENAME_LEN,                  //
             "%s%s%s%sv%df%dver%" PRId64 ".%s",  //
             tfsGetDiskPath(pTfs, f->did),       //
             TD_DIRSEP,                          //
             pTsdb->path,                        //
             TD_DIRSEP,                          //
             TD_VID(pVnode),                     //
             f->fid,                             //
             f->cid,                             //
             g_tfile_info[f->type].suffix);
  } else {
    snprintf(fname,                          //
             TSDB_FILENAME_LEN,              //
             "%s%sv%df%dver%" PRId64 ".%s",  //
             pTsdb->path,                    //
             TD_DIRSEP,                      //
             TD_VID(pVnode),                 //
             f->fid,                         //
             f->cid,                         //
             g_tfile_info[f->type].suffix);
  }
  return 0;
H
Hongze Cheng 已提交
267 268
}

H
Hongze Cheng 已提交
269 270 271 272 273 274 275 276 277 278 279
bool tsdbIsSameTFile(const STFile *f1, const STFile *f2) {
  if (f1->type != f2->type) return false;
  if (f1->did.level != f2->did.level) return false;
  if (f1->did.id != f2->did.id) return false;
  if (f1->fid != f2->fid) return false;
  if (f1->cid != f2->cid) return false;
  return true;
}

bool tsdbIsTFileChanged(const STFile *f1, const STFile *f2) {
  if (f1->size != f2->size) return true;
280
  // if (f1->type == TSDB_FTYPE_STT && f1->stt->nseg != f2->stt->nseg) return true;
H
Hongze Cheng 已提交
281 282 283
  return false;
}

H
Hongze Cheng 已提交
284
int32_t tsdbTFileObjCmpr(const STFileObj **fobj1, const STFileObj **fobj2) {
H
Hongze Cheng 已提交
285
  if (fobj1[0]->f->cid < fobj2[0]->f->cid) {
H
Hongze Cheng 已提交
286
    return -1;
H
Hongze Cheng 已提交
287
  } else if (fobj1[0]->f->cid > fobj2[0]->f->cid) {
H
Hongze Cheng 已提交
288 289 290 291
    return 1;
  } else {
    return 0;
  }
H
Hongze Cheng 已提交
292
}