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

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 69 70 71 72 73 74
#ifdef USE_MAINDB
  // open main db
  ret = tdbTbOpen(TDB_MAINDB_NAME, -1, sizeof(SPgno), NULL, pDb, &pDb->pMainDb);
  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 102
int tdbBegin(TDB *pDb, TXN *pTxn) {
  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
}

H
Hongze Cheng 已提交
115 116 117
int tdbCommit(TDB *pDb, TXN *pTxn) {
  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;
}

H
Hongze Cheng 已提交
130 131 132
SPager *tdbEnvGetPager(TDB *pDb, const char *fname) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
133

H
Hongze Cheng 已提交
134 135
  hash = tdbCstringHash(fname);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
136
  tdbTrace("tdbttl getPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
137 138
  for (; *ppPager && (strcmp(fname, (*ppPager)->dbFileName) != 0); ppPager = &((*ppPager)->pHashNext)) {
  }
139
  tdbTrace("tdbttl getPager2: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
140 141
  return *ppPager;
}
H
Hongze Cheng 已提交
142

H
Hongze Cheng 已提交
143 144 145
void tdbEnvAddPager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
146

H
Hongze Cheng 已提交
147 148 149 150
  // rehash if neccessary
  if (pDb->nPager + 1 > pDb->nPgrHash) {
    // TODO
  }
H
Hongze Cheng 已提交
151

H
Hongze Cheng 已提交
152 153 154
  // add to list
  pPager->pNext = pDb->pgrList;
  pDb->pgrList = pPager;
H
Hongze Cheng 已提交
155

H
Hongze Cheng 已提交
156 157 158
  // add to hash
  hash = tdbCstringHash(pPager->dbFileName);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
159
  tdbTrace("tdbttl addPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, pPager->dbFileName);
H
Hongze Cheng 已提交
160 161
  pPager->pHashNext = *ppPager;
  *ppPager = pPager;
H
Hongze Cheng 已提交
162

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

H
Hongze Cheng 已提交
165 166
  // increase the counter
  pDb->nPager++;
H
Hongze Cheng 已提交
167 168
}

H
Hongze Cheng 已提交
169 170 171
void tdbEnvRemovePager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
refact  
Hongze Cheng 已提交
172

H
Hongze Cheng 已提交
173 174
  // remove from the list
  for (ppPager = &pDb->pgrList; *ppPager && (*ppPager != pPager); ppPager = &((*ppPager)->pNext)) {
H
Hongze Cheng 已提交
175
  }
H
Hongze Cheng 已提交
176 177
  ASSERT(*ppPager == pPager);
  *ppPager = pPager->pNext;
H
Hongze Cheng 已提交
178

H
Hongze Cheng 已提交
179 180 181 182 183 184 185 186 187 188
  // 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 已提交
189

H
Hongze Cheng 已提交
190 191 192 193
  // rehash if necessary
  if (pDb->nPgrHash > 8 && pDb->nPager < pDb->nPgrHash / 2) {
    // TODO
  }
194
}