metaSma.c 4.3 KB
Newer Older
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/>.
 */

16
#include "vnodeInt.h"
17 18
#include "meta.h"

19

20
static int metaHandleSmaEntry(SMeta *pMeta, const SMetaEntry *pME);
C
Cary Xu 已提交
21
static int metaSaveSmaToDB(SMeta *pMeta, const SMetaEntry *pME);
22 23 24 25 26 27 28 29 30 31 32

int32_t metaCreateTSma(SMeta *pMeta, int64_t version, SSmaCfg *pCfg) {
  // TODO: Validate the cfg
  // The table uid should exists and be super table or normal table.
  // Check other cfg value

  SMetaEntry  me = {0};
  int         kLen = 0;
  int         vLen = 0;
  const void *pKey = NULL;
  const void *pVal = NULL;
H
Hongze Cheng 已提交
33
  void       *pBuf = NULL;
34
  int32_t     szBuf = 0;
H
Hongze Cheng 已提交
35
  void       *p = NULL;
36 37 38
  SMetaReader mr = {0};

  // validate req
C
Cary Xu 已提交
39
  // save smaIndex
40
  metaReaderDoInit(&mr, pMeta, 0);
41
  if (metaReaderGetTableEntryByUidCache(&mr, pCfg->indexUid) == 0) {
42
#if 1
C
Cary Xu 已提交
43
    terrno = TSDB_CODE_TSMA_ALREADY_EXIST;
44
    metaReaderClear(&mr);
C
Cary Xu 已提交
45
    return -1;  // don't goto _err;
46 47 48 49 50 51 52 53 54 55 56 57
#else
    metaReaderClear(&mr);
    return 0;
#endif
  }
  metaReaderClear(&mr);

  // set structs
  me.version = version;
  me.type = TSDB_TSMA_TABLE;
  me.uid = pCfg->indexUid;
  me.name = pCfg->indexName;
C
Cary Xu 已提交
58
  me.smaEntry.tsma = pCfg;
59 60 61

  if (metaHandleSmaEntry(pMeta, &me) < 0) goto _err;

S
Shengliang Guan 已提交
62
  metaDebug("vgId:%d, tsma is created, name:%s uid:%" PRId64, TD_VID(pMeta->pVnode), pCfg->indexName, pCfg->indexUid);
63 64 65 66

  return 0;

_err:
S
Shengliang Guan 已提交
67
  metaError("vgId:%d, failed to create tsma:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pCfg->indexName,
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
            pCfg->indexUid, tstrerror(terrno));
  return -1;
}

int32_t metaDropTSma(SMeta *pMeta, int64_t indexUid) {
  // TODO: Validate the cfg
  // TODO: add atomicity

#ifdef META_REFACT
#else
  if (metaRemoveSmaFromDb(pMeta, indexUid) < 0) {
    // TODO: handle error
    return -1;
  }
#endif
  return TSDB_CODE_SUCCESS;
}

static int metaSaveSmaToDB(SMeta *pMeta, const SMetaEntry *pME) {
  STbDbKey tbDbKey;
H
Hongze Cheng 已提交
88 89
  void    *pKey = NULL;
  void    *pVal = NULL;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  int      kLen = 0;
  int      vLen = 0;
  SEncoder coder = {0};

  // set key and value
  tbDbKey.version = pME->version;
  tbDbKey.uid = pME->uid;

  pKey = &tbDbKey;
  kLen = sizeof(tbDbKey);

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

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

  tEncoderInit(&coder, pVal, vLen);

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

  tEncoderClear(&coder);

  // write to table.db
122
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, pMeta->txn) < 0) {
123 124 125 126 127 128 129 130 131 132 133
    goto _err;
  }

  taosMemoryFree(pVal);
  return 0;

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

C
Cary Xu 已提交
134
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
135
  SUidIdxVal uidIdxVal = {.suid = pME->smaEntry.tsma->indexUid, .version = pME->version, .skmVer = 0};
136
  return tdbTbInsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &uidIdxVal, sizeof(uidIdxVal), pMeta->txn);
C
Cary Xu 已提交
137
}
138

C
Cary Xu 已提交
139
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME) {
140
  return tdbTbInsert(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), pMeta->txn);
C
Cary Xu 已提交
141 142
}

C
Cary Xu 已提交
143 144
static int metaUpdateSmaIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SSmaIdxKey smaIdxKey = {.uid = pME->smaEntry.tsma->tableUid, .smaUid = pME->smaEntry.tsma->indexUid};
145

146
  return tdbTbInsert(pMeta->pSmaIdx, &smaIdxKey, sizeof(smaIdxKey), NULL, 0, pMeta->txn);
C
Cary Xu 已提交
147
}
148 149 150 151 152 153 154

static int metaHandleSmaEntry(SMeta *pMeta, const SMetaEntry *pME) {
  metaWLock(pMeta);

  // save to table.db
  if (metaSaveSmaToDB(pMeta, pME) < 0) goto _err;

C
Cary Xu 已提交
155
  // update uid.idx
C
Cary Xu 已提交
156 157
  if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err;

C
Cary Xu 已提交
158 159 160 161
  // update name.idx
  if (metaUpdateNameIdx(pMeta, pME) < 0) goto _err;

  // update sma.idx
C
Cary Xu 已提交
162
  if (metaUpdateSmaIdx(pMeta, pME) < 0) goto _err;
163 164 165 166 167 168 169 170

  metaULock(pMeta);
  return 0;

_err:
  metaULock(pMeta);
  return -1;
}