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

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

H
Hongze Cheng 已提交
18 19 20 21 22
static int metaSaveToTbDb(SMeta *pMeta, int64_t version, const SMetaEntry *pME);
static int metaUpdateUidIdx(SMeta *pMeta, tb_uid_t uid, int64_t version);
static int metaUpdateNameIdx(SMeta *pMeta, const char *name, tb_uid_t uid);

int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
H
Hongze Cheng 已提交
23 24
  SSkmDbKey   skmDbKey = {0};
  SMetaEntry  me = {0};
H
Hongze Cheng 已提交
25 26 27 28 29 30 31 32
  int         kLen = 0;
  int         vLen = 0;
  const void *pKey = NULL;
  const void *pVal = NULL;
  void       *pBuf = NULL;
  int32_t     szBuf = 0;
  void       *p = NULL;
  SCoder      coder = {0};
H
Hongze Cheng 已提交
33

H
Hongze Cheng 已提交
34 35 36
  {
    // TODO: validate request (uid and name unique)
  }
H
Hongze Cheng 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50

  // set structs
  me.type = TSDB_SUPER_TABLE;
  me.uid = pReq->suid;
  me.name = pReq->name;
  me.stbEntry.nCols = pReq->nCols;
  me.stbEntry.sver = pReq->sver;
  me.stbEntry.pSchema = pReq->pSchema;
  me.stbEntry.nTags = pReq->nTags;
  me.stbEntry.pSchemaTg = pReq->pSchemaTg;

  skmDbKey.uid = pReq->suid;
  skmDbKey.sver = 0;  // (TODO)

H
Hongze Cheng 已提交
51 52
  // save to table.db
  if (metaSaveToTbDb(pMeta, version, &me) < 0) goto _err;
H
Hongze Cheng 已提交
53

H
Hongze Cheng 已提交
54 55
  // update uid idx
  if (metaUpdateUidIdx(pMeta, me.uid, version) < 0) goto _err;
H
Hongze Cheng 已提交
56 57

  // update name.idx
H
Hongze Cheng 已提交
58
  if (metaUpdateNameIdx(pMeta, me.name, me.uid) < 0) goto _err;
H
Hongze Cheng 已提交
59

H
Hongze Cheng 已提交
60 61 62 63 64 65 66 67 68 69 70 71
  metaDebug("vgId: %d super table is created, name:%s uid: %" PRId64, TD_VID(pMeta->pVnode), pReq->name, pReq->suid);

  return 0;

_err:
  metaError("vgId: %d failed to create super table: %s uid: %" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
            pReq->suid, tstrerror(terrno));
  return -1;
}

int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
  // TODO
H
Hongze Cheng 已提交
72 73 74
  return 0;
}

H
more  
Hongze Cheng 已提交
75
int metaCreateTable(SMeta *pMeta, STbCfg *pTbCfg) {
H
Hongze Cheng 已提交
76
#if 0
H
more  
Hongze Cheng 已提交
77
  if (metaSaveTableToDB(pMeta, pTbCfg) < 0) {
H
more  
Hongze Cheng 已提交
78 79 80 81
    // TODO: handle error
    return -1;
  }

H
more  
Hongze Cheng 已提交
82
  if (metaSaveTableToIdx(pMeta, pTbCfg) < 0) {
H
more  
Hongze Cheng 已提交
83 84 85
    // TODO: handle error
    return -1;
  }
H
Hongze Cheng 已提交
86
#endif
H
more  
Hongze Cheng 已提交
87

H
refact  
Hongze Cheng 已提交
88 89 90
  return 0;
}

H
more  
Hongze Cheng 已提交
91
int metaDropTable(SMeta *pMeta, tb_uid_t uid) {
H
Hongze Cheng 已提交
92
#if 0
H
more  
Hongze Cheng 已提交
93 94 95 96 97 98 99 100 101
  if (metaRemoveTableFromIdx(pMeta, uid) < 0) {
    // TODO: handle error
    return -1;
  }

  if (metaRemoveTableFromIdx(pMeta, uid) < 0) {
    // TODO
    return -1;
  }
H
Hongze Cheng 已提交
102
#endif
H
more  
Hongze Cheng 已提交
103

H
refact  
Hongze Cheng 已提交
104 105
  return 0;
}
H
Hongze Cheng 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

static int metaSaveToTbDb(SMeta *pMeta, int64_t version, const SMetaEntry *pME) {
  void  *pKey = NULL;
  void  *pVal = NULL;
  int    kLen = 0;
  int    vLen = 0;
  SCoder coder = {0};

  // set key and value
  pKey = &version;
  kLen = sizeof(version);

  if (tEncodeSize(metaEncodeEntry, pME, vLen) < 0) {
    goto _err;
  }

  pVal = taosMemoryMalloc(vLen);
  if (pVal == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  tCoderInit(&coder, TD_LITTLE_ENDIAN, pVal, vLen, TD_ENCODER);

  if (metaEncodeEntry(&coder, pME) < 0) {
    goto _err;
  }

  tCoderClear(&coder);

  // write to table.db
  if (tdbDbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, NULL) < 0) {
    goto _err;
  }

  taosMemoryFree(pVal);
  return 0;

_err:
  taosMemoryFree(pVal);
  return -1;
}

static int metaUpdateUidIdx(SMeta *pMeta, tb_uid_t uid, int64_t version) {
  return tdbDbInsert(pMeta->pUidIdx, &uid, sizeof(uid), &version, sizeof(version), NULL);
}

static int metaUpdateNameIdx(SMeta *pMeta, const char *name, tb_uid_t uid) {
  return tdbDbInsert(pMeta->pNameIdx, name, strlen(name) + 1, &uid, sizeof(uid), NULL);
}