metaMain.c 2.5 KB
Newer Older
H
more  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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 已提交
16 17
#include "tcoding.h"

H
Hongze Cheng 已提交
18
#include "vnodeInt.h"
H
more  
Hongze Cheng 已提交
19

H
more  
Hongze Cheng 已提交
20
static SMeta *metaNew(const char *path, const SMetaCfg *pMetaCfg, SMemAllocatorFactory *pMAF);
H
refact  
Hongze Cheng 已提交
21
static void   metaFree(SMeta *pMeta);
H
more  
Hongze Cheng 已提交
22 23
static int    metaOpenImpl(SMeta *pMeta);
static void   metaCloseImpl(SMeta *pMeta);
H
more  
Hongze Cheng 已提交
24

H
more  
Hongze Cheng 已提交
25
SMeta *metaOpen(const char *path, const SMetaCfg *pMetaCfg, SMemAllocatorFactory *pMAF) {
H
more  
Hongze Cheng 已提交
26 27
  SMeta *pMeta = NULL;

H
refact  
Hongze Cheng 已提交
28
  // Allocate handle
H
more  
Hongze Cheng 已提交
29
  pMeta = metaNew(path, pMetaCfg, pMAF);
H
more  
Hongze Cheng 已提交
30
  if (pMeta == NULL) {
H
refact  
Hongze Cheng 已提交
31 32 33 34
    // TODO: handle error
    return NULL;
  }

H
refact  
Hongze Cheng 已提交
35
  // Create META path (TODO)
H
refact  
Hongze Cheng 已提交
36 37
  taosMkDir(path);

H
more  
Hongze Cheng 已提交
38 39 40
  // Open meta
  if (metaOpenImpl(pMeta) < 0) {
    metaFree(pMeta);
H
refact  
Hongze Cheng 已提交
41 42
    return NULL;
  }
H
refact  
Hongze Cheng 已提交
43

L
Liu Jicong 已提交
44
  return pMeta;
H
more  
Hongze Cheng 已提交
45 46 47 48
}

void metaClose(SMeta *pMeta) {
  if (pMeta) {
H
more  
Hongze Cheng 已提交
49
    metaCloseImpl(pMeta);
H
refact  
Hongze Cheng 已提交
50
    metaFree(pMeta);
H
more  
Hongze Cheng 已提交
51 52 53
  }
}

H
refact  
Hongze Cheng 已提交
54
void metaRemove(const char *path) { taosRemoveDir(path); }
H
more  
Hongze Cheng 已提交
55 56

/* ------------------------ STATIC METHODS ------------------------ */
H
more  
Hongze Cheng 已提交
57
static SMeta *metaNew(const char *path, const SMetaCfg *pMetaCfg, SMemAllocatorFactory *pMAF) {
H
refact  
Hongze Cheng 已提交
58 59 60
  SMeta *pMeta;
  size_t psize = strlen(path);

wafwerar's avatar
wafwerar 已提交
61
  pMeta = (SMeta *)taosMemoryCalloc(1, sizeof(*pMeta));
H
refact  
Hongze Cheng 已提交
62 63 64 65 66 67
  if (pMeta == NULL) {
    return NULL;
  }

  pMeta->path = strdup(path);
  if (pMeta->path == NULL) {
H
refact  
Hongze Cheng 已提交
68
    metaFree(pMeta);
H
refact  
Hongze Cheng 已提交
69 70 71 72 73 74 75 76
    return NULL;
  }

  return pMeta;
};

static void metaFree(SMeta *pMeta) {
  if (pMeta) {
wafwerar's avatar
wafwerar 已提交
77 78
    taosMemoryFreeClear(pMeta->path);
    taosMemoryFree(pMeta);
H
refact  
Hongze Cheng 已提交
79 80 81
  }
}

H
more  
Hongze Cheng 已提交
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
static int metaOpenImpl(SMeta *pMeta) {
  // Open meta db
  if (metaOpenDB(pMeta) < 0) {
    // TODO: handle error
    metaCloseImpl(pMeta);
    return -1;
  }

  // Open meta index
  if (metaOpenIdx(pMeta) < 0) {
    // TODO: handle error
    metaCloseImpl(pMeta);
    return -1;
  }

  // Open meta table uid generator
  if (metaOpenUidGnrt(pMeta) < 0) {
    // TODO: handle error
    metaCloseImpl(pMeta);
    return -1;
  }

  return 0;
}

static void metaCloseImpl(SMeta *pMeta) {
  metaCloseUidGnrt(pMeta);
  metaCloseIdx(pMeta);
  metaCloseDB(pMeta);
H
refact  
Hongze Cheng 已提交
111
}