more TDB
This commit is contained in:
parent
cad1902337
commit
b8e346a3f5
|
@ -25,18 +25,29 @@ struct SBTree {
|
||||||
int (*FKeyComparator)(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2);
|
int (*FKeyComparator)(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct SPgHdr {
|
||||||
|
u8 flags;
|
||||||
|
u8 nFree;
|
||||||
|
u16 firstFree;
|
||||||
|
u16 nCells;
|
||||||
|
u16 pCell;
|
||||||
|
i32 kLen;
|
||||||
|
i32 vLen;
|
||||||
|
} SPgHdr;
|
||||||
|
|
||||||
|
typedef struct SBtPage {
|
||||||
|
SPgHdr *pHdr;
|
||||||
|
u16 * aCellIdx;
|
||||||
|
} SBtPage;
|
||||||
|
|
||||||
struct SBtCursor {
|
struct SBtCursor {
|
||||||
SBTree * pBt;
|
SBTree * pBt;
|
||||||
i8 iPage;
|
i8 iPage;
|
||||||
// SMemPage *pPage;
|
SBtPage *pPage;
|
||||||
// SMemPage *apPage[BTREE_MAX_DEPTH + 1];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// typedef struct SMemPage {
|
static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen);
|
||||||
// u8 isInit;
|
static int tdbEncodeLength(u8 *pBuf, uint len);
|
||||||
// u8 isLeaf;
|
|
||||||
// SPgno pgno;
|
|
||||||
// } SMemPage;
|
|
||||||
|
|
||||||
int tdbBtreeOpen(SPgno root, SBTree **ppBt) {
|
int tdbBtreeOpen(SPgno root, SBTree **ppBt) {
|
||||||
*ppBt = NULL;
|
*ppBt = NULL;
|
||||||
|
@ -56,21 +67,72 @@ int tdbBtreeCursor(SBTree *pBt, SBtCursor *pCur) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tdbBtreeCursorMoveTo(SBtCursor *pCur) {
|
int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen) {
|
||||||
/* TODO */
|
int ret;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int tdbBtreeCursorMoveToRoot(SBtCursor *pCur) {
|
|
||||||
SPFile *pFile;
|
SPFile *pFile;
|
||||||
SPage * pPage;
|
|
||||||
|
|
||||||
pFile = pCur->pBt->pFile;
|
ret = tdbBtCursorMoveTo(pCur, pKey, kLen);
|
||||||
|
if (ret < 0) {
|
||||||
pPage = tdbPFileGet(pFile, pCur->pBt->root);
|
// TODO: handle error
|
||||||
if (pPage == NULL) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pFile = pCur->pBt->pFile;
|
||||||
|
// ret = tdbPFileWrite(pFile, pCur->pPage);
|
||||||
|
// if (ret < 0) {
|
||||||
|
// // TODO: handle error
|
||||||
|
// return -1;
|
||||||
|
// }
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen) {
|
||||||
|
// TODO
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tdbEncodeKeyValue(const void *pKey, int kLen, int kLenG, const void *pVal, int vLen, int vLenG, void *pBuf,
|
||||||
|
int *bLen) {
|
||||||
|
u8 *pPtr;
|
||||||
|
|
||||||
|
ASSERT(kLen > 0 && vLen > 0);
|
||||||
|
ASSERT(kLenG == TDB_VARIANT_LEN || kLenG == kLen);
|
||||||
|
ASSERT(vLenG == TDB_VARIANT_LEN || vLenG == vLen);
|
||||||
|
|
||||||
|
pPtr = (u8 *)pBuf;
|
||||||
|
if (kLenG == TDB_VARIANT_LEN) {
|
||||||
|
pPtr += tdbEncodeLength(pPtr, kLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vLenG == TDB_VARIANT_LEN) {
|
||||||
|
pPtr += tdbEncodeLength(pPtr, vLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(pPtr, pKey, kLen);
|
||||||
|
pPtr += kLen;
|
||||||
|
|
||||||
|
memcpy(pPtr, pVal, vLen);
|
||||||
|
pPtr += vLen;
|
||||||
|
|
||||||
|
*bLen = pPtr - (u8 *)pBuf;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tdbDecodeKeyValue(const void *pBuf, void *pKey, int *kLen, void *pVal, int *vLen) {
|
||||||
|
// TODO
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tdbEncodeLength(u8 *pBuf, uint len) {
|
||||||
|
int iCount = 0;
|
||||||
|
|
||||||
|
while (len > 127) {
|
||||||
|
pBuf[iCount++] = (u8)((len & 0xff) | 128);
|
||||||
|
len >>= 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
pBuf[iCount++] = (u8)len;
|
||||||
|
|
||||||
|
return iCount;
|
||||||
}
|
}
|
|
@ -105,6 +105,18 @@ SPage *tdbPFileGet(SPFile *pFile, SPgno pgno) {
|
||||||
return pPage;
|
return pPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int tdbPFileWrite(SPFile *pFile, SPage *pPage) {
|
||||||
|
// TODO: if the page is not in journal, write to journal
|
||||||
|
// mark the page as dirty
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tdbPFileAllocPage(SPFile *pFile, SPage **ppPage) {
|
||||||
|
// TODO
|
||||||
|
*ppPage = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int tdbPFileBegin(SPFile *pFile) {
|
int tdbPFileBegin(SPFile *pFile) {
|
||||||
// TODO
|
// TODO
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -23,6 +23,8 @@ extern "C" {
|
||||||
typedef struct SBTree SBTree;
|
typedef struct SBTree SBTree;
|
||||||
typedef struct SBtCursor SBtCursor;
|
typedef struct SBtCursor SBtCursor;
|
||||||
|
|
||||||
|
int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -108,7 +108,7 @@ typedef TD_DLIST_NODE(SPgFile) SPgFileListNode;
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define TDB_VARIANT_LEN (int)-1
|
#define TDB_VARIANT_LEN ((int)-1)
|
||||||
|
|
||||||
// page payload format
|
// page payload format
|
||||||
// <keyLen> + <valLen> + [key] + [value]
|
// <keyLen> + <valLen> + [key] + [value]
|
||||||
|
|
|
@ -25,6 +25,8 @@ typedef struct SPFile SPFile;
|
||||||
int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile);
|
int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile);
|
||||||
int tdbPFileClose(SPFile *pFile);
|
int tdbPFileClose(SPFile *pFile);
|
||||||
SPage *tdbPFileGet(SPFile *pFile, SPgno pgno);
|
SPage *tdbPFileGet(SPFile *pFile, SPgno pgno);
|
||||||
|
int tdbPFileWrite(SPFile *pFile, SPage *pPage);
|
||||||
|
int tdbPFileAllocPage(SPFile *pFile, SPage **ppPage);
|
||||||
int tdbPFileBegin(SPFile *pFile);
|
int tdbPFileBegin(SPFile *pFile);
|
||||||
int tdbPFileCommit(SPFile *pFile);
|
int tdbPFileCommit(SPFile *pFile);
|
||||||
int tdbPFileRollback(SPFile *pFile);
|
int tdbPFileRollback(SPFile *pFile);
|
||||||
|
|
Loading…
Reference in New Issue