Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
417703c9
T
TDengine
项目概览
taosdata
/
TDengine
接近 2 年 前同步成功
通知
1192
Star
22018
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看板
提交
417703c9
编写于
3月 28, 2022
作者:
H
Hongze Cheng
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'feature/tdb' of
https://github.com/taosdata/TDengine
into feature/meta
上级
40406b6b
b23d2c7b
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
324 addition
and
203 deletion
+324
-203
source/libs/CMakeLists.txt
source/libs/CMakeLists.txt
+1
-1
source/libs/tdb/CMakeLists.txt
source/libs/tdb/CMakeLists.txt
+1
-0
source/libs/tdb/inc/tdb.h
source/libs/tdb/inc/tdb.h
+0
-37
source/libs/tdb/src/db/tdbBtree.c
source/libs/tdb/src/db/tdbBtree.c
+7
-7
source/libs/tdb/src/db/tdbDb.c
source/libs/tdb/src/db/tdbDb.c
+3
-3
source/libs/tdb/src/db/tdbEnv.c
source/libs/tdb/src/db/tdbEnv.c
+1
-1
source/libs/tdb/src/db/tdbOs.c
source/libs/tdb/src/db/tdbOs.c
+98
-0
source/libs/tdb/src/db/tdbPCache.c
source/libs/tdb/src/db/tdbPCache.c
+17
-17
source/libs/tdb/src/db/tdbPager.c
source/libs/tdb/src/db/tdbPager.c
+15
-15
source/libs/tdb/src/db/tdbUtil.c
source/libs/tdb/src/db/tdbUtil.c
+0
-63
source/libs/tdb/src/inc/tdbInt.h
source/libs/tdb/src/inc/tdbInt.h
+4
-1
source/libs/tdb/src/inc/tdbOs.h
source/libs/tdb/src/inc/tdbOs.h
+129
-0
source/libs/tdb/src/inc/tdbPage.h
source/libs/tdb/src/inc/tdbPage.h
+19
-19
source/libs/tdb/src/inc/tdbUtil.h
source/libs/tdb/src/inc/tdbUtil.h
+21
-31
source/libs/tdb/src/page/tdbPage.c
source/libs/tdb/src/page/tdbPage.c
+3
-3
source/libs/tdb/test/tdbTest.cpp
source/libs/tdb/test/tdbTest.cpp
+5
-5
未找到文件。
source/libs/CMakeLists.txt
浏览文件 @
417703c9
add_subdirectory
(
transport
)
add_subdirectory
(
sync
)
#
add_subdirectory(tdb)
add_subdirectory
(
tdb
)
add_subdirectory
(
index
)
add_subdirectory
(
wal
)
add_subdirectory
(
parser
)
...
...
source/libs/tdb/CMakeLists.txt
浏览文件 @
417703c9
...
...
@@ -9,6 +9,7 @@ target_sources(tdb
"src/db/tdbDb.c"
"src/db/tdbEnv.c"
"src/db/tdbTxn.c"
"src/db/tdbOs.c"
"src/page/tdbPage.c"
"src/page/tdbPageL.c"
)
...
...
source/libs/tdb/inc/tdb.h
浏览文件 @
417703c9
...
...
@@ -22,43 +22,6 @@
extern
"C"
{
#endif
// typedef struct STDb TDB;
// typedef struct STDbEnv TENV;
// typedef struct STDbCurosr TDBC;
// typedef int32_t pgsz_t;
// typedef int32_t cachesz_t;
// typedef int (*TdbKeyCmprFn)(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2);
// // TEVN
// int tdbEnvCreate(TENV **ppEnv, const char *rootDir);
// int tdbEnvOpen(TENV *ppEnv);
// int tdbEnvClose(TENV *pEnv);
// int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize);
// pgsz_t tdbEnvGetPageSize(TENV *pEnv);
// cachesz_t tdbEnvGetCacheSize(TENV *pEnv);
// int tdbEnvBeginTxn(TENV *pEnv);
// int tdbEnvCommit(TENV *pEnv);
// // TDB
// int tdbCreate(TDB **ppDb);
// int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv);
// int tdbClose(TDB *pDb);
// int tdbDrop(TDB *pDb);
// int tdbSetKeyLen(TDB *pDb, int klen);
// int tdbSetValLen(TDB *pDb, int vlen);
// int tdbSetDup(TDB *pDb, int dup);
// int tdbSetCmprFunc(TDB *pDb, TdbKeyCmprFn fn);
// int tdbGetKeyLen(TDB *pDb);
// int tdbGetValLen(TDB *pDb);
// int tdbGetDup(TDB *pDb);
// int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData);
#ifdef __cplusplus
}
#endif
...
...
source/libs/tdb/src/db/tdbBtree.c
浏览文件 @
417703c9
...
...
@@ -87,7 +87,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, S
*
ppBt
=
NULL
;
pBt
=
(
SBTree
*
)
c
alloc
(
1
,
sizeof
(
*
pBt
));
pBt
=
(
SBTree
*
)
tdbOsC
alloc
(
1
,
sizeof
(
*
pBt
));
if
(
pBt
==
NULL
)
{
return
-
1
;
}
...
...
@@ -121,7 +121,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, S
// TODO: pBt->root
ret
=
tdbBtreeOpenImpl
(
pBt
);
if
(
ret
<
0
)
{
f
ree
(
pBt
);
tdbOsF
ree
(
pBt
);
return
-
1
;
}
...
...
@@ -166,7 +166,7 @@ int tdbBtCursorInsert(SBTC *pBtc, const void *pKey, int kLen, const void *pVal,
// TODO: refact code here
pBt
=
pBtc
->
pBt
;
if
(
!
pBt
->
pTmp
)
{
pBt
->
pTmp
=
(
u8
*
)
m
alloc
(
pBt
->
pageSize
);
pBt
->
pTmp
=
(
u8
*
)
tdbOsM
alloc
(
pBt
->
pageSize
);
if
(
pBt
->
pTmp
==
NULL
)
{
return
-
1
;
}
...
...
@@ -550,7 +550,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
if
(
sIdx
+
i
<
TDB_PAGE_TOTAL_CELLS
(
pParent
))
{
pCell
=
tdbPageGetCell
(
pParent
,
sIdx
+
i
);
szDivCell
[
i
]
=
tdbBtreeCellSize
(
pParent
,
pCell
);
pDivCell
[
i
]
=
m
alloc
(
szDivCell
[
i
]);
pDivCell
[
i
]
=
tdbOsM
alloc
(
szDivCell
[
i
]);
memcpy
(
pDivCell
[
i
],
pCell
,
szDivCell
[
i
]);
}
...
...
@@ -740,13 +740,13 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
tdbBtreeDecodeCell
(
pPage
,
pCell
,
&
cd
);
// TODO: pCell here may be inserted as an overflow cell, handle it
SCell
*
pNewCell
=
m
alloc
(
cd
.
kLen
+
9
);
SCell
*
pNewCell
=
tdbOsM
alloc
(
cd
.
kLen
+
9
);
int
szNewCell
;
SPgno
pgno
;
pgno
=
TDB_PAGE_PGNO
(
pNews
[
iNew
]);
tdbBtreeEncodeCell
(
pParent
,
cd
.
pKey
,
cd
.
kLen
,
(
void
*
)
&
pgno
,
sizeof
(
SPgno
),
pNewCell
,
&
szNewCell
);
tdbPageInsertCell
(
pParent
,
sIdx
++
,
pNewCell
,
szNewCell
,
0
);
f
ree
(
pNewCell
);
tdbOsF
ree
(
pNewCell
);
}
// move to next new page
...
...
@@ -798,7 +798,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
if
(
pDivCell
[
i
])
{
f
ree
(
pDivCell
[
i
]);
tdbOsF
ree
(
pDivCell
[
i
]);
}
}
...
...
source/libs/tdb/src/db/tdbDb.c
浏览文件 @
417703c9
...
...
@@ -34,7 +34,7 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF
*
ppDb
=
NULL
;
pDb
=
(
STDB
*
)
c
alloc
(
1
,
sizeof
(
*
pDb
));
pDb
=
(
STDB
*
)
tdbOsC
alloc
(
1
,
sizeof
(
*
pDb
));
if
(
pDb
==
NULL
)
{
return
-
1
;
}
...
...
@@ -101,7 +101,7 @@ int tdbDbcOpen(STDB *pDb, STDBC **ppDbc) {
STDBC
*
pDbc
=
NULL
;
*
ppDbc
=
NULL
;
pDbc
=
m
alloc
(
sizeof
(
*
pDbc
));
pDbc
=
(
STDBC
*
)
tdbOsM
alloc
(
sizeof
(
*
pDbc
));
if
(
pDbc
==
NULL
)
{
return
-
1
;
}
...
...
@@ -126,7 +126,7 @@ int tdbDbNext(STDBC *pDbc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
int
tdbDbcClose
(
STDBC
*
pDbc
)
{
if
(
pDbc
)
{
f
ree
(
pDbc
);
tdbOsF
ree
(
pDbc
);
}
return
0
;
...
...
source/libs/tdb/src/db/tdbEnv.c
浏览文件 @
417703c9
...
...
@@ -27,7 +27,7 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, STEnv **ppEnv)
dsize
=
strlen
(
rootDir
);
zsize
=
sizeof
(
*
pEnv
)
+
dsize
*
2
+
strlen
(
TDB_JOURNAL_NAME
)
+
3
;
pPtr
=
(
uint8_t
*
)
c
alloc
(
1
,
zsize
);
pPtr
=
(
uint8_t
*
)
tdbOsC
alloc
(
1
,
zsize
);
if
(
pPtr
==
NULL
)
{
return
-
1
;
}
...
...
source/libs/tdb/src/db/tdbOs.c
0 → 100644
浏览文件 @
417703c9
/*
* 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"
#ifndef TDB_FOR_TDENGINE
// tdbOsRead
i64
tdbOsRead
(
tdb_fd_t
fd
,
void
*
pData
,
i64
nBytes
)
{
i64
nRead
=
0
;
i64
iRead
=
0
;
u8
*
pBuf
=
(
u8
*
)
pData
;
while
(
nBytes
>
0
)
{
iRead
=
read
(
fd
,
pBuf
,
nBytes
);
if
(
iRead
<
0
)
{
if
(
errno
==
EINTR
)
{
continue
;
}
else
{
return
-
1
;
}
}
else
if
(
iRead
==
0
)
{
break
;
}
nRead
+=
iRead
;
pBuf
+=
iRead
;
nBytes
-=
iRead
;
}
return
nRead
;
}
// tdbOsPRead
i64
tdbOsPRead
(
tdb_fd_t
fd
,
void
*
pData
,
i64
nBytes
,
i64
offset
)
{
i64
nRead
=
0
;
i64
iRead
=
0
;
i64
iOffset
=
offset
;
u8
*
pBuf
=
(
u8
*
)
pData
;
while
(
nBytes
>
0
)
{
iRead
=
pread
(
fd
,
pBuf
,
nBytes
,
iOffset
);
if
(
iRead
<
0
)
{
if
(
errno
==
EINTR
)
{
continue
;
}
else
{
return
-
1
;
}
}
else
if
(
iRead
==
0
)
{
break
;
}
nRead
+=
iRead
;
pBuf
+=
iRead
;
iOffset
+=
iRead
;
nBytes
-=
iRead
;
}
return
nRead
;
}
// tdbOsWrite
i64
tdbOsWrite
(
tdb_fd_t
fd
,
const
void
*
pData
,
i64
nBytes
)
{
i64
nWrite
=
0
;
i64
iWrite
=
0
;
u8
*
pBuf
=
(
u8
*
)
pData
;
while
(
nBytes
>
0
)
{
iWrite
=
write
(
fd
,
pBuf
,
nBytes
);
if
(
iWrite
<
0
)
{
if
(
errno
==
EINTR
)
{
continue
;
}
return
-
1
;
}
nWrite
+=
iWrite
;
pBuf
+=
iWrite
;
nBytes
-=
iWrite
;
}
return
nWrite
;
}
#endif
\ No newline at end of file
source/libs/tdb/src/db/tdbPCache.c
浏览文件 @
417703c9
...
...
@@ -15,16 +15,16 @@
#include "tdbInt.h"
struct
SPCache
{
int
pageSize
;
int
cacheSize
;
pthread
_mutex_t
mutex
;
int
nFree
;
SPage
*
pFree
;
int
nPage
;
int
nHash
;
SPage
**
pgHash
;
int
nRecyclable
;
SPage
lru
;
int
pageSize
;
int
cacheSize
;
tdb
_mutex_t
mutex
;
int
nFree
;
SPage
*
pFree
;
int
nPage
;
int
nHash
;
SPage
**
pgHash
;
int
nRecyclable
;
SPage
lru
;
};
#define PCACHE_PAGE_HASH(pPgid) \
...
...
@@ -63,7 +63,7 @@ int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
void
*
pPtr
;
SPage
*
pPgHdr
;
pCache
=
(
SPCache
*
)
c
alloc
(
1
,
sizeof
(
*
pCache
));
pCache
=
(
SPCache
*
)
tdbOsC
alloc
(
1
,
sizeof
(
*
pCache
));
if
(
pCache
==
NULL
)
{
return
-
1
;
}
...
...
@@ -72,7 +72,7 @@ int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
pCache
->
cacheSize
=
cacheSize
;
if
(
tdbPCacheOpenImpl
(
pCache
)
<
0
)
{
f
ree
(
pCache
);
tdbOsF
ree
(
pCache
);
return
-
1
;
}
...
...
@@ -116,13 +116,13 @@ void tdbPCacheRelease(SPCache *pCache, SPage *pPage) {
}
}
static
void
tdbPCacheInitLock
(
SPCache
*
pCache
)
{
pthread_mutex_i
nit
(
&
(
pCache
->
mutex
),
NULL
);
}
static
void
tdbPCacheInitLock
(
SPCache
*
pCache
)
{
tdbMutexI
nit
(
&
(
pCache
->
mutex
),
NULL
);
}
static
void
tdbPCacheClearLock
(
SPCache
*
pCache
)
{
pthread_mutex_d
estroy
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheClearLock
(
SPCache
*
pCache
)
{
tdbMutexD
estroy
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheLock
(
SPCache
*
pCache
)
{
pthread_mutex_l
ock
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheLock
(
SPCache
*
pCache
)
{
tdbMutexL
ock
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheUnlock
(
SPCache
*
pCache
)
{
pthread_mutex_unlock
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheUnlock
(
SPCache
*
pCache
)
{
tdbMutexDestroy
(
&
(
pCache
->
mutex
));
}
static
bool
tdbPCacheLocked
(
SPCache
*
pCache
)
{
assert
(
0
);
...
...
@@ -268,7 +268,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
// Open the hash table
pCache
->
nPage
=
0
;
pCache
->
nHash
=
pCache
->
cacheSize
;
pCache
->
pgHash
=
(
SPage
**
)
c
alloc
(
pCache
->
nHash
,
sizeof
(
SPage
*
));
pCache
->
pgHash
=
(
SPage
**
)
tdbOsC
alloc
(
pCache
->
nHash
,
sizeof
(
SPage
*
));
if
(
pCache
->
pgHash
==
NULL
)
{
// TODO
return
-
1
;
...
...
source/libs/tdb/src/db/tdbPager.c
浏览文件 @
417703c9
...
...
@@ -20,8 +20,8 @@ struct SPager {
char
*
jFileName
;
int
pageSize
;
uint8_t
fid
[
TDB_FILE_ID_LEN
];
int
fd
;
int
jfd
;
tdb_fd_t
fd
;
tdb_fd_t
jfd
;
SPCache
*
pCache
;
SPgno
dbFileSize
;
SPgno
dbOrigSize
;
...
...
@@ -60,7 +60,7 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) {
zsize
=
sizeof
(
*
pPager
)
/* SPager */
+
fsize
+
1
/* dbFileName */
+
fsize
+
8
+
1
;
/* jFileName */
pPtr
=
(
uint8_t
*
)
c
alloc
(
1
,
zsize
);
pPtr
=
(
uint8_t
*
)
tdbOsC
alloc
(
1
,
zsize
);
if
(
pPtr
==
NULL
)
{
return
-
1
;
}
...
...
@@ -80,7 +80,7 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) {
// pPager->pCache
pPager
->
pCache
=
pCache
;
pPager
->
fd
=
open
(
pPager
->
dbFileName
,
O_RDWR
|
O_CREAT
,
0755
);
pPager
->
fd
=
tdbOsOpen
(
pPager
->
dbFileName
,
O_RDWR
|
O_CREAT
);
if
(
pPager
->
fd
<
0
)
{
return
-
1
;
}
...
...
@@ -90,7 +90,7 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) {
return
-
1
;
}
pPager
->
jfd
=
-
1
;
//
pPager->jfd = -1;
pPager
->
pageSize
=
tdbPCacheGetPageSize
(
pCache
);
*
ppPager
=
pPager
;
...
...
@@ -168,7 +168,7 @@ int tdbPagerBegin(SPager *pPager) {
}
// Open the journal
pPager
->
jfd
=
open
(
pPager
->
jFileName
,
O_RDWR
|
O_CREAT
,
0755
);
pPager
->
jfd
=
tdbOsOpen
(
pPager
->
jFileName
,
O_RDWR
|
O_CREAT
);
if
(
pPager
->
jfd
<
0
)
{
return
-
1
;
}
...
...
@@ -206,11 +206,11 @@ int tdbPagerCommit(SPager *pPager) {
// TODO: release the page
}
fs
ync
(
pPager
->
fd
);
tdbOsFS
ync
(
pPager
->
fd
);
c
lose
(
pPager
->
jfd
);
r
emove
(
pPager
->
jFileName
);
pPager
->
jfd
=
-
1
;
tdbOsC
lose
(
pPager
->
jfd
);
tdbOsR
emove
(
pPager
->
jFileName
);
//
pPager->jfd = -1;
return
0
;
}
...
...
@@ -222,7 +222,7 @@ static int tdbPagerReadPage(SPager *pPager, SPage *pPage) {
ASSERT
(
memcmp
(
pPager
->
fid
,
pPage
->
pgid
.
fileid
,
TDB_FILE_ID_LEN
)
==
0
);
offset
=
(
pPage
->
pgid
.
pgno
-
1
)
*
(
i64
)(
pPager
->
pageSize
);
ret
=
tdbPRead
(
pPager
->
fd
,
pPage
->
pData
,
pPager
->
pageSize
,
offset
);
ret
=
tdb
Os
PRead
(
pPager
->
fd
,
pPage
->
pData
,
pPager
->
pageSize
,
offset
);
if
(
ret
<
0
)
{
// TODO: handle error
return
-
1
;
...
...
@@ -377,12 +377,12 @@ static int tdbPagerWritePageToJournal(SPager *pPager, SPage *pPage) {
pgno
=
TDB_PAGE_PGNO
(
pPage
);
ret
=
tdbWrite
(
pPager
->
jfd
,
&
pgno
,
sizeof
(
pgno
));
ret
=
tdb
Os
Write
(
pPager
->
jfd
,
&
pgno
,
sizeof
(
pgno
));
if
(
ret
<
0
)
{
return
-
1
;
}
ret
=
tdbWrite
(
pPager
->
jfd
,
pPage
->
pData
,
pPage
->
pageSize
);
ret
=
tdb
Os
Write
(
pPager
->
jfd
,
pPage
->
pData
,
pPage
->
pageSize
);
if
(
ret
<
0
)
{
return
-
1
;
}
...
...
@@ -395,12 +395,12 @@ static int tdbPagerWritePageToDB(SPager *pPager, SPage *pPage) {
int
ret
;
offset
=
pPage
->
pageSize
*
TDB_PAGE_PGNO
(
pPage
);
if
(
ls
eek
(
pPager
->
fd
,
offset
,
SEEK_SET
)
<
0
)
{
if
(
tdbOsLS
eek
(
pPager
->
fd
,
offset
,
SEEK_SET
)
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
ret
=
tdbWrite
(
pPager
->
fd
,
pPage
->
pData
,
pPage
->
pageSize
);
ret
=
tdb
Os
Write
(
pPager
->
fd
,
pPage
->
pData
,
pPage
->
pageSize
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
...
...
source/libs/tdb/src/db/tdbUtil.c
浏览文件 @
417703c9
...
...
@@ -31,67 +31,4 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) {
}
return
0
;
}
// int tdbCheckFileAccess(const char *pathname, int mode) {
// int flags = 0;
// if (mode & TDB_F_OK) {
// flags |= F_OK;
// }
// if (mode & TDB_R_OK) {
// flags |= R_OK;
// }
// if (mode & TDB_W_OK) {
// flags |= W_OK;
// }
// return access(pathname, flags);
// }
int
tdbGetFileSize
(
const
char
*
fname
,
int
pgSize
,
SPgno
*
pSize
)
{
struct
stat
st
;
int
ret
;
int64_t
file_size
=
0
;
ret
=
taosStatFile
(
fname
,
&
file_size
,
NULL
);
if
(
ret
!=
0
)
{
return
-
1
;
}
ASSERT
(
file_size
%
pgSize
==
0
);
*
pSize
=
file_size
/
pgSize
;
return
0
;
}
int
tdbPRead
(
int
fd
,
void
*
pData
,
int
count
,
i64
offset
)
{
void
*
pBuf
;
int
nbytes
;
i64
ioffset
;
int
iread
;
pBuf
=
pData
;
nbytes
=
count
;
ioffset
=
offset
;
while
(
nbytes
>
0
)
{
iread
=
pread
(
fd
,
pBuf
,
nbytes
,
ioffset
);
if
(
iread
<
0
)
{
/* TODO */
}
else
if
(
iread
==
0
)
{
return
(
count
-
iread
);
}
nbytes
=
nbytes
-
iread
;
pBuf
=
(
void
*
)((
u8
*
)
pBuf
+
iread
);
ioffset
+=
iread
;
}
return
count
;
}
int
tdbWrite
(
int
fd
,
void
*
pData
,
int
count
)
{
// TODO
return
write
(
fd
,
pData
,
count
);
}
\ No newline at end of file
source/libs/tdb/src/inc/tdbInt.h
浏览文件 @
417703c9
...
...
@@ -16,10 +16,11 @@
#ifndef _TD_TDB_INTERNAL_H_
#define _TD_TDB_INTERNAL_H_
#include "os.h"
#include "tlist.h"
#include "tlockfree.h"
//
#include "tdb.h"
#include "tdb.h"
#ifdef __cplusplus
extern
"C"
{
...
...
@@ -148,6 +149,8 @@ typedef struct SPager SPager;
typedef
struct
SPCache
SPCache
;
typedef
struct
SPage
SPage
;
#include "tdbOs.h"
#include "tdbUtil.h"
#include "tdbPCache.h"
...
...
source/libs/tdb/src/inc/tdbOs.h
0 → 100644
浏览文件 @
417703c9
/*
* 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_OS_H_
#define _TDB_OS_H_
#ifdef __cplusplus
extern
"C"
{
#endif
// TODO: use cmake to control the option
#define TDB_FOR_TDENGINE
// For memory -----------------
#ifdef TDB_FOR_TDENGINE
#define tdbOsMalloc taosMemoryMalloc
#define tdbOsCalloc taosMemoryCalloc
#define tdbOsRealloc taosMemoryRealloc
#define tdbOsFree taosMemoryFree
#else
#define tdbOsMalloc malloc
#define tdbOsCalloc calloc
#define tdbOsRealloc realloc
#define tdbOsFree free
#endif
// For file and directory -----------------
#ifdef TDB_FOR_TDENGINE
/* file */
typedef
TdFilePtr
tdb_fd_t
;
#define tdbOsOpen taosOpenFile
#define tdbOsClose(FD) taosCloseFile(&(FD))
#define tdbOsRead taosReadFile
#define tdbOsPRead taosPReadFile
#define tdbOsWrite taosWriteFile
#define tdbOsFSync taosFsyncFile
#define tdbOsLSeek taosLSeekFile
#define tdbOsRemove remove
/* directory */
#define tdbOsMkdir taosMkDir
#define tdbOsRmdir taosRemoveDir
#else
/* file */
typedef
int
tdb_fd_t
;
#define tdbOsOpen open
#define tdbOsClose close
i64
tdbOsRead
(
tdb_fd_t
fd
,
void
*
pData
,
i64
nBytes
);
i64
tdbOsPRead
(
tdb_fd_t
fd
,
void
*
pData
,
i64
nBytes
,
i64
offset
);
i64
tdbOsWrite
(
tdb_fd_t
fd
,
const
void
*
pData
,
i64
nBytes
);
#define tdbOsFSync fsync
#define tdbOsLSeek lseek
#define tdbOsRemove remove
/* directory */
#define tdbOsMkdir mkdir
#define tdbOsRmdir rmdir
#endif
// For threads and lock -----------------
#ifdef TDB_FOR_TDENGINE
/* spin lock */
typedef
TdThreadSpinlock
tdb_spinlock_t
;
#define tdbSpinlockInit taosThreadSpinInit
#define tdbSpinlockDestroy taosThreadSpinDestroy
#define tdbSpinlockLock taosThreadSpinLock
#define tdbSpinlockUnlock taosThreadSpinUnlock
#define tdbSpinlockTrylock pthread_spin_trylock
/* mutex lock */
typedef
TdThreadMutex
tdb_mutex_t
;
#define tdbMutexInit taosThreadMutexInit
#define tdbMutexDestroy taosThreadMutexDestroy
#define tdbMutexLock taosThreadMutexLock
#define tdbMutexUnlock taosThreadMutexUnlock
#else
/* spin lock */
typedef
pthread_spinlock_t
tdb_spinlock_t
;
#define tdbSpinlockInit pthread_spin_init
#define tdbSpinlockDestroy pthread_spin_destroy
#define tdbSpinlockLock pthread_spin_lock
#define tdbSpinlockUnlock pthread_spin_unlock
#define tdbSpinlockTrylock pthread_spin_trylock
/* mutex lock */
typedef
pthread_mutex_t
tdb_mutex_t
;
#define tdbMutexInit pthread_mutex_init
#define tdbMutexDestroy pthread_mutex_destroy
#define tdbMutexLock pthread_mutex_lock
#define tdbMutexUnlock pthread_mutex_unlock
#endif
#ifdef __cplusplus
}
#endif
#endif
/*_TDB_OS_H_*/
\ No newline at end of file
source/libs/tdb/src/inc/tdbPage.h
浏览文件 @
417703c9
...
...
@@ -53,10 +53,10 @@ typedef struct __attribute__((__packed__)) {
}
SPageFtr
;
struct
SPage
{
pthread
_spinlock_t
lock
;
int
pageSize
;
u8
*
pData
;
SPageMethods
*
pPageMethods
;
tdb
_spinlock_t
lock
;
int
pageSize
;
u8
*
pData
;
SPageMethods
*
pPageMethods
;
// Fields below used by pager and am
u8
*
pPageHdr
;
u8
*
pCellIdx
;
...
...
@@ -80,21 +80,21 @@ struct SPage {
#define P_LOCK_BUSY 1
#define P_LOCK_FAIL -1
#define TDB_INIT_PAGE_LOCK(pPage)
pthread_spin_i
nit(&((pPage)->lock), 0)
#define TDB_DESTROY_PAGE_LOCK(pPage)
pthread_spin_d
estroy(&((pPage)->lock))
#define TDB_LOCK_PAGE(pPage)
pthread_spin_l
ock(&((pPage)->lock))
#define TDB_UNLOCK_PAGE(pPage)
pthread_spin_u
nlock(&((pPage)->lock))
#define TDB_TRY_LOCK_PAGE(pPage)
\
({
\
int ret;
\
if (
pthread_spin_t
rylock(&((pPage)->lock)) == 0) { \
ret = P_LOCK_SUCC;
\
} else if (errno == EBUSY) {
\
ret = P_LOCK_BUSY;
\
} else {
\
ret = P_LOCK_FAIL;
\
}
\
ret;
\
#define TDB_INIT_PAGE_LOCK(pPage)
tdbSpinlockI
nit(&((pPage)->lock), 0)
#define TDB_DESTROY_PAGE_LOCK(pPage)
tdbSpinlockD
estroy(&((pPage)->lock))
#define TDB_LOCK_PAGE(pPage)
tdbSpinlockL
ock(&((pPage)->lock))
#define TDB_UNLOCK_PAGE(pPage)
tdbSpinlockU
nlock(&((pPage)->lock))
#define TDB_TRY_LOCK_PAGE(pPage) \
({ \
int ret; \
if (
tdbSpinlockT
rylock(&((pPage)->lock)) == 0) { \
ret = P_LOCK_SUCC; \
} else if (errno == EBUSY) { \
ret = P_LOCK_BUSY; \
} else { \
ret = P_LOCK_FAIL; \
} \
ret; \
})
// APIs
...
...
source/libs/tdb/src/inc/tdbUtil.h
浏览文件 @
417703c9
...
...
@@ -30,47 +30,37 @@ extern "C" {
int
tdbGnrtFileID
(
const
char
*
fname
,
uint8_t
*
fileid
,
bool
unique
);
// #define TDB_F_OK 0x1
// #define TDB_R_OK 0x2
// #define TDB_W_OK 0x4
// int tdbCheckFileAccess(const char *pathname, int mode);
int
tdbGetFileSize
(
const
char
*
fname
,
int
pgSize
,
SPgno
*
pSize
);
int
tdbPRead
(
int
fd
,
void
*
pData
,
int
count
,
i64
offset
);
int
tdbWrite
(
int
fd
,
void
*
pData
,
int
count
);
#define TDB_REALLOC(PTR, SIZE) \
({ \
void *nPtr; \
if ((PTR) == NULL || ((int *)(PTR))[-1] < (SIZE)) { \
nPtr = realloc((PTR) ? (char *)(PTR) - sizeof(int) : NULL, (SIZE) + sizeof(int)); \
if (nPtr) { \
((int *)nPtr)[0] = (SIZE); \
nPtr = (char *)nPtr + sizeof(int); \
} \
} else { \
nPtr = (PTR); \
} \
nPtr; \
#define TDB_REALLOC(PTR, SIZE) \
({ \
void *nPtr; \
if ((PTR) == NULL || ((int *)(PTR))[-1] < (SIZE)) { \
nPtr = tdbOsRealloc((PTR) ? (char *)(PTR) - sizeof(int) : NULL, (SIZE) + sizeof(int)); \
if (nPtr) { \
((int *)nPtr)[0] = (SIZE); \
nPtr = (char *)nPtr + sizeof(int); \
} \
} else { \
nPtr = (PTR); \
} \
nPtr; \
})
#define TDB_FREE(PTR) \
do { \
if (PTR) { \
f
ree((char *)(PTR) - sizeof(int)); \
} \
#define TDB_FREE(PTR)
\
do {
\
if (PTR) {
\
tdbOsF
ree((char *)(PTR) - sizeof(int)); \
}
\
} while (0)
static
inline
void
*
tdb
Os
Malloc
(
void
*
arg
,
size_t
size
)
{
static
inline
void
*
tdb
Default
Malloc
(
void
*
arg
,
size_t
size
)
{
void
*
ptr
;
ptr
=
m
alloc
(
size
);
ptr
=
tdbOsM
alloc
(
size
);
return
ptr
;
}
static
inline
void
tdb
OsFree
(
void
*
arg
,
void
*
ptr
)
{
f
ree
(
ptr
);
}
static
inline
void
tdb
DefaultFree
(
void
*
arg
,
void
*
ptr
)
{
tdbOsF
ree
(
ptr
);
}
static
inline
int
tdbPutVarInt
(
u8
*
p
,
int
v
)
{
int
n
=
0
;
...
...
source/libs/tdb/src/page/tdbPage.c
浏览文件 @
417703c9
...
...
@@ -48,7 +48,7 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t)
*
ppPage
=
NULL
;
size
=
pageSize
+
sizeof
(
*
pPage
);
if
(
xMalloc
==
NULL
)
{
xMalloc
=
tdb
Os
Malloc
;
xMalloc
=
tdb
Default
Malloc
;
}
ptr
=
(
u8
*
)((
*
xMalloc
)(
arg
,
size
));
...
...
@@ -76,7 +76,7 @@ int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg)
u8
*
ptr
;
if
(
!
xFree
)
{
xFree
=
tdb
Os
Free
;
xFree
=
tdb
Default
Free
;
}
ptr
=
pPage
->
pData
;
...
...
@@ -144,7 +144,7 @@ int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell, u8 asOvfl
}
// TODO: here has memory leak
pNewCell
=
(
SCell
*
)
m
alloc
(
szCell
);
pNewCell
=
(
SCell
*
)
tdbOsM
alloc
(
szCell
);
memcpy
(
pNewCell
,
pCell
,
szCell
);
pPage
->
apOvfl
[
iOvfl
]
=
pNewCell
;
...
...
source/libs/tdb/test/tdbTest.cpp
浏览文件 @
417703c9
...
...
@@ -11,7 +11,7 @@ typedef struct SPoolMem {
}
SPoolMem
;
static
SPoolMem
*
openPool
()
{
SPoolMem
*
pPool
=
(
SPoolMem
*
)
m
alloc
(
sizeof
(
*
pPool
));
SPoolMem
*
pPool
=
(
SPoolMem
*
)
tdbOsM
alloc
(
sizeof
(
*
pPool
));
pPool
->
prev
=
pPool
->
next
=
pPool
;
pPool
->
size
=
0
;
...
...
@@ -31,12 +31,12 @@ static void closePool(SPoolMem *pPool) {
pMem
->
prev
->
next
=
pMem
->
next
;
pPool
->
size
-=
pMem
->
size
;
f
ree
(
pMem
);
tdbOsF
ree
(
pMem
);
}
while
(
1
);
assert
(
pPool
->
size
==
0
);
f
ree
(
pPool
);
tdbOsF
ree
(
pPool
);
}
#define clearPool closePool
...
...
@@ -46,7 +46,7 @@ static void *poolMalloc(void *arg, int size) {
SPoolMem
*
pPool
=
(
SPoolMem
*
)
arg
;
SPoolMem
*
pMem
;
pMem
=
(
SPoolMem
*
)
m
alloc
(
sizeof
(
*
pMem
)
+
size
);
pMem
=
(
SPoolMem
*
)
tdbOsM
alloc
(
sizeof
(
*
pMem
)
+
size
);
if
(
pMem
==
NULL
)
{
assert
(
0
);
}
...
...
@@ -73,7 +73,7 @@ static void poolFree(void *arg, void *ptr) {
pMem
->
prev
->
next
=
pMem
->
next
;
pPool
->
size
-=
pMem
->
size
;
f
ree
(
pMem
);
tdbOsF
ree
(
pMem
);
}
static
int
tKeyCmpr
(
const
void
*
pKey1
,
int
kLen1
,
const
void
*
pKey2
,
int
kLen2
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录