tdbDb.c 5.6 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 73 74
  if (ret < 0) {
    return -1;
  }
#endif

H
more  
Hongze Cheng 已提交
75
  *ppDb = pDb;
H
Hongze Cheng 已提交
76 77 78
  return 0;
}

H
Hongze Cheng 已提交
79
int tdbClose(TDB *pDb) {
H
Hongze Cheng 已提交
80 81
  SPager *pPager;

H
Hongze Cheng 已提交
82
  if (pDb) {
83 84 85 86
#ifdef USE_MAINDB
    if (pDb->pMainDb) tdbTbClose(pDb->pMainDb);
#endif

H
Hongze Cheng 已提交
87 88 89 90 91 92 93
    for (pPager = pDb->pgrList; pPager; pPager = pDb->pgrList) {
      pDb->pgrList = pPager->pNext;
      tdbPagerClose(pPager);
    }

    tdbPCacheClose(pDb->pCache);
    tdbOsFree(pDb->pgrHash);
H
Hongze Cheng 已提交
94 95
    tdbOsFree(pDb);
  }
H
Hongze Cheng 已提交
96

H
Hongze Cheng 已提交
97 98 99
  return 0;
}

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

102
int32_t tdbBegin(TDB *pDb, TXN *pTxn) {
H
Hongze Cheng 已提交
103 104
  SPager *pPager;
  int     ret;
H
Hongze Cheng 已提交
105

H
Hongze Cheng 已提交
106 107 108
  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerBegin(pPager, pTxn);
    if (ret < 0) {
M
Minglei Jin 已提交
109
      tdbError("failed to begin pager since %s. dbName:%s, txnId:%ld", tstrerror(terrno), pDb->dbName, pTxn->txnId);
H
Hongze Cheng 已提交
110 111 112
      return -1;
    }
  }
H
Hongze Cheng 已提交
113

H
Hongze Cheng 已提交
114
  return 0;
115 116
}

117
int32_t tdbCommit(TDB *pDb, TXN *pTxn) {
H
Hongze Cheng 已提交
118 119
  SPager *pPager;
  int     ret;
H
Hongze Cheng 已提交
120

H
Hongze Cheng 已提交
121 122 123
  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerCommit(pPager, pTxn);
    if (ret < 0) {
M
Minglei Jin 已提交
124
      tdbError("failed to commit pager since %s. dbName:%s, txnId:%ld", tstrerror(terrno), pDb->dbName, pTxn->txnId);
H
Hongze Cheng 已提交
125 126
      return -1;
    }
H
Hongze Cheng 已提交
127 128
  }

H
Hongze Cheng 已提交
129 130 131
  return 0;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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) {
      tdbError("failed to commit pager since %s. dbName:%s, txnId:%d", tstrerror(terrno), pDb->dbName, pTxn->txnId);
      return -1;
    }
  }

  return 0;
}

147 148 149 150 151 152 153
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) {
M
Minglei Jin 已提交
154
      tdbError("failed to abort pager since %s. dbName:%s, txnId:%ld", tstrerror(terrno), pDb->dbName, pTxn->txnId);
155 156 157 158 159 160 161
      return -1;
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
162 163 164
SPager *tdbEnvGetPager(TDB *pDb, const char *fname) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
165

H
Hongze Cheng 已提交
166 167
  hash = tdbCstringHash(fname);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
168
  tdbTrace("tdbttl getPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
169 170
  for (; *ppPager && (strcmp(fname, (*ppPager)->dbFileName) != 0); ppPager = &((*ppPager)->pHashNext)) {
  }
171
  tdbTrace("tdbttl getPager2: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
172 173
  return *ppPager;
}
H
Hongze Cheng 已提交
174

H
Hongze Cheng 已提交
175 176 177
void tdbEnvAddPager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
178

H
Hongze Cheng 已提交
179 180 181 182
  // rehash if neccessary
  if (pDb->nPager + 1 > pDb->nPgrHash) {
    // TODO
  }
H
Hongze Cheng 已提交
183

H
Hongze Cheng 已提交
184 185 186
  // add to list
  pPager->pNext = pDb->pgrList;
  pDb->pgrList = pPager;
H
Hongze Cheng 已提交
187

H
Hongze Cheng 已提交
188 189 190
  // add to hash
  hash = tdbCstringHash(pPager->dbFileName);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
191
  tdbTrace("tdbttl addPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, pPager->dbFileName);
H
Hongze Cheng 已提交
192 193
  pPager->pHashNext = *ppPager;
  *ppPager = pPager;
H
Hongze Cheng 已提交
194

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

H
Hongze Cheng 已提交
197 198
  // increase the counter
  pDb->nPager++;
H
Hongze Cheng 已提交
199 200
}

H
Hongze Cheng 已提交
201 202 203
void tdbEnvRemovePager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
refact  
Hongze Cheng 已提交
204

H
Hongze Cheng 已提交
205 206
  // remove from the list
  for (ppPager = &pDb->pgrList; *ppPager && (*ppPager != pPager); ppPager = &((*ppPager)->pNext)) {
H
Hongze Cheng 已提交
207
  }
H
Hongze Cheng 已提交
208 209
  ASSERT(*ppPager == pPager);
  *ppPager = pPager->pNext;
H
Hongze Cheng 已提交
210

H
Hongze Cheng 已提交
211 212 213 214 215 216 217 218 219 220
  // remove from hash
  hash = tdbCstringHash(pPager->dbFileName);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
  for (; *ppPager && *ppPager != pPager; ppPager = &((*ppPager)->pHashNext)) {
  }
  ASSERT(*ppPager == pPager);
  *ppPager = pPager->pNext;

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

H
Hongze Cheng 已提交
222 223 224 225
  // rehash if necessary
  if (pDb->nPgrHash > 8 && pDb->nPager < pDb->nPgrHash / 2) {
    // TODO
  }
226
}