提交 dcc23699 编写于 作者: S senllang

partition attributes storage model PAX change to

上级 082a5cf9
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
# 更新记录
## updated by 2023/6/17
## updated by 2023/8/17
- 物理存储模式的改变
> * 原来传统的行存储模式,也叫NSM(N-Arr storage model),每一行的数据是连续存放在一起;
> * 改变后的存储模式为行列混合存放模式 PAX(partition attributes across), 这结合了DSM(Decomposition storage model ) 和 NSM的优势;
- 对四条SQL语句的适配
> * 创建表时,不仅创建表数据文件,还要创建分组管理文件,这两者是一一对应的
> * 在插入数据时,按分组查找空闲空间,如果没有时,先新建分组,再插入数据;
> * 查询时,按分组来扫描数据;当前只支持 select * 配匹;如果按列时,那么只从磁盘加载对应列的数据即可,这就是PAX模式的优势;
> * 删除表时,当然也是需要成对删除表数据文件和分组文件
## updated by 2023/6/17
- 支持四条SQL语句的执行,分别是
> create table tablename(columnname type, ...);
> drop table tablename;
> drop table tablename;
> insert into tablename(columnname,...) values(val...);
> select columnname,... from tablename;
> select columnname,... from tablename;
当前查找支持全部列表,不支持部分列名的过滤。
......@@ -85,10 +94,10 @@ return 1 rows
```
* 插入多行数据
`
```
toadb> insert into student(sid,sname,ssex) values(2,'hanmeimei','F');
toadb> insert into student(sid,sname,ssex) values(3,'richel','F');
`
```
* 查询多行数据
```
......
......@@ -16,12 +16,16 @@ TARGET = toadb-${VERSION_H}-${VERSION_L}
topdir = .
SRC_DIR=$(topdir)
# filter-out source code directory
OUT_SRC_DIR = $(topdir)/tools
OUT_DIR = $(shell find $(OUT_SRC_DIR) -maxdepth 5 -type d)
DIR_DISP = $(foreach dir, $(OUT_DIR), $(wildcard $(dir)/*.c))
# search all directory and subdirectory
DIRS = $(shell find $(SRC_DIR) -maxdepth 5 -type d)
# general source file list , and objects list
SOURCE = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
SOURCE = $(filter-out $(DIR_DISP), $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c)))
OBJS = ${patsubst %.c, %.o, $(SOURCE)}
# .h files take place all directories.
......@@ -39,8 +43,8 @@ builddir = $(topdir)/build
##########################################
##########################################
DEFINES += -DTEST_PRO
LDFLAGS += -lpthread
DEFINES += -DSTORAGE_FORMAT_PAX -DMEM_DEBUG
# LDFLAGS += -lpthread
DEBUG_FLAGS = -g
##########################################
......@@ -70,10 +74,11 @@ $(TARGET):$(topdir)/*.o
# show various value
default:
echo -e $(DIRS)
echo -e $(SOURCE)
echo -e $(OBJS)
echo -e $(INC)
@echo -e "DIRS "$(DIRS)
@echo -e "SOURCE "$(SOURCE)
@echo -e "OBJS "$(OBJS)
@echo -e "INC "$(INC)
@echo -e "DIR_DISP "$(DIR_DISP)
parser: $(PARSER) $(SCANNER)
$(PARSER): ${PARSER_DIR}/grammar.y
......
/*
* toadb table modify executor
* Copyright (C) 2023-2023, senllang
*/
#include "execModifyTable.h"
#include "tfile.h"
#include "tables.h"
#include "node.h"
#include "buffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define log printf
/*
* routine of modify table operators
*/
int ExecModifyTable(PTableList tblInfo, PTableRowData insertdata, NodeType type)
{
int ret = 0;
int tabletype = GET_STORAGE_TYPE_SHIFT(tblInfo->tableInfo->header.pageType);
switch(tabletype)
{
case ST_NSM:
ret = nsm_ExecModifyTable(tblInfo, insertdata, type);
break;
case ST_PAX:
ret = pax_ExecModifyTable(tblInfo, insertdata, type);
break;
default:
log("unknow table storage type %d. \n", tabletype);
ret = -1;
break;
}
return ret;
}
/*
* routine of modify table operators , under NSM storage
*/
int nsm_ExecModifyTable(PTableList tblInfo, PTableRowData insertdata, NodeType type)
{
int ret = 0;
switch(type)
{
case T_InsertStmt:
ret = nsm_ExecInsert(tblInfo, insertdata);
break;
default:
log("unsuport node type %d. \n", type);
ret = -1;
break;
}
return ret;
}
int nsm_ExecInsert(PTableList tblInfo, PTableRowData insertdata)
{
int ret = 0;
PPageDataHeader pageInsert = NULL;
/* there we find free space. */
pageInsert = GetSpacePage(tblInfo, insertdata->size, PAGE_NEW, MAIN_FORK);
/* row data will be writed through to the table file. */
ret = WriteRowData(tblInfo, pageInsert, insertdata);
if(ret != 0)
{
log("write row to page failure.[%d]\n", ret);
}
return ret;
}
/*
* routine of modify table operators , under PAX storage
*/
int pax_ExecModifyTable(PTableList tblInfo, PTableRowData insertdata, NodeType type)
{
int ret = 0;
switch(type)
{
case T_InsertStmt:
ret = pax_ExecInsert(tblInfo, insertdata);
break;
default:
log("unsuport node type %d of PAX storage . \n", type);
ret = -1;
break;
}
return ret;
}
int pax_ExecInsert(PTableList tblInfo, PTableRowData insertdata)
{
int ret = 0;
PPageDataHeader *pageList = NULL;
PTableRowData colRows = NULL;
PTableRowData currRows = NULL;
PPageDataHeader page = NULL;
int ColNum = 0;
int itemsize = sizeof(TableRowData) + sizeof(PRowColumnData);
/*
* find free space
* several pages will insert columns, and these are in one group.
*/
pageList = (PPageDataHeader *)AllocMem(sizeof(PPageDataHeader)*insertdata->num);
/* find enough free space group page */
do
{
ret = GetSpaceGroupPage(tblInfo, insertdata, PAGE_NEW, pageList);
if(ret < 0)
{
/* current groups has not enough free space, we extension a new group. */
page = ExtensionTbl(tblInfo, insertdata->num, MAIN_FORK);
/* update group file, insert one group data */
InsertGroupItem(tblInfo, page, insertdata->num);
FreeMem(page);
page = NULL;
}
} while (ret < 0);
/* Now, to form rows column by column. */
colRows = FormColRowsData(insertdata);
/* insert into pages */
for(ColNum = 0; ColNum < insertdata->num; ColNum++)
{
currRows = (PTableRowData)((char *) colRows + ColNum * itemsize);
ret = WriteRowItemData(tblInfo, pageList[ColNum], currRows);
}
/* resource release */
ReleasePageList(pageList, insertdata->num);
return 1;
}
/*
* toadb table modify executor
* Copyright (C) 2023-2023, senllang
*/
#ifndef HAT_EXEC_MODIFY_TABLE_H_H
#define HAT_EXEC_MODIFY_TABLE_H_H
#include "node.h"
#include "buffer.h"
/* routing Modify table operator */
int ExecModifyTable(PTableList tblInfo, PTableRowData insertdata, NodeType type);
/* NSM storage operator */
int nsm_ExecModifyTable(PTableList tblInfo, PTableRowData insertdata, NodeType type);
int nsm_ExecInsert(PTableList tblInfo, PTableRowData insertdata);
/* PAX storage operators */
int pax_ExecModifyTable(PTableList tblInfo, PTableRowData insertdata, NodeType type);
int pax_ExecInsert(PTableList tblInfo, PTableRowData insertdata);
#endif
......@@ -7,52 +7,61 @@
#include "exectable.h"
#include "executor.h"
#include "tables.h"
#include "portal.h"
#include "seqscan.h"
#include "tfile.h"
#include "execModifyTable.h"
/* printf snprintf */
#include <stdio.h>
#include <string.h>
/* file operator */
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#define log printf
extern char *DataDir;
static int CreateTableFile(char *filename, int mode);
static int DeleteTableFile(char *filename);
/*
* executor of table create.
* list is a tree of parser.
*/
int ExecCreateTable(PCreateStmt stmt)
int ExecCreateTable(PCreateStmt stmt, PPortal portal)
{
PColumnDef column = NULL;
PListCell tmpCell = NULL;
char pagebuffer[PAGE_MAX_SIZE] = {0};
PPageDataHeader pageheader = (PPageDataHeader)pagebuffer;
PTableMetaInfo tableinfo = (PTableMetaInfo) (pagebuffer + sizeof(PageDataHeader));
PTableMetaInfo tableinfo = (PTableMetaInfo) (pagebuffer + PAGE_DATA_HEADER_SIZE);
int i = 0;
int tablefile = -1;
TableList tbl_temp = {0};
int ret = 0;
int tableType = 0;
if(NULL == stmt)
{
log("create table stmt is NULL\n");
return -1;
}
#ifdef STORAGE_FORMAT_NSM
tableType = ST_NSM << STORAGE_TYPE_SHIFT;
#endif
#ifdef STORAGE_FORMAT_PAX
tableType = ST_PAX << STORAGE_TYPE_SHIFT;
#endif
/* initialize table infomastion to scan list */
pageheader->header.pageVersion = PAGE_VERSION;
pageheader->header.pageType = PAGE_HEADER;
pageheader->header.pageNum = 0x01;
pageheader->header.pageType = PAGE_HEADER | tableType;
pageheader->header.pageNum = PAGE_HEAD_PAGE_NUM;
pageheader->pageCnt = PAGE_HEAD_PAGE_NUM;
snprintf(tableinfo->tableName, NAME_MAX_LEN, "%s", stmt->tableName);
tableinfo->tableType = tableType;
tableinfo->tableId = GetObjectId();
/* column info */
for(tmpCell = stmt->ColList->head; tmpCell != NULL; tmpCell = tmpCell->next)
{
......@@ -74,6 +83,8 @@ int ExecCreateTable(PCreateStmt stmt)
i++;
}
tableinfo->colNum = i;
pageheader->dataOffset = PAGE_DATA_HEADER_SIZE;
pageheader->dataEndOffset = sizeof(TableMetaInfo) + sizeof(ColumnDefInfo) * i;
/* create table file */
tablefile = CreateTableFile(tableinfo->tableName, 0666);
......@@ -84,42 +95,32 @@ int ExecCreateTable(PCreateStmt stmt)
}
/* initialize table file */
write(tablefile, pagebuffer, PAGE_MAX_SIZE);
/* end of executor, close and release resources */
close(tablefile);
return 0;
}
static int CreateTableFile(char *filename, int mode)
{
int fd;
char filepath[1024];
snprintf(filepath, 1024, "%s/%s", DataDir, filename);
// 检查文件是否存在
if (access(filepath, F_OK) == 0)
tbl_temp.tbl_fd = tablefile;
ret = FlushBuffer(&tbl_temp, pagebuffer);
if(ret < 0)
{
log("table file %s already exist. \n", filepath);
log("exec create %s table failure,errno[%d].\n", tableinfo->tableName, ret);
return -1;
}
// 以二进制形式打开文件
fd = open(filepath, O_RDWR | O_CREAT, mode);
if (fd == -1)
smgrClose(tbl_temp.tbl_fd);
/* init group file and open */
ret = TableCreate(tableinfo->tableName, GROUP_FORK);
if (ret < 0)
{
log("create file %s error, maybe space not enough\n", filepath);
return -1;
log("create %s group file failure.\n", tableinfo->tableName);
}
return fd;
return ret;
}
int ExecDropTable(PDropStmt stmt)
int ExecDropTable(PDropStmt stmt, PPortal portal)
{
int ret = 0;
PTableList tblInfo = NULL;
if(NULL == stmt)
{
log("drop table stmt is NULL\n");
......@@ -127,71 +128,24 @@ int ExecDropTable(PDropStmt stmt)
}
/* find file */
/* delete file */
ret = DeleteTableFile(stmt->tableName);
if(0 != ret)
{
log("exec drop %s table failure.\n", stmt->tableName);
return -1;
}
return ret;
}
static int DeleteTableFile(char *filename)
{
int ret = 0;
char filepath[1024];
snprintf(filepath, 1024, "%s/%s", DataDir, filename);
// 检查文件是否存在
if (access(filepath, F_OK) != 0)
{
log("table file %s is not exist. \n", filepath);
return -1;
}
// 以二进制形式打开文件
ret = unlink(filepath);
if (ret != 0)
tblInfo = GetTableInfo(stmt->tableName);
if(NULL == tblInfo)
{
log("create file %s ,errno %d \n", filepath, errno);
log("drop table %s failure, It's not exist.\n",stmt->tableName);
return -1;
}
/* delete file */
ret = TableDrop(tblInfo);
ReleaseTblInfo(tblInfo);
return ret;
}
int OpenTableFile(char *filename, int mode)
{
int fd;
char filepath[1024];
snprintf(filepath, 1024, "%s/%s", DataDir, filename);
// 检查文件是否存在
if (access(filepath, F_OK) != 0)
{
log("table file %s is not exist. \n", filepath);
return -1;
}
// 以二进制形式打开文件
fd = open(filepath, O_RDWR, mode);
if (fd == -1)
{
log("open file %s error, errno[%d]\n", filepath, errno);
return -1;
}
return fd;
}
int ExecInsertStmt(PInsertStmt stmt)
int ExecInsertStmt(PInsertStmt stmt, PPortal portal)
{
PTableList tblInfo = NULL;
PTableRowData rowDataInsert = NULL;
PPageDataHeader pageInsert = NULL;
int ret = 0;
if(NULL == stmt)
......@@ -207,22 +161,15 @@ int ExecInsertStmt(PInsertStmt stmt)
log("insert table failure.\n");
return -1;
}
/* TODO check attrlist and values number is equal. */
/* form row data */
rowDataInsert = FormRowData(tblInfo->tableDef, stmt);
/* there we find free space. */
pageInsert = GetSpacePage(tblInfo, rowDataInsert->size, PAGE_NEW);
/* row data will be writed through to the table file. */
ret = WriteRowData(tblInfo, pageInsert, rowDataInsert);
if(ret != 0)
{
log("write row to page failure.[%d]\n", ret);
}
ret = ExecModifyTable(tblInfo, rowDataInsert, T_InsertStmt);
FreeMem(rowDataInsert);
return ret;
}
......@@ -320,50 +267,86 @@ int ClientFormRow(PScan scanHead, PSelectStmt stmt)
case INT:
case INTEGER:
{
int *tmp = NULL;
char digit[128];
int size = 0;
if(rawRow->columnData[attrIndex] != NULL)
{
tmp = (int *)(rawRow->columnData[attrIndex]->data);
snprintf(digit, 128, "%d", *tmp);
size = strlen(digit);
}
if(showPhare)
{
int *tmp = (int *)(rawRow->columnData[attrIndex]->data);
snprintf(pbuf + bufOffset, PORT_BUFFER_SIZE - bufOffset, "|%*d", rowMaxSize[attrIndex], *tmp);
bufOffset += rowMaxSize[attrIndex] + 1;
}
else
rowMaxSize[attrIndex] = sizeof(int);
{
if(size > rowMaxSize[attrIndex])
rowMaxSize[attrIndex] = size + 1;
}
}
break;
case VARCHAR:
{
int size = 0;
char *data = NULL;
if (rawRow->columnData[attrIndex] != NULL)
{
data = rawRow->columnData[attrIndex]->data;
size = rawRow->columnData[attrIndex]->size;
}
if(showPhare)
{
snprintf(pbuf + bufOffset, PORT_BUFFER_SIZE - bufOffset, "|%*s", rowMaxSize[attrIndex], rawRow->columnData[attrIndex]->data);
snprintf(pbuf + bufOffset, PORT_BUFFER_SIZE - bufOffset, "|%*s", rowMaxSize[attrIndex], data);
bufOffset += rowMaxSize[attrIndex] + 1;
}
else
{
if(strlen(rawRow->columnData[attrIndex]->data) > rowMaxSize[attrIndex])
rowMaxSize[attrIndex] = strlen(rawRow->columnData[attrIndex]->data);
if(size > rowMaxSize[attrIndex])
rowMaxSize[attrIndex] = size;
}
}
break;
case CHAR:
{
char data = '\0';
if (rawRow->columnData[attrIndex] != NULL)
{
data = rawRow->columnData[attrIndex]->data[0];
}
if(showPhare)
{
snprintf(pbuf + bufOffset, PORT_BUFFER_SIZE - bufOffset, "|%*c", rowMaxSize[attrIndex],rawRow->columnData[attrIndex]->data[0]);
snprintf(pbuf + bufOffset, PORT_BUFFER_SIZE - bufOffset, "|%*c", rowMaxSize[attrIndex],data);
// fillBack(pbuf+bufOffset+2, ' ', rowMaxSize[attrIndex] - 1);
bufOffset += rowMaxSize[attrIndex] + 1;
}
else
rowMaxSize[attrIndex] = sizeof(char);
}
break;
case BOOL:
{
char data = '\0';
if (rawRow->columnData[attrIndex] != NULL)
{
data = rawRow->columnData[attrIndex]->data[0];
}
if(showPhare)
{
snprintf(pbuf + bufOffset, PORT_BUFFER_SIZE - bufOffset, "|%*c", rowMaxSize[attrIndex],rawRow->columnData[attrIndex]->data[0]);
snprintf(pbuf + bufOffset, PORT_BUFFER_SIZE - bufOffset, "|%*c", rowMaxSize[attrIndex],data);
// fillBack(pbuf+bufOffset+2, ' ', rowMaxSize[attrIndex] - 1);
bufOffset += rowMaxSize[attrIndex] + 1;
}
else
rowMaxSize[attrIndex] = sizeof(char);
}
break;
default:
break;
......@@ -381,7 +364,7 @@ int ClientFormRow(PScan scanHead, PSelectStmt stmt)
showPhare++;
}while(showPhare < SHOW_PHARE_MAX);
snprintf(pbuf, PORT_BUFFER_SIZE , "return %d rows", showNum);
snprintf(pbuf, PORT_BUFFER_SIZE , "total %d rows", showNum);
SendToPortal(scanHead->portal);
if(NULL != rowMaxSize)
......@@ -397,12 +380,10 @@ int ClientFormRow(PScan scanHead, PSelectStmt stmt)
/*
* select 执行入口
*/
int ExecSelectStmt(PSelectStmt stmt)
int ExecSelectStmt(PSelectStmt stmt, PPortal portal)
{
PListCell tmpCell = NULL;
Scan scanState;
PPortal portal = NULL;
int num = 0;
if(NULL == stmt)
......@@ -420,7 +401,7 @@ int ExecSelectStmt(PSelectStmt stmt)
memset(&scanState, 0x00, sizeof(Scan));
/* create portal, which will store all rows. */
portal = CreatePortal(stmt);
InitSelectPortal(stmt, portal);
scanState.portal = portal;
/* we will scan all tables, from table head to table tail. */
......@@ -434,8 +415,5 @@ int ExecSelectStmt(PSelectStmt stmt)
/* form temp row data , which will be shown on client. */
ClientFormRow(&scanState, stmt);
// EndPort(portal);
return 0;
}
......@@ -8,6 +8,7 @@
#include "node.h"
#include "buffer.h"
#include "list.h"
#include "portal.h"
#define MAX_ROW_DATA 1024
......@@ -27,14 +28,15 @@ typedef struct ColumnSelectList
int ExecCreateTable(PCreateStmt stmt);
int ExecCreateTable(PCreateStmt stmt, PPortal portal);
int ExecDropTable(PDropStmt stmt);
int ExecDropTable(PDropStmt stmt, PPortal portal);
int ExecInsertStmt(PInsertStmt stmt);
int ExecInsertStmt(PInsertStmt stmt, PPortal portal);
int ExecSelectStmt(PSelectStmt stmt);
int ExecSelectStmt(PSelectStmt stmt, PPortal portal);
int OpenTableFile(char *filename, int mode);
#endif
\ No newline at end of file
......@@ -17,6 +17,9 @@
static void ExecutorPlan(PList list)
{
PListCell tmpCell = NULL;
PPortal portal = NULL;
char *pbuf = NULL;
int ret = 0;
if(NULL == list)
{
......@@ -24,6 +27,9 @@ static void ExecutorPlan(PList list)
return;
}
portal = CreatePortal();
pbuf = portal->buffer;
/* list cell node show */
for(tmpCell = list->head; tmpCell != NULL; tmpCell = tmpCell->next)
{
......@@ -37,21 +43,24 @@ static void ExecutorPlan(PList list)
{
PCreateStmt createstmt = (PCreateStmt)node;
log("exec T_CreateStmt Node: tablename:%s \n", createstmt->tableName);
ExecCreateTable(createstmt);
ret = ExecCreateTable(createstmt, portal);
snprintf(pbuf, PORT_BUFFER_SIZE, "Create table result %d", ret);
}
break;
case T_DropStmt:
{
PDropStmt dropstmt = (PDropStmt)node;
log("exec T_DropStmt Node: drop table :%s \n", dropstmt->tableName);
ExecDropTable(dropstmt);
ret = ExecDropTable(dropstmt, portal);
snprintf(pbuf, PORT_BUFFER_SIZE, "Drop table result %d", ret);
}
break;
case T_InsertStmt:
{
PInsertStmt insertstmt = (PInsertStmt)node;
log("T_InsertStmt Node: table :%s \n", insertstmt->tableName);
ExecInsertStmt(insertstmt);
ret = ExecInsertStmt(insertstmt, portal);
snprintf(pbuf, PORT_BUFFER_SIZE, "Insert into %d rows", ret);
}
break;
......@@ -60,15 +69,17 @@ static void ExecutorPlan(PList list)
PSelectStmt stmt = (PSelectStmt)node;
log("T_SelectStmt Node: \n");
ExecSelectStmt(stmt);
ret = ExecSelectStmt(stmt, portal);
}
break;
default:
break;
}
SendToPortal(portal);
}
EndPort(portal);
return;
}
......
文件模式从 100644 更改为 100755
......@@ -4,7 +4,7 @@
*/
#include "seqscan.h"
#include "tfile.h"
#include <stdio.h>
#include <string.h>
......@@ -14,6 +14,7 @@
* there, we read one row from table,
* from tblScan marked offset and index of page.
* rowdata is store into tblScan.
* NOTE: only N-Atrr storage mode page
*/
PTableRowData SeqScanRawRow(PTableList tbl, PScanState tblScan)
{
......@@ -27,14 +28,17 @@ PTableRowData SeqScanRawRow(PTableList tbl, PScanState tblScan)
return NULL;
}
/* maybe scan the end of table. */
/*
* maybe scan the end of table.
* at first time , pageIndex = 0;
*/
if(tblScan->pageIndex < 0)
return NULL;
/* skip first page */
if(tblScan->pageIndex == 0)
{
tblScan->pageIndex = PAGE_HEAD_PAGE_NUM+1;
tblScan->pageIndex = PAGE_HEAD_PAGE_NUM + 1;
tblScan->page = NULL;
tblScan->tblInfo = tbl;
}
......@@ -43,7 +47,7 @@ PTableRowData SeqScanRawRow(PTableList tbl, PScanState tblScan)
{
if(NULL == tblScan->page)
{
page = GetPageByIndex(tbl, tblScan->pageIndex);
page = GetPageByIndex(tbl, tblScan->pageIndex, MAIN_FORK);
if(NULL == page)
{
/* scan end of this table. */
......@@ -57,17 +61,15 @@ PTableRowData SeqScanRawRow(PTableList tbl, PScanState tblScan)
page = tblScan->page;
/* search rowdata untile end of file */
if(tblScan->pageOffset >= page->dataEndOffset)
{
tblScan->pageIndex++;
FreeMem(tblScan->page);
tblScan->page = NULL;
}
else
if(tblScan->pageOffset < page->dataEndOffset)
{
/* page find. */
break;
}
tblScan->pageIndex++;
FreeMem(tblScan->page);
tblScan->page = NULL;
} while (1);
/* second get row data */
......@@ -81,30 +83,131 @@ PTableRowData SeqScanRawRow(PTableList tbl, PScanState tblScan)
return rawrow;
}
PTableRowData SeqScanRawRowForPages(PTableList tbl, PScanState tblScan)
{
PTableRowData *rawcolrow = NULL;
PTableRowData rawrow = NULL;
int index = 0;
if(NULL == tbl || tblScan == NULL)
{
log("ScanTableForRawRow argments is invalid\n");
return NULL;
}
rawcolrow = (PTableRowData *)AllocMem(sizeof(PTableRowData) * tblScan->scanPageInfo.pageListNum);
/* second get row data */
for(index = 0; index < tblScan->scanPageInfo.pageListNum; index++)
{
tblScan->scanPageInfo.searchPageList[index].page = tblScan->scanPageInfo.pageList[index];
rawcolrow[index] = GetRowDataFromPage(tbl, &(tblScan->scanPageInfo.searchPageList[index]));
if(NULL == rawcolrow[index])
{
break;
}
}
if(index != tblScan->scanPageInfo.pageListNum)
{
rawrow = NULL;
goto END;
}
/* using colrow form one rawrow. */
if(tblScan->scanPageInfo.pageListNum > 1)
{
rawrow = FormCol2RowData(rawcolrow, tblScan->scanPageInfo.pageListNum);
}
else
{
rawrow = rawcolrow[0];
goto ENDONE;
}
END:
/* error ocur */
for(index = 0; index < tblScan->scanPageInfo.pageListNum; index++)
{
if(NULL != rawcolrow[index])
FreeMem(rawcolrow[index]);
}
ENDONE:
FreeMem(rawcolrow);
return rawrow;
}
int ScanTable(PTableList tbl, PScanState tblScan)
{
int num =0;
int num =0, grouprownum = 0, groupcount = 0;;
int pageNum = 0;
PTableRowData rawRow = NULL;
PDLCell cell = NULL;
if(NULL == tbl || tblScan == NULL)
PGroupItemData groupItem = NULL;
PSearchPageInfo groupSearchPage;
PPageDataHeader *pagelist = NULL;
if(NULL == tbl || NULL == tblScan)
{
log("ScanTable argments is invalid\n");
return -1;
}
groupSearchPage = &(tblScan->scanPageInfo.groupPageInfo);
memset(groupSearchPage, 0x00, sizeof(SearchPageInfo));
/*
* table group search item per page, if NULL when the end of file.
*/
do
{
rawRow = SeqScanRawRow(tbl, tblScan);
/* find one group, until return NULL when reach the end of group file. */
if(NULL == groupItem)
{
groupItem = GetGroupInfo(tbl, groupSearchPage);
if(NULL == groupItem)
break;
grouprownum = 0;
groupcount ++;
/*
* page list of per group, every attr will has multiple pages.
*/
pagelist = GetGroupMemberPages(tbl, groupItem, &pageNum);
if(NULL == pagelist)
break;
/* record search page list */
tblScan->scanPageInfo.pageList = pagelist;
tblScan->scanPageInfo.pageListNum = pageNum;
tblScan->scanPageInfo.searchPageList = (PSearchPageInfo)AllocMem(pageNum * sizeof(SearchPageInfo));
}
/* search row from group pages. and all attributes form one row. */
rawRow = SeqScanRawRowForPages(tbl, tblScan);
if(NULL == rawRow)
{
break;
ReleasePageList(pagelist, pageNum);
pagelist = NULL;
FreeMem(tblScan->scanPageInfo.searchPageList);
tblScan->scanPageInfo.searchPageList = NULL;
FreeMem(groupItem);
groupItem = NULL;
continue;
}
/* add to tblScan */
AddCellToListTail(&(tblScan->rows), rawRow);
num ++;
} while (1);
grouprownum++;
}while(1); /* until this table end. */
if(NULL != groupItem)
{
FreeMem(groupItem);
}
tblScan->rowNum = num;
return num;
......@@ -117,8 +220,8 @@ int ScanTable(PTableList tbl, PScanState tblScan)
int ScanOneTblRows(char *tblName, PScan scan)
{
PTableList tblInfo = NULL;
int ret = 0;
PScanState scanTbl = NULL;
int ret = 0;
/* get table information */
tblInfo = GetTableInfo(tblName);
......@@ -135,6 +238,10 @@ int ScanOneTblRows(char *tblName, PScan scan)
return ret;
}
/*
* Initialize scanState ,which is alive untile scan end.
* add this table to the scan table list.
*/
PScanState AddScanStateNode(PTableList tblInfo, PScan scan)
{
PScanState scanState = NULL;
......@@ -147,6 +254,9 @@ PScanState AddScanStateNode(PTableList tblInfo, PScan scan)
AddCellToListTail(((PDList*)&(scan->list)), (void *)scanState);
memset(scanState, 0x00, sizeof(ScanState));
scanState->tblInfo = tblInfo;
scanState->columnNum = tblInfo->tableDef->colNum;
return scanState;
}
......
......@@ -11,20 +11,35 @@
#include "buffer.h"
#include "portal.h"
typedef struct ScanPageInfo
{
SearchPageInfo groupPageInfo;
PSearchPageInfo searchPageList;
PPageDataHeader *pageList;
int pageListNum;
}ScanPageInfo, *PScanPageInfo;
/* per table record the scan state before the table end. */
typedef struct ScanState
{
PTableList tblInfo;
int pageIndex; /* page index of table file */
int pageOffset; /* offset of page */
int columnNum; /* query column of current table */
int rowNum;
PDList rows;
/////old below
int pageIndex; /* page index of table file */
int pageOffset; /* offset of page */
PPageDataHeader page;
/////old above
ScanPageInfo scanPageInfo;
PColumnDef columnDefArr;
}ScanState, *PScanState;
/*
* record per table raw rowdata
*/
......@@ -43,4 +58,7 @@ int ScanOneTblRows(char *tblName, PScan scanState);
PScanState AddScanStateNode(PTableList tblInfo, PScan scan);
/* PAX mode, search group member pages */
PTableRowData SeqScanRawRowForPages(PTableList tbl, PScanState tblScan);
#endif
\ No newline at end of file
......@@ -7,14 +7,11 @@
#include <string.h>
#include "toadmain.h"
char *DataDir = "./toadbtest";
int main(int argc, char *argv[])
{
printf("Welcome to Toad Database Manage System.\n") ;
if(argc > 1)
DataDir = strdup(argv[1]);
toadbMain(argc, argv);
return 0;
}
此差异已折叠。
......@@ -55,9 +55,12 @@ extern int yydebug;
INTO = 265,
VALUES = 266,
UPDATE = 267,
IDENT = 268,
STRING = 269,
DIGEST = 270
WHERE = 268,
AND = 269,
OR = 270,
IDENT = 271,
STRING = 272,
DIGEST = 273
};
#endif
......@@ -74,7 +77,7 @@ union YYSTYPE
PList list;
PNode node;
#line 78 "./parser/grammar.h" /* yacc.c:1909 */
#line 81 "./parser/grammar.h" /* yacc.c:1909 */
};
typedef union YYSTYPE YYSTYPE;
......
......@@ -41,6 +41,9 @@
%token INTO
%token VALUES
%token UPDATE
%token WHERE
%token AND
%token OR
%token <sval> IDENT
%token <sval> STRING
%token <ival> DIGEST
......
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
......@@ -370,8 +370,8 @@ static void yynoreturn yy_fatal_error (yyconst char* msg ,yyscan_t yyscanner );
*yy_cp = '\0'; \
yyg->yy_c_buf_p = yy_cp;
#define YY_NUM_RULES 16
#define YY_END_OF_BUFFER 17
#define YY_NUM_RULES 19
#define YY_END_OF_BUFFER 20
/* This struct is not used in this scanner,
but its presence is necessary. */
struct yy_trans_info
......@@ -379,15 +379,16 @@ struct yy_trans_info
flex_int32_t yy_verify;
flex_int32_t yy_nxt;
};
static yyconst flex_int16_t yy_accept[70] =
static yyconst flex_int16_t yy_accept[80] =
{ 0,
0, 0, 17, 16, 15, 14, 16, 14, 12, 13,
13, 13, 13, 13, 13, 13, 13, 13, 15, 11,
0, 15, 12, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 11, 15, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 10, 2, 13,
7, 13, 13, 13, 13, 13, 13, 13, 13, 4,
13, 13, 3, 9, 6, 1, 5, 8, 0
0, 0, 20, 19, 18, 17, 19, 17, 15, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 18, 14, 0, 18, 15, 16, 16, 16, 16,
16, 16, 16, 13, 16, 16, 16, 16, 16, 14,
18, 12, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 10, 2, 16, 7, 16,
16, 16, 16, 16, 16, 16, 16, 16, 4, 16,
16, 11, 3, 9, 6, 1, 5, 8, 0
} ;
static yyconst YY_CHAR yy_ec[256] =
......@@ -395,17 +396,17 @@ static yyconst YY_CHAR yy_ec[256] =
1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
1, 2, 3, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 4, 1, 1, 1, 4, 4, 5, 4,
1, 2, 1, 1, 1, 1, 4, 1, 5, 4,
4, 4, 4, 4, 6, 4, 4, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 1, 4, 1,
1, 1, 1, 1, 8, 9, 10, 11, 12, 13,
14, 14, 15, 14, 14, 16, 17, 18, 19, 20,
14, 21, 22, 23, 24, 25, 14, 14, 14, 14,
1, 1, 1, 4, 26, 1, 27, 28, 29, 30,
31, 32, 14, 14, 33, 14, 14, 34, 35, 36,
37, 38, 14, 39, 40, 41, 42, 43, 14, 14,
14, 14, 1, 4, 1, 4, 1, 1, 1, 1,
7, 7, 7, 7, 7, 7, 7, 4, 4, 4,
4, 4, 1, 1, 8, 9, 10, 11, 12, 13,
14, 15, 16, 14, 14, 17, 18, 19, 20, 21,
14, 22, 23, 24, 25, 26, 27, 14, 14, 14,
4, 1, 4, 4, 28, 1, 29, 30, 31, 32,
33, 34, 14, 35, 36, 14, 14, 37, 38, 39,
40, 41, 14, 42, 43, 44, 45, 46, 47, 14,
14, 14, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
......@@ -422,95 +423,104 @@ static yyconst YY_CHAR yy_ec[256] =
1, 1, 1, 1, 1
} ;
static yyconst YY_CHAR yy_meta[44] =
static yyconst YY_CHAR yy_meta[48] =
{ 0,
1, 1, 2, 1, 3, 1, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4
4, 4, 4, 4, 4, 4, 4
} ;
static yyconst flex_uint16_t yy_base[73] =
static yyconst flex_uint16_t yy_base[83] =
{ 0,
0, 0, 134, 156, 42, 156, 125, 40, 117, 0,
27, 37, 30, 34, 41, 46, 36, 51, 58, 88,
83, 0, 56, 0, 50, 41, 45, 46, 57, 51,
62, 66, 68, 50, 0, 78, 75, 69, 74, 80,
75, 83, 83, 92, 79, 81, 85, 0, 0, 89,
0, 91, 101, 92, 104, 106, 111, 102, 104, 0,
117, 109, 0, 0, 0, 0, 0, 0, 156, 147,
46, 151
0, 0, 148, 174, 46, 174, 139, 44, 132, 33,
0, 31, 42, 34, 38, 36, 47, 52, 41, 57,
48, 64, 106, 98, 0, 63, 0, 57, 57, 54,
54, 59, 64, 0, 68, 83, 82, 78, 84, 56,
0, 0, 89, 86, 79, 83, 90, 84, 94, 92,
102, 87, 94, 98, 101, 0, 0, 104, 0, 118,
118, 109, 122, 123, 125, 126, 116, 117, 0, 131,
124, 0, 0, 0, 0, 0, 0, 0, 174, 165,
51, 169
} ;
static yyconst flex_int16_t yy_def[73] =
static yyconst flex_int16_t yy_def[83] =
{ 0,
69, 1, 69, 69, 69, 69, 70, 69, 69, 71,
71, 71, 71, 71, 71, 71, 71, 71, 69, 70,
70, 72, 69, 71, 71, 71, 71, 71, 71, 71,
71, 71, 71, 69, 72, 71, 71, 71, 71, 71,
71, 71, 71, 71, 71, 71, 71, 71, 71, 71,
71, 71, 71, 71, 71, 71, 71, 71, 71, 71,
71, 71, 71, 71, 71, 71, 71, 71, 0, 69,
69, 69
79, 1, 79, 79, 79, 79, 80, 79, 79, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 79, 80, 80, 82, 79, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 79,
82, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 81, 81,
81, 81, 81, 81, 81, 81, 81, 81, 0, 79,
79, 79
} ;
static yyconst flex_uint16_t yy_nxt[200] =
static yyconst flex_uint16_t yy_nxt[222] =
{ 0,
4, 5, 5, 6, 7, 8, 9, 10, 10, 11,
12, 10, 13, 10, 14, 10, 10, 10, 10, 10,
10, 15, 16, 17, 18, 4, 10, 10, 11, 12,
10, 13, 14, 10, 10, 10, 10, 10, 10, 15,
16, 17, 18, 19, 19, 22, 23, 25, 26, 24,
28, 29, 30, 31, 34, 32, 37, 27, 33, 19,
19, 36, 23, 38, 39, 25, 42, 26, 28, 29,
43, 30, 31, 32, 37, 27, 44, 33, 40, 41,
36, 38, 39, 45, 42, 46, 47, 34, 48, 43,
49, 50, 20, 51, 52, 44, 40, 41, 53, 54,
59, 45, 55, 56, 46, 47, 48, 57, 49, 58,
50, 51, 60, 52, 61, 62, 53, 63, 54, 59,
55, 56, 64, 23, 65, 57, 66, 58, 67, 20,
68, 60, 61, 69, 62, 69, 63, 69, 69, 69,
69, 64, 65, 69, 66, 69, 69, 67, 68, 21,
21, 35, 69, 35, 35, 3, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69
4, 5, 5, 6, 7, 8, 9, 10, 11, 12,
13, 11, 14, 11, 11, 15, 11, 11, 11, 16,
11, 11, 17, 18, 19, 20, 21, 4, 10, 11,
12, 13, 11, 14, 11, 15, 11, 11, 11, 16,
11, 11, 17, 18, 19, 20, 21, 22, 22, 25,
26, 28, 29, 30, 27, 32, 33, 34, 35, 36,
40, 37, 39, 31, 38, 22, 22, 42, 43, 26,
44, 28, 29, 45, 30, 32, 33, 34, 46, 35,
36, 37, 39, 31, 49, 38, 47, 48, 42, 43,
44, 50, 51, 45, 52, 53, 54, 55, 46, 56,
57, 58, 40, 59, 49, 60, 47, 48, 61, 62,
23, 63, 50, 51, 52, 64, 53, 54, 55, 56,
57, 65, 58, 59, 66, 67, 60, 68, 61, 69,
62, 63, 70, 71, 72, 64, 73, 74, 26, 75,
76, 65, 77, 23, 66, 67, 78, 79, 68, 79,
69, 79, 70, 79, 71, 72, 79, 73, 74, 75,
76, 79, 79, 77, 79, 79, 78, 24, 24, 41,
79, 41, 41, 3, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79
} ;
static yyconst flex_int16_t yy_chk[200] =
static yyconst flex_int16_t yy_chk[222] =
{ 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 5, 5, 8, 8, 11, 12, 71,
13, 14, 15, 16, 34, 17, 26, 12, 18, 19,
19, 25, 23, 27, 28, 11, 30, 12, 13, 14,
31, 15, 16, 17, 26, 12, 32, 18, 29, 29,
25, 27, 28, 33, 30, 36, 37, 21, 38, 31,
39, 40, 20, 41, 42, 32, 29, 29, 43, 44,
52, 33, 45, 46, 36, 37, 38, 47, 39, 50,
40, 41, 53, 42, 54, 55, 43, 56, 44, 52,
45, 46, 57, 9, 58, 47, 59, 50, 61, 7,
62, 53, 54, 3, 55, 0, 56, 0, 0, 0,
0, 57, 58, 0, 59, 0, 0, 61, 62, 70,
70, 72, 0, 72, 72, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
69, 69, 69, 69, 69, 69, 69, 69, 69
1, 1, 1, 1, 1, 1, 1, 5, 5, 8,
8, 10, 12, 13, 81, 14, 15, 16, 17, 18,
40, 19, 21, 13, 20, 22, 22, 28, 29, 26,
30, 10, 12, 31, 13, 14, 15, 16, 32, 17,
18, 19, 21, 13, 35, 20, 33, 33, 28, 29,
30, 36, 37, 31, 38, 39, 43, 44, 32, 45,
46, 47, 24, 48, 35, 49, 33, 33, 50, 51,
23, 52, 36, 37, 38, 53, 39, 43, 44, 45,
46, 54, 47, 48, 55, 58, 49, 60, 50, 61,
51, 52, 62, 63, 64, 53, 65, 66, 9, 67,
68, 54, 70, 7, 55, 58, 71, 3, 60, 0,
61, 0, 62, 0, 63, 64, 0, 65, 66, 67,
68, 0, 0, 70, 0, 0, 71, 80, 80, 82,
0, 82, 82, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79
} ;
/* Table of booleans, true if rule could match eol. */
static yyconst flex_int32_t yy_rule_can_match_eol[17] =
static yyconst flex_int32_t yy_rule_can_match_eol[20] =
{ 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, };
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
};
/* The intent behind this definition is that it'll catch
* any uses of REJECT which flex missed.
......@@ -535,7 +545,7 @@ static yyconst flex_int32_t yy_rule_can_match_eol[17] =
// #define log printf
#define log
/* operators */
#line 539 "parser/scanner.c"
#line 549 "parser/scanner.c"
#define INITIAL 0
......@@ -810,9 +820,9 @@ YY_DECL
}
{
#line 42 "parser/scanner.l"
#line 51 "parser/scanner.l"
#line 816 "parser/scanner.c"
#line 826 "parser/scanner.c"
while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */
{
......@@ -839,13 +849,13 @@ yy_match:
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 70 )
if ( yy_current_state >= 80 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
++yy_cp;
}
while ( yy_base[yy_current_state] != 156 );
while ( yy_base[yy_current_state] != 174 );
yy_find_action:
yy_act = yy_accept[yy_current_state];
......@@ -883,77 +893,98 @@ do_action: /* This label is used only to access EOF actions. */
case 1:
YY_RULE_SETUP
#line 43 "parser/scanner.l"
#line 52 "parser/scanner.l"
{
return SELECT;
}
YY_BREAK
case 2:
YY_RULE_SETUP
#line 46 "parser/scanner.l"
#line 55 "parser/scanner.l"
{
return FROM;
}
YY_BREAK
case 3:
YY_RULE_SETUP
#line 49 "parser/scanner.l"
#line 58 "parser/scanner.l"
{
return CREATE;
}
YY_BREAK
case 4:
YY_RULE_SETUP
#line 52 "parser/scanner.l"
#line 61 "parser/scanner.l"
{
return TABLE;
}
YY_BREAK
case 5:
YY_RULE_SETUP
#line 55 "parser/scanner.l"
#line 64 "parser/scanner.l"
{
return UPDATE;
}
YY_BREAK
case 6:
YY_RULE_SETUP
#line 58 "parser/scanner.l"
#line 67 "parser/scanner.l"
{
return INSERT;
}
YY_BREAK
case 7:
YY_RULE_SETUP
#line 61 "parser/scanner.l"
#line 70 "parser/scanner.l"
{
return INTO;
}
YY_BREAK
case 8:
YY_RULE_SETUP
#line 64 "parser/scanner.l"
#line 73 "parser/scanner.l"
{
return VALUES;
}
YY_BREAK
case 9:
YY_RULE_SETUP
#line 67 "parser/scanner.l"
#line 76 "parser/scanner.l"
{
return DELETE;
}
YY_BREAK
case 10:
YY_RULE_SETUP
#line 70 "parser/scanner.l"
#line 79 "parser/scanner.l"
{
return DROP;
}
YY_BREAK
case 11:
YY_RULE_SETUP
#line 73 "parser/scanner.l"
#line 82 "parser/scanner.l"
{
return WHERE;
}
YY_BREAK
case 12:
YY_RULE_SETUP
#line 85 "parser/scanner.l"
{
return AND;
}
YY_BREAK
case 13:
YY_RULE_SETUP
#line 88 "parser/scanner.l"
{
return OR;
}
YY_BREAK
case 14:
YY_RULE_SETUP
#line 91 "parser/scanner.l"
{
int len = 0;
......@@ -965,44 +996,44 @@ YY_RULE_SETUP
return STRING;
}
YY_BREAK
case 12:
case 15:
YY_RULE_SETUP
#line 83 "parser/scanner.l"
#line 101 "parser/scanner.l"
{
yylval->ival = atoi(yytext);
log("digest :%d\n", yylval->ival);
return DIGEST;
}
YY_BREAK
case 13:
case 16:
YY_RULE_SETUP
#line 88 "parser/scanner.l"
#line 106 "parser/scanner.l"
{
yylval->sval = strdup(yytext);
return IDENT;
}
YY_BREAK
case 14:
case 17:
YY_RULE_SETUP
#line 92 "parser/scanner.l"
#line 110 "parser/scanner.l"
{
return yytext[0];
}
YY_BREAK
case 15:
/* rule 15 can match eol */
case 18:
/* rule 18 can match eol */
YY_RULE_SETUP
#line 95 "parser/scanner.l"
#line 113 "parser/scanner.l"
{
/* ignore */
}
YY_BREAK
case 16:
case 19:
YY_RULE_SETUP
#line 99 "parser/scanner.l"
#line 117 "parser/scanner.l"
ECHO;
YY_BREAK
#line 1006 "parser/scanner.c"
#line 1037 "parser/scanner.c"
case YY_STATE_EOF(INITIAL):
yyterminate();
......@@ -1297,7 +1328,7 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 70 )
if ( yy_current_state >= 80 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
......@@ -1326,11 +1357,11 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 70 )
if ( yy_current_state >= 80 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
yy_is_jam = (yy_current_state == 69);
yy_is_jam = (yy_current_state == 79);
(void)yyg;
return yy_is_jam ? 0 : yy_current_state;
......@@ -2194,7 +2225,7 @@ void yyfree (void * ptr , yyscan_t yyscanner)
#define YYTABLES_NAME "yytables"
#line 99 "parser/scanner.l"
#line 117 "parser/scanner.l"
yyscan_t scanner_init(char *sqlStr, PSCANNER_DATA yyext)
......
......@@ -30,7 +30,6 @@
%}
/* operators */
operator [-+&~|^/%*().!,;]
space [ \t\n\r\f]
nonewline [^\n\r]
comment ("--"{nonewline}*)
......@@ -39,6 +38,16 @@ identify [a-zA-Z][a-zA-Z0-9_]*
string [']+[a-zA-Z0-9_]*[']+
digest [-]?[0-9]+
equals_greater "=>"
less_equals "<="
greater_equals ">="
less_greater "<>"
not_equals "!="
self [,()\[\].;\:\+\-\*\/\%\^\<\>\=]
op_chars [\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]
operator {op_chars}+
%%
SELECT {
return SELECT;
......@@ -70,6 +79,15 @@ DELETE {
DROP {
return DROP;
}
WHERE {
return WHERE;
}
AND {
return AND;
}
OR {
return OR;
}
{string} {
int len = 0;
......@@ -89,7 +107,7 @@ DROP {
yylval->sval = strdup(yytext);
return IDENT;
}
{operator} {
{self} {
return yytext[0];
}
{whitespace} {
......
文件模式从 100644 更改为 100755
......@@ -9,28 +9,65 @@
#include <stdio.h>
#include <string.h>
#define log printf
PPortal CreatePortal(PSelectStmt stmt)
PPortal CreatePortal( )
{
PPortal portal = NULL;
PListCell tmpCell = NULL;
PScanHeaderRowInfo rowInfo = NULL;
portal = (PPortal) AllocMem(sizeof(Portal));
memset(portal, 0x00, sizeof(Portal));
/* general column list which ordered by stmt. */
for(tmpCell = stmt->columnList->head; tmpCell != NULL; tmpCell = tmpCell->next)
return portal;
}
int InitSelectPortal(PSelectStmt stmt, PPortal portal)
{
PListCell tmpCell = NULL;
PScanHeaderRowInfo rowInfo = NULL;
PTableList tblInfo = NULL;
int columnNum = 0;
if(NULL != stmt->columnList)
{
PAttrName node = (PAttrName)(tmpCell->value.pValue);
/* general column list which ordered by stmt. */
for(tmpCell = stmt->columnList->head; tmpCell != NULL; tmpCell = tmpCell->next)
{
PAttrName node = (PAttrName)(tmpCell->value.pValue);
rowInfo = (PScanHeaderRowInfo)AllocMem(sizeof(ScanHeaderRowInfo));
rowInfo->colName = strdup(node->attrName);
rowInfo = (PScanHeaderRowInfo)AllocMem(sizeof(ScanHeaderRowInfo));
rowInfo->colName = strdup(node->attrName);
AddCellToListTail(&(portal->list), rowInfo);
AddCellToListTail(&(portal->list), rowInfo);
}
return 0;
}
return portal;
/* select all */
for(tmpCell = stmt->tblList->head; tmpCell != NULL; tmpCell = tmpCell->next)
{
PTableRefName node = (PTableRefName)(tmpCell->value.pValue);
/* get table information */
tblInfo = GetTableInfo(node->tblRefName);
if (NULL == tblInfo)
{
log("select table failure.\n");
return -1;
}
columnNum = tblInfo->tableDef->colNum;
for(; columnNum > 0; columnNum--)
{
rowInfo = (PScanHeaderRowInfo)AllocMem(sizeof(ScanHeaderRowInfo));
rowInfo->colName = strdup(tblInfo->tableDef->column[columnNum].colName);
AddCellToListTail(&(portal->list), rowInfo);
}
}
return 0;
}
/*
......@@ -55,9 +92,14 @@ int SendToPortal(PPortal portal)
int EndPort(PPortal portal)
{
if(NULL == portal)
return -1;
PortalPrint(portal->buffer);
FreeMem(portal);
return 0;
}
......
......@@ -27,7 +27,10 @@ typedef struct ScanHeaderRowInfo
int colMaxSize;
}ScanHeaderRowInfo, *PScanHeaderRowInfo;
PPortal CreatePortal(PSelectStmt stmt);
PPortal CreatePortal();
int InitSelectPortal(PSelectStmt stmt, PPortal portal);
int SendToPortal(PPortal portal);
int EndPort(PPortal portal);
......
/*
* toadb relation
* Copyright (C) 2023-2023, senllang
*/
#ifndef HAT_RELATION_H_H
#define HAT_RELATION_H_H
typedef struct Relation
{
int relid;
int relType;
}Relation, *PRelation;
#endif
......@@ -343,7 +343,7 @@ extern int yylex \
#undef YY_DECL
#endif
#line 99 "parser/scanner.l"
#line 117 "parser/scanner.l"
#line 350 "scanner.h"
......
此差异已折叠。
......@@ -9,14 +9,8 @@
#include "tables.h"
#include "node.h"
#include "list.h"
#include "relation.h"
typedef struct TableList
{
DList list;
PPageDataHeader tableInfo;
PTableMetaInfo tableDef;
int tbl_fd;
}TableList, *PTableList;
typedef enum ColumnTypeEN
{
......@@ -28,6 +22,45 @@ typedef enum ColumnTypeEN
INVALID
}EnColumnType;
typedef struct TableList
{
DList list;
PRelation rel;
PPageDataHeader tableInfo;
PTableMetaInfo tableDef;
PGroupPageHeader groupInfo;
int tbl_fd;
PsgmrInfo sgmr;
}TableList, *PTableList;
typedef struct GroupItemData
{
PageOffset pagePos;
GroupItem ItemData;
MemberData memberData[FLEXIBLE_SIZE];
}GroupItemData, *PGroupItemData;
typedef struct HeapItemData
{
PageOffset pagePos;
ItemData ItemData;
}HeapItemData, *PHeapItemData;
typedef struct SearchPageInfo
{
PPageDataHeader page;
int item_offset;
int group_id;
int pageNum;
}SearchPageInfo, *PSearchPageInfo;
#define GetGroupMemberNum(gItemData) (GetItemSize(gItemData->ItemData.len) / sizeof(MemberData) - 1)
#define GroupItemDataMemOffset ((unsigned long)(((PGroupItemData)(0))->memberData))
#define HasFreeSpace(pageHeader, size) ((size) < (pageHeader->dataEndOffset - pageHeader->dataOffset))
/* global dictionary list */
extern DList* g_TblList;
void *AllocMem(unsigned int size);
......@@ -35,22 +68,64 @@ int FreeMem(void *pMem);
PTableList GetTableInfo(char *filename);
PTableList SearchTblInfo(char *filename);
int ReleaseTblInfoResource();
PPageDataHeader GetSpacePage(PTableList tblInfo, int size, PageOp op);
PPageDataHeader GetPageByIndex(PTableList tblInfo, int index);
int ReleaseTblInfo(PTableList tblInfo);
int ReleaseAllTblInfoResource();
/* get specified page */
PPageDataHeader GetPageByIndex(PTableList tblInfo, int index, ForkType forkNum);
/* find free space page */
PPageDataHeader GetSpacePage(PTableList tblInfo, int size, PageOp op, ForkType forkNum);
/* find free space group page */
int GetSpaceGroupPage(PTableList tblInfo, PTableRowData insertdata, PageOp op, PPageDataHeader *pageList);
PGroupItemData FindGroupInfo(PTableList tblInfo, int groupId);
PGroupItemData GetGroupInfo(PTableList tblInfo, PSearchPageInfo searchInfo);
PPageDataHeader GetFreeSpaceMemberPage(PTableList tblInfo, int size, PGroupItemData item, PageOp op, int colIndex);
PPageDataHeader* GetGroupMemberPages(PTableList tblInfo, PGroupItemData item, int *pageNum);
int ReleasePageList(PPageDataHeader *pagelist, int num);
int ReleasePageListMember(PPageDataHeader *pagelist, int num);
PPageDataHeader ExtensionTbl(PTableList tblInfo, int num);
int UpdateGroupMember(PTableList tblInfo, PPageDataHeader lastpage, int num, PGroupItemData item);
int GetInvlidRowCount(PPageDataHeader page);
/* page extension and init */
PPageDataHeader ExtensionTbl(PTableList tblInfo, int num, ForkType forkNum);
int InitPage(char *page, int flag);
/* main table fork page row */
PTableRowData GetRowDataFromPage(PTableList tblInfo, PSearchPageInfo searchInfo);
/* form row data and deform column data */
PTableRowData FormRowData(PTableMetaInfo tblMeta, PInsertStmt stmt);
PTableRowData DeFormRowData(PPageDataHeader page, int pageffset);
int WriteRowData(PTableList tblInfo, PPageDataHeader page, PTableRowData row);
PPageDataHeader ReadPage(PTableList tblInfo, int index);
/* form group row data, row array return */
PTableRowData FormColRowsData(PTableRowData insertdata);
PTableRowData FormCol2RowData(PTableRowData *colRows, int colNum);
int UpdateMetaData(PTableList tblInfo, ForkType forkNum);
int UpdateGroupMetaData(PTableList tblInfo, PGroupPageHeader page);
int UpdateTableMetaData(PTableList tblInfo, PPageDataHeader page);
int CloseTable(PTableList tbl);
/* buffer operator */
int FlushBuffer(PTableList tblInfo, char *buffer);
int WriteRowData(PTableList tblInfo, PPageDataHeader page, PTableRowData row);
int WriteRowItemData(PTableList tblInfo, PPageDataHeader page, PTableRowData row);
/* insert one group info into group file */
int InsertGroupItem(PTableList tblInfo, PPageDataHeader lastpage, int num);
PPageDataHeader ReadPage(PTableList tblInfo, int index, ForkType forkNum);
int WritePage(PTableList tblInfo, PPageDataHeader page, ForkType forkNum);
#endif
\ No newline at end of file
/*
* table files
* Copyright (C) 2023-2023, senllang
*/
#include "tfile.h"
#include "buffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
/* file operator */
#include <fcntl.h> /* Definition of AT_* constants */
#define log printf
#define debug
/* configure param */
int config_fsync = 0;
extern char *DataDir;
int CreateTableFile(char *filename, int mode)
{
int fd = -1;
char filepath[1024] = {0};
snprintf(filepath, 1024, "%s/%s", DataDir, filename);
debug("Debug: opentable file path:%s \n", filepath);
// 检查文件是否存在
if (access(filepath, F_OK) == 0)
{
log("table file %s already exist. err[%d]\n", filepath, errno);
return -1;
}
// 以二进制形式打开文件
fd = open(filepath, O_RDWR | O_CREAT, mode);
if (fd == -1)
{
log("create file %s error, maybe space not enough.errno[%d]\n", filepath, errno);
return -2;
}
return fd;
}
int OpenTableFile(char *filename, int mode)
{
int fd;
char filepath[1024];
int err = 0;
snprintf(filepath, 1024, "%s/%s", DataDir, filename);
debug("Debug: opentable file path:%s \n", filepath);
// 检查文件是否存在
if (access(filepath, F_OK) != 0)
{
// log("table file %s is not exist. \n", filepath);
err = errno;
return -1 * err;
}
// 以二进制形式打开文件
fd = open(filepath, O_RDWR, mode);
if (fd == -1)
{
log("open file %s error, errno[%d]\n", filepath, errno);
return -2;
}
return fd;
}
int DeleteTableFile(char *filename)
{
int ret = 0;
char filepath[FILE_PATH_MAX_LEN];
snprintf(filepath, FILE_PATH_MAX_LEN, "%s/%s", DataDir, filename);
// 检查文件是否存在
if (access(filepath, F_OK) != 0)
{
log("table file %s is not exist. \n", filepath);
return -1;
}
// 以二进制形式打开文件
ret = unlink(filepath);
if (ret != 0)
{
log("unlink file %s ,errno %d \n", filepath, errno);
return -1;
}
return ret;
}
int smgrOpen(char *filename)
{
int fd = OpenTableFile(filename, 0666);
}
int smgrFlush(int fd, char *buffer, int offnum)
{
int ret = 0;
if(fd <= 0)
{
log("table file not open\n");
return -1;
}
lseek(fd, (offnum-1)*PAGE_MAX_SIZE, SEEK_SET);
ret = write(fd, buffer, PAGE_MAX_SIZE);
if(config_fsync)
ret |= fsync(fd);
return ret;
}
int smgrClose(int fd)
{
int ret = 0;
if(config_fsync)
ret |= fsync(fd);
close(fd);
return ret;
}
int smgrRelease(PsgmrInfo sgmrInfo)
{
PVFVec head = sgmrInfo->vfhead;
PVFVec tpos = (PVFVec)head->list.next;
while(tpos != NULL)
{
if(tpos == head)
break;
DeleteVF(head, tpos);
smgrClose(tpos->fd);
FreeMem(tpos);
tpos = NULL;
tpos = (PVFVec)head->list.next;
}
if(tpos != NULL)
{
smgrClose(tpos->fd);
FreeMem(tpos);
tpos = NULL;
}
FreeMem(sgmrInfo);
return 0;
}
char* smgrReadPage(int fd, int offset, int size)
{
char *page = NULL;
int readSize = 0;
int filelen = 0;
filelen = lseek(fd, 0, SEEK_END);
if(offset >= filelen)
{
return NULL;
}
page = (char *)AllocMem(size);
/* read page */
lseek(fd, offset, SEEK_SET);
readSize = read(fd, (char *)page, size);
if(readSize < size)
{
FreeMem(page);
return NULL;
}
return page;
}
static int objId = 1;
void SetObjectId(int id)
{
if(id >= objId)
objId = id + 1;
return ;
}
int GetObjectId()
{
return objId++;
}
PVFVec SearchVF(PVFVec head, ForkType forknum)
{
PVFVec vpos = NULL;
PVFVec tpos = head;
while(tpos != NULL)
{
if(tpos->forkNum == forknum)
{
vpos = tpos;
break;
}
tpos = (PVFVec)tpos->list.next;
if(tpos == head)
break;
}
return vpos;
}
PVFVec DeleteVF(PVFVec head, PVFVec node)
{
DelDListNode((PDList*)&(head->list), &(node->list));
return node;
}
PVFVec smgr_open(PsgmrInfo smgrInfo, char *fileName, ForkType forkNum)
{
PVFVec vpos = NULL;
char fname[FILE_PATH_MAX_LEN] = {0};
int newnode = 0;
if(NULL == smgrInfo->vfhead)
{
smgrInfo->vfhead = (PVFVec)AllocMem(sizeof(VFVec));
INIT_DLIST_NODE(smgrInfo->vfhead->list);
smgrInfo->vfhead->forkNum = forkNum;
smgrInfo->vfhead->fd = 0;
vpos = smgrInfo->vfhead;
}
/* read info */
if(NULL == vpos)
{
vpos = SearchVF(smgrInfo->vfhead, forkNum);
if(NULL == vpos)
{
vpos = (PVFVec)AllocMem(sizeof(VFVec));
INIT_DLIST_NODE(smgrInfo->vfhead->list);
vpos->forkNum = forkNum;
vpos->fd = 0;
newnode = 1;
}
}
if(vpos->fd > 0)
return vpos;
switch(forkNum)
{
case MAIN_FORK:
snprintf(fname, FILE_PATH_MAX_LEN, "%s", fileName);
break;
case GROUP_FORK:
snprintf(fname, FILE_PATH_MAX_LEN, "%s%s", GROUP_FILE_PRE, fileName);
break;
default:
return NULL;
break;
}
vpos->fd = OpenTableFile(fname, 0666);
if(vpos->fd < 0)
{
// log("open file %s failure.\n", fname);
FreeMem(vpos);
return NULL;
}
if(newnode)
AddDListTail((PDList*)&(smgrInfo->vfhead->list), &(vpos->list));
return vpos;
}
PPageHeader smgr_read(PVFVec vfInfo, PPageOffset pageOffset)
{
PPageHeader page = NULL;
int offset = (pageOffset->pageno - 1) * PAGE_MAX_SIZE;
page = (PPageHeader)smgrReadPage(vfInfo->fd, offset, PAGE_MAX_SIZE);
if(NULL == page)
{
//log("read page %d-%d failure.\n", offset, PAGE_MAX_SIZE);
return NULL;
}
return page;
}
int smgr_create(char *fileName, ForkType forkNum)
{
char fname[FILE_PATH_MAX_LEN] = {0};
int fd = -1;
switch(forkNum)
{
case MAIN_FORK:
snprintf(fname, FILE_PATH_MAX_LEN, "%s", fileName);
break;
case GROUP_FORK:
snprintf(fname, FILE_PATH_MAX_LEN, "%s%s", GROUP_FILE_PRE, fileName);
break;
default:
return -1;
break;
}
fd = CreateTableFile(fname, 0666);
if(fd < 0)
{
log("create file %s failure.\n", fname);
}
return fd;
}
int smgr_write(PVFVec vfInfo, PPageOffset pageOffset, PPageHeader page)
{
int offset = 0;
int writeSize = 0;
offset = PAGE_MAX_SIZE * (pageOffset->pageno - 1);
lseek(vfInfo->fd, offset, SEEK_SET);
writeSize = write(vfInfo->fd, (char *)page, PAGE_MAX_SIZE);
if (writeSize != PAGE_MAX_SIZE)
{
log("table is extened failure, maybe is not enough space.\n");
return -1;
}
return 0;
}
/*
* table files
* Copyright (C) 2023-2023, senllang
*/
#ifndef HAT_TFILE_H_H
#define HAT_TFILE_H_H
#include "list.h"
/*
* filename
* table name : table file
* : grp_tablename
*/
#define GROUP_FILE_PRE "grp_"
#define FILE_PATH_MAX_LEN 1024
#define FILE_NAME_MAX_LEN 64
#define FLEXIBLE_SIZE
/* default 1GB (1*1024*1024*1024) 4Byte */
#define FILE_SEGMENT_MAX_SIZE (0x40000000)
#define PAGE_MAX_SIZE (4096)
#define FILE_SEGMENT_MAX_PAGE (FILE_SEGMENT_MAX_SIZE/PAGE_MAX_SIZE)
#define PAGE_VERSION (0x2B3C)
#define PAGE_HEAD_PAGE_NUM 1
#define PAGE_EXTENSION_MAX_NUM 512
typedef enum PageType
{
PAGE_HEADER = 0x01,
PAGE_DATA = 0x02,
PAGE_UNDO = 0x04,
PAGE_MAX_TYPE
}PageType;
typedef enum PageOp
{
PAGE_NEW,
PAGE_EXIST
}PageOp;
typedef enum FORK_NUM
{
MAIN_FORK,
GROUP_FORK,
MAX_FORK
}ForkType;
#define INVALID_PAGE_NUM 0
typedef struct PageOffset
{
int segno;
int pageno;
}PageOffset, *PPageOffset;
typedef struct PageHeader
{
int pageVersion;
int pageType; /* low 4bit pagetype, 4bit storagetype, .. */
int pageNum;
}PageHeader, *PPageHeader;
#define ITEM_DATA_START_OFFSET (sizeof(PageDataHeader))
#define PAGE_DATA_HEADER_SIZE sizeof(PageDataHeader)
#define PAGE_DATA_MAX_OFFSET (PAGE_MAX_SIZE)
#define ITEM_END_CHECK(item, page) (((char*)(item) - (char*)(page)) >= (page)->dataOffset)
#define GET_ITEM(offset, page) ((PItemData)((char*)(page) + offset))
#define ITEM_OFFSET(item, page) ((char*)(item) - (char*)(page))
#define ITEM_SIZE (sizeof(ItemData))
typedef struct ItemData
{
int offset;
int len; /* high bit |32 valid| */
long long rowid; /* todo: */
}ItemData, *PItemData;
/* high 32 bit is valid flag */
#define ITEM_VALID_MASK 0x80000000
#define GetItemValid(data) (data & ITEM_VALID_MASK)
#define GetItemSize(data) (data & ~ITEM_VALID_MASK)
/* file manage by pages units.
* page maxsize
* first page: pageheader --- table info -- columninfo ...
* seconde page: pageheader --- row data ...
*/
typedef struct PageDataHeader
{
PageHeader header;
int dataOffset; /* offset from this structure. */
int dataEndOffset; /* the same above */
int pageCnt; /* only head page record */
ItemData item[FLEXIBLE_SIZE];
}PageDataHeader, *PPageDataHeader;
#define MAX_ROW_DATA_SIZE (PAGE_MAX_SIZE - (PAGE_DATA_HEADER_SIZE + ITEM_SIZE))
#define STORAGE_TYPE_SHIFT 4
#define STORAGE_TYPE_MASK (0xF0)
#define GET_STORAGE_TYPE_SHIFT(sty) (((sty) & STORAGE_TYPE_MASK) >> STORAGE_TYPE_SHIFT)
#define SET_STORAGE_TYPE_SHIFT(sty) ((sty) << STORAGE_TYPE_SHIFT & STORAGE_TYPE_MASK)
typedef struct VFVec
{
DList list;
int forkNum;
int fd;
}VFVec, *PVFVec;
typedef struct StorageManagerInfo
{
int version;
int storageType;
PVFVec vfhead;
PVFVec vpos;
}sgmrInfo, *PsgmrInfo;
int CreateTableFile(char *filename, int mode);
int OpenTableFile(char *filename, int mode);
int DeleteTableFile(char *filename);
/* raw operations interface */
int smgrOpen(char *filename);
int smgrClose(int fd);
int smgrFlush(int fd, char *buffer, int offnum);
int smgrRelease(PsgmrInfo sgmrInfo);
char* smgrReadPage(int fd, int offset, int size);
/* object id update */
int GetObjectId();
void SetObjectId(int id);
PVFVec SearchVF(PVFVec head, ForkType forknum);
PVFVec DeleteVF(PVFVec head, PVFVec node);
int smgr_create(char *fileName, ForkType forkNum);
PVFVec smgr_open(PsgmrInfo smgrInfo, char *fileName, ForkType forkNum);
PPageHeader smgr_read(PVFVec vfInfo, PPageOffset pageOffset);
int smgr_write(PVFVec vfInfo, PPageOffset pageOffset, PPageHeader page);
#endif
\ No newline at end of file
/*
* toadb N-Ary storage model
* Copyright (C) 2023-2023, senllang
*/
#include "nsm.h"
\ No newline at end of file
/*
* toadb N-Ary storage model
* Copyright (C) 2023-2023, senllang
*/
#ifndef HAT_NSM_H_H
#define HAT_NSM_H_H
#include "tablecom.h"
#pragma push(pack(1))
#pragma po(pack(1))
#endif
/*
* toadb pax storage
* Copyright (C) 2023-2023, senllang
*/
#include "pax.h"
\ No newline at end of file
/*
* toadb pax storage
* Copyright (C) 2023-2023, senllang
*/
#ifndef HAT_PAX_H_H
#define HAT_PAX_H_H
#include "tablecom.h"
#pragma push(pack(1))
#define MAX_ROW_PER_GROUP ((PAGE_MAX_SIZE/(MIN_ROW_SIZE))&0xFFFFFF00)
typedef struct GroupChain
{
PageOffset prev;
PageOffset next;
PageOffset head;
}GroupChain;
typedef struct GroupPageInfo
{
int group_id;
int columnNum; /* It specified column index which data will storaged in this page. */
}GroupPageInfo;
#define FIRST_GROUP_ID 1
#define INVALID_GROUP_ID 0
/*
* table file format
* file manage by pages units.
* page maxsize
* first page: PageDataHeader --- table info -- columninfo ...
* seconde page: pageheader --- pagegroup-- item1,item2 ....freespace...columndata2,columndata1
* undo page: pageheader --- pagegroup-- item1,item2 ....freespace...columndata2,columndata1
*/
/*
* group file format
* first page: GroupPageHeader(pagedata--groupinfo) --- table info -- columninfo ...
* second page: pageheader --- groupitem1,groupitem2,... freespace ... memdata2,memdata2
*
*/
typedef struct GroupPageHeader
{
PageDataHeader pageheader;
GroupPageInfo groupInfo;
}GroupPageHeader, *PGroupPageHeader;
#define GROUP_PAGE_HEADER_SIZE (sizeof(GroupPageHeader))
/*
* size same as item
*/
typedef struct GroupItem
{
int offset;
int len; /* high bit |32 valid| */
long long groupid; /* todo: */
}GroupItem, *PGroupItem;
typedef struct MemberData
{
int colIndex ;
int memNum;
PageOffset member[FLEXIBLE_SIZE]; /* head is null, while link head */
}MemberData, *PMemberData;
#pragma pop(pack(1))
#endif
\ No newline at end of file
/*
* toadb tables common defines
* Copyright (C) 2023-2023, senllang
*/
#ifndef HAT_TABLE_COM_H_H
#define HAT_TABLE_COM_H_H
#include "tfile.h"
#define NAME_MAX_LEN 64
#pragma push(pack(1))
typedef struct ColumnDefInfo
{
char colName[NAME_MAX_LEN];
int type;
int options;
}ColumnDefInfo, *PColumnDefInfo;
typedef struct TableMetaInfo
{
int tableId;
int tableType;
char tableName[NAME_MAX_LEN];
int colNum;
ColumnDefInfo column[FLEXIBLE_SIZE];
}TableMetaInfo, *PTableMetaInfo;
typedef struct RowColumnData
{
int size;
int attrindex; /* start from 0 */
char data[FLEXIBLE_SIZE];
}RowColumnData, *PRowColumnData;
/* exec form rowdata */
typedef struct TableRowData
{
int size; /* row data size */
int num; /* column num of row data */
PRowColumnData columnData[FLEXIBLE_SIZE];
}TableRowData, *PTableRowData;
#define MIN_ROW_SIZE (sizeof(TableRowData)+sizeof(RowColumnData))
#pragma pop(pack(1))
#endif
\ No newline at end of file
......@@ -3,4 +3,183 @@
* Copyright (C) 2023-2023, senllang
*/
#include "tables.h"
\ No newline at end of file
#include "tables.h"
#include "buffer.h"
#include "tfile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define log printf
int TableOpen(PTableList tblInfo, ForkType forkNum)
{
PVFVec vfInfo = NULL;
PageOffset pageoffset = {1,1};
PPageHeader page = NULL;
int metadata_size = 0;
/* add vfinfo linker */
if(NULL == tblInfo->sgmr)
{
tblInfo->sgmr = (PsgmrInfo)AllocMem(sizeof(sgmrInfo));
tblInfo->sgmr->version = 0x01;
tblInfo->sgmr->storageType = tblInfo->tableDef->tableType;
tblInfo->sgmr->vfhead = NULL;
tblInfo->sgmr->vpos = NULL;
}
vfInfo = smgr_open(tblInfo->sgmr, tblInfo->tableDef->tableName, forkNum);
if(NULL == vfInfo)
{
//log("open table %s-%d failure.\n", tblInfo->tableDef->tableName, forkNum);
return -1;
}
if(forkNum == GROUP_FORK)
{
page = smgr_read(vfInfo, &pageoffset);
if(NULL == page)
{
//log("read table %s-%d first page failure.\n", tblInfo->tableDef->tableName, forkNum);
return -2;
}
tblInfo->groupInfo = (PGroupPageHeader)page;
}
return 0;
}
int TableCreate(char *fileName, ForkType forkNum)
{
PageOffset pageoffset = {1,1};
PGroupPageHeader gpage = NULL;
PPageDataHeader page = NULL;
PTableMetaInfo tableDef = NULL;
VFVec vf;
int metadata_size = 0;
int gfd = -1, tfd = -1;
/* create file on the disk */
gfd = smgr_create(fileName, forkNum);
if(gfd < 0)
{
log("create table %s-%d failure.\n", fileName, forkNum);
return -1;
}
/* initialize first page, which has the metadata infomation. */
if(GROUP_FORK == forkNum)
{
/* read first page of table file */
tfd = smgrOpen(fileName);
if(tfd < 0)
{
log("create table %s-%d failure.\n", fileName, forkNum);
smgrClose(gfd);
return -1;
}
vf.fd = tfd;
pageoffset.pageno = PAGE_HEAD_PAGE_NUM;
page = (PPageDataHeader)smgr_read(&vf, &pageoffset);
if(NULL == page)
{
log("read table %s-%d first page failure.\n", fileName, forkNum);
smgrClose(gfd);
smgrClose(tfd);
return -1;
}
/* first page content is grouppageheader and table metadata */
gpage = (PGroupPageHeader)AllocMem(PAGE_MAX_SIZE);
/* page header */
memcpy(&(gpage->pageheader), page, PAGE_DATA_HEADER_SIZE);
/* group info */
tableDef = (PTableMetaInfo) ((char*)(page) + page->dataOffset);
gpage->groupInfo.group_id = INVALID_GROUP_ID;
gpage->groupInfo.columnNum = tableDef->colNum;
/* table metadata, same as table */
metadata_size = page->dataEndOffset - page->dataOffset;
memcpy((char*)gpage + GROUP_PAGE_HEADER_SIZE,
tableDef,
metadata_size);
gpage->pageheader.dataOffset = GROUP_PAGE_HEADER_SIZE;
gpage->pageheader.dataEndOffset = GROUP_PAGE_HEADER_SIZE + metadata_size;
/* write to disk file */
smgrFlush(gfd, (char*)gpage, 1);
FreeMem(gpage);
smgrClose(tfd);
}
smgrClose(gfd);
return 0;
}
PPageHeader TableRead(PsgmrInfo smgrInfo, PPageOffset pageoffset, ForkType forkNum)
{
PPageHeader page = NULL;
PVFVec vpos = NULL;
vpos = SearchVF(smgrInfo->vfhead, forkNum);
if(NULL == vpos)
{
log("file not opened %d \n", forkNum);
return NULL;
}
page = smgr_read(vpos, pageoffset);
return page;
}
int TableWrite(PsgmrInfo smgrInfo, PPageHeader page, ForkType forkNum)
{
PageOffset pageoffset;
PVFVec vpos = NULL;
int ret = 0;
vpos = SearchVF(smgrInfo->vfhead, forkNum);
if(NULL == vpos)
{
log("file not opened %d \n", forkNum);
return -1;
}
pageoffset.pageno = page->pageNum;
ret = smgr_write(vpos, &pageoffset, page);
return ret;
}
int TableDrop(PTableList tblInfo)
{
int ret = 0;
char filepath[1024] = {0};
if(NULL == tblInfo)
{
log("exec drop table failure. unknow table name.\n");
return -1;
}
/* delete file main fork */
ret = DeleteTableFile(tblInfo->tableDef->tableName);
if(0 != ret)
{
log("exec drop %s table failure.\n", tblInfo->tableDef->tableName);
return -1;
}
/* delete file main fork */
snprintf(filepath, 1024, "%s%s", GROUP_FILE_PRE, tblInfo->tableDef->tableName);
ret = DeleteTableFile(filepath);
return ret;
}
\ No newline at end of file
......@@ -6,76 +6,29 @@
#ifndef HAT_TABLES_H_H
#define HAT_TABLES_H_H
#define PAGE_MAX_SIZE (1024)
#define PAGE_VERSION (0x2B3C)
#define FLEXIBLE_SIZE
#define NAME_MAX_LEN 64
#include "tfile.h"
#include "tablecom.h"
#include "nsm.h"
#include "pax.h"
#define PAGE_HEAD_PAGE_NUM 1
#define PAGE_EXTENSION_MAX_NUM 512
typedef struct TableList *PTableList;
typedef enum PageType
typedef enum StorageType
{
PAGE_HEADER = 0x01,
PAGE_DATA
}PageType;
ST_NSM = 0x01,
ST_PAX = 0x02,
ST_NUM
}StorageType;
typedef enum PageOp
{
PAGE_NEW,
PAGE_EXIST
}PageOp;
/* file manage by pages units.
* page maxsize
* first page: pageheader --- table info -- columninfo ...
* seconde page: pageheader --- row data ...
*/
typedef struct PageHeader
{
int pageVersion;
int pageType;
int pageNum;
}PageHeader, *PPageHeader;
typedef struct PageDataHeader
{
PageHeader header;
int dataOffset; /* offset from this structure. */
int dataEndOffset; /* the same above */
}PageDataHeader, *PPageDataHeader;
#define PAGE_DATA_HEADER_SIZE sizeof(PageDataHeader)
/* create table file and initialize first page with metadata. */
int TableCreate(char *fileName, ForkType forkNum);
typedef struct ColumnDefInfo
{
char colName[NAME_MAX_LEN];
int type;
int options;
}ColumnDefInfo, *PColumnDefInfo;
typedef struct TableMetaInfo
{
int tableId;
char tableName[NAME_MAX_LEN];
int colNum;
ColumnDefInfo column[FLEXIBLE_SIZE];
}TableMetaInfo, *PTableMetaInfo;
int TableOpen(PTableList tblInfo, ForkType forkNum);
typedef struct RowColumnData
{
int size;
int attrindex;
char data[FLEXIBLE_SIZE];
}RowColumnData, *PRowColumnData;
typedef struct TableRowData
{
int size; /* row data size */
int num; /* column num of row data */
PRowColumnData columnData[FLEXIBLE_SIZE];
}TableRowData, *PTableRowData;
int TableDrop(PTableList tblInfo);
PPageHeader TableRead(PsgmrInfo smgrInfo, PPageOffset pageoffset, ForkType forkNum);
int TableWrite(PsgmrInfo smgrInfo, PPageHeader page, ForkType forkNum);
#endif
\ No newline at end of file
......@@ -8,13 +8,20 @@
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <getopt.h>
#include <sys/types.h>
#include <fcntl.h>
#include "toadmain.h"
#include "parser.h"
#include "node.h"
#include "executor.h"
#include "buffer.h"
#define MAX_COMMAND_LENGTH 1024
#define log printf
char *DataDir = "./toadbtest";
/*
* toadb数据库服务入口,进行SQL解析,执行
......@@ -26,13 +33,23 @@ int toadbMain(int argc, char *argv[])
//char *username = pw->pw_name;
char *username = "toadb";
char *prompt = ">";
List *parserTree = NULL;
int commandlen = 0;
if (getuid() == 0) {
prompt = "#";
}
/* argments process */
args_opt(argc, argv);
ShowToadbInfo();
if(checkDataDir() < 0)
{
log("Data directory is invalid.\n");
return -1;
}
while (1)
{
printf("%s%s ", username, prompt);
......@@ -51,18 +68,94 @@ int toadbMain(int argc, char *argv[])
break;
}
/* 对输入的SQL进行词法和语法解析,生成解析树 */
parserTree = raw_parser(command);
// travelParserTree(parserTree);
ToadMainEtry(command);
}
/* 执行器调用入口,根据解析树进行执行 */
ExecutorMain(parserTree);
ExitToad();
return 0;
}
/* 释放解析树占用的内存资源 */
ReleaseParserTreeResource(parserTree);
}
int ToadMainEtry(char *query)
{
List *parserTree = NULL;
/* 对输入的SQL进行词法和语法解析,生成解析树 */
parserTree = raw_parser(query);
// travelParserTree(parserTree);
/* 执行器调用入口,根据解析树进行执行 */
ExecutorMain(parserTree);
/* 释放解析树占用的内存资源 */
ReleaseParserTreeResource(parserTree);
return 0;
}
int ExitToad()
{
/* 释放系统字典占用的内存资源 */
ReleaseTblInfoResource();
ReleaseAllTblInfoResource();
return 0;
}
int checkDataDir()
{
// 检查文件是否存在
if (access(DataDir, F_OK) != 0)
{
// log("table file %s is not exist. \n", filepath);
return -1;
}
return 0;
}
int args_opt(int argc, char *argv[])
{
int optindex;
int c;
int digit_optind = 0;
static struct option long_options[] =
{
{"reqarg", required_argument, NULL, 'r'},
{"optarg", optional_argument, NULL, 'o'},
{"noarg", no_argument, NULL, 'n'},
{NULL, 0, NULL, 0},
};
/* getopt error */
opterr = 1;
while((c =getopt_long(argc, argv, "D:C:-",
long_options, &optindex))!= -1)
{
switch(c)
{
case 'D':
DataDir = strdup(optarg);
break;
case 'C':
break;
default:
break;
}
}
return 0;
}
void ShowToadbInfo()
{
char *cwd = NULL;
/* current director path */
cwd = getcwd(NULL, 0);
printf("cwd :%s\n", cwd);
free(cwd);
/* Datadir */
printf("datadir: [%s]\n",DataDir);
}
\ No newline at end of file
......@@ -6,6 +6,16 @@
#ifndef HAT_TOADMAIN_H_H
#define HAT_TOADMAIN_H_H
#define MAX_COMMAND_LENGTH 1024
int toadbMain(int argc, char *argv[]) ;
int ToadMainEtry(char *query);
int ExitToad();
int args_opt(int argc, char *argv[]);
int checkDataDir();
void ShowToadbInfo();
#endif
\ No newline at end of file
2023/6/27
=====================
# 资源的释放清理
设计资源管理,每次申请都加到全局list中,释放时从list中删除,最后退出时检查list中是否有未释放资源
## 局部资源
## 事务级资源
* 解析树, 在每输query处理完后释放;
* 执行状态 , 在每输query处理完后释放;
* portal 资源 , 在每输query处理完后释放;
## 系统级资源
* 系统字典,在系统退出时释放;
# 参数输入解析
* 数据库目录参数 -D
* 初始化参数 -i 此时新建数据库目录及其它标识文件,如果不带时必须目录存在
* 帮助参数 -h
* 版本参数 -v
# 分层及接口
## 系统字典接口
* 初始化 系统字典缓存结构, 适合hash, (表名,id,数据库id)作为键值
* 创建表 需要插入系统字典
* 删除表 查找系统字典,先从系统字典中删除,再删除文件
* 查找表 查找系统字典,缓存中没有时,从文件中加载
* 销毁 在系统结束时清理资源
* 同步 多任务时的同步处理
* 物理存储
* 数据缓存
\ No newline at end of file
all:
make -C ./tsql all
make -C ./tbench all
clean:
make -C ./tsql clean
make -C ./tbench clean
......@@ -4,38 +4,29 @@
# Copyright (C) 2023-2023, senllang
#
VERSION_H = 0
VERSION_L = 01
TARGET = toadbench
##########################################
# toplevel source code directory
# topdir=$(shell pwd)
topdir = .
topsrcdir = $(topdir)/../../
SRC_DIR=$(topdir)
# search all directory and subdirectory
DIRS = $(shell find $(SRC_DIR) -maxdepth 5 -type d)
INC_DIR = $(shell find $(topsrcdir) -maxdepth 5 -type d)
# general source file list , and objects list
SOURCE = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
OBJS = ${patsubst %.c, %.o, $(SOURCE)}
# .h files take place all directories.
INC = ${foreach dir, $(DIRS), -I$(dir)}
##########################################
# test programme
builddir = $(topdir)/build
INC = ${foreach dir, $(INC_DIR), -I$(dir)}
##########################################
##########################################
DEFINES += -DTEST_PRO
LDFLAGS += -lpthread
DEFINES += -DSTORAGE_FORMAT_PAX -DMEM_DEBUG
DEBUG_FLAGS = -g
##########################################
......@@ -50,11 +41,8 @@ CFLAGS += ${DEBUG_FLAGS}
# compile start
.PHONY: all clean
all: $(default) $(builddir) $(OBJS) $(TARGET)
all: $(default) $(OBJS) $(TARGET)
#build dir create
$(builddir):
mkdir -p $(builddir)
# object compile to O_PATH
.c.o: $(SOURCE)
......@@ -70,7 +58,6 @@ default:
echo -e $(OBJS)
echo -e $(INC)
clean:
rm -rf ${TARGET} ${OBJS} ${builddir} $(topdir)/*.o
rm -rf ${TARGET} ${OBJS} $(topdir)/*.o
# Makefile toadb project
# create by senllang 2021/1/1
# mail : study@senllang.onaliyun.com
# Copyright (C) 2023-2023, senllang
#
TARGET = toadsql
##########################################
# toplevel source code directory
# topdir=$(shell pwd)
topdir = .
topsrcdir = $(topdir)/../..
SRC_DIR=$(topdir)
# filter-out source code directory
OUT_SRC_DIR = $(topsrcdir)/tools $(topsrcdir)/main
OUT_DIR = $(foreach dir, $(OUT_SRC_DIR), $(shell find $(dir) -maxdepth 5 -type d))
DIR_DISP = $(foreach dir, $(OUT_DIR), $(wildcard $(dir)/*.c))
# general source file list , and objects list
DIR_ALL = $(shell find $(topsrcdir) -maxdepth 5 -type d)
SOURCE = $(filter-out $(DIR_DISP), $(foreach dir, $(DIR_ALL), $(wildcard $(dir)/*.c)))
# search all directory and subdirectory
INC_DIR = $(DIR_ALL)
# general source file list , and objects list
DIRS = $(shell find $(SRC_DIR) -maxdepth 5 -type d)
# SOURCE += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
OBJS = ${patsubst %.c, %.o, $(SOURCE)}
# .h files take place all directories.
INC = ${foreach dir, $(INC_DIR), -I$(dir)}
##########################################
##########################################
DEFINES += -DSTORAGE_FORMAT_PAX -DMEM_DEBUG
DEBUG_FLAGS = -g
##########################################
CC = gcc
#CFLAGS += -O2
CFLAGS += ${INC}
CFLAGS += ${DEFINES}
CFLAGS += ${LDFLAGS}
CFLAGS += ${DEBUG_FLAGS}
# compile start
.PHONY: all clean
all: $(default) $(OBJS) $(TARGET)
# object compile to O_PATH
.c.o: $(SOURCE)
$(CC) $(CFLAGS) -c $<
$(TARGET):$(topdir)/*.o
$(CC) $(CFLAGS) $^ -o $@
# show various value
default:
@echo -e "DIRS "$(DIRS)
@echo -e "SOURCE "$(SOURCE)
@echo -e "OBJS "$(OBJS)
@echo -e "INC "$(INC)
@echo -e "DIR_DISP "$(DIR_DISP)
clean:
rm -rf ${TARGET} ${OBJS} $(topdir)/*.o
/*
* toadb tsqlmain
* Copyright (C) 2023-2023, senllang
*/
#include <unistd.h>
#include <getopt.h> /*所在头文件 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "toadmain.h"
extern char *DataDir;
int main(int argc, char *argv[])
{
int optindex;
int c;
int digit_optind = 0;
char *command = NULL;
int runMode = 0;
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0 },
};
/* getopt error */
opterr = 1;
while((c =getopt_long(argc, argv, "D:C:-",
long_options, &optindex))!= -1)
{
switch(c)
{
case 'D':
DataDir = strdup(optarg);
break;
case 'C':
command = strdup(optarg);
runMode = 1;
break;
default:
printf("unknow argument %c\n", c);
return -1;
}
}
if(runMode)
{
ToadMainEtry(command);
ExitToad();
}
else
{
toadbMain(argc, argv);
}
return 0;
}
\ No newline at end of file
此差异已折叠。
此差异已折叠。
......@@ -19,7 +19,8 @@ typedef struct dListCell
void *value;
}DLCell, *PDLCell;
#define INIT_DLIST_NODE(list) (list.prev = &list, list.next = &list)
int AddDListTail(PDList *list, PDList cell);
int DelDListNode(PDList *list, PDList cell);
int AddCellToListTail(PDList *head, void *ptr);
#endif
\ No newline at end of file
此差异已折叠。
此差异已折叠。
文件模式从 100644 更改为 100755
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册