Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
10319e3c
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
Star
22018
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
10319e3c
编写于
3月 28, 2022
作者:
L
Liu Jicong
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'feature/tq' of github.com:taosdata/TDengine into feature/tq
上级
90e4cab2
7d7cd10a
变更
63
隐藏空白更改
内联
并排
Showing
63 changed file
with
1905 addition
and
1101 deletion
+1905
-1101
2.0/src/client/inc/tsclient.h
2.0/src/client/inc/tsclient.h
+8
-8
2.0/src/client/src/tscParseInsert.c
2.0/src/client/src/tscParseInsert.c
+10
-10
2.0/src/client/src/tscUtil.c
2.0/src/client/src/tscUtil.c
+1
-1
include/common/tdataformat.h
include/common/tdataformat.h
+9
-6
include/common/tmsg.h
include/common/tmsg.h
+11
-11
include/common/trow.h
include/common/trow.h
+3
-2
include/common/ttypes.h
include/common/ttypes.h
+1
-0
include/libs/nodes/querynodes.h
include/libs/nodes/querynodes.h
+1
-1
include/libs/qcom/query.h
include/libs/qcom/query.h
+5
-5
include/util/tdef.h
include/util/tdef.h
+1
-1
include/util/tprocess.h
include/util/tprocess.h
+1
-0
source/client/src/clientHb.c
source/client/src/clientHb.c
+1
-1
source/common/src/tdataformat.c
source/common/src/tdataformat.c
+5
-5
source/common/src/tmsg.c
source/common/src/tmsg.c
+6
-6
source/dnode/mgmt/container/inc/dndInt.h
source/dnode/mgmt/container/inc/dndInt.h
+0
-1
source/dnode/mgmt/container/src/dndExec.c
source/dnode/mgmt/container/src/dndExec.c
+16
-12
source/dnode/mgmt/container/src/dndFile.c
source/dnode/mgmt/container/src/dndFile.c
+14
-14
source/dnode/mgmt/container/src/dndMsg.c
source/dnode/mgmt/container/src/dndMsg.c
+11
-7
source/dnode/mgmt/container/src/dndObj.c
source/dnode/mgmt/container/src/dndObj.c
+1
-1
source/dnode/mgmt/container/src/dndTransport.c
source/dnode/mgmt/container/src/dndTransport.c
+1
-1
source/dnode/mnode/impl/src/mndStb.c
source/dnode/mnode/impl/src/mndStb.c
+6
-6
source/dnode/mnode/impl/src/mndUser.c
source/dnode/mnode/impl/src/mndUser.c
+4
-4
source/dnode/vnode/src/meta/metaBDBImpl.c
source/dnode/vnode/src/meta/metaBDBImpl.c
+5
-3
source/dnode/vnode/src/tq/tqRead.c
source/dnode/vnode/src/tq/tqRead.c
+3
-3
source/dnode/vnode/src/tsdb/tsdbCommit.c
source/dnode/vnode/src/tsdb/tsdbCommit.c
+1
-1
source/dnode/vnode/src/tsdb/tsdbMemTable.c
source/dnode/vnode/src/tsdb/tsdbMemTable.c
+1
-1
source/dnode/vnode/src/tsdb/tsdbSma.c
source/dnode/vnode/src/tsdb/tsdbSma.c
+74
-28
source/dnode/vnode/src/vnd/vnodeWrite.c
source/dnode/vnode/src/vnd/vnodeWrite.c
+11
-0
source/dnode/vnode/test/tsdbSmaTest.cpp
source/dnode/vnode/test/tsdbSmaTest.cpp
+1
-1
source/libs/catalog/src/catalog.c
source/libs/catalog/src/catalog.c
+1
-1
source/libs/parser/inc/parInsertData.h
source/libs/parser/inc/parInsertData.h
+13
-13
source/libs/parser/src/parInsert.c
source/libs/parser/src/parInsert.c
+24
-21
source/libs/parser/src/parInsertData.c
source/libs/parser/src/parInsertData.c
+4
-4
source/libs/parser/src/parTranslater.c
source/libs/parser/src/parTranslater.c
+1
-1
source/libs/parser/test/mockCatalogService.cpp
source/libs/parser/test/mockCatalogService.cpp
+1
-1
source/libs/qcom/src/queryUtil.c
source/libs/qcom/src/queryUtil.c
+1
-1
source/libs/qcom/src/querymsg.c
source/libs/qcom/src/querymsg.c
+1
-1
source/libs/qworker/src/qworkerMsg.c
source/libs/qworker/src/qworkerMsg.c
+1
-1
source/libs/tdb/CMakeLists.txt
source/libs/tdb/CMakeLists.txt
+0
-1
source/libs/tdb/src/btree/tdbBtreeBalance.c
source/libs/tdb/src/btree/tdbBtreeBalance.c
+14
-0
source/libs/tdb/src/btree/tdbBtreeCommon.c
source/libs/tdb/src/btree/tdbBtreeCommon.c
+14
-0
source/libs/tdb/src/btree/tdbBtreeDelete.c
source/libs/tdb/src/btree/tdbBtreeDelete.c
+14
-0
source/libs/tdb/src/btree/tdbBtreeInsert.c
source/libs/tdb/src/btree/tdbBtreeInsert.c
+14
-0
source/libs/tdb/src/btree/tdbBtreeInt.h
source/libs/tdb/src/btree/tdbBtreeInt.h
+27
-0
source/libs/tdb/src/btree/tdbBtreeOpen.c
source/libs/tdb/src/btree/tdbBtreeOpen.c
+14
-0
source/libs/tdb/src/db/tdbBtree.c
source/libs/tdb/src/db/tdbBtree.c
+711
-326
source/libs/tdb/src/db/tdbDb.c
source/libs/tdb/src/db/tdbDb.c
+55
-11
source/libs/tdb/src/db/tdbEnv.c
source/libs/tdb/src/db/tdbEnv.c
+1
-1
source/libs/tdb/src/db/tdbPCache.c
source/libs/tdb/src/db/tdbPCache.c
+27
-50
source/libs/tdb/src/db/tdbPage.c
source/libs/tdb/src/db/tdbPage.c
+0
-253
source/libs/tdb/src/db/tdbPager.c
source/libs/tdb/src/db/tdbPager.c
+5
-1
source/libs/tdb/src/inc/tdbBtree.h
source/libs/tdb/src/inc/tdbBtree.h
+19
-5
source/libs/tdb/src/inc/tdbDb.h
source/libs/tdb/src/inc/tdbDb.h
+13
-5
source/libs/tdb/src/inc/tdbInt.h
source/libs/tdb/src/inc/tdbInt.h
+3
-3
source/libs/tdb/src/inc/tdbPCache.h
source/libs/tdb/src/inc/tdbPCache.h
+12
-13
source/libs/tdb/src/inc/tdbPage.h
source/libs/tdb/src/inc/tdbPage.h
+50
-38
source/libs/tdb/src/inc/tdbPager.h
source/libs/tdb/src/inc/tdbPager.h
+10
-9
source/libs/tdb/src/inc/tdbUtil.h
source/libs/tdb/src/inc/tdbUtil.h
+32
-0
source/libs/tdb/src/page/tdbPage.c
source/libs/tdb/src/page/tdbPage.c
+351
-117
source/libs/tdb/src/page/tdbPageL.c
source/libs/tdb/src/page/tdbPageL.c
+32
-24
source/libs/tdb/test/tdbTest.cpp
source/libs/tdb/test/tdbTest.cpp
+172
-10
source/util/src/tprocess.c
source/util/src/tprocess.c
+49
-44
tests/test/c/tmqDemo.c
tests/test/c/tmqDemo.c
+7
-6
未找到文件。
2.0/src/client/inc/tsclient.h
浏览文件 @
10319e3c
...
@@ -116,7 +116,7 @@ typedef struct SParsedDataColInfo {
...
@@ -116,7 +116,7 @@ typedef struct SParsedDataColInfo {
uint16_t
allNullLen
;
// TODO: get from STSchema(base on SDataRow)
uint16_t
allNullLen
;
// TODO: get from STSchema(base on SDataRow)
uint16_t
extendedVarLen
;
uint16_t
extendedVarLen
;
uint16_t
boundNullLen
;
// bound column len with all NULL value(without VarDataOffsetT/SColIdx part)
uint16_t
boundNullLen
;
// bound column len with all NULL value(without VarDataOffsetT/SColIdx part)
int32_t
*
boundedColumns
;
// bound column idx according to schema
int32_t
*
boundColumns
;
// bound column idx according to schema
SBoundColumn
*
cols
;
SBoundColumn
*
cols
;
SBoundIdxInfo
*
colIdxInfo
;
SBoundIdxInfo
*
colIdxInfo
;
int8_t
orderStatus
;
// bound columns
int8_t
orderStatus
;
// bound columns
...
@@ -125,7 +125,7 @@ typedef struct SParsedDataColInfo {
...
@@ -125,7 +125,7 @@ typedef struct SParsedDataColInfo {
#define IS_DATA_COL_ORDERED(spd) ((spd->orderStatus) == (int8_t)ORDER_STATUS_ORDERED)
#define IS_DATA_COL_ORDERED(spd) ((spd->orderStatus) == (int8_t)ORDER_STATUS_ORDERED)
typedef
struct
{
typedef
struct
{
uint8_t
memR
owType
;
// default is 0, that is SDataRow
uint8_t
r
owType
;
// default is 0, that is SDataRow
int32_t
rowSize
;
int32_t
rowSize
;
}
SMemRowBuilder
;
}
SMemRowBuilder
;
...
@@ -137,17 +137,17 @@ void destroyMemRowBuilder(SMemRowBuilder *pBuilder);
...
@@ -137,17 +137,17 @@ void destroyMemRowBuilder(SMemRowBuilder *pBuilder);
/**
/**
* @brief
* @brief
*
*
* @param
memR
owType
* @param
r
owType
* @param spd
* @param spd
* @param idx the absolute bound index of columns
* @param idx the absolute bound index of columns
* @return FORCE_INLINE
* @return FORCE_INLINE
*/
*/
static
FORCE_INLINE
void
tscGet
MemRowAppendInfo
(
SSchema
*
pSchema
,
uint8_t
memRowType
,
SParsedDataColInfo
*
spd
,
static
FORCE_INLINE
void
tscGet
STSRowAppendInfo
(
SSchema
*
pSchema
,
uint8_t
rowType
,
SParsedDataColInfo
*
spd
,
int32_t
idx
,
int32_t
idx
,
int32_t
*
toffset
,
int16_t
*
colId
)
{
int32_t
*
toffset
,
int16_t
*
colId
)
{
int32_t
schemaIdx
=
0
;
int32_t
schemaIdx
=
0
;
if
(
IS_DATA_COL_ORDERED
(
spd
))
{
if
(
IS_DATA_COL_ORDERED
(
spd
))
{
schemaIdx
=
spd
->
bound
ed
Columns
[
idx
];
schemaIdx
=
spd
->
boundColumns
[
idx
];
if
(
isDataRowT
(
memR
owType
))
{
if
(
isDataRowT
(
r
owType
))
{
*
toffset
=
(
spd
->
cols
+
schemaIdx
)
->
toffset
;
// the offset of firstPart
*
toffset
=
(
spd
->
cols
+
schemaIdx
)
->
toffset
;
// the offset of firstPart
}
else
{
}
else
{
*
toffset
=
idx
*
sizeof
(
SColIdx
);
// the offset of SColIdx
*
toffset
=
idx
*
sizeof
(
SColIdx
);
// the offset of SColIdx
...
@@ -155,7 +155,7 @@ static FORCE_INLINE void tscGetMemRowAppendInfo(SSchema *pSchema, uint8_t memRow
...
@@ -155,7 +155,7 @@ static FORCE_INLINE void tscGetMemRowAppendInfo(SSchema *pSchema, uint8_t memRow
}
else
{
}
else
{
ASSERT
(
idx
==
(
spd
->
colIdxInfo
+
idx
)
->
boundIdx
);
ASSERT
(
idx
==
(
spd
->
colIdxInfo
+
idx
)
->
boundIdx
);
schemaIdx
=
(
spd
->
colIdxInfo
+
idx
)
->
schemaColIdx
;
schemaIdx
=
(
spd
->
colIdxInfo
+
idx
)
->
schemaColIdx
;
if
(
isDataRowT
(
memR
owType
))
{
if
(
isDataRowT
(
r
owType
))
{
*
toffset
=
(
spd
->
cols
+
schemaIdx
)
->
toffset
;
*
toffset
=
(
spd
->
cols
+
schemaIdx
)
->
toffset
;
}
else
{
}
else
{
*
toffset
=
((
spd
->
colIdxInfo
+
idx
)
->
finalIdx
)
*
sizeof
(
SColIdx
);
*
toffset
=
((
spd
->
colIdxInfo
+
idx
)
->
finalIdx
)
*
sizeof
(
SColIdx
);
...
...
2.0/src/client/src/tscParseInsert.c
浏览文件 @
10319e3c
...
@@ -428,7 +428,7 @@ int tsParseOneRow(char **str, STableDataBlocks *pDataBlocks, int16_t timePrec, i
...
@@ -428,7 +428,7 @@ int tsParseOneRow(char **str, STableDataBlocks *pDataBlocks, int16_t timePrec, i
// 1. set the parsed value from sql string
// 1. set the parsed value from sql string
for
(
int
i
=
0
;
i
<
spd
->
numOfBound
;
++
i
)
{
for
(
int
i
=
0
;
i
<
spd
->
numOfBound
;
++
i
)
{
// the start position in data block buffer of current value in sql
// the start position in data block buffer of current value in sql
int32_t
colIndex
=
spd
->
bound
ed
Columns
[
i
];
int32_t
colIndex
=
spd
->
boundColumns
[
i
];
char
*
start
=
row
+
spd
->
cols
[
colIndex
].
offset
;
char
*
start
=
row
+
spd
->
cols
[
colIndex
].
offset
;
...
@@ -495,7 +495,7 @@ int tsParseOneRow(char **str, STableDataBlocks *pDataBlocks, int16_t timePrec, i
...
@@ -495,7 +495,7 @@ int tsParseOneRow(char **str, STableDataBlocks *pDataBlocks, int16_t timePrec, i
bool
isPrimaryKey
=
(
colIndex
==
PRIMARYKEY_TIMESTAMP_COL_INDEX
);
bool
isPrimaryKey
=
(
colIndex
==
PRIMARYKEY_TIMESTAMP_COL_INDEX
);
int32_t
toffset
=
-
1
;
int32_t
toffset
=
-
1
;
int16_t
colId
=
-
1
;
int16_t
colId
=
-
1
;
tscGet
Mem
RowAppendInfo
(
schema
,
pBuilder
->
memRowType
,
spd
,
i
,
&
toffset
,
&
colId
);
tscGet
STS
RowAppendInfo
(
schema
,
pBuilder
->
memRowType
,
spd
,
i
,
&
toffset
,
&
colId
);
int32_t
ret
=
int32_t
ret
=
tsParseOneColumnKV
(
pSchema
,
&
sToken
,
row
,
pInsertParam
->
msg
,
str
,
isPrimaryKey
,
timePrec
,
toffset
,
colId
);
tsParseOneColumnKV
(
pSchema
,
&
sToken
,
row
,
pInsertParam
->
msg
,
str
,
isPrimaryKey
,
timePrec
,
toffset
,
colId
);
...
@@ -630,7 +630,7 @@ void tscSetBoundColumnInfo(SParsedDataColInfo *pColInfo, SSchema *pSchema, int32
...
@@ -630,7 +630,7 @@ void tscSetBoundColumnInfo(SParsedDataColInfo *pColInfo, SSchema *pSchema, int32
pColInfo
->
numOfCols
=
numOfCols
;
pColInfo
->
numOfCols
=
numOfCols
;
pColInfo
->
numOfBound
=
numOfCols
;
pColInfo
->
numOfBound
=
numOfCols
;
pColInfo
->
orderStatus
=
ORDER_STATUS_ORDERED
;
// default is ORDERED for non-bound mode
pColInfo
->
orderStatus
=
ORDER_STATUS_ORDERED
;
// default is ORDERED for non-bound mode
pColInfo
->
bound
ed
Columns
=
calloc
(
pColInfo
->
numOfCols
,
sizeof
(
int32_t
));
pColInfo
->
boundColumns
=
calloc
(
pColInfo
->
numOfCols
,
sizeof
(
int32_t
));
pColInfo
->
cols
=
calloc
(
pColInfo
->
numOfCols
,
sizeof
(
SBoundColumn
));
pColInfo
->
cols
=
calloc
(
pColInfo
->
numOfCols
,
sizeof
(
SBoundColumn
));
pColInfo
->
colIdxInfo
=
NULL
;
pColInfo
->
colIdxInfo
=
NULL
;
pColInfo
->
flen
=
0
;
pColInfo
->
flen
=
0
;
...
@@ -656,7 +656,7 @@ void tscSetBoundColumnInfo(SParsedDataColInfo *pColInfo, SSchema *pSchema, int32
...
@@ -656,7 +656,7 @@ void tscSetBoundColumnInfo(SParsedDataColInfo *pColInfo, SSchema *pSchema, int32
default:
default:
break
;
break
;
}
}
pColInfo
->
bound
ed
Columns
[
i
]
=
i
;
pColInfo
->
boundColumns
[
i
]
=
i
;
}
}
pColInfo
->
allNullLen
+=
pColInfo
->
flen
;
pColInfo
->
allNullLen
+=
pColInfo
->
flen
;
pColInfo
->
boundNullLen
=
pColInfo
->
allNullLen
;
// default set allNullLen
pColInfo
->
boundNullLen
=
pColInfo
->
allNullLen
;
// default set allNullLen
...
@@ -991,7 +991,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql, char** boundC
...
@@ -991,7 +991,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql, char** boundC
}
}
for
(
int
i
=
0
;
i
<
spd
.
numOfBound
;
++
i
)
{
for
(
int
i
=
0
;
i
<
spd
.
numOfBound
;
++
i
)
{
SSchema
*
pSchema
=
&
pTagSchema
[
spd
.
bounde
dColumns
[
i
]];
SSchema
*
pSchema
=
&
pTagSchema
[
spd
.
boun
dColumns
[
i
]];
index
=
0
;
index
=
0
;
sToken
=
tStrGetToken
(
sql
,
&
index
,
true
);
sToken
=
tStrGetToken
(
sql
,
&
index
,
true
);
...
@@ -1158,7 +1158,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
...
@@ -1158,7 +1158,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
pColInfo
->
numOfBound
=
0
;
pColInfo
->
numOfBound
=
0
;
pColInfo
->
boundNullLen
=
0
;
pColInfo
->
boundNullLen
=
0
;
memset
(
pColInfo
->
bound
ed
Columns
,
0
,
sizeof
(
int32_t
)
*
nCols
);
memset
(
pColInfo
->
boundColumns
,
0
,
sizeof
(
int32_t
)
*
nCols
);
for
(
int32_t
i
=
0
;
i
<
nCols
;
++
i
)
{
for
(
int32_t
i
=
0
;
i
<
nCols
;
++
i
)
{
pColInfo
->
cols
[
i
].
valStat
=
VAL_STAT_NONE
;
pColInfo
->
cols
[
i
].
valStat
=
VAL_STAT_NONE
;
}
}
...
@@ -1205,7 +1205,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
...
@@ -1205,7 +1205,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
}
}
pColInfo
->
cols
[
t
].
valStat
=
VAL_STAT_HAS
;
pColInfo
->
cols
[
t
].
valStat
=
VAL_STAT_HAS
;
pColInfo
->
bound
ed
Columns
[
pColInfo
->
numOfBound
]
=
t
;
pColInfo
->
boundColumns
[
pColInfo
->
numOfBound
]
=
t
;
++
pColInfo
->
numOfBound
;
++
pColInfo
->
numOfBound
;
switch
(
pSchema
[
t
].
type
)
{
switch
(
pSchema
[
t
].
type
)
{
case
TSDB_DATA_TYPE_BINARY
:
case
TSDB_DATA_TYPE_BINARY
:
...
@@ -1239,7 +1239,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
...
@@ -1239,7 +1239,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
}
}
pColInfo
->
cols
[
t
].
valStat
=
VAL_STAT_HAS
;
pColInfo
->
cols
[
t
].
valStat
=
VAL_STAT_HAS
;
pColInfo
->
bound
ed
Columns
[
pColInfo
->
numOfBound
]
=
t
;
pColInfo
->
boundColumns
[
pColInfo
->
numOfBound
]
=
t
;
++
pColInfo
->
numOfBound
;
++
pColInfo
->
numOfBound
;
switch
(
pSchema
[
t
].
type
)
{
switch
(
pSchema
[
t
].
type
)
{
case
TSDB_DATA_TYPE_BINARY
:
case
TSDB_DATA_TYPE_BINARY
:
...
@@ -1279,7 +1279,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
...
@@ -1279,7 +1279,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
}
}
SBoundIdxInfo
*
pColIdx
=
pColInfo
->
colIdxInfo
;
SBoundIdxInfo
*
pColIdx
=
pColInfo
->
colIdxInfo
;
for
(
uint16_t
i
=
0
;
i
<
pColInfo
->
numOfBound
;
++
i
)
{
for
(
uint16_t
i
=
0
;
i
<
pColInfo
->
numOfBound
;
++
i
)
{
pColIdx
[
i
].
schemaColIdx
=
(
uint16_t
)
pColInfo
->
bound
ed
Columns
[
i
];
pColIdx
[
i
].
schemaColIdx
=
(
uint16_t
)
pColInfo
->
boundColumns
[
i
];
pColIdx
[
i
].
boundIdx
=
i
;
pColIdx
[
i
].
boundIdx
=
i
;
}
}
qsort
(
pColIdx
,
pColInfo
->
numOfBound
,
sizeof
(
SBoundIdxInfo
),
schemaIdxCompar
);
qsort
(
pColIdx
,
pColInfo
->
numOfBound
,
sizeof
(
SBoundIdxInfo
),
schemaIdxCompar
);
...
@@ -1289,7 +1289,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
...
@@ -1289,7 +1289,7 @@ static int32_t parseBoundColumns(SInsertStatementParam *pInsertParam, SParsedDat
qsort
(
pColIdx
,
pColInfo
->
numOfBound
,
sizeof
(
SBoundIdxInfo
),
boundIdxCompar
);
qsort
(
pColIdx
,
pColInfo
->
numOfBound
,
sizeof
(
SBoundIdxInfo
),
boundIdxCompar
);
}
}
memset
(
&
pColInfo
->
bound
ed
Columns
[
pColInfo
->
numOfBound
],
0
,
memset
(
&
pColInfo
->
boundColumns
[
pColInfo
->
numOfBound
],
0
,
sizeof
(
int32_t
)
*
(
pColInfo
->
numOfCols
-
pColInfo
->
numOfBound
));
sizeof
(
int32_t
)
*
(
pColInfo
->
numOfCols
-
pColInfo
->
numOfBound
));
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
...
...
2.0/src/client/src/tscUtil.c
浏览文件 @
10319e3c
...
@@ -1554,7 +1554,7 @@ void tscFreeSqlObj(SSqlObj* pSql) {
...
@@ -1554,7 +1554,7 @@ void tscFreeSqlObj(SSqlObj* pSql) {
}
}
void
tscDestroyBoundColumnInfo
(
SParsedDataColInfo
*
pColInfo
)
{
void
tscDestroyBoundColumnInfo
(
SParsedDataColInfo
*
pColInfo
)
{
tfree
(
pColInfo
->
bound
ed
Columns
);
tfree
(
pColInfo
->
boundColumns
);
tfree
(
pColInfo
->
cols
);
tfree
(
pColInfo
->
cols
);
tfree
(
pColInfo
->
colIdxInfo
);
tfree
(
pColInfo
->
colIdxInfo
);
}
}
...
...
include/common/tdataformat.h
浏览文件 @
10319e3c
...
@@ -59,12 +59,15 @@ extern "C" {
...
@@ -59,12 +59,15 @@ extern "C" {
} while (0);
} while (0);
// ----------------- TSDB COLUMN DEFINITION
// ----------------- TSDB COLUMN DEFINITION
#pragma pack(push, 1)
typedef
struct
{
typedef
struct
{
int8_t
type
;
// Column type
col_id_t
colId
;
// column ID(start from PRIMARYKEY_TIMESTAMP_COL_ID(1))
col_id_t
colId
;
// column ID(start from PRIMARYKEY_TIMESTAMP_COL_ID(1))
int32_t
type
:
8
;
// column type
int16_t
bytes
;
// column bytes (restore to int16_t in case of misuse)
int32_t
bytes
:
24
;
// column bytes (restore to int32_t in case of misuse)
uint16_t
offset
;
// point offset in STpRow after the header part.
int32_t
sma
:
8
;
// block SMA: 0, no SMA, 1, sum/min/max, 2, ...
int32_t
offset
:
24
;
// point offset in STpRow after the header part.
}
STColumn
;
}
STColumn
;
#pragma pack(pop)
#define colType(col) ((col)->type)
#define colType(col) ((col)->type)
#define colColId(col) ((col)->colId)
#define colColId(col) ((col)->colId)
...
@@ -136,7 +139,7 @@ typedef struct {
...
@@ -136,7 +139,7 @@ typedef struct {
int32_t
tdInitTSchemaBuilder
(
STSchemaBuilder
*
pBuilder
,
int32_t
version
);
int32_t
tdInitTSchemaBuilder
(
STSchemaBuilder
*
pBuilder
,
int32_t
version
);
void
tdDestroyTSchemaBuilder
(
STSchemaBuilder
*
pBuilder
);
void
tdDestroyTSchemaBuilder
(
STSchemaBuilder
*
pBuilder
);
void
tdResetTSchemaBuilder
(
STSchemaBuilder
*
pBuilder
,
int32_t
version
);
void
tdResetTSchemaBuilder
(
STSchemaBuilder
*
pBuilder
,
int32_t
version
);
int32_t
tdAddColToSchema
(
STSchemaBuilder
*
pBuilder
,
int8_t
type
,
int16_t
colId
,
int16
_t
bytes
);
int32_t
tdAddColToSchema
(
STSchemaBuilder
*
pBuilder
,
int8_t
type
,
col_id_t
colId
,
col_bytes
_t
bytes
);
STSchema
*
tdGetSchemaFromBuilder
(
STSchemaBuilder
*
pBuilder
);
STSchema
*
tdGetSchemaFromBuilder
(
STSchemaBuilder
*
pBuilder
);
// ----------------- Semantic timestamp key definition
// ----------------- Semantic timestamp key definition
...
@@ -590,7 +593,7 @@ void tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder);
...
@@ -590,7 +593,7 @@ void tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder);
void
tdResetKVRowBuilder
(
SKVRowBuilder
*
pBuilder
);
void
tdResetKVRowBuilder
(
SKVRowBuilder
*
pBuilder
);
SKVRow
tdGetKVRowFromBuilder
(
SKVRowBuilder
*
pBuilder
);
SKVRow
tdGetKVRowFromBuilder
(
SKVRowBuilder
*
pBuilder
);
static
FORCE_INLINE
int32_t
tdAddColToKVRow
(
SKVRowBuilder
*
pBuilder
,
int16
_t
colId
,
int8_t
type
,
const
void
*
value
)
{
static
FORCE_INLINE
int32_t
tdAddColToKVRow
(
SKVRowBuilder
*
pBuilder
,
col_id
_t
colId
,
int8_t
type
,
const
void
*
value
)
{
if
(
pBuilder
->
nCols
>=
pBuilder
->
tCols
)
{
if
(
pBuilder
->
nCols
>=
pBuilder
->
tCols
)
{
pBuilder
->
tCols
*=
2
;
pBuilder
->
tCols
*=
2
;
SColIdx
*
pColIdx
=
(
SColIdx
*
)
taosMemoryRealloc
((
void
*
)(
pBuilder
->
pColIdx
),
sizeof
(
SColIdx
)
*
pBuilder
->
tCols
);
SColIdx
*
pColIdx
=
(
SColIdx
*
)
taosMemoryRealloc
((
void
*
)(
pBuilder
->
pColIdx
),
sizeof
(
SColIdx
)
*
pBuilder
->
tCols
);
...
...
include/common/tmsg.h
浏览文件 @
10319e3c
...
@@ -208,11 +208,11 @@ typedef struct {
...
@@ -208,11 +208,11 @@ typedef struct {
typedef
struct
SSubmitBlk
{
typedef
struct
SSubmitBlk
{
int64_t
uid
;
// table unique id
int64_t
uid
;
// table unique id
int64_t
suid
;
// stable id
int64_t
suid
;
// stable id
int32_t
padding
;
// TODO just for padding here
int32_t
sversion
;
// data schema version
int32_t
sversion
;
// data schema version
int32_t
dataLen
;
// data part length, not including the SSubmitBlk head
int32_t
dataLen
;
// data part length, not including the SSubmitBlk head
int32_t
schemaLen
;
// schema length, if length is 0, no schema exists
int32_t
schemaLen
;
// schema length, if length is 0, no schema exists
int16_t
numOfRows
;
// total number of rows in current submit block
int16_t
numOfRows
;
// total number of rows in current submit block
int16_t
padding
;
// TODO just for padding here
char
data
[];
char
data
[];
}
SSubmitBlk
;
}
SSubmitBlk
;
...
@@ -259,10 +259,10 @@ typedef struct {
...
@@ -259,10 +259,10 @@ typedef struct {
}
SSubmitRsp
;
}
SSubmitRsp
;
typedef
struct
SSchema
{
typedef
struct
SSchema
{
int8_t
type
;
int8_t
type
;
int32
_t
colId
;
col_id
_t
colId
;
int32_t
bytes
;
int32_t
bytes
;
char
name
[
TSDB_COL_NAME_LEN
];
char
name
[
TSDB_COL_NAME_LEN
];
}
SSchema
;
}
SSchema
;
typedef
struct
{
typedef
struct
{
...
@@ -438,8 +438,8 @@ typedef struct {
...
@@ -438,8 +438,8 @@ typedef struct {
*/
*/
typedef
struct
{
typedef
struct
{
union
{
union
{
int16
_t
colId
;
col_id
_t
colId
;
int16_t
slotId
;
int16_t
slotId
;
};
};
int16_t
type
;
int16_t
type
;
...
@@ -1901,7 +1901,7 @@ static FORCE_INLINE int32_t taosEncodeSSchema(void** buf, const SSchema* pSchema
...
@@ -1901,7 +1901,7 @@ static FORCE_INLINE int32_t taosEncodeSSchema(void** buf, const SSchema* pSchema
int32_t
tlen
=
0
;
int32_t
tlen
=
0
;
tlen
+=
taosEncodeFixedI8
(
buf
,
pSchema
->
type
);
tlen
+=
taosEncodeFixedI8
(
buf
,
pSchema
->
type
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pSchema
->
bytes
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pSchema
->
bytes
);
tlen
+=
taosEncodeFixedI
32
(
buf
,
pSchema
->
colId
);
tlen
+=
taosEncodeFixedI
16
(
buf
,
pSchema
->
colId
);
tlen
+=
taosEncodeString
(
buf
,
pSchema
->
name
);
tlen
+=
taosEncodeString
(
buf
,
pSchema
->
name
);
return
tlen
;
return
tlen
;
}
}
...
@@ -1909,7 +1909,7 @@ static FORCE_INLINE int32_t taosEncodeSSchema(void** buf, const SSchema* pSchema
...
@@ -1909,7 +1909,7 @@ static FORCE_INLINE int32_t taosEncodeSSchema(void** buf, const SSchema* pSchema
static
FORCE_INLINE
void
*
taosDecodeSSchema
(
void
*
buf
,
SSchema
*
pSchema
)
{
static
FORCE_INLINE
void
*
taosDecodeSSchema
(
void
*
buf
,
SSchema
*
pSchema
)
{
buf
=
taosDecodeFixedI8
(
buf
,
&
pSchema
->
type
);
buf
=
taosDecodeFixedI8
(
buf
,
&
pSchema
->
type
);
buf
=
taosDecodeFixedI32
(
buf
,
&
pSchema
->
bytes
);
buf
=
taosDecodeFixedI32
(
buf
,
&
pSchema
->
bytes
);
buf
=
taosDecodeFixedI
32
(
buf
,
&
pSchema
->
colId
);
buf
=
taosDecodeFixedI
16
(
buf
,
&
pSchema
->
colId
);
buf
=
taosDecodeStringTo
(
buf
,
pSchema
->
name
);
buf
=
taosDecodeStringTo
(
buf
,
pSchema
->
name
);
return
buf
;
return
buf
;
}
}
...
@@ -1917,7 +1917,7 @@ static FORCE_INLINE void* taosDecodeSSchema(void* buf, SSchema* pSchema) {
...
@@ -1917,7 +1917,7 @@ static FORCE_INLINE void* taosDecodeSSchema(void* buf, SSchema* pSchema) {
static
FORCE_INLINE
int32_t
tEncodeSSchema
(
SCoder
*
pEncoder
,
const
SSchema
*
pSchema
)
{
static
FORCE_INLINE
int32_t
tEncodeSSchema
(
SCoder
*
pEncoder
,
const
SSchema
*
pSchema
)
{
if
(
tEncodeI8
(
pEncoder
,
pSchema
->
type
)
<
0
)
return
-
1
;
if
(
tEncodeI8
(
pEncoder
,
pSchema
->
type
)
<
0
)
return
-
1
;
if
(
tEncodeI32
(
pEncoder
,
pSchema
->
bytes
)
<
0
)
return
-
1
;
if
(
tEncodeI32
(
pEncoder
,
pSchema
->
bytes
)
<
0
)
return
-
1
;
if
(
tEncodeI
32
(
pEncoder
,
pSchema
->
colId
)
<
0
)
return
-
1
;
if
(
tEncodeI
16
(
pEncoder
,
pSchema
->
colId
)
<
0
)
return
-
1
;
if
(
tEncodeCStr
(
pEncoder
,
pSchema
->
name
)
<
0
)
return
-
1
;
if
(
tEncodeCStr
(
pEncoder
,
pSchema
->
name
)
<
0
)
return
-
1
;
return
0
;
return
0
;
}
}
...
@@ -1925,7 +1925,7 @@ static FORCE_INLINE int32_t tEncodeSSchema(SCoder* pEncoder, const SSchema* pSch
...
@@ -1925,7 +1925,7 @@ static FORCE_INLINE int32_t tEncodeSSchema(SCoder* pEncoder, const SSchema* pSch
static
FORCE_INLINE
int32_t
tDecodeSSchema
(
SCoder
*
pDecoder
,
SSchema
*
pSchema
)
{
static
FORCE_INLINE
int32_t
tDecodeSSchema
(
SCoder
*
pDecoder
,
SSchema
*
pSchema
)
{
if
(
tDecodeI8
(
pDecoder
,
&
pSchema
->
type
)
<
0
)
return
-
1
;
if
(
tDecodeI8
(
pDecoder
,
&
pSchema
->
type
)
<
0
)
return
-
1
;
if
(
tDecodeI32
(
pDecoder
,
&
pSchema
->
bytes
)
<
0
)
return
-
1
;
if
(
tDecodeI32
(
pDecoder
,
&
pSchema
->
bytes
)
<
0
)
return
-
1
;
if
(
tDecodeI
32
(
pDecoder
,
&
pSchema
->
colId
)
<
0
)
return
-
1
;
if
(
tDecodeI
16
(
pDecoder
,
&
pSchema
->
colId
)
<
0
)
return
-
1
;
if
(
tDecodeCStrTo
(
pDecoder
,
pSchema
->
name
)
<
0
)
return
-
1
;
if
(
tDecodeCStrTo
(
pDecoder
,
pSchema
->
name
)
<
0
)
return
-
1
;
return
0
;
return
0
;
}
}
...
...
include/common/trow.h
浏览文件 @
10319e3c
...
@@ -671,8 +671,9 @@ static FORCE_INLINE int32_t tdAppendColValToKvRow(SRowBuilder *pBuilder, TDRowVa
...
@@ -671,8 +671,9 @@ static FORCE_INLINE int32_t tdAppendColValToKvRow(SRowBuilder *pBuilder, TDRowVa
* @param colIdx sorted column index, start from 0
* @param colIdx sorted column index, start from 0
* @return FORCE_INLINE
* @return FORCE_INLINE
*/
*/
static
FORCE_INLINE
int32_t
tdAppendColValToRow
(
SRowBuilder
*
pBuilder
,
int16_t
colId
,
int8_t
colType
,
TDRowValT
valType
,
static
FORCE_INLINE
int32_t
tdAppendColValToRow
(
SRowBuilder
*
pBuilder
,
col_id_t
colId
,
int8_t
colType
,
const
void
*
val
,
bool
isCopyVarData
,
int32_t
offset
,
int16_t
colIdx
)
{
TDRowValT
valType
,
const
void
*
val
,
bool
isCopyVarData
,
int32_t
offset
,
col_id_t
colIdx
)
{
STSRow
*
pRow
=
pBuilder
->
pBuf
;
STSRow
*
pRow
=
pBuilder
->
pBuf
;
if
(
!
val
)
{
if
(
!
val
)
{
#ifdef TD_SUPPORT_BITMAP
#ifdef TD_SUPPORT_BITMAP
...
...
include/common/ttypes.h
浏览文件 @
10319e3c
...
@@ -29,6 +29,7 @@ typedef uint32_t TDRowLenT;
...
@@ -29,6 +29,7 @@ typedef uint32_t TDRowLenT;
typedef
uint8_t
TDRowValT
;
typedef
uint8_t
TDRowValT
;
typedef
int16_t
col_id_t
;
typedef
int16_t
col_id_t
;
typedef
int8_t
col_type_t
;
typedef
int8_t
col_type_t
;
typedef
int32_t
col_bytes_t
;
#pragma pack(push, 1)
#pragma pack(push, 1)
typedef
struct
{
typedef
struct
{
...
...
include/libs/nodes/querynodes.h
浏览文件 @
10319e3c
...
@@ -56,7 +56,7 @@ typedef enum EColumnType {
...
@@ -56,7 +56,7 @@ typedef enum EColumnType {
typedef
struct
SColumnNode
{
typedef
struct
SColumnNode
{
SExprNode
node
;
// QUERY_NODE_COLUMN
SExprNode
node
;
// QUERY_NODE_COLUMN
uint64_t
tableId
;
uint64_t
tableId
;
int16
_t
colId
;
col_id
_t
colId
;
EColumnType
colType
;
// column or tag
EColumnType
colType
;
// column or tag
char
dbName
[
TSDB_DB_NAME_LEN
];
char
dbName
[
TSDB_DB_NAME_LEN
];
char
tableName
[
TSDB_TABLE_NAME_LEN
];
char
tableName
[
TSDB_TABLE_NAME_LEN
];
...
...
include/libs/qcom/query.h
浏览文件 @
10319e3c
...
@@ -43,10 +43,10 @@ typedef enum {
...
@@ -43,10 +43,10 @@ typedef enum {
}
ETaskType
;
}
ETaskType
;
typedef
struct
STableComInfo
{
typedef
struct
STableComInfo
{
uint8_t
numOfTags
;
// the number of tags in schema
uint8_t
numOfTags
;
// the number of tags in schema
uint8_t
precision
;
// the number of precision
uint8_t
precision
;
// the number of precision
int16
_t
numOfColumns
;
// the number of columns
col_id
_t
numOfColumns
;
// the number of columns
int32_t
rowSize
;
// row size of the schema
int32_t
rowSize
;
// row size of the schema
}
STableComInfo
;
}
STableComInfo
;
typedef
struct
SIndexMeta
{
typedef
struct
SIndexMeta
{
...
@@ -173,7 +173,7 @@ bool tIsValidSchema(struct SSchema* pSchema, int32_t numOfCols, int32_
...
@@ -173,7 +173,7 @@ bool tIsValidSchema(struct SSchema* pSchema, int32_t numOfCols, int32_
int32_t
queryCreateTableMetaFromMsg
(
STableMetaRsp
*
msg
,
bool
isSuperTable
,
STableMeta
**
pMeta
);
int32_t
queryCreateTableMetaFromMsg
(
STableMetaRsp
*
msg
,
bool
isSuperTable
,
STableMeta
**
pMeta
);
char
*
jobTaskStatusStr
(
int32_t
status
);
char
*
jobTaskStatusStr
(
int32_t
status
);
SSchema
createSchema
(
uint8_t
type
,
int32_t
bytes
,
int32
_t
colId
,
const
char
*
name
);
SSchema
createSchema
(
int8_t
type
,
int32_t
bytes
,
col_id
_t
colId
,
const
char
*
name
);
extern
int32_t
(
*
queryBuildMsg
[
TDMT_MAX
])(
void
*
input
,
char
**
msg
,
int32_t
msgSize
,
int32_t
*
msgLen
);
extern
int32_t
(
*
queryBuildMsg
[
TDMT_MAX
])(
void
*
input
,
char
**
msg
,
int32_t
msgSize
,
int32_t
*
msgLen
);
extern
int32_t
(
*
queryProcessMsgRsp
[
TDMT_MAX
])(
void
*
output
,
char
*
msg
,
int32_t
msgSize
);
extern
int32_t
(
*
queryProcessMsgRsp
[
TDMT_MAX
])(
void
*
output
,
char
*
msg
,
int32_t
msgSize
);
...
...
include/util/tdef.h
浏览文件 @
10319e3c
...
@@ -209,7 +209,7 @@ typedef enum ELogicConditionType {
...
@@ -209,7 +209,7 @@ typedef enum ELogicConditionType {
#define TSDB_FUNC_TYPE_AGGREGATE 2
#define TSDB_FUNC_TYPE_AGGREGATE 2
#define TSDB_FUNC_MAX_RETRIEVE 1024
#define TSDB_FUNC_MAX_RETRIEVE 1024
#define TSDB_INDEX_NAME_LEN
33 // 32
+ 1 '\0'
#define TSDB_INDEX_NAME_LEN
65 // 64
+ 1 '\0'
#define TSDB_TYPE_STR_MAX_LEN 32
#define TSDB_TYPE_STR_MAX_LEN 32
#define TSDB_TABLE_FNAME_LEN (TSDB_DB_FNAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_NAME_DELIMITER_LEN)
#define TSDB_TABLE_FNAME_LEN (TSDB_DB_FNAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_NAME_DELIMITER_LEN)
#define TSDB_TOPIC_FNAME_LEN TSDB_TABLE_FNAME_LEN
#define TSDB_TOPIC_FNAME_LEN TSDB_TABLE_FNAME_LEN
...
...
include/util/tprocess.h
浏览文件 @
10319e3c
...
@@ -51,6 +51,7 @@ void taosProcCleanup(SProcObj *pProc);
...
@@ -51,6 +51,7 @@ void taosProcCleanup(SProcObj *pProc);
int32_t
taosProcRun
(
SProcObj
*
pProc
);
int32_t
taosProcRun
(
SProcObj
*
pProc
);
void
taosProcStop
(
SProcObj
*
pProc
);
void
taosProcStop
(
SProcObj
*
pProc
);
bool
taosProcIsChild
(
SProcObj
*
pProc
);
bool
taosProcIsChild
(
SProcObj
*
pProc
);
int32_t
taosProcChildId
(
SProcObj
*
pProc
);
int32_t
taosProcPutToChildQueue
(
SProcObj
*
pProc
,
void
*
pHead
,
int32_t
headLen
,
void
*
pBody
,
int32_t
bodyLen
);
int32_t
taosProcPutToChildQueue
(
SProcObj
*
pProc
,
void
*
pHead
,
int32_t
headLen
,
void
*
pBody
,
int32_t
bodyLen
);
int32_t
taosProcPutToParentQueue
(
SProcObj
*
pProc
,
void
*
pHead
,
int32_t
headLen
,
void
*
pBody
,
int32_t
bodyLen
);
int32_t
taosProcPutToParentQueue
(
SProcObj
*
pProc
,
void
*
pHead
,
int32_t
headLen
,
void
*
pBody
,
int32_t
bodyLen
);
...
...
source/client/src/clientHb.c
浏览文件 @
10319e3c
...
@@ -91,7 +91,7 @@ static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalo
...
@@ -91,7 +91,7 @@ static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalo
}
else
{
}
else
{
tscDebug
(
"hb update stb, db:%s, stb:%s"
,
rsp
->
dbFName
,
rsp
->
stbName
);
tscDebug
(
"hb update stb, db:%s, stb:%s"
,
rsp
->
dbFName
,
rsp
->
stbName
);
if
(
rsp
->
pSchemas
[
0
].
colId
!=
PRIMARYKEY_TIMESTAMP_COL_ID
)
{
if
(
rsp
->
pSchemas
[
0
].
colId
!=
PRIMARYKEY_TIMESTAMP_COL_ID
)
{
tscError
(
"invalid colId[%
d
] for the first column in table meta rsp msg"
,
rsp
->
pSchemas
[
0
].
colId
);
tscError
(
"invalid colId[%
"
PRIi16
"
] for the first column in table meta rsp msg"
,
rsp
->
pSchemas
[
0
].
colId
);
tFreeSTableMetaBatchRsp
(
&
batchMetaRsp
);
tFreeSTableMetaBatchRsp
(
&
batchMetaRsp
);
return
TSDB_CODE_TSC_INVALID_VALUE
;
return
TSDB_CODE_TSC_INVALID_VALUE
;
}
}
...
...
source/common/src/tdataformat.c
浏览文件 @
10319e3c
...
@@ -106,12 +106,12 @@ void *tdDecodeSchema(void *buf, STSchema **pRSchema) {
...
@@ -106,12 +106,12 @@ void *tdDecodeSchema(void *buf, STSchema **pRSchema) {
if
(
tdInitTSchemaBuilder
(
&
schemaBuilder
,
version
)
<
0
)
return
NULL
;
if
(
tdInitTSchemaBuilder
(
&
schemaBuilder
,
version
)
<
0
)
return
NULL
;
for
(
int
i
=
0
;
i
<
numOfCols
;
i
++
)
{
for
(
int
i
=
0
;
i
<
numOfCols
;
i
++
)
{
int8
_t
type
=
0
;
col_type
_t
type
=
0
;
int16_t
colId
=
0
;
col_id_t
colId
=
0
;
int16
_t
bytes
=
0
;
col_bytes
_t
bytes
=
0
;
buf
=
taosDecodeFixedI8
(
buf
,
&
type
);
buf
=
taosDecodeFixedI8
(
buf
,
&
type
);
buf
=
taosDecodeFixedI16
(
buf
,
&
colId
);
buf
=
taosDecodeFixedI16
(
buf
,
&
colId
);
buf
=
taosDecodeFixedI
16
(
buf
,
&
bytes
);
buf
=
taosDecodeFixedI
32
(
buf
,
&
bytes
);
if
(
tdAddColToSchema
(
&
schemaBuilder
,
type
,
colId
,
bytes
)
<
0
)
{
if
(
tdAddColToSchema
(
&
schemaBuilder
,
type
,
colId
,
bytes
)
<
0
)
{
tdDestroyTSchemaBuilder
(
&
schemaBuilder
);
tdDestroyTSchemaBuilder
(
&
schemaBuilder
);
return
NULL
;
return
NULL
;
...
@@ -148,7 +148,7 @@ void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, int32_t version) {
...
@@ -148,7 +148,7 @@ void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, int32_t version) {
pBuilder
->
version
=
version
;
pBuilder
->
version
=
version
;
}
}
int
tdAddColToSchema
(
STSchemaBuilder
*
pBuilder
,
int8_t
type
,
int16_t
colId
,
int16
_t
bytes
)
{
int
tdAddColToSchema
(
STSchemaBuilder
*
pBuilder
,
int8_t
type
,
col_id_t
colId
,
col_bytes
_t
bytes
)
{
if
(
!
isValidDataType
(
type
))
return
-
1
;
if
(
!
isValidDataType
(
type
))
return
-
1
;
if
(
pBuilder
->
nCols
>=
pBuilder
->
tCols
)
{
if
(
pBuilder
->
nCols
>=
pBuilder
->
tCols
)
{
...
...
source/common/src/tmsg.c
浏览文件 @
10319e3c
...
@@ -299,14 +299,14 @@ int32_t tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) {
...
@@ -299,14 +299,14 @@ int32_t tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) {
tlen
+=
taosEncodeFixedU32
(
buf
,
pReq
->
stbCfg
.
nCols
);
tlen
+=
taosEncodeFixedU32
(
buf
,
pReq
->
stbCfg
.
nCols
);
for
(
uint32_t
i
=
0
;
i
<
pReq
->
stbCfg
.
nCols
;
i
++
)
{
for
(
uint32_t
i
=
0
;
i
<
pReq
->
stbCfg
.
nCols
;
i
++
)
{
tlen
+=
taosEncodeFixedI8
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
type
);
tlen
+=
taosEncodeFixedI8
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
type
);
tlen
+=
taosEncodeFixedI
32
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
colId
);
tlen
+=
taosEncodeFixedI
16
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
colId
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
bytes
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
bytes
);
tlen
+=
taosEncodeString
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
name
);
tlen
+=
taosEncodeString
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
name
);
}
}
tlen
+=
taosEncodeFixedU32
(
buf
,
pReq
->
stbCfg
.
nTagCols
);
tlen
+=
taosEncodeFixedU32
(
buf
,
pReq
->
stbCfg
.
nTagCols
);
for
(
uint32_t
i
=
0
;
i
<
pReq
->
stbCfg
.
nTagCols
;
i
++
)
{
for
(
uint32_t
i
=
0
;
i
<
pReq
->
stbCfg
.
nTagCols
;
i
++
)
{
tlen
+=
taosEncodeFixedI8
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
type
);
tlen
+=
taosEncodeFixedI8
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
type
);
tlen
+=
taosEncodeFixedI
32
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
colId
);
tlen
+=
taosEncodeFixedI
16
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
colId
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
bytes
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
bytes
);
tlen
+=
taosEncodeString
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
name
);
tlen
+=
taosEncodeString
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
name
);
}
}
...
@@ -333,7 +333,7 @@ int32_t tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) {
...
@@ -333,7 +333,7 @@ int32_t tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) {
tlen
+=
taosEncodeFixedU32
(
buf
,
pReq
->
ntbCfg
.
nCols
);
tlen
+=
taosEncodeFixedU32
(
buf
,
pReq
->
ntbCfg
.
nCols
);
for
(
uint32_t
i
=
0
;
i
<
pReq
->
ntbCfg
.
nCols
;
i
++
)
{
for
(
uint32_t
i
=
0
;
i
<
pReq
->
ntbCfg
.
nCols
;
i
++
)
{
tlen
+=
taosEncodeFixedI8
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
type
);
tlen
+=
taosEncodeFixedI8
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
type
);
tlen
+=
taosEncodeFixedI
32
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
colId
);
tlen
+=
taosEncodeFixedI
16
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
colId
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
bytes
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
bytes
);
tlen
+=
taosEncodeString
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
name
);
tlen
+=
taosEncodeString
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
name
);
}
}
...
@@ -374,7 +374,7 @@ void *tDeserializeSVCreateTbReq(void *buf, SVCreateTbReq *pReq) {
...
@@ -374,7 +374,7 @@ void *tDeserializeSVCreateTbReq(void *buf, SVCreateTbReq *pReq) {
pReq
->
stbCfg
.
pSchema
=
(
SSchema
*
)
taosMemoryMalloc
(
pReq
->
stbCfg
.
nCols
*
sizeof
(
SSchema
));
pReq
->
stbCfg
.
pSchema
=
(
SSchema
*
)
taosMemoryMalloc
(
pReq
->
stbCfg
.
nCols
*
sizeof
(
SSchema
));
for
(
uint32_t
i
=
0
;
i
<
pReq
->
stbCfg
.
nCols
;
i
++
)
{
for
(
uint32_t
i
=
0
;
i
<
pReq
->
stbCfg
.
nCols
;
i
++
)
{
buf
=
taosDecodeFixedI8
(
buf
,
&
(
pReq
->
stbCfg
.
pSchema
[
i
].
type
));
buf
=
taosDecodeFixedI8
(
buf
,
&
(
pReq
->
stbCfg
.
pSchema
[
i
].
type
));
buf
=
taosDecodeFixedI
32
(
buf
,
&
(
pReq
->
stbCfg
.
pSchema
[
i
].
colId
));
buf
=
taosDecodeFixedI
16
(
buf
,
&
(
pReq
->
stbCfg
.
pSchema
[
i
].
colId
));
buf
=
taosDecodeFixedI32
(
buf
,
&
(
pReq
->
stbCfg
.
pSchema
[
i
].
bytes
));
buf
=
taosDecodeFixedI32
(
buf
,
&
(
pReq
->
stbCfg
.
pSchema
[
i
].
bytes
));
buf
=
taosDecodeStringTo
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
name
);
buf
=
taosDecodeStringTo
(
buf
,
pReq
->
stbCfg
.
pSchema
[
i
].
name
);
}
}
...
@@ -382,7 +382,7 @@ void *tDeserializeSVCreateTbReq(void *buf, SVCreateTbReq *pReq) {
...
@@ -382,7 +382,7 @@ void *tDeserializeSVCreateTbReq(void *buf, SVCreateTbReq *pReq) {
pReq
->
stbCfg
.
pTagSchema
=
(
SSchema
*
)
taosMemoryMalloc
(
pReq
->
stbCfg
.
nTagCols
*
sizeof
(
SSchema
));
pReq
->
stbCfg
.
pTagSchema
=
(
SSchema
*
)
taosMemoryMalloc
(
pReq
->
stbCfg
.
nTagCols
*
sizeof
(
SSchema
));
for
(
uint32_t
i
=
0
;
i
<
pReq
->
stbCfg
.
nTagCols
;
i
++
)
{
for
(
uint32_t
i
=
0
;
i
<
pReq
->
stbCfg
.
nTagCols
;
i
++
)
{
buf
=
taosDecodeFixedI8
(
buf
,
&
(
pReq
->
stbCfg
.
pTagSchema
[
i
].
type
));
buf
=
taosDecodeFixedI8
(
buf
,
&
(
pReq
->
stbCfg
.
pTagSchema
[
i
].
type
));
buf
=
taosDecodeFixedI
32
(
buf
,
&
pReq
->
stbCfg
.
pTagSchema
[
i
].
colId
);
buf
=
taosDecodeFixedI
16
(
buf
,
&
pReq
->
stbCfg
.
pTagSchema
[
i
].
colId
);
buf
=
taosDecodeFixedI32
(
buf
,
&
pReq
->
stbCfg
.
pTagSchema
[
i
].
bytes
);
buf
=
taosDecodeFixedI32
(
buf
,
&
pReq
->
stbCfg
.
pTagSchema
[
i
].
bytes
);
buf
=
taosDecodeStringTo
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
name
);
buf
=
taosDecodeStringTo
(
buf
,
pReq
->
stbCfg
.
pTagSchema
[
i
].
name
);
}
}
...
@@ -422,7 +422,7 @@ void *tDeserializeSVCreateTbReq(void *buf, SVCreateTbReq *pReq) {
...
@@ -422,7 +422,7 @@ void *tDeserializeSVCreateTbReq(void *buf, SVCreateTbReq *pReq) {
pReq
->
ntbCfg
.
pSchema
=
(
SSchema
*
)
taosMemoryMalloc
(
pReq
->
ntbCfg
.
nCols
*
sizeof
(
SSchema
));
pReq
->
ntbCfg
.
pSchema
=
(
SSchema
*
)
taosMemoryMalloc
(
pReq
->
ntbCfg
.
nCols
*
sizeof
(
SSchema
));
for
(
uint32_t
i
=
0
;
i
<
pReq
->
ntbCfg
.
nCols
;
i
++
)
{
for
(
uint32_t
i
=
0
;
i
<
pReq
->
ntbCfg
.
nCols
;
i
++
)
{
buf
=
taosDecodeFixedI8
(
buf
,
&
pReq
->
ntbCfg
.
pSchema
[
i
].
type
);
buf
=
taosDecodeFixedI8
(
buf
,
&
pReq
->
ntbCfg
.
pSchema
[
i
].
type
);
buf
=
taosDecodeFixedI
32
(
buf
,
&
pReq
->
ntbCfg
.
pSchema
[
i
].
colId
);
buf
=
taosDecodeFixedI
16
(
buf
,
&
pReq
->
ntbCfg
.
pSchema
[
i
].
colId
);
buf
=
taosDecodeFixedI32
(
buf
,
&
pReq
->
ntbCfg
.
pSchema
[
i
].
bytes
);
buf
=
taosDecodeFixedI32
(
buf
,
&
pReq
->
ntbCfg
.
pSchema
[
i
].
bytes
);
buf
=
taosDecodeStringTo
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
name
);
buf
=
taosDecodeStringTo
(
buf
,
pReq
->
ntbCfg
.
pSchema
[
i
].
name
);
}
}
...
...
source/dnode/mgmt/container/inc/dndInt.h
浏览文件 @
10319e3c
...
@@ -56,7 +56,6 @@ void dndCleanupServer(SDnode *pDnode);
...
@@ -56,7 +56,6 @@ void dndCleanupServer(SDnode *pDnode);
int32_t
dndInitClient
(
SDnode
*
pDnode
);
int32_t
dndInitClient
(
SDnode
*
pDnode
);
void
dndCleanupClient
(
SDnode
*
pDnode
);
void
dndCleanupClient
(
SDnode
*
pDnode
);
int32_t
dndInitMsgHandle
(
SDnode
*
pDnode
);
int32_t
dndInitMsgHandle
(
SDnode
*
pDnode
);
void
dndSendRpcRsp
(
SMgmtWrapper
*
pWrapper
,
SRpcMsg
*
pRsp
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
source/dnode/mgmt/container/src/dndExec.c
浏览文件 @
10319e3c
...
@@ -20,7 +20,7 @@ static void dndResetLog(SMgmtWrapper *pMgmt) {
...
@@ -20,7 +20,7 @@ static void dndResetLog(SMgmtWrapper *pMgmt) {
char
logname
[
24
]
=
{
0
};
char
logname
[
24
]
=
{
0
};
snprintf
(
logname
,
sizeof
(
logname
),
"%slog"
,
pMgmt
->
name
);
snprintf
(
logname
,
sizeof
(
logname
),
"%slog"
,
pMgmt
->
name
);
dInfo
(
"node:%s, reset log to %s"
,
pMgmt
->
name
,
logname
);
dInfo
(
"node:%s, reset log to %s
in child process
"
,
pMgmt
->
name
,
logname
);
taosCloseLog
();
taosCloseLog
();
taosInitLog
(
logname
,
1
);
taosInitLog
(
logname
,
1
);
}
}
...
@@ -51,6 +51,7 @@ int32_t dndOpenNode(SMgmtWrapper *pWrapper) {
...
@@ -51,6 +51,7 @@ int32_t dndOpenNode(SMgmtWrapper *pWrapper) {
void
dndCloseNode
(
SMgmtWrapper
*
pWrapper
)
{
void
dndCloseNode
(
SMgmtWrapper
*
pWrapper
)
{
dDebug
(
"node:%s, start to close"
,
pWrapper
->
name
);
dDebug
(
"node:%s, start to close"
,
pWrapper
->
name
);
pWrapper
->
required
=
false
;
taosWLockLatch
(
&
pWrapper
->
latch
);
taosWLockLatch
(
&
pWrapper
->
latch
);
if
(
pWrapper
->
deployed
)
{
if
(
pWrapper
->
deployed
)
{
(
*
pWrapper
->
fp
.
closeFp
)(
pWrapper
);
(
*
pWrapper
->
fp
.
closeFp
)(
pWrapper
);
...
@@ -138,7 +139,7 @@ static void dndConsumeChildQueue(SMgmtWrapper *pWrapper, SNodeMsg *pMsg, int32_t
...
@@ -138,7 +139,7 @@ static void dndConsumeChildQueue(SMgmtWrapper *pWrapper, SNodeMsg *pMsg, int32_t
static
void
dndConsumeParentQueue
(
SMgmtWrapper
*
pWrapper
,
SRpcMsg
*
pRsp
,
int32_t
msgLen
,
void
*
pCont
,
int32_t
contLen
)
{
static
void
dndConsumeParentQueue
(
SMgmtWrapper
*
pWrapper
,
SRpcMsg
*
pRsp
,
int32_t
msgLen
,
void
*
pCont
,
int32_t
contLen
)
{
dTrace
(
"msg:%p, get from parent queue"
,
pRsp
);
dTrace
(
"msg:%p, get from parent queue"
,
pRsp
);
pRsp
->
pCont
=
pCont
;
pRsp
->
pCont
=
pCont
;
dndSendR
pcR
sp
(
pWrapper
,
pRsp
);
dndSendRsp
(
pWrapper
,
pRsp
);
taosMemoryFree
(
pRsp
);
taosMemoryFree
(
pRsp
);
}
}
...
@@ -178,7 +179,6 @@ static int32_t dndRunInMultiProcess(SDnode *pDnode) {
...
@@ -178,7 +179,6 @@ static int32_t dndRunInMultiProcess(SDnode *pDnode) {
.
parentFreeHeadFp
=
(
ProcFreeFp
)
taosMemoryFree
,
.
parentFreeHeadFp
=
(
ProcFreeFp
)
taosMemoryFree
,
.
parentMallocBodyFp
=
(
ProcMallocFp
)
rpcMallocCont
,
.
parentMallocBodyFp
=
(
ProcMallocFp
)
rpcMallocCont
,
.
parentFreeBodyFp
=
(
ProcFreeFp
)
rpcFreeCont
,
.
parentFreeBodyFp
=
(
ProcFreeFp
)
rpcFreeCont
,
.
testFlag
=
0
,
.
pParent
=
pWrapper
,
.
pParent
=
pWrapper
,
.
name
=
pWrapper
->
name
};
.
name
=
pWrapper
->
name
};
SProcObj
*
pProc
=
taosProcInit
(
&
cfg
);
SProcObj
*
pProc
=
taosProcInit
(
&
cfg
);
...
@@ -200,7 +200,7 @@ static int32_t dndRunInMultiProcess(SDnode *pDnode) {
...
@@ -200,7 +200,7 @@ static int32_t dndRunInMultiProcess(SDnode *pDnode) {
dInfo
(
"node:%s, will be initialized in child process"
,
pWrapper
->
name
);
dInfo
(
"node:%s, will be initialized in child process"
,
pWrapper
->
name
);
dndOpenNode
(
pWrapper
);
dndOpenNode
(
pWrapper
);
}
else
{
}
else
{
dInfo
(
"node:%s, will not start in parent process
"
,
pWrapper
->
name
);
dInfo
(
"node:%s, will not start in parent process
, child pid:%d"
,
pWrapper
->
name
,
taosProcChildId
(
pProc
)
);
pWrapper
->
procType
=
PROC_PARENT
;
pWrapper
->
procType
=
PROC_PARENT
;
}
}
...
@@ -210,16 +210,20 @@ static int32_t dndRunInMultiProcess(SDnode *pDnode) {
...
@@ -210,16 +210,20 @@ static int32_t dndRunInMultiProcess(SDnode *pDnode) {
}
}
}
}
#if 0
dndSetStatus
(
pDnode
,
DND_STAT_RUNNING
);
SMgmtWrapper *pWrapper = dndAcquireWrapper(pDnode, DNODE);
if (pWrapper->procType == PROC_PARENT && dmStart(pWrapper->pMgmt) != 0) {
for
(
ENodeType
n
=
0
;
n
<
NODE_MAX
;
++
n
)
{
dndReleaseWrapper(pWrapper);
SMgmtWrapper
*
pWrapper
=
&
pDnode
->
wrappers
[
n
];
dError("failed to start dnode worker since %s", terrstr());
if
(
!
pWrapper
->
required
)
continue
;
return -1;
if
(
pWrapper
->
fp
.
startFp
==
NULL
)
continue
;
if
(
pWrapper
->
procType
==
PROC_PARENT
&&
n
!=
DNODE
)
continue
;
if
(
pWrapper
->
procType
==
PROC_CHILD
&&
n
==
DNODE
)
continue
;
if
((
*
pWrapper
->
fp
.
startFp
)(
pWrapper
)
!=
0
)
{
dError
(
"node:%s, failed to start since %s"
,
pWrapper
->
name
,
terrstr
());
return
-
1
;
}
}
}
dndReleaseWrapper(pWrapper);
#endif
return
0
;
return
0
;
}
}
...
...
source/dnode/mgmt/container/src/dndFile.c
浏览文件 @
10319e3c
...
@@ -16,14 +16,16 @@
...
@@ -16,14 +16,16 @@
#define _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#include "dndInt.h"
#include "dndInt.h"
#define MAXLEN 1024
int32_t
dndReadFile
(
SMgmtWrapper
*
pWrapper
,
bool
*
pDeployed
)
{
int32_t
dndReadFile
(
SMgmtWrapper
*
pWrapper
,
bool
*
pDeployed
)
{
int32_t
code
=
TSDB_CODE_NODE_PARSE_FILE_ERROR
;
int32_t
code
=
TSDB_CODE_NODE_PARSE_FILE_ERROR
;
int32_t
len
=
0
;
int32_t
len
=
0
;
int32_t
maxLen
=
1024
;
const
int32_t
maxLen
=
MAXLEN
;
char
*
content
=
taosMemoryCalloc
(
1
,
maxLen
+
1
)
;
char
content
[
MAXLEN
+
1
]
=
{
0
}
;
cJSON
*
root
=
NULL
;
cJSON
*
root
=
NULL
;
char
file
[
PATH_MAX
];
char
file
[
PATH_MAX
];
TdFilePtr
pFile
=
NULL
;
TdFilePtr
pFile
=
NULL
;
snprintf
(
file
,
sizeof
(
file
),
"%s%s%s.json"
,
pWrapper
->
path
,
TD_DIRSEP
,
pWrapper
->
name
);
snprintf
(
file
,
sizeof
(
file
),
"%s%s%s.json"
,
pWrapper
->
path
,
TD_DIRSEP
,
pWrapper
->
name
);
pFile
=
taosOpenFile
(
file
,
TD_FILE_READ
);
pFile
=
taosOpenFile
(
file
,
TD_FILE_READ
);
...
@@ -57,7 +59,6 @@ int32_t dndReadFile(SMgmtWrapper *pWrapper, bool *pDeployed) {
...
@@ -57,7 +59,6 @@ int32_t dndReadFile(SMgmtWrapper *pWrapper, bool *pDeployed) {
dDebug
(
"succcessed to read file %s, deployed:%d"
,
file
,
*
pDeployed
);
dDebug
(
"succcessed to read file %s, deployed:%d"
,
file
,
*
pDeployed
);
_OVER:
_OVER:
if
(
content
!=
NULL
)
taosMemoryFree
(
content
);
if
(
root
!=
NULL
)
cJSON_Delete
(
root
);
if
(
root
!=
NULL
)
cJSON_Delete
(
root
);
if
(
pFile
!=
NULL
)
taosCloseFile
(
&
pFile
);
if
(
pFile
!=
NULL
)
taosCloseFile
(
&
pFile
);
...
@@ -66,7 +67,7 @@ _OVER:
...
@@ -66,7 +67,7 @@ _OVER:
}
}
int32_t
dndWriteFile
(
SMgmtWrapper
*
pWrapper
,
bool
deployed
)
{
int32_t
dndWriteFile
(
SMgmtWrapper
*
pWrapper
,
bool
deployed
)
{
char
file
[
PATH_MAX
];
char
file
[
PATH_MAX
]
=
{
0
}
;
snprintf
(
file
,
sizeof
(
file
),
"%s%s%s.json"
,
pWrapper
->
path
,
TD_DIRSEP
,
pWrapper
->
name
);
snprintf
(
file
,
sizeof
(
file
),
"%s%s%s.json"
,
pWrapper
->
path
,
TD_DIRSEP
,
pWrapper
->
name
);
TdFilePtr
pFile
=
taosOpenFile
(
file
,
TD_FILE_CTEATE
|
TD_FILE_WRITE
|
TD_FILE_TRUNC
);
TdFilePtr
pFile
=
taosOpenFile
(
file
,
TD_FILE_CTEATE
|
TD_FILE_WRITE
|
TD_FILE_TRUNC
);
...
@@ -76,9 +77,9 @@ int32_t dndWriteFile(SMgmtWrapper *pWrapper, bool deployed) {
...
@@ -76,9 +77,9 @@ int32_t dndWriteFile(SMgmtWrapper *pWrapper, bool deployed) {
return
-
1
;
return
-
1
;
}
}
int32_t
len
=
0
;
int32_t
len
=
0
;
int32_t
maxLen
=
1024
;
const
int32_t
maxLen
=
MAXLEN
;
char
*
content
=
taosMemoryCalloc
(
1
,
maxLen
+
1
)
;
char
content
[
MAXLEN
+
1
]
=
{
0
}
;
len
+=
snprintf
(
content
+
len
,
maxLen
-
len
,
"{
\n
"
);
len
+=
snprintf
(
content
+
len
,
maxLen
-
len
,
"{
\n
"
);
len
+=
snprintf
(
content
+
len
,
maxLen
-
len
,
"
\"
deployed
\"
: %d
\n
"
,
deployed
);
len
+=
snprintf
(
content
+
len
,
maxLen
-
len
,
"
\"
deployed
\"
: %d
\n
"
,
deployed
);
...
@@ -87,9 +88,8 @@ int32_t dndWriteFile(SMgmtWrapper *pWrapper, bool deployed) {
...
@@ -87,9 +88,8 @@ int32_t dndWriteFile(SMgmtWrapper *pWrapper, bool deployed) {
taosWriteFile
(
pFile
,
content
,
len
);
taosWriteFile
(
pFile
,
content
,
len
);
taosFsyncFile
(
pFile
);
taosFsyncFile
(
pFile
);
taosCloseFile
(
&
pFile
);
taosCloseFile
(
&
pFile
);
taosMemoryFree
(
content
);
char
realfile
[
PATH_MAX
];
char
realfile
[
PATH_MAX
]
=
{
0
}
;
snprintf
(
realfile
,
sizeof
(
realfile
),
"%s%s%s.json"
,
pWrapper
->
path
,
TD_DIRSEP
,
pWrapper
->
name
);
snprintf
(
realfile
,
sizeof
(
realfile
),
"%s%s%s.json"
,
pWrapper
->
path
,
TD_DIRSEP
,
pWrapper
->
name
);
if
(
taosRenameFile
(
file
,
realfile
)
!=
0
)
{
if
(
taosRenameFile
(
file
,
realfile
)
!=
0
)
{
...
...
source/dnode/mgmt/container/src/dndMsg.c
浏览文件 @
10319e3c
...
@@ -43,36 +43,40 @@ static inline int32_t dndBuildMsg(SNodeMsg *pMsg, SRpcMsg *pRpc) {
...
@@ -43,36 +43,40 @@ static inline int32_t dndBuildMsg(SNodeMsg *pMsg, SRpcMsg *pRpc) {
memcpy
(
pMsg
->
user
,
connInfo
.
user
,
TSDB_USER_LEN
);
memcpy
(
pMsg
->
user
,
connInfo
.
user
,
TSDB_USER_LEN
);
memcpy
(
&
pMsg
->
rpcMsg
,
pRpc
,
sizeof
(
SRpcMsg
));
memcpy
(
&
pMsg
->
rpcMsg
,
pRpc
,
sizeof
(
SRpcMsg
));
return
0
;
return
0
;
}
}
void
dndProcessRpcMsg
(
SMgmtWrapper
*
pWrapper
,
SRpcMsg
*
pRpc
,
SEpSet
*
pEpSet
)
{
void
dndProcessRpcMsg
(
SMgmtWrapper
*
pWrapper
,
SRpcMsg
*
pRpc
,
SEpSet
*
pEpSet
)
{
if
(
pEpSet
&&
pEpSet
->
numOfEps
>
0
&&
pRpc
->
msgType
==
TDMT_MND_STATUS_RSP
)
{
dndUpdateMnodeEpSet
(
pWrapper
->
pDnode
,
pEpSet
);
}
int32_t
code
=
-
1
;
int32_t
code
=
-
1
;
SNodeMsg
*
pMsg
=
NULL
;
SNodeMsg
*
pMsg
=
NULL
;
NodeMsgFp
msgFp
=
NULL
;
NodeMsgFp
msgFp
=
NULL
;
if
(
pEpSet
&&
pEpSet
->
numOfEps
>
0
&&
pRpc
->
msgType
==
TDMT_MND_STATUS_RSP
)
{
dndUpdateMnodeEpSet
(
pWrapper
->
pDnode
,
pEpSet
);
}
if
(
dndMarkWrapper
(
pWrapper
)
!=
0
)
goto
_OVER
;
if
(
dndMarkWrapper
(
pWrapper
)
!=
0
)
goto
_OVER
;
if
((
msgFp
=
dndGetMsgFp
(
pWrapper
,
pRpc
))
==
NULL
)
goto
_OVER
;
if
((
msgFp
=
dndGetMsgFp
(
pWrapper
,
pRpc
))
==
NULL
)
goto
_OVER
;
if
((
pMsg
=
taosAllocateQitem
(
sizeof
(
SNodeMsg
)))
==
NULL
)
goto
_OVER
;
if
((
pMsg
=
taosAllocateQitem
(
sizeof
(
SNodeMsg
)))
==
NULL
)
goto
_OVER
;
if
(
dndBuildMsg
(
pMsg
,
pRpc
)
!=
0
)
goto
_OVER
;
if
(
dndBuildMsg
(
pMsg
,
pRpc
)
!=
0
)
goto
_OVER
;
dTrace
(
"msg:%p, is created, handle:%p app:%p user:%s"
,
pMsg
,
pRpc
->
handle
,
pRpc
->
ahandle
,
pMsg
->
user
);
if
(
pWrapper
->
procType
==
PROC_SINGLE
)
{
if
(
pWrapper
->
procType
==
PROC_SINGLE
)
{
dTrace
(
"msg:%p, is created, handle:%p app:%p user:%s"
,
pMsg
,
pRpc
->
handle
,
pRpc
->
ahandle
,
pMsg
->
user
);
code
=
(
*
msgFp
)(
pWrapper
->
pMgmt
,
pMsg
);
code
=
(
*
msgFp
)(
pWrapper
->
pMgmt
,
pMsg
);
}
else
if
(
pWrapper
->
procType
==
PROC_PARENT
)
{
}
else
if
(
pWrapper
->
procType
==
PROC_PARENT
)
{
dTrace
(
"msg:%p, is created and will put into child queue, handle:%p app:%p user:%s"
,
pMsg
,
pRpc
->
handle
,
pRpc
->
ahandle
,
pMsg
->
user
);
code
=
taosProcPutToChildQueue
(
pWrapper
->
pProc
,
pMsg
,
sizeof
(
SNodeMsg
),
pRpc
->
pCont
,
pRpc
->
contLen
);
code
=
taosProcPutToChildQueue
(
pWrapper
->
pProc
,
pMsg
,
sizeof
(
SNodeMsg
),
pRpc
->
pCont
,
pRpc
->
contLen
);
}
else
{
}
else
{
dTrace
(
"msg:%p, should not processed in child process, handle:%p app:%p user:%s"
,
pMsg
,
pRpc
->
handle
,
pRpc
->
ahandle
,
pMsg
->
user
);
ASSERT
(
1
);
}
}
_OVER:
_OVER:
if
(
code
==
0
)
{
if
(
code
==
0
)
{
if
(
pWrapper
->
procType
==
PROC_PARENT
)
{
if
(
pWrapper
->
procType
==
PROC_PARENT
)
{
dTrace
(
"msg:%p, is freed"
,
pMsg
);
dTrace
(
"msg:%p, is freed
in parent process
"
,
pMsg
);
taosFreeQitem
(
pMsg
);
taosFreeQitem
(
pMsg
);
rpcFreeCont
(
pRpc
->
pCont
);
rpcFreeCont
(
pRpc
->
pCont
);
}
}
...
...
source/dnode/mgmt/container/src/dndObj.c
浏览文件 @
10319e3c
...
@@ -175,7 +175,7 @@ int32_t dndMarkWrapper(SMgmtWrapper *pWrapper) {
...
@@ -175,7 +175,7 @@ int32_t dndMarkWrapper(SMgmtWrapper *pWrapper) {
int32_t
code
=
0
;
int32_t
code
=
0
;
taosRLockLatch
(
&
pWrapper
->
latch
);
taosRLockLatch
(
&
pWrapper
->
latch
);
if
(
pWrapper
->
deployed
)
{
if
(
pWrapper
->
deployed
||
(
pWrapper
->
procType
==
PROC_PARENT
&&
pWrapper
->
required
)
)
{
int32_t
refCount
=
atomic_add_fetch_32
(
&
pWrapper
->
refCount
,
1
);
int32_t
refCount
=
atomic_add_fetch_32
(
&
pWrapper
->
refCount
,
1
);
dTrace
(
"node:%s, is marked, refCount:%d"
,
pWrapper
->
name
,
refCount
);
dTrace
(
"node:%s, is marked, refCount:%d"
,
pWrapper
->
name
,
refCount
);
}
else
{
}
else
{
...
...
source/dnode/mgmt/container/src/dndTransport.c
浏览文件 @
10319e3c
...
@@ -348,7 +348,7 @@ int32_t dndSendReqToMnode(SMgmtWrapper *pWrapper, SRpcMsg *pReq) {
...
@@ -348,7 +348,7 @@ int32_t dndSendReqToMnode(SMgmtWrapper *pWrapper, SRpcMsg *pReq) {
}
}
}
}
void
dndSendRpcRsp
(
SMgmtWrapper
*
pWrapper
,
SRpcMsg
*
pRsp
)
{
static
void
dndSendRpcRsp
(
SMgmtWrapper
*
pWrapper
,
SRpcMsg
*
pRsp
)
{
if
(
pRsp
->
code
==
TSDB_CODE_APP_NOT_READY
)
{
if
(
pRsp
->
code
==
TSDB_CODE_APP_NOT_READY
)
{
SMgmtWrapper
*
pDnodeWrapper
=
dndAcquireWrapper
(
pWrapper
->
pDnode
,
DNODE
);
SMgmtWrapper
*
pDnodeWrapper
=
dndAcquireWrapper
(
pWrapper
->
pDnode
,
DNODE
);
if
(
pDnodeWrapper
!=
NULL
)
{
if
(
pDnodeWrapper
!=
NULL
)
{
...
...
source/dnode/mnode/impl/src/mndStb.c
浏览文件 @
10319e3c
...
@@ -98,7 +98,7 @@ SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
...
@@ -98,7 +98,7 @@ SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfColumns
;
++
i
)
{
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfColumns
;
++
i
)
{
SSchema
*
pSchema
=
&
pStb
->
pColumns
[
i
];
SSchema
*
pSchema
=
&
pStb
->
pColumns
[
i
];
SDB_SET_INT8
(
pRaw
,
dataPos
,
pSchema
->
type
,
STB_ENCODE_OVER
)
SDB_SET_INT8
(
pRaw
,
dataPos
,
pSchema
->
type
,
STB_ENCODE_OVER
)
SDB_SET_INT
32
(
pRaw
,
dataPos
,
pSchema
->
colId
,
STB_ENCODE_OVER
)
SDB_SET_INT
16
(
pRaw
,
dataPos
,
pSchema
->
colId
,
STB_ENCODE_OVER
)
SDB_SET_INT32
(
pRaw
,
dataPos
,
pSchema
->
bytes
,
STB_ENCODE_OVER
)
SDB_SET_INT32
(
pRaw
,
dataPos
,
pSchema
->
bytes
,
STB_ENCODE_OVER
)
SDB_SET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_ENCODE_OVER
)
SDB_SET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_ENCODE_OVER
)
}
}
...
@@ -106,7 +106,7 @@ SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
...
@@ -106,7 +106,7 @@ SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfTags
;
++
i
)
{
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfTags
;
++
i
)
{
SSchema
*
pSchema
=
&
pStb
->
pTags
[
i
];
SSchema
*
pSchema
=
&
pStb
->
pTags
[
i
];
SDB_SET_INT8
(
pRaw
,
dataPos
,
pSchema
->
type
,
STB_ENCODE_OVER
)
SDB_SET_INT8
(
pRaw
,
dataPos
,
pSchema
->
type
,
STB_ENCODE_OVER
)
SDB_SET_INT
32
(
pRaw
,
dataPos
,
pSchema
->
colId
,
STB_ENCODE_OVER
)
SDB_SET_INT
16
(
pRaw
,
dataPos
,
pSchema
->
colId
,
STB_ENCODE_OVER
)
SDB_SET_INT32
(
pRaw
,
dataPos
,
pSchema
->
bytes
,
STB_ENCODE_OVER
)
SDB_SET_INT32
(
pRaw
,
dataPos
,
pSchema
->
bytes
,
STB_ENCODE_OVER
)
SDB_SET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_ENCODE_OVER
)
SDB_SET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_ENCODE_OVER
)
}
}
...
@@ -114,7 +114,7 @@ SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
...
@@ -114,7 +114,7 @@ SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfSmas
;
++
i
)
{
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfSmas
;
++
i
)
{
SSchema
*
pSchema
=
&
pStb
->
pSmas
[
i
];
SSchema
*
pSchema
=
&
pStb
->
pSmas
[
i
];
SDB_SET_INT8
(
pRaw
,
dataPos
,
pSchema
->
type
,
STB_ENCODE_OVER
)
SDB_SET_INT8
(
pRaw
,
dataPos
,
pSchema
->
type
,
STB_ENCODE_OVER
)
SDB_SET_INT
32
(
pRaw
,
dataPos
,
pSchema
->
colId
,
STB_ENCODE_OVER
)
SDB_SET_INT
16
(
pRaw
,
dataPos
,
pSchema
->
colId
,
STB_ENCODE_OVER
)
SDB_SET_INT32
(
pRaw
,
dataPos
,
pSchema
->
bytes
,
STB_ENCODE_OVER
)
SDB_SET_INT32
(
pRaw
,
dataPos
,
pSchema
->
bytes
,
STB_ENCODE_OVER
)
SDB_SET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_ENCODE_OVER
)
SDB_SET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_ENCODE_OVER
)
}
}
...
@@ -185,7 +185,7 @@ static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
...
@@ -185,7 +185,7 @@ static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfColumns
;
++
i
)
{
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfColumns
;
++
i
)
{
SSchema
*
pSchema
=
&
pStb
->
pColumns
[
i
];
SSchema
*
pSchema
=
&
pStb
->
pColumns
[
i
];
SDB_GET_INT8
(
pRaw
,
dataPos
,
&
pSchema
->
type
,
STB_DECODE_OVER
)
SDB_GET_INT8
(
pRaw
,
dataPos
,
&
pSchema
->
type
,
STB_DECODE_OVER
)
SDB_GET_INT
32
(
pRaw
,
dataPos
,
&
pSchema
->
colId
,
STB_DECODE_OVER
)
SDB_GET_INT
16
(
pRaw
,
dataPos
,
&
pSchema
->
colId
,
STB_DECODE_OVER
)
SDB_GET_INT32
(
pRaw
,
dataPos
,
&
pSchema
->
bytes
,
STB_DECODE_OVER
)
SDB_GET_INT32
(
pRaw
,
dataPos
,
&
pSchema
->
bytes
,
STB_DECODE_OVER
)
SDB_GET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_DECODE_OVER
)
SDB_GET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_DECODE_OVER
)
}
}
...
@@ -193,7 +193,7 @@ static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
...
@@ -193,7 +193,7 @@ static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfTags
;
++
i
)
{
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfTags
;
++
i
)
{
SSchema
*
pSchema
=
&
pStb
->
pTags
[
i
];
SSchema
*
pSchema
=
&
pStb
->
pTags
[
i
];
SDB_GET_INT8
(
pRaw
,
dataPos
,
&
pSchema
->
type
,
STB_DECODE_OVER
)
SDB_GET_INT8
(
pRaw
,
dataPos
,
&
pSchema
->
type
,
STB_DECODE_OVER
)
SDB_GET_INT
32
(
pRaw
,
dataPos
,
&
pSchema
->
colId
,
STB_DECODE_OVER
)
SDB_GET_INT
16
(
pRaw
,
dataPos
,
&
pSchema
->
colId
,
STB_DECODE_OVER
)
SDB_GET_INT32
(
pRaw
,
dataPos
,
&
pSchema
->
bytes
,
STB_DECODE_OVER
)
SDB_GET_INT32
(
pRaw
,
dataPos
,
&
pSchema
->
bytes
,
STB_DECODE_OVER
)
SDB_GET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_DECODE_OVER
)
SDB_GET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_DECODE_OVER
)
}
}
...
@@ -201,7 +201,7 @@ static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
...
@@ -201,7 +201,7 @@ static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfSmas
;
++
i
)
{
for
(
int32_t
i
=
0
;
i
<
pStb
->
numOfSmas
;
++
i
)
{
SSchema
*
pSchema
=
&
pStb
->
pSmas
[
i
];
SSchema
*
pSchema
=
&
pStb
->
pSmas
[
i
];
SDB_GET_INT8
(
pRaw
,
dataPos
,
&
pSchema
->
type
,
STB_DECODE_OVER
)
SDB_GET_INT8
(
pRaw
,
dataPos
,
&
pSchema
->
type
,
STB_DECODE_OVER
)
SDB_GET_INT
32
(
pRaw
,
dataPos
,
&
pSchema
->
colId
,
STB_DECODE_OVER
)
SDB_GET_INT
16
(
pRaw
,
dataPos
,
&
pSchema
->
colId
,
STB_DECODE_OVER
)
SDB_GET_INT32
(
pRaw
,
dataPos
,
&
pSchema
->
bytes
,
STB_DECODE_OVER
)
SDB_GET_INT32
(
pRaw
,
dataPos
,
&
pSchema
->
bytes
,
STB_DECODE_OVER
)
SDB_GET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_DECODE_OVER
)
SDB_GET_BINARY
(
pRaw
,
dataPos
,
pSchema
->
name
,
TSDB_COL_NAME_LEN
,
STB_DECODE_OVER
)
}
}
...
...
source/dnode/mnode/impl/src/mndUser.c
浏览文件 @
10319e3c
...
@@ -21,7 +21,7 @@
...
@@ -21,7 +21,7 @@
#include "mndTrans.h"
#include "mndTrans.h"
#include "tbase64.h"
#include "tbase64.h"
#define TSDB_USER_VER_NUMBER 1
#define TSDB_USER_VER_NUMBER
1
#define TSDB_USER_RESERVE_SIZE 64
#define TSDB_USER_RESERVE_SIZE 64
static
int32_t
mndCreateDefaultUsers
(
SMnode
*
pMnode
);
static
int32_t
mndCreateDefaultUsers
(
SMnode
*
pMnode
);
...
@@ -270,7 +270,7 @@ static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate
...
@@ -270,7 +270,7 @@ static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate
userObj
.
updateTime
=
userObj
.
createdTime
;
userObj
.
updateTime
=
userObj
.
createdTime
;
userObj
.
superUser
=
pCreate
->
superUser
;
userObj
.
superUser
=
pCreate
->
superUser
;
STrans
*
pTrans
=
mndTransCreate
(
pMnode
,
TRN_POLICY_ROLLBACK
,
TRN_TYPE_CREATE_USER
,
&
pReq
->
rpcMsg
);
STrans
*
pTrans
=
mndTransCreate
(
pMnode
,
TRN_POLICY_ROLLBACK
,
TRN_TYPE_CREATE_USER
,
&
pReq
->
rpcMsg
);
if
(
pTrans
==
NULL
)
{
if
(
pTrans
==
NULL
)
{
mError
(
"user:%s, failed to create since %s"
,
pCreate
->
user
,
terrstr
());
mError
(
"user:%s, failed to create since %s"
,
pCreate
->
user
,
terrstr
());
return
-
1
;
return
-
1
;
...
@@ -350,7 +350,7 @@ CREATE_USER_OVER:
...
@@ -350,7 +350,7 @@ CREATE_USER_OVER:
}
}
static
int32_t
mndUpdateUser
(
SMnode
*
pMnode
,
SUserObj
*
pOld
,
SUserObj
*
pNew
,
SNodeMsg
*
pReq
)
{
static
int32_t
mndUpdateUser
(
SMnode
*
pMnode
,
SUserObj
*
pOld
,
SUserObj
*
pNew
,
SNodeMsg
*
pReq
)
{
STrans
*
pTrans
=
mndTransCreate
(
pMnode
,
TRN_POLICY_ROLLBACK
,
TRN_TYPE_ALTER_USER
,
&
pReq
->
rpcMsg
);
STrans
*
pTrans
=
mndTransCreate
(
pMnode
,
TRN_POLICY_ROLLBACK
,
TRN_TYPE_ALTER_USER
,
&
pReq
->
rpcMsg
);
if
(
pTrans
==
NULL
)
{
if
(
pTrans
==
NULL
)
{
mError
(
"user:%s, failed to update since %s"
,
pOld
->
user
,
terrstr
());
mError
(
"user:%s, failed to update since %s"
,
pOld
->
user
,
terrstr
());
return
-
1
;
return
-
1
;
...
@@ -511,7 +511,7 @@ ALTER_USER_OVER:
...
@@ -511,7 +511,7 @@ ALTER_USER_OVER:
}
}
static
int32_t
mndDropUser
(
SMnode
*
pMnode
,
SNodeMsg
*
pReq
,
SUserObj
*
pUser
)
{
static
int32_t
mndDropUser
(
SMnode
*
pMnode
,
SNodeMsg
*
pReq
,
SUserObj
*
pUser
)
{
STrans
*
pTrans
=
mndTransCreate
(
pMnode
,
TRN_POLICY_ROLLBACK
,
TRN_TYPE_DROP_USER
,
&
pReq
->
rpcMsg
);
STrans
*
pTrans
=
mndTransCreate
(
pMnode
,
TRN_POLICY_ROLLBACK
,
TRN_TYPE_DROP_USER
,
&
pReq
->
rpcMsg
);
if
(
pTrans
==
NULL
)
{
if
(
pTrans
==
NULL
)
{
mError
(
"user:%s, failed to drop since %s"
,
pUser
->
user
,
terrstr
());
mError
(
"user:%s, failed to drop since %s"
,
pUser
->
user
,
terrstr
());
return
-
1
;
return
-
1
;
...
...
source/dnode/vnode/src/meta/metaBDBImpl.c
浏览文件 @
10319e3c
...
@@ -233,7 +233,7 @@ int metaSaveSmaToDB(SMeta *pMeta, STSma *pSmaCfg) {
...
@@ -233,7 +233,7 @@ int metaSaveSmaToDB(SMeta *pMeta, STSma *pSmaCfg) {
// save sma info
// save sma info
int32_t
len
=
tEncodeTSma
(
NULL
,
pSmaCfg
);
int32_t
len
=
tEncodeTSma
(
NULL
,
pSmaCfg
);
pBuf
=
taosMemoryCalloc
(
len
,
1
);
pBuf
=
taosMemoryCalloc
(
1
,
len
);
if
(
pBuf
==
NULL
)
{
if
(
pBuf
==
NULL
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
-
1
;
return
-
1
;
...
@@ -285,7 +285,7 @@ static int metaEncodeSchema(void **buf, SSchemaWrapper *pSW) {
...
@@ -285,7 +285,7 @@ static int metaEncodeSchema(void **buf, SSchemaWrapper *pSW) {
for
(
int
i
=
0
;
i
<
pSW
->
nCols
;
i
++
)
{
for
(
int
i
=
0
;
i
<
pSW
->
nCols
;
i
++
)
{
pSchema
=
pSW
->
pSchema
+
i
;
pSchema
=
pSW
->
pSchema
+
i
;
tlen
+=
taosEncodeFixedI8
(
buf
,
pSchema
->
type
);
tlen
+=
taosEncodeFixedI8
(
buf
,
pSchema
->
type
);
tlen
+=
taosEncodeFixedI
32
(
buf
,
pSchema
->
colId
);
tlen
+=
taosEncodeFixedI
16
(
buf
,
pSchema
->
colId
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pSchema
->
bytes
);
tlen
+=
taosEncodeFixedI32
(
buf
,
pSchema
->
bytes
);
tlen
+=
taosEncodeString
(
buf
,
pSchema
->
name
);
tlen
+=
taosEncodeString
(
buf
,
pSchema
->
name
);
}
}
...
@@ -301,7 +301,7 @@ static void *metaDecodeSchema(void *buf, SSchemaWrapper *pSW) {
...
@@ -301,7 +301,7 @@ static void *metaDecodeSchema(void *buf, SSchemaWrapper *pSW) {
for
(
int
i
=
0
;
i
<
pSW
->
nCols
;
i
++
)
{
for
(
int
i
=
0
;
i
<
pSW
->
nCols
;
i
++
)
{
pSchema
=
pSW
->
pSchema
+
i
;
pSchema
=
pSW
->
pSchema
+
i
;
buf
=
taosDecodeFixedI8
(
buf
,
&
pSchema
->
type
);
buf
=
taosDecodeFixedI8
(
buf
,
&
pSchema
->
type
);
buf
=
taosDecodeFixedI
32
(
buf
,
&
pSchema
->
colId
);
buf
=
taosDecodeFixedI
16
(
buf
,
&
pSchema
->
colId
);
buf
=
taosDecodeFixedI32
(
buf
,
&
pSchema
->
bytes
);
buf
=
taosDecodeFixedI32
(
buf
,
&
pSchema
->
bytes
);
buf
=
taosDecodeStringTo
(
buf
,
pSchema
->
name
);
buf
=
taosDecodeStringTo
(
buf
,
pSchema
->
name
);
}
}
...
@@ -516,6 +516,7 @@ static int metaEncodeTbInfo(void **buf, STbCfg *pTbCfg) {
...
@@ -516,6 +516,7 @@ static int metaEncodeTbInfo(void **buf, STbCfg *pTbCfg) {
tsize
+=
taosEncodeFixedU64
(
buf
,
pTbCfg
->
ctbCfg
.
suid
);
tsize
+=
taosEncodeFixedU64
(
buf
,
pTbCfg
->
ctbCfg
.
suid
);
tsize
+=
tdEncodeKVRow
(
buf
,
pTbCfg
->
ctbCfg
.
pTag
);
tsize
+=
tdEncodeKVRow
(
buf
,
pTbCfg
->
ctbCfg
.
pTag
);
}
else
if
(
pTbCfg
->
type
==
META_NORMAL_TABLE
)
{
}
else
if
(
pTbCfg
->
type
==
META_NORMAL_TABLE
)
{
// TODO
}
else
{
}
else
{
ASSERT
(
0
);
ASSERT
(
0
);
}
}
...
@@ -538,6 +539,7 @@ static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) {
...
@@ -538,6 +539,7 @@ static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) {
buf
=
taosDecodeFixedU64
(
buf
,
&
(
pTbCfg
->
ctbCfg
.
suid
));
buf
=
taosDecodeFixedU64
(
buf
,
&
(
pTbCfg
->
ctbCfg
.
suid
));
buf
=
tdDecodeKVRow
(
buf
,
&
(
pTbCfg
->
ctbCfg
.
pTag
));
buf
=
tdDecodeKVRow
(
buf
,
&
(
pTbCfg
->
ctbCfg
.
pTag
));
}
else
if
(
pTbCfg
->
type
==
META_NORMAL_TABLE
)
{
}
else
if
(
pTbCfg
->
type
==
META_NORMAL_TABLE
)
{
// TODO
}
else
{
}
else
{
ASSERT
(
0
);
ASSERT
(
0
);
}
}
...
...
source/dnode/vnode/src/tq/tqRead.c
浏览文件 @
10319e3c
...
@@ -130,8 +130,8 @@ SArray* tqRetrieveDataBlock(STqReadHandle* pHandle) {
...
@@ -130,8 +130,8 @@ SArray* tqRetrieveDataBlock(STqReadHandle* pHandle) {
int32_t
colNeed
=
0
;
int32_t
colNeed
=
0
;
while
(
colMeta
<
pSchemaWrapper
->
nCols
&&
colNeed
<
colNumNeed
)
{
while
(
colMeta
<
pSchemaWrapper
->
nCols
&&
colNeed
<
colNumNeed
)
{
SSchema
*
pColSchema
=
&
pSchemaWrapper
->
pSchema
[
colMeta
];
SSchema
*
pColSchema
=
&
pSchemaWrapper
->
pSchema
[
colMeta
];
int16_t
colIdSchema
=
pColSchema
->
colId
;
col_id_t
colIdSchema
=
pColSchema
->
colId
;
int16_t
colIdNeed
=
*
(
int16
_t
*
)
taosArrayGet
(
pHandle
->
pColIdList
,
colNeed
);
col_id_t
colIdNeed
=
*
(
col_id
_t
*
)
taosArrayGet
(
pHandle
->
pColIdList
,
colNeed
);
if
(
colIdSchema
<
colIdNeed
)
{
if
(
colIdSchema
<
colIdNeed
)
{
colMeta
++
;
colMeta
++
;
}
else
if
(
colIdSchema
>
colIdNeed
)
{
}
else
if
(
colIdSchema
>
colIdNeed
)
{
...
@@ -159,7 +159,7 @@ SArray* tqRetrieveDataBlock(STqReadHandle* pHandle) {
...
@@ -159,7 +159,7 @@ SArray* tqRetrieveDataBlock(STqReadHandle* pHandle) {
int
j
=
0
;
int
j
=
0
;
for
(
int32_t
i
=
0
;
i
<
colNumNeed
;
i
++
)
{
for
(
int32_t
i
=
0
;
i
<
colNumNeed
;
i
++
)
{
int16_t
colId
=
*
(
int16
_t
*
)
taosArrayGet
(
pHandle
->
pColIdList
,
i
);
col_id_t
colId
=
*
(
col_id
_t
*
)
taosArrayGet
(
pHandle
->
pColIdList
,
i
);
while
(
j
<
pSchemaWrapper
->
nCols
&&
pSchemaWrapper
->
pSchema
[
j
].
colId
<
colId
)
{
while
(
j
<
pSchemaWrapper
->
nCols
&&
pSchemaWrapper
->
pSchema
[
j
].
colId
<
colId
)
{
j
++
;
j
++
;
}
}
...
...
source/dnode/vnode/src/tsdb/tsdbCommit.c
浏览文件 @
10319e3c
...
@@ -1369,7 +1369,7 @@ int tsdbWriteBlockImpl(STsdb *pRepo, STable *pTable, SDFile *pDFile, SDFile *pDF
...
@@ -1369,7 +1369,7 @@ int tsdbWriteBlockImpl(STsdb *pRepo, STable *pTable, SDFile *pDFile, SDFile *pDF
}
}
}
}
// Update pBlock membership va
ir
ables
// Update pBlock membership va
ri
ables
pBlock
->
last
=
isLast
;
pBlock
->
last
=
isLast
;
pBlock
->
offset
=
offset
;
pBlock
->
offset
=
offset
;
pBlock
->
algorithm
=
pCfg
->
compression
;
pBlock
->
algorithm
=
pCfg
->
compression
;
...
...
source/dnode/vnode/src/tsdb/tsdbMemTable.c
浏览文件 @
10319e3c
...
@@ -255,7 +255,7 @@ int32_t tdScanAndConvertSubmitMsg(SSubmitReq *pMsg) {
...
@@ -255,7 +255,7 @@ int32_t tdScanAndConvertSubmitMsg(SSubmitReq *pMsg) {
return
0
;
return
0
;
}
}
int
tsdbScanAndConvertSubmitMsg
(
STsdb
*
pTsdb
,
SSubmitReq
*
pMsg
)
{
static
int
tsdbScanAndConvertSubmitMsg
(
STsdb
*
pTsdb
,
SSubmitReq
*
pMsg
)
{
ASSERT
(
pMsg
!=
NULL
);
ASSERT
(
pMsg
!=
NULL
);
// STsdbMeta * pMeta = pTsdb->tsdbMeta;
// STsdbMeta * pMeta = pTsdb->tsdbMeta;
SSubmitMsgIter
msgIter
=
{
0
};
SSubmitMsgIter
msgIter
=
{
0
};
...
...
source/dnode/vnode/src/tsdb/tsdbSma.c
浏览文件 @
10319e3c
...
@@ -38,9 +38,10 @@ typedef enum {
...
@@ -38,9 +38,10 @@ typedef enum {
}
ESmaStorageLevel
;
}
ESmaStorageLevel
;
typedef
struct
{
typedef
struct
{
STsdb
*
pTsdb
;
STsdb
*
pTsdb
;
SDBFile
dFile
;
SDBFile
dFile
;
int32_t
interval
;
// interval with the precision of DB
SSDataBlock
*
pData
;
// sma data
int32_t
interval
;
// interval with the precision of DB
}
STSmaWriteH
;
}
STSmaWriteH
;
typedef
struct
{
typedef
struct
{
...
@@ -98,7 +99,8 @@ static int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSmaDataWrapper *pData, int64_
...
@@ -98,7 +99,8 @@ static int32_t tsdbGetTSmaDataImpl(STsdb *pTsdb, STSmaDataWrapper *pData, int64_
int32_t
nMaxResult
);
int32_t
nMaxResult
);
// insert data
// insert data
static
int32_t
tsdbInitTSmaWriteH
(
STSmaWriteH
*
pSmaH
,
STsdb
*
pTsdb
,
STSmaDataWrapper
*
pData
);
static
int32_t
tsdbInitTSmaWriteH
(
STSmaWriteH
*
pSmaH
,
STsdb
*
pTsdb
,
SSDataBlock
*
pData
,
int64_t
interval
,
int8_t
intervalUnit
);
static
void
tsdbDestroyTSmaWriteH
(
STSmaWriteH
*
pSmaH
);
static
void
tsdbDestroyTSmaWriteH
(
STSmaWriteH
*
pSmaH
);
static
int32_t
tsdbInitTSmaReadH
(
STSmaReadH
*
pSmaH
,
STsdb
*
pTsdb
,
int64_t
interval
,
int8_t
intervalUnit
);
static
int32_t
tsdbInitTSmaReadH
(
STSmaReadH
*
pSmaH
,
STsdb
*
pTsdb
,
int64_t
interval
,
int8_t
intervalUnit
);
static
int32_t
tsdbGetSmaStorageLevel
(
int64_t
interval
,
int8_t
intervalUnit
);
static
int32_t
tsdbGetSmaStorageLevel
(
int64_t
interval
,
int8_t
intervalUnit
);
...
@@ -800,9 +802,10 @@ static int32_t tsdbInsertTSmaDataSection(STSmaWriteH *pSmaH, STSmaDataWrapper *p
...
@@ -800,9 +802,10 @@ static int32_t tsdbInsertTSmaDataSection(STSmaWriteH *pSmaH, STSmaDataWrapper *p
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
}
}
static
int32_t
tsdbInitTSmaWriteH
(
STSmaWriteH
*
pSmaH
,
STsdb
*
pTsdb
,
S
TSmaDataWrapper
*
pData
)
{
static
int32_t
tsdbInitTSmaWriteH
(
STSmaWriteH
*
pSmaH
,
STsdb
*
pTsdb
,
S
SDataBlock
*
pData
,
int64_t
interval
,
int8_t
intervalUnit
)
{
pSmaH
->
pTsdb
=
pTsdb
;
pSmaH
->
pTsdb
=
pTsdb
;
pSmaH
->
interval
=
tsdbGetIntervalByPrecision
(
pData
->
interval
,
pData
->
intervalUnit
,
REPO_CFG
(
pTsdb
)
->
precision
);
pSmaH
->
interval
=
tsdbGetIntervalByPrecision
(
interval
,
intervalUnit
,
REPO_CFG
(
pTsdb
)
->
precision
);
pSmaH
->
pData
=
pData
;
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
}
}
...
@@ -857,10 +860,10 @@ static int32_t tsdbGetTSmaDays(STsdb *pTsdb, int64_t interval, int32_t storageLe
...
@@ -857,10 +860,10 @@ static int32_t tsdbGetTSmaDays(STsdb *pTsdb, int64_t interval, int32_t storageLe
* @return int32_t
* @return int32_t
*/
*/
static
int32_t
tsdbInsertTSmaDataImpl
(
STsdb
*
pTsdb
,
char
*
msg
)
{
static
int32_t
tsdbInsertTSmaDataImpl
(
STsdb
*
pTsdb
,
char
*
msg
)
{
STsdbCfg
*
pCfg
=
REPO_CFG
(
pTsdb
);
STsdbCfg
*
pCfg
=
REPO_CFG
(
pTsdb
);
S
TSmaDataWrapper
*
pData
=
(
STSmaDataWrapper
*
)
msg
;
S
SDataBlock
*
pData
=
(
SSDataBlock
*
)
msg
;
SSmaEnv
*
pEnv
=
atomic_load_ptr
(
&
pTsdb
->
pTSmaEnv
);
SSmaEnv
*
pEnv
=
atomic_load_ptr
(
&
pTsdb
->
pTSmaEnv
);
int64_t
indexUid
=
SMA_TEST_INDEX_UID
;
int64_t
indexUid
=
SMA_TEST_INDEX_UID
;
if
(
pEnv
==
NULL
)
{
if
(
pEnv
==
NULL
)
{
terrno
=
TSDB_CODE_INVALID_PTR
;
terrno
=
TSDB_CODE_INVALID_PTR
;
...
@@ -868,15 +871,15 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) {
...
@@ -868,15 +871,15 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) {
return
terrno
;
return
terrno
;
}
}
if
(
pData
->
dataLen
<=
0
)
{
if
(
pData
==
NULL
)
{
TASSERT
(
0
)
;
terrno
=
TSDB_CODE_INVALID_PTR
;
t
errno
=
TSDB_CODE_INVALID_PARA
;
t
sdbWarn
(
"vgId:%d insert tSma data failed since pData is NULL"
,
REPO_ID
(
pTsdb
))
;
return
TSDB_CODE_FAILED
;
return
terrno
;
}
}
STSmaWriteH
tSmaH
=
{
0
};
if
(
taosArrayGetSize
(
pData
->
pDataBlock
)
<=
0
)
{
terrno
=
TSDB_CODE_INVALID_PARA
;
if
(
tsdbInitTSmaWriteH
(
&
tSmaH
,
pTsdb
,
pData
)
!=
0
)
{
tsdbWarn
(
"vgId:%d insert tSma data failed since pDataBlock is empty"
,
REPO_ID
(
pTsdb
));
return
TSDB_CODE_FAILED
;
return
TSDB_CODE_FAILED
;
}
}
...
@@ -895,6 +898,14 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) {
...
@@ -895,6 +898,14 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) {
return
TSDB_CODE_FAILED
;
return
TSDB_CODE_FAILED
;
}
}
STSma
*
pSma
=
pItem
->
pSma
;
STSmaWriteH
tSmaH
=
{
0
};
if
(
tsdbInitTSmaWriteH
(
&
tSmaH
,
pTsdb
,
pData
,
pSma
->
interval
,
pSma
->
intervalUnit
)
!=
0
)
{
return
TSDB_CODE_FAILED
;
}
char
rPath
[
TSDB_FILENAME_LEN
]
=
{
0
};
char
rPath
[
TSDB_FILENAME_LEN
]
=
{
0
};
char
aPath
[
TSDB_FILENAME_LEN
]
=
{
0
};
char
aPath
[
TSDB_FILENAME_LEN
]
=
{
0
};
snprintf
(
rPath
,
TSDB_FILENAME_LEN
,
"%s%s%"
PRIi64
,
SMA_ENV_PATH
(
pEnv
),
TD_DIRSEP
,
indexUid
);
snprintf
(
rPath
,
TSDB_FILENAME_LEN
,
"%s%s%"
PRIi64
,
SMA_ENV_PATH
(
pEnv
),
TD_DIRSEP
,
indexUid
);
...
@@ -907,8 +918,11 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) {
...
@@ -907,8 +918,11 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) {
}
}
// Step 1: Judge the storage level and days
// Step 1: Judge the storage level and days
int32_t
storageLevel
=
tsdbGetSmaStorageLevel
(
p
Data
->
interval
,
pDat
a
->
intervalUnit
);
int32_t
storageLevel
=
tsdbGetSmaStorageLevel
(
p
Sma
->
interval
,
pSm
a
->
intervalUnit
);
int32_t
daysPerFile
=
tsdbGetTSmaDays
(
pTsdb
,
tSmaH
.
interval
,
storageLevel
);
int32_t
daysPerFile
=
tsdbGetTSmaDays
(
pTsdb
,
tSmaH
.
interval
,
storageLevel
);
#if 0
int32_t fid = (int32_t)(TSDB_KEY_FID(pData->skey, daysPerFile, pCfg->precision));
int32_t fid = (int32_t)(TSDB_KEY_FID(pData->skey, daysPerFile, pCfg->precision));
// Step 2: Set the DFile for storage of SMA index, and iterate/split the TSma data and store to B+Tree index file
// Step 2: Set the DFile for storage of SMA index, and iterate/split the TSma data and store to B+Tree index file
...
@@ -933,7 +947,7 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) {
...
@@ -933,7 +947,7 @@ static int32_t tsdbInsertTSmaDataImpl(STsdb *pTsdb, char *msg) {
// Step 3: reset the SSmaStat
// Step 3: reset the SSmaStat
tsdbResetExpiredWindow(pTsdb, SMA_ENV_STAT(pTsdb->pTSmaEnv), pData->indexUid, pData->skey);
tsdbResetExpiredWindow(pTsdb, SMA_ENV_STAT(pTsdb->pTSmaEnv), pData->indexUid, pData->skey);
#endif
tsdbDestroyTSmaWriteH
(
&
tSmaH
);
tsdbDestroyTSmaWriteH
(
&
tSmaH
);
tsdbUnRefSmaStat
(
pTsdb
,
pStat
);
tsdbUnRefSmaStat
(
pTsdb
,
pStat
);
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
...
@@ -999,29 +1013,58 @@ static int32_t tsdbSetRSmaDataFile(STSmaWriteH *pSmaH, STSmaDataWrapper *pData,
...
@@ -999,29 +1013,58 @@ static int32_t tsdbSetRSmaDataFile(STSmaWriteH *pSmaH, STSmaDataWrapper *pData,
}
}
static
int32_t
tsdbInsertRSmaDataImpl
(
STsdb
*
pTsdb
,
char
*
msg
)
{
static
int32_t
tsdbInsertRSmaDataImpl
(
STsdb
*
pTsdb
,
char
*
msg
)
{
STsdbCfg
*
pCfg
=
REPO_CFG
(
pTsdb
);
STsdbCfg
*
pCfg
=
REPO_CFG
(
pTsdb
);
STSmaDataWrapper
*
pData
=
(
STSmaDataWrapper
*
)
msg
;
SSDataBlock
*
pData
=
(
SSDataBlock
*
)
msg
;
SSmaEnv
*
pEnv
=
atomic_load_ptr
(
&
pTsdb
->
pRSmaEnv
);
SSmaEnv
*
pEnv
=
atomic_load_ptr
(
&
pTsdb
->
pRSmaEnv
);
int64_t
indexUid
=
SMA_TEST_INDEX_UID
;
if
(
pEnv
==
NULL
)
{
if
(
pEnv
==
NULL
)
{
terrno
=
TSDB_CODE_INVALID_PTR
;
terrno
=
TSDB_CODE_INVALID_PTR
;
tsdbWarn
(
"vgId:%d insert tSma data failed since pTSmaEnv is NULL"
,
REPO_ID
(
pTsdb
));
tsdbWarn
(
"vgId:%d insert rSma data failed since pTSmaEnv is NULL"
,
REPO_ID
(
pTsdb
));
return
terrno
;
}
if
(
pEnv
==
NULL
)
{
terrno
=
TSDB_CODE_INVALID_PTR
;
tsdbWarn
(
"vgId:%d insert rSma data failed since pTSmaEnv is NULL"
,
REPO_ID
(
pTsdb
));
return
terrno
;
}
if
(
pData
==
NULL
)
{
terrno
=
TSDB_CODE_INVALID_PTR
;
tsdbWarn
(
"vgId:%d insert rSma data failed since pData is NULL"
,
REPO_ID
(
pTsdb
));
return
terrno
;
return
terrno
;
}
}
if
(
pData
->
dataLen
<=
0
)
{
if
(
taosArrayGetSize
(
pData
->
pDataBlock
)
<=
0
)
{
TASSERT
(
0
);
terrno
=
TSDB_CODE_INVALID_PARA
;
terrno
=
TSDB_CODE_INVALID_PARA
;
tsdbWarn
(
"vgId:%d insert rSma data failed since pDataBlock is empty"
,
REPO_ID
(
pTsdb
));
return
TSDB_CODE_FAILED
;
return
TSDB_CODE_FAILED
;
}
}
SSmaStat
*
pStat
=
SMA_ENV_STAT
(
pTsdb
->
pTSmaEnv
);
SSmaStatItem
*
pItem
=
NULL
;
tsdbRefSmaStat
(
pTsdb
,
pStat
);
if
(
pStat
&&
pStat
->
smaStatItems
)
{
pItem
=
taosHashGet
(
pStat
->
smaStatItems
,
&
indexUid
,
sizeof
(
indexUid
));
}
if
((
pItem
==
NULL
)
||
((
pItem
=
*
(
SSmaStatItem
**
)
pItem
)
==
NULL
)
||
tsdbSmaStatIsDropped
(
pItem
))
{
terrno
=
TSDB_CODE_TDB_INVALID_SMA_STAT
;
tsdbUnRefSmaStat
(
pTsdb
,
pStat
);
return
TSDB_CODE_FAILED
;
}
STSma
*
pSma
=
pItem
->
pSma
;
STSmaWriteH
tSmaH
=
{
0
};
STSmaWriteH
tSmaH
=
{
0
};
if
(
tsdbInitTSmaWriteH
(
&
tSmaH
,
pTsdb
,
pData
)
!=
0
)
{
if
(
tsdbInitTSmaWriteH
(
&
tSmaH
,
pTsdb
,
pData
,
pSma
->
interval
,
pSma
->
intervalUnit
)
!=
0
)
{
return
TSDB_CODE_FAILED
;
return
TSDB_CODE_FAILED
;
}
}
int64_t
indexUid
=
SMA_TEST_INDEX_UID
;
char
rPath
[
TSDB_FILENAME_LEN
]
=
{
0
};
char
rPath
[
TSDB_FILENAME_LEN
]
=
{
0
};
char
aPath
[
TSDB_FILENAME_LEN
]
=
{
0
};
char
aPath
[
TSDB_FILENAME_LEN
]
=
{
0
};
snprintf
(
rPath
,
TSDB_FILENAME_LEN
,
"%s%s%"
PRIi64
,
SMA_ENV_PATH
(
pEnv
),
TD_DIRSEP
,
indexUid
);
snprintf
(
rPath
,
TSDB_FILENAME_LEN
,
"%s%s%"
PRIi64
,
SMA_ENV_PATH
(
pEnv
),
TD_DIRSEP
,
indexUid
);
...
@@ -1033,8 +1076,9 @@ static int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, char *msg) {
...
@@ -1033,8 +1076,9 @@ static int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, char *msg) {
}
}
// Step 1: Judge the storage level and days
// Step 1: Judge the storage level and days
int32_t
storageLevel
=
tsdbGetSmaStorageLevel
(
p
Data
->
interval
,
pDat
a
->
intervalUnit
);
int32_t
storageLevel
=
tsdbGetSmaStorageLevel
(
p
Sma
->
interval
,
pSm
a
->
intervalUnit
);
int32_t
daysPerFile
=
tsdbGetTSmaDays
(
pTsdb
,
tSmaH
.
interval
,
storageLevel
);
int32_t
daysPerFile
=
tsdbGetTSmaDays
(
pTsdb
,
tSmaH
.
interval
,
storageLevel
);
#if 0
int32_t fid = (int32_t)(TSDB_KEY_FID(pData->skey, daysPerFile, pCfg->precision));
int32_t fid = (int32_t)(TSDB_KEY_FID(pData->skey, daysPerFile, pCfg->precision));
// Step 2: Set the DFile for storage of SMA index, and iterate/split the TSma data and store to B+Tree index file
// Step 2: Set the DFile for storage of SMA index, and iterate/split the TSma data and store to B+Tree index file
...
@@ -1057,8 +1101,10 @@ static int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, char *msg) {
...
@@ -1057,8 +1101,10 @@ static int32_t tsdbInsertRSmaDataImpl(STsdb *pTsdb, char *msg) {
// Step 3: reset the SSmaStat
// Step 3: reset the SSmaStat
tsdbResetExpiredWindow(pTsdb, SMA_ENV_STAT(pTsdb->pTSmaEnv), pData->indexUid, pData->skey);
tsdbResetExpiredWindow(pTsdb, SMA_ENV_STAT(pTsdb->pTSmaEnv), pData->indexUid, pData->skey);
#endif
tsdbDestroyTSmaWriteH
(
&
tSmaH
);
tsdbDestroyTSmaWriteH
(
&
tSmaH
);
tsdbUnRefSmaStat
(
pTsdb
,
pStat
);
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
}
}
...
...
source/dnode/vnode/src/vnd/vnodeWrite.c
浏览文件 @
10319e3c
...
@@ -74,6 +74,9 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
...
@@ -74,6 +74,9 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
// TODO: maybe need to clear the request struct
// TODO: maybe need to clear the request struct
taosMemoryFree
(
vCreateTbReq
.
stbCfg
.
pSchema
);
taosMemoryFree
(
vCreateTbReq
.
stbCfg
.
pSchema
);
taosMemoryFree
(
vCreateTbReq
.
stbCfg
.
pTagSchema
);
taosMemoryFree
(
vCreateTbReq
.
stbCfg
.
pTagSchema
);
taosMemoryFree
(
vCreateTbReq
.
stbCfg
.
pBSmaCols
);
taosMemoryFree
(
vCreateTbReq
.
stbCfg
.
pRSmaParam
);
taosMemoryFree
(
vCreateTbReq
.
dbFName
);
taosMemoryFree
(
vCreateTbReq
.
name
);
taosMemoryFree
(
vCreateTbReq
.
name
);
break
;
break
;
}
}
...
@@ -102,13 +105,18 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
...
@@ -102,13 +105,18 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
vError
(
"vgId:%d, failed to create table: %s"
,
pVnode
->
vgId
,
pCreateTbReq
->
name
);
vError
(
"vgId:%d, failed to create table: %s"
,
pVnode
->
vgId
,
pCreateTbReq
->
name
);
}
}
taosMemoryFree
(
pCreateTbReq
->
name
);
taosMemoryFree
(
pCreateTbReq
->
name
);
taosMemoryFree
(
pCreateTbReq
->
dbFName
);
if
(
pCreateTbReq
->
type
==
TD_SUPER_TABLE
)
{
if
(
pCreateTbReq
->
type
==
TD_SUPER_TABLE
)
{
taosMemoryFree
(
pCreateTbReq
->
stbCfg
.
pSchema
);
taosMemoryFree
(
pCreateTbReq
->
stbCfg
.
pSchema
);
taosMemoryFree
(
pCreateTbReq
->
stbCfg
.
pTagSchema
);
taosMemoryFree
(
pCreateTbReq
->
stbCfg
.
pTagSchema
);
taosMemoryFree
(
pCreateTbReq
->
stbCfg
.
pBSmaCols
);
taosMemoryFree
(
pCreateTbReq
->
stbCfg
.
pRSmaParam
);
}
else
if
(
pCreateTbReq
->
type
==
TD_CHILD_TABLE
)
{
}
else
if
(
pCreateTbReq
->
type
==
TD_CHILD_TABLE
)
{
taosMemoryFree
(
pCreateTbReq
->
ctbCfg
.
pTag
);
taosMemoryFree
(
pCreateTbReq
->
ctbCfg
.
pTag
);
}
else
{
}
else
{
taosMemoryFree
(
pCreateTbReq
->
ntbCfg
.
pSchema
);
taosMemoryFree
(
pCreateTbReq
->
ntbCfg
.
pSchema
);
taosMemoryFree
(
pCreateTbReq
->
ntbCfg
.
pBSmaCols
);
taosMemoryFree
(
pCreateTbReq
->
ntbCfg
.
pRSmaParam
);
}
}
}
}
...
@@ -135,6 +143,9 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
...
@@ -135,6 +143,9 @@ int vnodeApplyWMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
tDeserializeSVCreateTbReq
(
POINTER_SHIFT
(
pMsg
->
pCont
,
sizeof
(
SMsgHead
)),
&
vAlterTbReq
);
tDeserializeSVCreateTbReq
(
POINTER_SHIFT
(
pMsg
->
pCont
,
sizeof
(
SMsgHead
)),
&
vAlterTbReq
);
taosMemoryFree
(
vAlterTbReq
.
stbCfg
.
pSchema
);
taosMemoryFree
(
vAlterTbReq
.
stbCfg
.
pSchema
);
taosMemoryFree
(
vAlterTbReq
.
stbCfg
.
pTagSchema
);
taosMemoryFree
(
vAlterTbReq
.
stbCfg
.
pTagSchema
);
taosMemoryFree
(
vAlterTbReq
.
stbCfg
.
pBSmaCols
);
taosMemoryFree
(
vAlterTbReq
.
stbCfg
.
pRSmaParam
);
taosMemoryFree
(
vAlterTbReq
.
dbFName
);
taosMemoryFree
(
vAlterTbReq
.
name
);
taosMemoryFree
(
vAlterTbReq
.
name
);
break
;
break
;
}
}
...
...
source/dnode/vnode/test/tsdbSmaTest.cpp
浏览文件 @
10319e3c
...
@@ -280,7 +280,7 @@ TEST(testCase, tSma_metaDB_Put_Get_Del_Test) {
...
@@ -280,7 +280,7 @@ TEST(testCase, tSma_metaDB_Put_Get_Del_Test) {
}
}
#endif
#endif
#if
1
#if
0
TEST(testCase, tSma_Data_Insert_Query_Test) {
TEST(testCase, tSma_Data_Insert_Query_Test) {
// step 1: prepare meta
// step 1: prepare meta
const char *smaIndexName1 = "sma_index_test_1";
const char *smaIndexName1 = "sma_index_test_1";
...
...
source/libs/catalog/src/catalog.c
浏览文件 @
10319e3c
...
@@ -172,7 +172,7 @@ void ctgDbgShowTableMeta(SCatalog* pCtg, const char *tbName, STableMeta* p) {
...
@@ -172,7 +172,7 @@ void ctgDbgShowTableMeta(SCatalog* pCtg, const char *tbName, STableMeta* p) {
int32_t
colNum
=
c
->
numOfColumns
+
c
->
numOfTags
;
int32_t
colNum
=
c
->
numOfColumns
+
c
->
numOfTags
;
for
(
int32_t
i
=
0
;
i
<
colNum
;
++
i
)
{
for
(
int32_t
i
=
0
;
i
<
colNum
;
++
i
)
{
SSchema
*
s
=
&
p
->
schema
[
i
];
SSchema
*
s
=
&
p
->
schema
[
i
];
ctgDebug
(
"[%d] name:%s, type:%d, colId:%
d
, bytes:%d"
,
i
,
s
->
name
,
s
->
type
,
s
->
colId
,
s
->
bytes
);
ctgDebug
(
"[%d] name:%s, type:%d, colId:%
"
PRIi16
"
, bytes:%d"
,
i
,
s
->
name
,
s
->
type
,
s
->
colId
,
s
->
bytes
);
}
}
}
}
...
...
source/libs/parser/inc/parInsertData.h
浏览文件 @
10319e3c
...
@@ -41,26 +41,26 @@ typedef struct SBoundColumn {
...
@@ -41,26 +41,26 @@ typedef struct SBoundColumn {
}
SBoundColumn
;
}
SBoundColumn
;
typedef
struct
{
typedef
struct
{
uint16
_t
schemaColIdx
;
col_id
_t
schemaColIdx
;
uint16
_t
boundIdx
;
col_id
_t
boundIdx
;
uint16
_t
finalIdx
;
col_id
_t
finalIdx
;
}
SBoundIdxInfo
;
}
SBoundIdxInfo
;
typedef
struct
SParsedDataColInfo
{
typedef
struct
SParsedDataColInfo
{
int16_t
numOfCols
;
col_id_t
numOfCols
;
int16_t
numOfBound
;
col_id_t
numOfBound
;
uint16_t
flen
;
// TODO: get from STSchema
uint16_t
flen
;
// TODO: get from STSchema
uint16_t
allNullLen
;
// TODO: get from STSchema(base on SDataRow)
uint16_t
allNullLen
;
// TODO: get from STSchema(base on SDataRow)
uint16_t
extendedVarLen
;
uint16_t
extendedVarLen
;
uint16_t
boundNullLen
;
// bound column len with all NULL value(without VarDataOffsetT/SColIdx part)
uint16_t
boundNullLen
;
// bound column len with all NULL value(without VarDataOffsetT/SColIdx part)
int32_t
*
boundedColumns
;
// bound column idx according to schema
col_id_t
*
boundColumns
;
// bound column idx according to schema
SBoundColumn
*
cols
;
SBoundColumn
*
cols
;
SBoundIdxInfo
*
colIdxInfo
;
SBoundIdxInfo
*
colIdxInfo
;
int8_t
orderStatus
;
// bound columns
int8_t
orderStatus
;
// bound columns
}
SParsedDataColInfo
;
}
SParsedDataColInfo
;
typedef
struct
{
typedef
struct
{
uint8_t
memR
owType
;
// default is 0, that is SDataRow
uint8_t
r
owType
;
// default is 0, that is SDataRow
int32_t
rowSize
;
int32_t
rowSize
;
}
SMemRowBuilder
;
}
SMemRowBuilder
;
...
@@ -92,11 +92,11 @@ static FORCE_INLINE int32_t getExtendedRowSize(STableDataBlocks *pBlock) {
...
@@ -92,11 +92,11 @@ static FORCE_INLINE int32_t getExtendedRowSize(STableDataBlocks *pBlock) {
(
int32_t
)
TD_BITMAP_BYTES
(
pTableInfo
->
numOfColumns
-
1
);
(
int32_t
)
TD_BITMAP_BYTES
(
pTableInfo
->
numOfColumns
-
1
);
}
}
static
FORCE_INLINE
void
get
MemRowAppendInfo
(
SSchema
*
pSchema
,
uint8_t
rowType
,
SParsedDataColInfo
*
spd
,
static
FORCE_INLINE
void
get
STSRowAppendInfo
(
SSchema
*
pSchema
,
uint8_t
rowType
,
SParsedDataColInfo
*
spd
,
col_id_t
idx
,
int32_t
idx
,
int32_t
*
toffset
,
int32
_t
*
colIdx
)
{
int32_t
*
toffset
,
col_id
_t
*
colIdx
)
{
int32
_t
schemaIdx
=
0
;
col_id
_t
schemaIdx
=
0
;
if
(
IS_DATA_COL_ORDERED
(
spd
))
{
if
(
IS_DATA_COL_ORDERED
(
spd
))
{
schemaIdx
=
spd
->
bound
ed
Columns
[
idx
]
-
PRIMARYKEY_TIMESTAMP_COL_ID
;
schemaIdx
=
spd
->
boundColumns
[
idx
]
-
PRIMARYKEY_TIMESTAMP_COL_ID
;
if
(
TD_IS_TP_ROW_T
(
rowType
))
{
if
(
TD_IS_TP_ROW_T
(
rowType
))
{
*
toffset
=
(
spd
->
cols
+
schemaIdx
)
->
toffset
;
// the offset of firstPart
*
toffset
=
(
spd
->
cols
+
schemaIdx
)
->
toffset
;
// the offset of firstPart
*
colIdx
=
schemaIdx
;
*
colIdx
=
schemaIdx
;
...
@@ -132,7 +132,7 @@ static FORCE_INLINE int32_t setBlockInfo(SSubmitBlk *pBlocks, STableDataBlocks*
...
@@ -132,7 +132,7 @@ static FORCE_INLINE int32_t setBlockInfo(SSubmitBlk *pBlocks, STableDataBlocks*
int32_t
schemaIdxCompar
(
const
void
*
lhs
,
const
void
*
rhs
);
int32_t
schemaIdxCompar
(
const
void
*
lhs
,
const
void
*
rhs
);
int32_t
boundIdxCompar
(
const
void
*
lhs
,
const
void
*
rhs
);
int32_t
boundIdxCompar
(
const
void
*
lhs
,
const
void
*
rhs
);
void
setBoundColumnInfo
(
SParsedDataColInfo
*
pColList
,
SSchema
*
pSchema
,
int32
_t
numOfCols
);
void
setBoundColumnInfo
(
SParsedDataColInfo
*
pColList
,
SSchema
*
pSchema
,
col_id
_t
numOfCols
);
void
destroyBoundColumnInfo
(
SParsedDataColInfo
*
pColList
);
void
destroyBoundColumnInfo
(
SParsedDataColInfo
*
pColList
);
void
destroyBlockArrayList
(
SArray
*
pDataBlockList
);
void
destroyBlockArrayList
(
SArray
*
pDataBlockList
);
void
destroyBlockHashmap
(
SHashObj
*
pDataBlockHash
);
void
destroyBlockHashmap
(
SHashObj
*
pDataBlockHash
);
...
...
source/libs/parser/src/parInsert.c
浏览文件 @
10319e3c
...
@@ -600,9 +600,9 @@ static int32_t parseValueToken(char** end, SToken* pToken, SSchema* pSchema, int
...
@@ -600,9 +600,9 @@ static int32_t parseValueToken(char** end, SToken* pToken, SSchema* pSchema, int
typedef
struct
SMemParam
{
typedef
struct
SMemParam
{
SRowBuilder
*
rb
;
SRowBuilder
*
rb
;
SSchema
*
schema
;
SSchema
*
schema
;
int32_t
toffset
;
int32_t
toffset
;
int32_t
colIdx
;
col_id_t
colIdx
;
}
SMemParam
;
}
SMemParam
;
static
FORCE_INLINE
int32_t
MemRowAppend
(
const
void
*
value
,
int32_t
len
,
void
*
param
)
{
static
FORCE_INLINE
int32_t
MemRowAppend
(
const
void
*
value
,
int32_t
len
,
void
*
param
)
{
...
@@ -623,9 +623,11 @@ static FORCE_INLINE int32_t MemRowAppend(const void* value, int32_t len, void* p
...
@@ -623,9 +623,11 @@ static FORCE_INLINE int32_t MemRowAppend(const void* value, int32_t len, void* p
tdAppendColValToRow
(
rb
,
pa
->
schema
->
colId
,
pa
->
schema
->
type
,
TD_VTYPE_NORM
,
rowEnd
,
false
,
pa
->
toffset
,
pa
->
colIdx
);
tdAppendColValToRow
(
rb
,
pa
->
schema
->
colId
,
pa
->
schema
->
type
,
TD_VTYPE_NORM
,
rowEnd
,
false
,
pa
->
toffset
,
pa
->
colIdx
);
}
else
{
}
else
{
if
(
value
==
NULL
)
{
// it is a null data
if
(
value
==
NULL
)
{
// it is a null data
tdAppendColValToRow
(
rb
,
pa
->
schema
->
colId
,
pa
->
schema
->
type
,
TD_VTYPE_NULL
,
value
,
false
,
pa
->
toffset
,
pa
->
colIdx
);
tdAppendColValToRow
(
rb
,
pa
->
schema
->
colId
,
pa
->
schema
->
type
,
TD_VTYPE_NULL
,
value
,
false
,
pa
->
toffset
,
pa
->
colIdx
);
}
else
{
}
else
{
tdAppendColValToRow
(
rb
,
pa
->
schema
->
colId
,
pa
->
schema
->
type
,
TD_VTYPE_NORM
,
value
,
false
,
pa
->
toffset
,
pa
->
colIdx
);
tdAppendColValToRow
(
rb
,
pa
->
schema
->
colId
,
pa
->
schema
->
type
,
TD_VTYPE_NORM
,
value
,
false
,
pa
->
toffset
,
pa
->
colIdx
);
}
}
}
}
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
...
@@ -633,18 +635,18 @@ static FORCE_INLINE int32_t MemRowAppend(const void* value, int32_t len, void* p
...
@@ -633,18 +635,18 @@ static FORCE_INLINE int32_t MemRowAppend(const void* value, int32_t len, void* p
// pSql -> tag1_name, ...)
// pSql -> tag1_name, ...)
static
int32_t
parseBoundColumns
(
SInsertParseContext
*
pCxt
,
SParsedDataColInfo
*
pColList
,
SSchema
*
pSchema
)
{
static
int32_t
parseBoundColumns
(
SInsertParseContext
*
pCxt
,
SParsedDataColInfo
*
pColList
,
SSchema
*
pSchema
)
{
int32
_t
nCols
=
pColList
->
numOfCols
;
col_id
_t
nCols
=
pColList
->
numOfCols
;
pColList
->
numOfBound
=
0
;
pColList
->
numOfBound
=
0
;
pColList
->
boundNullLen
=
0
;
pColList
->
boundNullLen
=
0
;
memset
(
pColList
->
bound
edColumns
,
0
,
sizeof
(
int32
_t
)
*
nCols
);
memset
(
pColList
->
bound
Columns
,
0
,
sizeof
(
col_id
_t
)
*
nCols
);
for
(
int32
_t
i
=
0
;
i
<
nCols
;
++
i
)
{
for
(
col_id
_t
i
=
0
;
i
<
nCols
;
++
i
)
{
pColList
->
cols
[
i
].
valStat
=
VAL_STAT_NONE
;
pColList
->
cols
[
i
].
valStat
=
VAL_STAT_NONE
;
}
}
SToken
sToken
;
SToken
sToken
;
bool
isOrdered
=
true
;
bool
isOrdered
=
true
;
int32
_t
lastColIdx
=
-
1
;
// last column found
col_id
_t
lastColIdx
=
-
1
;
// last column found
while
(
1
)
{
while
(
1
)
{
NEXT_TOKEN
(
pCxt
->
pSql
,
sToken
);
NEXT_TOKEN
(
pCxt
->
pSql
,
sToken
);
...
@@ -652,8 +654,8 @@ static int32_t parseBoundColumns(SInsertParseContext* pCxt, SParsedDataColInfo*
...
@@ -652,8 +654,8 @@ static int32_t parseBoundColumns(SInsertParseContext* pCxt, SParsedDataColInfo*
break
;
break
;
}
}
int32
_t
t
=
lastColIdx
+
1
;
col_id
_t
t
=
lastColIdx
+
1
;
int32
_t
index
=
findCol
(
&
sToken
,
t
,
nCols
,
pSchema
);
col_id
_t
index
=
findCol
(
&
sToken
,
t
,
nCols
,
pSchema
);
if
(
index
<
0
&&
t
>
0
)
{
if
(
index
<
0
&&
t
>
0
)
{
index
=
findCol
(
&
sToken
,
0
,
t
,
pSchema
);
index
=
findCol
(
&
sToken
,
0
,
t
,
pSchema
);
isOrdered
=
false
;
isOrdered
=
false
;
...
@@ -666,7 +668,7 @@ static int32_t parseBoundColumns(SInsertParseContext* pCxt, SParsedDataColInfo*
...
@@ -666,7 +668,7 @@ static int32_t parseBoundColumns(SInsertParseContext* pCxt, SParsedDataColInfo*
}
}
lastColIdx
=
index
;
lastColIdx
=
index
;
pColList
->
cols
[
index
].
valStat
=
VAL_STAT_HAS
;
pColList
->
cols
[
index
].
valStat
=
VAL_STAT_HAS
;
pColList
->
bound
ed
Columns
[
pColList
->
numOfBound
]
=
index
+
PRIMARYKEY_TIMESTAMP_COL_ID
;
pColList
->
boundColumns
[
pColList
->
numOfBound
]
=
index
+
PRIMARYKEY_TIMESTAMP_COL_ID
;
++
pColList
->
numOfBound
;
++
pColList
->
numOfBound
;
switch
(
pSchema
[
t
].
type
)
{
switch
(
pSchema
[
t
].
type
)
{
case
TSDB_DATA_TYPE_BINARY
:
case
TSDB_DATA_TYPE_BINARY
:
...
@@ -689,18 +691,19 @@ static int32_t parseBoundColumns(SInsertParseContext* pCxt, SParsedDataColInfo*
...
@@ -689,18 +691,19 @@ static int32_t parseBoundColumns(SInsertParseContext* pCxt, SParsedDataColInfo*
return
TSDB_CODE_TSC_OUT_OF_MEMORY
;
return
TSDB_CODE_TSC_OUT_OF_MEMORY
;
}
}
SBoundIdxInfo
*
pColIdx
=
pColList
->
colIdxInfo
;
SBoundIdxInfo
*
pColIdx
=
pColList
->
colIdxInfo
;
for
(
uint16
_t
i
=
0
;
i
<
pColList
->
numOfBound
;
++
i
)
{
for
(
col_id
_t
i
=
0
;
i
<
pColList
->
numOfBound
;
++
i
)
{
pColIdx
[
i
].
schemaColIdx
=
(
uint16_t
)
pColList
->
bounde
dColumns
[
i
];
pColIdx
[
i
].
schemaColIdx
=
pColList
->
boun
dColumns
[
i
];
pColIdx
[
i
].
boundIdx
=
i
;
pColIdx
[
i
].
boundIdx
=
i
;
}
}
qsort
(
pColIdx
,
pColList
->
numOfBound
,
sizeof
(
SBoundIdxInfo
),
schemaIdxCompar
);
qsort
(
pColIdx
,
pColList
->
numOfBound
,
sizeof
(
SBoundIdxInfo
),
schemaIdxCompar
);
for
(
uint16
_t
i
=
0
;
i
<
pColList
->
numOfBound
;
++
i
)
{
for
(
col_id
_t
i
=
0
;
i
<
pColList
->
numOfBound
;
++
i
)
{
pColIdx
[
i
].
finalIdx
=
i
;
pColIdx
[
i
].
finalIdx
=
i
;
}
}
qsort
(
pColIdx
,
pColList
->
numOfBound
,
sizeof
(
SBoundIdxInfo
),
boundIdxCompar
);
qsort
(
pColIdx
,
pColList
->
numOfBound
,
sizeof
(
SBoundIdxInfo
),
boundIdxCompar
);
}
}
memset
(
&
pColList
->
boundedColumns
[
pColList
->
numOfBound
],
0
,
sizeof
(
int32_t
)
*
(
pColList
->
numOfCols
-
pColList
->
numOfBound
));
memset
(
&
pColList
->
boundColumns
[
pColList
->
numOfBound
],
0
,
sizeof
(
col_id_t
)
*
(
pColList
->
numOfCols
-
pColList
->
numOfBound
));
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
}
}
...
@@ -714,8 +717,8 @@ typedef struct SKvParam {
...
@@ -714,8 +717,8 @@ typedef struct SKvParam {
static
int32_t
KvRowAppend
(
const
void
*
value
,
int32_t
len
,
void
*
param
)
{
static
int32_t
KvRowAppend
(
const
void
*
value
,
int32_t
len
,
void
*
param
)
{
SKvParam
*
pa
=
(
SKvParam
*
)
param
;
SKvParam
*
pa
=
(
SKvParam
*
)
param
;
int
32_t
type
=
pa
->
schema
->
type
;
int
8_t
type
=
pa
->
schema
->
type
;
int
32
_t
colId
=
pa
->
schema
->
colId
;
int
16
_t
colId
=
pa
->
schema
->
colId
;
if
(
TSDB_DATA_TYPE_BINARY
==
type
)
{
if
(
TSDB_DATA_TYPE_BINARY
==
type
)
{
STR_WITH_SIZE_TO_VARSTR
(
pa
->
buf
,
value
,
len
);
STR_WITH_SIZE_TO_VARSTR
(
pa
->
buf
,
value
,
len
);
...
@@ -747,7 +750,7 @@ static int32_t parseTagsClause(SInsertParseContext* pCxt, SSchema* pTagsSchema,
...
@@ -747,7 +750,7 @@ static int32_t parseTagsClause(SInsertParseContext* pCxt, SSchema* pTagsSchema,
char
tmpTokenBuf
[
TSDB_MAX_BYTES_PER_ROW
]
=
{
0
};
// used for deleting Escape character: \\, \', \"
char
tmpTokenBuf
[
TSDB_MAX_BYTES_PER_ROW
]
=
{
0
};
// used for deleting Escape character: \\, \', \"
for
(
int
i
=
0
;
i
<
pCxt
->
tags
.
numOfBound
;
++
i
)
{
for
(
int
i
=
0
;
i
<
pCxt
->
tags
.
numOfBound
;
++
i
)
{
NEXT_TOKEN_WITH_PREV
(
pCxt
->
pSql
,
sToken
);
NEXT_TOKEN_WITH_PREV
(
pCxt
->
pSql
,
sToken
);
SSchema
*
pSchema
=
&
pTagsSchema
[
pCxt
->
tags
.
bound
ed
Columns
[
i
]];
SSchema
*
pSchema
=
&
pTagsSchema
[
pCxt
->
tags
.
boundColumns
[
i
]];
param
.
schema
=
pSchema
;
param
.
schema
=
pSchema
;
CHECK_CODE
(
parseValueToken
(
&
pCxt
->
pSql
,
&
sToken
,
pSchema
,
precision
,
tmpTokenBuf
,
KvRowAppend
,
&
param
,
&
pCxt
->
msg
));
CHECK_CODE
(
parseValueToken
(
&
pCxt
->
pSql
,
&
sToken
,
pSchema
,
precision
,
tmpTokenBuf
,
KvRowAppend
,
&
param
,
&
pCxt
->
msg
));
}
}
...
@@ -813,9 +816,9 @@ static int parseOneRow(SInsertParseContext* pCxt, STableDataBlocks* pDataBlocks,
...
@@ -813,9 +816,9 @@ static int parseOneRow(SInsertParseContext* pCxt, STableDataBlocks* pDataBlocks,
// 1. set the parsed value from sql string
// 1. set the parsed value from sql string
for
(
int
i
=
0
;
i
<
spd
->
numOfBound
;
++
i
)
{
for
(
int
i
=
0
;
i
<
spd
->
numOfBound
;
++
i
)
{
NEXT_TOKEN_WITH_PREV
(
pCxt
->
pSql
,
sToken
);
NEXT_TOKEN_WITH_PREV
(
pCxt
->
pSql
,
sToken
);
SSchema
*
pSchema
=
&
schema
[
spd
->
bounde
dColumns
[
i
]
-
1
];
SSchema
*
pSchema
=
&
schema
[
spd
->
boun
dColumns
[
i
]
-
1
];
param
.
schema
=
pSchema
;
param
.
schema
=
pSchema
;
get
Mem
RowAppendInfo
(
schema
,
pBuilder
->
rowType
,
spd
,
i
,
&
param
.
toffset
,
&
param
.
colIdx
);
get
STS
RowAppendInfo
(
schema
,
pBuilder
->
rowType
,
spd
,
i
,
&
param
.
toffset
,
&
param
.
colIdx
);
CHECK_CODE
(
parseValueToken
(
&
pCxt
->
pSql
,
&
sToken
,
pSchema
,
timePrec
,
tmpTokenBuf
,
MemRowAppend
,
&
param
,
&
pCxt
->
msg
));
CHECK_CODE
(
parseValueToken
(
&
pCxt
->
pSql
,
&
sToken
,
pSchema
,
timePrec
,
tmpTokenBuf
,
MemRowAppend
,
&
param
,
&
pCxt
->
msg
));
if
(
PRIMARYKEY_TIMESTAMP_COL_ID
==
pSchema
->
colId
)
{
if
(
PRIMARYKEY_TIMESTAMP_COL_ID
==
pSchema
->
colId
)
{
...
...
source/libs/parser/src/parInsertData.c
浏览文件 @
10319e3c
...
@@ -43,11 +43,11 @@ static int32_t rowDataCompar(const void *lhs, const void *rhs) {
...
@@ -43,11 +43,11 @@ static int32_t rowDataCompar(const void *lhs, const void *rhs) {
}
}
}
}
void
setBoundColumnInfo
(
SParsedDataColInfo
*
pColList
,
SSchema
*
pSchema
,
int32
_t
numOfCols
)
{
void
setBoundColumnInfo
(
SParsedDataColInfo
*
pColList
,
SSchema
*
pSchema
,
col_id
_t
numOfCols
)
{
pColList
->
numOfCols
=
numOfCols
;
pColList
->
numOfCols
=
numOfCols
;
pColList
->
numOfBound
=
numOfCols
;
pColList
->
numOfBound
=
numOfCols
;
pColList
->
orderStatus
=
ORDER_STATUS_ORDERED
;
// default is ORDERED for non-bound mode
pColList
->
orderStatus
=
ORDER_STATUS_ORDERED
;
// default is ORDERED for non-bound mode
pColList
->
bound
edColumns
=
taosMemoryCalloc
(
pColList
->
numOfCols
,
sizeof
(
int32
_t
));
pColList
->
bound
Columns
=
taosMemoryCalloc
(
pColList
->
numOfCols
,
sizeof
(
col_id
_t
));
pColList
->
cols
=
taosMemoryCalloc
(
pColList
->
numOfCols
,
sizeof
(
SBoundColumn
));
pColList
->
cols
=
taosMemoryCalloc
(
pColList
->
numOfCols
,
sizeof
(
SBoundColumn
));
pColList
->
colIdxInfo
=
NULL
;
pColList
->
colIdxInfo
=
NULL
;
pColList
->
flen
=
0
;
pColList
->
flen
=
0
;
...
@@ -73,7 +73,7 @@ void setBoundColumnInfo(SParsedDataColInfo* pColList, SSchema* pSchema, int32_t
...
@@ -73,7 +73,7 @@ void setBoundColumnInfo(SParsedDataColInfo* pColList, SSchema* pSchema, int32_t
default:
default:
break
;
break
;
}
}
pColList
->
bound
ed
Columns
[
i
]
=
pSchema
[
i
].
colId
;
pColList
->
boundColumns
[
i
]
=
pSchema
[
i
].
colId
;
}
}
pColList
->
allNullLen
+=
pColList
->
flen
;
pColList
->
allNullLen
+=
pColList
->
flen
;
pColList
->
boundNullLen
=
pColList
->
allNullLen
;
// default set allNullLen
pColList
->
boundNullLen
=
pColList
->
allNullLen
;
// default set allNullLen
...
@@ -103,7 +103,7 @@ int32_t boundIdxCompar(const void *lhs, const void *rhs) {
...
@@ -103,7 +103,7 @@ int32_t boundIdxCompar(const void *lhs, const void *rhs) {
}
}
void
destroyBoundColumnInfo
(
SParsedDataColInfo
*
pColList
)
{
void
destroyBoundColumnInfo
(
SParsedDataColInfo
*
pColList
)
{
taosMemoryFreeClear
(
pColList
->
bound
ed
Columns
);
taosMemoryFreeClear
(
pColList
->
boundColumns
);
taosMemoryFreeClear
(
pColList
->
cols
);
taosMemoryFreeClear
(
pColList
->
cols
);
taosMemoryFreeClear
(
pColList
->
colIdxInfo
);
taosMemoryFreeClear
(
pColList
->
colIdxInfo
);
}
}
...
...
source/libs/parser/src/parTranslater.c
浏览文件 @
10319e3c
...
@@ -1990,7 +1990,7 @@ typedef struct SVgroupTablesBatch {
...
@@ -1990,7 +1990,7 @@ typedef struct SVgroupTablesBatch {
char
dbName
[
TSDB_DB_NAME_LEN
];
char
dbName
[
TSDB_DB_NAME_LEN
];
}
SVgroupTablesBatch
;
}
SVgroupTablesBatch
;
static
void
toSchema
(
const
SColumnDefNode
*
pCol
,
int32
_t
colId
,
SSchema
*
pSchema
)
{
static
void
toSchema
(
const
SColumnDefNode
*
pCol
,
col_id
_t
colId
,
SSchema
*
pSchema
)
{
pSchema
->
colId
=
colId
;
pSchema
->
colId
=
colId
;
pSchema
->
type
=
pCol
->
dataType
.
type
;
pSchema
->
type
=
pCol
->
dataType
.
type
;
pSchema
->
bytes
=
pCol
->
dataType
.
bytes
;
pSchema
->
bytes
=
pCol
->
dataType
.
bytes
;
...
...
source/libs/parser/test/mockCatalogService.cpp
浏览文件 @
10319e3c
...
@@ -87,7 +87,7 @@ private:
...
@@ -87,7 +87,7 @@ private:
return
meta_
;
return
meta_
;
}
}
int32_t
colId_
;
col_id_t
colId_
;
int32_t
rowsize_
;
int32_t
rowsize_
;
std
::
shared_ptr
<
MockTableMeta
>
meta_
;
std
::
shared_ptr
<
MockTableMeta
>
meta_
;
};
};
...
...
source/libs/qcom/src/queryUtil.c
浏览文件 @
10319e3c
...
@@ -198,7 +198,7 @@ char *jobTaskStatusStr(int32_t status) {
...
@@ -198,7 +198,7 @@ char *jobTaskStatusStr(int32_t status) {
return
"UNKNOWN"
;
return
"UNKNOWN"
;
}
}
SSchema
createSchema
(
uint8_t
type
,
int32_t
bytes
,
int32
_t
colId
,
const
char
*
name
)
{
SSchema
createSchema
(
int8_t
type
,
int32_t
bytes
,
col_id
_t
colId
,
const
char
*
name
)
{
SSchema
s
=
{
0
};
SSchema
s
=
{
0
};
s
.
type
=
type
;
s
.
type
=
type
;
s
.
bytes
=
bytes
;
s
.
bytes
=
bytes
;
...
...
source/libs/qcom/src/querymsg.c
浏览文件 @
10319e3c
...
@@ -188,7 +188,7 @@ static int32_t queryConvertTableMetaMsg(STableMetaRsp *pMetaMsg) {
...
@@ -188,7 +188,7 @@ static int32_t queryConvertTableMetaMsg(STableMetaRsp *pMetaMsg) {
}
}
if
(
pMetaMsg
->
pSchemas
[
0
].
colId
!=
PRIMARYKEY_TIMESTAMP_COL_ID
)
{
if
(
pMetaMsg
->
pSchemas
[
0
].
colId
!=
PRIMARYKEY_TIMESTAMP_COL_ID
)
{
qError
(
"invalid colId[%
d
] for the first column in table meta rsp msg"
,
pMetaMsg
->
pSchemas
[
0
].
colId
);
qError
(
"invalid colId[%
"
PRIi16
"
] for the first column in table meta rsp msg"
,
pMetaMsg
->
pSchemas
[
0
].
colId
);
return
TSDB_CODE_TSC_INVALID_VALUE
;
return
TSDB_CODE_TSC_INVALID_VALUE
;
}
}
...
...
source/libs/qworker/src/qworkerMsg.c
浏览文件 @
10319e3c
...
@@ -170,7 +170,7 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) {
...
@@ -170,7 +170,7 @@ int32_t qwBuildAndSendShowRsp(SRpcMsg *pMsg, int32_t code) {
return
-
1
;
return
-
1
;
}
}
int32
_t
cols
=
0
;
col_id
_t
cols
=
0
;
SSchema
*
pSchema
=
showRsp
.
tableMeta
.
pSchemas
;
SSchema
*
pSchema
=
showRsp
.
tableMeta
.
pSchemas
;
const
SSchema
*
s
=
tGetTbnameColumnSchema
();
const
SSchema
*
s
=
tGetTbnameColumnSchema
();
...
...
source/libs/tdb/CMakeLists.txt
浏览文件 @
10319e3c
...
@@ -8,7 +8,6 @@ target_sources(tdb
...
@@ -8,7 +8,6 @@ target_sources(tdb
"src/db/tdbBtree.c"
"src/db/tdbBtree.c"
"src/db/tdbDb.c"
"src/db/tdbDb.c"
"src/db/tdbEnv.c"
"src/db/tdbEnv.c"
# "src/db/tdbPage.c"
"src/page/tdbPage.c"
"src/page/tdbPage.c"
"src/page/tdbPageL.c"
"src/page/tdbPageL.c"
)
)
...
...
source/libs/tdb/src/btree/tdbBtreeBalance.c
0 → 100644
浏览文件 @
10319e3c
/*
* 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/>.
*/
\ No newline at end of file
source/libs/tdb/src/btree/tdbBtreeCommon.c
0 → 100644
浏览文件 @
10319e3c
/*
* 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/>.
*/
\ No newline at end of file
source/libs/tdb/src/btree/tdbBtreeDelete.c
0 → 100644
浏览文件 @
10319e3c
/*
* 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/>.
*/
\ No newline at end of file
source/libs/tdb/src/btree/tdbBtreeInsert.c
0 → 100644
浏览文件 @
10319e3c
/*
* 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/>.
*/
\ No newline at end of file
source/libs/tdb/src/btree/tdbBtreeInt.h
0 → 100644
浏览文件 @
10319e3c
/*
* 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_BTREE_INT_H_
#define _TDB_BTREE_INT_H_
#ifdef __cplusplus
extern
"C"
{
#endif
#ifdef __cplusplus
}
#endif
#endif
/*_TDB_BTREE_INT_H_*/
\ No newline at end of file
source/libs/tdb/src/btree/tdbBtreeOpen.c
0 → 100644
浏览文件 @
10319e3c
/*
* 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/>.
*/
\ No newline at end of file
source/libs/tdb/src/db/tdbBtree.c
浏览文件 @
10319e3c
...
@@ -39,14 +39,24 @@ struct SBTree {
...
@@ -39,14 +39,24 @@ struct SBTree {
u8
*
pTmp
;
u8
*
pTmp
;
};
};
#define TDB_BTREE_PAGE_COMMON_HDR u8 flags;
#define TDB_BTREE_PAGE_GET_FLAGS(PAGE) (PAGE)->pData[0]
#define TDB_BTREE_PAGE_SET_FLAGS(PAGE, flags) ((PAGE)->pData[0] = (flags))
typedef
struct
__attribute__
((
__packed__
))
{
typedef
struct
__attribute__
((
__packed__
))
{
SPgno
rChild
;
TDB_BTREE_PAGE_COMMON_HDR
}
SBtPageHdr
;
}
SLeafHdr
;
typedef
struct
__attribute__
((
__packed__
))
{
TDB_BTREE_PAGE_COMMON_HDR
;
SPgno
pgno
;
// right-most child
}
SIntHdr
;
typedef
struct
{
typedef
struct
{
u
16
flags
;
u
8
flags
;
SBTree
*
pBt
;
SBTree
*
pBt
;
}
SBtree
Zero
PageArg
;
}
SBtree
Init
PageArg
;
typedef
struct
{
typedef
struct
{
int
kLen
;
int
kLen
;
...
@@ -57,7 +67,7 @@ typedef struct {
...
@@ -57,7 +67,7 @@ typedef struct {
u8
*
pTmpSpace
;
u8
*
pTmpSpace
;
}
SCellDecoder
;
}
SCellDecoder
;
static
int
tdbBtCursorMoveTo
(
SB
tCursor
*
pCur
,
const
void
*
pKey
,
int
kLen
,
int
*
pCRst
);
static
int
tdbBtCursorMoveTo
(
SB
TC
*
pCur
,
const
void
*
pKey
,
int
kLen
,
int
*
pCRst
);
static
int
tdbDefaultKeyCmprFn
(
const
void
*
pKey1
,
int
keyLen1
,
const
void
*
pKey2
,
int
keyLen2
);
static
int
tdbDefaultKeyCmprFn
(
const
void
*
pKey1
,
int
keyLen1
,
const
void
*
pKey2
,
int
keyLen2
);
static
int
tdbBtreeOpenImpl
(
SBTree
*
pBt
);
static
int
tdbBtreeOpenImpl
(
SBTree
*
pBt
);
static
int
tdbBtreeZeroPage
(
SPage
*
pPage
,
void
*
arg
);
static
int
tdbBtreeZeroPage
(
SPage
*
pPage
,
void
*
arg
);
...
@@ -65,7 +75,11 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg);
...
@@ -65,7 +75,11 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg);
static
int
tdbBtreeEncodeCell
(
SPage
*
pPage
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
,
SCell
*
pCell
,
static
int
tdbBtreeEncodeCell
(
SPage
*
pPage
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
,
SCell
*
pCell
,
int
*
szCell
);
int
*
szCell
);
static
int
tdbBtreeDecodeCell
(
SPage
*
pPage
,
const
SCell
*
pCell
,
SCellDecoder
*
pDecoder
);
static
int
tdbBtreeDecodeCell
(
SPage
*
pPage
,
const
SCell
*
pCell
,
SCellDecoder
*
pDecoder
);
static
int
tdbBtreeBalance
(
SBtCursor
*
pCur
);
static
int
tdbBtreeBalance
(
SBTC
*
pCur
);
static
int
tdbBtreeCellSize
(
const
SPage
*
pPage
,
SCell
*
pCell
);
static
int
tdbBtcMoveToNext
(
SBTC
*
pBtc
);
static
int
tdbBtcMoveDownward
(
SBTC
*
pCur
,
SPgno
pgno
);
static
int
tdbBtcMoveUpward
(
SBTC
*
pBtc
);
int
tdbBtreeOpen
(
int
keyLen
,
int
valLen
,
SPager
*
pPager
,
FKeyComparator
kcmpr
,
SBTree
**
ppBt
)
{
int
tdbBtreeOpen
(
int
keyLen
,
int
valLen
,
SPager
*
pPager
,
FKeyComparator
kcmpr
,
SBTree
**
ppBt
)
{
SBTree
*
pBt
;
SBTree
*
pBt
;
...
@@ -73,7 +87,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, S
...
@@ -73,7 +87,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, S
*
ppBt
=
NULL
;
*
ppBt
=
NULL
;
pBt
=
(
SBTree
*
)
taosMemoryC
alloc
(
1
,
sizeof
(
*
pBt
));
pBt
=
(
SBTree
*
)
c
alloc
(
1
,
sizeof
(
*
pBt
));
if
(
pBt
==
NULL
)
{
if
(
pBt
==
NULL
)
{
return
-
1
;
return
-
1
;
}
}
...
@@ -107,7 +121,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, S
...
@@ -107,7 +121,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, S
// TODO: pBt->root
// TODO: pBt->root
ret
=
tdbBtreeOpenImpl
(
pBt
);
ret
=
tdbBtreeOpenImpl
(
pBt
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
taosMemoryF
ree
(
pBt
);
f
ree
(
pBt
);
return
-
1
;
return
-
1
;
}
}
...
@@ -120,16 +134,7 @@ int tdbBtreeClose(SBTree *pBt) {
...
@@ -120,16 +134,7 @@ int tdbBtreeClose(SBTree *pBt) {
return
0
;
return
0
;
}
}
int
tdbBtreeCursor
(
SBtCursor
*
pCur
,
SBTree
*
pBt
)
{
int
tdbBtCursorInsert
(
SBTC
*
pCur
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
)
{
pCur
->
pBt
=
pBt
;
pCur
->
iPage
=
-
1
;
pCur
->
pPage
=
NULL
;
pCur
->
idx
=
-
1
;
return
0
;
}
int
tdbBtCursorInsert
(
SBtCursor
*
pCur
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
)
{
int
ret
;
int
ret
;
int
idx
;
int
idx
;
SPager
*
pPager
;
SPager
*
pPager
;
...
@@ -145,7 +150,7 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
...
@@ -145,7 +150,7 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
}
}
if
(
pCur
->
idx
==
-
1
)
{
if
(
pCur
->
idx
==
-
1
)
{
ASSERT
(
TDB_PAGE_
N
CELLS
(
pCur
->
pPage
)
==
0
);
ASSERT
(
TDB_PAGE_
TOTAL_
CELLS
(
pCur
->
pPage
)
==
0
);
idx
=
0
;
idx
=
0
;
}
else
{
}
else
{
if
(
cret
>
0
)
{
if
(
cret
>
0
)
{
...
@@ -161,7 +166,7 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
...
@@ -161,7 +166,7 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
// TODO: refact code here
// TODO: refact code here
pBt
=
pCur
->
pBt
;
pBt
=
pCur
->
pBt
;
if
(
!
pBt
->
pTmp
)
{
if
(
!
pBt
->
pTmp
)
{
pBt
->
pTmp
=
(
u8
*
)
taosMemoryM
alloc
(
pBt
->
pageSize
);
pBt
->
pTmp
=
(
u8
*
)
m
alloc
(
pBt
->
pageSize
);
if
(
pBt
->
pTmp
==
NULL
)
{
if
(
pBt
->
pTmp
==
NULL
)
{
return
-
1
;
return
-
1
;
}
}
...
@@ -176,7 +181,7 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
...
@@ -176,7 +181,7 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
}
}
// Insert the cell to the index
// Insert the cell to the index
ret
=
tdbPageInsertCell
(
pCur
->
pPage
,
idx
,
pCell
,
szCell
);
ret
=
tdbPageInsertCell
(
pCur
->
pPage
,
idx
,
pCell
,
szCell
,
0
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
return
-
1
;
return
-
1
;
}
}
...
@@ -192,12 +197,36 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
...
@@ -192,12 +197,36 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
return
0
;
return
0
;
}
}
static
int
tdbBtCursorMoveToChild
(
SBtCursor
*
pCur
,
SPgno
pgno
)
{
int
tdbBtreeGet
(
SBTree
*
pBt
,
const
void
*
pKey
,
int
kLen
,
void
**
ppVal
,
int
*
vLen
)
{
// TODO
SBTC
btc
;
SCell
*
pCell
;
int
cret
;
void
*
pVal
;
SCellDecoder
cd
;
tdbBtcOpen
(
&
btc
,
pBt
);
tdbBtCursorMoveTo
(
&
btc
,
pKey
,
kLen
,
&
cret
);
if
(
cret
)
{
return
cret
;
}
pCell
=
tdbPageGetCell
(
btc
.
pPage
,
btc
.
idx
);
tdbBtreeDecodeCell
(
btc
.
pPage
,
pCell
,
&
cd
);
*
vLen
=
cd
.
vLen
;
pVal
=
TDB_REALLOC
(
*
ppVal
,
*
vLen
);
if
(
pVal
==
NULL
)
{
return
-
1
;
}
*
ppVal
=
pVal
;
memcpy
(
*
ppVal
,
cd
.
pVal
,
cd
.
vLen
);
return
0
;
return
0
;
}
}
static
int
tdbBtCursorMoveTo
(
SB
tCursor
*
pCur
,
const
void
*
pKey
,
int
kLen
,
int
*
pCRst
)
{
static
int
tdbBtCursorMoveTo
(
SB
TC
*
pCur
,
const
void
*
pKey
,
int
kLen
,
int
*
pCRst
)
{
int
ret
;
int
ret
;
SBTree
*
pBt
;
SBTree
*
pBt
;
SPager
*
pPager
;
SPager
*
pPager
;
...
@@ -218,9 +247,9 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
...
@@ -218,9 +247,9 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
pCur
->
iPage
=
0
;
pCur
->
iPage
=
0
;
if
(
TDB_PAGE_
N
CELLS
(
pCur
->
pPage
)
==
0
)
{
if
(
TDB_PAGE_
TOTAL_
CELLS
(
pCur
->
pPage
)
==
0
)
{
// Current page is empty
// Current page is empty
ASSERT
(
TDB_FLAG_IS
(
TDB_PAGE_FLAGS
(
pCur
->
pPage
),
TDB_BTREE_ROOT
|
TDB_BTREE_LEAF
));
//
ASSERT(TDB_FLAG_IS(TDB_PAGE_FLAGS(pCur->pPage), TDB_BTREE_ROOT | TDB_BTREE_LEAF));
return
0
;
return
0
;
}
}
...
@@ -231,7 +260,7 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
...
@@ -231,7 +260,7 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
SCellDecoder
cd
=
{
0
};
SCellDecoder
cd
=
{
0
};
pPage
=
pCur
->
pPage
;
pPage
=
pCur
->
pPage
;
nCells
=
TDB_PAGE_
N
CELLS
(
pPage
);
nCells
=
TDB_PAGE_
TOTAL_
CELLS
(
pPage
);
lidx
=
0
;
lidx
=
0
;
ridx
=
nCells
-
1
;
ridx
=
nCells
-
1
;
...
@@ -242,7 +271,7 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
...
@@ -242,7 +271,7 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
midx
=
(
lidx
+
ridx
)
>>
1
;
midx
=
(
lidx
+
ridx
)
>>
1
;
pCell
=
TDB_PAGE_CELL_AT
(
pPage
,
midx
);
pCell
=
tdbPageGetCell
(
pPage
,
midx
);
ret
=
tdbBtreeDecodeCell
(
pPage
,
pCell
,
&
cd
);
ret
=
tdbBtreeDecodeCell
(
pPage
,
pCell
,
&
cd
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
// TODO: handle error
// TODO: handle error
...
@@ -265,8 +294,8 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
...
@@ -265,8 +294,8 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
}
}
// Move downward or break
// Move downward or break
u
16
flags
=
TDB_PAGE
_FLAGS
(
pPage
);
u
8
flags
=
TDB_BTREE_PAGE_GET
_FLAGS
(
pPage
);
u8
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
u8
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
if
(
leaf
)
{
if
(
leaf
)
{
pCur
->
idx
=
midx
;
pCur
->
idx
=
midx
;
*
pCRst
=
c
;
*
pCRst
=
c
;
...
@@ -274,18 +303,16 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
...
@@ -274,18 +303,16 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
}
else
{
}
else
{
if
(
c
<=
0
)
{
if
(
c
<=
0
)
{
pCur
->
idx
=
midx
;
pCur
->
idx
=
midx
;
tdbBt
CursorMoveToChil
d
(
pCur
,
cd
.
pgno
);
tdbBt
cMoveDownwar
d
(
pCur
,
cd
.
pgno
);
}
else
{
}
else
{
pCur
->
idx
=
midx
+
1
;
if
(
midx
==
nCells
-
1
)
{
if
(
midx
==
nCells
-
1
)
{
/* Move to right-most child */
/* Move to right-most child */
pCur
->
idx
=
midx
+
1
;
tdbBtcMoveDownward
(
pCur
,
((
SIntHdr
*
)
pCur
->
pPage
->
pData
)
->
pgno
);
tdbBtCursorMoveToChild
(
pCur
,
((
SBtPageHdr
*
)(
pPage
->
pAmHdr
))
->
rChild
);
}
else
{
}
else
{
// TODO: reset cd as uninitialized
pCell
=
tdbPageGetCell
(
pPage
,
pCur
->
idx
);
pCur
->
idx
=
midx
+
1
;
pCell
=
TDB_PAGE_CELL_AT
(
pPage
,
midx
+
1
);
tdbBtreeDecodeCell
(
pPage
,
pCell
,
&
cd
);
tdbBtreeDecodeCell
(
pPage
,
pCell
,
&
cd
);
tdbBt
CursorMoveToChil
d
(
pCur
,
cd
.
pgno
);
tdbBt
cMoveDownwar
d
(
pCur
,
cd
.
pgno
);
}
}
}
}
}
}
...
@@ -299,32 +326,6 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
...
@@ -299,32 +326,6 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
return
0
;
return
0
;
}
}
static
int
tdbBtCursorMoveToRoot
(
SBtCursor
*
pCur
)
{
SBTree
*
pBt
;
SPager
*
pPager
;
SPage
*
pPage
;
int
ret
;
pBt
=
pCur
->
pBt
;
pPager
=
pBt
->
pPager
;
// pPage = tdbPagerGet(pPager, pBt->root, true);
// if (pPage == NULL) {
// // TODO: handle error
// }
// ret = tdbInitBtPage(pPage, &pBtPage);
// if (ret < 0) {
// // TODO
// return 0;
// }
// pCur->pPage = pBtPage;
// pCur->iPage = 0;
return
0
;
}
static
int
tdbDefaultKeyCmprFn
(
const
void
*
pKey1
,
int
keyLen1
,
const
void
*
pKey2
,
int
keyLen2
)
{
static
int
tdbDefaultKeyCmprFn
(
const
void
*
pKey1
,
int
keyLen1
,
const
void
*
pKey2
,
int
keyLen2
)
{
int
mlen
;
int
mlen
;
int
cret
;
int
cret
;
...
@@ -363,7 +364,7 @@ static int tdbBtreeOpenImpl(SBTree *pBt) {
...
@@ -363,7 +364,7 @@ static int tdbBtreeOpenImpl(SBTree *pBt) {
}
}
// Try to create a new database
// Try to create a new database
SBtree
Zero
PageArg
zArg
=
{.
flags
=
TDB_BTREE_ROOT
|
TDB_BTREE_LEAF
,
.
pBt
=
pBt
};
SBtree
Init
PageArg
zArg
=
{.
flags
=
TDB_BTREE_ROOT
|
TDB_BTREE_LEAF
,
.
pBt
=
pBt
};
ret
=
tdbPagerNewPage
(
pBt
->
pPager
,
&
pgno
,
&
pPage
,
tdbBtreeZeroPage
,
&
zArg
);
ret
=
tdbPagerNewPage
(
pBt
->
pPager
,
&
pgno
,
&
pPage
,
tdbBtreeZeroPage
,
&
zArg
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
return
-
1
;
return
-
1
;
...
@@ -379,28 +380,19 @@ static int tdbBtreeOpenImpl(SBTree *pBt) {
...
@@ -379,28 +380,19 @@ static int tdbBtreeOpenImpl(SBTree *pBt) {
static
int
tdbBtreeInitPage
(
SPage
*
pPage
,
void
*
arg
)
{
static
int
tdbBtreeInitPage
(
SPage
*
pPage
,
void
*
arg
)
{
SBTree
*
pBt
;
SBTree
*
pBt
;
u
16
flags
;
u
8
flags
;
u8
isLeaf
;
u8
isLeaf
;
pBt
=
(
SBTree
*
)
arg
;
pBt
=
(
SBTree
*
)
arg
;
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pPage
);
flags
=
TDB_PAGE_FLAGS
(
pPage
);
isLeaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
isLeaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
if
(
isLeaf
)
{
pPage
->
szAmHdr
=
0
;
ASSERT
(
flags
==
TDB_BTREE_PAGE_GET_FLAGS
(
pPage
));
}
else
{
pPage
->
szAmHdr
=
sizeof
(
SBtPageHdr
);
tdbPageInit
(
pPage
,
isLeaf
?
sizeof
(
SLeafHdr
)
:
sizeof
(
SIntHdr
),
tdbBtreeCellSize
);
}
pPage
->
pPageHdr
=
pPage
->
pData
;
pPage
->
pAmHdr
=
pPage
->
pPageHdr
+
pPage
->
pPageMethods
->
szPageHdr
;
pPage
->
pCellIdx
=
pPage
->
pAmHdr
+
pPage
->
szAmHdr
;
pPage
->
pFreeStart
=
pPage
->
pCellIdx
+
pPage
->
pPageMethods
->
szOffset
*
TDB_PAGE_NCELLS
(
pPage
);
pPage
->
pFreeEnd
=
pPage
->
pData
+
TDB_PAGE_CCELLS
(
pPage
);
pPage
->
pPageFtr
=
(
SPageFtr
*
)(
pPage
->
pData
+
pPage
->
pageSize
-
sizeof
(
SPageFtr
));
TDB_BTREE_ASSERT_FLAG
(
flags
);
TDB_BTREE_ASSERT_FLAG
(
flags
);
// Init other fields
if
(
isLeaf
)
{
if
(
isLeaf
)
{
pPage
->
kLen
=
pBt
->
keyLen
;
pPage
->
kLen
=
pBt
->
keyLen
;
pPage
->
vLen
=
pBt
->
valLen
;
pPage
->
vLen
=
pBt
->
valLen
;
...
@@ -413,30 +405,38 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg) {
...
@@ -413,30 +405,38 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg) {
pPage
->
minLocal
=
pBt
->
minLocal
;
pPage
->
minLocal
=
pBt
->
minLocal
;
}
}
// TODO: need to update the SPage.nFree
pPage
->
nFree
=
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
;
pPage
->
nOverflow
=
0
;
return
0
;
return
0
;
}
}
static
int
tdbBtreeZeroPage
(
SPage
*
pPage
,
void
*
arg
)
{
static
int
tdbBtreeZeroPage
(
SPage
*
pPage
,
void
*
arg
)
{
u
16
flags
;
u
8
flags
;
SBTree
*
pBt
;
SBTree
*
pBt
;
u8
isLeaf
;
flags
=
((
SBtreeZeroPageArg
*
)
arg
)
->
flags
;
flags
=
((
SBtreeInitPageArg
*
)
arg
)
->
flags
;
pBt
=
((
SBtreeZeroPageArg
*
)
arg
)
->
pBt
;
pBt
=
((
SBtreeInitPageArg
*
)
arg
)
->
pBt
;
isLeaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
pPage
->
pPageHdr
=
pPage
->
pData
;
tdbPageZero
(
pPage
,
isLeaf
?
sizeof
(
SLeafHdr
)
:
sizeof
(
SIntHdr
),
tdbBtreeCellSize
)
;
// Init the page header
if
(
isLeaf
)
{
TDB_PAGE_FLAGS_SET
(
pPage
,
flags
);
SLeafHdr
*
pLeafHdr
=
(
SLeafHdr
*
)(
pPage
->
pData
);
TDB_PAGE_NCELLS_SET
(
pPage
,
0
);
pLeafHdr
->
flags
=
flags
;
TDB_PAGE_CCELLS_SET
(
pPage
,
pBt
->
pageSize
-
sizeof
(
SPageFtr
));
TDB_PAGE_FCELL_SET
(
pPage
,
0
);
TDB_PAGE_NFREE_SET
(
pPage
,
0
);
tdbBtreeInitPage
(
pPage
,
(
void
*
)
pBt
);
pPage
->
kLen
=
pBt
->
keyLen
;
pPage
->
vLen
=
pBt
->
valLen
;
pPage
->
maxLocal
=
pBt
->
maxLeaf
;
pPage
->
minLocal
=
pBt
->
minLeaf
;
}
else
{
SIntHdr
*
pIntHdr
=
(
SIntHdr
*
)(
pPage
->
pData
);
pIntHdr
->
flags
=
flags
;
pIntHdr
->
pgno
=
0
;
pPage
->
kLen
=
pBt
->
keyLen
;
pPage
->
vLen
=
sizeof
(
SPgno
);
pPage
->
maxLocal
=
pBt
->
maxLocal
;
pPage
->
minLocal
=
pBt
->
minLocal
;
}
return
0
;
return
0
;
}
}
...
@@ -452,55 +452,34 @@ typedef struct {
...
@@ -452,55 +452,34 @@ typedef struct {
SPage
*
pNewPages
[
5
];
SPage
*
pNewPages
[
5
];
}
SBtreeBalanceHelper
;
}
SBtreeBalanceHelper
;
static
int
tdbBtreeCopyPageContent
(
SPage
*
pFrom
,
SPage
*
pTo
)
{
int
nCells
=
TDB_PAGE_NCELLS
(
pFrom
);
int
cCells
=
TDB_PAGE_CCELLS
(
pFrom
);
int
fCell
=
TDB_PAGE_FCELL
(
pFrom
);
int
nFree
=
TDB_PAGE_NFREE
(
pFrom
);
pTo
->
pFreeStart
=
pTo
->
pCellIdx
+
nCells
*
pFrom
->
pPageMethods
->
szOffset
;
memcpy
(
pTo
->
pCellIdx
,
pFrom
->
pCellIdx
,
nCells
*
pFrom
->
pPageMethods
->
szOffset
);
pTo
->
pFreeEnd
=
(
u8
*
)
pTo
->
pPageFtr
-
(
u8
*
)(
pFrom
->
pPageFtr
)
+
pFrom
->
pFreeEnd
;
memcpy
(
pTo
->
pFreeEnd
,
pFrom
->
pFreeEnd
,
(
u8
*
)
pFrom
->
pPageFtr
-
pFrom
->
pFreeEnd
);
TDB_PAGE_NCELLS_SET
(
pTo
,
nCells
);
TDB_PAGE_CCELLS_SET
(
pTo
,
cCells
);
TDB_PAGE_FCELL_SET
(
pTo
,
fCell
);
TDB_PAGE_NFREE_SET
(
pTo
,
nFree
);
// TODO: update other fields
return
0
;
}
static
int
tdbBtreeBalanceDeeper
(
SBTree
*
pBt
,
SPage
*
pRoot
,
SPage
**
ppChild
)
{
static
int
tdbBtreeBalanceDeeper
(
SBTree
*
pBt
,
SPage
*
pRoot
,
SPage
**
ppChild
)
{
SPager
*
pPager
;
SPager
*
pPager
;
SPage
*
pChild
;
SPage
*
pChild
;
SPgno
pgnoChild
;
SPgno
pgnoChild
;
int
ret
;
int
ret
;
SBtreeZeroPageArg
zArg
;
u8
flags
;
SIntHdr
*
pIntHdr
;
SBtreeInitPageArg
zArg
;
u8
leaf
;
pPager
=
pRoot
->
pPager
;
pPager
=
pRoot
->
pPager
;
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pRoot
);
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
// Allocate a new child page
// Allocate a new child page
zArg
.
flags
=
TDB_
BTREE_LEAF
;
zArg
.
flags
=
TDB_
FLAG_REMOVE
(
flags
,
TDB_BTREE_ROOT
)
;
zArg
.
pBt
=
pBt
;
zArg
.
pBt
=
pBt
;
ret
=
tdbPagerNewPage
(
pPager
,
&
pgnoChild
,
&
pChild
,
tdbBtreeZeroPage
,
&
zArg
);
ret
=
tdbPagerNewPage
(
pPager
,
&
pgnoChild
,
&
pChild
,
tdbBtreeZeroPage
,
&
zArg
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
return
-
1
;
return
-
1
;
}
}
// Copy the root page content to the child page
if
(
!
leaf
)
{
ret
=
tdbBtreeCopyPageContent
(
pRoot
,
pChild
);
((
SIntHdr
*
)
pChild
->
pData
)
->
pgno
=
((
SIntHdr
*
)(
pRoot
->
pData
))
->
pgno
;
if
(
ret
<
0
)
{
return
-
1
;
}
}
pChild
->
nOverflow
=
pRoot
->
nOverflow
;
// Copy the root page content to the child page
for
(
int
i
=
0
;
i
<
pChild
->
nOverflow
;
i
++
)
{
tdbPageCopy
(
pRoot
,
pChild
);
pChild
->
apOvfl
[
i
]
=
pRoot
->
apOvfl
[
i
];
pChild
->
aiOvfl
[
i
]
=
pRoot
->
aiOvfl
[
i
];
}
// Reinitialize the root page
// Reinitialize the root page
zArg
.
flags
=
TDB_BTREE_ROOT
;
zArg
.
flags
=
TDB_BTREE_ROOT
;
...
@@ -510,225 +489,328 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) {
...
@@ -510,225 +489,328 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) {
return
-
1
;
return
-
1
;
}
}
((
SBtPageHdr
*
)
pRoot
->
pAmHdr
)[
0
].
rChild
=
pgnoChild
;
pIntHdr
=
(
SIntHdr
*
)(
pRoot
->
pData
);
pIntHdr
->
pgno
=
pgnoChild
;
*
ppChild
=
pChild
;
*
ppChild
=
pChild
;
return
0
;
return
0
;
}
}
static
int
tdbBtreeBalanceStep1
(
SBtreeBalanceHelper
*
pBlh
)
{
static
int
tdbBtreeBalanceNonRoot
(
SBTree
*
pBt
,
SPage
*
pParent
,
int
idx
)
{
int
nCells
;
int
ret
;
int
i
;
int
idxStart
;
int
nChild
;
int
ret
;
SPage
*
pParent
;
SPgno
pgno
;
SCell
*
pCell
;
SCellDecoder
cd
;
SBTree
*
pBt
;
pParent
=
pBlh
->
pParent
;
nCells
=
TDB_PAGE_NCELLS
(
pParent
);
nChild
=
nCells
+
1
;
pBt
=
pBlh
->
pBt
;
// TODO: ASSERT below needs to be removed
ASSERT
(
pParent
->
nOverflow
==
0
);
ASSERT
(
pBlh
->
idx
<=
nCells
);
if
(
nChild
<
3
)
{
int
nOlds
;
idxStart
=
0
;
SPage
*
pOlds
[
3
]
=
{
0
};
pBlh
->
nOld
=
nChild
;
SCell
*
pDivCell
[
3
]
=
{
0
};
}
else
{
int
szDivCell
[
3
];
if
(
pBlh
->
idx
==
0
)
{
int
sIdx
;
idxStart
=
0
;
u8
childNotLeaf
;
}
else
if
(
pBlh
->
idx
==
nCells
)
{
SPgno
rPgno
;
idxStart
=
pBlh
->
idx
-
2
;
{
// Find 3 child pages at most to do balance
int
nCells
=
TDB_PAGE_TOTAL_CELLS
(
pParent
);
SCell
*
pCell
;
if
(
nCells
<=
2
)
{
sIdx
=
0
;
nOlds
=
nCells
+
1
;
}
else
{
}
else
{
idxStart
=
pBlh
->
idx
-
1
;
// has more than three child pages
if
(
idx
==
0
)
{
sIdx
=
0
;
}
else
if
(
idx
==
nCells
)
{
sIdx
=
idx
-
2
;
}
else
{
sIdx
=
idx
-
1
;
}
nOlds
=
3
;
}
}
pBlh
->
nOld
=
3
;
for
(
int
i
=
0
;
i
<
nOlds
;
i
++
)
{
}
ASSERT
(
sIdx
+
i
<=
nCells
);
i
=
pBlh
->
nOld
-
1
;
SPgno
pgno
;
if
(
sIdx
+
i
==
nCells
)
{
ASSERT
(
!
TDB_BTREE_PAGE_IS_LEAF
(
TDB_BTREE_PAGE_GET_FLAGS
(
pParent
)));
pgno
=
((
SIntHdr
*
)(
pParent
->
pData
))
->
pgno
;
}
else
{
pCell
=
tdbPageGetCell
(
pParent
,
sIdx
+
i
);
pgno
=
*
(
SPgno
*
)
pCell
;
}
if
(
idxStart
+
i
==
nCells
)
{
ret
=
tdbPagerFetchPage
(
pBt
->
pPager
,
pgno
,
pOlds
+
i
,
tdbBtreeInitPage
,
pBt
);
pgno
=
((
SBtPageHdr
*
)(
pParent
->
pAmHdr
))[
0
].
rChild
;
if
(
ret
<
0
)
{
}
else
{
ASSERT
(
0
);
pCell
=
TDB_PAGE_CELL_AT
(
pParent
,
idxStart
+
i
);
return
-
1
;
// TODO: no need to decode the payload part, and even the kLen, vLen part
}
// we only need the pgno part
ret
=
tdbBtreeDecodeCell
(
pParent
,
pCell
,
&
cd
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
pgno
=
cd
.
pgno
;
}
for
(;;)
{
ret
=
tdbPagerFetchPage
(
pBt
->
pPager
,
pgno
,
&
(
pBlh
->
pOldPages
[
i
]),
tdbBtreeInitPage
,
pBt
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
}
// copy the parent key out if child pages are not leaf page
childNotLeaf
=
!
TDB_BTREE_PAGE_IS_LEAF
(
TDB_BTREE_PAGE_GET_FLAGS
(
pOlds
[
0
]));
if
(
childNotLeaf
)
{
for
(
int
i
=
0
;
i
<
nOlds
;
i
++
)
{
if
(
sIdx
+
i
<
TDB_PAGE_TOTAL_CELLS
(
pParent
))
{
pCell
=
tdbPageGetCell
(
pParent
,
sIdx
+
i
);
szDivCell
[
i
]
=
tdbBtreeCellSize
(
pParent
,
pCell
);
pDivCell
[
i
]
=
malloc
(
szDivCell
[
i
]);
memcpy
(
pDivCell
[
i
],
pCell
,
szDivCell
[
i
]);
}
// Loop over
if
(
i
<
nOlds
-
1
)
{
if
((
i
--
)
==
0
)
break
;
((
SPgno
*
)
pDivCell
[
i
])[
0
]
=
((
SIntHdr
*
)
pOlds
[
i
]
->
pData
)
->
pgno
;
((
SIntHdr
*
)
pOlds
[
i
]
->
pData
)
->
pgno
=
0
;
{
tdbPageInsertCell
(
pOlds
[
i
],
TDB_PAGE_TOTAL_CELLS
(
pOlds
[
i
]),
pDivCell
[
i
],
szDivCell
[
i
],
1
);
// TODO
}
// ASSERT(0);
}
rPgno
=
((
SIntHdr
*
)
pOlds
[
nOlds
-
1
]
->
pData
)
->
pgno
;
}
}
}
// drop the cells on parent page
for
(
int
i
=
0
;
i
<
nOlds
;
i
++
)
{
return
0
;
nCells
=
TDB_PAGE_TOTAL_CELLS
(
pParent
);
}
if
(
sIdx
<
nCells
)
{
tdbPageDropCell
(
pParent
,
sIdx
);
static
int
tdbBtreeBalanceStep2
(
SBtreeBalanceHelper
*
pBlh
)
{
}
else
{
#if 0
((
SIntHdr
*
)
pParent
->
pData
)
->
pgno
=
0
;
SPage *pPage;
}
int oidx;
int cidx;
int limit;
SCell *pCell;
for (int i = 0; i < pBlh->nOld; i++) {
pPage = pBlh->pOldPages[i];
oidx = 0;
cidx = 0;
if (oidx < pPage->nOverflow) {
limit = pPage->aiOvfl[oidx];
} else {
limit = pPage->pPageHdr->nCells;
}
}
}
// Loop to copy each cell pointer out
int
nNews
=
0
;
for (;;) {
struct
{
if (oidx >= pPage->nOverflow && cidx >= pPage->pPageHdr->nCells) break;
int
cnt
;
int
size
;
if (cidx < limit) {
int
iPage
;
// Get local cells
int
oIdx
;
pCell = TDB_PAGE_CELL_AT(pPage, cidx);
}
infoNews
[
5
]
=
{
0
};
} else if (cidx == limit) {
// Get overflow cells
{
// Get how many new pages are needed and the new distribution
pCell = pPage->apOvfl[oidx++];
// first loop to find minimum number of pages needed
if (oidx < pPage->nOverflow) {
for
(
int
oPage
=
0
;
oPage
<
nOlds
;
oPage
++
)
{
limit = pPage->aiOvfl[oidx];
SPage
*
pPage
=
pOlds
[
oPage
];
} else {
SCell
*
pCell
;
limit = pPage->pPageHdr->nCells;
int
cellBytes
;
int
oIdx
;
for
(
oIdx
=
0
;
oIdx
<
TDB_PAGE_TOTAL_CELLS
(
pPage
);
oIdx
++
)
{
pCell
=
tdbPageGetCell
(
pPage
,
oIdx
);
cellBytes
=
TDB_BYTES_CELL_TAKEN
(
pPage
,
pCell
);
if
(
infoNews
[
nNews
].
size
+
cellBytes
>
TDB_PAGE_USABLE_SIZE
(
pPage
))
{
// page is full, use a new page
nNews
++
;
ASSERT
(
infoNews
[
nNews
].
size
+
cellBytes
<=
TDB_PAGE_USABLE_SIZE
(
pPage
));
if
(
childNotLeaf
)
{
// for non-child page, this cell is used as the right-most child,
// the divider cell to parent as well
continue
;
}
}
}
} else {
infoNews
[
nNews
].
cnt
++
;
ASSERT(0);
infoNews
[
nNews
].
size
+=
cellBytes
;
infoNews
[
nNews
].
iPage
=
oPage
;
infoNews
[
nNews
].
oIdx
=
oIdx
;
}
}
}
}
{
nNews
++
;
// TODO: Copy divider cells here
}
}
/* TODO */
// back loop to make the distribution even
for
(
int
iNew
=
nNews
-
1
;
iNew
>
0
;
iNew
--
)
{
SCell
*
pCell
;
int
szLCell
,
szRCell
;
#endif
for
(;;)
{
return
0
;
pCell
=
tdbPageGetCell
(
pOlds
[
infoNews
[
iNew
-
1
].
iPage
],
infoNews
[
iNew
-
1
].
oIdx
);
}
static
int
tdbBtreeBalanceStep3
(
SBtreeBalanceHelper
*
pBlh
)
{
if
(
childNotLeaf
)
{
// Figure out number of pages needed after balance
szLCell
=
szRCell
=
tdbBtreeCellSize
(
pOlds
[
infoNews
[
iNew
-
1
].
iPage
],
pCell
);
for
(
int
i
=
0
;
i
<
pBlh
->
nOld
;
i
++
)
{
}
else
{
/* TODO */
szLCell
=
tdbBtreeCellSize
(
pOlds
[
infoNews
[
iNew
-
1
].
iPage
],
pCell
);
}
int
iPage
=
infoNews
[
iNew
-
1
].
iPage
;
int
oIdx
=
infoNews
[
iNew
-
1
].
oIdx
+
1
;
SPage
*
pPage
;
for
(;;)
{
pPage
=
pOlds
[
iPage
];
if
(
oIdx
<
TDB_PAGE_TOTAL_CELLS
(
pPage
))
{
break
;
}
iPage
++
;
oIdx
=
0
;
}
return
0
;
pCell
=
tdbPageGetCell
(
pPage
,
oIdx
);
}
szRCell
=
tdbBtreeCellSize
(
pPage
,
pCell
);
}
static
int
tdbBtreeBalanceStep4
(
SBtreeBalanceHelper
*
pBlh
)
{
ASSERT
(
infoNews
[
iNew
-
1
].
cnt
>
0
);
// TODO
return
0
;
}
static
int
tdbBtreeBalanceStep5
(
SBtreeBalanceHelper
*
pBlh
)
{
if
(
infoNews
[
iNew
].
size
+
szRCell
>=
infoNews
[
iNew
-
1
].
size
-
szRCell
)
{
// TODO
break
;
return
0
;
}
}
static
int
tdbBtreeBalanceStep6
(
SBtreeBalanceHelper
*
pBlh
)
{
// Move a cell right forward
// TODO
infoNews
[
iNew
-
1
].
cnt
--
;
return
0
;
infoNews
[
iNew
-
1
].
size
-=
szLCell
;
}
infoNews
[
iNew
-
1
].
oIdx
--
;
for
(;;)
{
if
(
infoNews
[
iNew
-
1
].
oIdx
>=
0
)
{
break
;
}
static
int
tdbBtreeBalanceNonRoot
(
SBTree
*
pBt
,
SPage
*
pParent
,
int
idx
)
{
infoNews
[
iNew
-
1
].
iPage
--
;
int
ret
;
infoNews
[
iNew
-
1
].
oIdx
=
TDB_PAGE_TOTAL_CELLS
(
pOlds
[
infoNews
[
iNew
-
1
].
iPage
])
-
1
;
SBtreeBalanceHelper
blh
;
}
ASSERT
(
!
TDB_BTREE_PAGE_IS_LEAF
(
TDB_PAGE_FLAGS
(
pParent
)));
infoNews
[
iNew
].
cnt
++
;
infoNews
[
iNew
].
size
+=
szRCell
;
}
}
}
blh
.
pBt
=
pBt
;
SPage
*
pNews
[
5
]
=
{
0
};
blh
.
pParent
=
pParent
;
{
// Allocate new pages, reuse the old page when possible
blh
.
idx
=
idx
;
// Step 1: find two sibling pages and get engough info about the old pages
SPgno
pgno
;
ret
=
tdbBtreeBalanceStep1
(
&
blh
);
SBtreeInitPageArg
iarg
;
if
(
ret
<
0
)
{
u8
flags
;
ASSERT
(
0
);
return
-
1
;
}
// Step 2: Load all cells on the old page and the divider cells
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pOlds
[
0
]);
ret
=
tdbBtreeBalanceStep2
(
&
blh
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
// Step 3: Get the number of pages needed to hold all cells
for
(
int
iNew
=
0
;
iNew
<
nNews
;
iNew
++
)
{
ret
=
tdbBtreeBalanceStep3
(
&
blh
);
if
(
iNew
<
nOlds
)
{
if
(
ret
<
0
)
{
pNews
[
iNew
]
=
pOlds
[
iNew
];
ASSERT
(
0
);
}
else
{
return
-
1
;
iarg
.
pBt
=
pBt
;
}
iarg
.
flags
=
flags
;
ret
=
tdbPagerNewPage
(
pBt
->
pPager
,
&
pgno
,
pNews
+
iNew
,
tdbBtreeZeroPage
,
&
iarg
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
}
}
}
// Step 4: Allocate enough new pages. Reuse old pages as much as possible
// TODO: sort the page according to the page number
ret
=
tdbBtreeBalanceStep4
(
&
blh
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
}
// Step 5: Insert new divider cells into pParent
{
// Do the real cell distribution
ret
=
tdbBtreeBalanceStep5
(
&
blh
);
SPage
*
pOldsCopy
[
3
]
=
{
0
};
if
(
ret
<
0
)
{
SCell
*
pCell
;
ASSERT
(
0
);
int
szCell
;
return
-
1
;
SBtreeInitPageArg
iarg
;
}
int
iNew
,
nNewCells
;
SCellDecoder
cd
;
iarg
.
pBt
=
pBt
;
iarg
.
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pOlds
[
0
]);
for
(
int
i
=
0
;
i
<
nOlds
;
i
++
)
{
tdbPageCreate
(
pOlds
[
0
]
->
pageSize
,
&
pOldsCopy
[
i
],
NULL
,
NULL
);
tdbBtreeZeroPage
(
pOldsCopy
[
i
],
&
iarg
);
tdbPageCopy
(
pOlds
[
i
],
pOldsCopy
[
i
]);
}
iNew
=
0
;
nNewCells
=
0
;
tdbBtreeZeroPage
(
pNews
[
iNew
],
&
iarg
);
for
(
int
iOld
=
0
;
iOld
<
nOlds
;
iOld
++
)
{
SPage
*
pPage
;
pPage
=
pOldsCopy
[
iOld
];
for
(
int
oIdx
=
0
;
oIdx
<
TDB_PAGE_TOTAL_CELLS
(
pPage
);
oIdx
++
)
{
pCell
=
tdbPageGetCell
(
pPage
,
oIdx
);
szCell
=
tdbBtreeCellSize
(
pPage
,
pCell
);
ASSERT
(
nNewCells
<=
infoNews
[
iNew
].
cnt
);
ASSERT
(
iNew
<
nNews
);
if
(
nNewCells
<
infoNews
[
iNew
].
cnt
)
{
tdbPageInsertCell
(
pNews
[
iNew
],
nNewCells
,
pCell
,
szCell
,
0
);
nNewCells
++
;
// insert parent page
if
(
!
childNotLeaf
&&
nNewCells
==
infoNews
[
iNew
].
cnt
)
{
SIntHdr
*
pIntHdr
=
(
SIntHdr
*
)
pParent
->
pData
;
if
(
iNew
==
nNews
-
1
&&
pIntHdr
->
pgno
==
0
)
{
pIntHdr
->
pgno
=
TDB_PAGE_PGNO
(
pNews
[
iNew
]);
}
else
{
tdbBtreeDecodeCell
(
pPage
,
pCell
,
&
cd
);
// TODO: pCell here may be inserted as an overflow cell, handle it
SCell
*
pNewCell
=
malloc
(
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
);
free
(
pNewCell
);
}
// move to next new page
iNew
++
;
nNewCells
=
0
;
if
(
iNew
<
nNews
)
{
tdbBtreeZeroPage
(
pNews
[
iNew
],
&
iarg
);
}
}
}
else
{
ASSERT
(
childNotLeaf
);
ASSERT
(
iNew
<
nNews
-
1
);
// set current new page right-most child
((
SIntHdr
*
)
pNews
[
iNew
]
->
pData
)
->
pgno
=
((
SPgno
*
)
pCell
)[
0
];
// insert to parent as divider cell
ASSERT
(
iNew
<
nNews
-
1
);
((
SPgno
*
)
pCell
)[
0
]
=
TDB_PAGE_PGNO
(
pNews
[
iNew
]);
tdbPageInsertCell
(
pParent
,
sIdx
++
,
pCell
,
szCell
,
0
);
// move to next new page
iNew
++
;
nNewCells
=
0
;
if
(
iNew
<
nNews
)
{
tdbBtreeZeroPage
(
pNews
[
iNew
],
&
iarg
);
}
}
}
}
// Step 6: Update the sibling pages
if
(
childNotLeaf
)
{
ret
=
tdbBtreeBalanceStep6
(
&
blh
);
ASSERT
(
TDB_PAGE_TOTAL_CELLS
(
pNews
[
nNews
-
1
])
==
infoNews
[
nNews
-
1
].
cnt
);
if
(
ret
<
0
)
{
((
SIntHdr
*
)(
pNews
[
nNews
-
1
]
->
pData
))
->
pgno
=
rPgno
;
ASSERT
(
0
);
return
-
1
;
}
{
SIntHdr
*
pIntHdr
=
(
SIntHdr
*
)
pParent
->
pData
;
// TODO: Reset states
if
(
pIntHdr
->
pgno
==
0
)
{
pIntHdr
->
pgno
=
TDB_PAGE_PGNO
(
pNews
[
nNews
-
1
]);
}
else
{
((
SPgno
*
)
pDivCell
[
nOlds
-
1
])[
0
]
=
TDB_PAGE_PGNO
(
pNews
[
nNews
-
1
]);
tdbPageInsertCell
(
pParent
,
sIdx
,
pDivCell
[
nOlds
-
1
],
szDivCell
[
nOlds
-
1
],
0
);
}
}
for
(
int
i
=
0
;
i
<
nOlds
;
i
++
)
{
tdbPageDestroy
(
pOldsCopy
[
i
],
NULL
,
NULL
);
}
}
}
{
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
// TODO: Clear resources
if
(
pDivCell
[
i
])
{
free
(
pDivCell
[
i
]);
}
}
}
return
0
;
return
0
;
}
}
static
int
tdbBtreeBalance
(
SB
tCursor
*
pCur
)
{
static
int
tdbBtreeBalance
(
SB
TC
*
pCur
)
{
int
iPage
;
int
iPage
;
SPage
*
pParent
;
SPage
*
pParent
;
SPage
*
pPage
;
SPage
*
pPage
;
int
ret
;
int
ret
;
u
16
flags
;
u
8
flags
;
u8
leaf
;
u8
leaf
;
u8
root
;
u8
root
;
...
@@ -736,17 +818,10 @@ static int tdbBtreeBalance(SBtCursor *pCur) {
...
@@ -736,17 +818,10 @@ static int tdbBtreeBalance(SBtCursor *pCur) {
for
(;;)
{
for
(;;)
{
iPage
=
pCur
->
iPage
;
iPage
=
pCur
->
iPage
;
pPage
=
pCur
->
pPage
;
pPage
=
pCur
->
pPage
;
flags
=
TDB_
PAGE
_FLAGS
(
pPage
);
flags
=
TDB_
BTREE_PAGE_GET
_FLAGS
(
pPage
);
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
root
=
TDB_BTREE_PAGE_IS_ROOT
(
flags
);
root
=
TDB_BTREE_PAGE_IS_ROOT
(
flags
);
// TODO: Get the page free space if not get yet
// if (pPage->nFree < 0) {
// if (tdbBtreeComputeFreeSpace(pPage) < 0) {
// return -1;
// }
// }
// when the page is not overflow and not too empty, the balance work
// when the page is not overflow and not too empty, the balance work
// is finished. Just break out the balance loop.
// is finished. Just break out the balance loop.
if
(
pPage
->
nOverflow
==
0
/* TODO: && pPage->nFree <= */
)
{
if
(
pPage
->
nOverflow
==
0
/* TODO: && pPage->nFree <= */
)
{
...
@@ -758,7 +833,7 @@ static int tdbBtreeBalance(SBtCursor *pCur) {
...
@@ -758,7 +833,7 @@ static int tdbBtreeBalance(SBtCursor *pCur) {
// ignore the case of empty
// ignore the case of empty
if
(
pPage
->
nOverflow
==
0
)
break
;
if
(
pPage
->
nOverflow
==
0
)
break
;
ret
=
tdbBtreeBalanceDeeper
(
pCur
->
pBt
,
p
Cur
->
p
Page
,
&
(
pCur
->
pgStack
[
1
]));
ret
=
tdbBtreeBalanceDeeper
(
pCur
->
pBt
,
pPage
,
&
(
pCur
->
pgStack
[
1
]));
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
return
-
1
;
return
-
1
;
}
}
...
@@ -817,9 +892,10 @@ static int tdbBtreeEncodePayload(SPage *pPage, u8 *pPayload, const void *pKey, i
...
@@ -817,9 +892,10 @@ static int tdbBtreeEncodePayload(SPage *pPage, u8 *pPayload, const void *pKey, i
return
0
;
return
0
;
}
}
// TODO: allow vLen = 0
static
int
tdbBtreeEncodeCell
(
SPage
*
pPage
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
,
SCell
*
pCell
,
static
int
tdbBtreeEncodeCell
(
SPage
*
pPage
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
,
SCell
*
pCell
,
int
*
szCell
)
{
int
*
szCell
)
{
u
16
flags
;
u
8
flags
;
u8
leaf
;
u8
leaf
;
int
nHeader
;
int
nHeader
;
int
nPayload
;
int
nPayload
;
...
@@ -830,10 +906,18 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo
...
@@ -830,10 +906,18 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo
nPayload
=
0
;
nPayload
=
0
;
nHeader
=
0
;
nHeader
=
0
;
flags
=
TDB_
PAGE
_FLAGS
(
pPage
);
flags
=
TDB_
BTREE_PAGE_GET
_FLAGS
(
pPage
);
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
// 1. Encode Header part
// 1. Encode Header part
/* Encode SPgno if interior page */
if
(
!
leaf
)
{
ASSERT
(
pPage
->
vLen
==
sizeof
(
SPgno
));
((
SPgno
*
)(
pCell
+
nHeader
))[
0
]
=
((
SPgno
*
)
pVal
)[
0
];
nHeader
=
nHeader
+
sizeof
(
SPgno
);
}
/* Encode kLen if need */
/* Encode kLen if need */
if
(
pPage
->
kLen
==
TDB_VARIANT_LEN
)
{
if
(
pPage
->
kLen
==
TDB_VARIANT_LEN
)
{
nHeader
+=
tdbPutVarInt
(
pCell
+
nHeader
,
kLen
);
nHeader
+=
tdbPutVarInt
(
pCell
+
nHeader
,
kLen
);
...
@@ -844,14 +928,6 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo
...
@@ -844,14 +928,6 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo
nHeader
+=
tdbPutVarInt
(
pCell
+
nHeader
,
vLen
);
nHeader
+=
tdbPutVarInt
(
pCell
+
nHeader
,
vLen
);
}
}
/* Encode SPgno if interior page */
if
(
!
leaf
)
{
ASSERT
(
pPage
->
vLen
==
sizeof
(
SPgno
));
((
SPgno
*
)(
pCell
+
nHeader
))[
0
]
=
((
SPgno
*
)
pVal
)[
0
];
nHeader
=
nHeader
+
sizeof
(
SPgno
);
}
// 2. Encode payload part
// 2. Encode payload part
if
(
leaf
)
{
if
(
leaf
)
{
ret
=
tdbBtreeEncodePayload
(
pPage
,
pCell
+
nHeader
,
pKey
,
kLen
,
pVal
,
vLen
,
&
nPayload
);
ret
=
tdbBtreeEncodePayload
(
pPage
,
pCell
+
nHeader
,
pKey
,
kLen
,
pVal
,
vLen
,
&
nPayload
);
...
@@ -893,13 +969,13 @@ static int tdbBtreeDecodePayload(SPage *pPage, const u8 *pPayload, SCellDecoder
...
@@ -893,13 +969,13 @@ static int tdbBtreeDecodePayload(SPage *pPage, const u8 *pPayload, SCellDecoder
}
}
static
int
tdbBtreeDecodeCell
(
SPage
*
pPage
,
const
SCell
*
pCell
,
SCellDecoder
*
pDecoder
)
{
static
int
tdbBtreeDecodeCell
(
SPage
*
pPage
,
const
SCell
*
pCell
,
SCellDecoder
*
pDecoder
)
{
u
16
flags
;
u
8
flags
;
u8
leaf
;
u8
leaf
;
int
nHeader
;
int
nHeader
;
int
ret
;
int
ret
;
nHeader
=
0
;
nHeader
=
0
;
flags
=
TDB_
PAGE
_FLAGS
(
pPage
);
flags
=
TDB_
BTREE_PAGE_GET
_FLAGS
(
pPage
);
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
// Clear the state of decoder
// Clear the state of decoder
...
@@ -910,6 +986,14 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
...
@@ -910,6 +986,14 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
pDecoder
->
pgno
=
0
;
pDecoder
->
pgno
=
0
;
// 1. Decode header part
// 1. Decode header part
if
(
!
leaf
)
{
ASSERT
(
pPage
->
vLen
==
sizeof
(
SPgno
));
pDecoder
->
pgno
=
((
SPgno
*
)(
pCell
+
nHeader
))[
0
];
pDecoder
->
pVal
=
(
u8
*
)(
&
(
pDecoder
->
pgno
));
nHeader
=
nHeader
+
sizeof
(
SPgno
);
}
if
(
pPage
->
kLen
==
TDB_VARIANT_LEN
)
{
if
(
pPage
->
kLen
==
TDB_VARIANT_LEN
)
{
nHeader
+=
tdbGetVarInt
(
pCell
+
nHeader
,
&
(
pDecoder
->
kLen
));
nHeader
+=
tdbGetVarInt
(
pCell
+
nHeader
,
&
(
pDecoder
->
kLen
));
}
else
{
}
else
{
...
@@ -922,14 +1006,6 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
...
@@ -922,14 +1006,6 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
pDecoder
->
vLen
=
pPage
->
vLen
;
pDecoder
->
vLen
=
pPage
->
vLen
;
}
}
if
(
!
leaf
)
{
ASSERT
(
pPage
->
vLen
==
sizeof
(
SPgno
));
pDecoder
->
pgno
=
((
SPgno
*
)(
pCell
+
nHeader
))[
0
];
pDecoder
->
pVal
=
(
u8
*
)(
&
(
pDecoder
->
pgno
));
nHeader
=
nHeader
+
sizeof
(
SPgno
);
}
// 2. Decode payload part
// 2. Decode payload part
ret
=
tdbBtreeDecodePayload
(
pPage
,
pCell
+
nHeader
,
pDecoder
);
ret
=
tdbBtreeDecodePayload
(
pPage
,
pCell
+
nHeader
,
pDecoder
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
...
@@ -939,4 +1015,313 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
...
@@ -939,4 +1015,313 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
return
0
;
return
0
;
}
}
static
int
tdbBtreeCellSize
(
const
SPage
*
pPage
,
SCell
*
pCell
)
{
u8
flags
;
u8
isLeaf
;
int
szCell
;
int
kLen
=
0
,
vLen
=
0
;
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pPage
);
isLeaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
szCell
=
0
;
if
(
!
isLeaf
)
{
szCell
+=
sizeof
(
SPgno
);
}
if
(
pPage
->
kLen
==
TDB_VARIANT_LEN
)
{
szCell
+=
tdbGetVarInt
(
pCell
+
szCell
,
&
kLen
);
}
else
{
kLen
=
pPage
->
kLen
;
}
if
(
isLeaf
)
{
if
(
pPage
->
vLen
==
TDB_VARIANT_LEN
)
{
szCell
+=
tdbGetVarInt
(
pCell
+
szCell
,
&
vLen
);
}
else
{
vLen
=
pPage
->
vLen
;
}
}
szCell
=
szCell
+
kLen
+
vLen
;
return
szCell
;
}
#endif
int
tdbBtcOpen
(
SBTC
*
pCur
,
SBTree
*
pBt
)
{
pCur
->
pBt
=
pBt
;
pCur
->
iPage
=
-
1
;
pCur
->
pPage
=
NULL
;
pCur
->
idx
=
-
1
;
return
0
;
}
int
tdbBtcMoveToFirst
(
SBTC
*
pBtc
)
{
int
ret
;
SBTree
*
pBt
;
SPager
*
pPager
;
u8
flags
;
SCell
*
pCell
;
SPgno
pgno
;
pBt
=
pBtc
->
pBt
;
pPager
=
pBt
->
pPager
;
if
(
pBtc
->
iPage
<
0
)
{
// move a clean cursor
ret
=
tdbPagerFetchPage
(
pPager
,
pBt
->
root
,
&
(
pBtc
->
pPage
),
tdbBtreeInitPage
,
pBt
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
pBtc
->
iPage
=
0
;
pBtc
->
idx
=
0
;
}
else
{
// move from a position
ASSERT
(
0
);
}
// move downward
for
(;;)
{
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pBtc
->
pPage
);
if
(
TDB_BTREE_PAGE_IS_LEAF
(
flags
))
break
;
pCell
=
tdbPageGetCell
(
pBtc
->
pPage
,
0
);
pgno
=
*
(
SPgno
*
)
pCell
;
ret
=
tdbBtcMoveDownward
(
pBtc
,
pgno
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
pBtc
->
idx
=
0
;
}
return
0
;
}
int
tdbBtcMoveToLast
(
SBTC
*
pBtc
)
{
int
ret
;
SBTree
*
pBt
;
SPager
*
pPager
;
u8
flags
;
SPgno
pgno
;
pBt
=
pBtc
->
pBt
;
pPager
=
pBt
->
pPager
;
if
(
pBtc
->
iPage
<
0
)
{
// move a clean cursor
ret
=
tdbPagerFetchPage
(
pPager
,
pBt
->
root
,
&
(
pBtc
->
pPage
),
tdbBtreeInitPage
,
pBt
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
pBtc
->
iPage
=
0
;
}
else
{
// move from a position
ASSERT
(
0
);
}
// move downward
for
(;;)
{
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pBtc
->
pPage
);
if
(
TDB_BTREE_PAGE_IS_LEAF
(
flags
))
{
// TODO: handle empty case
ASSERT
(
TDB_PAGE_TOTAL_CELLS
(
pBtc
->
pPage
)
>
0
);
pBtc
->
idx
=
TDB_PAGE_TOTAL_CELLS
(
pBtc
->
pPage
)
-
1
;
break
;
}
else
{
pBtc
->
idx
=
TDB_PAGE_TOTAL_CELLS
(
pBtc
->
pPage
);
pgno
=
((
SIntHdr
*
)
pBtc
->
pPage
->
pData
)
->
pgno
;
ret
=
tdbBtcMoveDownward
(
pBtc
,
pgno
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
}
}
return
0
;
}
int
tdbBtcMoveTo
(
SBTC
*
pBtc
,
const
void
*
pKey
,
int
kLen
)
{
// TODO
return
0
;
}
int
tdbBtreeNext
(
SBTC
*
pBtc
,
void
**
ppKey
,
int
*
kLen
,
void
**
ppVal
,
int
*
vLen
)
{
SCell
*
pCell
;
SCellDecoder
cd
;
void
*
pKey
,
*
pVal
;
int
ret
;
if
(
pBtc
->
idx
<
0
)
{
return
-
1
;
}
pCell
=
tdbPageGetCell
(
pBtc
->
pPage
,
pBtc
->
idx
);
tdbBtreeDecodeCell
(
pBtc
->
pPage
,
pCell
,
&
cd
);
pKey
=
TDB_REALLOC
(
*
ppKey
,
cd
.
kLen
);
if
(
pKey
==
NULL
)
{
return
-
1
;
}
// TODO: vLen may be zero
pVal
=
TDB_REALLOC
(
*
ppVal
,
cd
.
vLen
);
if
(
pVal
==
NULL
)
{
TDB_FREE
(
pKey
);
return
-
1
;
}
*
ppKey
=
pKey
;
*
ppVal
=
pVal
;
*
kLen
=
cd
.
kLen
;
*
vLen
=
cd
.
vLen
;
memcpy
(
pKey
,
cd
.
pKey
,
cd
.
kLen
);
memcpy
(
pVal
,
cd
.
pVal
,
cd
.
vLen
);
ret
=
tdbBtcMoveToNext
(
pBtc
);
return
0
;
}
static
int
tdbBtcMoveToNext
(
SBTC
*
pBtc
)
{
int
nCells
;
SPgno
pgno
;
SCell
*
pCell
;
u8
flags
;
ASSERT
(
TDB_BTREE_PAGE_IS_LEAF
(
TDB_BTREE_PAGE_GET_FLAGS
(
pBtc
->
pPage
)));
if
(
pBtc
->
idx
<
0
)
return
-
1
;
pBtc
->
idx
++
;
if
(
pBtc
->
idx
<
TDB_PAGE_TOTAL_CELLS
(
pBtc
->
pPage
))
{
return
0
;
}
if
(
pBtc
->
iPage
==
0
)
{
pBtc
->
idx
=
-
1
;
return
0
;
}
// Move upward
for
(;;)
{
tdbBtcMoveUpward
(
pBtc
);
pBtc
->
idx
++
;
nCells
=
TDB_PAGE_TOTAL_CELLS
(
pBtc
->
pPage
);
if
(
pBtc
->
idx
<=
nCells
)
{
break
;
}
if
(
pBtc
->
iPage
==
0
)
{
pBtc
->
idx
=
-
1
;
return
0
;
}
}
// Move downward
for
(;;)
{
nCells
=
TDB_PAGE_TOTAL_CELLS
(
pBtc
->
pPage
);
if
(
pBtc
->
idx
<
nCells
)
{
pCell
=
tdbPageGetCell
(
pBtc
->
pPage
,
pBtc
->
idx
);
pgno
=
*
(
SPgno
*
)
pCell
;
}
else
{
pgno
=
((
SIntHdr
*
)
pBtc
->
pPage
->
pData
)
->
pgno
;
}
tdbBtcMoveDownward
(
pBtc
,
pgno
);
pBtc
->
idx
=
0
;
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pBtc
->
pPage
);
if
(
TDB_BTREE_PAGE_IS_LEAF
(
flags
))
{
break
;
}
}
return
0
;
}
int
tdbBtcClose
(
SBTC
*
pBtc
)
{
// TODO
return
0
;
}
static
int
tdbBtcMoveDownward
(
SBTC
*
pCur
,
SPgno
pgno
)
{
int
ret
;
pCur
->
pgStack
[
pCur
->
iPage
]
=
pCur
->
pPage
;
pCur
->
idxStack
[
pCur
->
iPage
]
=
pCur
->
idx
;
pCur
->
iPage
++
;
pCur
->
pPage
=
NULL
;
pCur
->
idx
=
-
1
;
ret
=
tdbPagerFetchPage
(
pCur
->
pBt
->
pPager
,
pgno
,
&
pCur
->
pPage
,
tdbBtreeInitPage
,
pCur
->
pBt
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
}
return
0
;
}
static
int
tdbBtcMoveUpward
(
SBTC
*
pBtc
)
{
if
(
pBtc
->
iPage
==
0
)
return
-
1
;
// tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage);
pBtc
->
iPage
--
;
pBtc
->
pPage
=
pBtc
->
pgStack
[
pBtc
->
iPage
];
pBtc
->
idx
=
pBtc
->
idxStack
[
pBtc
->
iPage
];
return
0
;
}
#ifndef NODEBUG
typedef
struct
{
SPgno
pgno
;
u8
root
;
u8
leaf
;
SPgno
rChild
;
int
nCells
;
int
nOvfl
;
}
SBtPageInfo
;
SBtPageInfo
btPageInfos
[
20
];
void
tdbBtPageInfo
(
SPage
*
pPage
,
int
idx
)
{
u8
flags
;
SBtPageInfo
*
pBtPageInfo
;
pBtPageInfo
=
btPageInfos
+
idx
;
pBtPageInfo
->
pgno
=
TDB_PAGE_PGNO
(
pPage
);
flags
=
TDB_BTREE_PAGE_GET_FLAGS
(
pPage
);
pBtPageInfo
->
root
=
TDB_BTREE_PAGE_IS_ROOT
(
flags
);
pBtPageInfo
->
leaf
=
TDB_BTREE_PAGE_IS_LEAF
(
flags
);
pBtPageInfo
->
rChild
=
0
;
if
(
!
pBtPageInfo
->
leaf
)
{
pBtPageInfo
->
rChild
=
*
(
SPgno
*
)(
pPage
->
pData
+
1
);
}
pBtPageInfo
->
nCells
=
TDB_PAGE_TOTAL_CELLS
(
pPage
)
-
pPage
->
nOverflow
;
pBtPageInfo
->
nOvfl
=
pPage
->
nOverflow
;
}
#endif
#endif
\ No newline at end of file
source/libs/tdb/src/db/tdbDb.c
浏览文件 @
10319e3c
...
@@ -15,13 +15,17 @@
...
@@ -15,13 +15,17 @@
#include "tdbInt.h"
#include "tdbInt.h"
struct
STD
b
{
struct
STD
B
{
STEnv
*
pEnv
;
STEnv
*
pEnv
;
SBTree
*
pBt
;
SBTree
*
pBt
;
};
};
int
tdbDbOpen
(
const
char
*
fname
,
int
keyLen
,
int
valLen
,
FKeyComparator
keyCmprFn
,
STEnv
*
pEnv
,
STDb
**
ppDb
)
{
struct
STDBC
{
STDb
*
pDb
;
SBTC
btc
;
};
int
tdbDbOpen
(
const
char
*
fname
,
int
keyLen
,
int
valLen
,
FKeyComparator
keyCmprFn
,
STEnv
*
pEnv
,
STDB
**
ppDb
)
{
STDB
*
pDb
;
SPager
*
pPager
;
SPager
*
pPager
;
int
ret
;
int
ret
;
char
fFullName
[
TDB_FILENAME_LEN
];
char
fFullName
[
TDB_FILENAME_LEN
];
...
@@ -30,7 +34,7 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF
...
@@ -30,7 +34,7 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF
*
ppDb
=
NULL
;
*
ppDb
=
NULL
;
pDb
=
(
STD
b
*
)
taosMemoryC
alloc
(
1
,
sizeof
(
*
pDb
));
pDb
=
(
STD
B
*
)
c
alloc
(
1
,
sizeof
(
*
pDb
));
if
(
pDb
==
NULL
)
{
if
(
pDb
==
NULL
)
{
return
-
1
;
return
-
1
;
}
}
...
@@ -59,23 +63,23 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF
...
@@ -59,23 +63,23 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF
return
0
;
return
0
;
}
}
int
tdbDbClose
(
STD
b
*
pDb
)
{
int
tdbDbClose
(
STD
B
*
pDb
)
{
// TODO
// TODO
return
0
;
return
0
;
}
}
int
tdbDbDrop
(
STD
b
*
pDb
)
{
int
tdbDbDrop
(
STD
B
*
pDb
)
{
// TODO
// TODO
return
0
;
return
0
;
}
}
int
tdbDbInsert
(
STD
b
*
pDb
,
const
void
*
pKey
,
int
keyLen
,
const
void
*
pVal
,
int
valLen
)
{
int
tdbDbInsert
(
STD
B
*
pDb
,
const
void
*
pKey
,
int
keyLen
,
const
void
*
pVal
,
int
valLen
)
{
SB
tCursor
btc
;
SB
TC
btc
;
SB
tCursor
*
pCur
;
SB
TC
*
pCur
;
int
ret
;
int
ret
;
pCur
=
&
btc
;
pCur
=
&
btc
;
ret
=
tdbBt
reeCursor
(
pCur
,
pDb
->
pBt
);
ret
=
tdbBt
cOpen
(
pCur
,
pDb
->
pBt
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
return
-
1
;
return
-
1
;
}
}
...
@@ -85,5 +89,45 @@ int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int v
...
@@ -85,5 +89,45 @@ int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int v
return
-
1
;
return
-
1
;
}
}
return
0
;
}
int
tdbDbGet
(
STDB
*
pDb
,
const
void
*
pKey
,
int
kLen
,
void
**
ppVal
,
int
*
vLen
)
{
return
tdbBtreeGet
(
pDb
->
pBt
,
pKey
,
kLen
,
ppVal
,
vLen
);
}
int
tdbDbcOpen
(
STDB
*
pDb
,
STDBC
**
ppDbc
)
{
int
ret
;
STDBC
*
pDbc
=
NULL
;
*
ppDbc
=
NULL
;
pDbc
=
malloc
(
sizeof
(
*
pDbc
));
if
(
pDbc
==
NULL
)
{
return
-
1
;
}
tdbBtcOpen
(
&
pDbc
->
btc
,
pDb
->
pBt
);
// TODO: move to first now, we can move to any key-value
// and in any direction, design new APIs.
ret
=
tdbBtcMoveToFirst
(
&
pDbc
->
btc
);
if
(
ret
<
0
)
{
ASSERT
(
0
);
return
-
1
;
}
*
ppDbc
=
pDbc
;
return
0
;
}
int
tdbDbNext
(
STDBC
*
pDbc
,
void
**
ppKey
,
int
*
kLen
,
void
**
ppVal
,
int
*
vLen
)
{
return
tdbBtreeNext
(
&
pDbc
->
btc
,
ppKey
,
kLen
,
ppVal
,
vLen
);
}
int
tdbDbcClose
(
STDBC
*
pDbc
)
{
if
(
pDbc
)
{
free
(
pDbc
);
}
return
0
;
return
0
;
}
}
\ No newline at end of file
source/libs/tdb/src/db/tdbEnv.c
浏览文件 @
10319e3c
...
@@ -27,7 +27,7 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, STEnv **ppEnv)
...
@@ -27,7 +27,7 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, STEnv **ppEnv)
dsize
=
strlen
(
rootDir
);
dsize
=
strlen
(
rootDir
);
zsize
=
sizeof
(
*
pEnv
)
+
dsize
*
2
+
strlen
(
TDB_JOURNAL_NAME
)
+
3
;
zsize
=
sizeof
(
*
pEnv
)
+
dsize
*
2
+
strlen
(
TDB_JOURNAL_NAME
)
+
3
;
pPtr
=
(
uint8_t
*
)
taosMemoryC
alloc
(
1
,
zsize
);
pPtr
=
(
uint8_t
*
)
c
alloc
(
1
,
zsize
);
if
(
pPtr
==
NULL
)
{
if
(
pPtr
==
NULL
)
{
return
-
1
;
return
-
1
;
}
}
...
...
source/libs/tdb/src/db/tdbPCache.c
浏览文件 @
10319e3c
...
@@ -17,7 +17,7 @@
...
@@ -17,7 +17,7 @@
struct
SPCache
{
struct
SPCache
{
int
pageSize
;
int
pageSize
;
int
cacheSize
;
int
cacheSize
;
TdThreadMutex
mutex
;
pthread_mutex_t
mutex
;
int
nFree
;
int
nFree
;
SPage
*
pFree
;
SPage
*
pFree
;
int
nPage
;
int
nPage
;
...
@@ -53,19 +53,17 @@ static void tdbPCacheLock(SPCache *pCache);
...
@@ -53,19 +53,17 @@ static void tdbPCacheLock(SPCache *pCache);
static
void
tdbPCacheUnlock
(
SPCache
*
pCache
);
static
void
tdbPCacheUnlock
(
SPCache
*
pCache
);
static
bool
tdbPCacheLocked
(
SPCache
*
pCache
);
static
bool
tdbPCacheLocked
(
SPCache
*
pCache
);
static
SPage
*
tdbPCacheFetchImpl
(
SPCache
*
pCache
,
const
SPgid
*
pPgid
,
bool
alcNewPage
);
static
SPage
*
tdbPCacheFetchImpl
(
SPCache
*
pCache
,
const
SPgid
*
pPgid
,
bool
alcNewPage
);
static
void
tdbPCachePinPage
(
SPage
*
pPage
);
static
void
tdbPCachePinPage
(
SPCache
*
pCache
,
SPage
*
pPage
);
static
void
tdbPCacheRemovePageFromHash
(
SPage
*
pPage
);
static
void
tdbPCacheRemovePageFromHash
(
SPCache
*
pCache
,
SPage
*
pPage
);
static
void
tdbPCacheAddPageToHash
(
SPage
*
pPage
);
static
void
tdbPCacheAddPageToHash
(
SPCache
*
pCache
,
SPage
*
pPage
);
static
void
tdbPCacheUnpinPage
(
SPage
*
pPage
);
static
void
tdbPCacheUnpinPage
(
SPCache
*
pCache
,
SPage
*
pPage
);
static
void
*
tdbOsMalloc
(
void
*
arg
,
size_t
size
);
static
void
tdbOsFree
(
void
*
arg
,
void
*
ptr
);
int
tdbPCacheOpen
(
int
pageSize
,
int
cacheSize
,
SPCache
**
ppCache
)
{
int
tdbPCacheOpen
(
int
pageSize
,
int
cacheSize
,
SPCache
**
ppCache
)
{
SPCache
*
pCache
;
SPCache
*
pCache
;
void
*
pPtr
;
void
*
pPtr
;
SPage
*
pPgHdr
;
SPage
*
pPgHdr
;
pCache
=
(
SPCache
*
)
taosMemoryC
alloc
(
1
,
sizeof
(
*
pCache
));
pCache
=
(
SPCache
*
)
c
alloc
(
1
,
sizeof
(
*
pCache
));
if
(
pCache
==
NULL
)
{
if
(
pCache
==
NULL
)
{
return
-
1
;
return
-
1
;
}
}
...
@@ -74,7 +72,7 @@ int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
...
@@ -74,7 +72,7 @@ int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
pCache
->
cacheSize
=
cacheSize
;
pCache
->
cacheSize
=
cacheSize
;
if
(
tdbPCacheOpenImpl
(
pCache
)
<
0
)
{
if
(
tdbPCacheOpenImpl
(
pCache
)
<
0
)
{
taosMemoryF
ree
(
pCache
);
f
ree
(
pCache
);
return
-
1
;
return
-
1
;
}
}
...
@@ -102,7 +100,7 @@ SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
...
@@ -102,7 +100,7 @@ SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
return
pPage
;
return
pPage
;
}
}
void
tdbPCacheRelease
(
SPage
*
pPage
)
{
void
tdbPCacheRelease
(
SP
Cache
*
pCache
,
SP
age
*
pPage
)
{
i32
nRef
;
i32
nRef
;
nRef
=
TDB_UNREF_PAGE
(
pPage
);
nRef
=
TDB_UNREF_PAGE
(
pPage
);
...
@@ -110,7 +108,7 @@ void tdbPCacheRelease(SPage *pPage) {
...
@@ -110,7 +108,7 @@ void tdbPCacheRelease(SPage *pPage) {
if
(
nRef
==
0
)
{
if
(
nRef
==
0
)
{
if
(
1
/*TODO: page still clean*/
)
{
if
(
1
/*TODO: page still clean*/
)
{
tdbPCacheUnpinPage
(
pPage
);
tdbPCacheUnpinPage
(
p
Cache
,
p
Page
);
}
else
{
}
else
{
// TODO
// TODO
ASSERT
(
0
);
ASSERT
(
0
);
...
@@ -118,13 +116,13 @@ void tdbPCacheRelease(SPage *pPage) {
...
@@ -118,13 +116,13 @@ void tdbPCacheRelease(SPage *pPage) {
}
}
}
}
static
void
tdbPCacheInitLock
(
SPCache
*
pCache
)
{
taosThreadMutexI
nit
(
&
(
pCache
->
mutex
),
NULL
);
}
static
void
tdbPCacheInitLock
(
SPCache
*
pCache
)
{
pthread_mutex_i
nit
(
&
(
pCache
->
mutex
),
NULL
);
}
static
void
tdbPCacheClearLock
(
SPCache
*
pCache
)
{
taosThreadMutexD
estroy
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheClearLock
(
SPCache
*
pCache
)
{
pthread_mutex_d
estroy
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheLock
(
SPCache
*
pCache
)
{
taosThreadMutexL
ock
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheLock
(
SPCache
*
pCache
)
{
pthread_mutex_l
ock
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheUnlock
(
SPCache
*
pCache
)
{
taosThreadMutexU
nlock
(
&
(
pCache
->
mutex
));
}
static
void
tdbPCacheUnlock
(
SPCache
*
pCache
)
{
pthread_mutex_u
nlock
(
&
(
pCache
->
mutex
));
}
static
bool
tdbPCacheLocked
(
SPCache
*
pCache
)
{
static
bool
tdbPCacheLocked
(
SPCache
*
pCache
)
{
assert
(
0
);
assert
(
0
);
...
@@ -144,7 +142,7 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
...
@@ -144,7 +142,7 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
if
(
pPage
||
!
alcNewPage
)
{
if
(
pPage
||
!
alcNewPage
)
{
if
(
pPage
)
{
if
(
pPage
)
{
tdbPCachePinPage
(
pPage
);
tdbPCachePinPage
(
p
Cache
,
p
Page
);
}
}
return
pPage
;
return
pPage
;
}
}
...
@@ -160,8 +158,8 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
...
@@ -160,8 +158,8 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
// 3. Try to Recycle a page
// 3. Try to Recycle a page
if
(
!
pPage
&&
!
pCache
->
lru
.
pLruPrev
->
isAnchor
)
{
if
(
!
pPage
&&
!
pCache
->
lru
.
pLruPrev
->
isAnchor
)
{
pPage
=
pCache
->
lru
.
pLruPrev
;
pPage
=
pCache
->
lru
.
pLruPrev
;
tdbPCacheRemovePageFromHash
(
pPage
);
tdbPCacheRemovePageFromHash
(
p
Cache
,
p
Page
);
tdbPCachePinPage
(
pPage
);
tdbPCachePinPage
(
p
Cache
,
p
Page
);
}
}
// 4. Try a stress allocation (TODO)
// 4. Try a stress allocation (TODO)
...
@@ -173,16 +171,13 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
...
@@ -173,16 +171,13 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
memcpy
(
&
(
pPage
->
pgid
),
pPgid
,
sizeof
(
*
pPgid
));
memcpy
(
&
(
pPage
->
pgid
),
pPgid
,
sizeof
(
*
pPgid
));
pPage
->
pLruNext
=
NULL
;
pPage
->
pLruNext
=
NULL
;
pPage
->
pPager
=
NULL
;
pPage
->
pPager
=
NULL
;
tdbPCacheAddPageToHash
(
pPage
);
tdbPCacheAddPageToHash
(
p
Cache
,
p
Page
);
}
}
return
pPage
;
return
pPage
;
}
}
static
void
tdbPCachePinPage
(
SPage
*
pPage
)
{
static
void
tdbPCachePinPage
(
SPCache
*
pCache
,
SPage
*
pPage
)
{
SPCache
*
pCache
;
pCache
=
pPage
->
pCache
;
if
(
!
PAGE_IS_PINNED
(
pPage
))
{
if
(
!
PAGE_IS_PINNED
(
pPage
))
{
pPage
->
pLruPrev
->
pLruNext
=
pPage
->
pLruNext
;
pPage
->
pLruPrev
->
pLruNext
=
pPage
->
pLruNext
;
pPage
->
pLruNext
->
pLruPrev
=
pPage
->
pLruPrev
;
pPage
->
pLruNext
->
pLruPrev
=
pPage
->
pLruPrev
;
...
@@ -192,11 +187,8 @@ static void tdbPCachePinPage(SPage *pPage) {
...
@@ -192,11 +187,8 @@ static void tdbPCachePinPage(SPage *pPage) {
}
}
}
}
static
void
tdbPCacheUnpinPage
(
SPage
*
pPage
)
{
static
void
tdbPCacheUnpinPage
(
SPCache
*
pCache
,
SPage
*
pPage
)
{
SPCache
*
pCache
;
i32
nRef
;
i32
nRef
;
pCache
=
pPage
->
pCache
;
tdbPCacheLock
(
pCache
);
tdbPCacheLock
(
pCache
);
...
@@ -217,12 +209,10 @@ static void tdbPCacheUnpinPage(SPage *pPage) {
...
@@ -217,12 +209,10 @@ static void tdbPCacheUnpinPage(SPage *pPage) {
tdbPCacheUnlock
(
pCache
);
tdbPCacheUnlock
(
pCache
);
}
}
static
void
tdbPCacheRemovePageFromHash
(
SPage
*
pPage
)
{
static
void
tdbPCacheRemovePageFromHash
(
SPCache
*
pCache
,
SPage
*
pPage
)
{
SPCache
*
pCache
;
SPage
**
ppPage
;
SPage
**
ppPage
;
int
h
;
int
h
;
pCache
=
pPage
->
pCache
;
h
=
PCACHE_PAGE_HASH
(
&
(
pPage
->
pgid
));
h
=
PCACHE_PAGE_HASH
(
&
(
pPage
->
pgid
));
for
(
ppPage
=
&
(
pCache
->
pgHash
[
h
%
pCache
->
nHash
]);
*
ppPage
!=
pPage
;
ppPage
=
&
((
*
ppPage
)
->
pHashNext
))
for
(
ppPage
=
&
(
pCache
->
pgHash
[
h
%
pCache
->
nHash
]);
*
ppPage
!=
pPage
;
ppPage
=
&
((
*
ppPage
)
->
pHashNext
))
;
;
...
@@ -232,11 +222,9 @@ static void tdbPCacheRemovePageFromHash(SPage *pPage) {
...
@@ -232,11 +222,9 @@ static void tdbPCacheRemovePageFromHash(SPage *pPage) {
pCache
->
nPage
--
;
pCache
->
nPage
--
;
}
}
static
void
tdbPCacheAddPageToHash
(
SPage
*
pPage
)
{
static
void
tdbPCacheAddPageToHash
(
SPCache
*
pCache
,
SPage
*
pPage
)
{
SPCache
*
pCache
;
int
h
;
int
h
;
pCache
=
pPage
->
pCache
;
h
=
PCACHE_PAGE_HASH
(
&
(
pPage
->
pgid
))
%
pCache
->
nHash
;
h
=
PCACHE_PAGE_HASH
(
&
(
pPage
->
pgid
))
%
pCache
->
nHash
;
pPage
->
pHashNext
=
pCache
->
pgHash
[
h
];
pPage
->
pHashNext
=
pCache
->
pgHash
[
h
];
...
@@ -257,7 +245,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
...
@@ -257,7 +245,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
pCache
->
nFree
=
0
;
pCache
->
nFree
=
0
;
pCache
->
pFree
=
NULL
;
pCache
->
pFree
=
NULL
;
for
(
int
i
=
0
;
i
<
pCache
->
cacheSize
;
i
++
)
{
for
(
int
i
=
0
;
i
<
pCache
->
cacheSize
;
i
++
)
{
ret
=
tdbPageCreate
(
pCache
->
pageSize
,
&
pPage
,
tdbOsMalloc
,
NULL
);
ret
=
tdbPageCreate
(
pCache
->
pageSize
,
&
pPage
,
NULL
,
NULL
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
// TODO: handle error
// TODO: handle error
return
-
1
;
return
-
1
;
...
@@ -266,7 +254,6 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
...
@@ -266,7 +254,6 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
// pPage->pgid = 0;
// pPage->pgid = 0;
pPage
->
isAnchor
=
0
;
pPage
->
isAnchor
=
0
;
pPage
->
isLocalPage
=
1
;
pPage
->
isLocalPage
=
1
;
pPage
->
pCache
=
pCache
;
TDB_INIT_PAGE_REF
(
pPage
);
TDB_INIT_PAGE_REF
(
pPage
);
pPage
->
pHashNext
=
NULL
;
pPage
->
pHashNext
=
NULL
;
pPage
->
pLruNext
=
NULL
;
pPage
->
pLruNext
=
NULL
;
...
@@ -281,7 +268,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
...
@@ -281,7 +268,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
// Open the hash table
// Open the hash table
pCache
->
nPage
=
0
;
pCache
->
nPage
=
0
;
pCache
->
nHash
=
pCache
->
cacheSize
;
pCache
->
nHash
=
pCache
->
cacheSize
;
pCache
->
pgHash
=
(
SPage
**
)
taosMemoryC
alloc
(
pCache
->
nHash
,
sizeof
(
SPage
*
));
pCache
->
pgHash
=
(
SPage
**
)
c
alloc
(
pCache
->
nHash
,
sizeof
(
SPage
*
));
if
(
pCache
->
pgHash
==
NULL
)
{
if
(
pCache
->
pgHash
==
NULL
)
{
// TODO
// TODO
return
-
1
;
return
-
1
;
...
@@ -297,13 +284,3 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
...
@@ -297,13 +284,3 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
}
}
int
tdbPCacheGetPageSize
(
SPCache
*
pCache
)
{
return
pCache
->
pageSize
;
}
int
tdbPCacheGetPageSize
(
SPCache
*
pCache
)
{
return
pCache
->
pageSize
;
}
static
void
*
tdbOsMalloc
(
void
*
arg
,
size_t
size
)
{
void
*
ptr
;
ptr
=
taosMemoryMalloc
(
size
);
return
ptr
;
}
static
void
tdbOsFree
(
void
*
arg
,
void
*
ptr
)
{
taosMemoryFree
(
ptr
);
}
\ No newline at end of file
source/libs/tdb/src/db/tdbPage.c
已删除
100644 → 0
浏览文件 @
90e4cab2
/*
* 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"
typedef
struct
__attribute__
((
__packed__
))
{
u8
szCell
[
2
];
u8
nxOffset
[
2
];
}
SFreeCell
;
typedef
struct
__attribute__
((
__packed__
))
{
u8
szCell
[
3
];
u8
nxOffset
[
3
];
}
SFreeCellL
;
/* For small page */
#define TDB_SPAGE_FREE_CELL_SIZE_PTR(PCELL) (((SFreeCell *)(PCELL))->szCell)
#define TDB_SPAGE_FREE_CELL_NXOFFSET_PTR(PCELL) (((SFreeCell *)(PCELL))->nxOffset)
#define TDB_SPAGE_FREE_CELL_SIZE(PCELL) ((u16 *)TDB_SPAGE_FREE_CELL_SIZE_PTR(PCELL))[0]
#define TDB_SPAGE_FREE_CELL_NXOFFSET(PCELL) ((u16 *)TDB_SPAGE_FREE_CELL_NXOFFSET_PTR(PCELL))[0]
#define TDB_SPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE) (TDB_SPAGE_FREE_CELL_SIZE(PCELL) = (SIZE))
#define TDB_SPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET) (TDB_SPAGE_FREE_CELL_NXOFFSET(PCELL) = (OFFSET))
/* For large page */
#define TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL) (((SFreeCellL *)(PCELL))->szCell)
#define TDB_LPAGE_FREE_CELL_NXOFFSET_PTR(PCELL) (((SFreeCellL *)(PCELL))->nxOffset)
#define TDB_LPAGE_FREE_CELL_SIZE(PCELL) TDB_GET_U24(TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL))
#define TDB_LPAGE_FREE_CELL_NXOFFSET(PCELL) TDB_GET_U24(TDB_LPAGE_FREE_CELL_NXOFFSET_PTR(PCELL))
#define TDB_LPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE) TDB_PUT_U24(TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL), SIZE)
#define TDB_LPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET) TDB_PUT_U24(TDB_LPAGE_FREE_CELL_NXOFFSET_PTR(PCELL), OFFSET)
/* For page */
#define TDB_PAGE_FREE_CELL_SIZE_PTR(PPAGE, PCELL) \
(TDB_IS_LARGE_PAGE(pPage) ? TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL) : TDB_SPAGE_FREE_CELL_SIZE_PTR(PCELL))
#define TDB_PAGE_FREE_CELL_NXOFFSET_PTR(PPAGE, PCELL) \
(TDB_IS_LARGE_PAGE(pPage) ? TDB_LPAGE_FREE_CELL_NXOFFSET_PTR(PCELL) : TDB_SPAGE_FREE_CELL_NXOFFSET_PTR(PCELL))
#define TDB_PAGE_FREE_CELL_SIZE(PPAGE, PCELL) \
(TDB_IS_LARGE_PAGE(pPage) ? TDB_LPAGE_FREE_CELL_SIZE(PCELL) : TDB_SPAGE_FREE_CELL_SIZE(PCELL))
#define TDB_PAGE_FREE_CELL_NXOFFSET(PPAGE, PCELL) \
(TDB_IS_LARGE_PAGE(pPage) ? TDB_LPAGE_FREE_CELL_NXOFFSET(PCELL) : TDB_SPAGE_FREE_CELL_NXOFFSET(PCELL))
#define TDB_PAGE_FREE_CELL_SIZE_SET(PPAGE, PCELL, SIZE) \
do { \
if (TDB_IS_LARGE_PAGE(PPAGE)) { \
TDB_LPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE); \
} else { \
TDB_SPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE); \
} \
} while (0)
#define TDB_PAGE_FREE_CELL_NXOFFSET_SET(PPAGE, PCELL, OFFSET) \
do { \
if (TDB_IS_LARGE_PAGE(PPAGE)) { \
TDB_LPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET); \
} else { \
TDB_SPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET); \
} \
} while (0)
static
int
tdbPageAllocate
(
SPage
*
pPage
,
int
size
,
SCell
**
ppCell
);
static
int
tdbPageDefragment
(
SPage
*
pPage
);
int
tdbPageCreate
(
int
pageSize
,
SPage
**
ppPage
,
void
*
(
*
xMalloc
)(
void
*
,
size_t
),
void
*
arg
)
{
SPage
*
pPage
;
u8
*
ptr
;
int
size
;
ASSERT
(
TDB_IS_PGSIZE_VLD
(
pageSize
));
*
ppPage
=
NULL
;
size
=
pageSize
+
sizeof
(
*
pPage
);
ptr
=
(
u8
*
)((
*
xMalloc
)(
arg
,
size
));
if
(
pPage
==
NULL
)
{
return
-
1
;
}
memset
(
ptr
,
0
,
size
);
pPage
=
(
SPage
*
)(
ptr
+
pageSize
);
pPage
->
pData
=
ptr
;
pPage
->
pageSize
=
pageSize
;
if
(
pageSize
<
65536
)
{
pPage
->
szOffset
=
2
;
pPage
->
szPageHdr
=
sizeof
(
SPageHdr
);
pPage
->
szFreeCell
=
sizeof
(
SFreeCell
);
}
else
{
pPage
->
szOffset
=
3
;
pPage
->
szPageHdr
=
sizeof
(
SPageHdrL
);
pPage
->
szFreeCell
=
sizeof
(
SFreeCellL
);
}
TDB_INIT_PAGE_LOCK
(
pPage
);
/* TODO */
*
ppPage
=
pPage
;
return
0
;
}
int
tdbPageDestroy
(
SPage
*
pPage
,
void
(
*
xFree
)(
void
*
arg
,
void
*
ptr
),
void
*
arg
)
{
u8
*
ptr
;
ptr
=
pPage
->
pData
;
(
*
xFree
)(
arg
,
ptr
);
return
0
;
}
int
tdbPageInsertCell
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
szCell
)
{
int
ret
;
SCell
*
pTarget
;
u8
*
pTmp
;
int
j
;
if
(
pPage
->
nOverflow
||
szCell
+
pPage
->
szOffset
>
pPage
->
nFree
)
{
// TODO: need to figure out if pCell may be used by outside of this function
j
=
pPage
->
nOverflow
++
;
pPage
->
apOvfl
[
j
]
=
pCell
;
pPage
->
aiOvfl
[
j
]
=
idx
;
}
else
{
ret
=
tdbPageAllocate
(
pPage
,
szCell
,
&
pTarget
);
if
(
ret
<
0
)
{
return
-
1
;
}
memcpy
(
pTarget
,
pCell
,
szCell
);
pTmp
=
pPage
->
pCellIdx
+
idx
*
pPage
->
szOffset
;
memmove
(
pTmp
+
pPage
->
szOffset
,
pTmp
,
pPage
->
pFreeStart
-
pTmp
-
pPage
->
szOffset
);
TDB_PAGE_CELL_OFFSET_AT_SET
(
pPage
,
idx
,
pTarget
-
pPage
->
pData
);
TDB_PAGE_NCELLS_SET
(
pPage
,
TDB_PAGE_NCELLS
(
pPage
)
+
1
);
}
return
0
;
}
int
tdbPageDropCell
(
SPage
*
pPage
,
int
idx
)
{
// TODO
return
0
;
}
static
int
tdbPageAllocate
(
SPage
*
pPage
,
int
size
,
SCell
**
ppCell
)
{
SCell
*
pCell
;
SFreeCell
*
pFreeCell
;
u8
*
pOffset
;
int
ret
;
ASSERT
(
pPage
->
nFree
>
size
+
pPage
->
szOffset
);
pCell
=
NULL
;
*
ppCell
=
NULL
;
// 1. Try to allocate from the free space area
if
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
>
size
+
pPage
->
szOffset
)
{
pPage
->
pFreeEnd
-=
size
;
pPage
->
pFreeStart
+=
pPage
->
szOffset
;
pCell
=
pPage
->
pFreeEnd
;
}
// 2. Try to allocate from the page free list
if
((
pCell
==
NULL
)
&&
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
>=
pPage
->
szOffset
)
&&
TDB_PAGE_FCELL
(
pPage
))
{
int
szCell
;
int
nxOffset
;
pCell
=
pPage
->
pData
+
TDB_PAGE_FCELL
(
pPage
);
pOffset
=
TDB_IS_LARGE_PAGE
(
pPage
)
?
((
SPageHdrL
*
)(
pPage
->
pPageHdr
))[
0
].
fCell
:
(
u8
*
)
&
(((
SPageHdr
*
)(
pPage
->
pPageHdr
))[
0
].
fCell
);
szCell
=
TDB_PAGE_FREE_CELL_SIZE
(
pPage
,
pCell
);
nxOffset
=
TDB_PAGE_FREE_CELL_NXOFFSET
(
pPage
,
pCell
);
for
(;;)
{
// Find a cell
if
(
szCell
>=
size
)
{
if
(
szCell
-
size
>=
pPage
->
szFreeCell
)
{
SCell
*
pTmpCell
=
pCell
+
size
;
TDB_PAGE_FREE_CELL_SIZE_SET
(
pPage
,
pTmpCell
,
szCell
-
size
);
TDB_PAGE_FREE_CELL_NXOFFSET_SET
(
pPage
,
pTmpCell
,
nxOffset
);
// TODO: *pOffset = pTmpCell - pPage->pData;
}
else
{
TDB_PAGE_NFREE_SET
(
pPage
,
TDB_PAGE_NFREE
(
pPage
)
+
szCell
-
size
);
// TODO: *pOffset = nxOffset;
}
break
;
}
// Not find a cell yet
if
(
nxOffset
>
0
)
{
pCell
=
pPage
->
pData
+
nxOffset
;
pOffset
=
TDB_PAGE_FREE_CELL_NXOFFSET_PTR
(
pPage
,
pCell
);
szCell
=
TDB_PAGE_FREE_CELL_SIZE
(
pPage
,
pCell
);
nxOffset
=
TDB_PAGE_FREE_CELL_NXOFFSET
(
pPage
,
pCell
);
continue
;
}
else
{
pCell
=
NULL
;
break
;
}
}
if
(
pCell
)
{
pPage
->
pFreeStart
=
pPage
->
pFreeStart
+
pPage
->
szOffset
;
}
}
// 3. Try to dfragment and allocate again
if
(
pCell
==
NULL
)
{
ret
=
tdbPageDefragment
(
pPage
);
if
(
ret
<
0
)
{
return
-
1
;
}
ASSERT
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
>
size
+
pPage
->
szOffset
);
ASSERT
(
pPage
->
nFree
==
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
);
// Allocate from the free space area again
pPage
->
pFreeEnd
-=
size
;
pPage
->
pFreeStart
+=
pPage
->
szOffset
;
pCell
=
pPage
->
pFreeEnd
;
}
ASSERT
(
pCell
!=
NULL
);
pPage
->
nFree
=
pPage
->
nFree
-
size
-
pPage
->
szOffset
;
*
ppCell
=
pCell
;
return
0
;
}
static
int
tdbPageFree
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
size
)
{
// TODO
return
0
;
}
static
int
tdbPageDefragment
(
SPage
*
pPage
)
{
// TODO
ASSERT
(
0
);
return
0
;
}
\ No newline at end of file
source/libs/tdb/src/db/tdbPager.c
浏览文件 @
10319e3c
...
@@ -60,7 +60,7 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) {
...
@@ -60,7 +60,7 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) {
zsize
=
sizeof
(
*
pPager
)
/* SPager */
zsize
=
sizeof
(
*
pPager
)
/* SPager */
+
fsize
+
1
/* dbFileName */
+
fsize
+
1
/* dbFileName */
+
fsize
+
8
+
1
;
/* jFileName */
+
fsize
+
8
+
1
;
/* jFileName */
pPtr
=
(
uint8_t
*
)
taosMemoryC
alloc
(
1
,
zsize
);
pPtr
=
(
uint8_t
*
)
c
alloc
(
1
,
zsize
);
if
(
pPtr
==
NULL
)
{
if
(
pPtr
==
NULL
)
{
return
-
1
;
return
-
1
;
}
}
...
@@ -255,6 +255,10 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage
...
@@ -255,6 +255,10 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage
return
0
;
return
0
;
}
}
void
tdbPagerReturnPage
(
SPager
*
pPager
,
SPage
*
pPage
)
{
tdbPCacheRelease
(
pPager
->
pCache
,
pPage
);
}
static
int
tdbPagerAllocFreePage
(
SPager
*
pPager
,
SPgno
*
ppgno
)
{
static
int
tdbPagerAllocFreePage
(
SPager
*
pPager
,
SPgno
*
ppgno
)
{
// TODO: Allocate a page from the free list
// TODO: Allocate a page from the free list
return
0
;
return
0
;
...
...
source/libs/tdb/src/inc/tdbBtree.h
浏览文件 @
10319e3c
...
@@ -20,10 +20,15 @@
...
@@ -20,10 +20,15 @@
extern
"C"
{
extern
"C"
{
#endif
#endif
typedef
struct
SBTree
SBTree
;
typedef
struct
SBTree
SBTree
;
typedef
struct
SBtCursor
SBtCursor
;
typedef
struct
SBTC
SBTC
;
typedef
struct
SBtInfo
{
SPgno
root
;
int
nLevel
;
int
nData
;
}
SBtInfo
;
struct
SB
tCursor
{
struct
SB
TC
{
SBTree
*
pBt
;
SBTree
*
pBt
;
i8
iPage
;
i8
iPage
;
SPage
*
pPage
;
SPage
*
pPage
;
...
@@ -33,10 +38,19 @@ struct SBtCursor {
...
@@ -33,10 +38,19 @@ struct SBtCursor {
void
*
pBuf
;
void
*
pBuf
;
};
};
// SBTree
int
tdbBtreeOpen
(
int
keyLen
,
int
valLen
,
SPager
*
pFile
,
FKeyComparator
kcmpr
,
SBTree
**
ppBt
);
int
tdbBtreeOpen
(
int
keyLen
,
int
valLen
,
SPager
*
pFile
,
FKeyComparator
kcmpr
,
SBTree
**
ppBt
);
int
tdbBtreeClose
(
SBTree
*
pBt
);
int
tdbBtreeClose
(
SBTree
*
pBt
);
int
tdbBtreeCursor
(
SBtCursor
*
pCur
,
SBTree
*
pBt
);
int
tdbBtCursorInsert
(
SBTC
*
pCur
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
);
int
tdbBtCursorInsert
(
SBtCursor
*
pCur
,
const
void
*
pKey
,
int
kLen
,
const
void
*
pVal
,
int
vLen
);
int
tdbBtreeGet
(
SBTree
*
pBt
,
const
void
*
pKey
,
int
kLen
,
void
**
ppVal
,
int
*
vLen
);
// SBTC
int
tdbBtcOpen
(
SBTC
*
pCur
,
SBTree
*
pBt
);
int
tdbBtcMoveToFirst
(
SBTC
*
pBtc
);
int
tdbBtcMoveToLast
(
SBTC
*
pBtc
);
int
tdbBtcMoveTo
(
SBTC
*
pBtc
,
const
void
*
pKey
,
int
kLen
);
int
tdbBtreeNext
(
SBTC
*
pBtc
,
void
**
ppKey
,
int
*
kLen
,
void
**
ppVal
,
int
*
vLen
);
int
tdbBtcClose
(
SBTC
*
pBtc
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
source/libs/tdb/src/inc/tdbDb.h
浏览文件 @
10319e3c
...
@@ -20,12 +20,20 @@
...
@@ -20,12 +20,20 @@
extern
"C"
{
extern
"C"
{
#endif
#endif
typedef
struct
STDb
STDb
;
typedef
struct
STDB
STDB
;
typedef
struct
STDBC
STDBC
;
int
tdbDbOpen
(
const
char
*
fname
,
int
keyLen
,
int
valLen
,
FKeyComparator
keyCmprFn
,
STEnv
*
pEnv
,
STDb
**
ppDb
);
// STDB
int
tdbDbClose
(
STDb
*
pDb
);
int
tdbDbOpen
(
const
char
*
fname
,
int
keyLen
,
int
valLen
,
FKeyComparator
keyCmprFn
,
STEnv
*
pEnv
,
STDB
**
ppDb
);
int
tdbDbDrop
(
STDb
*
pDb
);
int
tdbDbClose
(
STDB
*
pDb
);
int
tdbDbInsert
(
STDb
*
pDb
,
const
void
*
pKey
,
int
keyLen
,
const
void
*
pVal
,
int
valLen
);
int
tdbDbDrop
(
STDB
*
pDb
);
int
tdbDbInsert
(
STDB
*
pDb
,
const
void
*
pKey
,
int
keyLen
,
const
void
*
pVal
,
int
valLen
);
int
tdbDbGet
(
STDB
*
pDb
,
const
void
*
pKey
,
int
kLen
,
void
**
ppVal
,
int
*
vLen
);
// STDBC
int
tdbDbcOpen
(
STDB
*
pDb
,
STDBC
**
ppDbc
);
int
tdbDbNext
(
STDBC
*
pDbc
,
void
**
ppKey
,
int
*
kLen
,
void
**
ppVal
,
int
*
vLen
);
int
tdbDbcClose
(
STDBC
*
pDbc
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
source/libs/tdb/src/inc/tdbInt.h
浏览文件 @
10319e3c
...
@@ -95,7 +95,7 @@ static FORCE_INLINE int tdbCmprPgId(const void *p1, const void *p2) {
...
@@ -95,7 +95,7 @@ static FORCE_INLINE int tdbCmprPgId(const void *p1, const void *p2) {
// tdb_log
// tdb_log
#define tdbError(var)
#define tdbError(var)
typedef
TD_DLIST
(
STD
b
)
STDbList
;
typedef
TD_DLIST
(
STD
B
)
STDbList
;
typedef
TD_DLIST
(
SPgFile
)
SPgFileList
;
typedef
TD_DLIST
(
SPgFile
)
SPgFileList
;
typedef
TD_DLIST_NODE
(
SPgFile
)
SPgFileListNode
;
typedef
TD_DLIST_NODE
(
SPgFile
)
SPgFileListNode
;
...
@@ -141,8 +141,8 @@ typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, i
...
@@ -141,8 +141,8 @@ typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, i
#define TDB_FLAG_IS(flags, flag) ((flags) == (flag))
#define TDB_FLAG_IS(flags, flag) ((flags) == (flag))
#define TDB_FLAG_HAS(flags, flag) (((flags) & (flag)) != 0)
#define TDB_FLAG_HAS(flags, flag) (((flags) & (flag)) != 0)
#define TDB_FLAG_NO(flags, flag) ((flags) & (flag) == 0)
#define TDB_FLAG_NO(flags, flag) ((flags) & (flag) == 0)
#define TDB_FLAG_ADD(flags, flag) ((flags) |
=
(flag))
#define TDB_FLAG_ADD(flags, flag) ((flags) | (flag))
#define TDB_FLAG_REMOVE(flags, flag) ((flags) &
=
(~(flag)))
#define TDB_FLAG_REMOVE(flags, flag) ((flags) & (~(flag)))
typedef
struct
SPager
SPager
;
typedef
struct
SPager
SPager
;
typedef
struct
SPCache
SPCache
;
typedef
struct
SPCache
SPCache
;
...
...
source/libs/tdb/src/inc/tdbPCache.h
浏览文件 @
10319e3c
...
@@ -21,23 +21,22 @@ extern "C" {
...
@@ -21,23 +21,22 @@ extern "C" {
#endif
#endif
#define TDB_PCACHE_PAGE \
#define TDB_PCACHE_PAGE \
u8 isAnchor; \
u8 isAnchor; \
u8 isLocalPage; \
u8 isLocalPage; \
u8 isDirty; \
u8 isDirty; \
i32 nRef; \
i32 nRef; \
SPCache *pCache; \
SPage *pFreeNext; \
SPage *pFreeNext; \
SPage *pHashNext; \
SPage *pHashNext; \
SPage *pLruNext; \
SPage *pLruNext; \
SPage *pLruPrev; \
SPage *pLruPrev; \
SPage *pDirtyNext; \
SPage *pDirtyNext; \
SPager *pPager; \
SPager *pPager; \
SPgid pgid;
SPgid pgid;
int
tdbPCacheOpen
(
int
pageSize
,
int
cacheSize
,
SPCache
**
ppCache
);
int
tdbPCacheOpen
(
int
pageSize
,
int
cacheSize
,
SPCache
**
ppCache
);
int
tdbPCacheClose
(
SPCache
*
pCache
);
int
tdbPCacheClose
(
SPCache
*
pCache
);
SPage
*
tdbPCacheFetch
(
SPCache
*
pCache
,
const
SPgid
*
pPgid
,
bool
alcNewPage
);
SPage
*
tdbPCacheFetch
(
SPCache
*
pCache
,
const
SPgid
*
pPgid
,
bool
alcNewPage
);
void
tdbPCacheRelease
(
SPage
*
pPage
);
void
tdbPCacheRelease
(
SP
Cache
*
pCache
,
SP
age
*
pPage
);
int
tdbPCacheGetPageSize
(
SPCache
*
pCache
);
int
tdbPCacheGetPageSize
(
SPCache
*
pCache
);
#ifdef __cplusplus
#ifdef __cplusplus
...
...
source/libs/tdb/src/inc/tdbPage.h
浏览文件 @
10319e3c
...
@@ -27,9 +27,6 @@ typedef struct {
...
@@ -27,9 +27,6 @@ typedef struct {
int
szOffset
;
int
szOffset
;
int
szPageHdr
;
int
szPageHdr
;
int
szFreeCell
;
int
szFreeCell
;
// flags
u16
(
*
getFlags
)(
SPage
*
);
void
(
*
setFlags
)(
SPage
*
,
u16
);
// cell number
// cell number
int
(
*
getCellNum
)(
SPage
*
);
int
(
*
getCellNum
)(
SPage
*
);
void
(
*
setCellNum
)(
SPage
*
,
int
);
void
(
*
setCellNum
)(
SPage
*
,
int
);
...
@@ -45,6 +42,9 @@ typedef struct {
...
@@ -45,6 +42,9 @@ typedef struct {
// cell offset at idx
// cell offset at idx
int
(
*
getCellOffset
)(
SPage
*
,
int
);
int
(
*
getCellOffset
)(
SPage
*
,
int
);
void
(
*
setCellOffset
)(
SPage
*
,
int
,
int
);
void
(
*
setCellOffset
)(
SPage
*
,
int
,
int
);
// free cell info
void
(
*
getFreeCellInfo
)(
SCell
*
pCell
,
int
*
szCell
,
int
*
nxOffset
);
void
(
*
setFreeCellInfo
)(
SCell
*
pCell
,
int
szCell
,
int
nxOffset
);
}
SPageMethods
;
}
SPageMethods
;
// Page footer
// Page footer
...
@@ -53,58 +53,37 @@ typedef struct __attribute__((__packed__)) {
...
@@ -53,58 +53,37 @@ typedef struct __attribute__((__packed__)) {
}
SPageFtr
;
}
SPageFtr
;
struct
SPage
{
struct
SPage
{
TdThreadSpinlock
lock
;
pthread_spinlock_t
lock
;
u8
*
pData
;
int
pageSize
;
int
pageSize
;
u8
*
pData
;
SPageMethods
*
pPageMethods
;
SPageMethods
*
pPageMethods
;
// Fields below used by pager and am
// Fields below used by pager and am
u8
szAmHdr
;
u8
*
pPageHdr
;
u8
*
pPageHdr
;
u8
*
pAmHdr
;
u8
*
pCellIdx
;
u8
*
pCellIdx
;
u8
*
pFreeStart
;
u8
*
pFreeStart
;
u8
*
pFreeEnd
;
u8
*
pFreeEnd
;
SPageFtr
*
pPageFtr
;
SPageFtr
*
pPageFtr
;
int
nOverflow
;
SCell
*
apOvfl
[
4
];
int
aiOvfl
[
4
];
int
kLen
;
// key length of the page, -1 for unknown
int
kLen
;
// key length of the page, -1 for unknown
int
vLen
;
// value length of the page, -1 for unknown
int
vLen
;
// value length of the page, -1 for unknown
int
nFree
;
int
maxLocal
;
int
maxLocal
;
int
minLocal
;
int
minLocal
;
int
nOverflow
;
int
(
*
xCellSize
)(
const
SPage
*
,
SCell
*
);
SCell
*
apOvfl
[
4
];
int
aiOvfl
[
4
];
// Fields used by SPCache
// Fields used by SPCache
TDB_PCACHE_PAGE
TDB_PCACHE_PAGE
};
};
/* For page */
#define TDB_PAGE_FLAGS(pPage) (*(pPage)->pPageMethods->getFlags)(pPage)
#define TDB_PAGE_NCELLS(pPage) (*(pPage)->pPageMethods->getCellNum)(pPage)
#define TDB_PAGE_CCELLS(pPage) (*(pPage)->pPageMethods->getCellBody)(pPage)
#define TDB_PAGE_FCELL(pPage) (*(pPage)->pPageMethods->getCellFree)(pPage)
#define TDB_PAGE_NFREE(pPage) (*(pPage)->pPageMethods->getFreeBytes)(pPage)
#define TDB_PAGE_CELL_OFFSET_AT(pPage, idx) (*(pPage)->pPageMethods->getCellOffset)(pPage, idx)
#define TDB_PAGE_FLAGS_SET(pPage, FLAGS) (*(pPage)->pPageMethods->setFlags)(pPage, FLAGS)
#define TDB_PAGE_NCELLS_SET(pPage, NCELLS) (*(pPage)->pPageMethods->setCellNum)(pPage, NCELLS)
#define TDB_PAGE_CCELLS_SET(pPage, CCELLS) (*(pPage)->pPageMethods->setCellBody)(pPage, CCELLS)
#define TDB_PAGE_FCELL_SET(pPage, FCELL) (*(pPage)->pPageMethods->setCellFree)(pPage, FCELL)
#define TDB_PAGE_NFREE_SET(pPage, NFREE) (*(pPage)->pPageMethods->setFreeBytes)(pPage, NFREE)
#define TDB_PAGE_CELL_OFFSET_AT_SET(pPage, idx, OFFSET) (*(pPage)->pPageMethods->setCellOffset)(pPage, idx, OFFSET)
#define TDB_PAGE_OFFSET_SIZE(pPage) ((pPage)->pPageMethods->szOffset)
#define TDB_PAGE_CELL_AT(pPage, idx) ((pPage)->pData + TDB_PAGE_CELL_OFFSET_AT(pPage, idx))
// For page lock
// For page lock
#define P_LOCK_SUCC 0
#define P_LOCK_SUCC 0
#define P_LOCK_BUSY 1
#define P_LOCK_BUSY 1
#define P_LOCK_FAIL -1
#define P_LOCK_FAIL -1
#define TDB_INIT_PAGE_LOCK(pPage)
taosThreadSpinI
nit(&((pPage)->lock), 0)
#define TDB_INIT_PAGE_LOCK(pPage)
pthread_spin_i
nit(&((pPage)->lock), 0)
#define TDB_DESTROY_PAGE_LOCK(pPage)
taosThreadSpinD
estroy(&((pPage)->lock))
#define TDB_DESTROY_PAGE_LOCK(pPage)
pthread_spin_d
estroy(&((pPage)->lock))
#define TDB_LOCK_PAGE(pPage)
taosThreadSpinL
ock(&((pPage)->lock))
#define TDB_LOCK_PAGE(pPage)
pthread_spin_l
ock(&((pPage)->lock))
#define TDB_UNLOCK_PAGE(pPage)
taosThreadSpinU
nlock(&((pPage)->lock))
#define TDB_UNLOCK_PAGE(pPage)
pthread_spin_u
nlock(&((pPage)->lock))
#define TDB_TRY_LOCK_PAGE(pPage) \
#define TDB_TRY_LOCK_PAGE(pPage) \
({ \
({ \
int ret; \
int ret; \
...
@@ -119,10 +98,43 @@ struct SPage {
...
@@ -119,10 +98,43 @@ struct SPage {
})
})
// APIs
// APIs
int
tdbPageCreate
(
int
pageSize
,
SPage
**
ppPage
,
void
*
(
*
xMalloc
)(
void
*
,
size_t
),
void
*
arg
);
#define TDB_PAGE_TOTAL_CELLS(pPage) ((pPage)->nOverflow + (pPage)->pPageMethods->getCellNum(pPage))
int
tdbPageDestroy
(
SPage
*
pPage
,
void
(
*
xFree
)(
void
*
arg
,
void
*
ptr
),
void
*
arg
);
#define TDB_PAGE_USABLE_SIZE(pPage) ((u8 *)(pPage)->pPageFtr - (pPage)->pCellIdx)
int
tdbPageInsertCell
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
szCell
);
#define TDB_PAGE_PGNO(pPage) ((pPage)->pgid.pgno)
int
tdbPageDropCell
(
SPage
*
pPage
,
int
idx
);
#define TDB_BYTES_CELL_TAKEN(pPage, pCell) ((*(pPage)->xCellSize)(pPage, pCell) + (pPage)->pPageMethods->szOffset)
#define TDB_PAGE_OFFSET_SIZE(pPage) ((pPage)->pPageMethods->szOffset)
int
tdbPageCreate
(
int
pageSize
,
SPage
**
ppPage
,
void
*
(
*
xMalloc
)(
void
*
,
size_t
),
void
*
arg
);
int
tdbPageDestroy
(
SPage
*
pPage
,
void
(
*
xFree
)(
void
*
arg
,
void
*
ptr
),
void
*
arg
);
void
tdbPageZero
(
SPage
*
pPage
,
u8
szAmHdr
,
int
(
*
xCellSize
)(
const
SPage
*
,
SCell
*
));
void
tdbPageInit
(
SPage
*
pPage
,
u8
szAmHdr
,
int
(
*
xCellSize
)(
const
SPage
*
,
SCell
*
));
int
tdbPageInsertCell
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
szCell
,
u8
asOvfl
);
int
tdbPageDropCell
(
SPage
*
pPage
,
int
idx
);
void
tdbPageCopy
(
SPage
*
pFromPage
,
SPage
*
pToPage
);
static
inline
SCell
*
tdbPageGetCell
(
SPage
*
pPage
,
int
idx
)
{
SCell
*
pCell
;
int
iOvfl
;
int
lidx
;
ASSERT
(
idx
>=
0
&&
idx
<
TDB_PAGE_TOTAL_CELLS
(
pPage
));
iOvfl
=
0
;
for
(;
iOvfl
<
pPage
->
nOverflow
;
iOvfl
++
)
{
if
(
pPage
->
aiOvfl
[
iOvfl
]
==
idx
)
{
pCell
=
pPage
->
apOvfl
[
iOvfl
];
return
pCell
;
}
else
if
(
pPage
->
aiOvfl
[
iOvfl
]
>
idx
)
{
break
;
}
}
lidx
=
idx
-
iOvfl
;
ASSERT
(
lidx
>=
0
&&
lidx
<
pPage
->
pPageMethods
->
getCellNum
(
pPage
));
pCell
=
pPage
->
pData
+
pPage
->
pPageMethods
->
getCellOffset
(
pPage
,
lidx
);
return
pCell
;
}
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
source/libs/tdb/src/inc/tdbPager.h
浏览文件 @
10319e3c
...
@@ -20,15 +20,16 @@
...
@@ -20,15 +20,16 @@
extern
"C"
{
extern
"C"
{
#endif
#endif
int
tdbPagerOpen
(
SPCache
*
pCache
,
const
char
*
fileName
,
SPager
**
ppPager
);
int
tdbPagerOpen
(
SPCache
*
pCache
,
const
char
*
fileName
,
SPager
**
ppPager
);
int
tdbPagerClose
(
SPager
*
pPager
);
int
tdbPagerClose
(
SPager
*
pPager
);
int
tdbPagerOpenDB
(
SPager
*
pPager
,
SPgno
*
ppgno
,
bool
toCreate
);
int
tdbPagerOpenDB
(
SPager
*
pPager
,
SPgno
*
ppgno
,
bool
toCreate
);
int
tdbPagerWrite
(
SPager
*
pPager
,
SPage
*
pPage
);
int
tdbPagerWrite
(
SPager
*
pPager
,
SPage
*
pPage
);
int
tdbPagerBegin
(
SPager
*
pPager
);
int
tdbPagerBegin
(
SPager
*
pPager
);
int
tdbPagerCommit
(
SPager
*
pPager
);
int
tdbPagerCommit
(
SPager
*
pPager
);
int
tdbPagerGetPageSize
(
SPager
*
pPager
);
int
tdbPagerGetPageSize
(
SPager
*
pPager
);
int
tdbPagerFetchPage
(
SPager
*
pPager
,
SPgno
pgno
,
SPage
**
ppPage
,
int
(
*
initPage
)(
SPage
*
,
void
*
),
void
*
arg
);
int
tdbPagerFetchPage
(
SPager
*
pPager
,
SPgno
pgno
,
SPage
**
ppPage
,
int
(
*
initPage
)(
SPage
*
,
void
*
),
void
*
arg
);
int
tdbPagerNewPage
(
SPager
*
pPager
,
SPgno
*
ppgno
,
SPage
**
ppPage
,
int
(
*
initPage
)(
SPage
*
,
void
*
),
void
*
arg
);
int
tdbPagerNewPage
(
SPager
*
pPager
,
SPgno
*
ppgno
,
SPage
**
ppPage
,
int
(
*
initPage
)(
SPage
*
,
void
*
),
void
*
arg
);
void
tdbPagerReturnPage
(
SPager
*
pPager
,
SPage
*
pPage
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
source/libs/tdb/src/inc/tdbUtil.h
浏览文件 @
10319e3c
...
@@ -39,6 +39,38 @@ int tdbGetFileSize(const char *fname, int pgSize, SPgno *pSize);
...
@@ -39,6 +39,38 @@ int tdbGetFileSize(const char *fname, int pgSize, SPgno *pSize);
int
tdbPRead
(
int
fd
,
void
*
pData
,
int
count
,
i64
offset
);
int
tdbPRead
(
int
fd
,
void
*
pData
,
int
count
,
i64
offset
);
#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_FREE(PTR) \
do { \
if (PTR) { \
free((char *)(PTR) - sizeof(int)); \
} \
} while (0)
static
inline
void
*
tdbOsMalloc
(
void
*
arg
,
size_t
size
)
{
void
*
ptr
;
ptr
=
malloc
(
size
);
return
ptr
;
}
static
inline
void
tdbOsFree
(
void
*
arg
,
void
*
ptr
)
{
free
(
ptr
);
}
static
inline
int
tdbPutVarInt
(
u8
*
p
,
int
v
)
{
static
inline
int
tdbPutVarInt
(
u8
*
p
,
int
v
)
{
int
n
=
0
;
int
n
=
0
;
...
...
source/libs/tdb/src/page/tdbPage.c
浏览文件 @
10319e3c
...
@@ -18,13 +18,25 @@
...
@@ -18,13 +18,25 @@
extern
SPageMethods
pageMethods
;
extern
SPageMethods
pageMethods
;
extern
SPageMethods
pageLargeMethods
;
extern
SPageMethods
pageLargeMethods
;
typedef
struct
__attribute__
((
__packed__
))
{
#define TDB_PAGE_HDR_SIZE(pPage) ((pPage)->pPageMethods->szPageHdr)
u16
szCell
;
#define TDB_PAGE_FREE_CELL_SIZE(pPage) ((pPage)->pPageMethods->szFreeCell)
u16
nxOffset
;
#define TDB_PAGE_NCELLS(pPage) (*(pPage)->pPageMethods->getCellNum)(pPage)
}
SFreeCell
;
#define TDB_PAGE_CCELLS(pPage) (*(pPage)->pPageMethods->getCellBody)(pPage)
#define TDB_PAGE_FCELL(pPage) (*(pPage)->pPageMethods->getCellFree)(pPage)
#define TDB_PAGE_NFREE(pPage) (*(pPage)->pPageMethods->getFreeBytes)(pPage)
#define TDB_PAGE_CELL_OFFSET_AT(pPage, idx) (*(pPage)->pPageMethods->getCellOffset)(pPage, idx)
#define TDB_PAGE_NCELLS_SET(pPage, NCELLS) (*(pPage)->pPageMethods->setCellNum)(pPage, NCELLS)
#define TDB_PAGE_CCELLS_SET(pPage, CCELLS) (*(pPage)->pPageMethods->setCellBody)(pPage, CCELLS)
#define TDB_PAGE_FCELL_SET(pPage, FCELL) (*(pPage)->pPageMethods->setCellFree)(pPage, FCELL)
#define TDB_PAGE_NFREE_SET(pPage, NFREE) (*(pPage)->pPageMethods->setFreeBytes)(pPage, NFREE)
#define TDB_PAGE_CELL_OFFSET_AT_SET(pPage, idx, OFFSET) (*(pPage)->pPageMethods->setCellOffset)(pPage, idx, OFFSET)
#define TDB_PAGE_CELL_AT(pPage, idx) ((pPage)->pData + TDB_PAGE_CELL_OFFSET_AT(pPage, idx))
#define TDB_PAGE_MAX_FREE_BLOCK(pPage, szAmHdr) \
((pPage)->pageSize - (szAmHdr)-TDB_PAGE_HDR_SIZE(pPage) - sizeof(SPageFtr))
static
int
tdbPageAllocate
(
SPage
*
pPage
,
int
size
,
SCell
**
ppCell
);
static
int
tdbPageAllocate
(
SPage
*
pPage
,
int
size
,
SCell
**
ppCell
);
static
int
tdbPageDefragment
(
SPage
*
pPage
);
static
int
tdbPageDefragment
(
SPage
*
pPage
);
static
int
tdbPageFree
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
szCell
);
int
tdbPageCreate
(
int
pageSize
,
SPage
**
ppPage
,
void
*
(
*
xMalloc
)(
void
*
,
size_t
),
void
*
arg
)
{
int
tdbPageCreate
(
int
pageSize
,
SPage
**
ppPage
,
void
*
(
*
xMalloc
)(
void
*
,
size_t
),
void
*
arg
)
{
SPage
*
pPage
;
SPage
*
pPage
;
...
@@ -35,25 +47,26 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t)
...
@@ -35,25 +47,26 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t)
*
ppPage
=
NULL
;
*
ppPage
=
NULL
;
size
=
pageSize
+
sizeof
(
*
pPage
);
size
=
pageSize
+
sizeof
(
*
pPage
);
if
(
xMalloc
==
NULL
)
{
xMalloc
=
tdbOsMalloc
;
}
ptr
=
(
u8
*
)((
*
xMalloc
)(
arg
,
size
));
ptr
=
(
u8
*
)((
*
xMalloc
)(
arg
,
size
));
if
(
p
Page
==
NULL
)
{
if
(
p
tr
==
NULL
)
{
return
-
1
;
return
-
1
;
}
}
memset
(
ptr
,
0
,
size
);
memset
(
ptr
,
0
,
size
);
pPage
=
(
SPage
*
)(
ptr
+
pageSize
);
pPage
=
(
SPage
*
)(
ptr
+
pageSize
);
pPage
->
pData
=
ptr
;
TDB_INIT_PAGE_LOCK
(
pPage
)
;
pPage
->
pageSize
=
pageSize
;
pPage
->
pageSize
=
pageSize
;
pPage
->
pData
=
ptr
;
if
(
pageSize
<
65536
)
{
if
(
pageSize
<
65536
)
{
pPage
->
pPageMethods
=
&
pageMethods
;
pPage
->
pPageMethods
=
&
pageMethods
;
}
else
{
}
else
{
pPage
->
pPageMethods
=
&
pageLargeMethods
;
pPage
->
pPageMethods
=
&
pageLargeMethods
;
}
}
TDB_INIT_PAGE_LOCK
(
pPage
);
/* TODO */
*
ppPage
=
pPage
;
*
ppPage
=
pPage
;
return
0
;
return
0
;
...
@@ -62,157 +75,365 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t)
...
@@ -62,157 +75,365 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t)
int
tdbPageDestroy
(
SPage
*
pPage
,
void
(
*
xFree
)(
void
*
arg
,
void
*
ptr
),
void
*
arg
)
{
int
tdbPageDestroy
(
SPage
*
pPage
,
void
(
*
xFree
)(
void
*
arg
,
void
*
ptr
),
void
*
arg
)
{
u8
*
ptr
;
u8
*
ptr
;
if
(
!
xFree
)
{
xFree
=
tdbOsFree
;
}
ptr
=
pPage
->
pData
;
ptr
=
pPage
->
pData
;
(
*
xFree
)(
arg
,
ptr
);
(
*
xFree
)(
arg
,
ptr
);
return
0
;
return
0
;
}
}
int
tdbPageInsertCell
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
szCell
)
{
void
tdbPageZero
(
SPage
*
pPage
,
u8
szAmHdr
,
int
(
*
xCellSize
)(
const
SPage
*
,
SCell
*
))
{
int
ret
;
pPage
->
pPageHdr
=
pPage
->
pData
+
szAmHdr
;
SCell
*
pTarget
;
TDB_PAGE_NCELLS_SET
(
pPage
,
0
);
u8
*
pTmp
;
TDB_PAGE_CCELLS_SET
(
pPage
,
pPage
->
pageSize
-
sizeof
(
SPageFtr
));
int
j
;
TDB_PAGE_FCELL_SET
(
pPage
,
0
);
TDB_PAGE_NFREE_SET
(
pPage
,
TDB_PAGE_MAX_FREE_BLOCK
(
pPage
,
szAmHdr
));
pPage
->
pCellIdx
=
pPage
->
pPageHdr
+
TDB_PAGE_HDR_SIZE
(
pPage
);
pPage
->
pFreeStart
=
pPage
->
pCellIdx
;
pPage
->
pFreeEnd
=
pPage
->
pData
+
TDB_PAGE_CCELLS
(
pPage
);
pPage
->
pPageFtr
=
(
SPageFtr
*
)(
pPage
->
pData
+
pPage
->
pageSize
-
sizeof
(
SPageFtr
));
pPage
->
nOverflow
=
0
;
pPage
->
xCellSize
=
xCellSize
;
ASSERT
((
u8
*
)
pPage
->
pPageFtr
==
pPage
->
pFreeEnd
);
}
void
tdbPageInit
(
SPage
*
pPage
,
u8
szAmHdr
,
int
(
*
xCellSize
)(
const
SPage
*
,
SCell
*
))
{
pPage
->
pPageHdr
=
pPage
->
pData
+
szAmHdr
;
pPage
->
pCellIdx
=
pPage
->
pPageHdr
+
TDB_PAGE_HDR_SIZE
(
pPage
);
pPage
->
pFreeStart
=
pPage
->
pCellIdx
+
TDB_PAGE_OFFSET_SIZE
(
pPage
)
*
TDB_PAGE_NCELLS
(
pPage
);
pPage
->
pFreeEnd
=
pPage
->
pData
+
TDB_PAGE_CCELLS
(
pPage
);
pPage
->
pPageFtr
=
(
SPageFtr
*
)(
pPage
->
pData
+
pPage
->
pageSize
-
sizeof
(
SPageFtr
));
pPage
->
nOverflow
=
0
;
pPage
->
xCellSize
=
xCellSize
;
ASSERT
(
pPage
->
pFreeEnd
>=
pPage
->
pFreeStart
);
ASSERT
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
<=
TDB_PAGE_NFREE
(
pPage
));
}
if
(
pPage
->
nOverflow
||
szCell
+
TDB_PAGE_OFFSET_SIZE
(
pPage
)
>
pPage
->
nFree
)
{
int
tdbPageInsertCell
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
szCell
,
u8
asOvfl
)
{
// TODO: need to figure out if pCell may be used by outside of this function
int
nFree
;
j
=
pPage
->
nOverflow
++
;
int
nCells
;
int
iOvfl
;
int
lidx
;
// local idx
SCell
*
pNewCell
;
pPage
->
apOvfl
[
j
]
=
pCell
;
ASSERT
(
szCell
<=
TDB_PAGE_MAX_FREE_BLOCK
(
pPage
,
pPage
->
pPageHdr
-
pPage
->
pData
));
pPage
->
aiOvfl
[
j
]
=
idx
;
}
else
{
nFree
=
TDB_PAGE_NFREE
(
pPage
);
ret
=
tdbPageAllocate
(
pPage
,
szCell
,
&
pTarget
);
nCells
=
TDB_PAGE_NCELLS
(
pPage
);
if
(
ret
<
0
)
{
iOvfl
=
0
;
return
-
1
;
for
(;
iOvfl
<
pPage
->
nOverflow
;
iOvfl
++
)
{
if
(
pPage
->
aiOvfl
[
iOvfl
]
>=
idx
)
{
break
;
}
}
lidx
=
idx
-
iOvfl
;
if
(
asOvfl
||
nFree
<
szCell
+
TDB_PAGE_OFFSET_SIZE
(
pPage
))
{
// TODO: make it extensible
// add the cell as an overflow cell
for
(
int
i
=
pPage
->
nOverflow
;
i
>
iOvfl
;
i
--
)
{
pPage
->
apOvfl
[
i
]
=
pPage
->
apOvfl
[
i
-
1
];
pPage
->
aiOvfl
[
i
]
=
pPage
->
aiOvfl
[
i
-
1
];
}
}
memcpy
(
pTarget
,
pCell
,
szCell
);
// TODO: here has memory leak
pTmp
=
pPage
->
pCellIdx
+
idx
*
TDB_PAGE_OFFSET_SIZE
(
pPage
);
pNewCell
=
(
SCell
*
)
malloc
(
szCell
);
memmove
(
pTmp
+
TDB_PAGE_OFFSET_SIZE
(
pPage
),
pTmp
,
pPage
->
pFreeStart
-
pTmp
-
TDB_PAGE_OFFSET_SIZE
(
pPage
));
memcpy
(
pNewCell
,
pCell
,
szCell
);
TDB_PAGE_CELL_OFFSET_AT_SET
(
pPage
,
idx
,
pTarget
-
pPage
->
pData
);
TDB_PAGE_NCELLS_SET
(
pPage
,
TDB_PAGE_NCELLS
(
pPage
)
+
1
);
pPage
->
apOvfl
[
iOvfl
]
=
pNewCell
;
pPage
->
aiOvfl
[
iOvfl
]
=
idx
;
pPage
->
nOverflow
++
;
iOvfl
++
;
}
else
{
// page must has enough space to hold the cell locally
tdbPageAllocate
(
pPage
,
szCell
,
&
pNewCell
);
memcpy
(
pNewCell
,
pCell
,
szCell
);
// no overflow cell exists in this page
u8
*
src
=
pPage
->
pCellIdx
+
TDB_PAGE_OFFSET_SIZE
(
pPage
)
*
lidx
;
u8
*
dest
=
src
+
TDB_PAGE_OFFSET_SIZE
(
pPage
);
memmove
(
dest
,
src
,
pPage
->
pFreeStart
-
dest
);
TDB_PAGE_CELL_OFFSET_AT_SET
(
pPage
,
lidx
,
pNewCell
-
pPage
->
pData
);
TDB_PAGE_NCELLS_SET
(
pPage
,
nCells
+
1
);
ASSERT
(
pPage
->
pFreeStart
==
pPage
->
pCellIdx
+
TDB_PAGE_OFFSET_SIZE
(
pPage
)
*
(
nCells
+
1
));
}
for
(;
iOvfl
<
pPage
->
nOverflow
;
iOvfl
++
)
{
pPage
->
aiOvfl
[
iOvfl
]
++
;
}
}
return
0
;
return
0
;
}
}
int
tdbPageDropCell
(
SPage
*
pPage
,
int
idx
)
{
int
tdbPageDropCell
(
SPage
*
pPage
,
int
idx
)
{
// TODO
int
lidx
;
SCell
*
pCell
;
int
szCell
;
int
nCells
;
int
iOvfl
;
nCells
=
TDB_PAGE_NCELLS
(
pPage
);
ASSERT
(
idx
>=
0
&&
idx
<
nCells
+
pPage
->
nOverflow
);
iOvfl
=
0
;
for
(;
iOvfl
<
pPage
->
nOverflow
;
iOvfl
++
)
{
if
(
pPage
->
aiOvfl
[
iOvfl
]
==
idx
)
{
// remove the over flow cell
for
(;
(
++
iOvfl
)
<
pPage
->
nOverflow
;)
{
pPage
->
aiOvfl
[
iOvfl
-
1
]
=
pPage
->
aiOvfl
[
iOvfl
]
-
1
;
pPage
->
apOvfl
[
iOvfl
-
1
]
=
pPage
->
apOvfl
[
iOvfl
];
}
pPage
->
nOverflow
--
;
return
0
;
}
else
if
(
pPage
->
aiOvfl
[
iOvfl
]
>
idx
)
{
break
;
}
}
lidx
=
idx
-
iOvfl
;
pCell
=
TDB_PAGE_CELL_AT
(
pPage
,
lidx
);
szCell
=
(
*
pPage
->
xCellSize
)(
pPage
,
pCell
);
tdbPageFree
(
pPage
,
lidx
,
pCell
,
szCell
);
TDB_PAGE_NCELLS_SET
(
pPage
,
nCells
-
1
);
for
(;
iOvfl
<
pPage
->
nOverflow
;
iOvfl
++
)
{
pPage
->
aiOvfl
[
iOvfl
]
--
;
ASSERT
(
pPage
->
aiOvfl
[
iOvfl
]
>
0
);
}
return
0
;
return
0
;
}
}
static
int
tdbPageAllocate
(
SPage
*
pPage
,
int
size
,
SCell
**
ppCell
)
{
void
tdbPageCopy
(
SPage
*
pFromPage
,
SPage
*
pToPage
)
{
SCell
*
pCell
;
int
delta
,
nFree
;
SFreeCell
*
pFreeCell
;
u8
*
pOffset
;
int
ret
;
ASSERT
(
pPage
->
nFree
>
size
+
TDB_PAGE_OFFSET_SIZE
(
pPage
));
pToPage
->
pFreeStart
=
pToPage
->
pPageHdr
+
(
pFromPage
->
pFreeStart
-
pFromPage
->
pPageHdr
);
pToPage
->
pFreeEnd
=
(
u8
*
)(
pToPage
->
pPageFtr
)
-
((
u8
*
)
pFromPage
->
pPageFtr
-
pFromPage
->
pFreeEnd
);
ASSERT
(
pToPage
->
pFreeEnd
>=
pToPage
->
pFreeStart
);
memcpy
(
pToPage
->
pPageHdr
,
pFromPage
->
pPageHdr
,
pFromPage
->
pFreeStart
-
pFromPage
->
pPageHdr
);
memcpy
(
pToPage
->
pFreeEnd
,
pFromPage
->
pFreeEnd
,
(
u8
*
)
pFromPage
->
pPageFtr
-
pFromPage
->
pFreeEnd
);
ASSERT
(
TDB_PAGE_CCELLS
(
pToPage
)
==
pToPage
->
pFreeEnd
-
pToPage
->
pData
);
delta
=
(
pToPage
->
pPageHdr
-
pToPage
->
pData
)
-
(
pFromPage
->
pPageHdr
-
pFromPage
->
pData
);
if
(
delta
!=
0
)
{
nFree
=
TDB_PAGE_NFREE
(
pFromPage
);
TDB_PAGE_NFREE_SET
(
pToPage
,
nFree
-
delta
);
}
// Copy the overflow cells
for
(
int
iOvfl
=
0
;
iOvfl
<
pFromPage
->
nOverflow
;
iOvfl
++
)
{
pToPage
->
aiOvfl
[
iOvfl
]
=
pFromPage
->
aiOvfl
[
iOvfl
];
pToPage
->
apOvfl
[
iOvfl
]
=
pFromPage
->
apOvfl
[
iOvfl
];
}
pToPage
->
nOverflow
=
pFromPage
->
nOverflow
;
}
static
int
tdbPageAllocate
(
SPage
*
pPage
,
int
szCell
,
SCell
**
ppCell
)
{
SCell
*
pFreeCell
;
u8
*
pOffset
;
int
nFree
;
int
ret
;
int
cellFree
;
SCell
*
pCell
=
NULL
;
pCell
=
NULL
;
*
ppCell
=
NULL
;
*
ppCell
=
NULL
;
nFree
=
TDB_PAGE_NFREE
(
pPage
);
// 1. Try to allocate from the free space area
ASSERT
(
nFree
>=
szCell
+
TDB_PAGE_OFFSET_SIZE
(
pPage
));
if
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
>
size
+
TDB_PAGE_OFFSET_SIZE
(
pPage
))
{
ASSERT
(
TDB_PAGE_CCELLS
(
pPage
)
==
pPage
->
pFreeEnd
-
pPage
->
pData
);
pPage
->
pFreeEnd
-=
size
;
pPage
->
pFreeStart
+=
TDB_PAGE_OFFSET_SIZE
(
pPage
);
// 1. Try to allocate from the free space block area
if
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
>=
szCell
+
TDB_PAGE_OFFSET_SIZE
(
pPage
))
{
pPage
->
pFreeEnd
-=
szCell
;
pCell
=
pPage
->
pFreeEnd
;
pCell
=
pPage
->
pFreeEnd
;
TDB_PAGE_CCELLS_SET
(
pPage
,
pPage
->
pFreeEnd
-
pPage
->
pData
);
goto
_alloc_finish
;
}
}
// 2. Try to allocate from the page free list
// 2. Try to allocate from the page free list
if
((
pCell
==
NULL
)
&&
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
>=
TDB_PAGE_OFFSET_SIZE
(
pPage
))
&&
cellFree
=
TDB_PAGE_FCELL
(
pPage
);
TDB_PAGE_FCELL
(
pPage
))
{
ASSERT
(
cellFree
==
0
||
cellFree
>
pPage
->
pFreeEnd
-
pPage
->
pData
);
#if 0
if
(
cellFree
&&
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
>=
TDB_PAGE_OFFSET_SIZE
(
pPage
))
{
int szCell;
SCell
*
pPrevFreeCell
=
NULL
;
int nxOffset;
int
szPrevFreeCell
;
int
szFreeCell
;
pCell = pPage->pData + TDB_PAGE_FCELL(pPage);
int
nxFreeCell
;
pOffset = TDB_IS_LARGE_PAGE(pPage) ? ((SPageHdrL *)(pPage->pPageHdr))[0].fCell
int
newSize
;
: (u8 *)&(((SPageHdr *)(pPage->pPageHdr))[0].fCell);
szCell = TDB_PAGE_FREE_CELL_SIZE(pPage, pCell);
nxOffset = TDB_PAGE_FREE_CELL_NXOFFSET(pPage, pCell);
for
(;;)
{
for
(;;)
{
// Find a cell
if
(
cellFree
==
0
)
break
;
if (szCell >= size) {
if (szCell - size >= pPage->szFreeCell) {
pFreeCell
=
pPage
->
pData
+
cellFree
;
SCell *pTmpCell = pCell + size;
pPage
->
pPageMethods
->
getFreeCellInfo
(
pFreeCell
,
&
szFreeCell
,
&
nxFreeCell
);
TDB_PAGE_FREE_CELL_SIZE_SET(pPage, pTmpCell, szCell - size);
if
(
szFreeCell
>=
szCell
)
{
TDB_PAGE_FREE_CELL_NXOFFSET_SET(pPage, pTmpCell, nxOffset);
pCell
=
pFreeCell
;
// TODO: *pOffset = pTmpCell - pPage->pData;
newSize
=
szFreeCell
-
szCell
;
pFreeCell
+=
szCell
;
if
(
newSize
>=
TDB_PAGE_FREE_CELL_SIZE
(
pPage
))
{
pPage
->
pPageMethods
->
setFreeCellInfo
(
pFreeCell
,
newSize
,
nxFreeCell
);
if
(
pPrevFreeCell
)
{
pPage
->
pPageMethods
->
setFreeCellInfo
(
pPrevFreeCell
,
szPrevFreeCell
,
pFreeCell
-
pPage
->
pData
);
}
else
{
TDB_PAGE_FCELL_SET
(
pPage
,
pFreeCell
-
pPage
->
pData
);
}
}
else
{
}
else
{
TDB_PAGE_NFREE_SET(pPage, TDB_PAGE_NFREE(pPage) + szCell - size);
if
(
pPrevFreeCell
)
{
// TODO: *pOffset = nxOffset;
pPage
->
pPageMethods
->
setFreeCellInfo
(
pPrevFreeCell
,
szPrevFreeCell
,
nxFreeCell
);
}
else
{
TDB_PAGE_FCELL_SET
(
pPage
,
nxFreeCell
);
}
}
}
break;
}
// Not find a cell yet
goto
_alloc_finish
;
if (nxOffset > 0) {
pCell = pPage->pData + nxOffset;
pOffset = TDB_PAGE_FREE_CELL_NXOFFSET_PTR(pPage, pCell);
szCell = TDB_PAGE_FREE_CELL_SIZE(pPage, pCell);
nxOffset = TDB_PAGE_FREE_CELL_NXOFFSET(pPage, pCell);
continue;
}
else
{
}
else
{
pCell = NULL;
pPrevFreeCell
=
pFreeCell
;
break;
szPrevFreeCell
=
szFreeCell
;
cellFree
=
nxFreeCell
;
}
}
}
}
if (pCell) {
pPage->pFreeStart = pPage->pFreeStart + pPage->szOffset;
}
#endif
}
}
// 3. Try to dfragment and allocate again
// 3. Try to dfragment and allocate again
if
(
pCell
==
NULL
)
{
tdbPageDefragment
(
pPage
);
ret
=
tdbPageDefragment
(
pPage
);
ASSERT
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
==
nFree
);
if
(
ret
<
0
)
{
ASSERT
(
nFree
==
TDB_PAGE_NFREE
(
pPage
));
return
-
1
;
ASSERT
(
pPage
->
pFreeEnd
-
pPage
->
pData
==
TDB_PAGE_CCELLS
(
pPage
));
}
pPage
->
pFreeEnd
-=
szCell
;
pCell
=
pPage
->
pFreeEnd
;
TDB_PAGE_CCELLS_SET
(
pPage
,
pPage
->
pFreeEnd
-
pPage
->
pData
);
_alloc_finish:
ASSERT
(
pCell
);
pPage
->
pFreeStart
+=
TDB_PAGE_OFFSET_SIZE
(
pPage
);
TDB_PAGE_NFREE_SET
(
pPage
,
nFree
-
szCell
-
TDB_PAGE_OFFSET_SIZE
(
pPage
));
*
ppCell
=
pCell
;
return
0
;
}
ASSERT
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
>
size
+
TDB_PAGE_OFFSET_SIZE
(
pPage
));
static
int
tdbPageFree
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
szCell
)
{
ASSERT
(
pPage
->
nFree
==
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
);
int
nFree
;
int
cellFree
;
u8
*
dest
;
u8
*
src
;
// Allocate from the free space area again
ASSERT
(
pCell
>=
pPage
->
pFreeEnd
);
pPage
->
pFreeEnd
-=
size
;
ASSERT
(
pCell
+
szCell
<=
(
u8
*
)(
pPage
->
pPageFtr
));
pPage
->
pFreeStart
+=
TDB_PAGE_OFFSET_SIZE
(
pPage
);
ASSERT
(
pCell
==
TDB_PAGE_CELL_AT
(
pPage
,
idx
));
pCell
=
pPage
->
pFreeEnd
;
}
ASSERT
(
pCell
!=
NULL
);
nFree
=
TDB_PAGE_NFREE
(
pPage
);
pPage
->
nFree
=
pPage
->
nFree
-
size
-
TDB_PAGE_OFFSET_SIZE
(
pPage
);
if
(
pCell
==
pPage
->
pFreeEnd
)
{
*
ppCell
=
pCell
;
pPage
->
pFreeEnd
+=
szCell
;
return
0
;
TDB_PAGE_CCELLS_SET
(
pPage
,
pPage
->
pFreeEnd
-
pPage
->
pData
);
}
}
else
{
if
(
szCell
>=
TDB_PAGE_FREE_CELL_SIZE
(
pPage
))
{
cellFree
=
TDB_PAGE_FCELL
(
pPage
);
pPage
->
pPageMethods
->
setFreeCellInfo
(
pCell
,
szCell
,
cellFree
);
TDB_PAGE_FCELL_SET
(
pPage
,
pCell
-
pPage
->
pData
);
}
else
{
ASSERT
(
0
);
}
}
dest
=
pPage
->
pCellIdx
+
TDB_PAGE_OFFSET_SIZE
(
pPage
)
*
idx
;
src
=
dest
+
TDB_PAGE_OFFSET_SIZE
(
pPage
);
memmove
(
dest
,
src
,
pPage
->
pFreeStart
-
src
);
static
int
tdbPageFree
(
SPage
*
pPage
,
int
idx
,
SCell
*
pCell
,
int
size
)
{
pPage
->
pFreeStart
-=
TDB_PAGE_OFFSET_SIZE
(
pPage
);
// TODO
nFree
=
nFree
+
szCell
+
TDB_PAGE_OFFSET_SIZE
(
pPage
);
TDB_PAGE_NFREE_SET
(
pPage
,
nFree
);
return
0
;
return
0
;
}
}
static
int
tdbPageDefragment
(
SPage
*
pPage
)
{
static
int
tdbPageDefragment
(
SPage
*
pPage
)
{
// TODO
int
nFree
;
ASSERT
(
0
);
int
nCells
;
SCell
*
pCell
;
SCell
*
pNextCell
;
SCell
*
pTCell
;
int
szCell
;
int
idx
;
int
iCell
;
ASSERT
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
<
nFree
);
nFree
=
TDB_PAGE_NFREE
(
pPage
);
nCells
=
TDB_PAGE_NCELLS
(
pPage
);
// Loop to compact the page content
// Here we use an O(n^2) algorithm to do the job since
// this is a low frequency job.
pNextCell
=
(
u8
*
)
pPage
->
pPageFtr
;
pCell
=
NULL
;
for
(
iCell
=
0
;;
iCell
++
)
{
// compact over
if
(
iCell
==
nCells
)
{
pPage
->
pFreeEnd
=
pNextCell
;
break
;
}
for
(
int
i
=
0
;
i
<
nCells
;
i
++
)
{
if
(
TDB_PAGE_CELL_OFFSET_AT
(
pPage
,
i
)
<
pNextCell
-
pPage
->
pData
)
{
pTCell
=
TDB_PAGE_CELL_AT
(
pPage
,
i
);
if
(
pCell
==
NULL
||
pCell
<
pTCell
)
{
pCell
=
pTCell
;
idx
=
i
;
}
}
else
{
continue
;
}
}
ASSERT
(
pCell
!=
NULL
);
szCell
=
(
*
pPage
->
xCellSize
)(
pPage
,
pCell
);
ASSERT
(
pCell
+
szCell
<=
pNextCell
);
if
(
pCell
+
szCell
<
pNextCell
)
{
memmove
(
pNextCell
-
szCell
,
pCell
,
szCell
);
}
pCell
=
NULL
;
pNextCell
=
pNextCell
-
szCell
;
TDB_PAGE_CELL_OFFSET_AT_SET
(
pPage
,
idx
,
pNextCell
-
pPage
->
pData
);
}
ASSERT
(
pPage
->
pFreeEnd
-
pPage
->
pFreeStart
==
nFree
);
TDB_PAGE_CCELLS_SET
(
pPage
,
pPage
->
pFreeEnd
-
pPage
->
pData
);
TDB_PAGE_FCELL_SET
(
pPage
,
0
);
return
0
;
return
0
;
}
}
/* ---------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------- */
typedef
struct
__attribute__
((
__packed__
))
{
typedef
struct
__attribute__
((
__packed__
))
{
u16
flags
;
u16
cellNum
;
u16
cellNum
;
u16
cellBody
;
u16
cellBody
;
u16
cellFree
;
u16
cellFree
;
u16
nFree
;
u16
nFree
;
}
SPageHdr
;
}
SPageHdr
;
// flags
typedef
struct
__attribute__
((
__packed__
))
{
static
inline
u16
getPageFlags
(
SPage
*
pPage
)
{
return
((
SPageHdr
*
)(
pPage
->
pPageHdr
))[
0
].
flags
;
}
u16
szCell
;
static
inline
void
setPageFlags
(
SPage
*
pPage
,
u16
flags
)
{
((
SPageHdr
*
)(
pPage
->
pPageHdr
))[
0
].
flags
=
flags
;
}
u16
nxOffset
;
}
SFreeCell
;
// cellNum
// cellNum
static
inline
int
getPageCellNum
(
SPage
*
pPage
)
{
return
((
SPageHdr
*
)(
pPage
->
pPageHdr
))[
0
].
cellNum
;
}
static
inline
int
getPageCellNum
(
SPage
*
pPage
)
{
return
((
SPageHdr
*
)(
pPage
->
pPageHdr
))[
0
].
cellNum
;
}
...
@@ -253,20 +474,33 @@ static inline void setPageCellOffset(SPage *pPage, int idx, int offset) {
...
@@ -253,20 +474,33 @@ static inline void setPageCellOffset(SPage *pPage, int idx, int offset) {
((
u16
*
)
pPage
->
pCellIdx
)[
idx
]
=
(
u16
)
offset
;
((
u16
*
)
pPage
->
pCellIdx
)[
idx
]
=
(
u16
)
offset
;
}
}
// free cell info
static
inline
void
getPageFreeCellInfo
(
SCell
*
pCell
,
int
*
szCell
,
int
*
nxOffset
)
{
SFreeCell
*
pFreeCell
=
(
SFreeCell
*
)
pCell
;
*
szCell
=
pFreeCell
->
szCell
;
*
nxOffset
=
pFreeCell
->
nxOffset
;
}
static
inline
void
setPageFreeCellInfo
(
SCell
*
pCell
,
int
szCell
,
int
nxOffset
)
{
SFreeCell
*
pFreeCell
=
(
SFreeCell
*
)
pCell
;
pFreeCell
->
szCell
=
szCell
;
pFreeCell
->
nxOffset
=
nxOffset
;
}
SPageMethods
pageMethods
=
{
SPageMethods
pageMethods
=
{
2
,
// szOffset
2
,
// szOffset
sizeof
(
SPageHdr
),
// szPageHdr
sizeof
(
SPageHdr
),
// szPageHdr
sizeof
(
SFreeCell
),
// szFreeCell
sizeof
(
SFreeCell
),
// szFreeCell
getPage
Flags
,
// getPageFlags
getPage
CellNum
,
// getCellNum
setPage
Flags
,
// setFlagsp
setPage
CellNum
,
// setCellNum
getPageCell
Num
,
// getCellNum
getPageCell
Body
,
// getCellBody
setPageCell
Num
,
// setCellNum
setPageCell
Body
,
// setCellBody
getPageCell
Body
,
// getCellBody
getPageCell
Free
,
// getCellFree
setPageCell
Body
,
// setCellBody
setPageCell
Free
,
// setCellFree
getPage
CellFree
,
// getCellFree
getPage
NFree
,
// getFreeBytes
setPage
CellFree
,
// setCellFree
setPage
NFree
,
// setFreeBytes
getPage
NFree
,
// getFreeBytes
getPage
CellOffset
,
// getCellOffset
setPage
NFree
,
// setFreeBytes
setPage
CellOffset
,
// setCellOffset
getPage
CellOffset
,
// getCellOffset
getPage
FreeCellInfo
,
// getFreeCellInfo
setPage
CellOffset
// setCellOffset
setPage
FreeCellInfo
// setFreeCellInfo
};
};
\ No newline at end of file
source/libs/tdb/src/page/tdbPageL.c
浏览文件 @
10319e3c
...
@@ -16,11 +16,10 @@
...
@@ -16,11 +16,10 @@
#include "tdbInt.h"
#include "tdbInt.h"
typedef
struct
__attribute__
((
__packed__
))
{
typedef
struct
__attribute__
((
__packed__
))
{
u16
flags
;
u8
cellNum
[
3
];
u8
cellNum
[
3
];
u8
cellBody
[
3
];
u8
cellBody
[
3
];
u8
cellFree
[
3
];
u8
cellFree
[
3
];
u8
nFree
[
3
];
u8
nFree
[
3
];
}
SPageHdrL
;
}
SPageHdrL
;
typedef
struct
__attribute__
((
__packed__
))
{
typedef
struct
__attribute__
((
__packed__
))
{
...
@@ -28,10 +27,6 @@ typedef struct __attribute__((__packed__)) {
...
@@ -28,10 +27,6 @@ typedef struct __attribute__((__packed__)) {
u8
nxOffset
[
3
];
u8
nxOffset
[
3
];
}
SFreeCellL
;
}
SFreeCellL
;
// flags
static
inline
u16
getPageFlags
(
SPage
*
pPage
)
{
return
((
SPageHdrL
*
)(
pPage
->
pPageHdr
))[
0
].
flags
;
}
static
inline
void
setPageFlags
(
SPage
*
pPage
,
u16
flags
)
{
((
SPageHdrL
*
)(
pPage
->
pPageHdr
))[
0
].
flags
=
flags
;
}
// cellNum
// cellNum
static
inline
int
getPageCellNum
(
SPage
*
pPage
)
{
return
TDB_GET_U24
(((
SPageHdrL
*
)(
pPage
->
pPageHdr
))[
0
].
cellNum
);
}
static
inline
int
getPageCellNum
(
SPage
*
pPage
)
{
return
TDB_GET_U24
(((
SPageHdrL
*
)(
pPage
->
pPageHdr
))[
0
].
cellNum
);
}
static
inline
void
setPageCellNum
(
SPage
*
pPage
,
int
cellNum
)
{
static
inline
void
setPageCellNum
(
SPage
*
pPage
,
int
cellNum
)
{
...
@@ -66,20 +61,33 @@ static inline void setPageCellOffset(SPage *pPage, int idx, int offset) {
...
@@ -66,20 +61,33 @@ static inline void setPageCellOffset(SPage *pPage, int idx, int offset) {
TDB_PUT_U24
(
pPage
->
pCellIdx
+
3
*
idx
,
offset
);
TDB_PUT_U24
(
pPage
->
pCellIdx
+
3
*
idx
,
offset
);
}
}
// free cell info
static
inline
void
getPageFreeCellInfo
(
SCell
*
pCell
,
int
*
szCell
,
int
*
nxOffset
)
{
SFreeCellL
*
pFreeCell
=
(
SFreeCellL
*
)
pCell
;
*
szCell
=
TDB_GET_U24
(
pFreeCell
->
szCell
);
*
nxOffset
=
TDB_GET_U24
(
pFreeCell
->
nxOffset
);
}
static
inline
void
setPageFreeCellInfo
(
SCell
*
pCell
,
int
szCell
,
int
nxOffset
)
{
SFreeCellL
*
pFreeCell
=
(
SFreeCellL
*
)
pCell
;
TDB_PUT_U24
(
pFreeCell
->
szCell
,
szCell
);
TDB_PUT_U24
(
pFreeCell
->
nxOffset
,
nxOffset
);
}
SPageMethods
pageLargeMethods
=
{
SPageMethods
pageLargeMethods
=
{
3
,
// szOffset
3
,
// szOffset
sizeof
(
SPageHdrL
),
// szPageHdr
sizeof
(
SPageHdrL
),
// szPageHdr
sizeof
(
SFreeCellL
),
// szFreeCell
sizeof
(
SFreeCellL
),
// szFreeCell
getPage
Flags
,
// getPageFlags
getPage
CellNum
,
// getCellNum
setPage
Flags
,
// setFlagsp
setPage
CellNum
,
// setCellNum
getPageCell
Num
,
// getCellNum
getPageCell
Body
,
// getCellBody
setPageCell
Num
,
// setCellNum
setPageCell
Body
,
// setCellBody
getPageCell
Body
,
// getCellBody
getPageCell
Free
,
// getCellFree
setPageCell
Body
,
// setCellBody
setPageCell
Free
,
// setCellFree
getPage
CellFree
,
// getCellFree
getPage
NFree
,
// getFreeBytes
setPage
CellFree
,
// setCellFree
setPage
NFree
,
// setFreeBytes
getPage
NFree
,
// getFreeBytes
getPage
CellOffset
,
// getCellOffset
setPage
NFree
,
// setFreeBytes
setPage
CellOffset
,
// setCellOffset
getPage
CellOffset
,
// getCellOffset
getPage
FreeCellInfo
,
// getFreeCellInfo
setPage
CellOffset
// setCellOffset
setPage
FreeCellInfo
// setFreeCellInfo
};
};
\ No newline at end of file
source/libs/tdb/test/tdbTest.cpp
浏览文件 @
10319e3c
...
@@ -2,28 +2,190 @@
...
@@ -2,28 +2,190 @@
#include "tdbInt.h"
#include "tdbInt.h"
#include <string>
typedef
struct
SPoolMem
{
int64_t
size
;
struct
SPoolMem
*
prev
;
struct
SPoolMem
*
next
;
}
SPoolMem
;
static
SPoolMem
*
openPool
()
{
SPoolMem
*
pPool
=
(
SPoolMem
*
)
malloc
(
sizeof
(
*
pPool
));
pPool
->
prev
=
pPool
->
next
=
pPool
;
pPool
->
size
=
0
;
return
pPool
;
}
static
void
closePool
(
SPoolMem
*
pPool
)
{
SPoolMem
*
pMem
;
do
{
pMem
=
pPool
->
next
;
if
(
pMem
==
pPool
)
break
;
pMem
->
next
->
prev
=
pMem
->
prev
;
pMem
->
prev
->
next
=
pMem
->
next
;
pPool
->
size
-=
pMem
->
size
;
free
(
pMem
);
}
while
(
1
);
assert
(
pPool
->
size
==
0
);
free
(
pPool
);
}
static
void
*
poolMalloc
(
void
*
arg
,
int
size
)
{
void
*
ptr
=
NULL
;
SPoolMem
*
pPool
=
(
SPoolMem
*
)
arg
;
SPoolMem
*
pMem
;
pMem
=
(
SPoolMem
*
)
malloc
(
sizeof
(
*
pMem
)
+
size
);
if
(
pMem
==
NULL
)
{
assert
(
0
);
}
pMem
->
size
=
sizeof
(
*
pMem
)
+
size
;
pMem
->
next
=
pPool
->
next
;
pMem
->
prev
=
pPool
;
pPool
->
next
->
prev
=
pMem
;
pPool
->
next
=
pMem
;
pPool
->
size
+=
pMem
->
size
;
ptr
=
(
void
*
)(
&
pMem
[
1
]);
return
ptr
;
}
static
void
poolFree
(
void
*
arg
,
void
*
ptr
)
{
SPoolMem
*
pPool
=
(
SPoolMem
*
)
arg
;
SPoolMem
*
pMem
;
pMem
=
&
(((
SPoolMem
*
)
ptr
)[
-
1
]);
pMem
->
next
->
prev
=
pMem
->
prev
;
pMem
->
prev
->
next
=
pMem
->
next
;
pPool
->
size
-=
pMem
->
size
;
free
(
pMem
);
}
static
int
tKeyCmpr
(
const
void
*
pKey1
,
int
kLen1
,
const
void
*
pKey2
,
int
kLen2
)
{
int
k1
,
k2
;
std
::
string
s1
((
char
*
)
pKey1
+
3
,
kLen1
-
3
);
std
::
string
s2
((
char
*
)
pKey2
+
3
,
kLen2
-
3
);
k1
=
stoi
(
s1
);
k2
=
stoi
(
s2
);
if
(
k1
<
k2
)
{
return
-
1
;
}
else
if
(
k1
>
k2
)
{
return
1
;
}
else
{
return
0
;
}
}
static
int
tDefaultKeyCmpr
(
const
void
*
pKey1
,
int
keyLen1
,
const
void
*
pKey2
,
int
keyLen2
)
{
int
mlen
;
int
cret
;
ASSERT
(
keyLen1
>
0
&&
keyLen2
>
0
&&
pKey1
!=
NULL
&&
pKey2
!=
NULL
);
mlen
=
keyLen1
<
keyLen2
?
keyLen1
:
keyLen2
;
cret
=
memcmp
(
pKey1
,
pKey2
,
mlen
);
if
(
cret
==
0
)
{
if
(
keyLen1
<
keyLen2
)
{
cret
=
-
1
;
}
else
if
(
keyLen1
>
keyLen2
)
{
cret
=
1
;
}
else
{
cret
=
0
;
}
}
return
cret
;
}
TEST
(
tdb_test
,
simple_test
)
{
TEST
(
tdb_test
,
simple_test
)
{
int
ret
;
int
ret
;
STEnv
*
pEnv
;
STEnv
*
pEnv
;
STDb
*
pDb
;
STDB
*
pDb
;
FKeyComparator
compFunc
;
int
nData
=
10000000
;
// Open Env
// Open Env
ret
=
tdbEnvOpen
(
"tdb"
,
1024
,
2
0
,
&
pEnv
);
ret
=
tdbEnvOpen
(
"tdb"
,
4096
,
25600
0
,
&
pEnv
);
GTEST_ASSERT_EQ
(
ret
,
0
);
GTEST_ASSERT_EQ
(
ret
,
0
);
// Create a database
// Create a database
ret
=
tdbDbOpen
(
"db.db"
,
TDB_VARIANT_LEN
,
TDB_VARIANT_LEN
,
NULL
,
pEnv
,
&
pDb
);
compFunc
=
tKeyCmpr
;
ret
=
tdbDbOpen
(
"db.db"
,
TDB_VARIANT_LEN
,
TDB_VARIANT_LEN
,
compFunc
,
pEnv
,
&
pDb
);
GTEST_ASSERT_EQ
(
ret
,
0
);
GTEST_ASSERT_EQ
(
ret
,
0
);
{
// Insert some data
{
char
key
[
64
];
char
key
[
64
];
char
val
[
64
];
char
val
[
64
];
for
(
int
i
=
1
;
i
<=
1000
;
i
++
)
{
{
// Insert some data
sprintf
(
key
,
"key%d"
,
i
);
sprintf
(
val
,
"value%d"
,
i
);
for
(
int
i
=
1
;
i
<=
nData
;
i
++
)
{
ret
=
tdbDbInsert
(
pDb
,
key
,
strlen
(
key
),
val
,
strlen
(
val
));
sprintf
(
key
,
"key%d"
,
i
);
sprintf
(
val
,
"value%d"
,
i
);
ret
=
tdbDbInsert
(
pDb
,
key
,
strlen
(
key
),
val
,
strlen
(
val
));
GTEST_ASSERT_EQ
(
ret
,
0
);
}
}
{
// Query the data
void
*
pVal
=
NULL
;
int
vLen
;
for
(
int
i
=
1
;
i
<=
nData
;
i
++
)
{
sprintf
(
key
,
"key%d"
,
i
);
sprintf
(
val
,
"value%d"
,
i
);
ret
=
tdbDbGet
(
pDb
,
key
,
strlen
(
key
),
&
pVal
,
&
vLen
);
GTEST_ASSERT_EQ
(
ret
,
0
);
GTEST_ASSERT_EQ
(
vLen
,
strlen
(
val
));
GTEST_ASSERT_EQ
(
memcmp
(
val
,
pVal
,
vLen
),
0
);
}
TDB_FREE
(
pVal
);
}
{
// Iterate to query the DB data
STDBC
*
pDBC
;
void
*
pKey
=
NULL
;
void
*
pVal
=
NULL
;
int
vLen
,
kLen
;
int
count
=
0
;
ret
=
tdbDbcOpen
(
pDb
,
&
pDBC
);
GTEST_ASSERT_EQ
(
ret
,
0
);
GTEST_ASSERT_EQ
(
ret
,
0
);
for
(;;)
{
ret
=
tdbDbNext
(
pDBC
,
&
pKey
,
&
kLen
,
&
pVal
,
&
vLen
);
if
(
ret
<
0
)
break
;
// std::cout.write((char *)pKey, kLen) /* << " " << kLen */ << " ";
// std::cout.write((char *)pVal, vLen) /* << " " << vLen */;
// std::cout << std::endl;
count
++
;
}
GTEST_ASSERT_EQ
(
count
,
nData
);
tdbDbcClose
(
pDBC
);
TDB_FREE
(
pKey
);
TDB_FREE
(
pVal
);
}
}
}
}
...
...
source/util/src/tprocess.c
浏览文件 @
10319e3c
...
@@ -56,7 +56,6 @@ typedef struct SProcObj {
...
@@ -56,7 +56,6 @@ typedef struct SProcObj {
int32_t
pid
;
int32_t
pid
;
bool
isChild
;
bool
isChild
;
bool
stopFlag
;
bool
stopFlag
;
bool
testFlag
;
}
SProcObj
;
}
SProcObj
;
static
int32_t
taosProcInitMutex
(
TdThreadMutex
**
ppMutex
,
int32_t
*
pShmid
)
{
static
int32_t
taosProcInitMutex
(
TdThreadMutex
**
ppMutex
,
int32_t
*
pShmid
)
{
...
@@ -77,7 +76,7 @@ static int32_t taosProcInitMutex(TdThreadMutex **ppMutex, int32_t *pShmid) {
...
@@ -77,7 +76,7 @@ static int32_t taosProcInitMutex(TdThreadMutex **ppMutex, int32_t *pShmid) {
goto
_OVER
;
goto
_OVER
;
}
}
shmid
=
shmget
(
IPC_PRIVATE
,
sizeof
(
TdThreadMutex
),
0600
);
shmid
=
shmget
(
IPC_PRIVATE
,
sizeof
(
TdThreadMutex
),
IPC_CREAT
|
0600
);
if
(
shmid
<=
0
)
{
if
(
shmid
<=
0
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
uError
(
"failed to init mutex while shmget since %s"
,
terrstr
());
uError
(
"failed to init mutex while shmget since %s"
,
terrstr
());
...
@@ -101,8 +100,13 @@ static int32_t taosProcInitMutex(TdThreadMutex **ppMutex, int32_t *pShmid) {
...
@@ -101,8 +100,13 @@ static int32_t taosProcInitMutex(TdThreadMutex **ppMutex, int32_t *pShmid) {
_OVER:
_OVER:
if
(
code
!=
0
)
{
if
(
code
!=
0
)
{
taosThreadMutexDestroy
(
pMutex
);
if
(
pMutex
!=
NULL
)
{
shmctl
(
shmid
,
IPC_RMID
,
NULL
);
taosThreadMutexDestroy
(
pMutex
);
shmdt
(
pMutex
);
}
if
(
shmid
>=
0
)
{
shmctl
(
shmid
,
IPC_RMID
,
NULL
);
}
}
else
{
}
else
{
*
ppMutex
=
pMutex
;
*
ppMutex
=
pMutex
;
*
pShmid
=
shmid
;
*
pShmid
=
shmid
;
...
@@ -112,12 +116,12 @@ _OVER:
...
@@ -112,12 +116,12 @@ _OVER:
return
code
;
return
code
;
}
}
static
void
taosProcDestroyMutex
(
TdThreadMutex
*
pMutex
,
int32_t
*
pS
hmid
)
{
static
void
taosProcDestroyMutex
(
TdThreadMutex
*
pMutex
,
int32_t
s
hmid
)
{
if
(
pMutex
!=
NULL
)
{
if
(
pMutex
!=
NULL
)
{
taosThreadMutexDestroy
(
pMutex
);
taosThreadMutexDestroy
(
pMutex
);
}
}
if
(
*
pShmid
>
0
)
{
if
(
shmid
>=
0
)
{
shmctl
(
*
pS
hmid
,
IPC_RMID
,
NULL
);
shmctl
(
s
hmid
,
IPC_RMID
,
NULL
);
}
}
}
}
...
@@ -141,13 +145,14 @@ static int32_t taosProcInitBuffer(void **ppBuffer, int32_t size) {
...
@@ -141,13 +145,14 @@ static int32_t taosProcInitBuffer(void **ppBuffer, int32_t size) {
return
shmid
;
return
shmid
;
}
}
static
void
taosProcDestroyBuffer
(
void
*
pBuffer
,
int32_t
*
pShmid
)
{
static
void
taosProcDestroyBuffer
(
void
*
pBuffer
,
int32_t
shmid
)
{
if
(
*
pShmid
>
0
)
{
if
(
shmid
>
0
)
{
shmctl
(
*
pShmid
,
IPC_RMID
,
NULL
);
shmdt
(
pBuffer
);
shmctl
(
shmid
,
IPC_RMID
,
NULL
);
}
}
}
}
static
SProcQueue
*
taosProc
QueueInit
(
int32_t
size
)
{
static
SProcQueue
*
taosProc
InitQueue
(
int32_t
size
)
{
if
(
size
<=
0
)
size
=
SHM_DEFAULT_SIZE
;
if
(
size
<=
0
)
size
=
SHM_DEFAULT_SIZE
;
int32_t
bufSize
=
CEIL8
(
size
);
int32_t
bufSize
=
CEIL8
(
size
);
...
@@ -155,29 +160,28 @@ static SProcQueue *taosProcQueueInit(int32_t size) {
...
@@ -155,29 +160,28 @@ static SProcQueue *taosProcQueueInit(int32_t size) {
SProcQueue
*
pQueue
=
NULL
;
SProcQueue
*
pQueue
=
NULL
;
int32_t
shmId
=
taosProcInitBuffer
((
void
**
)
&
pQueue
,
bufSize
+
headSize
);
int32_t
shmId
=
taosProcInitBuffer
((
void
**
)
&
pQueue
,
bufSize
+
headSize
);
if
(
shmId
<
=
0
)
{
if
(
shmId
<
0
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
return
NULL
;
}
}
pQueue
->
bufferShmid
=
shmId
;
pQueue
->
bufferShmid
=
shmId
;
if
(
taosProcInitMutex
(
&
pQueue
->
mutex
,
&
pQueue
->
mutexShmid
)
!=
0
)
{
if
(
taosProcInitMutex
(
&
pQueue
->
mutex
,
&
pQueue
->
mutexShmid
)
!=
0
)
{
taos
MemoryFree
(
pQueue
);
taos
ProcDestroyBuffer
(
pQueue
,
pQueue
->
bufferShmid
);
return
NULL
;
return
NULL
;
}
}
if
(
tsem_init
(
&
pQueue
->
sem
,
1
,
0
)
!=
0
)
{
if
(
tsem_init
(
&
pQueue
->
sem
,
1
,
0
)
!=
0
)
{
taosProcDestroyMutex
(
pQueue
->
mutex
,
&
pQueue
->
mutexShmid
);
taosProcDestroyMutex
(
pQueue
->
mutex
,
pQueue
->
mutexShmid
);
taos
MemoryFree
(
pQueue
);
taos
ProcDestroyBuffer
(
pQueue
,
pQueue
->
bufferShmid
);
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
return
NULL
;
}
}
if
(
taosProcInitMutex
(
&
pQueue
->
mutex
,
&
pQueue
->
mutexShmid
)
!=
0
)
{
if
(
taosProcInitMutex
(
&
pQueue
->
mutex
,
&
pQueue
->
mutexShmid
)
!=
0
)
{
taosProcDestroyMutex
(
pQueue
->
mutex
,
&
pQueue
->
mutexShmid
);
tsem_destroy
(
&
pQueue
->
sem
);
tsem_destroy
(
&
pQueue
->
sem
);
taosMemoryFree
(
pQueue
);
taosProcDestroyMutex
(
pQueue
->
mutex
,
pQueue
->
mutexShmid
);
taosProcDestroyBuffer
(
pQueue
,
pQueue
->
bufferShmid
);
return
NULL
;
return
NULL
;
}
}
...
@@ -190,12 +194,12 @@ static SProcQueue *taosProcQueueInit(int32_t size) {
...
@@ -190,12 +194,12 @@ static SProcQueue *taosProcQueueInit(int32_t size) {
return
pQueue
;
return
pQueue
;
}
}
static
void
taosProc
QueueCleanup
(
SProcQueue
*
pQueue
)
{
static
void
taosProc
CleanupQueue
(
SProcQueue
*
pQueue
)
{
if
(
pQueue
!=
NULL
)
{
if
(
pQueue
!=
NULL
)
{
uDebug
(
"proc:%s, queue:%p clean up"
,
pQueue
->
name
,
pQueue
);
uDebug
(
"proc:%s, queue:%p clean up"
,
pQueue
->
name
,
pQueue
);
taosProcDestroyMutex
(
pQueue
->
mutex
,
&
pQueue
->
mutexShmid
);
tsem_destroy
(
&
pQueue
->
sem
);
tsem_destroy
(
&
pQueue
->
sem
);
taosMemoryFree
(
pQueue
);
taosProcDestroyMutex
(
pQueue
->
mutex
,
pQueue
->
mutexShmid
);
taosProcDestroyBuffer
(
pQueue
,
pQueue
->
bufferShmid
);
}
}
}
}
...
@@ -204,6 +208,11 @@ static int32_t taosProcQueuePush(SProcQueue *pQueue, char *pHead, int32_t rawHea
...
@@ -204,6 +208,11 @@ static int32_t taosProcQueuePush(SProcQueue *pQueue, char *pHead, int32_t rawHea
const
int32_t
bodyLen
=
CEIL8
(
rawBodyLen
);
const
int32_t
bodyLen
=
CEIL8
(
rawBodyLen
);
const
int32_t
fullLen
=
headLen
+
bodyLen
+
8
;
const
int32_t
fullLen
=
headLen
+
bodyLen
+
8
;
if
(
headLen
<=
0
||
bodyLen
<=
0
)
{
terrno
=
TSDB_CODE_INVALID_PARA
;
return
-
1
;
}
taosThreadMutexLock
(
pQueue
->
mutex
);
taosThreadMutexLock
(
pQueue
->
mutex
);
if
(
fullLen
>
pQueue
->
avail
)
{
if
(
fullLen
>
pQueue
->
avail
)
{
taosThreadMutexUnlock
(
pQueue
->
mutex
);
taosThreadMutexUnlock
(
pQueue
->
mutex
);
...
@@ -255,7 +264,7 @@ static int32_t taosProcQueuePush(SProcQueue *pQueue, char *pHead, int32_t rawHea
...
@@ -255,7 +264,7 @@ static int32_t taosProcQueuePush(SProcQueue *pQueue, char *pHead, int32_t rawHea
taosThreadMutexUnlock
(
pQueue
->
mutex
);
taosThreadMutexUnlock
(
pQueue
->
mutex
);
tsem_post
(
&
pQueue
->
sem
);
tsem_post
(
&
pQueue
->
sem
);
uTrace
(
"proc:%s, push msg:%p:%d cont:%p:%d to queue:%p"
,
pQueue
->
name
,
pHead
,
rawHeadLen
,
pBody
,
rawB
odyLen
,
pQueue
);
uTrace
(
"proc:%s, push msg:%p:%d cont:%p:%d to queue:%p"
,
pQueue
->
name
,
pHead
,
headLen
,
pBody
,
b
odyLen
,
pQueue
);
return
0
;
return
0
;
}
}
...
@@ -344,12 +353,10 @@ SProcObj *taosProcInit(const SProcCfg *pCfg) {
...
@@ -344,12 +353,10 @@ SProcObj *taosProcInit(const SProcCfg *pCfg) {
}
}
pProc
->
name
=
pCfg
->
name
;
pProc
->
name
=
pCfg
->
name
;
pProc
->
testFlag
=
pCfg
->
testFlag
;
pProc
->
pChildQueue
=
taosProcInitQueue
(
pCfg
->
childQueueSize
);
pProc
->
pParentQueue
=
taosProcInitQueue
(
pCfg
->
parentQueueSize
);
pProc
->
pChildQueue
=
taosProcQueueInit
(
pCfg
->
childQueueSize
);
pProc
->
pParentQueue
=
taosProcQueueInit
(
pCfg
->
parentQueueSize
);
if
(
pProc
->
pChildQueue
==
NULL
||
pProc
->
pParentQueue
==
NULL
)
{
if
(
pProc
->
pChildQueue
==
NULL
||
pProc
->
pParentQueue
==
NULL
)
{
taosProc
QueueCleanup
(
pProc
->
pChildQueue
);
taosProc
CleanupQueue
(
pProc
->
pChildQueue
);
taosMemoryFree
(
pProc
);
taosMemoryFree
(
pProc
);
return
NULL
;
return
NULL
;
}
}
...
@@ -369,17 +376,15 @@ SProcObj *taosProcInit(const SProcCfg *pCfg) {
...
@@ -369,17 +376,15 @@ SProcObj *taosProcInit(const SProcCfg *pCfg) {
pProc
->
pParentQueue
->
freeBodyFp
=
pCfg
->
parentFreeBodyFp
;
pProc
->
pParentQueue
->
freeBodyFp
=
pCfg
->
parentFreeBodyFp
;
pProc
->
pParentQueue
->
consumeFp
=
pCfg
->
parentConsumeFp
;
pProc
->
pParentQueue
->
consumeFp
=
pCfg
->
parentConsumeFp
;
uDebug
(
"proc:%s, initialized, child queue:%p parent queue:%p"
,
pProc
->
name
,
pProc
->
pChildQueue
,
pProc
->
pParentQueue
);
uDebug
(
"proc:%s, i
s i
nitialized, child queue:%p parent queue:%p"
,
pProc
->
name
,
pProc
->
pChildQueue
,
pProc
->
pParentQueue
);
if
(
!
pProc
->
testFlag
)
{
pProc
->
pid
=
fork
();
pProc
->
pid
=
fork
();
if
(
pProc
->
pid
==
0
)
{
if
(
pProc
->
pid
==
0
)
{
pProc
->
isChild
=
1
;
pProc
->
isChild
=
1
;
prctl
(
PR_SET_NAME
,
pProc
->
name
,
NULL
,
NULL
,
NULL
);
uInfo
(
"this is child process, pid:%d"
,
pProc
->
pid
);
}
else
{
}
else
{
pProc
->
isChild
=
0
;
pProc
->
isChild
=
0
;
uInfo
(
"this is parent process, child pid:%d"
,
pProc
->
pid
);
uInfo
(
"this is parent process, pid:%d"
,
pProc
->
pid
);
}
}
}
return
pProc
;
return
pProc
;
...
@@ -398,7 +403,7 @@ static void taosProcThreadLoop(SProcQueue *pQueue) {
...
@@ -398,7 +403,7 @@ static void taosProcThreadLoop(SProcQueue *pQueue) {
if
(
code
<
0
)
{
if
(
code
<
0
)
{
uDebug
(
"proc:%s, get no message from queue:%p and exiting"
,
pQueue
->
name
,
pQueue
);
uDebug
(
"proc:%s, get no message from queue:%p and exiting"
,
pQueue
->
name
,
pQueue
);
break
;
break
;
}
else
if
(
code
<
0
)
{
}
else
if
(
code
==
0
)
{
uTrace
(
"proc:%s, get no message from queue:%p since %s"
,
pQueue
->
name
,
pQueue
,
terrstr
());
uTrace
(
"proc:%s, get no message from queue:%p since %s"
,
pQueue
->
name
,
pQueue
,
terrstr
());
taosMsleep
(
1
);
taosMsleep
(
1
);
continue
;
continue
;
...
@@ -413,16 +418,14 @@ int32_t taosProcRun(SProcObj *pProc) {
...
@@ -413,16 +418,14 @@ int32_t taosProcRun(SProcObj *pProc) {
taosThreadAttrInit
(
&
thAttr
);
taosThreadAttrInit
(
&
thAttr
);
taosThreadAttrSetDetachState
(
&
thAttr
,
PTHREAD_CREATE_JOINABLE
);
taosThreadAttrSetDetachState
(
&
thAttr
,
PTHREAD_CREATE_JOINABLE
);
if
(
pProc
->
isChild
||
pProc
->
testFlag
)
{
if
(
pProc
->
isChild
)
{
if
(
taosThreadCreate
(
&
pProc
->
childThread
,
&
thAttr
,
(
ProcThreadFp
)
taosProcThreadLoop
,
pProc
->
pChildQueue
)
!=
0
)
{
if
(
taosThreadCreate
(
&
pProc
->
childThread
,
&
thAttr
,
(
ProcThreadFp
)
taosProcThreadLoop
,
pProc
->
pChildQueue
)
!=
0
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
uError
(
"failed to create thread since %s"
,
terrstr
());
uError
(
"failed to create thread since %s"
,
terrstr
());
return
-
1
;
return
-
1
;
}
}
uDebug
(
"proc:%s, child start to consume queue:%p"
,
pProc
->
name
,
pProc
->
pChildQueue
);
uDebug
(
"proc:%s, child start to consume queue:%p"
,
pProc
->
name
,
pProc
->
pChildQueue
);
}
}
else
{
if
(
!
pProc
->
isChild
||
pProc
->
testFlag
)
{
if
(
taosThreadCreate
(
&
pProc
->
parentThread
,
&
thAttr
,
(
ProcThreadFp
)
taosProcThreadLoop
,
pProc
->
pParentQueue
)
!=
0
)
{
if
(
taosThreadCreate
(
&
pProc
->
parentThread
,
&
thAttr
,
(
ProcThreadFp
)
taosProcThreadLoop
,
pProc
->
pParentQueue
)
!=
0
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
uError
(
"failed to create thread since %s"
,
terrstr
());
uError
(
"failed to create thread since %s"
,
terrstr
());
...
@@ -441,12 +444,14 @@ void taosProcStop(SProcObj *pProc) {
...
@@ -441,12 +444,14 @@ void taosProcStop(SProcObj *pProc) {
bool
taosProcIsChild
(
SProcObj
*
pProc
)
{
return
pProc
->
isChild
;
}
bool
taosProcIsChild
(
SProcObj
*
pProc
)
{
return
pProc
->
isChild
;
}
int32_t
taosProcChildId
(
SProcObj
*
pProc
)
{
return
pProc
->
pid
;
}
void
taosProcCleanup
(
SProcObj
*
pProc
)
{
void
taosProcCleanup
(
SProcObj
*
pProc
)
{
if
(
pProc
!=
NULL
)
{
if
(
pProc
!=
NULL
)
{
uDebug
(
"proc:%s, clean up"
,
pProc
->
name
);
uDebug
(
"proc:%s, clean up"
,
pProc
->
name
);
taosProcStop
(
pProc
);
taosProcStop
(
pProc
);
taosProc
QueueCleanup
(
pProc
->
pChildQueue
);
taosProc
CleanupQueue
(
pProc
->
pChildQueue
);
taosProc
QueueCleanup
(
pProc
->
pParentQueue
);
taosProc
CleanupQueue
(
pProc
->
pParentQueue
);
taosMemoryFree
(
pProc
);
taosMemoryFree
(
pProc
);
}
}
}
}
...
...
tests/test/c/tmqDemo.c
浏览文件 @
10319e3c
...
@@ -661,12 +661,13 @@ int main(int32_t argc, char *argv[]) {
...
@@ -661,12 +661,13 @@ int main(int32_t argc, char *argv[]) {
float
rowsSpeed
=
totalRows
/
seconds
;
float
rowsSpeed
=
totalRows
/
seconds
;
float
msgsSpeed
=
totalMsgs
/
seconds
;
float
msgsSpeed
=
totalMsgs
/
seconds
;
walLogSize
=
getDirectorySize
(
g_stConfInfo
.
vnodeWalPath
);
if
(
walLogSize
<=
0
)
{
if
(
0
==
g_stConfInfo
.
simCase
)
{
printf
(
"vnode2/wal size incorrect!
\n
"
);
walLogSize
=
getDirectorySize
(
g_stConfInfo
.
vnodeWalPath
);
/*exit(-1);*/
if
(
walLogSize
<=
0
)
{
}
else
{
printf
(
"%s size incorrect!"
,
g_stConfInfo
.
vnodeWalPath
);
if
(
0
==
g_stConfInfo
.
simCase
)
{
exit
(
-
1
);
}
else
{
pPrint
(
".log file size in vnode2/wal: %.3f MBytes
\n
"
,
(
double
)
walLogSize
/
(
1024
*
1024
.
0
));
pPrint
(
".log file size in vnode2/wal: %.3f MBytes
\n
"
,
(
double
)
walLogSize
/
(
1024
*
1024
.
0
));
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录