tsdbFile2.c 9.2 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
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
137
  /* nseg */
H
Hongze Cheng 已提交
138
  if (cJSON_AddNumberToObject(json, "nseg", file->stt->nseg) == NULL) {
H
Hongze Cheng 已提交
139 140 141 142 143 144
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  return 0;
}

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

  /* nseg */
  item = cJSON_GetObjectItem(json, "nseg");
  if (cJSON_IsNumber(item)) {
H
Hongze Cheng 已提交
166
    file->stt->nseg = item->valuedouble;
H
Hongze Cheng 已提交
167 168 169 170 171 172 173
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }

  return 0;
}

H
Hongze Cheng 已提交
174
int32_t tsdbTFileToJson(const STFile *file, cJSON *json) {
H
Hongze Cheng 已提交
175 176 177 178 179 180 181
  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 已提交
182 183
}

H
Hongze Cheng 已提交
184 185 186 187 188 189
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 已提交
190
  } else {
H
Hongze Cheng 已提交
191 192 193 194 195 196 197
    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 已提交
198 199
  }

H
Hongze Cheng 已提交
200 201 202
  return 0;
}

H
Hongze Cheng 已提交
203
int32_t tsdbTFileObjInit(STsdb *pTsdb, const STFile *f, STFileObj **fobj) {
H
Hongze Cheng 已提交
204
  fobj[0] = taosMemoryMalloc(sizeof(*fobj[0]));
H
Hongze Cheng 已提交
205
  if (!fobj[0]) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
206

H
Hongze Cheng 已提交
207
  taosThreadMutexInit(&fobj[0]->mutex, NULL);
H
Hongze Cheng 已提交
208
  fobj[0]->f[0] = f[0];
H
Hongze Cheng 已提交
209
  fobj[0]->state = TSDB_FSTATE_LIVE;
H
Hongze Cheng 已提交
210
  fobj[0]->ref = 1;
H
Hongze Cheng 已提交
211
  tsdbTFileName(pTsdb, f, fobj[0]->fname);
H
Hongze Cheng 已提交
212 213 214
  return 0;
}

H
Hongze Cheng 已提交
215
int32_t tsdbTFileObjRef(STFileObj *fobj) {
H
Hongze Cheng 已提交
216 217
  int32_t nRef;
  taosThreadMutexLock(&fobj->mutex);
H
Hongze Cheng 已提交
218
  ASSERT(fobj->ref > 0 && fobj->state == TSDB_FSTATE_LIVE);
H
Hongze Cheng 已提交
219 220
  nRef = fobj->ref++;
  taosThreadMutexUnlock(&fobj->mutex);
H
Hongze Cheng 已提交
221 222 223 224
  return 0;
}

int32_t tsdbTFileObjUnref(STFileObj *fobj) {
H
Hongze Cheng 已提交
225
  taosThreadMutexLock(&fobj->mutex);
H
Hongze Cheng 已提交
226
  int32_t nRef = --fobj->ref;
H
Hongze Cheng 已提交
227
  taosThreadMutexUnlock(&fobj->mutex);
H
Hongze Cheng 已提交
228
  ASSERT(nRef >= 0);
H
Hongze Cheng 已提交
229
  if (nRef == 0) {
H
Hongze Cheng 已提交
230 231
    if (fobj->state == TSDB_FSTATE_DEAD) {
      remove_file(fobj->fname);
H
Hongze Cheng 已提交
232 233 234
    }
    taosMemoryFree(fobj);
  }
H
Hongze Cheng 已提交
235

H
Hongze Cheng 已提交
236 237 238
  return 0;
}

H
Hongze Cheng 已提交
239
int32_t tsdbTFileObjRemove(STFileObj *fobj) {
H
Hongze Cheng 已提交
240
  taosThreadMutexLock(&fobj->mutex);
H
Hongze Cheng 已提交
241 242
  ASSERT(fobj->state == TSDB_FSTATE_LIVE && fobj->ref > 0);
  fobj->state = TSDB_FSTATE_DEAD;
H
Hongze Cheng 已提交
243 244
  int32_t nRef = --fobj->ref;
  taosThreadMutexUnlock(&fobj->mutex);
H
Hongze Cheng 已提交
245
  if (nRef == 0) {
H
Hongze Cheng 已提交
246
    remove_file(fobj->fname);
H
Hongze Cheng 已提交
247 248
    taosMemoryFree(fobj);
  }
H
Hongze Cheng 已提交
249
  return 0;
H
Hongze Cheng 已提交
250
}
H
Hongze Cheng 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

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 已提交
280 281
}

H
Hongze Cheng 已提交
282 283 284 285 286 287 288 289 290 291 292
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;
H
Hongze Cheng 已提交
293
  if (f1->type == TSDB_FTYPE_STT && f1->stt->nseg != f2->stt->nseg) return true;
H
Hongze Cheng 已提交
294 295 296
  return false;
}

H
Hongze Cheng 已提交
297
int32_t tsdbTFileObjCmpr(const STFileObj **fobj1, const STFileObj **fobj2) {
H
Hongze Cheng 已提交
298
  if (fobj1[0]->f->cid < fobj2[0]->f->cid) {
H
Hongze Cheng 已提交
299
    return -1;
H
Hongze Cheng 已提交
300
  } else if (fobj1[0]->f->cid > fobj2[0]->f->cid) {
H
Hongze Cheng 已提交
301 302 303 304
    return 1;
  } else {
    return 0;
  }
H
Hongze Cheng 已提交
305
}