tdbDb.c 2.9 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
    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 79
int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn) {
  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 91 92
int tdbDbcOpen(TDB *pDb, TDBC **ppDbc) {
  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, NULL);
H
Hongze Cheng 已提交
101 102 103 104 105 106 107 108 109 110

  // 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 已提交
111 112 113
  return 0;
}

H
Hongze Cheng 已提交
114
int tdbDbNext(TDBC *pDbc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
H
Hongze Cheng 已提交
115
  return tdbBtreeNext(&pDbc->btc, ppKey, kLen, ppVal, vLen);
H
Hongze Cheng 已提交
116 117
}

H
Hongze Cheng 已提交
118
int tdbDbcClose(TDBC *pDbc) {
H
Hongze Cheng 已提交
119
  if (pDbc) {
H
Hongze Cheng 已提交
120
    tdbOsFree(pDbc);
H
Hongze Cheng 已提交
121 122
  }

H
Hongze Cheng 已提交
123 124 125 126 127 128
  return 0;
}

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