tdbDb.c 2.0 KB
Newer Older
H
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
#include "tdbInt.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
struct STDb {
H
Hongze Cheng 已提交
19
  STEnv  *pEnv;
H
more  
Hongze Cheng 已提交
20
  SBTree *pBt;
H
Hongze Cheng 已提交
21 22
};

H
more  
Hongze Cheng 已提交
23
int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprFn, STEnv *pEnv, STDb **ppDb) {
H
Hongze Cheng 已提交
24
  STDb   *pDb;
H
refact  
Hongze Cheng 已提交
25
  SPager *pPager;
H
more  
Hongze Cheng 已提交
26
  int     ret;
H
Hongze Cheng 已提交
27
  char    fFullName[TDB_FILENAME_LEN];
H
Hongze Cheng 已提交
28
  SPage  *pPage;
H
Hongze Cheng 已提交
29
  SPgno   pgno;
H
Hongze Cheng 已提交
30 31

  *ppDb = NULL;
H
more  
Hongze Cheng 已提交
32 33 34 35 36 37

  pDb = (STDb *)calloc(1, sizeof(*pDb));
  if (pDb == NULL) {
    return -1;
  }

H
more  
Hongze Cheng 已提交
38
  // pDb->pEnv
H
more  
Hongze Cheng 已提交
39 40
  pDb->pEnv = pEnv;

H
refact  
Hongze Cheng 已提交
41 42
  pPager = tdbEnvGetPager(pEnv, fname);
  if (pPager == NULL) {
H
Hongze Cheng 已提交
43
    snprintf(fFullName, TDB_FILENAME_LEN, "%s/%s", pEnv->rootDir, fname);
H
refact  
Hongze Cheng 已提交
44
    ret = tdbPagerOpen(pEnv->pCache, fFullName, &pPager);
H
Hongze Cheng 已提交
45 46 47 48 49
    if (ret < 0) {
      return -1;
    }
  }

H
refact  
Hongze Cheng 已提交
50
  ASSERT(pPager != NULL);
H
Hongze Cheng 已提交
51

H
more  
Hongze Cheng 已提交
52
  // pDb->pBt
H
Hongze Cheng 已提交
53
  ret = tdbBtreeOpen(keyLen, valLen, pPager, keyCmprFn, &(pDb->pBt));
H
more  
Hongze Cheng 已提交
54 55 56 57
  if (ret < 0) {
    return -1;
  }

H
more  
Hongze Cheng 已提交
58
  *ppDb = pDb;
H
Hongze Cheng 已提交
59 60 61 62 63 64 65 66
  return 0;
}

int tdbDbClose(STDb *pDb) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
67 68 69 70 71
int tdbDbDrop(STDb *pDb) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
72
int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) {
H
refact  
Hongze Cheng 已提交
73 74 75
  SBTC  btc;
  SBTC *pCur;
  int   ret;
H
Hongze Cheng 已提交
76 77 78 79 80 81 82 83 84 85 86 87

  pCur = &btc;
  ret = tdbBtreeCursor(pCur, pDb->pBt);
  if (ret < 0) {
    return -1;
  }

  ret = tdbBtCursorInsert(pCur, pKey, keyLen, pVal, valLen);
  if (ret < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
88 89 90
  return 0;
}

H
Hongze Cheng 已提交
91 92
int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) {
  return tdbBtreeGet(pDb->pBt, pKey, kLen, ppVal, vLen);
H
refact  
Hongze Cheng 已提交
93
}