metaBDBImpl.c 5.4 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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 "metaDef.h"

#include "db.h"

struct SMetaDB {
H
more  
Hongze Cheng 已提交
21 22 23
  DB *    pStbDB;
  DB *    pCtbDB;
  DB *    pNtbDB;
H
Hongze Cheng 已提交
24 25 26 27 28
  DB *    pIdx;
  DB_ENV *pEvn;
};

int metaOpenDB(SMeta *pMeta) {
H
more  
Hongze Cheng 已提交
29
  int ret;
H
Hongze Cheng 已提交
30 31 32 33 34 35 36

  pMeta->pDB = (SMetaDB *)calloc(1, sizeof(SMetaDB));
  if (pMeta->pDB == NULL) {
    // TODO: handle error
    return -1;
  }

H
more  
Hongze Cheng 已提交
37
  // create the env
H
more  
Hongze Cheng 已提交
38 39 40 41 42 43 44 45 46 47 48
  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 已提交
49

H
more  
Hongze Cheng 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
  ret = db_create(&(pMeta->pDB->pStbDB), pMeta->pDB->pEvn, 0);
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

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

  ret = db_create(&(pMeta->pDB->pNtbDB), pMeta->pDB->pEvn, 0);
H
Hongze Cheng 已提交
63 64 65 66 67 68 69 70 71 72 73
  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 已提交
74 75 76 77 78 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
  ret = pMeta->pDB->pStbDB->open(pMeta->pDB->pStbDB, /* 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 */
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

  ret = pMeta->pDB->pCtbDB->open(pMeta->pDB->pCtbDB, /* 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 */
  if (ret != 0) {
    // TODO: handle error
    return -1;
  }

  ret = pMeta->pDB->pNtbDB->open(pMeta->pDB->pNtbDB, /* 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 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  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;
  }

  // TODO
  return 0;
}

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

H
more  
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
    if (pMeta->pDB->pNtbDB) {
      pMeta->pDB->pNtbDB->close(pMeta->pDB->pNtbDB, 0);
      pMeta->pDB->pNtbDB = NULL;
    }

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

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

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

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

H
more  
Hongze Cheng 已提交
157
int metaSaveTableToDB(SMeta *pMeta, const STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  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;

  pMeta->pDB->pStbDB->put(pMeta->pDB->pStbDB, NULL, &key, &value, 0);

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

int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) {
  // TODO
}