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"

H
Hongze Cheng 已提交
19
#include "vnodeInt.h"
C
Cary Xu 已提交
20 21 22

#define IMPL_WITH_LOCK 1

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

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

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

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

  return 0;
}

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

53
int32_t tsdbOpenBDBEnv(DB_ENV **ppEnv, const char *path) {
C
Cary Xu 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66
  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 已提交
67 68
    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 已提交
69 70 71 72 73 74 75 76
    return -1;
  }

  *ppEnv = pEnv;

  return 0;
}

77
void tsdbCloseBDBEnv(DB_ENV *pEnv) {
C
Cary Xu 已提交
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 105 106 107 108 109 110 111 112 113 114 115
  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);
  }
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

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 已提交
138 139
}

H
Hongze Cheng 已提交
140
void *tsdbGetSmaDataByKey(SDBFile *pDBF, void *key, uint32_t keySize, uint32_t *valueSize) {
C
Cary Xu 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  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 已提交
158
  result = taosMemoryCalloc(1, value1.size);
C
Cary Xu 已提交
159 160 161 162 163 164 165 166 167 168

  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 已提交
169
}