tdbDb.c 3.7 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 tdbDbInsert(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 83
int tdbDbDelete(TDB *pDb, const void *pKey, int kLen, TXN *pTxn) { return tdbBtreeDelete(pDb->pBt, pKey, kLen, pTxn); }

H
Hongze Cheng 已提交
84 85
int tdbDbUpsert(TDB *pDb, const void *pKey, int kLen, const void *pVal, int vLen, TXN *pTxn) {
  return tdbBtreeUpsert(pDb->pBt, pKey, kLen, pVal, vLen, pTxn);
H
Hongze Cheng 已提交
86 87
}

H
Hongze Cheng 已提交
88
int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) {
H
Hongze Cheng 已提交
89
  return tdbBtreeGet(pDb->pBt, pKey, kLen, ppVal, vLen);
H
Hongze Cheng 已提交
90 91
}

92 93 94 95
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 已提交
96
int tdbDbcOpen(TDB *pDb, TDBC **ppDbc, TXN *pTxn) {
H
Hongze Cheng 已提交
97 98
  int   ret;
  TDBC *pDbc = NULL;
H
Hongze Cheng 已提交
99 100

  *ppDbc = NULL;
H
Hongze Cheng 已提交
101
  pDbc = (TDBC *)tdbOsMalloc(sizeof(*pDbc));
H
Hongze Cheng 已提交
102 103 104 105
  if (pDbc == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
106
  tdbBtcOpen(&pDbc->btc, pDb->pBt, pTxn);
H
Hongze Cheng 已提交
107 108

  *ppDbc = pDbc;
H
Hongze Cheng 已提交
109 110 111
  return 0;
}

H
Hongze Cheng 已提交
112 113 114 115 116 117
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 已提交
118 119 120 121 122 123
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 已提交
124 125
}

H
Hongze Cheng 已提交
126
int tdbDbcDelete(TDBC *pDbc) { return tdbBtcDelete(&pDbc->btc); }
H
Hongze Cheng 已提交
127 128

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

H
refact  
Hongze Cheng 已提交
132 133 134 135
int tdbDbcUpsert(TDBC *pDbc, const void *pKey, int nKey, const void *pData, int nData, int insert) {
  return tdbBtcUpsert(&pDbc->btc, pKey, nKey, pData, nData, insert);
}

H
Hongze Cheng 已提交
136
int tdbDbcClose(TDBC *pDbc) {
H
Hongze Cheng 已提交
137
  if (pDbc) {
H
Hongze Cheng 已提交
138
    tdbBtcClose(&pDbc->btc);
H
Hongze Cheng 已提交
139
    tdbOsFree(pDbc);
H
Hongze Cheng 已提交
140 141
  }

H
Hongze Cheng 已提交
142 143
  return 0;
}