tdbTable.c 5.2 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#include "tdbInt.h"

struct STTB {
19
  TDB    *pEnv;
H
Hongze Cheng 已提交
20 21 22 23 24 25 26
  SBTree *pBt;
};

struct STBC {
  SBTC btc;
};

27 28
int tdbTbOpen(const char *tbname, int keyLen, int valLen, tdb_cmpr_fn_t keyCmprFn, TDB *pEnv, TTB **ppTb,
              int8_t rollback) {
29
  TTB    *pTb;
H
Hongze Cheng 已提交
30 31 32
  SPager *pPager;
  int     ret;
  char    fFullName[TDB_FILENAME_LEN];
33
  SPage  *pPage;
H
Hongze Cheng 已提交
34
  SPgno   pgno;
35 36 37 38
  void   *pKey = NULL;
  int     nKey = 0;
  void   *pData = NULL;
  int     nData = 0;
H
Hongze Cheng 已提交
39 40 41 42 43 44 45 46 47 48 49

  *ppTb = NULL;

  pTb = (TTB *)tdbOsCalloc(1, sizeof(*pTb));
  if (pTb == NULL) {
    return -1;
  }

  // pTb->pEnv
  pTb->pEnv = pEnv;

50 51 52 53 54 55
#ifdef USE_MAINDB
  snprintf(fFullName, TDB_FILENAME_LEN, "%s/%s", pEnv->dbName, TDB_MAINDB_NAME);

  if (strcmp(TDB_MAINDB_NAME, tbname)) {
    pPager = tdbEnvGetPager(pEnv, fFullName);
    if (!pPager) {
M
Minglei Jin 已提交
56
      tdbOsFree(pTb);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      return -1;
    }

    ret = tdbTbGet(pPager->pEnv->pMainDb, tbname, strlen(tbname) + 1, &pData, &nData);
    if (ret < 0) {
      // new pgno & insert into main db
      pgno = 0;
    } else {
      pgno = *(SPgno *)pData;

      tdbFree(pKey);
      tdbFree(pData);
    }

  } else {
    pPager = tdbEnvGetPager(pEnv, fFullName);
    if (pPager == NULL) {
      ret = tdbPagerOpen(pEnv->pCache, fFullName, &pPager);
      if (ret < 0) {
M
Minglei Jin 已提交
76
        tdbOsFree(pTb);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        return -1;
      }

      tdbEnvAddPager(pEnv, pPager);

      pPager->pEnv = pEnv;
    }

    if (pPager->dbOrigSize > 0) {
      pgno = 1;
    } else {
      pgno = 0;
    }
  }

#else

H
Hongze Cheng 已提交
94
  pPager = tdbEnvGetPager(pEnv, tbname);
H
Hongze Cheng 已提交
95
  if (pPager == NULL) {
H
Hongze Cheng 已提交
96
    snprintf(fFullName, TDB_FILENAME_LEN, "%s/%s", pEnv->dbName, tbname);
H
Hongze Cheng 已提交
97 98
    ret = tdbPagerOpen(pEnv->pCache, fFullName, &pPager);
    if (ret < 0) {
M
Minglei Jin 已提交
99
      tdbOsFree(pTb);
H
Hongze Cheng 已提交
100 101 102 103 104 105
      return -1;
    }

    tdbEnvAddPager(pEnv, pPager);
  }

106 107
#endif

108
  ASSERT(pPager != NULL);
H
Hongze Cheng 已提交
109 110

  // pTb->pBt
111
  ret = tdbBtreeOpen(keyLen, valLen, pPager, tbname, pgno, keyCmprFn, pEnv, &(pTb->pBt));
H
Hongze Cheng 已提交
112
  if (ret < 0) {
M
Minglei Jin 已提交
113
    tdbOsFree(pTb);
H
Hongze Cheng 已提交
114 115 116
    return -1;
  }

117 118 119
  if (rollback) {
    tdbPagerRollback(pPager);
  } else {
120
    ret = tdbPagerRestoreJournals(pPager, pTb->pBt);
121 122 123 124
    if (ret < 0) {
      tdbOsFree(pTb);
      return -1;
    }
125 126
  }

H
Hongze Cheng 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  *ppTb = pTb;
  return 0;
}

int tdbTbClose(TTB *pTb) {
  if (pTb) {
    tdbBtreeClose(pTb->pBt);
    tdbOsFree(pTb);
  }
  return 0;
}

int tdbTbDrop(TTB *pTb) {
  // TODO
  return 0;
}

int tdbTbInsert(TTB *pTb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn) {
  return tdbBtreeInsert(pTb->pBt, pKey, keyLen, pVal, valLen, pTxn);
}

int tdbTbDelete(TTB *pTb, const void *pKey, int kLen, TXN *pTxn) { return tdbBtreeDelete(pTb->pBt, pKey, kLen, pTxn); }

int tdbTbUpsert(TTB *pTb, const void *pKey, int kLen, const void *pVal, int vLen, TXN *pTxn) {
  return tdbBtreeUpsert(pTb->pBt, pKey, kLen, pVal, vLen, pTxn);
}

int tdbTbGet(TTB *pTb, const void *pKey, int kLen, void **ppVal, int *vLen) {
  return tdbBtreeGet(pTb->pBt, pKey, kLen, ppVal, vLen);
}

int tdbTbPGet(TTB *pTb, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen) {
  return tdbBtreePGet(pTb->pBt, pKey, kLen, ppKey, pkLen, ppVal, vLen);
}

int tdbTbcOpen(TTB *pTb, TBC **ppTbc, TXN *pTxn) {
  int  ret;
  TBC *pTbc = NULL;

  *ppTbc = NULL;
  pTbc = (TBC *)tdbOsMalloc(sizeof(*pTbc));
  if (pTbc == NULL) {
    return -1;
  }

  tdbBtcOpen(&pTbc->btc, pTb->pBt, pTxn);

  *ppTbc = pTbc;
  return 0;
}

int tdbTbcMoveTo(TBC *pTbc, const void *pKey, int kLen, int *c) { return tdbBtcMoveTo(&pTbc->btc, pKey, kLen, c); }

int tdbTbcMoveToFirst(TBC *pTbc) { return tdbBtcMoveToFirst(&pTbc->btc); }

int tdbTbcMoveToLast(TBC *pTbc) { return tdbBtcMoveToLast(&pTbc->btc); }

int tdbTbcMoveToNext(TBC *pTbc) { return tdbBtcMoveToNext(&pTbc->btc); }

int tdbTbcMoveToPrev(TBC *pTbc) { return tdbBtcMoveToPrev(&pTbc->btc); }

int tdbTbcGet(TBC *pTbc, const void **ppKey, int *pkLen, const void **ppVal, int *pvLen) {
  return tdbBtcGet(&pTbc->btc, ppKey, pkLen, ppVal, pvLen);
}

int tdbTbcDelete(TBC *pTbc) { return tdbBtcDelete(&pTbc->btc); }

int tdbTbcNext(TBC *pTbc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
  return tdbBtreeNext(&pTbc->btc, ppKey, kLen, ppVal, vLen);
}

198 199 200 201
int tdbTbcPrev(TBC *pTbc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
  return tdbBtreePrev(&pTbc->btc, ppKey, kLen, ppVal, vLen);
}

H
Hongze Cheng 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214
int tdbTbcUpsert(TBC *pTbc, const void *pKey, int nKey, const void *pData, int nData, int insert) {
  return tdbBtcUpsert(&pTbc->btc, pKey, nKey, pData, nData, insert);
}

int tdbTbcClose(TBC *pTbc) {
  if (pTbc) {
    tdbBtcClose(&pTbc->btc);
    tdbOsFree(pTbc);
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
215
int tdbTbcIsValid(TBC *pTbc) { return tdbBtcIsValid(&pTbc->btc); }