tdbDb.c 5.4 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
struct STDb {
H
more  
Hongze Cheng 已提交
19 20
  STEnv * pEnv;
  SBTree *pBt;
H
Hongze Cheng 已提交
21 22
};

H
more  
Hongze Cheng 已提交
23 24 25
int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprFn, STEnv *pEnv, STDb **ppDb) {
  STDb *  pDb;
  SPFile *pFile;
H
more  
Hongze Cheng 已提交
26
  int     ret;
H
Hongze Cheng 已提交
27
  char    fFullName[TDB_FILENAME_LEN];
H
Hongze Cheng 已提交
28
  SPage * pPage;
H
Hongze Cheng 已提交
29
  SPgno   pgno;
H
Hongze Cheng 已提交
30 31

  *ppDb = NULL;
H
more  
Hongze Cheng 已提交
32 33 34 35 36 37

  pDb = (STDb *)calloc(1, sizeof(*pDb));
  if (pDb == NULL) {
    return -1;
  }

H
more  
Hongze Cheng 已提交
38
  // pDb->pEnv
H
more  
Hongze Cheng 已提交
39 40
  pDb->pEnv = pEnv;

H
Hongze Cheng 已提交
41 42 43 44 45 46 47 48 49
  pFile = tdbEnvGetPFile(pEnv, fname);
  if (pFile == NULL) {
    snprintf(fFullName, TDB_FILENAME_LEN, "%s/%s", pEnv->rootDir, fname);
    ret = tdbPFileOpen(pEnv->pCache, fFullName, &pFile);
    if (ret < 0) {
      return -1;
    }
  }

H
Hongze Cheng 已提交
50 51 52 53
  ASSERT(pFile != NULL);

  // TODO: Search if the DB already in the file
  if (1 /*todo: db should be created*/) {
H
Hongze Cheng 已提交
54 55 56 57 58 59
    ret = tdbPFileAllocPage(pFile, &pPage, &pgno);
    if (ret < 0) {
      return -1;
    }

    ret = tdbPFileWrite(pFile, pPage);
H
Hongze Cheng 已提交
60 61 62
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
63 64
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
65 66
  }

H
more  
Hongze Cheng 已提交
67
  // pDb->pBt
H
Hongze Cheng 已提交
68
  ret = tdbBtreeOpen(pgno, keyLen, valLen, pFile, keyCmprFn, &(pDb->pBt));
H
more  
Hongze Cheng 已提交
69 70 71 72
  if (ret < 0) {
    return -1;
  }

H
more  
Hongze Cheng 已提交
73
  *ppDb = pDb;
H
Hongze Cheng 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
  return 0;
}

int tdbDbClose(STDb *pDb) {
  // TODO
  return 0;
}

int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
87
#if 0
H
Hongze Cheng 已提交
88
struct STDb {
H
Hongze Cheng 已提交
89
  char         dbname[TDB_MAX_DBNAME_LEN];
H
Hongze Cheng 已提交
90 91 92 93 94 95 96
  SBTree *     pBt;      // current access method (may extend)
  SPgFile *    pPgFile;  // backend page file this DB is using
  TENV *       pEnv;     // TENV containing the DB
  int          klen;     // key length if know
  int          vlen;     // value length if know
  bool         dup;      // dup mode
  TdbKeyCmprFn cFn;      // compare function
H
Hongze Cheng 已提交
97
};
H
Hongze Cheng 已提交
98

H
Hongze Cheng 已提交
99 100 101 102
struct STDbCurosr {
  SBtCursor *pBtCur;
};

H
Hongze Cheng 已提交
103 104
static int tdbDefaultKeyCmprFn(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2);

H
Hongze Cheng 已提交
105 106 107
int tdbCreate(TDB **ppDb) {
  TDB *pDb;

H
Hongze Cheng 已提交
108
  // create the handle
H
Hongze Cheng 已提交
109 110 111 112 113
  pDb = (TDB *)calloc(1, sizeof(*pDb));
  if (pDb == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
114 115 116
  pDb->klen = TDB_VARIANT_LEN;
  pDb->vlen = TDB_VARIANT_LEN;
  pDb->dup = false;
H
Hongze Cheng 已提交
117
  pDb->cFn = tdbDefaultKeyCmprFn;
H
Hongze Cheng 已提交
118

H
Hongze Cheng 已提交
119
  *ppDb = pDb;
H
Hongze Cheng 已提交
120 121 122
  return 0;
}

H
Hongze Cheng 已提交
123
static int tdbDestroy(TDB *pDb) {
H
Hongze Cheng 已提交
124 125 126 127 128 129
  if (pDb) {
    free(pDb);
  }
  return 0;
}

H
Hongze Cheng 已提交
130
int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
H
Hongze Cheng 已提交
131 132 133 134
  int       ret;
  uint8_t   fileid[TDB_FILE_ID_LEN];
  SPgFile * pPgFile;
  SPgCache *pPgCache;
H
Hongze Cheng 已提交
135
  SBTree *  pBt;
H
Hongze Cheng 已提交
136 137
  bool      fileExist;
  size_t    dbNameLen;
H
more  
Hongze Cheng 已提交
138
  SPgno    dbRootPgno;
H
Hongze Cheng 已提交
139
  char      dbfname[128];  // TODO: make this as a macro or malloc on the heap
H
Hongze Cheng 已提交
140

H
Hongze Cheng 已提交
141
  ASSERT(pDb != NULL);
H
Hongze Cheng 已提交
142 143 144 145 146 147 148 149 150 151 152 153
  ASSERT(fname != NULL);
  // TODO: Here we simply put an assert here. In the future, make `pEnv`
  // can be set as NULL.
  ASSERT(pEnv != NULL);

  // check the DB name
  dbNameLen = 0;
  if (dbname) {
    dbNameLen = strlen(dbname);
    if (dbNameLen >= TDB_MAX_DBNAME_LEN) {
      return -1;
    }
H
Hongze Cheng 已提交
154

H
Hongze Cheng 已提交
155 156 157 158 159
    memcpy(pDb->dbname, dbname, dbNameLen);
  }

  pDb->dbname[dbNameLen] = '\0';

H
Hongze Cheng 已提交
160
  // get page file from the env, if not opened yet, open it
H
Hongze Cheng 已提交
161
  pPgFile = NULL;
H
Hongze Cheng 已提交
162 163 164
  snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname);
  fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0);
  if (fileExist) {
H
Hongze Cheng 已提交
165 166 167 168 169
    tdbGnrtFileID(dbfname, fileid, false);
    pPgFile = tdbEnvGetPageFile(pEnv, fileid);
  }

  if (pPgFile == NULL) {
H
Hongze Cheng 已提交
170 171 172 173 174
    ret = pgFileOpen(&pPgFile, dbfname, pEnv);
    if (ret != 0) {
      // TODO: handle error
      return -1;
    }
H
Hongze Cheng 已提交
175 176
  }

H
Hongze Cheng 已提交
177 178 179 180
  // TODO: get the root page number from the master DB of the page file
  // tdbGet(&dbRootPgno);
  if (dbRootPgno == 0) {
    // DB not exist, create one
H
Hongze Cheng 已提交
181 182 183
    ret = pgFileAllocatePage(pPgFile, &dbRootPgno);
    if (ret != 0) {
      // TODO: handle error
H
Hongze Cheng 已提交
184
    
H
Hongze Cheng 已提交
185
    // tdbInsert(pPgFile->pMasterDB, dbname, strlen(dbname), &dbRootPgno, sizeof(dbRootPgno));
H
Hongze Cheng 已提交
186 187
  }

H
Hongze Cheng 已提交
188 189
  ASSERT(dbRootPgno > 1);

H
Hongze Cheng 已提交
190
  // pDb->pBt->root = dbRootPgno;
H
Hongze Cheng 已提交
191

H
Hongze Cheng 已提交
192
  // register
H
Hongze Cheng 已提交
193
  pDb->pPgFile = pPgFile;
H
Hongze Cheng 已提交
194 195
  tdbEnvRgstDB(pEnv, pDb);
  pDb->pEnv = pEnv;
H
Hongze Cheng 已提交
196

H
Hongze Cheng 已提交
197 198 199 200
  return 0;
}

int tdbClose(TDB *pDb) {
H
Hongze Cheng 已提交
201 202
  if (pDb == NULL) return 0;
  return tdbDestroy(pDb);
H
Hongze Cheng 已提交
203 204
}

H
more  
Hongze Cheng 已提交
205 206 207 208 209
int tdbDrop(TDB *pDb) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
int tdbSetKeyLen(TDB *pDb, int klen) {
  // TODO: check `klen`
  pDb->klen = klen;
  return 0;
}

int tdbSetValLen(TDB *pDb, int vlen) {
  // TODO: check `vlen`
  pDb->vlen = vlen;
  return 0;
}

int tdbSetDup(TDB *pDb, int dup) {
  if (dup) {
    pDb->dup = true;
  } else {
    pDb->dup = false;
  }
  return 0;
}

int tdbSetCmprFunc(TDB *pDb, TdbKeyCmprFn fn) {
  if (fn == NULL) {
    return -1;
  } else {
    pDb->cFn = fn;
  }
  return 0;
}

int tdbGetKeyLen(TDB *pDb) { return pDb->klen; }

int tdbGetValLen(TDB *pDb) { return pDb->vlen; }

int tdbGetDup(TDB *pDb) {
  if (pDb->dup) {
    return 1;
  } else {
    return 0;
  }
H
Hongze Cheng 已提交
250 251
}

H
Hongze Cheng 已提交
252 253 254 255 256
int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
257
#endif