tsdbFSet2.c 15.1 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 "tsdbFSet2.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
int32_t tsdbSttLvlInit(int32_t level, SSttLvl **lvl) {
H
Hongze Cheng 已提交
19
  if (!(lvl[0] = taosMemoryMalloc(sizeof(SSttLvl)))) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
20
  lvl[0]->level = level;
H
Hongze Cheng 已提交
21
  TARRAY2_INIT(lvl[0]->fobjArr);
H
Hongze Cheng 已提交
22 23
  return 0;
}
H
Hongze Cheng 已提交
24

H
Hongze Cheng 已提交
25 26
static void    tsdbSttLvlClearFObj(void *data) { tsdbTFileObjUnref(*(STFileObj **)data); }
static int32_t tsdbSttLvlClear(SSttLvl **lvl) {
H
Hongze Cheng 已提交
27
  TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlClearFObj);
H
Hongze Cheng 已提交
28 29 30 31
  taosMemoryFree(lvl[0]);
  lvl[0] = NULL;
  return 0;
}
H
Hongze Cheng 已提交
32

H
Hongze Cheng 已提交
33
static int32_t tsdbSttLvlInitEx(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
H
Hongze Cheng 已提交
34 35 36 37
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
  if (code) return code;

  const STFileObj *fobj1;
H
Hongze Cheng 已提交
38
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
H
Hongze Cheng 已提交
39
    STFileObj *fobj;
H
Hongze Cheng 已提交
40
    code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj);
H
Hongze Cheng 已提交
41 42 43 44 45
    if (code) {
      tsdbSttLvlClear(lvl);
      return code;
    }

H
Hongze Cheng 已提交
46
    TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
H
Hongze Cheng 已提交
47 48 49 50
  }
  return 0;
}

H
Hongze Cheng 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63
static int32_t tsdbSttLvlInitRef(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl **lvl) {
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
  if (code) return code;

  STFileObj *fobj1;
  TARRAY2_FOREACH(lvl1->fobjArr, fobj1) {
    tsdbTFileObjRef(fobj1);
    code = TARRAY2_APPEND(lvl[0]->fobjArr, fobj1);
    if (code) return code;
  }
  return 0;
}

H
Hongze Cheng 已提交
64 65
static void tsdbSttLvlRemoveFObj(void *data) { tsdbTFileObjRemove(*(STFileObj **)data); }
static void tsdbSttLvlRemove(SSttLvl **lvl) {
H
Hongze Cheng 已提交
66
  TARRAY2_DESTROY(lvl[0]->fobjArr, tsdbSttLvlRemoveFObj);
H
Hongze Cheng 已提交
67 68 69 70
  taosMemoryFree(lvl[0]);
  lvl[0] = NULL;
}

H
Hongze Cheng 已提交
71 72 73 74 75 76
static int32_t tsdbSttLvlApplyEdit(STsdb *pTsdb, const SSttLvl *lvl1, SSttLvl *lvl2) {
  int32_t code = 0;

  ASSERT(lvl1->level == lvl2->level);

  int32_t i1 = 0, i2 = 0;
H
Hongze Cheng 已提交
77 78 79
  while (i1 < TARRAY2_SIZE(lvl1->fobjArr) || i2 < TARRAY2_SIZE(lvl2->fobjArr)) {
    STFileObj *fobj1 = i1 < TARRAY2_SIZE(lvl1->fobjArr) ? TARRAY2_GET(lvl1->fobjArr, i1) : NULL;
    STFileObj *fobj2 = i2 < TARRAY2_SIZE(lvl2->fobjArr) ? TARRAY2_GET(lvl2->fobjArr, i2) : NULL;
H
Hongze Cheng 已提交
80 81

    if (fobj1 && fobj2) {
H
Hongze Cheng 已提交
82
      if (fobj1->f->cid < fobj2->f->cid) {
H
Hongze Cheng 已提交
83
        // create a file obj
H
Hongze Cheng 已提交
84
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
H
Hongze Cheng 已提交
85
        if (code) return code;
H
Hongze Cheng 已提交
86
        code = TARRAY2_APPEND(lvl2->fobjArr, fobj2);
H
Hongze Cheng 已提交
87 88 89
        if (code) return code;
        i1++;
        i2++;
H
Hongze Cheng 已提交
90
      } else if (fobj1->f->cid > fobj2->f->cid) {
H
Hongze Cheng 已提交
91
        // remove a file obj
H
Hongze Cheng 已提交
92
        TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
H
Hongze Cheng 已提交
93
      } else {
H
Hongze Cheng 已提交
94 95 96
        if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
          if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
            fobj2->f[0] = fobj1->f[0];
H
Hongze Cheng 已提交
97 98
          }
        } else {
H
Hongze Cheng 已提交
99
          TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
H
Hongze Cheng 已提交
100
          code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
H
Hongze Cheng 已提交
101
          if (code) return code;
H
Hongze Cheng 已提交
102
          code = TARRAY2_SORT_INSERT(lvl2->fobjArr, fobj2, tsdbTFileObjCmpr);
H
Hongze Cheng 已提交
103 104 105 106 107 108 109
          if (code) return code;
        }
        i1++;
        i2++;
      }
    } else if (fobj1) {
      // create a file obj
H
Hongze Cheng 已提交
110
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fobj2);
H
Hongze Cheng 已提交
111
      if (code) return code;
H
Hongze Cheng 已提交
112
      code = TARRAY2_APPEND(lvl2->fobjArr, fobj2);
H
Hongze Cheng 已提交
113 114 115 116 117
      if (code) return code;
      i1++;
      i2++;
    } else {
      // remove a file obj
H
Hongze Cheng 已提交
118
      TARRAY2_REMOVE(lvl2->fobjArr, i2, tsdbSttLvlRemoveFObj);
H
Hongze Cheng 已提交
119 120 121 122 123
    }
  }
  return 0;
}

H
Hongze Cheng 已提交
124 125 126
static int32_t tsdbSttLvlCmprFn(const SSttLvl **lvl1, const SSttLvl **lvl2) {
  if (lvl1[0]->level < lvl2[0]->level) return -1;
  if (lvl1[0]->level > lvl2[0]->level) return 1;
H
Hongze Cheng 已提交
127 128 129
  return 0;
}

H
Hongze Cheng 已提交
130
static int32_t tsdbSttLvlToJson(const SSttLvl *lvl, cJSON *json) {
H
Hongze Cheng 已提交
131
  if (cJSON_AddNumberToObject(json, "level", lvl->level) == NULL) {
H
Hongze Cheng 已提交
132 133 134
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
135
  cJSON *ajson = cJSON_AddArrayToObject(json, "files");
H
Hongze Cheng 已提交
136
  if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
137
  const STFileObj *fobj;
H
Hongze Cheng 已提交
138
  TARRAY2_FOREACH(lvl->fobjArr, fobj) {
H
Hongze Cheng 已提交
139 140
    cJSON *item = cJSON_CreateObject();
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
141
    cJSON_AddItemToArray(ajson, item);
H
Hongze Cheng 已提交
142

H
Hongze Cheng 已提交
143
    int32_t code = tsdbTFileToJson(fobj->f, item);
H
Hongze Cheng 已提交
144
    if (code) return code;
H
Hongze Cheng 已提交
145
  }
H
Hongze Cheng 已提交
146 147 148 149

  return 0;
}

H
Hongze Cheng 已提交
150
static int32_t tsdbJsonToSttLvl(STsdb *pTsdb, const cJSON *json, SSttLvl **lvl) {
H
Hongze Cheng 已提交
151
  const cJSON *item1, *item2;
H
Hongze Cheng 已提交
152
  int32_t      level;
H
Hongze Cheng 已提交
153

H
Hongze Cheng 已提交
154
  item1 = cJSON_GetObjectItem(json, "level");
H
Hongze Cheng 已提交
155
  if (cJSON_IsNumber(item1)) {
H
Hongze Cheng 已提交
156
    level = item1->valuedouble;
H
Hongze Cheng 已提交
157 158 159 160
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }

H
Hongze Cheng 已提交
161 162
  int32_t code = tsdbSttLvlInit(level, lvl);
  if (code) return code;
H
Hongze Cheng 已提交
163

H
Hongze Cheng 已提交
164
  item1 = cJSON_GetObjectItem(json, "files");
H
Hongze Cheng 已提交
165 166
  if (!cJSON_IsArray(item1)) {
    tsdbSttLvlClear(lvl);
H
Hongze Cheng 已提交
167 168 169
    return TSDB_CODE_FILE_CORRUPTED;
  }

H
Hongze Cheng 已提交
170 171 172 173 174 175 176
  cJSON_ArrayForEach(item2, item1) {
    STFile tf;
    code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &tf);
    if (code) {
      tsdbSttLvlClear(lvl);
      return code;
    }
H
Hongze Cheng 已提交
177

H
Hongze Cheng 已提交
178
    STFileObj *fobj;
H
Hongze Cheng 已提交
179
    code = tsdbTFileObjInit(pTsdb, &tf, &fobj);
H
Hongze Cheng 已提交
180 181 182 183
    if (code) {
      tsdbSttLvlClear(lvl);
      return code;
    }
H
Hongze Cheng 已提交
184

H
Hongze Cheng 已提交
185
    TARRAY2_APPEND(lvl[0]->fobjArr, fobj);
H
Hongze Cheng 已提交
186 187 188 189
  }
  return 0;
}

H
Hongze Cheng 已提交
190
int32_t tsdbTFileSetToJson(const STFileSet *fset, cJSON *json) {
H
Hongze Cheng 已提交
191
  int32_t code = 0;
H
Hongze Cheng 已提交
192
  cJSON  *item1, *item2;
H
Hongze Cheng 已提交
193

H
Hongze Cheng 已提交
194 195 196 197
  // fid
  if (cJSON_AddNumberToObject(json, "fid", fset->fid) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Hongze Cheng 已提交
198

H
Hongze Cheng 已提交
199
  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
H
Hongze Cheng 已提交
200
    if (fset->farr[ftype] == NULL) continue;
H
Hongze Cheng 已提交
201

H
Hongze Cheng 已提交
202
    code = tsdbTFileToJson(fset->farr[ftype]->f, json);
H
Hongze Cheng 已提交
203
    if (code) return code;
H
Hongze Cheng 已提交
204 205 206
  }

  // each level
H
Hongze Cheng 已提交
207
  item1 = cJSON_AddArrayToObject(json, "stt lvl");
H
Hongze Cheng 已提交
208
  if (item1 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
209
  const SSttLvl *lvl;
H
Hongze Cheng 已提交
210
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
H
Hongze Cheng 已提交
211 212 213 214
    item2 = cJSON_CreateObject();
    if (!item2) return TSDB_CODE_OUT_OF_MEMORY;
    cJSON_AddItemToArray(item1, item2);

H
Hongze Cheng 已提交
215
    code = tsdbSttLvlToJson(lvl, item2);
H
Hongze Cheng 已提交
216 217
    if (code) return code;
  }
H
Hongze Cheng 已提交
218 219

  return 0;
H
Hongze Cheng 已提交
220 221
}

H
Hongze Cheng 已提交
222
int32_t tsdbJsonToTFileSet(STsdb *pTsdb, const cJSON *json, STFileSet **fset) {
H
Hongze Cheng 已提交
223 224 225 226
  int32_t      code;
  const cJSON *item1, *item2;
  int32_t      fid;
  STFile       tf;
H
Hongze Cheng 已提交
227

H
Hongze Cheng 已提交
228 229 230 231 232 233 234
  // fid
  item1 = cJSON_GetObjectItem(json, "fid");
  if (cJSON_IsNumber(item1)) {
    fid = item1->valuedouble;
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }
H
Hongze Cheng 已提交
235

H
Hongze Cheng 已提交
236 237
  code = tsdbTFileSetInit(fid, fset);
  if (code) return code;
H
Hongze Cheng 已提交
238

H
Hongze Cheng 已提交
239 240 241 242 243 244 245
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
    code = tsdbJsonToTFile(json, ftype, &tf);
    if (code == TSDB_CODE_NOT_FOUND) {
      continue;
    } else if (code) {
      tsdbTFileSetClear(fset);
      return code;
H
Hongze Cheng 已提交
246 247 248
    } else {
      code = tsdbTFileObjInit(pTsdb, &tf, &(*fset)->farr[ftype]);
      if (code) return code;
H
Hongze Cheng 已提交
249 250
    }
  }
H
Hongze Cheng 已提交
251

H
Hongze Cheng 已提交
252 253 254 255 256
  // each level
  item1 = cJSON_GetObjectItem(json, "stt lvl");
  if (cJSON_IsArray(item1)) {
    cJSON_ArrayForEach(item2, item1) {
      SSttLvl *lvl;
H
Hongze Cheng 已提交
257
      code = tsdbJsonToSttLvl(pTsdb, item2, &lvl);
H
Hongze Cheng 已提交
258 259 260 261 262
      if (code) {
        tsdbTFileSetClear(fset);
        return code;
      }

H
Hongze Cheng 已提交
263
      TARRAY2_APPEND((*fset)->lvlArr, lvl);
H
Hongze Cheng 已提交
264 265 266 267
    }
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }
H
Hongze Cheng 已提交
268

H
Hongze Cheng 已提交
269
  return 0;
H
Hongze Cheng 已提交
270 271
}

H
Hongze Cheng 已提交
272
// NOTE: the api does not remove file, only do memory operation
H
Hongze Cheng 已提交
273
int32_t tsdbTFileSetEdit(STsdb *pTsdb, STFileSet *fset, const STFileOp *op) {
H
Hongze Cheng 已提交
274
  int32_t code = 0;
H
Hongze Cheng 已提交
275

H
Hongze Cheng 已提交
276 277 278 279 280 281
  if (op->optype == TSDB_FOP_CREATE) {
    // create a new file
    STFileObj *fobj;
    code = tsdbTFileObjInit(pTsdb, &op->nf, &fobj);
    if (code) return code;

H
Hongze Cheng 已提交
282
    if (fobj->f->type == TSDB_FTYPE_STT) {
H
Hongze Cheng 已提交
283
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, fobj->f->stt->level);
H
Hongze Cheng 已提交
284
      if (!lvl) {
H
Hongze Cheng 已提交
285
        code = tsdbSttLvlInit(fobj->f->stt->level, &lvl);
H
Hongze Cheng 已提交
286 287
        if (code) return code;

H
Hongze Cheng 已提交
288
        code = TARRAY2_SORT_INSERT(fset->lvlArr, lvl, tsdbSttLvlCmprFn);
H
Hongze Cheng 已提交
289 290 291
        if (code) return code;
      }

H
Hongze Cheng 已提交
292
      code = TARRAY2_SORT_INSERT(lvl->fobjArr, fobj, tsdbTFileObjCmpr);
H
Hongze Cheng 已提交
293 294
      if (code) return code;
    } else {
H
Hongze Cheng 已提交
295 296
      ASSERT(fset->farr[fobj->f->type] == NULL);
      fset->farr[fobj->f->type] = fobj;
H
Hongze Cheng 已提交
297 298 299 300
    }
  } else if (op->optype == TSDB_FOP_REMOVE) {
    // delete a file
    if (op->of.type == TSDB_FTYPE_STT) {
H
Hongze Cheng 已提交
301
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
H
Hongze Cheng 已提交
302 303
      ASSERT(lvl);

H
Hongze Cheng 已提交
304
      STFileObj  tfobj = {.f[0] = {.cid = op->of.cid}};
H
Hongze Cheng 已提交
305
      STFileObj *tfobjp = &tfobj;
H
Hongze Cheng 已提交
306
      int32_t    idx = TARRAY2_SEARCH_IDX(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
H
Hongze Cheng 已提交
307
      ASSERT(idx >= 0);
H
Hongze Cheng 已提交
308
      TARRAY2_REMOVE(lvl->fobjArr, idx, tsdbSttLvlRemoveFObj);
H
Hongze Cheng 已提交
309

H
Hongze Cheng 已提交
310
      if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
H
Hongze Cheng 已提交
311 312 313 314
        // TODO: remove the stt level if no file exists anymore
        // TARRAY2_REMOVE(&fset->lvlArr, lvl - fset->lvlArr.data, tsdbSttLvlClear);
      }
    } else {
H
Hongze Cheng 已提交
315
      ASSERT(tsdbIsSameTFile(&op->of, fset->farr[op->of.type]->f));
H
Hongze Cheng 已提交
316 317 318 319 320
      tsdbTFileObjUnref(fset->farr[op->of.type]);
      fset->farr[op->of.type] = NULL;
    }
  } else {
    if (op->nf.type == TSDB_FTYPE_STT) {
H
Hongze Cheng 已提交
321
      SSttLvl *lvl = tsdbTFileSetGetSttLvl(fset, op->of.stt->level);
H
Hongze Cheng 已提交
322 323
      ASSERT(lvl);

H
Hongze Cheng 已提交
324
      STFileObj tfobj = {.f[0] = {.cid = op->of.cid}}, *tfobjp = &tfobj;
H
Hongze Cheng 已提交
325
      tfobjp = TARRAY2_SEARCH_EX(lvl->fobjArr, &tfobjp, tsdbTFileObjCmpr, TD_EQ);
H
Hongze Cheng 已提交
326 327 328

      ASSERT(tfobjp);

H
Hongze Cheng 已提交
329
      tfobjp->f[0] = op->nf;
H
Hongze Cheng 已提交
330
    } else {
H
Hongze Cheng 已提交
331
      fset->farr[op->nf.type]->f[0] = op->nf;
H
Hongze Cheng 已提交
332 333
    }
  }
H
Hongze Cheng 已提交
334

H
Hongze Cheng 已提交
335 336 337
  return 0;
}

H
Hongze Cheng 已提交
338 339 340 341 342 343
int32_t tsdbTFileSetApplyEdit(STsdb *pTsdb, const STFileSet *fset1, STFileSet *fset2) {
  int32_t code = 0;

  ASSERT(fset1->fid == fset2->fid);

  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
H
Hongze Cheng 已提交
344 345
    if (!fset1->farr[ftype] && !fset2->farr[ftype]) continue;

H
Hongze Cheng 已提交
346 347 348 349
    STFileObj *fobj1 = fset1->farr[ftype];
    STFileObj *fobj2 = fset2->farr[ftype];

    if (fobj1 && fobj2) {
H
Hongze Cheng 已提交
350 351 352
      if (tsdbIsSameTFile(fobj1->f, fobj2->f)) {
        if (tsdbIsTFileChanged(fobj1->f, fobj2->f)) {
          fobj2->f[0] = fobj1->f[0];
H
Hongze Cheng 已提交
353 354 355
        }
      } else {
        tsdbTFileObjRemove(fobj2);
H
Hongze Cheng 已提交
356
        code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
H
Hongze Cheng 已提交
357 358 359 360
        if (code) return code;
      }
    } else if (fobj1) {
      // create a new file
H
Hongze Cheng 已提交
361
      code = tsdbTFileObjInit(pTsdb, fobj1->f, &fset2->farr[ftype]);
H
Hongze Cheng 已提交
362 363 364 365 366 367 368 369 370 371
      if (code) return code;
    } else {
      // remove the file
      tsdbTFileObjRemove(fobj2);
      fset2->farr[ftype] = NULL;
    }
  }

  // stt part
  int32_t i1 = 0, i2 = 0;
H
Hongze Cheng 已提交
372 373 374
  while (i1 < TARRAY2_SIZE(fset1->lvlArr) || i2 < TARRAY2_SIZE(fset2->lvlArr)) {
    SSttLvl *lvl1 = i1 < TARRAY2_SIZE(fset1->lvlArr) ? TARRAY2_GET(fset1->lvlArr, i1) : NULL;
    SSttLvl *lvl2 = i2 < TARRAY2_SIZE(fset2->lvlArr) ? TARRAY2_GET(fset2->lvlArr, i2) : NULL;
H
Hongze Cheng 已提交
375 376 377 378 379 380

    if (lvl1 && lvl2) {
      if (lvl1->level < lvl2->level) {
        // add a new stt level
        code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
        if (code) return code;
H
Hongze Cheng 已提交
381
        code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
H
Hongze Cheng 已提交
382 383 384 385 386
        if (code) return code;
        i1++;
        i2++;
      } else if (lvl1->level > lvl2->level) {
        // remove the stt level
H
Hongze Cheng 已提交
387
        TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
H
Hongze Cheng 已提交
388 389 390 391 392 393 394 395 396 397 398
      } else {
        // apply edit on stt level
        code = tsdbSttLvlApplyEdit(pTsdb, lvl1, lvl2);
        if (code) return code;
        i1++;
        i2++;
      }
    } else if (lvl1) {
      // add a new stt level
      code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl2);
      if (code) return code;
H
Hongze Cheng 已提交
399
      code = TARRAY2_SORT_INSERT(fset2->lvlArr, lvl2, tsdbSttLvlCmprFn);
H
Hongze Cheng 已提交
400 401 402 403 404
      if (code) return code;
      i1++;
      i2++;
    } else {
      // remove the stt level
H
Hongze Cheng 已提交
405
      TARRAY2_REMOVE(fset2->lvlArr, i2, tsdbSttLvlRemove);
H
Hongze Cheng 已提交
406 407 408
    }
  }

H
Hongze Cheng 已提交
409 410 411
  return 0;
}

H
Hongze Cheng 已提交
412
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) {
H
Hongze Cheng 已提交
413 414 415 416
  fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
  if (fset[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  fset[0]->fid = fid;
H
Hongze Cheng 已提交
417
  TARRAY2_INIT(fset[0]->lvlArr);
H
Hongze Cheng 已提交
418 419
  return 0;
}
H
Hongze Cheng 已提交
420

H
Hongze Cheng 已提交
421
int32_t tsdbTFileSetInitDup(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
H
Hongze Cheng 已提交
422
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
H
Hongze Cheng 已提交
423
  if (code) return code;
H
Hongze Cheng 已提交
424 425 426 427

  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
    if (fset1->farr[ftype] == NULL) continue;

H
Hongze Cheng 已提交
428
    code = tsdbTFileObjInit(pTsdb, fset1->farr[ftype]->f, &fset[0]->farr[ftype]);
H
Hongze Cheng 已提交
429
    if (code) {
H
Hongze Cheng 已提交
430
      tsdbTFileSetClear(fset);
H
Hongze Cheng 已提交
431 432
      return code;
    }
H
Hongze Cheng 已提交
433 434
  }

H
Hongze Cheng 已提交
435
  const SSttLvl *lvl1;
H
Hongze Cheng 已提交
436
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
H
Hongze Cheng 已提交
437
    SSttLvl *lvl;
H
Hongze Cheng 已提交
438
    code = tsdbSttLvlInitEx(pTsdb, lvl1, &lvl);
H
Hongze Cheng 已提交
439
    if (code) {
H
Hongze Cheng 已提交
440
      tsdbTFileSetClear(fset);
H
Hongze Cheng 已提交
441
      return code;
H
Hongze Cheng 已提交
442
    }
H
Hongze Cheng 已提交
443

H
Hongze Cheng 已提交
444
    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
H
Hongze Cheng 已提交
445
    if (code) return code;
H
Hongze Cheng 已提交
446
  }
H
Hongze Cheng 已提交
447

H
Hongze Cheng 已提交
448 449 450
  return 0;
}

H
Hongze Cheng 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
int32_t tsdbTFileSetInitRef(STsdb *pTsdb, const STFileSet *fset1, STFileSet **fset) {
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
  if (code) return code;

  for (int32_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
    if (fset1->farr[ftype] == NULL) continue;

    tsdbTFileObjRef(fset1->farr[ftype]);
    fset[0]->farr[ftype] = fset1->farr[ftype];
  }

  const SSttLvl *lvl1;
  TARRAY2_FOREACH(fset1->lvlArr, lvl1) {
    SSttLvl *lvl;
    code = tsdbSttLvlInitRef(pTsdb, lvl1, &lvl);
    if (code) {
      tsdbTFileSetClear(fset);
      return code;
    }

    code = TARRAY2_APPEND(fset[0]->lvlArr, lvl);
    if (code) return code;
  }

  return 0;
}

H
Hongze Cheng 已提交
478 479
int32_t tsdbTFileSetClear(STFileSet **fset) {
  if (!fset[0]) return 0;
H
Hongze Cheng 已提交
480

H
Hongze Cheng 已提交
481 482 483
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
    if (fset[0]->farr[ftype] == NULL) continue;
    tsdbTFileObjUnref(fset[0]->farr[ftype]);
H
Hongze Cheng 已提交
484
  }
H
Hongze Cheng 已提交
485

H
Hongze Cheng 已提交
486
  TARRAY2_DESTROY(fset[0]->lvlArr, tsdbSttLvlClear);
H
Hongze Cheng 已提交
487 488 489 490

  taosMemoryFree(fset[0]);
  fset[0] = NULL;

H
Hongze Cheng 已提交
491
  return 0;
H
Hongze Cheng 已提交
492
}
H
Hongze Cheng 已提交
493

H
Hongze Cheng 已提交
494 495 496 497 498 499
int32_t tsdbTFileSetRemove(STFileSet **fset) {
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
    if (fset[0]->farr[ftype] == NULL) continue;
    tsdbTFileObjRemove(fset[0]->farr[ftype]);
  }

H
Hongze Cheng 已提交
500
  TARRAY2_DESTROY(fset[0]->lvlArr, tsdbSttLvlRemove);
H
Hongze Cheng 已提交
501 502 503 504 505
  taosMemoryFree(fset[0]);
  fset[0] = NULL;
  return 0;
}

H
Hongze Cheng 已提交
506 507 508
SSttLvl *tsdbTFileSetGetSttLvl(STFileSet *fset, int32_t level) {
  SSttLvl  sttLvl = {.level = level};
  SSttLvl *lvl = &sttLvl;
H
Hongze Cheng 已提交
509
  return TARRAY2_SEARCH_EX(fset->lvlArr, &lvl, tsdbSttLvlCmprFn, TD_EQ);
H
Hongze Cheng 已提交
510 511 512 513 514 515
}

int32_t tsdbTFileSetCmprFn(const STFileSet **fset1, const STFileSet **fset2) {
  if (fset1[0]->fid < fset2[0]->fid) return -1;
  if (fset1[0]->fid > fset2[0]->fid) return 1;
  return 0;
H
Hongze Cheng 已提交
516 517 518 519 520 521
}

int64_t tsdbTFileSetMaxCid(const STFileSet *fset) {
  int64_t maxCid = 0;
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
    if (fset->farr[ftype] == NULL) continue;
H
Hongze Cheng 已提交
522
    maxCid = TMAX(maxCid, fset->farr[ftype]->f->cid);
H
Hongze Cheng 已提交
523 524 525
  }
  const SSttLvl   *lvl;
  const STFileObj *fobj;
H
Hongze Cheng 已提交
526 527
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
    TARRAY2_FOREACH(lvl->fobjArr, fobj) { maxCid = TMAX(maxCid, fobj->f->cid); }
H
Hongze Cheng 已提交
528 529
  }
  return maxCid;
H
Hongze Cheng 已提交
530 531 532 533 534 535
}

bool tsdbTFileSetIsEmpty(const STFileSet *fset) {
  for (tsdb_ftype_t ftype = TSDB_FTYPE_MIN; ftype < TSDB_FTYPE_MAX; ++ftype) {
    if (fset->farr[ftype] != NULL) return false;
  }
H
Hongze Cheng 已提交
536
  return TARRAY2_SIZE(fset->lvlArr) == 0;
H
Hongze Cheng 已提交
537
}