tdbDb.c 3.5 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
Hongze Cheng 已提交
27
int tdbDbOpen(const char *fname, int keyLen, int valLen, tdb_cmpr_fn_t 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
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
52 53

    tdbEnvAddPager(pEnv, pPager);
H
Hongze Cheng 已提交
54 55
  }

H
refact  
Hongze Cheng 已提交
56
  ASSERT(pPager != NULL);
H
Hongze Cheng 已提交
57

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

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

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

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

H
Hongze Cheng 已提交
78
int tdbDbPut(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn) {
H
Hongze Cheng 已提交
79
  return tdbBtreeInsert(pDb->pBt, pKey, keyLen, pVal, valLen, pTxn);
H
Hongze Cheng 已提交
80 81
}

H
Hongze Cheng 已提交
82
int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) {
H
Hongze Cheng 已提交
83
  return tdbBtreeGet(pDb->pBt, pKey, kLen, ppVal, vLen);
H
Hongze Cheng 已提交
84 85
}

86 87 88 89
int tdbDbPGet(TDB *pDb, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen) {
  return tdbBtreePGet(pDb->pBt, pKey, kLen, ppKey, pkLen, ppVal, vLen);
}

H
Hongze Cheng 已提交
90
int tdbDbcOpen(TDB *pDb, TDBC **ppDbc, TXN *pTxn) {
H
Hongze Cheng 已提交
91 92
  int   ret;
  TDBC *pDbc = NULL;
H
Hongze Cheng 已提交
93 94

  *ppDbc = NULL;
H
Hongze Cheng 已提交
95
  pDbc = (TDBC *)tdbOsMalloc(sizeof(*pDbc));
H
Hongze Cheng 已提交
96 97 98 99
  if (pDbc == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
100
  tdbBtcOpen(&pDbc->btc, pDb->pBt, pTxn);
H
Hongze Cheng 已提交
101 102

  *ppDbc = pDbc;
H
Hongze Cheng 已提交
103 104 105
  return 0;
}

H
Hongze Cheng 已提交
106 107 108 109 110 111
int tdbDbcMoveTo(TDBC *pDbc, const void *pKey, int kLen, int *c) { return tdbBtcMoveTo(&pDbc->btc, pKey, kLen, c); }

int tdbDbcMoveToFirst(TDBC *pDbc) { return tdbBtcMoveToFirst(&pDbc->btc); }

int tdbDbcMoveToLast(TDBC *pDbc) { return tdbBtcMoveToLast(&pDbc->btc); }

H
Hongze Cheng 已提交
112 113 114 115 116 117
int tdbDbcMoveToNext(TDBC *pDbc) { return tdbBtcMoveToNext(&pDbc->btc); }

int tdbDbcMoveToPrev(TDBC *pDbc) { return tdbBtcMoveToPrev(&pDbc->btc); }

int tdbDbcGet(TDBC *pDbc, const void **ppKey, int *pkLen, const void **ppVal, int *pvLen) {
  return tdbBtcGet(&pDbc->btc, ppKey, pkLen, ppVal, pvLen);
H
Hongze Cheng 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
}

int tdbDbcPut(TDBC *pDbc, const void *pKey, int keyLen, const void *pVal, int valLen) {
  // TODO
  ASSERT(0);
  return 0;
}

int tdbDbcUpdate(TDBC *pDbc, const void *pKey, int kLen, const void *pVal, int vLen) {
  // TODO
  ASSERT(0);
  return 0;
}

int tdbDbcDrop(TDBC *pDbc) {
  // TODO
  ASSERT(0);
  return 0;
}

int tdbDbcNext(TDBC *pDbc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
H
Hongze Cheng 已提交
139
  return tdbBtreeNext(&pDbc->btc, ppKey, kLen, ppVal, vLen);
H
Hongze Cheng 已提交
140 141
}

H
Hongze Cheng 已提交
142
int tdbDbcClose(TDBC *pDbc) {
H
Hongze Cheng 已提交
143
  if (pDbc) {
H
Hongze Cheng 已提交
144
    tdbBtcClose(&pDbc->btc);
H
Hongze Cheng 已提交
145
    tdbOsFree(pDbc);
H
Hongze Cheng 已提交
146 147
  }

H
Hongze Cheng 已提交
148 149
  return 0;
}