tdbDb.c 4.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
Hongze Cheng 已提交
18
int32_t tdbOpen(const char *dbname, int32_t szPage, int32_t pages, TDB **ppDb) {
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);

H
Hongze Cheng 已提交
65
  mkdir(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);
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;
}

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

H
Hongze Cheng 已提交
104 105 106 107 108 109 110
  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerBegin(pPager, pTxn);
    if (ret < 0) {
      ASSERT(0);
      return -1;
    }
  }
H
Hongze Cheng 已提交
111

H
Hongze Cheng 已提交
112
  return 0;
113 114
}

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

H
Hongze Cheng 已提交
119 120 121 122 123 124
  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerCommit(pPager, pTxn);
    if (ret < 0) {
      ASSERT(0);
      return -1;
    }
H
Hongze Cheng 已提交
125 126
  }

H
Hongze Cheng 已提交
127 128 129
  return 0;
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
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) {
      ASSERT(0);
      return -1;
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
145 146 147
SPager *tdbEnvGetPager(TDB *pDb, const char *fname) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
148

H
Hongze Cheng 已提交
149 150
  hash = tdbCstringHash(fname);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
151
  tdbTrace("tdbttl getPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
152 153
  for (; *ppPager && (strcmp(fname, (*ppPager)->dbFileName) != 0); ppPager = &((*ppPager)->pHashNext)) {
  }
154
  tdbTrace("tdbttl getPager2: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
155 156
  return *ppPager;
}
H
Hongze Cheng 已提交
157

H
Hongze Cheng 已提交
158 159 160
void tdbEnvAddPager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
161

H
Hongze Cheng 已提交
162 163 164 165
  // rehash if neccessary
  if (pDb->nPager + 1 > pDb->nPgrHash) {
    // TODO
  }
H
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167 168 169
  // add to list
  pPager->pNext = pDb->pgrList;
  pDb->pgrList = pPager;
H
Hongze Cheng 已提交
170

H
Hongze Cheng 已提交
171 172 173
  // add to hash
  hash = tdbCstringHash(pPager->dbFileName);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
174
  tdbTrace("tdbttl addPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, pPager->dbFileName);
H
Hongze Cheng 已提交
175 176
  pPager->pHashNext = *ppPager;
  *ppPager = pPager;
H
Hongze Cheng 已提交
177

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

H
Hongze Cheng 已提交
180 181
  // increase the counter
  pDb->nPager++;
H
Hongze Cheng 已提交
182 183
}

H
Hongze Cheng 已提交
184 185 186
void tdbEnvRemovePager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
refact  
Hongze Cheng 已提交
187

H
Hongze Cheng 已提交
188 189
  // remove from the list
  for (ppPager = &pDb->pgrList; *ppPager && (*ppPager != pPager); ppPager = &((*ppPager)->pNext)) {
H
Hongze Cheng 已提交
190
  }
H
Hongze Cheng 已提交
191 192
  ASSERT(*ppPager == pPager);
  *ppPager = pPager->pNext;
H
Hongze Cheng 已提交
193

H
Hongze Cheng 已提交
194 195 196 197 198 199 200 201 202 203
  // 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 已提交
204

H
Hongze Cheng 已提交
205 206 207 208
  // rehash if necessary
  if (pDb->nPgrHash > 8 && pDb->nPager < pDb->nPgrHash / 2) {
    // TODO
  }
209
}