tdb.c 3.1 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 35
int tdbCreate(TDB **ppDb) {
  TDB *pDb;

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

H
Hongze Cheng 已提交
42 43 44 45
  pDb->klen = TDB_VARIANT_LEN;
  pDb->vlen = TDB_VARIANT_LEN;
  pDb->dup = false;
  pDb->cFn = NULL /*TODO*/;
H
Hongze Cheng 已提交
46

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

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

H
Hongze Cheng 已提交
58
int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
H
Hongze Cheng 已提交
59 60 61 62
  int       ret;
  uint8_t   fileid[TDB_FILE_ID_LEN];
  SPgFile * pPgFile;
  SPgCache *pPgCache;
H
Hongze Cheng 已提交
63
  SBTree *  pBt;
H
Hongze Cheng 已提交
64

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

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

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

  // register DB to ENV

  ASSERT(fname != NULL);

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

  // Check if the SPgFile already opened
H
Hongze Cheng 已提交
88
  tdbGnrtFileID(fname, fileid, false);
H
Hongze Cheng 已提交
89 90 91 92 93 94 95 96 97 98 99
  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 已提交
100 101 102 103 104
  if (btreeOpen(&pBt, pPgFile) != 0) {
    return -1;
  }

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

H
Hongze Cheng 已提交
106 107 108 109
  return 0;
}

int tdbClose(TDB *pDb) {
H
Hongze Cheng 已提交
110 111
  if (pDb == NULL) return 0;
  return tdbDestroy(pDb);
H
Hongze Cheng 已提交
112 113 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
}

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