tdbDb.c 6.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

18
int32_t tdbOpen(const char *dbname, int32_t szPage, int32_t pages, TDB **ppDb, int8_t rollback) {
H
Hongze Cheng 已提交
19 20 21 22 23 24
  TDB *pDb;
  int  dsize;
  int  zsize;
  int  tsize;
  u8  *pPtr;
  int  ret;
H
Hongze Cheng 已提交
25 26

  *ppDb = NULL;
H
more  
Hongze Cheng 已提交
27

H
Hongze Cheng 已提交
28
  dsize = strlen(dbname);
H
Hongze Cheng 已提交
29 30 31 32
  zsize = sizeof(*pDb) + dsize * 2 + strlen(TDB_JOURNAL_NAME) + 3;

  pPtr = (uint8_t *)tdbOsCalloc(1, zsize);
  if (pPtr == NULL) {
H
more  
Hongze Cheng 已提交
33 34 35
    return -1;
  }

H
Hongze Cheng 已提交
36 37 38
  pDb = (TDB *)pPtr;
  pPtr += sizeof(*pDb);
  // pDb->rootDir
H
Hongze Cheng 已提交
39 40 41
  pDb->dbName = pPtr;
  memcpy(pDb->dbName, dbname, dsize);
  pDb->dbName[dsize] = '\0';
H
Hongze Cheng 已提交
42 43
  pPtr = pPtr + dsize + 1;
  // pDb->jfname
H
Hongze Cheng 已提交
44 45 46 47 48
  pDb->jnName = pPtr;
  memcpy(pDb->jnName, dbname, dsize);
  pDb->jnName[dsize] = '/';
  memcpy(pDb->jnName + dsize + 1, TDB_JOURNAL_NAME, strlen(TDB_JOURNAL_NAME));
  pDb->jnName[dsize + 1 + strlen(TDB_JOURNAL_NAME)] = '\0';
H
Hongze Cheng 已提交
49 50 51 52 53 54

  pDb->jfd = -1;

  ret = tdbPCacheOpen(szPage, pages, &(pDb->pCache));
  if (ret < 0) {
    return -1;
H
Hongze Cheng 已提交
55 56
  }

H
Hongze Cheng 已提交
57 58 59 60
  pDb->nPgrHash = 8;
  tsize = sizeof(SPager *) * pDb->nPgrHash;
  pDb->pgrHash = tdbOsMalloc(tsize);
  if (pDb->pgrHash == NULL) {
H
more  
Hongze Cheng 已提交
61 62
    return -1;
  }
H
Hongze Cheng 已提交
63 64
  memset(pDb->pgrHash, 0, tsize);

wafwerar's avatar
wafwerar 已提交
65
  taosMulModeMkDir(dbname, 0755);
H
more  
Hongze Cheng 已提交
66

67 68
#ifdef USE_MAINDB
  // open main db
69
  ret = tdbTbOpen(TDB_MAINDB_NAME, -1, sizeof(SBtInfo), NULL, pDb, &pDb->pMainDb, rollback);
70 71 72
  if (ret < 0) {
    return -1;
  }
73 74 75 76 77

  ret = tdbTbOpen(TDB_FREEDB_NAME, sizeof(SPgno), 0, NULL, pDb, &pDb->pFreeDb, rollback);
  if (ret < 0) {
    return -1;
  }
78 79
#endif

H
more  
Hongze Cheng 已提交
80
  *ppDb = pDb;
H
Hongze Cheng 已提交
81 82 83
  return 0;
}

H
Hongze Cheng 已提交
84
int tdbClose(TDB *pDb) {
H
Hongze Cheng 已提交
85 86
  SPager *pPager;

H
Hongze Cheng 已提交
87
  if (pDb) {
88 89
#ifdef USE_MAINDB
    if (pDb->pMainDb) tdbTbClose(pDb->pMainDb);
90
    if (pDb->pFreeDb) tdbTbClose(pDb->pFreeDb);
91 92
#endif

H
Hongze Cheng 已提交
93 94 95 96 97 98 99
    for (pPager = pDb->pgrList; pPager; pPager = pDb->pgrList) {
      pDb->pgrList = pPager->pNext;
      tdbPagerClose(pPager);
    }

    tdbPCacheClose(pDb->pCache);
    tdbOsFree(pDb->pgrHash);
H
Hongze Cheng 已提交
100 101
    tdbOsFree(pDb);
  }
H
Hongze Cheng 已提交
102

H
Hongze Cheng 已提交
103 104 105
  return 0;
}

H
Hongze Cheng 已提交
106 107
int32_t tdbAlter(TDB *pDb, int pages) { return tdbPCacheAlter(pDb->pCache, pages); }

108 109
int32_t tdbBegin(TDB *pDb, TXN **ppTxn, void *(*xMalloc)(void *, size_t), void (*xFree)(void *, void *), void *xArg,
                 int flags) {
H
Hongze Cheng 已提交
110 111
  SPager *pPager;
  int     ret;
M
Minglei Jin 已提交
112
  int64_t txnId = ++pDb->txnId;
M
Minglei Jin 已提交
113 114 115
  if (txnId == INT64_MAX) {
    pDb->txnId = 0;
  }
116 117 118 119 120 121 122 123 124 125

  TXN *pTxn = tdbOsCalloc(1, sizeof(*pTxn));
  if (!pTxn) {
    return -1;
  }

  if (tdbTxnOpen(pTxn, txnId, xMalloc, xFree, xArg, flags) < 0) {
    tdbOsFree(pTxn);
    return -1;
  }
H
Hongze Cheng 已提交
126

H
Hongze Cheng 已提交
127 128 129
  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerBegin(pPager, pTxn);
    if (ret < 0) {
130 131
      tdbError("failed to begin pager since %s. dbName:%s, txnId:%" PRId64, tstrerror(terrno), pDb->dbName,
               pTxn->txnId);
132
      tdbTxnClose(pTxn);
H
Hongze Cheng 已提交
133 134 135
      return -1;
    }
  }
H
Hongze Cheng 已提交
136

137 138
  *ppTxn = pTxn;

H
Hongze Cheng 已提交
139
  return 0;
140 141
}

142
int32_t tdbCommit(TDB *pDb, TXN *pTxn) {
H
Hongze Cheng 已提交
143 144
  SPager *pPager;
  int     ret;
H
Hongze Cheng 已提交
145

H
Hongze Cheng 已提交
146 147 148
  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerCommit(pPager, pTxn);
    if (ret < 0) {
149 150
      tdbError("failed to commit pager since %s. dbName:%s, txnId:%" PRId64, tstrerror(terrno), pDb->dbName,
               pTxn->txnId);
H
Hongze Cheng 已提交
151 152
      return -1;
    }
H
Hongze Cheng 已提交
153 154
  }

H
Hongze Cheng 已提交
155 156 157
  return 0;
}

158 159 160 161 162 163 164
int32_t tdbPostCommit(TDB *pDb, TXN *pTxn) {
  SPager *pPager;
  int     ret;

  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerPostCommit(pPager, pTxn);
    if (ret < 0) {
165 166 167 168 169 170
      tdbError("failed to commit pager since %s. dbName:%s, txnId:%" PRId64, tstrerror(terrno), pDb->dbName,
               pTxn->txnId);
      return -1;
    }
  }

171 172
  tdbTxnClose(pTxn);

173 174 175 176 177 178 179 180 181 182 183 184
  return 0;
}

int32_t tdbPrepareAsyncCommit(TDB *pDb, TXN *pTxn) {
  SPager *pPager;
  int     ret;

  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerPrepareAsyncCommit(pPager, pTxn);
    if (ret < 0) {
      tdbError("failed to commit pager since %s. dbName:%s, txnId:%" PRId64, tstrerror(terrno), pDb->dbName,
               pTxn->txnId);
185 186 187 188 189 190 191
      return -1;
    }
  }

  return 0;
}

192 193 194 195 196 197 198
int32_t tdbAbort(TDB *pDb, TXN *pTxn) {
  SPager *pPager;
  int     ret;

  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerAbort(pPager, pTxn);
    if (ret < 0) {
199 200
      tdbError("failed to abort pager since %s. dbName:%s, txnId:%" PRId64, tstrerror(terrno), pDb->dbName,
               pTxn->txnId);
201 202 203 204
      return -1;
    }
  }

205 206
  tdbTxnClose(pTxn);

207 208 209
  return 0;
}

H
Hongze Cheng 已提交
210 211 212
SPager *tdbEnvGetPager(TDB *pDb, const char *fname) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
213

H
Hongze Cheng 已提交
214 215
  hash = tdbCstringHash(fname);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
216
  tdbTrace("tdbttl getPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
217 218
  for (; *ppPager && (strcmp(fname, (*ppPager)->dbFileName) != 0); ppPager = &((*ppPager)->pHashNext)) {
  }
219
  tdbTrace("tdbttl getPager2: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
220 221
  return *ppPager;
}
H
Hongze Cheng 已提交
222

H
Hongze Cheng 已提交
223 224 225
void tdbEnvAddPager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
226

H
Hongze Cheng 已提交
227 228 229 230
  // rehash if neccessary
  if (pDb->nPager + 1 > pDb->nPgrHash) {
    // TODO
  }
H
Hongze Cheng 已提交
231

H
Hongze Cheng 已提交
232 233 234
  // add to list
  pPager->pNext = pDb->pgrList;
  pDb->pgrList = pPager;
H
Hongze Cheng 已提交
235

H
Hongze Cheng 已提交
236 237 238
  // add to hash
  hash = tdbCstringHash(pPager->dbFileName);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
239
  tdbTrace("tdbttl addPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, pPager->dbFileName);
H
Hongze Cheng 已提交
240 241
  pPager->pHashNext = *ppPager;
  *ppPager = pPager;
H
Hongze Cheng 已提交
242

243 244
  tdbTrace("tdbttl addPager2: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, pPager->dbFileName);

H
Hongze Cheng 已提交
245 246
  // increase the counter
  pDb->nPager++;
H
Hongze Cheng 已提交
247 248
}

H
Hongze Cheng 已提交
249 250 251
void tdbEnvRemovePager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
refact  
Hongze Cheng 已提交
252

H
Hongze Cheng 已提交
253 254
  // remove from the list
  for (ppPager = &pDb->pgrList; *ppPager && (*ppPager != pPager); ppPager = &((*ppPager)->pNext)) {
H
Hongze Cheng 已提交
255
  }
256 257 258 259
  if (*ppPager != pPager) {
    tdbError("tdb/db: invalid pPager: %p, *ppPager: %p", pPager, *ppPager);
    return;
  }
H
Hongze Cheng 已提交
260
  *ppPager = pPager->pNext;
H
Hongze Cheng 已提交
261

H
Hongze Cheng 已提交
262 263 264 265 266
  // remove from hash
  hash = tdbCstringHash(pPager->dbFileName);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
  for (; *ppPager && *ppPager != pPager; ppPager = &((*ppPager)->pHashNext)) {
  }
267 268 269 270
  if (*ppPager != pPager) {
    tdbError("tdb/db: invalid pPager: %p, *ppPager: %p", pPager, *ppPager);
    return;
  }
H
Hongze Cheng 已提交
271 272 273 274
  *ppPager = pPager->pNext;

  // decrease the counter
  pDb->nPager--;
H
Hongze Cheng 已提交
275

H
Hongze Cheng 已提交
276 277 278 279
  // rehash if necessary
  if (pDb->nPgrHash > 8 && pDb->nPager < pDb->nPgrHash / 2) {
    // TODO
  }
280
}