metaIdx.c 3.3 KB
Newer Older
H
refact  
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
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
17
#include "index.h"
H
Hongze Cheng 已提交
18
#endif
H
more  
Hongze Cheng 已提交
19
#include "metaDef.h"
H
refact  
Hongze Cheng 已提交
20

H
Hongze Cheng 已提交
21
struct SMetaIdx {
dengyihao's avatar
dengyihao 已提交
22 23 24
#ifdef USE_INVERTED_INDEX
  SIndex *pIdx;
#endif
H
Hongze Cheng 已提交
25 26 27
  /* data */
};

H
refact  
Hongze Cheng 已提交
28
int metaOpenIdx(SMeta *pMeta) {
H
Hongze Cheng 已提交
29
#if 0
H
more  
Hongze Cheng 已提交
30 31 32 33 34 35 36 37 38 39
  char               idxDir[128];  // TODO
  char *             err = NULL;
  rocksdb_options_t *options = rocksdb_options_create();

  // TODO
  sprintf(idxDir, "%s/index", pMeta->path);

  if (pMeta->pCache) {
    rocksdb_options_set_row_cache(options, pMeta->pCache);
  }
H
refact  
Hongze Cheng 已提交
40
  rocksdb_options_set_create_if_missing(options, 1);
H
more  
Hongze Cheng 已提交
41 42 43 44 45 46 47 48 49

  pMeta->pIdx = rocksdb_open(options, idxDir, &err);
  if (pMeta->pIdx == NULL) {
    // TODO: handle error
    rocksdb_options_destroy(options);
    return -1;
  }

  rocksdb_options_destroy(options);
H
Hongze Cheng 已提交
50
#endif
H
more  
Hongze Cheng 已提交
51

dengyihao's avatar
dengyihao 已提交
52 53
#ifdef USE_INVERTED_INDEX
  SIndexOpts opts;
dengyihao's avatar
dengyihao 已提交
54
  if (indexOpen(&opts, pMeta->path, &pMeta->pIdx->pIdx) != 0) { return -1; }
dengyihao's avatar
dengyihao 已提交
55 56

#endif
H
refact  
Hongze Cheng 已提交
57 58 59
  return 0;
}

H
more  
Hongze Cheng 已提交
60
void metaCloseIdx(SMeta *pMeta) { /* TODO */
H
Hongze Cheng 已提交
61
#if 0
H
more  
Hongze Cheng 已提交
62 63 64 65
  if (pMeta->pIdx) {
    rocksdb_close(pMeta->pIdx);
    pMeta->pIdx = NULL;
  }
H
Hongze Cheng 已提交
66
#endif
dengyihao's avatar
dengyihao 已提交
67 68 69

#ifdef USE_INVERTED_INDEX
  SIndexOpts opts;
dengyihao's avatar
dengyihao 已提交
70
  if (indexClose(pMeta->pIdx->pIdx) != 0) { return -1; }
dengyihao's avatar
dengyihao 已提交
71 72

#endif
H
more  
Hongze Cheng 已提交
73 74
}

dengyihao's avatar
dengyihao 已提交
75 76
int metaSaveTableToIdx(SMeta *pMeta, const STbCfg *pTbCfg) {
#ifdef USE_INVERTED_INDEX
dengyihao's avatar
dengyihao 已提交
77
  if (pTbCfgs->type == META_CHILD_TABLE) {
dengyihao's avatar
dengyihao 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    char    buf[8] = {0};
    int16_t colId = (kvRowColIdx(pTbCfg->ctbCfg.pTag))[0].colId;
    sprintf(buf, "%d", colId);  // colname

    char *pTagVal = (char *)tdGetKVRowValOfCol(pTbCfg->ctbCfg.pTag, (kvRowColIdx(pTbCfg->ctbCfg.pTag))[0].colId);

    tb_uid_t         suid = pTbCfg->ctbCfg.suid;  // super id
    tb_uid_t         tuid = 0;                    // child table uid
    SIndexMultiTerm *terms = indexMultiTermCreate();
    SIndexTerm *     term =
        indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BINARY, buf, strlen(buf), pTagVal, strlen(pTagVal), tuid);
    indexMultiTermAdd(terms, term);

    int ret = indexPut(pMeta->pIdx->pIdx, terms);
    indexMultiTermDestroy(terms);
    return ret;
  } else {
    return DB_DONOTINDEX;
  }
#endif
H
more  
Hongze Cheng 已提交
98 99
  // TODO
  return 0;
H
more  
Hongze Cheng 已提交
100 101 102
}

int metaRemoveTableFromIdx(SMeta *pMeta, tb_uid_t uid) {
dengyihao's avatar
dengyihao 已提交
103 104 105
#ifdef USE_INVERTED_INDEX

#endif
H
more  
Hongze Cheng 已提交
106 107
  // TODO
  return 0;
dengyihao's avatar
dengyihao 已提交
108
}
C
Cary Xu 已提交
109

C
Cary Xu 已提交
110 111 112 113
int32_t metaCreateTSma(SMeta *pMeta, SSmaCfg *pCfg) {
  // TODO: Validate the cfg
  // The table uid should exists and be super table or common table.
  // Check other cfg value
C
Cary Xu 已提交
114 115 116

  // TODO: add atomicity

C
Cary Xu 已提交
117
  if (metaSaveSmaToDB(pMeta, &pCfg->tSma) < 0) {
C
Cary Xu 已提交
118 119 120
    // TODO: handle error
    return -1;
  }
C
Cary Xu 已提交
121
  return TSDB_CODE_SUCCESS;
C
Cary Xu 已提交
122
}
C
Cary Xu 已提交
123

C
Cary Xu 已提交
124
int32_t metaDropTSma(SMeta *pMeta, int64_t indexUid) {
C
Cary Xu 已提交
125 126 127
  // TODO: Validate the cfg
  // TODO: add atomicity

C
Cary Xu 已提交
128
  if (metaRemoveSmaFromDb(pMeta, indexUid) < 0) {
C
Cary Xu 已提交
129 130 131 132 133
    // TODO: handle error
    return -1;
  }
  return TSDB_CODE_SUCCESS;
}