metaMain.c 2.9 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
more  
Hongze Cheng 已提交
18 19
#include "metaDef.h"

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
  // Set default options
H
more  
Hongze Cheng 已提交
29 30
  if (pMetaCfg == NULL) {
    pMetaCfg = &defaultMetaOptions;
H
refact  
Hongze Cheng 已提交
31 32
  }

H
refact  
Hongze Cheng 已提交
33 34 35 36 37
  // // Validate the options
  // if (metaValidateOptions(pMetaCfg) < 0) {
  //   // TODO: deal with error
  //   return NULL;
  // }
H
refact  
Hongze Cheng 已提交
38 39

  // Allocate handle
H
more  
Hongze Cheng 已提交
40
  pMeta = metaNew(path, pMetaCfg, pMAF);
H
more  
Hongze Cheng 已提交
41
  if (pMeta == NULL) {
H
refact  
Hongze Cheng 已提交
42 43 44 45
    // TODO: handle error
    return NULL;
  }

H
refact  
Hongze Cheng 已提交
46
  // Create META path (TODO)
H
refact  
Hongze Cheng 已提交
47 48
  taosMkDir(path);

H
more  
Hongze Cheng 已提交
49 50 51
  // Open meta
  if (metaOpenImpl(pMeta) < 0) {
    metaFree(pMeta);
H
refact  
Hongze Cheng 已提交
52 53
    return NULL;
  }
H
refact  
Hongze Cheng 已提交
54

L
Liu Jicong 已提交
55
  return pMeta;
H
more  
Hongze Cheng 已提交
56 57 58 59
}

void metaClose(SMeta *pMeta) {
  if (pMeta) {
H
more  
Hongze Cheng 已提交
60
    metaCloseImpl(pMeta);
H
refact  
Hongze Cheng 已提交
61
    metaFree(pMeta);
H
more  
Hongze Cheng 已提交
62 63 64
  }
}

H
refact  
Hongze Cheng 已提交
65
void metaRemove(const char *path) { taosRemoveDir(path); }
H
more  
Hongze Cheng 已提交
66 67

/* ------------------------ STATIC METHODS ------------------------ */
H
more  
Hongze Cheng 已提交
68
static SMeta *metaNew(const char *path, const SMetaCfg *pMetaCfg, SMemAllocatorFactory *pMAF) {
H
refact  
Hongze Cheng 已提交
69 70 71 72 73 74 75 76 77 78
  SMeta *pMeta;
  size_t psize = strlen(path);

  pMeta = (SMeta *)calloc(1, sizeof(*pMeta));
  if (pMeta == NULL) {
    return NULL;
  }

  pMeta->path = strdup(path);
  if (pMeta->path == NULL) {
H
refact  
Hongze Cheng 已提交
79
    metaFree(pMeta);
H
refact  
Hongze Cheng 已提交
80 81 82
    return NULL;
  }

H
more  
Hongze Cheng 已提交
83
  metaOptionsCopy(&(pMeta->options), pMetaCfg);
H
more  
Hongze Cheng 已提交
84
  pMeta->pmaf = pMAF;
H
refact  
Hongze Cheng 已提交
85 86 87 88 89 90 91 92 93 94 95

  return pMeta;
};

static void metaFree(SMeta *pMeta) {
  if (pMeta) {
    tfree(pMeta->path);
    free(pMeta);
  }
}

H
more  
Hongze Cheng 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static int metaOpenImpl(SMeta *pMeta) {
  // Open meta cache
  if (metaOpenCache(pMeta) < 0) {
    // TODO: handle error
    metaCloseImpl(pMeta);
    return -1;
  }

  // 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);
  metaCloseCache(pMeta);
H
refact  
Hongze Cheng 已提交
133
}