tdb.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
struct STDb {
H
Hongze Cheng 已提交
19
  char         dbname[TDB_MAX_DBNAME_LEN];
H
Hongze Cheng 已提交
20 21 22 23 24 25 26
  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 已提交
27
};
H
Hongze Cheng 已提交
28

H
Hongze Cheng 已提交
29 30 31 32
struct STDbCurosr {
  SBtCursor *pBtCur;
};

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

H
Hongze Cheng 已提交
35 36 37
int tdbCreate(TDB **ppDb) {
  TDB *pDb;

H
Hongze Cheng 已提交
38
  // create the handle
H
Hongze Cheng 已提交
39 40 41 42 43
  pDb = (TDB *)calloc(1, sizeof(*pDb));
  if (pDb == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
44 45 46
  pDb->klen = TDB_VARIANT_LEN;
  pDb->vlen = TDB_VARIANT_LEN;
  pDb->dup = false;
H
Hongze Cheng 已提交
47
  pDb->cFn = tdbDefaultKeyCmprFn;
H
Hongze Cheng 已提交
48

H
Hongze Cheng 已提交
49
  *ppDb = pDb;
H
Hongze Cheng 已提交
50 51 52
  return 0;
}

H
Hongze Cheng 已提交
53
static int tdbDestroy(TDB *pDb) {
H
Hongze Cheng 已提交
54 55 56 57 58 59
  if (pDb) {
    free(pDb);
  }
  return 0;
}

H
Hongze Cheng 已提交
60
int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
H
Hongze Cheng 已提交
61 62 63 64
  int       ret;
  uint8_t   fileid[TDB_FILE_ID_LEN];
  SPgFile * pPgFile;
  SPgCache *pPgCache;
H
Hongze Cheng 已提交
65
  SBTree *  pBt;
H
Hongze Cheng 已提交
66 67
  bool      fileExist;
  size_t    dbNameLen;
H
Hongze Cheng 已提交
68
  pgno_t    dbRootPgno;
H
Hongze Cheng 已提交
69
  char      dbfname[128];  // TODO: make this as a macro or malloc on the heap
H
Hongze Cheng 已提交
70

H
Hongze Cheng 已提交
71
  ASSERT(pDb != NULL);
H
Hongze Cheng 已提交
72 73 74 75 76 77 78 79 80 81 82 83
  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 已提交
84

H
Hongze Cheng 已提交
85 86 87 88 89
    memcpy(pDb->dbname, dbname, dbNameLen);
  }

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

H
Hongze Cheng 已提交
90
  // get page file from the env, if not opened yet, open it
H
Hongze Cheng 已提交
91
  pPgFile = NULL;
H
Hongze Cheng 已提交
92 93 94
  snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname);
  fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0);
  if (fileExist) {
H
Hongze Cheng 已提交
95 96 97 98 99
    tdbGnrtFileID(dbfname, fileid, false);
    pPgFile = tdbEnvGetPageFile(pEnv, fileid);
  }

  if (pPgFile == NULL) {
H
Hongze Cheng 已提交
100 101 102 103 104
    ret = pgFileOpen(&pPgFile, dbfname, pEnv);
    if (ret != 0) {
      // TODO: handle error
      return -1;
    }
H
Hongze Cheng 已提交
105 106
  }

H
Hongze Cheng 已提交
107
  // TODO: open the database (an existing or a new one)
H
Hongze Cheng 已提交
108 109 110 111 112 113 114 115 116 117 118
  if (0) {
    // Search the page file master DB to check if the db exists
    // If exists, run this branch (TODO)
  } else {
    ret = pgFileAllocatePage(pPgFile, &dbRootPgno);
    if (ret != 0) {
      // TODO: handle error
    }
  }

  pDb->pBt->root = dbRootPgno;
H
Hongze Cheng 已提交
119

H
Hongze Cheng 已提交
120
  // register
H
Hongze Cheng 已提交
121
  pDb->pPgFile = pPgFile;
H
Hongze Cheng 已提交
122 123
  tdbEnvRgstDB(pEnv, pDb);
  pDb->pEnv = pEnv;
H
Hongze Cheng 已提交
124

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

int tdbClose(TDB *pDb) {
H
Hongze Cheng 已提交
129 130
  if (pDb == NULL) return 0;
  return tdbDestroy(pDb);
H
Hongze Cheng 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

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 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

static int tdbDefaultKeyCmprFn(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2) {
  int mlen;
  int cret;

  ASSERT(keyLen1 > 0 && keyLen2 > 0 && pKey1 != NULL && pKey2 != NULL);

  mlen = keyLen1 < keyLen2 ? keyLen1 : keyLen2;
  cret = memcmp(pKey1, pKey2, mlen);
  if (cret == 0) {
    if (keyLen1 < keyLen2) {
      cret = -1;
    } else if (keyLen1 > keyLen2) {
      cret = 1;
    } else {
      cret = 0;
    }
  }
  return cret;
H
Hongze Cheng 已提交
193
}