more TDB
This commit is contained in:
parent
698377b830
commit
8050546d7d
|
@ -29,29 +29,32 @@ typedef struct STDbCurosr TDBC;
|
||||||
typedef int32_t pgsz_t;
|
typedef int32_t pgsz_t;
|
||||||
typedef int32_t cachesz_t;
|
typedef int32_t cachesz_t;
|
||||||
|
|
||||||
|
typedef int (*TdbKeyCmprFn)(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2);
|
||||||
|
|
||||||
// TEVN
|
// TEVN
|
||||||
int tdbEnvCreate(TENV **ppEnv, const char *rootDir);
|
int tdbEnvCreate(TENV **ppEnv, const char *rootDir);
|
||||||
int tdbEnvOpen(TENV *ppEnv);
|
int tdbEnvOpen(TENV *ppEnv);
|
||||||
int tdbEnvClose(TENV *pEnv);
|
int tdbEnvClose(TENV *pEnv);
|
||||||
|
|
||||||
int tdbEnvBeginTxn(TENV *pEnv);
|
|
||||||
int tdbEnvCommit(TENV *pEnv);
|
|
||||||
|
|
||||||
int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize);
|
int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize);
|
||||||
pgsz_t tdbEnvGetPageSize(TENV *pEnv);
|
pgsz_t tdbEnvGetPageSize(TENV *pEnv);
|
||||||
cachesz_t tdbEnvGetCacheSize(TENV *pEnv);
|
cachesz_t tdbEnvGetCacheSize(TENV *pEnv);
|
||||||
|
|
||||||
|
int tdbEnvBeginTxn(TENV *pEnv);
|
||||||
|
int tdbEnvCommit(TENV *pEnv);
|
||||||
|
|
||||||
// TDB
|
// TDB
|
||||||
int tdbCreate(TDB **ppDb);
|
int tdbCreate(TDB **ppDb);
|
||||||
int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv);
|
int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv);
|
||||||
int tdbClose(TDB *pDb);
|
int tdbClose(TDB *pDb);
|
||||||
|
|
||||||
int tdbSetKeyLen(TDB *pDb, int klen);
|
int tdbSetKeyLen(TDB *pDb, int klen);
|
||||||
int tdbSetValLen(TDB *pDb, int vlen);
|
int tdbSetValLen(TDB *pDb, int vlen);
|
||||||
int tdbSetDup(TDB *pDb, int dup);
|
int tdbSetDup(TDB *pDb, int dup);
|
||||||
int tdbGetKeyLen(TDB *pDb, int *pklen);
|
int tdbSetCmprFunc(TDB *pDb, TdbKeyCmprFn fn);
|
||||||
int tdbGetValLen(TDB *pDb, int *pvlen);
|
int tdbGetKeyLen(TDB *pDb);
|
||||||
int tdbGetDup(TDB *pDb, int *pdup);
|
int tdbGetValLen(TDB *pDb);
|
||||||
|
int tdbGetDup(TDB *pDb);
|
||||||
|
|
||||||
int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData);
|
int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData);
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,14 @@
|
||||||
#include "tdbInt.h"
|
#include "tdbInt.h"
|
||||||
|
|
||||||
struct STDb {
|
struct STDb {
|
||||||
SBTree * pBt; // current access method
|
char * dbname; // dbname;
|
||||||
SPgFile *pPgFile; // backend page file this DB is using
|
SBTree * pBt; // current access method (may extend)
|
||||||
TENV * pEnv; // TENV containing the DB
|
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
|
||||||
};
|
};
|
||||||
|
|
||||||
struct STDbCurosr {
|
struct STDbCurosr {
|
||||||
|
@ -28,13 +33,18 @@ struct STDbCurosr {
|
||||||
int tdbCreate(TDB **ppDb) {
|
int tdbCreate(TDB **ppDb) {
|
||||||
TDB *pDb;
|
TDB *pDb;
|
||||||
|
|
||||||
|
// create the handle
|
||||||
pDb = (TDB *)calloc(1, sizeof(*pDb));
|
pDb = (TDB *)calloc(1, sizeof(*pDb));
|
||||||
if (pDb == NULL) {
|
if (pDb == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO */
|
pDb->klen = TDB_VARIANT_LEN;
|
||||||
|
pDb->vlen = TDB_VARIANT_LEN;
|
||||||
|
pDb->dup = false;
|
||||||
|
pDb->cFn = NULL /*TODO*/;
|
||||||
|
|
||||||
|
*ppDb = pDb;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,22 +55,14 @@ static int tdbDestroy(TDB *pDb) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) {
|
int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
|
||||||
TDB * pDb;
|
|
||||||
int ret;
|
int ret;
|
||||||
uint8_t fileid[TDB_FILE_ID_LEN];
|
uint8_t fileid[TDB_FILE_ID_LEN];
|
||||||
SPgFile * pPgFile;
|
SPgFile * pPgFile;
|
||||||
SPgCache *pPgCache;
|
SPgCache *pPgCache;
|
||||||
SBTree * pBt;
|
SBTree * pBt;
|
||||||
|
|
||||||
// Create DB if DB handle is not created yet
|
ASSERT(pDb != NULL);
|
||||||
if (ppDb == NULL) {
|
|
||||||
if ((ret = tdbCreate(ppDb)) != 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pDb = *ppDb;
|
|
||||||
|
|
||||||
// Create a default ENV if pEnv is not set
|
// Create a default ENV if pEnv is not set
|
||||||
if (pEnv == NULL) {
|
if (pEnv == NULL) {
|
||||||
|
@ -107,4 +109,46 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) {
|
||||||
int tdbClose(TDB *pDb) {
|
int tdbClose(TDB *pDb) {
|
||||||
if (pDb == NULL) return 0;
|
if (pDb == NULL) return 0;
|
||||||
return tdbDestroy(pDb);
|
return tdbDestroy(pDb);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -96,7 +96,7 @@ typedef TD_DLIST(SPgFile) SPgFileList;
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define TDB_VARIANT_LEN (int32_t) - 1
|
#define TDB_VARIANT_LEN (int)-1
|
||||||
|
|
||||||
// page payload format
|
// page payload format
|
||||||
// <keyLen> + <valLen> + [key] + [value]
|
// <keyLen> + <valLen> + [key] + [value]
|
||||||
|
|
|
@ -19,12 +19,19 @@ TEST(tdb_test, simple_test) {
|
||||||
|
|
||||||
GTEST_ASSERT_EQ(tdbEnvOpen(pEnv), 0);
|
GTEST_ASSERT_EQ(tdbEnvOpen(pEnv), 0);
|
||||||
|
|
||||||
#if 0
|
#if 1
|
||||||
// DB
|
// DB
|
||||||
tdbOpen(&pDb1, "db.db", "db1", pEnv);
|
GTEST_ASSERT_EQ(tdbCreate(&pDb1), 0);
|
||||||
tdbOpen(&pDb2, "db.db", "db2", pEnv);
|
|
||||||
tdbOpen(&pDb3, "index.db", NULL, pEnv);
|
|
||||||
|
|
||||||
|
GTEST_ASSERT_EQ(tdbSetKeyLen(pDb1, 8), 0);
|
||||||
|
|
||||||
|
// GTEST_ASSERT_EQ(tdbSetValLen(pDb1, 3), 0);
|
||||||
|
|
||||||
|
// GTEST_ASSERT_EQ(tdbSetDup(pDb1, 3), 0);
|
||||||
|
|
||||||
|
GTEST_ASSERT_EQ(tdbOpen(pDb1, "db.db", "db1", pEnv), 0);
|
||||||
|
|
||||||
|
#if 0
|
||||||
// Insert
|
// Insert
|
||||||
|
|
||||||
// Query
|
// Query
|
||||||
|
@ -32,10 +39,13 @@ TEST(tdb_test, simple_test) {
|
||||||
// Delete
|
// Delete
|
||||||
|
|
||||||
// Query
|
// Query
|
||||||
|
#endif
|
||||||
|
|
||||||
// Close
|
// GTEST_ASSERT_EQ(tdbOpen(&pDb2, "db.db", "db2", pEnv), 0);
|
||||||
|
// GTEST_ASSERT_EQ(tdbOpen(&pDb3, "index.db", NULL, pEnv), 0);
|
||||||
|
// tdbClose(pDb3);
|
||||||
|
// tdbClose(pDb2);
|
||||||
tdbClose(pDb1);
|
tdbClose(pDb1);
|
||||||
tdbClose(pDb2);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
tdbEnvClose(pEnv);
|
tdbEnvClose(pEnv);
|
||||||
|
|
Loading…
Reference in New Issue