tdbDb.c 2.8 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
refact  
Hongze Cheng 已提交
18
struct STDB {
H
refact  
Hongze Cheng 已提交
19
  TENV   *pEnv;
H
more  
Hongze Cheng 已提交
20
  SBTree *pBt;
H
Hongze Cheng 已提交
21 22
};

H
Hongze Cheng 已提交
23 24 25 26
struct STDBC {
  SBTC btc;
};

H
refact  
Hongze Cheng 已提交
27
int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprFn, TENV *pEnv, TDB **ppDb) {
H
Hongze Cheng 已提交
28
  TDB    *pDb;
H
refact  
Hongze Cheng 已提交
29
  SPager *pPager;
H
more  
Hongze Cheng 已提交
30
  int     ret;
H
Hongze Cheng 已提交
31
  char    fFullName[TDB_FILENAME_LEN];
H
Hongze Cheng 已提交
32
  SPage  *pPage;
H
Hongze Cheng 已提交
33
  SPgno   pgno;
H
Hongze Cheng 已提交
34 35

  *ppDb = NULL;
H
more  
Hongze Cheng 已提交
36

H
Hongze Cheng 已提交
37
  pDb = (TDB *)tdbOsCalloc(1, sizeof(*pDb));
H
more  
Hongze Cheng 已提交
38 39 40 41
  if (pDb == NULL) {
    return -1;
  }

H
more  
Hongze Cheng 已提交
42
  // pDb->pEnv
H
more  
Hongze Cheng 已提交
43 44
  pDb->pEnv = pEnv;

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

H
refact  
Hongze Cheng 已提交
54
  ASSERT(pPager != NULL);
H
Hongze Cheng 已提交
55

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

H
more  
Hongze Cheng 已提交
62
  *ppDb = pDb;
H
Hongze Cheng 已提交
63 64 65
  return 0;
}

H
Hongze Cheng 已提交
66
int tdbDbClose(TDB *pDb) {
H
Hongze Cheng 已提交
67 68 69 70
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
71
int tdbDbDrop(TDB *pDb) {
H
Hongze Cheng 已提交
72 73 74 75
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
76
int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) {
H
refact  
Hongze Cheng 已提交
77 78 79
  SBTC  btc;
  SBTC *pCur;
  int   ret;
H
Hongze Cheng 已提交
80 81

  pCur = &btc;
H
Hongze Cheng 已提交
82
  ret = tdbBtcOpen(pCur, pDb->pBt);
H
Hongze Cheng 已提交
83 84 85 86 87 88 89 90 91
  if (ret < 0) {
    return -1;
  }

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

H
Hongze Cheng 已提交
92 93 94
  return 0;
}

H
Hongze Cheng 已提交
95
int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) {
H
Hongze Cheng 已提交
96
  return tdbBtreeGet(pDb->pBt, pKey, kLen, ppVal, vLen);
H
Hongze Cheng 已提交
97 98
}

H
Hongze Cheng 已提交
99 100 101
int tdbDbcOpen(TDB *pDb, TDBC **ppDbc) {
  int   ret;
  TDBC *pDbc = NULL;
H
Hongze Cheng 已提交
102 103

  *ppDbc = NULL;
H
Hongze Cheng 已提交
104
  pDbc = (TDBC *)tdbOsMalloc(sizeof(*pDbc));
H
Hongze Cheng 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  if (pDbc == NULL) {
    return -1;
  }

  tdbBtcOpen(&pDbc->btc, pDb->pBt);

  // TODO: move to first now, we can move to any key-value
  // and in any direction, design new APIs.
  ret = tdbBtcMoveToFirst(&pDbc->btc);
  if (ret < 0) {
    ASSERT(0);
    return -1;
  }

  *ppDbc = pDbc;
H
Hongze Cheng 已提交
120 121 122
  return 0;
}

H
Hongze Cheng 已提交
123
int tdbDbNext(TDBC *pDbc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
H
Hongze Cheng 已提交
124
  return tdbBtreeNext(&pDbc->btc, ppKey, kLen, ppVal, vLen);
H
Hongze Cheng 已提交
125 126
}

H
Hongze Cheng 已提交
127
int tdbDbcClose(TDBC *pDbc) {
H
Hongze Cheng 已提交
128
  if (pDbc) {
H
Hongze Cheng 已提交
129
    tdbOsFree(pDbc);
H
Hongze Cheng 已提交
130 131
  }

H
Hongze Cheng 已提交
132 133 134 135 136 137
  return 0;
}

int tdbDbcInsert(TDBC *pDbc, const void *pKey, int keyLen, const void *pVal, int valLen) {
  // TODO
  ASSERT(0);
H
Hongze Cheng 已提交
138
  return 0;
H
refact  
Hongze Cheng 已提交
139
}