mndIndex.c 25.5 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
dengyihao's avatar
dengyihao 已提交
17
#include "mndIndex.h"
dengyihao's avatar
dengyihao 已提交
18 19
#include "mndDb.h"
#include "mndDnode.h"
dengyihao's avatar
dengyihao 已提交
20
#include "mndIndexComm.h"
dengyihao's avatar
dengyihao 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include "mndInfoSchema.h"
#include "mndMnode.h"
#include "mndPrivilege.h"
#include "mndScheduler.h"
#include "mndShow.h"
#include "mndStb.h"
#include "mndStream.h"
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
#include "parser.h"
#include "tname.h"

#define TSDB_IDX_VER_NUMBER   1
#define TSDB_IDX_RESERVE_SIZE 64

static SSdbRaw *mndIdxActionEncode(SIdxObj *pSma);
static SSdbRow *mndIdxActionDecode(SSdbRaw *pRaw);
static int32_t  mndIdxActionInsert(SSdb *pSdb, SIdxObj *pIdx);
static int32_t  mndIdxActionDelete(SSdb *pSdb, SIdxObj *pIdx);
static int32_t  mndIdxActionUpdate(SSdb *pSdb, SIdxObj *pOld, SIdxObj *pNew);
static int32_t  mndProcessCreateIdxReq(SRpcMsg *pReq);
dengyihao's avatar
dengyihao 已提交
43 44 45
// static int32_t  mndProcessDropIdxReq(SRpcMsg *pReq);
static int32_t mndProcessGetIdxReq(SRpcMsg *pReq);
static int32_t mndProcessGetTbIdxReq(SRpcMsg *pReq);
dengyihao's avatar
dengyihao 已提交
46 47 48
// static int32_t mndRetrieveIdx(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
//  static void    mndCancelGetNextIdx(SMnode *pMnode, void *pIter);
static void mndDestroyIdxObj(SIdxObj *pIdxObj);
dengyihao's avatar
dengyihao 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *req, SDbObj *pDb, SStbObj *pStb);

int32_t mndInitIdx(SMnode *pMnode) {
  SSdbTable table = {
      .sdbType = SDB_IDX,
      .keyType = SDB_KEY_BINARY,
      .encodeFp = (SdbEncodeFp)mndIdxActionEncode,
      .decodeFp = (SdbDecodeFp)mndIdxActionDecode,
      .insertFp = (SdbInsertFp)mndIdxActionInsert,
      .updateFp = (SdbUpdateFp)mndIdxActionUpdate,
      .deleteFp = (SdbDeleteFp)mndIdxActionDelete,
  };

  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_INDEX, mndProcessCreateIdxReq);
dengyihao's avatar
dengyihao 已提交
64
  // mndSetMsgHandle(pMnode, TDMT_MND_DROP_INDEX, mndProcessDropIdxReq);
dengyihao's avatar
dengyihao 已提交
65

dengyihao's avatar
dengyihao 已提交
66 67 68 69 70 71 72
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_INDEX_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_INDEX_RSP, mndTransProcessRsp);

  // mndSetMsgHandle(pMnode, TDMT_MND_CREATE_SMA, mndProcessCreateIdxReq);
  // mndSetMsgHandle(pMnode, TDMT_MND_DROP_SMA, mndProcessDropIdxReq);
  // mndSetMsgHandle(pMnode, TDMT_VND_CREATE_SMA_RSP, mndTransProcessRsp);
  // mndSetMsgHandle(pMnode, TDMT_VND_DROP_SMA_RSP, mndTransProcessRsp);
dengyihao's avatar
dengyihao 已提交
73 74
  // mndSetMsgHandle(pMnode, TDMT_MND_GET_INDEX, mndProcessGetIdxReq);
  // mndSetMsgHandle(pMnode, TDMT_MND_GET_TABLE_INDEX, mndProcessGetTbIdxReq);
dengyihao's avatar
dengyihao 已提交
75

dengyihao's avatar
dengyihao 已提交
76 77 78
  // type same with sma
  // mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_INDEX, mndRetrieveIdx);
  // mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_INDEX, mndCancelGetNextIdx);
dengyihao's avatar
dengyihao 已提交
79 80 81 82 83 84 85 86 87 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 120 121 122 123 124 125 126 127
  return sdbSetTable(pMnode->pSdb, table);
}

static int32_t mndFindSuperTableTagId(const SStbObj *pStb, const char *tagName) {
  for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
    if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) {
      return tag;
    }
  }

  return -1;
}

int mndSetCreateIdxRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb, SIdxObj *pIdx) {
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
  int32_t contLen;

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }

    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, NULL, 0);
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
    action.contLen = contLen;
    action.msgType = TDMT_VND_CREATE_INDEX;
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      taosMemoryFree(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}
dengyihao's avatar
dengyihao 已提交
128
static void *mndBuildDropIdxReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStbObj, SIdxObj *pIdx, int32_t *contLen) {
dengyihao's avatar
dengyihao 已提交
129 130 131
  int32_t len = 0;
  int32_t ret = 0;

dengyihao's avatar
dengyihao 已提交
132 133 134 135 136 137 138
  SDropIndexReq req = {0};
  memcpy(req.colName, pIdx->colName, sizeof(pIdx->colName));
  memcpy(req.stb, pIdx->stb, sizeof(pIdx->stb));
  req.dbUid = pIdx->dbUid;
  req.stbUid = pIdx->stbUid;

  mInfo("idx: %s start to build drop index req", pIdx->name);
dengyihao's avatar
dengyihao 已提交
139 140

  len = tSerializeSDropIdxReq(NULL, 0, &req);
dengyihao's avatar
dengyihao 已提交
141
  if (len < 0) {
dengyihao's avatar
dengyihao 已提交
142
    goto _err;
dengyihao's avatar
dengyihao 已提交
143 144
  }

dengyihao's avatar
dengyihao 已提交
145 146 147
  len += sizeof(SMsgHead);
  SMsgHead *pHead = taosMemoryCalloc(1, len);
  if (pHead == NULL) {
dengyihao's avatar
dengyihao 已提交
148
    terrno = TSDB_CODE_OUT_OF_MEMORY;
dengyihao's avatar
dengyihao 已提交
149
    goto _err;
dengyihao's avatar
dengyihao 已提交
150
  }
dengyihao's avatar
dengyihao 已提交
151 152 153 154 155 156

  pHead->contLen = htonl(len);
  pHead->vgId = htonl(pVgroup->vgId);

  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
  tSerializeSDropIdxReq(pBuf, len - sizeof(SMsgHead), &req);
dengyihao's avatar
dengyihao 已提交
157
  *contLen = len;
dengyihao's avatar
dengyihao 已提交
158 159 160 161
  return pHead;
_err:

  return NULL;
dengyihao's avatar
dengyihao 已提交
162
}
dengyihao's avatar
dengyihao 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176
int mndSetDropIdxRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb, SIdxObj *pIdx) {
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
  int32_t contLen;

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }

dengyihao's avatar
dengyihao 已提交
177 178
    int32_t len;
    void   *pReq = mndBuildDropIdxReq(pMnode, pVgroup, pStb, pIdx, &len);
dengyihao's avatar
dengyihao 已提交
179 180 181 182 183 184 185 186
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
dengyihao's avatar
dengyihao 已提交
187
    action.contLen = len;
dengyihao's avatar
dengyihao 已提交
188 189 190 191 192 193 194 195 196 197 198 199
    action.msgType = TDMT_VND_DROP_INDEX;
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      taosMemoryFree(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}
dengyihao's avatar
dengyihao 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

void mndCleanupIdx(SMnode *pMnode) {
  // do nothing
  return;
}

static SSdbRaw *mndIdxActionEncode(SIdxObj *pIdx) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;

  // int32_t size =
  //     sizeof(SSmaObj) + pSma->exprLen + pSma->tagsFilterLen + pSma->sqlLen + pSma->astLen + TSDB_IDX_RESERVE_SIZE;
  int32_t size = sizeof(SIdxObj) + TSDB_IDX_RESERVE_SIZE;

  SSdbRaw *pRaw = sdbAllocRaw(SDB_IDX, TSDB_IDX_VER_NUMBER, size);
  if (pRaw == NULL) goto _OVER;

  int32_t dataPos = 0;
  SDB_SET_BINARY(pRaw, dataPos, pIdx->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pIdx->stb, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pIdx->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pIdx->dstTbName, TSDB_DB_FNAME_LEN, _OVER)
dengyihao's avatar
opt idx  
dengyihao 已提交
221
  SDB_SET_BINARY(pRaw, dataPos, pIdx->colName, TSDB_COL_NAME_LEN, _OVER)
dengyihao's avatar
dengyihao 已提交
222 223 224 225 226 227 228 229 230 231 232 233
  SDB_SET_INT64(pRaw, dataPos, pIdx->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pIdx->uid, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pIdx->stbUid, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pIdx->dbUid, _OVER)

  SDB_SET_RESERVE(pRaw, dataPos, TSDB_IDX_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)

  terrno = 0;

_OVER:
  if (terrno != 0) {
dengyihao's avatar
dengyihao 已提交
234
    mError("idx:%s, failed to encode to raw:%p since %s", pIdx->name, pRaw, terrstr());
dengyihao's avatar
dengyihao 已提交
235 236 237 238
    sdbFreeRaw(pRaw);
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
239
  mTrace("idx:%s, encode to raw:%p, row:%p", pIdx->name, pRaw, pIdx);
dengyihao's avatar
dengyihao 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  return pRaw;
}

static SSdbRow *mndIdxActionDecode(SSdbRaw *pRaw) {
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  SSdbRow *pRow = NULL;
  SIdxObj *pIdx = NULL;

  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;

  if (sver != TSDB_IDX_VER_NUMBER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
    goto _OVER;
  }

  pRow = sdbAllocRow(sizeof(SIdxObj));
  if (pRow == NULL) goto _OVER;

  pIdx = sdbGetRowObj(pRow);
  if (pIdx == NULL) goto _OVER;

  int32_t dataPos = 0;

  SDB_GET_BINARY(pRaw, dataPos, pIdx->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pIdx->stb, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pIdx->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pIdx->dstTbName, TSDB_DB_FNAME_LEN, _OVER)
dengyihao's avatar
opt idx  
dengyihao 已提交
268
  SDB_GET_BINARY(pRaw, dataPos, pIdx->colName, TSDB_COL_NAME_LEN, _OVER)
dengyihao's avatar
dengyihao 已提交
269 270 271 272 273 274 275 276 277 278 279
  SDB_GET_INT64(pRaw, dataPos, &pIdx->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pIdx->uid, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pIdx->stbUid, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pIdx->dbUid, _OVER)

  SDB_GET_RESERVE(pRaw, dataPos, TSDB_IDX_RESERVE_SIZE, _OVER)

  terrno = 0;

_OVER:
  if (terrno != 0) {
dengyihao's avatar
dengyihao 已提交
280
    taosMemoryFree(pRow);
dengyihao's avatar
dengyihao 已提交
281 282 283
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
284
  mTrace("idx:%s, decode from raw:%p, row:%p", pIdx->name, pRaw, pIdx);
dengyihao's avatar
dengyihao 已提交
285 286 287 288 289 290 291 292 293
  return pRow;
}

static int32_t mndIdxActionInsert(SSdb *pSdb, SIdxObj *pIdx) {
  mTrace("idx:%s, perform insert action, row:%p", pIdx->name, pIdx);
  return 0;
}

static int32_t mndIdxActionDelete(SSdb *pSdb, SIdxObj *pIdx) {
dengyihao's avatar
dengyihao 已提交
294
  mTrace("idx:%s, perform delete action, row:%p", pIdx->name, pIdx);
dengyihao's avatar
dengyihao 已提交
295 296 297 298
  return 0;
}

static int32_t mndIdxActionUpdate(SSdb *pSdb, SIdxObj *pOld, SIdxObj *pNew) {
dengyihao's avatar
dengyihao 已提交
299 300 301 302
  // lock no not
  if (strncmp(pOld->colName, pNew->colName, TSDB_COL_NAME_LEN) != 0) {
    memcpy(pOld->colName, pNew->colName, sizeof(pNew->colName));
  }
dengyihao's avatar
dengyihao 已提交
303
  mTrace("idx:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
dengyihao's avatar
dengyihao 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  return 0;
}

SIdxObj *mndAcquireIdx(SMnode *pMnode, char *idxName) {
  SSdb    *pSdb = pMnode->pSdb;
  SIdxObj *pIdx = sdbAcquire(pSdb, SDB_IDX, idxName);
  if (pIdx == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
    terrno = TSDB_CODE_MND_TAG_INDEX_NOT_EXIST;
  }
  return pIdx;
}

void mndReleaseIdx(SMnode *pMnode, SIdxObj *pIdx) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pIdx);
}

SDbObj *mndAcquireDbByIdx(SMnode *pMnode, const char *idxName) {
  SName name = {0};
  tNameFromString(&name, idxName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);

  return mndAcquireDb(pMnode, db);
}

dengyihao's avatar
dengyihao 已提交
331
int32_t mndSetCreateIdxRedoLogs(SMnode *pMnode, STrans *pTrans, SIdxObj *pIdx) {
dengyihao's avatar
dengyihao 已提交
332 333 334 335 336 337 338 339
  SSdbRaw *pRedoRaw = mndIdxActionEncode(pIdx);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) return -1;

  return 0;
}

dengyihao's avatar
dengyihao 已提交
340
int32_t mndSetCreateIdxCommitLogs(SMnode *pMnode, STrans *pTrans, SIdxObj *pIdx) {
dengyihao's avatar
dengyihao 已提交
341 342 343 344 345 346 347 348
  SSdbRaw *pCommitRaw = mndIdxActionEncode(pIdx);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

dengyihao's avatar
dengyihao 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
int32_t mndSetAlterIdxRedoLogs(SMnode *pMnode, STrans *pTrans, SIdxObj *pIdx) {
  SSdbRaw *pRedoRaw = mndIdxActionEncode(pIdx);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

int32_t mndSetAlterIdxCommitLogs(SMnode *pMnode, STrans *pTrans, SIdxObj *pIdx) {
  SSdbRaw *pCommitRaw = mndIdxActionEncode(pIdx);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

dengyihao's avatar
dengyihao 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
static int32_t mndSetCreateIdxVgroupRedoLogs(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup) {
  SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup);
  if (pVgRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) return -1;
  if (sdbSetRawStatus(pVgRaw, SDB_STATUS_CREATING) != 0) return -1;
  return 0;
}

static int32_t mndSetCreateIdxVgroupCommitLogs(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup) {
  SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup);
  if (pVgRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pVgRaw) != 0) return -1;
  if (sdbSetRawStatus(pVgRaw, SDB_STATUS_READY) != 0) return -1;
  return 0;
}

// static int32_t mndSetUpdateIdxStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
//   SStbObj stbObj = {0};
//   taosRLockLatch(&pStb->lock);
//   memcpy(&stbObj, pStb, sizeof(SStbObj));
//   taosRUnLockLatch(&pStb->lock);
//   stbObj.numOfColumns = 0;
//   stbObj.pColumns = NULL;
//   stbObj.numOfTags = 0;
//  stbObj.pTags = NULL;
//   stbObj.numOfFuncs = 0;
//   stbObj.pFuncs = NULL;
//   stbObj.updateTime = taosGetTimestampMs();
//   stbObj.lock = 0;
//   stbObj.tagVer++;

// SSdbRaw *pCommitRaw = mndStbActionEncode(&stbObj);
// if (pCommitRaw == NULL) return -1;
// if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
// if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;
//
// return 0;
//}

static void mndDestroyIdxObj(SIdxObj *pIdxObj) {
  if (pIdxObj) {
    // do nothing
  }
}

static int32_t mndProcessCreateIdxReq(SRpcMsg *pReq) {
  SMnode  *pMnode = pReq->info.node;
  int32_t  code = -1;
  SStbObj *pStb = NULL;
  SIdxObj *pIdx = NULL;

  SDbObj            *pDb = NULL;
  SCreateTagIndexReq createReq = {0};

  if (tDeserializeSCreateTagIdxReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  mInfo("idx:%s start to create", createReq.idxName);
  // if (mndCheckCreateIdxReq(&createReq) != 0) {
  //   goto _OVER;
  // }

dengyihao's avatar
dengyihao 已提交
437
  pDb = mndAcquireDbByStb(pMnode, createReq.stbName);
dengyihao's avatar
dengyihao 已提交
438 439 440 441 442 443 444 445 446 447
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
    goto _OVER;
  }

  pStb = mndAcquireStb(pMnode, createReq.stbName);
  if (pStb == NULL) {
    mError("idx:%s, failed to create since stb:%s not exist", createReq.idxName, createReq.stbName);
    goto _OVER;
  }
dengyihao's avatar
dengyihao 已提交
448
  SSIdx idx = {0};
449
  if (mndAcquireGlobalIdx(pMnode, createReq.idxName, SDB_IDX, &idx) == 0) {
dengyihao's avatar
dengyihao 已提交
450
    pIdx = idx.pIdx;
dengyihao's avatar
dengyihao 已提交
451 452
  } else {
    goto _OVER;
dengyihao's avatar
dengyihao 已提交
453
  }
dengyihao's avatar
dengyihao 已提交
454
  if (pIdx != NULL) {
dengyihao's avatar
dengyihao 已提交
455
    terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
dengyihao's avatar
dengyihao 已提交
456 457 458
    goto _OVER;
  }

dengyihao's avatar
dengyihao 已提交
459 460 461
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
    goto _OVER;
  }
dengyihao's avatar
dengyihao 已提交
462

dengyihao's avatar
dengyihao 已提交
463 464 465 466 467 468
  code = mndAddIndex(pMnode, pReq, &createReq, pDb, pStb);
  if (terrno == TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST || terrno == TSDB_CODE_MND_TAG_NOT_EXIST) {
    return terrno;
  } else {
    if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
  }
dengyihao's avatar
dengyihao 已提交
469

dengyihao's avatar
dengyihao 已提交
470 471 472 473
_OVER:
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
    mError("stb:%s, failed to create since %s", createReq.idxName, terrstr());
  }
dengyihao's avatar
dengyihao 已提交
474

dengyihao's avatar
dengyihao 已提交
475 476 477
  mndReleaseStb(pMnode, pStb);
  mndReleaseIdx(pMnode, pIdx);
  mndReleaseDb(pMnode, pDb);
dengyihao's avatar
dengyihao 已提交
478

dengyihao's avatar
dengyihao 已提交
479
  return code;
dengyihao's avatar
dengyihao 已提交
480 481
}

dengyihao's avatar
dengyihao 已提交
482
int32_t mndSetDropIdxRedoLogs(SMnode *pMnode, STrans *pTrans, SIdxObj *pIdx) {
dengyihao's avatar
dengyihao 已提交
483 484 485 486
  SSdbRaw *pRedoRaw = mndIdxActionEncode(pIdx);
  if (pRedoRaw == NULL) return -1;
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) return -1;
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING) != 0) return -1;
dengyihao's avatar
dengyihao 已提交
487

dengyihao's avatar
dengyihao 已提交
488 489 490
  return 0;
}

dengyihao's avatar
dengyihao 已提交
491
int32_t mndSetDropIdxCommitLogs(SMnode *pMnode, STrans *pTrans, SIdxObj *pIdx) {
dengyihao's avatar
dengyihao 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504
  SSdbRaw *pCommitRaw = mndIdxActionEncode(pIdx);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

static int32_t mndProcessGetTbIdxReq(SRpcMsg *pReq) {
  //
  return 0;
}

dengyihao's avatar
dengyihao 已提交
505
int32_t mndRetrieveTagIdx(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
dengyihao's avatar
dengyihao 已提交
506 507 508
  SMnode  *pMnode = pReq->info.node;
  SSdb    *pSdb = pMnode->pSdb;
  int32_t  numOfRows = 0;
dengyihao's avatar
dengyihao 已提交
509
  SIdxObj *pIdx = NULL;
dengyihao's avatar
dengyihao 已提交
510 511 512 513 514 515 516
  int32_t  cols = 0;

  SDbObj *pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
    if (pDb == NULL) return 0;
  }
dengyihao's avatar
dengyihao 已提交
517 518
  SSmaAndTagIter *pIter = pShow->pIter;
  int             invalid = -1;
dengyihao's avatar
dengyihao 已提交
519
  while (numOfRows < rows) {
dengyihao's avatar
dengyihao 已提交
520 521
    pIter->pIdxIter = sdbFetch(pSdb, SDB_IDX, pIter->pIdxIter, (void **)&pIdx);
    if (pIter->pIdxIter == NULL) break;
dengyihao's avatar
dengyihao 已提交
522

dengyihao's avatar
dengyihao 已提交
523 524
    if (NULL != pDb && pIdx->dbUid != pDb->uid) {
      sdbRelease(pSdb, pIdx);
dengyihao's avatar
dengyihao 已提交
525 526 527 528 529
      continue;
    }

    cols = 0;

dengyihao's avatar
dengyihao 已提交
530 531
    SName idxName = {0};
    tNameFromString(&idxName, pIdx->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
dengyihao's avatar
dengyihao 已提交
532
    char n1[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
dengyihao's avatar
opt idx  
dengyihao 已提交
533

dengyihao's avatar
dengyihao 已提交
534
    STR_TO_VARSTR(n1, (char *)tNameGetTableName(&idxName));
dengyihao's avatar
dengyihao 已提交
535 536

    char n2[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
dengyihao's avatar
dengyihao 已提交
537
    STR_TO_VARSTR(n2, (char *)mndGetDbStr(pIdx->db));
dengyihao's avatar
dengyihao 已提交
538 539

    SName stbName = {0};
dengyihao's avatar
dengyihao 已提交
540
    tNameFromString(&stbName, pIdx->stb, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
dengyihao's avatar
dengyihao 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553
    char n3[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_TO_VARSTR(n3, (char *)tNameGetTableName(&stbName));

    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)n1, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)n2, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)n3, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
dengyihao's avatar
dengyihao 已提交
554 555

    colDataAppend(pColInfo, numOfRows, (const char *)&invalid, false);
dengyihao's avatar
dengyihao 已提交
556 557

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
dengyihao's avatar
dengyihao 已提交
558
    colDataAppend(pColInfo, numOfRows, (const char *)&pIdx->createdTime, false);
dengyihao's avatar
dengyihao 已提交
559

dengyihao's avatar
opt idx  
dengyihao 已提交
560 561 562 563 564 565 566 567 568 569 570 571
    char col[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_TO_VARSTR(col, (char *)pIdx->colName);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)col, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);

    char tag[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_TO_VARSTR(tag, (char *)"tag_index");
    colDataAppend(pColInfo, numOfRows, (const char *)tag, false);

dengyihao's avatar
dengyihao 已提交
572
    numOfRows++;
dengyihao's avatar
dengyihao 已提交
573
    sdbRelease(pSdb, pIdx);
dengyihao's avatar
dengyihao 已提交
574 575 576 577 578 579 580
  }

  mndReleaseDb(pMnode, pDb);
  pShow->numOfRows += numOfRows;
  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
581 582 583 584 585
// static void mndCancelGetNextIdx(SMnode *pMnode, void *pIter) {
//   SSdb *pSdb = pMnode->pSdb;
//
//  sdbCancelFetch(pSdb, pIter);
//}
dengyihao's avatar
dengyihao 已提交
586 587 588 589 590
static int32_t mndCheckIndexReq(SCreateTagIndexReq *pReq) {
  // impl
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
591 592
static int32_t mndSetUpdateIdxStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pOld, SStbObj *pNew, char *tagName,
                                            int on) {
dengyihao's avatar
dengyihao 已提交
593
  taosRLockLatch(&pOld->lock);
dengyihao's avatar
dengyihao 已提交
594
  memcpy(pNew, pOld, sizeof(SStbObj));
dengyihao's avatar
dengyihao 已提交
595 596
  taosRUnLockLatch(&pOld->lock);

dengyihao's avatar
dengyihao 已提交
597 598 599
  pNew->pTags = NULL;
  pNew->updateTime = taosGetTimestampMs();
  pNew->lock = 0;
dengyihao's avatar
dengyihao 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613

  int32_t tag = mndFindSuperTableTagId(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }
  col_id_t colId = pOld->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
    return -1;
  }
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }
  SSchema *pTag = pNew->pTags + tag;
dengyihao's avatar
dengyihao 已提交
614 615 616 617 618 619

  if (on == 1) {
    if (IS_IDX_ON(pTag)) {
      terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
      return -1;
    } else {
dengyihao's avatar
dengyihao 已提交
620
      SSCHMEA_SET_IDX_ON(pTag);
dengyihao's avatar
dengyihao 已提交
621
    }
dengyihao's avatar
dengyihao 已提交
622
  } else {
dengyihao's avatar
dengyihao 已提交
623 624 625
    if (!IS_IDX_ON(pTag)) {
      terrno = TSDB_CODE_MND_SMA_NOT_EXIST;
    } else {
dengyihao's avatar
dengyihao 已提交
626
      SSCHMEA_SET_IDX_OFF(pTag);
dengyihao's avatar
dengyihao 已提交
627 628
      pTag->flags = 0;
    }
dengyihao's avatar
dengyihao 已提交
629 630 631 632 633 634 635 636 637 638 639 640
  }
  pNew->tagVer++;

  SSdbRaw *pCommitRaw = mndStbActionEncode(pNew);
  if (pCommitRaw == NULL) return -1;
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) return -1;
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}
int32_t mndAddIndexImpl(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, SIdxObj *pIdx) {
  // impl later
641
  int32_t code = -1;
dengyihao's avatar
dengyihao 已提交
642
  SStbObj newStb = {0};
dengyihao's avatar
dengyihao 已提交
643 644 645 646 647
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "create-stb-index");
  if (pTrans == NULL) goto _OVER;

  // mInfo("trans:%d, used to add index to stb:%s", pTrans->id, pStb->name);
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
648
  if (mndTransCheckConflict(pMnode, pTrans) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
649 650 651 652 653 654

  mndTransSetSerial(pTrans);

  if (mndSetCreateIdxRedoLogs(pMnode, pTrans, pIdx) != 0) goto _OVER;
  if (mndSetCreateIdxCommitLogs(pMnode, pTrans, pIdx) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
655
  if (mndSetUpdateIdxStbCommitLogs(pMnode, pTrans, pStb, &newStb, pIdx->colName, 1) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
656
  if (mndSetCreateIdxRedoActions(pMnode, pTrans, pDb, &newStb, pIdx) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
657 658 659

  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
660
  code = 0;
dengyihao's avatar
dengyihao 已提交
661 662 663

_OVER:
  // mndDestoryIdxObj(pIdx);
dengyihao's avatar
dengyihao 已提交
664 665 666 667
  if (newStb.pTags != NULL) {
    taosMemoryFree(newStb.pTags);
    taosMemoryFree(newStb.pColumns);
  }
dengyihao's avatar
dengyihao 已提交
668 669 670
  mndTransDrop(pTrans);
  return code;
}
dengyihao's avatar
dengyihao 已提交
671

dengyihao's avatar
dengyihao 已提交
672
static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *req, SDbObj *pDb, SStbObj *pStb) {
673
  int32_t code = -1;
dengyihao's avatar
dengyihao 已提交
674
  SIdxObj idxObj = {0};
dengyihao's avatar
dengyihao 已提交
675
  memcpy(idxObj.name, req->idxName, TSDB_INDEX_FNAME_LEN);
dengyihao's avatar
dengyihao 已提交
676 677 678 679 680
  memcpy(idxObj.stb, pStb->name, TSDB_TABLE_FNAME_LEN);
  memcpy(idxObj.db, pDb->name, TSDB_DB_FNAME_LEN);
  memcpy(idxObj.colName, req->colName, TSDB_COL_NAME_LEN);

  idxObj.createdTime = taosGetTimestampMs();
dengyihao's avatar
dengyihao 已提交
681
  idxObj.uid = mndGenerateUid(req->idxName, strlen(req->idxName));
dengyihao's avatar
dengyihao 已提交
682 683 684 685 686 687 688
  idxObj.stbUid = pStb->uid;
  idxObj.dbUid = pStb->dbUid;

  int32_t tag = mndFindSuperTableTagId(pStb, req->colName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
dengyihao's avatar
dengyihao 已提交
689 690 691
  } else if (tag == 0) {
    terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
    return -1;
dengyihao's avatar
dengyihao 已提交
692
  }
dengyihao's avatar
dengyihao 已提交
693

dengyihao's avatar
dengyihao 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707
  col_id_t colId = pStb->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pStb->name, pStb->uid, colId) != 0) {
    return -1;
  }

  SSchema *pTag = pStb->pTags + tag;
  if (IS_IDX_ON(pTag)) {
    terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
    return -1;
  }
  code = mndAddIndexImpl(pMnode, pReq, pDb, pStb, &idxObj);

  return code;
}
dengyihao's avatar
dengyihao 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

static int32_t mndDropIdx(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SIdxObj *pIdx) {
  int32_t  code = -1;
  SStbObj *pStb = NULL;
  STrans  *pTrans = NULL;

  SStbObj newObj = {0};

  pStb = mndAcquireStb(pMnode, pIdx->stb);
  if (pStb == NULL) goto _OVER;

  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB, pReq, "drop-index");
  if (pTrans == NULL) goto _OVER;

  mInfo("trans:%d, used to drop idx:%s", pTrans->id, pIdx->name);
  mndTransSetDbName(pTrans, pDb->name, NULL);
724
  if (mndTransCheckConflict(pMnode, pTrans) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
725 726 727 728 729

  mndTransSetSerial(pTrans);
  if (mndSetDropIdxRedoLogs(pMnode, pTrans, pIdx) != 0) goto _OVER;
  if (mndSetDropIdxCommitLogs(pMnode, pTrans, pIdx) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
730
  if (mndSetUpdateIdxStbCommitLogs(pMnode, pTrans, pStb, &newObj, pIdx->colName, 0) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
731
  if (mndSetDropIdxRedoActions(pMnode, pTrans, pDb, &newObj, pIdx) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
732 733 734
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

  code = 0;
dengyihao's avatar
dengyihao 已提交
735 736

_OVER:
dengyihao's avatar
dengyihao 已提交
737 738
  taosMemoryFree(newObj.pTags);
  taosMemoryFree(newObj.pColumns);
dengyihao's avatar
dengyihao 已提交
739

dengyihao's avatar
dengyihao 已提交
740 741
  mndTransDrop(pTrans);
  mndReleaseStb(pMnode, pStb);
dengyihao's avatar
dengyihao 已提交
742
  return code;
dengyihao's avatar
dengyihao 已提交
743
}
dengyihao's avatar
dengyihao 已提交
744
int32_t mndProcessDropTagIdxReq(SRpcMsg *pReq) {
dengyihao's avatar
dengyihao 已提交
745 746 747 748 749 750
  SMnode  *pMnode = pReq->info.node;
  int32_t  code = -1;
  SDbObj  *pDb = NULL;
  SIdxObj *pIdx = NULL;

  SDropTagIndexReq req = {0};
dengyihao's avatar
dengyihao 已提交
751
  if (tDeserializeSDropTagIdxReq(pReq->pCont, pReq->contLen, &req) != 0) {
dengyihao's avatar
dengyihao 已提交
752 753 754 755
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }
  mInfo("idx:%s, start to drop", req.name);
dengyihao's avatar
dengyihao 已提交
756
  SSIdx idx = {0};
757
  if (mndAcquireGlobalIdx(pMnode, req.name, SDB_IDX, &idx) == 0) {
dengyihao's avatar
dengyihao 已提交
758
    pIdx = idx.pIdx;
dengyihao's avatar
dengyihao 已提交
759 760
  } else {
    goto _OVER;
dengyihao's avatar
dengyihao 已提交
761
  }
dengyihao's avatar
dengyihao 已提交
762 763 764 765 766 767
  if (pIdx == NULL) {
    if (req.igNotExists) {
      mInfo("idx:%s, not exist, ignore not exist is set", req.name);
      code = 0;
      goto _OVER;
    } else {
dengyihao's avatar
dengyihao 已提交
768
      terrno = TSDB_CODE_MND_TAG_INDEX_NOT_EXIST;
dengyihao's avatar
dengyihao 已提交
769 770 771 772
      goto _OVER;
    }
  }

dengyihao's avatar
dengyihao 已提交
773
  pDb = mndAcquireDbByIdx(pMnode, req.name);
dengyihao's avatar
dengyihao 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    goto _OVER;
  }

  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
    goto _OVER;
  }

  code = mndDropIdx(pMnode, pReq, pDb, pIdx);
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;

_OVER:
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
    mError("idx:%s, failed to drop since %s", req.name, terrstr());
  }
dengyihao's avatar
dengyihao 已提交
790 791
  mndReleaseIdx(pMnode, pIdx);
  mndReleaseDb(pMnode, pDb);
dengyihao's avatar
dengyihao 已提交
792
  return code;
dengyihao's avatar
dengyihao 已提交
793 794 795 796 797
}
static int32_t mndProcessGetIdxReq(SRpcMsg *pReq) {
  // do nothing
  return 0;
}
dengyihao's avatar
dengyihao 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

int32_t mndDropIdxsByStb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;

  while (1) {
    SIdxObj *pIdx = NULL;
    pIter = sdbFetch(pSdb, SDB_IDX, pIter, (void **)&pIdx);
    if (pIter == NULL) break;

    if (pIdx->stbUid == pStb->uid) {
      if (mndSetDropIdxCommitLogs(pMnode, pTrans, pIdx) != 0) {
        sdbRelease(pSdb, pIdx);
        sdbCancelFetch(pSdb, pIdx);
        return -1;
      }
    }

    sdbRelease(pSdb, pIdx);
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
822
int32_t mndGetIdxsByTagName(SMnode *pMnode, SStbObj *pStb, char *tagName, SIdxObj *idx) {
dengyihao's avatar
dengyihao 已提交
823 824 825 826 827 828 829 830 831
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;

  while (1) {
    SIdxObj *pIdx = NULL;
    pIter = sdbFetch(pSdb, SDB_IDX, pIter, (void **)&pIdx);
    if (pIter == NULL) break;

    if (pIdx->stbUid == pStb->uid && strcasecmp(pIdx->colName, tagName) == 0) {
dengyihao's avatar
dengyihao 已提交
832 833
      memcpy((char *)idx, (char *)pIdx, sizeof(SIdxObj));
      sdbRelease(pSdb, pIdx);
834
      sdbCancelFetch(pSdb, pIter);
dengyihao's avatar
dengyihao 已提交
835
      return 0;
dengyihao's avatar
dengyihao 已提交
836 837 838 839 840
    }

    sdbRelease(pSdb, pIdx);
  }

dengyihao's avatar
dengyihao 已提交
841 842
  return -1;
}
dengyihao's avatar
dengyihao 已提交
843 844 845 846 847 848 849 850 851 852 853 854
int32_t mndDropIdxsByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;

  while (1) {
    SIdxObj *pIdx = NULL;
    pIter = sdbFetch(pSdb, SDB_IDX, pIter, (void **)&pIdx);
    if (pIter == NULL) break;

    if (pIdx->dbUid == pDb->uid) {
      if (mndSetDropIdxCommitLogs(pMnode, pTrans, pIdx) != 0) {
        sdbRelease(pSdb, pIdx);
855
        sdbCancelFetch(pSdb, pIter);
dengyihao's avatar
dengyihao 已提交
856 857 858 859 860 861 862 863
        return -1;
      }
    }

    sdbRelease(pSdb, pIdx);
  }

  return 0;
864
}