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
#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

C
Cary Xu 已提交
32
int32_t tsdbOpenDBF(TDBEnv pEnv, SDBFile *pDBF) {
C
Cary Xu 已提交
33
  // 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
    tsdbCloseBDBDb(pDBF->pDB);
    return -1;
  }

  return 0;
}

void tsdbCloseDBF(SDBFile *pDBF) {
  if (pDBF->pDB) {
    tsdbCloseBDBDb(pDBF->pDB);
C
Cary Xu 已提交
52
    pDBF->pDB = NULL;
C
Cary Xu 已提交
53
  }
C
Cary Xu 已提交
54
  tfree(pDBF->path);
C
Cary Xu 已提交
55 56
}

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

  *ppEnv = pEnv;

  return 0;
}

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

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 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

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;
  }

  result = calloc(1, value1.size);

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