metaBDBImpl.c 5.9 KB
Newer Older
H
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
more  
Hongze Cheng 已提交
16 17
#include "db.h"

H
Hongze Cheng 已提交
18 19
#include "metaDef.h"

H
more  
Hongze Cheng 已提交
20
#include "thash.h"
H
Hongze Cheng 已提交
21 22

struct SMetaDB {
H
more  
Hongze Cheng 已提交
23 24 25 26 27 28 29 30 31 32
  // DB
  DB *      pStbDB;
  DB *      pNtbDB;
  SHashObj *pCtbMap;
  DB *      pSchemaDB;
  // IDX
  SHashObj *pIdxMap;
  DB *      pNameIdx;
  DB *      pUidIdx;
  // ENV
H
Hongze Cheng 已提交
33 34 35
  DB_ENV *pEvn;
};

H
more  
Hongze Cheng 已提交
36 37 38
static SMetaDB *metaNewDB();
static void     metaFreeDB(SMetaDB *pDB);

H
Hongze Cheng 已提交
39
int metaOpenDB(SMeta *pMeta) {
H
more  
Hongze Cheng 已提交
40 41
  int      ret;
  SMetaDB *pDB;
H
Hongze Cheng 已提交
42

H
more  
Hongze Cheng 已提交
43
  pMeta->pDB = metaNewDB();
H
Hongze Cheng 已提交
44

H
more  
Hongze Cheng 已提交
45
#if 0
H
more  
Hongze Cheng 已提交
46
  // create the env
H
more  
Hongze Cheng 已提交
47 48 49 50 51 52 53 54 55 56 57
  ret = db_env_create(&(pMeta->pDB->pEvn), 0);
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

  ret = pMeta->pDB->pEvn->open(pMeta->pDB->pEvn, pMeta->path, DB_CREATE | DB_INIT_MPOOL, 0);
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }
H
Hongze Cheng 已提交
58

H
more  
Hongze Cheng 已提交
59
  ret = db_create(&(pMeta->pDB->pDB), pMeta->pDB->pEvn, 0);
H
more  
Hongze Cheng 已提交
60 61 62 63 64
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

H
more  
Hongze Cheng 已提交
65
  ret = db_create(&(pMeta->pDB->pSchemaDB), pMeta->pDB->pEvn, 0);
H
Hongze Cheng 已提交
66 67 68 69 70 71 72 73 74 75 76
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

  ret = db_create(&(pMeta->pDB->pIdx), pMeta->pDB->pEvn, 0);
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

H
more  
Hongze Cheng 已提交
77 78 79 80 81 82 83
  ret = pMeta->pDB->pDB->open(pMeta->pDB->pDB, /* DB structure pointer */
                              NULL,            /* Transaction pointer */
                              "meta.db",       /* On-disk file that holds the database */
                              NULL,            /* Optional logical database name */
                              DB_BTREE,        /* Database access method */
                              DB_CREATE,       /* Open flags */
                              0);              /* File mode */
H
more  
Hongze Cheng 已提交
84 85 86 87 88
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

H
more  
Hongze Cheng 已提交
89 90 91 92 93 94 95
  ret = pMeta->pDB->pSchemaDB->open(pMeta->pDB->pSchemaDB, /* DB structure pointer */
                                    NULL,                  /* Transaction pointer */
                                    "meta.db",             /* On-disk file that holds the database */
                                    NULL,                  /* Optional logical database name */
                                    DB_BTREE,              /* Database access method */
                                    DB_CREATE,             /* Open flags */
                                    0);                    /* File mode */
H
Hongze Cheng 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

  ret = pMeta->pDB->pIdx->open(pMeta->pDB->pIdx, /* DB structure pointer */
                               NULL,             /* Transaction pointer */
                               "index.db",       /* On-disk file that holds the database */
                               NULL,             /* Optional logical database name */
                               DB_BTREE,         /* Database access method */
                               DB_CREATE,        /* Open flags */
                               0);               /* File mode */
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

H
more  
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122 123
  ret = pMeta->pDB->pDB->associate(pMeta->pDB->pDB,  /* Primary database */
                                   NULL,             /* TXN id */
                                   pMeta->pDB->pIdx, /* Secondary database */
                                   metaIdxCallback,  /* Callback used for key creation */
                                   0);               /* Flags */
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }
#endif

H
Hongze Cheng 已提交
124 125 126 127
  return 0;
}

void metaCloseDB(SMeta *pMeta) {
H
more  
Hongze Cheng 已提交
128 129 130
  metaFreeDB(pMeta->pDB);
  pMeta->pDB = NULL;
#if 0
H
Hongze Cheng 已提交
131
  if (pMeta->pDB) {
H
more  
Hongze Cheng 已提交
132 133 134 135 136
    if (pMeta->pDB->pIdx) {
      pMeta->pDB->pIdx->close(pMeta->pDB->pIdx, 0);
      pMeta->pDB->pIdx = NULL;
    }

H
more  
Hongze Cheng 已提交
137 138 139
    if (pMeta->pDB->pSchemaDB) {
      pMeta->pDB->pSchemaDB->close(pMeta->pDB->pSchemaDB, 0);
      pMeta->pDB->pSchemaDB = NULL;
H
more  
Hongze Cheng 已提交
140 141
    }

H
more  
Hongze Cheng 已提交
142 143 144
    if (pMeta->pDB->pDB) {
      pMeta->pDB->pDB->close(pMeta->pDB->pDB, 0);
      pMeta->pDB->pDB = NULL;
H
more  
Hongze Cheng 已提交
145 146 147 148 149 150 151
    }

    if (pMeta->pDB->pEvn) {
      pMeta->pDB->pEvn->close(pMeta->pDB->pEvn, 0);
      pMeta->pDB->pEvn = NULL;
    }

H
Hongze Cheng 已提交
152 153
    free(pMeta->pDB);
  }
H
more  
Hongze Cheng 已提交
154
#endif
H
Hongze Cheng 已提交
155 156
}

H
more  
Hongze Cheng 已提交
157 158
int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) {
#if 0
H
more  
Hongze Cheng 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  tb_uid_t uid;
  DBT      key = {0};
  DBT      value = {0};
  char     buf[256];
  void *   pBuf;
  int      bsize;

  if (pTbCfg->type == META_SUPER_TABLE) {
    uid = pTbCfg->stbCfg.suid;
  } else {
    uid = metaGenerateUid(pMeta);
  }

  key.size = sizeof(uid);
  key.data = &uid;

  pBuf = buf;
  value.size = metaEncodeTbCfg(&pBuf, pTbCfg);
  value.data = buf;

H
more  
Hongze Cheng 已提交
179 180
  pMeta->pDB->pDB->put(pMeta->pDB->pDB, NULL, &key, &value, 0);
#endif
H
more  
Hongze Cheng 已提交
181

H
Hongze Cheng 已提交
182 183 184 185 186
  return 0;
}

int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) {
  // TODO
H
more  
Hongze Cheng 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
}

/* ------------------------ STATIC METHODS ------------------------ */
static SMetaDB *metaNewDB() {
  SMetaDB *pDB;
  pDB = (SMetaDB *)calloc(1, sizeof(*pDB));
  if (pDB == NULL) {
    return NULL;
  }

  pDB->pCtbMap = taosHashInit(0, MurmurHash3_32, false, HASH_NO_LOCK);
  if (pDB->pCtbMap == NULL) {
    metaFreeDB(pDB);
    return NULL;
  }

  pDB->pIdxMap = taosHashInit(0, MurmurHash3_32, false, HASH_NO_LOCK);
  if (pDB->pIdxMap == NULL) {
    metaFreeDB(pDB);
    return NULL;
  }

  return pDB;
}

static void metaFreeDB(SMetaDB *pDB) {
  if (pDB == NULL) {
    if (pDB->pIdxMap) {
      taosHashCleanup(pDB->pIdxMap);
    }

    if (pDB->pCtbMap) {
      taosHashCleanup(pDB->pCtbMap);
    }

    free(pDB);
  }
H
Hongze Cheng 已提交
224
}