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
Hongze Cheng 已提交
77
  return tdbBtreeInsert(pDb->pBt, pKey, keyLen, pVal, valLen);
H
Hongze Cheng 已提交
78 79
}

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

84 85 86 87
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 已提交
88 89 90
int tdbDbcOpen(TDB *pDb, TDBC **ppDbc) {
  int   ret;
  TDBC *pDbc = NULL;
H
Hongze Cheng 已提交
91 92

  *ppDbc = NULL;
H
Hongze Cheng 已提交
93
  pDbc = (TDBC *)tdbOsMalloc(sizeof(*pDbc));
H
Hongze Cheng 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  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 已提交
109 110 111
  return 0;
}

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

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

H
Hongze Cheng 已提交
121 122 123 124 125 126
  return 0;
}

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