tdb.c 3.7 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 20 21 22 23 24 25 26
  char *       dbname;   // dbname;
  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

H
Hongze Cheng 已提交
67
  ASSERT(pDb != NULL);
H
Hongze Cheng 已提交
68 69 70

  // Create a default ENV if pEnv is not set
  if (pEnv == NULL) {
H
Hongze Cheng 已提交
71 72 73
    // if ((ret = tdbEnvOpen(&pEnv)) != 0) {
    //   return -1;
    // }
H
Hongze Cheng 已提交
74 75
  }

H
Hongze Cheng 已提交
76 77 78 79 80 81
  pDb->pEnv = pEnv;

  // register DB to ENV

  ASSERT(fname != NULL);

H
Hongze Cheng 已提交
82 83 84 85 86 87
  // Check if file exists
  if (tdbCheckFileAccess(fname, TDB_F_OK) != 0) {
    if (1) {
      // create the file
    }
  }
H
Hongze Cheng 已提交
88 89

  // Check if the SPgFile already opened
H
Hongze Cheng 已提交
90
  tdbGnrtFileID(fname, fileid, false);
H
Hongze Cheng 已提交
91 92 93 94 95 96 97 98 99 100 101
  pPgFile = tdbEnvGetPageFile(pEnv, fileid);
  if (pPgFile == NULL) {
    pPgCache = tdbEnvGetPgCache(pEnv);
    if ((ret = pgFileOpen(&pPgFile, fname, pPgCache)) != 0) {
      return -1;
    }
  }

  pDb->pPgFile = pPgFile;

  // open the access method (TODO)
H
Hongze Cheng 已提交
102 103 104 105 106
  if (btreeOpen(&pBt, pPgFile) != 0) {
    return -1;
  }

  pDb->pBt = pBt;
H
Hongze Cheng 已提交
107

H
Hongze Cheng 已提交
108 109 110 111
  return 0;
}

int tdbClose(TDB *pDb) {
H
Hongze Cheng 已提交
112 113
  if (pDb == NULL) return 0;
  return tdbDestroy(pDb);
H
Hongze Cheng 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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
}

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 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
}

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 已提交
176
}