您当前的位置: 首页 >  数据库

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

如何做一个国产数据库(三)

qianbo_insist 发布时间:2021-06-27 20:42:29 ,浏览量:0

一和二

如何做一个国产数据库一 如何做一个国产数据库二

1、数据结构重新定义

再次重新定义数据结构

typedef struct sdata
{
	uint32_t index;
	char vardata[128];
}sdata;

typedef struct sdata_index
{
	uint32_t index;
	uint32_t offset;
}sdata_index;


//增加的数据库上下文环境结构体
typedef struct db_context
{
	char v_tablename[256];
	sdata_index *v_index = NULL;
	uint32_t v_tindex = 0;
	uint32_t v_nindex = 0;
}db_context;
2、增删改查

此次增加了一个数据库的db_context 结构体,包含文件名称,索引内存保存,两个index的内存保存,在增加四个api函数,以前的函数变成工具,定义tool.h 和tool.cpp.

增加api.h 和api.cpp,增加四个函数声明:

int   Insert(void *data,db_context *v_context);
int   Delete(void *key, db_context *v_context);
int   Update(void *key, void *data, db_context *v_context);
void *Select(void *key, db_context *v_context);

今天实现Insert 函数,暂不测试,因为单元测试需要单独一个教程才行

xx xx xx xx | xx xx xx xx | xx xx xx xx – xx xx xx xx 4字节最大索引| 4字节当前索引 4bytes index 4bytes offset data … */ //2M的空间做索引 //uint32_t indext;//索引总长度 //uint32_t indexn;//索引长度,内容为x个索引 indexn v_tablename,tname); if (context->v_index == NULL) context->v_index = bfile_read_index( tname, /*filename*/ context->v_tindex, context->v_nindex ); if (context->v_index == NULL) return -1; return 0; } 2.3实现插入

整个增加的流程为:

#mermaid-svg-LnXl96nwWtNZ1uYc .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .label text{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .node rect,#mermaid-svg-LnXl96nwWtNZ1uYc .node circle,#mermaid-svg-LnXl96nwWtNZ1uYc .node ellipse,#mermaid-svg-LnXl96nwWtNZ1uYc .node polygon,#mermaid-svg-LnXl96nwWtNZ1uYc .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-LnXl96nwWtNZ1uYc .node .label{text-align:center;fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .node.clickable{cursor:pointer}#mermaid-svg-LnXl96nwWtNZ1uYc .arrowheadPath{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-LnXl96nwWtNZ1uYc .flowchart-link{stroke:#333;fill:none}#mermaid-svg-LnXl96nwWtNZ1uYc .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-LnXl96nwWtNZ1uYc .edgeLabel rect{opacity:0.9}#mermaid-svg-LnXl96nwWtNZ1uYc .edgeLabel span{color:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-LnXl96nwWtNZ1uYc .cluster text{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-LnXl96nwWtNZ1uYc .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-LnXl96nwWtNZ1uYc text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-LnXl96nwWtNZ1uYc .actor-line{stroke:grey}#mermaid-svg-LnXl96nwWtNZ1uYc .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-LnXl96nwWtNZ1uYc #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .sequenceNumber{fill:#fff}#mermaid-svg-LnXl96nwWtNZ1uYc #sequencenumber{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc #crosshead path{fill:#333;stroke:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .messageText{fill:#333;stroke:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-LnXl96nwWtNZ1uYc .labelText,#mermaid-svg-LnXl96nwWtNZ1uYc .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-LnXl96nwWtNZ1uYc .loopText,#mermaid-svg-LnXl96nwWtNZ1uYc .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-LnXl96nwWtNZ1uYc .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-LnXl96nwWtNZ1uYc .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-LnXl96nwWtNZ1uYc .noteText,#mermaid-svg-LnXl96nwWtNZ1uYc .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-LnXl96nwWtNZ1uYc .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-LnXl96nwWtNZ1uYc .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-LnXl96nwWtNZ1uYc .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-LnXl96nwWtNZ1uYc .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc .section{stroke:none;opacity:0.2}#mermaid-svg-LnXl96nwWtNZ1uYc .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-LnXl96nwWtNZ1uYc .section2{fill:#fff400}#mermaid-svg-LnXl96nwWtNZ1uYc .section1,#mermaid-svg-LnXl96nwWtNZ1uYc .section3{fill:#fff;opacity:0.2}#mermaid-svg-LnXl96nwWtNZ1uYc .sectionTitle0{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .sectionTitle1{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .sectionTitle2{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .sectionTitle3{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-LnXl96nwWtNZ1uYc .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc .grid path{stroke-width:0}#mermaid-svg-LnXl96nwWtNZ1uYc .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-LnXl96nwWtNZ1uYc .task{stroke-width:2}#mermaid-svg-LnXl96nwWtNZ1uYc .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc .taskText:not([font-size]){font-size:11px}#mermaid-svg-LnXl96nwWtNZ1uYc .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-LnXl96nwWtNZ1uYc .task.clickable{cursor:pointer}#mermaid-svg-LnXl96nwWtNZ1uYc .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LnXl96nwWtNZ1uYc .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LnXl96nwWtNZ1uYc .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LnXl96nwWtNZ1uYc .taskText0,#mermaid-svg-LnXl96nwWtNZ1uYc .taskText1,#mermaid-svg-LnXl96nwWtNZ1uYc .taskText2,#mermaid-svg-LnXl96nwWtNZ1uYc .taskText3{fill:#fff}#mermaid-svg-LnXl96nwWtNZ1uYc .task0,#mermaid-svg-LnXl96nwWtNZ1uYc .task1,#mermaid-svg-LnXl96nwWtNZ1uYc .task2,#mermaid-svg-LnXl96nwWtNZ1uYc .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-LnXl96nwWtNZ1uYc .taskTextOutside0,#mermaid-svg-LnXl96nwWtNZ1uYc .taskTextOutside2{fill:#000}#mermaid-svg-LnXl96nwWtNZ1uYc .taskTextOutside1,#mermaid-svg-LnXl96nwWtNZ1uYc .taskTextOutside3{fill:#000}#mermaid-svg-LnXl96nwWtNZ1uYc .active0,#mermaid-svg-LnXl96nwWtNZ1uYc .active1,#mermaid-svg-LnXl96nwWtNZ1uYc .active2,#mermaid-svg-LnXl96nwWtNZ1uYc .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-LnXl96nwWtNZ1uYc .activeText0,#mermaid-svg-LnXl96nwWtNZ1uYc .activeText1,#mermaid-svg-LnXl96nwWtNZ1uYc .activeText2,#mermaid-svg-LnXl96nwWtNZ1uYc .activeText3{fill:#000 !important}#mermaid-svg-LnXl96nwWtNZ1uYc .done0,#mermaid-svg-LnXl96nwWtNZ1uYc .done1,#mermaid-svg-LnXl96nwWtNZ1uYc .done2,#mermaid-svg-LnXl96nwWtNZ1uYc .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-LnXl96nwWtNZ1uYc .doneText0,#mermaid-svg-LnXl96nwWtNZ1uYc .doneText1,#mermaid-svg-LnXl96nwWtNZ1uYc .doneText2,#mermaid-svg-LnXl96nwWtNZ1uYc .doneText3{fill:#000 !important}#mermaid-svg-LnXl96nwWtNZ1uYc .crit0,#mermaid-svg-LnXl96nwWtNZ1uYc .crit1,#mermaid-svg-LnXl96nwWtNZ1uYc .crit2,#mermaid-svg-LnXl96nwWtNZ1uYc .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-LnXl96nwWtNZ1uYc .activeCrit0,#mermaid-svg-LnXl96nwWtNZ1uYc .activeCrit1,#mermaid-svg-LnXl96nwWtNZ1uYc .activeCrit2,#mermaid-svg-LnXl96nwWtNZ1uYc .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-LnXl96nwWtNZ1uYc .doneCrit0,#mermaid-svg-LnXl96nwWtNZ1uYc .doneCrit1,#mermaid-svg-LnXl96nwWtNZ1uYc .doneCrit2,#mermaid-svg-LnXl96nwWtNZ1uYc .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-LnXl96nwWtNZ1uYc .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-LnXl96nwWtNZ1uYc .milestoneText{font-style:italic}#mermaid-svg-LnXl96nwWtNZ1uYc .doneCritText0,#mermaid-svg-LnXl96nwWtNZ1uYc .doneCritText1,#mermaid-svg-LnXl96nwWtNZ1uYc .doneCritText2,#mermaid-svg-LnXl96nwWtNZ1uYc .doneCritText3{fill:#000 !important}#mermaid-svg-LnXl96nwWtNZ1uYc .activeCritText0,#mermaid-svg-LnXl96nwWtNZ1uYc .activeCritText1,#mermaid-svg-LnXl96nwWtNZ1uYc .activeCritText2,#mermaid-svg-LnXl96nwWtNZ1uYc .activeCritText3{fill:#000 !important}#mermaid-svg-LnXl96nwWtNZ1uYc .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-LnXl96nwWtNZ1uYc g.classGroup text .title{font-weight:bolder}#mermaid-svg-LnXl96nwWtNZ1uYc g.clickable{cursor:pointer}#mermaid-svg-LnXl96nwWtNZ1uYc g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-LnXl96nwWtNZ1uYc g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-LnXl96nwWtNZ1uYc .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-LnXl96nwWtNZ1uYc .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-LnXl96nwWtNZ1uYc .dashed-line{stroke-dasharray:3}#mermaid-svg-LnXl96nwWtNZ1uYc #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc .commit-id,#mermaid-svg-LnXl96nwWtNZ1uYc .commit-msg,#mermaid-svg-LnXl96nwWtNZ1uYc .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-LnXl96nwWtNZ1uYc g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-LnXl96nwWtNZ1uYc g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-LnXl96nwWtNZ1uYc g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-LnXl96nwWtNZ1uYc .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-LnXl96nwWtNZ1uYc .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-LnXl96nwWtNZ1uYc .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-LnXl96nwWtNZ1uYc .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-LnXl96nwWtNZ1uYc .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-LnXl96nwWtNZ1uYc .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-LnXl96nwWtNZ1uYc .edgeLabel text{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LnXl96nwWtNZ1uYc .node circle.state-start{fill:black;stroke:black}#mermaid-svg-LnXl96nwWtNZ1uYc .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-LnXl96nwWtNZ1uYc #statediagram-barbEnd{fill:#9370db}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-state .divider{stroke:#9370db}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-LnXl96nwWtNZ1uYc .note-edge{stroke-dasharray:5}#mermaid-svg-LnXl96nwWtNZ1uYc .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-LnXl96nwWtNZ1uYc .error-icon{fill:#522}#mermaid-svg-LnXl96nwWtNZ1uYc .error-text{fill:#522;stroke:#522}#mermaid-svg-LnXl96nwWtNZ1uYc .edge-thickness-normal{stroke-width:2px}#mermaid-svg-LnXl96nwWtNZ1uYc .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-LnXl96nwWtNZ1uYc .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-LnXl96nwWtNZ1uYc .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-LnXl96nwWtNZ1uYc .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-LnXl96nwWtNZ1uYc .marker{fill:#333}#mermaid-svg-LnXl96nwWtNZ1uYc .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-LnXl96nwWtNZ1uYc { color: rgba(0, 0, 0, 0.75); font: ; }
打开数据库
初始化读取
读取上次索引信息
写入当次索引+偏移
写入索引量last+1
int Insert(void *data, db_context *context)
{
	if (context->v_nindex v_tindex)
	{
		FILE * fp = fopen(context->v_tablename, "wb");
		if (fp == NULL)
			return -1;


		fpos_t pos = 4 + 4 + (context->v_nindex) * (8);
		fsetpos(fp, &pos);
		//int last_index = context->v_nindex - 1;
		uint32_t  indexlast = context->v_index[context->v_nindex - 1].index ;
		uint32_t  indexinsert = indexlast + 1;
		size_t n = fwrite(&indexinsert, sizeof(uint32_t), 1, fp);
		if (n != sizeof(uint32_t))
		{
			fclose(fp);
			return -1;
		}

		sdata *pdata = (sdata*)data;

		size_t vsize = sizeof(pdata->vardata);
		uint32_t datasize = vsize * indexlast;
		fpos_t offset = 4 + 4 + INDEX_BYTES + datasize;
		n = fwrite(&offset, sizeof(uint32_t), 1, fp);
		if (n != sizeof(uint32_t))
		{
			fclose(fp);
			return -1;
		}

		fsetpos(fp, &offset);
		n = fwrite(data, vsize, 1, fp);
		if (n != vsize)
		{
			fclose(fp);
			return -1;
		}
		fpos_t index = sizeof(uint32_t);
		fsetpos(fp, &index);
		uint32_t newnindex = context->v_nindex + 1;
		n = fwrite(&newnindex, sizeof(uint32_t), 1, fp);
		if (n != sizeof(int))
		{
			fclose(fp);
			return -1;
		}
		fclose(fp);
		return 0;
	}
	return -1;
}


int   Delete(void *key, db_context *v_context)
{

}
int   Update(void *key, void *data, db_context *v_context)
{

}
void *Select(void *key, db_context *v_context)
{

}

上面说了,先实现增加函数,其他先不实现,也不测试,单元测试需要一个严格的定义和测试用例,此次先为增加流程的初步代码实现,有待完善。

关注
打赏
1663161521
查看更多评论
立即登录/注册

微信扫码登录

0.0410s