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

H
more  
Hongze Cheng 已提交
67
  *ppDb = pDb;
H
Hongze Cheng 已提交
68 69 70
  return 0;
}

H
Hongze Cheng 已提交
71
int tdbClose(TDB *pDb) {
H
Hongze Cheng 已提交
72 73
  SPager *pPager;

H
Hongze Cheng 已提交
74
  if (pDb) {
H
Hongze Cheng 已提交
75 76 77 78 79 80 81
    for (pPager = pDb->pgrList; pPager; pPager = pDb->pgrList) {
      pDb->pgrList = pPager->pNext;
      tdbPagerClose(pPager);
    }

    tdbPCacheClose(pDb->pCache);
    tdbOsFree(pDb->pgrHash);
H
Hongze Cheng 已提交
82 83
    tdbOsFree(pDb);
  }
H
Hongze Cheng 已提交
84

H
Hongze Cheng 已提交
85 86 87
  return 0;
}

H
Hongze Cheng 已提交
88 89 90
int tdbBegin(TDB *pDb, TXN *pTxn) {
  SPager *pPager;
  int     ret;
H
Hongze Cheng 已提交
91

H
Hongze Cheng 已提交
92 93 94 95 96 97 98
  for (pPager = pDb->pgrList; pPager; pPager = pPager->pNext) {
    ret = tdbPagerBegin(pPager, pTxn);
    if (ret < 0) {
      ASSERT(0);
      return -1;
    }
  }
H
Hongze Cheng 已提交
99

H
Hongze Cheng 已提交
100
  return 0;
101 102
}

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

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

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

H
Hongze Cheng 已提交
118 119 120
SPager *tdbEnvGetPager(TDB *pDb, const char *fname) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
121

H
Hongze Cheng 已提交
122 123
  hash = tdbCstringHash(fname);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
124
  tdbTrace("tdbttl getPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
125 126
  for (; *ppPager && (strcmp(fname, (*ppPager)->dbFileName) != 0); ppPager = &((*ppPager)->pHashNext)) {
  }
127
  tdbTrace("tdbttl getPager2: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, fname);
H
Hongze Cheng 已提交
128 129
  return *ppPager;
}
H
Hongze Cheng 已提交
130

H
Hongze Cheng 已提交
131 132 133
void tdbEnvAddPager(TDB *pDb, SPager *pPager) {
  u32      hash;
  SPager **ppPager;
H
Hongze Cheng 已提交
134

H
Hongze Cheng 已提交
135 136 137 138
  // rehash if neccessary
  if (pDb->nPager + 1 > pDb->nPgrHash) {
    // TODO
  }
H
Hongze Cheng 已提交
139

H
Hongze Cheng 已提交
140 141 142
  // add to list
  pPager->pNext = pDb->pgrList;
  pDb->pgrList = pPager;
H
Hongze Cheng 已提交
143

H
Hongze Cheng 已提交
144 145 146
  // add to hash
  hash = tdbCstringHash(pPager->dbFileName);
  ppPager = &pDb->pgrHash[hash % pDb->nPgrHash];
147
  tdbTrace("tdbttl addPager1: pager:%p, index:%d, name:%s", *ppPager, hash % pDb->nPgrHash, pPager->dbFileName);
H
Hongze Cheng 已提交
148 149
  pPager->pHashNext = *ppPager;
  *ppPager = pPager;
H
Hongze Cheng 已提交
150

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

H
Hongze Cheng 已提交
153 154
  // increase the counter
  pDb->nPager++;
H
Hongze Cheng 已提交
155 156
}

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

H
Hongze Cheng 已提交
161 162
  // remove from the list
  for (ppPager = &pDb->pgrList; *ppPager && (*ppPager != pPager); ppPager = &((*ppPager)->pNext)) {
H
Hongze Cheng 已提交
163
  }
H
Hongze Cheng 已提交
164 165
  ASSERT(*ppPager == pPager);
  *ppPager = pPager->pNext;
H
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167 168 169 170 171 172 173 174 175 176
  // 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 已提交
177

H
Hongze Cheng 已提交
178 179 180 181 182
  // rehash if necessary
  if (pDb->nPgrHash > 8 && pDb->nPager < pDb->nPgrHash / 2) {
    // TODO
  }
}