Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
77957065
T
TDengine
项目概览
taosdata
/
TDengine
12 个月 前同步成功
通知
1180
Star
22014
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
77957065
编写于
3月 28, 2022
作者:
H
Hongze Cheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refact and more
上级
db2c31cf
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
154 addition
and
58 deletion
+154
-58
source/libs/tdb/CMakeLists.txt
source/libs/tdb/CMakeLists.txt
+1
-0
source/libs/tdb/src/db/tdbBtree.c
source/libs/tdb/src/db/tdbBtree.c
+56
-56
source/libs/tdb/src/db/tdbTxn.c
source/libs/tdb/src/db/tdbTxn.c
+31
-0
source/libs/tdb/src/inc/tdbInt.h
source/libs/tdb/src/inc/tdbInt.h
+2
-0
source/libs/tdb/src/inc/tdbTxn.h
source/libs/tdb/src/inc/tdbTxn.h
+39
-0
source/libs/tdb/test/tdbTest.cpp
source/libs/tdb/test/tdbTest.cpp
+25
-2
未找到文件。
source/libs/tdb/CMakeLists.txt
浏览文件 @
77957065
...
...
@@ -8,6 +8,7 @@ target_sources(tdb
"src/db/tdbBtree.c"
"src/db/tdbDb.c"
"src/db/tdbEnv.c"
"src/db/tdbTxn.c"
"src/page/tdbPage.c"
"src/page/tdbPageL.c"
)
...
...
source/libs/tdb/src/db/tdbBtree.c
浏览文件 @
77957065
...
...
@@ -67,7 +67,7 @@ typedef struct {
u8
*
pTmpSpace
;
}
SCellDecoder
;
static
int
tdbBtCursorMoveTo
(
SBTC
*
p
Cur
,
const
void
*
pKey
,
int
kLen
,
int
*
pCRst
);
static
int
tdbBtCursorMoveTo
(
SBTC
*
p
Btc
,
const
void
*
pKey
,
int
kLen
,
int
*
pCRst
);
static
int
tdbDefaultKeyCmprFn
(
const
void
*
pKey1
,
int
keyLen1
,
const
void
*
pKey2
,
int
keyLen2
);
static
int
tdbBtreeOpenImpl
(
SBTree
*
pBt
);
static
int
tdbBtreeZeroPage
(
SPage
*
pPage
,
void
*
arg
);
...
...
@@ -75,10 +75,10 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg);
static
int
tdbBtreeEncodeCell
(
SPage
*
pPage
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
,
SCell
*
pCell
,
int
*
szCell
);
static
int
tdbBtreeDecodeCell
(
SPage
*
pPage
,
const
SCell
*
pCell
,
SCellDecoder
*
pDecoder
);
static
int
tdbBtreeBalance
(
SBTC
*
p
Cur
);
static
int
tdbBtreeBalance
(
SBTC
*
p
Btc
);
static
int
tdbBtreeCellSize
(
const
SPage
*
pPage
,
SCell
*
pCell
);
static
int
tdbBtcMoveToNext
(
SBTC
*
pBtc
);
static
int
tdbBtcMoveDownward
(
SBTC
*
p
Cur
,
SPgno
pgno
);
static
int
tdbBtcMoveDownward
(
SBTC
*
p
Btc
,
SPgno
pgno
);
static
int
tdbBtcMoveUpward
(
SBTC
*
pBtc
);
int
tdbBtreeOpen
(
int
keyLen
,
int
valLen
,
SPager
*
pPager
,
FKeyComparator
kcmpr
,
SBTree
**
ppBt
)
{
...
...
@@ -134,7 +134,7 @@ int tdbBtreeClose(SBTree *pBt) {
return
0
;
}
int
tdbBtCursorInsert
(
SBTC
*
p
Cur
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
)
{
int
tdbBtCursorInsert
(
SBTC
*
p
Btc
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
)
{
int
ret
;
int
idx
;
SPager
*
pPager
;
...
...
@@ -143,20 +143,20 @@ int tdbBtCursorInsert(SBTC *pCur, const void *pKey, int kLen, const void *pVal,
int
cret
;
SBTree
*
pBt
;
ret
=
tdbBtCursorMoveTo
(
p
Cur
,
pKey
,
kLen
,
&
cret
);
ret
=
tdbBtCursorMoveTo
(
p
Btc
,
pKey
,
kLen
,
&
cret
);
if
(
ret
<
0
)
{
// TODO: handle error
return
-
1
;
}
if
(
p
Cur
->
idx
==
-
1
)
{
ASSERT
(
TDB_PAGE_TOTAL_CELLS
(
p
Cur
->
pPage
)
==
0
);
if
(
p
Btc
->
idx
==
-
1
)
{
ASSERT
(
TDB_PAGE_TOTAL_CELLS
(
p
Btc
->
pPage
)
==
0
);
idx
=
0
;
}
else
{
if
(
cret
>
0
)
{
idx
=
p
Cur
->
idx
+
1
;
idx
=
p
Btc
->
idx
+
1
;
}
else
if
(
cret
<
0
)
{
idx
=
p
Cur
->
idx
;
idx
=
p
Btc
->
idx
;
}
else
{
/* TODO */
ASSERT
(
0
);
...
...
@@ -164,7 +164,7 @@ int tdbBtCursorInsert(SBTC *pCur, const void *pKey, int kLen, const void *pVal,
}
// TODO: refact code here
pBt
=
p
Cur
->
pBt
;
pBt
=
p
Btc
->
pBt
;
if
(
!
pBt
->
pTmp
)
{
pBt
->
pTmp
=
(
u8
*
)
malloc
(
pBt
->
pageSize
);
if
(
pBt
->
pTmp
==
NULL
)
{
...
...
@@ -175,20 +175,20 @@ int tdbBtCursorInsert(SBTC *pCur, const void *pKey, int kLen, const void *pVal,
pCell
=
pBt
->
pTmp
;
// Encode the cell
ret
=
tdbBtreeEncodeCell
(
p
Cur
->
pPage
,
pKey
,
kLen
,
pVal
,
vLen
,
pCell
,
&
szCell
);
ret
=
tdbBtreeEncodeCell
(
p
Btc
->
pPage
,
pKey
,
kLen
,
pVal
,
vLen
,
pCell
,
&
szCell
);
if
(
ret
<
0
)
{
return
-
1
;
}
// Insert the cell to the index
ret
=
tdbPageInsertCell
(
p
Cur
->
pPage
,
idx
,
pCell
,
szCell
,
0
);
ret
=
tdbPageInsertCell
(
p
Btc
->
pPage
,
idx
,
pCell
,
szCell
,
0
);
if
(
ret
<
0
)
{
return
-
1
;
}
// If page is overflow, balance the tree
if
(
p
Cur
->
pPage
->
nOverflow
>
0
)
{
ret
=
tdbBtreeBalance
(
p
Cur
);
if
(
p
Btc
->
pPage
->
nOverflow
>
0
)
{
ret
=
tdbBtreeBalance
(
p
Btc
);
if
(
ret
<
0
)
{
return
-
1
;
}
...
...
@@ -226,30 +226,30 @@ int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen
return
0
;
}
static
int
tdbBtCursorMoveTo
(
SBTC
*
p
Cur
,
const
void
*
pKey
,
int
kLen
,
int
*
pCRst
)
{
static
int
tdbBtCursorMoveTo
(
SBTC
*
p
Btc
,
const
void
*
pKey
,
int
kLen
,
int
*
pCRst
)
{
int
ret
;
SBTree
*
pBt
;
SPager
*
pPager
;
pBt
=
p
Cur
->
pBt
;
pBt
=
p
Btc
->
pBt
;
pPager
=
pBt
->
pPager
;
if
(
p
Cur
->
iPage
<
0
)
{
ASSERT
(
p
Cur
->
iPage
==
-
1
);
ASSERT
(
p
Cur
->
idx
==
-
1
);
if
(
p
Btc
->
iPage
<
0
)
{
ASSERT
(
p
Btc
->
iPage
==
-
1
);
ASSERT
(
p
Btc
->
idx
==
-
1
);
// Move from the root
ret
=
tdbPagerFetchPage
(
pPager
,
pBt
->
root
,
&
(
p
Cur
->
pPage
),
tdbBtreeInitPage
,
pBt
);
ret
=
tdbPagerFetchPage
(
pPager
,
pBt
->
root
,
&
(
p
Btc
->
pPage
),
tdbBtreeInitPage
,
pBt
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
p
Cur
->
iPage
=
0
;
p
Btc
->
iPage
=
0
;
if
(
TDB_PAGE_TOTAL_CELLS
(
p
Cur
->
pPage
)
==
0
)
{
if
(
TDB_PAGE_TOTAL_CELLS
(
p
Btc
->
pPage
)
==
0
)
{
// Current page is empty
// ASSERT(TDB_FLAG_IS(TDB_PAGE_FLAGS(p
Cur
->pPage), TDB_BTREE_ROOT | TDB_BTREE_LEAF));
// ASSERT(TDB_FLAG_IS(TDB_PAGE_FLAGS(p
Btc
->pPage), TDB_BTREE_ROOT | TDB_BTREE_LEAF));
return
0
;
}
...
...
@@ -259,7 +259,7 @@ static int tdbBtCursorMoveTo(SBTC *pCur, const void *pKey, int kLen, int *pCRst)
SPage
*
pPage
;
SCellDecoder
cd
=
{
0
};
pPage
=
p
Cur
->
pPage
;
pPage
=
p
Btc
->
pPage
;
nCells
=
TDB_PAGE_TOTAL_CELLS
(
pPage
);
lidx
=
0
;
ridx
=
nCells
-
1
;
...
...
@@ -297,22 +297,22 @@ static int tdbBtCursorMoveTo(SBTC *pCur, const void *pKey, int kLen, int *pCRst)
u8
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pPage
);
u8
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
if
(
leaf
)
{
p
Cur
->
idx
=
midx
;
p
Btc
->
idx
=
midx
;
*
pCRst
=
c
;
break
;
}
else
{
if
(
c
<=
0
)
{
p
Cur
->
idx
=
midx
;
tdbBtcMoveDownward
(
p
Cur
,
cd
.
pgno
);
p
Btc
->
idx
=
midx
;
tdbBtcMoveDownward
(
p
Btc
,
cd
.
pgno
);
}
else
{
p
Cur
->
idx
=
midx
+
1
;
p
Btc
->
idx
=
midx
+
1
;
if
(
midx
==
nCells
-
1
)
{
/* Move to right-most child */
tdbBtcMoveDownward
(
p
Cur
,
((
SIntHdr
*
)
pCur
->
pPage
->
pData
)
->
pgno
);
tdbBtcMoveDownward
(
p
Btc
,
((
SIntHdr
*
)
pBtc
->
pPage
->
pData
)
->
pgno
);
}
else
{
pCell
=
tdbPageGetCell
(
pPage
,
p
Cur
->
idx
);
pCell
=
tdbPageGetCell
(
pPage
,
p
Btc
->
idx
);
tdbBtreeDecodeCell
(
pPage
,
pCell
,
&
cd
);
tdbBtcMoveDownward
(
p
Cur
,
cd
.
pgno
);
tdbBtcMoveDownward
(
p
Btc
,
cd
.
pgno
);
}
}
}
...
...
@@ -805,7 +805,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
return
0
;
}
static
int
tdbBtreeBalance
(
SBTC
*
p
Cur
)
{
static
int
tdbBtreeBalance
(
SBTC
*
p
Btc
)
{
int
iPage
;
SPage
*
pParent
;
SPage
*
pPage
;
...
...
@@ -816,8 +816,8 @@ static int tdbBtreeBalance(SBTC *pCur) {
// Main loop to balance the BTree
for
(;;)
{
iPage
=
p
Cur
->
iPage
;
pPage
=
p
Cur
->
pPage
;
iPage
=
p
Btc
->
iPage
;
pPage
=
p
Btc
->
pPage
;
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pPage
);
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
root
=
TDB_BTREE_PAGE_IS_ROOT
(
flags
);
...
...
@@ -833,27 +833,27 @@ static int tdbBtreeBalance(SBTC *pCur) {
// ignore the case of empty
if
(
pPage
->
nOverflow
==
0
)
break
;
ret
=
tdbBtreeBalanceDeeper
(
p
Cur
->
pBt
,
pPage
,
&
(
pCur
->
pgStack
[
1
]));
ret
=
tdbBtreeBalanceDeeper
(
p
Btc
->
pBt
,
pPage
,
&
(
pBtc
->
pgStack
[
1
]));
if
(
ret
<
0
)
{
return
-
1
;
}
p
Cur
->
idx
=
0
;
p
Cur
->
idxStack
[
0
]
=
0
;
p
Cur
->
pgStack
[
0
]
=
pCur
->
pPage
;
p
Cur
->
iPage
=
1
;
p
Cur
->
pPage
=
pCur
->
pgStack
[
1
];
p
Btc
->
idx
=
0
;
p
Btc
->
idxStack
[
0
]
=
0
;
p
Btc
->
pgStack
[
0
]
=
pBtc
->
pPage
;
p
Btc
->
iPage
=
1
;
p
Btc
->
pPage
=
pBtc
->
pgStack
[
1
];
}
else
{
// Generalized balance step
pParent
=
p
Cur
->
pgStack
[
iPage
-
1
];
pParent
=
p
Btc
->
pgStack
[
iPage
-
1
];
ret
=
tdbBtreeBalanceNonRoot
(
p
Cur
->
pBt
,
pParent
,
pCur
->
idxStack
[
pCur
->
iPage
-
1
]);
ret
=
tdbBtreeBalanceNonRoot
(
p
Btc
->
pBt
,
pParent
,
pBtc
->
idxStack
[
pBtc
->
iPage
-
1
]);
if
(
ret
<
0
)
{
return
-
1
;
}
p
Cur
->
iPage
--
;
p
Cur
->
pPage
=
pCur
->
pgStack
[
pCur
->
iPage
];
p
Btc
->
iPage
--
;
p
Btc
->
pPage
=
pBtc
->
pgStack
[
pBtc
->
iPage
];
}
}
...
...
@@ -1050,11 +1050,11 @@ static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell) {
#endif
int
tdbBtcOpen
(
SBTC
*
p
Cur
,
SBTree
*
pBt
)
{
p
Cur
->
pBt
=
pBt
;
p
Cur
->
iPage
=
-
1
;
p
Cur
->
pPage
=
NULL
;
p
Cur
->
idx
=
-
1
;
int
tdbBtcOpen
(
SBTC
*
p
Btc
,
SBTree
*
pBt
)
{
p
Btc
->
pBt
=
pBt
;
p
Btc
->
iPage
=
-
1
;
p
Btc
->
pPage
=
NULL
;
p
Btc
->
idx
=
-
1
;
return
0
;
}
...
...
@@ -1262,16 +1262,16 @@ int tdbBtcClose(SBTC *pBtc) {
return
0
;
}
static
int
tdbBtcMoveDownward
(
SBTC
*
p
Cur
,
SPgno
pgno
)
{
static
int
tdbBtcMoveDownward
(
SBTC
*
p
Btc
,
SPgno
pgno
)
{
int
ret
;
p
Cur
->
pgStack
[
pCur
->
iPage
]
=
pCur
->
pPage
;
p
Cur
->
idxStack
[
pCur
->
iPage
]
=
pCur
->
idx
;
p
Cur
->
iPage
++
;
p
Cur
->
pPage
=
NULL
;
p
Cur
->
idx
=
-
1
;
p
Btc
->
pgStack
[
pBtc
->
iPage
]
=
pBtc
->
pPage
;
p
Btc
->
idxStack
[
pBtc
->
iPage
]
=
pBtc
->
idx
;
p
Btc
->
iPage
++
;
p
Btc
->
pPage
=
NULL
;
p
Btc
->
idx
=
-
1
;
ret
=
tdbPagerFetchPage
(
p
Cur
->
pBt
->
pPager
,
pgno
,
&
pCur
->
pPage
,
tdbBtreeInitPage
,
pCur
->
pBt
);
ret
=
tdbPagerFetchPage
(
p
Btc
->
pBt
->
pPager
,
pgno
,
&
pBtc
->
pPage
,
tdbBtreeInitPage
,
pBtc
->
pBt
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
}
...
...
source/libs/tdb/src/db/tdbTxn.c
0 → 100644
浏览文件 @
77957065
/*
* 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/>.
*/
#include "tdbInt.h"
int
tdbTxnBegin
(
STEnv
*
pEnv
)
{
// TODO
return
0
;
}
int
tdbTxnCommit
(
STEnv
*
pEnv
)
{
// TODO
return
0
;
}
int
tdbTxnRollback
(
STEnv
*
pEnv
)
{
// TODO
return
0
;
}
\ No newline at end of file
source/libs/tdb/src/inc/tdbInt.h
浏览文件 @
77957065
...
...
@@ -162,6 +162,8 @@ typedef struct SPage SPage;
#include "tdbPage.h"
#include "tdbTxn.h"
#ifdef __cplusplus
}
#endif
...
...
source/libs/tdb/src/inc/tdbTxn.h
0 → 100644
浏览文件 @
77957065
/*
* 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/>.
*/
#ifndef _TDB_TXN_H_
#define _TDB_TXN_H_
#ifdef __cplusplus
extern
"C"
{
#endif
typedef
struct
STxn
STXN
;
struct
STxn
{
u64
txnId
;
void
*
(
*
xMalloc
)(
void
*
,
int
);
void
*
xArg
;
};
int
tdbTxnBegin
(
STEnv
*
pEnv
);
int
tdbTxnCommit
(
STEnv
*
pEnv
);
int
tdbTxnRollback
(
STEnv
*
pEnv
);
#ifdef __cplusplus
}
#endif
#endif
/*_TDB_TXN_H_*/
\ No newline at end of file
source/libs/tdb/test/tdbTest.cpp
浏览文件 @
77957065
...
...
@@ -39,6 +39,8 @@ static void closePool(SPoolMem *pPool) {
free
(
pPool
);
}
#define clearPool closePool
static
void
*
poolMalloc
(
void
*
arg
,
int
size
)
{
void
*
ptr
=
NULL
;
SPoolMem
*
pPool
=
(
SPoolMem
*
)
arg
;
...
...
@@ -116,7 +118,7 @@ TEST(tdb_test, simple_test) {
STEnv
*
pEnv
;
STDB
*
pDb
;
FKeyComparator
compFunc
;
int
nData
=
1000000
0
;
int
nData
=
1000000
;
// Open Env
ret
=
tdbEnvOpen
(
"tdb"
,
4096
,
256000
,
&
pEnv
);
...
...
@@ -132,13 +134,34 @@ TEST(tdb_test, simple_test) {
char
val
[
64
];
{
// Insert some data
int
i
=
1
;
SPoolMem
*
pPool
;
int
memPoolCapacity
=
16
*
1024
;
pPool
=
openPool
();
tdbTxnBegin
(
pEnv
);
for
(;;)
{
if
(
i
>
nData
)
break
;
for
(
int
i
=
1
;
i
<=
nData
;
i
++
)
{
sprintf
(
key
,
"key%d"
,
i
);
sprintf
(
val
,
"value%d"
,
i
);
ret
=
tdbDbInsert
(
pDb
,
key
,
strlen
(
key
),
val
,
strlen
(
val
));
GTEST_ASSERT_EQ
(
ret
,
0
);
if
(
pPool
->
size
>=
memPoolCapacity
)
{
tdbTxnCommit
(
pEnv
);
clearPool
(
pPool
);
tdbTxnBegin
(
pEnv
);
}
i
++
;
}
closePool
(
pPool
);
}
{
// Query the data
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录