metaDB.c 1.8 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 21 22 23 24 25
  char *             err = NULL;
  rocksdb_options_t *pOpts;

  pOpts = rocksdb_options_create();
  if (pOpts == NULL) {
    // TODO: handle error
H
refact  
Hongze Cheng 已提交
26 27 28
    return -1;
  }

H
more  
Hongze Cheng 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  // 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;
    }

    rocksdb_options_set_row_cache(pOpts, pMeta->metaDB.pCache);
  }

  // Open raw data DB
  pMeta->metaDB.pDB = rocksdb_open(pOpts, "db", &err);
  if (pMeta->metaDB.pDB == NULL) {
    // TODO: handle error
H
refact  
Hongze Cheng 已提交
44 45 46
    return -1;
  }

H
more  
Hongze Cheng 已提交
47 48 49 50 51 52
  // Open index DB
  pMeta->metaDB.pIdx = rocksdb_open(pOpts, "index", &err);
  if (pMeta->metaDB.pIdx == NULL) {
    // TODO: handle error
    rocksdb_close(pMeta->metaDB.pDB);
    return -1;
H
refact  
Hongze Cheng 已提交
53 54 55 56 57 58
  }

  return 0;
}

void metaCloseDB(SMeta *pMeta) { /* TODO */
H
more  
Hongze Cheng 已提交
59
  // Close index DB
H
refact  
Hongze Cheng 已提交
60
  if (pMeta->metaDB.pIdx) {
H
more  
Hongze Cheng 已提交
61
    rocksdb_close(pMeta->metaDB.pIdx);
H
refact  
Hongze Cheng 已提交
62 63
  }

H
more  
Hongze Cheng 已提交
64
  // Close raw data DB
H
refact  
Hongze Cheng 已提交
65
  if (pMeta->metaDB.pDB) {
H
more  
Hongze Cheng 已提交
66 67 68 69 70 71
    rocksdb_close(pMeta->metaDB.pDB);
  }

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