tsdbBDBImpl.c 3.7 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 23 24
#include "tcoding.h"
#include "thash.h"

#define IMPL_WITH_LOCK 1

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

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

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

  // Open DBF
  if (tsdbOpenBDBDb(&(pDBF->pDB), pEnv, pDBF->path, false) < 0) {
39
    terrno = TSDB_CODE_TDB_INIT_FAILED;
C
Cary Xu 已提交
40 41 42 43 44 45 46 47 48 49
    tsdbCloseBDBDb(pDBF->pDB);
    return -1;
  }

  return 0;
}

void tsdbCloseDBF(SDBFile *pDBF) {
  if (pDBF->pDB) {
    tsdbCloseBDBDb(pDBF->pDB);
C
Cary Xu 已提交
50
    pDBF->pDB = NULL;
C
Cary Xu 已提交
51
  }
wafwerar's avatar
wafwerar 已提交
52
  taosMemoryFreeClear(pDBF->path);
C
Cary Xu 已提交
53 54
}

55
int32_t tsdbOpenBDBEnv(DB_ENV **ppEnv, const char *path) {
C
Cary Xu 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
  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) {
C
Cary Xu 已提交
69 70
    terrno = TSDB_CODE_TDB_TDB_ENV_OPEN_ERROR;
    tsdbWarn("Failed to open tsdb env for path %s since ret %d != 0", path ? path : "NULL", ret);
C
Cary Xu 已提交
71 72 73 74 75 76 77 78
    return -1;
  }

  *ppEnv = pEnv;

  return 0;
}

79
void tsdbCloseBDBEnv(DB_ENV *pEnv) {
C
Cary Xu 已提交
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
  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);
  }
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

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 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}

void *tsdbGetSmaDataByKey(SDBFile *pDBF, void* key, uint32_t keySize, uint32_t *valueSize) {
  void *result = NULL;
  DBT   key1 = {0};
  DBT   value1 = {0};
  int   ret;

  // Set key/value
  key1.data = key;
  key1.size = keySize;

  // Query
  // TODO: lock
  ret = pDBF->pDB->get(pDBF->pDB, NULL, &key1, &value1, 0);
  // TODO: unlock
  if (ret != 0) {
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
160
  result = taosMemoryCalloc(1, value1.size);
C
Cary Xu 已提交
161 162 163 164 165 166 167 168 169 170

  if (result == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  *valueSize = value1.size;
  memcpy(result, value1.data, value1.size);

  return result;
C
Cary Xu 已提交
171
}