tsdbFSet.c 7.0 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 "inc/tsdbFSet.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
static int32_t tsdbSttLvlInit(int32_t level, SSttLvl **lvl) {
  lvl[0] = taosMemoryMalloc(sizeof(SSttLvl));
  if (lvl[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  lvl[0]->level = level;
  TARRAY2_INIT(&lvl[0]->farr);
  return 0;
}
static void    tsdbSttLvlClearFObj(void *data) { tsdbTFileObjUnref(*(STFileObj **)data); }
static int32_t tsdbSttLvlClear(SSttLvl **lvl) {
  TARRAY2_CLEAR_FREE(&lvl[0]->farr, tsdbSttLvlClearFObj);
  taosMemoryFree(lvl[0]);
  lvl[0] = NULL;
  return 0;
}
static int32_t tsdbSttLvlInitEx(const SSttLvl *lvl1, SSttLvl **lvl) {
  int32_t code = tsdbSttLvlInit(lvl1->level, lvl);
  if (code) return code;

  const STFileObj *fobj1;
  TARRAY2_FOREACH(&lvl1->farr, fobj1) {
    STFileObj *fobj;
    code = tsdbTFileObjInit(&fobj1->f, &fobj);
    if (code) {
      tsdbSttLvlClear(lvl);
      return code;
    }

    TARRAY2_APPEND(&lvl[0]->farr, fobj);
  }
  return 0;
}

static int32_t tsdbSttLvlToJson(const SSttLvl *lvl, cJSON *json) {
H
Hongze Cheng 已提交
52
  if (cJSON_AddNumberToObject(json, "level", lvl->level) == NULL) {
H
Hongze Cheng 已提交
53 54 55
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
56
  cJSON *ajson = cJSON_AddArrayToObject(json, "files");
H
Hongze Cheng 已提交
57
  if (ajson == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
58 59
  const STFileObj *fobj;
  TARRAY2_FOREACH(&lvl->farr, fobj) {
H
Hongze Cheng 已提交
60 61
    cJSON *item = cJSON_CreateObject();
    if (item == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
62
    cJSON_AddItemToArray(ajson, item);
H
Hongze Cheng 已提交
63

H
Hongze Cheng 已提交
64
    int32_t code = tsdbTFileToJson(&fobj->f, item);
H
Hongze Cheng 已提交
65
    if (code) return code;
H
Hongze Cheng 已提交
66
  }
H
Hongze Cheng 已提交
67 68 69 70

  return 0;
}

H
Hongze Cheng 已提交
71
static int32_t tsdbJsonToSttLvl(const cJSON *json, SSttLvl **lvl) {
H
Hongze Cheng 已提交
72
  const cJSON *item1, *item2;
H
Hongze Cheng 已提交
73
  int32_t      level;
H
Hongze Cheng 已提交
74

H
Hongze Cheng 已提交
75
  item1 = cJSON_GetObjectItem(json, "level");
H
Hongze Cheng 已提交
76
  if (cJSON_IsNumber(item1)) {
H
Hongze Cheng 已提交
77
    level = item1->valuedouble;
H
Hongze Cheng 已提交
78 79 80 81
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }

H
Hongze Cheng 已提交
82 83
  int32_t code = tsdbSttLvlInit(level, lvl);
  if (code) return code;
H
Hongze Cheng 已提交
84

H
Hongze Cheng 已提交
85
  item1 = cJSON_GetObjectItem(json, "files");
H
Hongze Cheng 已提交
86 87
  if (!cJSON_IsArray(item1)) {
    tsdbSttLvlClear(lvl);
H
Hongze Cheng 已提交
88 89 90
    return TSDB_CODE_FILE_CORRUPTED;
  }

H
Hongze Cheng 已提交
91 92 93 94 95 96 97
  cJSON_ArrayForEach(item2, item1) {
    STFile tf;
    code = tsdbJsonToTFile(item2, TSDB_FTYPE_STT, &tf);
    if (code) {
      tsdbSttLvlClear(lvl);
      return code;
    }
H
Hongze Cheng 已提交
98

H
Hongze Cheng 已提交
99 100 101 102 103 104
    STFileObj *fobj;
    code = tsdbTFileObjInit(&tf, &fobj);
    if (code) {
      tsdbSttLvlClear(lvl);
      return code;
    }
H
Hongze Cheng 已提交
105

H
Hongze Cheng 已提交
106
    TARRAY2_APPEND(&lvl[0]->farr, fobj);
H
Hongze Cheng 已提交
107 108 109 110
  }
  return 0;
}

H
Hongze Cheng 已提交
111
int32_t tsdbTFileSetToJson(const STFileSet *fset, cJSON *json) {
H
Hongze Cheng 已提交
112
  int32_t code = 0;
H
Hongze Cheng 已提交
113
  cJSON  *item1, *item2;
H
Hongze Cheng 已提交
114

H
Hongze Cheng 已提交
115 116 117 118
  // fid
  if (cJSON_AddNumberToObject(json, "fid", fset->fid) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Hongze Cheng 已提交
119

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

H
Hongze Cheng 已提交
123 124
    code = tsdbTFileToJson(&fset->farr[ftype]->f, json);
    if (code) return code;
H
Hongze Cheng 已提交
125 126 127
  }

  // each level
H
Hongze Cheng 已提交
128
  item1 = cJSON_AddArrayToObject(json, "stt lvl");
H
Hongze Cheng 已提交
129
  if (item1 == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
130 131
  const SSttLvl *lvl;
  TARRAY2_FOREACH(&fset->lvlArr, lvl) {
H
Hongze Cheng 已提交
132 133 134 135
    item2 = cJSON_CreateObject();
    if (!item2) return TSDB_CODE_OUT_OF_MEMORY;
    cJSON_AddItemToArray(item1, item2);

H
Hongze Cheng 已提交
136
    code = tsdbSttLvlToJson(lvl, item2);
H
Hongze Cheng 已提交
137 138
    if (code) return code;
  }
H
Hongze Cheng 已提交
139 140

  return 0;
H
Hongze Cheng 已提交
141 142
}

H
Hongze Cheng 已提交
143 144 145 146 147
int32_t tsdbJsonToTFileSet(const cJSON *json, STFileSet **fset) {
  int32_t      code;
  const cJSON *item1, *item2;
  int32_t      fid;
  STFile       tf;
H
Hongze Cheng 已提交
148

H
Hongze Cheng 已提交
149 150 151 152 153 154 155
  // fid
  item1 = cJSON_GetObjectItem(json, "fid");
  if (cJSON_IsNumber(item1)) {
    fid = item1->valuedouble;
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }
H
Hongze Cheng 已提交
156

H
Hongze Cheng 已提交
157 158
  code = tsdbTFileSetInit(fid, fset);
  if (code) return code;
H
Hongze Cheng 已提交
159

H
Hongze Cheng 已提交
160 161 162 163 164 165 166 167 168
  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 已提交
169

H
Hongze Cheng 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  // each level
  item1 = cJSON_GetObjectItem(json, "stt lvl");
  if (cJSON_IsArray(item1)) {
    cJSON_ArrayForEach(item2, item1) {
      SSttLvl *lvl;
      code = tsdbJsonToSttLvl(item2, &lvl);
      if (code) {
        tsdbTFileSetClear(fset);
        return code;
      }

      TARRAY2_APPEND(&(*fset)->lvlArr, lvl);
    }
  } else {
    return TSDB_CODE_FILE_CORRUPTED;
  }
H
Hongze Cheng 已提交
186

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

H
Hongze Cheng 已提交
190
int32_t tsdbTFileSetEdit(STFileSet *fset, const STFileOp *op) {
H
Hongze Cheng 已提交
191
  int32_t code = 0;
H
Hongze Cheng 已提交
192

H
Hongze Cheng 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  // if (op->oState.size == 0  //
  //     || 0                  /* TODO*/
  // ) {
  //   STFileObj *fobj;
  //   // code = tsdbTFileObjCreate(&fobj);
  //   if (code) return code;
  //   fobj->f = op->nState;
  //   add_file_to_fset(fset, fobj);
  // } else if (op->nState.size == 0) {
  //   // delete
  //   ASSERT(0);
  // } else {
  //   // modify
  //   ASSERT(0);
  // }
H
Hongze Cheng 已提交
208 209 210
  return 0;
}

H
Hongze Cheng 已提交
211 212 213 214 215
int32_t tsdbTFileSetEditEx(const STFileSet *fset1, STFileSet *fset) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
216
int32_t tsdbTFileSetInit(int32_t fid, STFileSet **fset) {
H
Hongze Cheng 已提交
217 218 219 220 221 222 223
  fset[0] = taosMemoryCalloc(1, sizeof(STFileSet));
  if (fset[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  fset[0]->fid = fid;
  TARRAY2_INIT(&fset[0]->lvlArr);
  return 0;
}
H
Hongze Cheng 已提交
224

H
Hongze Cheng 已提交
225 226
int32_t tsdbTFileSetInitEx(const STFileSet *fset1, STFileSet **fset) {
  int32_t code = tsdbTFileSetInit(fset1->fid, fset);
H
Hongze Cheng 已提交
227
  if (code) return code;
H
Hongze Cheng 已提交
228 229 230 231

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

H
Hongze Cheng 已提交
232
    code = tsdbTFileObjInit(&fset1->farr[ftype]->f, &fset[0]->farr[ftype]);
H
Hongze Cheng 已提交
233
    if (code) {
H
Hongze Cheng 已提交
234
      tsdbTFileSetClear(fset);
H
Hongze Cheng 已提交
235 236
      return code;
    }
H
Hongze Cheng 已提交
237 238
  }

H
Hongze Cheng 已提交
239 240 241
  const SSttLvl *lvl1;
  TARRAY2_FOREACH(&fset1->lvlArr, lvl1) {
    SSttLvl *lvl;
H
Hongze Cheng 已提交
242
    code = tsdbSttLvlInitEx(lvl1, &lvl);
H
Hongze Cheng 已提交
243
    if (code) {
H
Hongze Cheng 已提交
244
      tsdbTFileSetClear(fset);
H
Hongze Cheng 已提交
245
      return code;
H
Hongze Cheng 已提交
246
    }
H
Hongze Cheng 已提交
247 248

    TARRAY2_APPEND(&fset[0]->lvlArr, lvl);
H
Hongze Cheng 已提交
249
  }
H
Hongze Cheng 已提交
250

H
Hongze Cheng 已提交
251 252 253
  return 0;
}

H
Hongze Cheng 已提交
254 255
int32_t tsdbTFileSetClear(STFileSet **fset) {
  if (!fset[0]) return 0;
H
Hongze Cheng 已提交
256

H
Hongze Cheng 已提交
257 258 259
  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 已提交
260
  }
H
Hongze Cheng 已提交
261 262 263 264 265 266

  TARRAY2_CLEAR_FREE(&fset[0]->lvlArr, tsdbSttLvlClear);

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

H
Hongze Cheng 已提交
267
  return 0;
H
Hongze Cheng 已提交
268
}
H
Hongze Cheng 已提交
269

H
Hongze Cheng 已提交
270
const SSttLvl *tsdbTFileSetGetLvl(const STFileSet *fset, int32_t level) {
H
Hongze Cheng 已提交
271 272 273 274 275
  // SSttLvl      tlvl = {.level = level};
  // SRBTreeNode *node = tRBTreeGet(&fset->lvlTree, &tlvl.rbtn);
  // return node ? TCONTAINER_OF(node, SSttLvl, rbtn) : NULL;
  // TODO
  return NULL;
H
Hongze Cheng 已提交
276 277 278 279 280 281
}

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