metaSma.c 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#include "meta.h"

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

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 已提交
31
  void       *pBuf = NULL;
32
  int32_t     szBuf = 0;
H
Hongze Cheng 已提交
33
  void       *p = NULL;
34 35 36
  SMetaReader mr = {0};

  // validate req
C
Cary Xu 已提交
37
  // save smaIndex
38
  metaReaderInit(&mr, pMeta, 0);
H
Haojun Liao 已提交
39
  if (metaGetTableEntryByUidCache(&mr, pCfg->indexUid) == 0) {
40
#if 1
C
Cary Xu 已提交
41
    terrno = TSDB_CODE_TSMA_ALREADY_EXIST;
42
    metaReaderClear(&mr);
C
Cary Xu 已提交
43
    return -1;  // don't goto _err;
44 45 46 47 48 49 50 51 52 53 54 55
#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 已提交
56
  me.smaEntry.tsma = pCfg;
57 58 59

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

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

  return 0;

_err:
S
Shengliang Guan 已提交
65
  metaError("vgId:%d, failed to create tsma:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pCfg->indexName,
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            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 已提交
86 87
  void    *pKey = NULL;
  void    *pVal = NULL;
88 89 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
  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
H
Hongze Cheng 已提交
120
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, &pMeta->txn) < 0) {
121 122 123 124 125 126 127 128 129 130 131
    goto _err;
  }

  taosMemoryFree(pVal);
  return 0;

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

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

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

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

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

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

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

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

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

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

  metaULock(pMeta);
  return 0;

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