tsdbBDBImpl.c 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
C
Cary Xu 已提交
15 16 17 18

#define ALLOW_FORBID_FUNC
#include "db.h"

19
#include "taoserror.h"
C
Cary Xu 已提交
20 21 22
#include "tcoding.h"
#include "thash.h"
#include "tsdbDBDef.h"
23
#include "tsdbLog.h"
C
Cary Xu 已提交
24 25 26

#define IMPL_WITH_LOCK 1

27 28
static int  tsdbOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName, bool isDup);
static void tsdbCloseBDBDb(DB *pDB);
C
Cary Xu 已提交
29

30
#define BDB_PERR(info, code) fprintf(stderr, "%s:%d " info " reason: %s\n", __FILE__, __LINE__, db_strerror(code))
C
Cary Xu 已提交
31 32 33

int tsdbOpenDBF(TDBEnv pEnv, SDBFile *pDBF) {
  // TDBEnv is shared by a group of SDBFile
34 35 36 37
  if (!pEnv) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }
C
Cary Xu 已提交
38 39 40

  // Open DBF
  if (tsdbOpenBDBDb(&(pDBF->pDB), pEnv, pDBF->path, false) < 0) {
41
    terrno = TSDB_CODE_TDB_INIT_FAILED;
C
Cary Xu 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    tsdbCloseBDBDb(pDBF->pDB);
    return -1;
  }

  return 0;
}

static void *tsdbFreeDBF(SDBFile *pDBF) {
  if (pDBF) {
    free(pDBF);
  }
  return NULL;
}

void tsdbCloseDBF(SDBFile *pDBF) {
  if (pDBF->pDB) {
    tsdbCloseBDBDb(pDBF->pDB);
    pDBF->pDB = tsdbFreeDBF(pDBF);
  }
}

63
int32_t tsdbOpenBDBEnv(DB_ENV **ppEnv, const char *path) {
C
Cary Xu 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
  int     ret = 0;
  DB_ENV *pEnv = NULL;

  if (path == NULL) return 0;

  ret = db_env_create(&pEnv, 0);
  if (ret != 0) {
    BDB_PERR("Failed to create tsdb env", ret);
    return -1;
  }

  ret = pEnv->open(pEnv, path, DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL, 0);
  if (ret != 0) {
77 78
    // BDB_PERR("Failed to open tsdb env", ret);
    tsdbWarn("Failed to open tsdb env for path %s since %d", path ? path : "NULL", ret);
C
Cary Xu 已提交
79 80 81 82 83 84 85 86
    return -1;
  }

  *ppEnv = pEnv;

  return 0;
}

87
void tsdbCloseBDBEnv(DB_ENV *pEnv) {
C
Cary Xu 已提交
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
  if (pEnv) {
    pEnv->close(pEnv, 0);
  }
}

static int tsdbOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName, bool isDup) {
  int ret;
  DB *pDB;

  ret = db_create(&(pDB), pEnv, 0);
  if (ret != 0) {
    BDB_PERR("Failed to create DBP", ret);
    return -1;
  }

  if (isDup) {
    ret = pDB->set_flags(pDB, DB_DUPSORT);
    if (ret != 0) {
      BDB_PERR("Failed to set DB flags", ret);
      return -1;
    }
  }

  ret = pDB->open(pDB, NULL, pFName, NULL, DB_BTREE, DB_CREATE, 0);
  if (ret) {
    BDB_PERR("Failed to open DBF", ret);
    return -1;
  }

  *ppDB = pDB;

  return 0;
}

static void tsdbCloseBDBDb(DB *pDB) {
  if (pDB) {
    pDB->close(pDB, 0);
  }
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
}

int32_t tsdbSaveSmaToDB(SDBFile *pDBF, void *key, uint32_t keySize, void *data, uint32_t dataSize) {
  int ret;
  DBT key1 = {0}, value1 = {0};

  key1.data = key;
  key1.size = keySize;

  value1.data = data;
  value1.size = dataSize;

  // TODO: lock
  ret = pDBF->pDB->put(pDBF->pDB, NULL, &key1, &value1, 0);
  if (ret) {
    BDB_PERR("Failed to put data to DBF", ret);
    // TODO: unlock
    return -1;
  }
  // TODO: unlock

  return 0;
C
Cary Xu 已提交
148
}