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

#include "meta.h"
#include "metaDef.h"

int metaOpenDB(SMeta *pMeta) {
H
more  
Hongze Cheng 已提交
20
  char *             err = NULL;
H
refact  
Hongze Cheng 已提交
21 22
  rocksdb_options_t *dbOptions;
  rocksdb_options_t *idxOptions;
H
refact  
Hongze Cheng 已提交
23

H
more  
Hongze Cheng 已提交
24 25 26 27 28 29 30
  // Create LRU cache
  if (pMeta->options.lruCacheSize) {
    pMeta->metaDB.pCache = rocksdb_cache_create_lru(pMeta->options.lruCacheSize);
    if (pMeta->metaDB.pCache == NULL) {
      // TODO: handle error
      return -1;
    }
H
refact  
Hongze Cheng 已提交
31
  }
H
more  
Hongze Cheng 已提交
32

H
refact  
Hongze Cheng 已提交
33 34 35 36 37 38 39 40 41
  // Open raw data DB ---------------------------
  dbOptions = rocksdb_options_create();
  if (dbOptions == NULL) {
    // TODO: handle error
    return -1;
  }

  if (pMeta->metaDB.pCache) {
    rocksdb_options_set_row_cache(dbOptions, pMeta->metaDB.pCache);
H
more  
Hongze Cheng 已提交
42 43
  }

H
refact  
Hongze Cheng 已提交
44
  pMeta->metaDB.pDB = rocksdb_open(dbOptions, "db", &err);
H
more  
Hongze Cheng 已提交
45 46
  if (pMeta->metaDB.pDB == NULL) {
    // TODO: handle error
H
refact  
Hongze Cheng 已提交
47 48 49
    return -1;
  }

H
refact  
Hongze Cheng 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
  rocksdb_options_destroy(dbOptions);

  // Open index DB ---------------------------
  idxOptions = rocksdb_options_create();
  if (idxOptions == NULL) {
    // TODO: handle error
    return -1;
  }

  if (pMeta->metaDB.pCache) {
    rocksdb_options_set_row_cache(dbOptions, pMeta->metaDB.pCache);
  }

  pMeta->metaDB.pIdx = rocksdb_open(idxOptions, "index", &err);
H
more  
Hongze Cheng 已提交
64 65 66 67
  if (pMeta->metaDB.pIdx == NULL) {
    // TODO: handle error
    rocksdb_close(pMeta->metaDB.pDB);
    return -1;
H
refact  
Hongze Cheng 已提交
68 69
  }

H
refact  
Hongze Cheng 已提交
70 71
  rocksdb_options_destroy(idxOptions);

H
refact  
Hongze Cheng 已提交
72 73 74 75
  return 0;
}

void metaCloseDB(SMeta *pMeta) { /* TODO */
H
more  
Hongze Cheng 已提交
76
  // Close index DB
H
refact  
Hongze Cheng 已提交
77
  if (pMeta->metaDB.pIdx) {
H
more  
Hongze Cheng 已提交
78
    rocksdb_close(pMeta->metaDB.pIdx);
H
refact  
Hongze Cheng 已提交
79 80
  }

H
more  
Hongze Cheng 已提交
81
  // Close raw data DB
H
refact  
Hongze Cheng 已提交
82
  if (pMeta->metaDB.pDB) {
H
more  
Hongze Cheng 已提交
83 84 85 86 87 88
    rocksdb_close(pMeta->metaDB.pDB);
  }

  // Destroy cache
  if (pMeta->metaDB.pCache) {
    rocksdb_cache_destroy(pMeta->metaDB.pCache);
H
refact  
Hongze Cheng 已提交
89 90
  }
}