📄 db.cpp
字号:
{
inout.seekg(i*pagesize,ios::beg);
inout.read(buff,pagesize);
for(int j=0;j<pagesize-pagecondition[i];j++)//search key from one block
{
youwant="";
memcpy(&mykeysize,&buff[j],4);
memcpy(&mydatasize,&buff[j+4+mykeysize],4);
for(int k=0;k<mykeysize;k++)// get one key from the buff
{
temp=buff[j+4+k];
youwant+=temp;
}
if (youwant==key.getstring())//compare two keys,if equal ,take related action
{
j+=key.totalsize();
youwant="";
for(int k=0;k<mydatasize;k++)//get the data youwant
{
temp=buff[j+4+k];
youwant+=temp;
}
//////////// change the attribute of DB
//my_key=key;
//my_data=youwant;
//pagecondition[8]=mykeyblock=i;
//pagecondition[9]=mykeyoffset=j-key.totalsize();
////////////
for(k=j-key.totalsize();k<(pagesize-pagecondition[i]);k++)//delete key,move ahead
buff[k]=buff[k+key.totalsize()+youwant.size()+4];
inout.seekp(i*pagesize,ios::beg);
pagecondition[i]+=key.totalsize()+youwant.size()+4;
inout.write(&buff[0],pagesize-pagecondition[i]);
// block management
if(pagecondition[i]==pagesize)
{
i++;
for(;i<pageno;i++)// search key from different block
{
inout.seekg((i)*pagesize,ios::beg);
inout.read(buff,pagesize-pagecondition[i]);// change pagesize-pagecondition[i] to pagesize is wrong
inout.seekp((i-1)*pagesize,ios::beg);
inout.write(&buff[0],pagesize-pagecondition[i]);
pagecondition[i-1]=pagecondition[i];
}
pageno--;
pagecondition[pageno]=pagesize;
pagecondition[7]=pageno;
}
cout<<"delete "<<key<<" success!"<<endl;
cout<<key<<" "<<youwant<<endl<<endl;
//check node check
// for(int i=0;i<=9;i++)
// {
// cout<<pagecondition[i]<<" ";
// }
// cout<<endl<<buff;
cout<<endl<<endl;
//////check node check
inout.close();
db_close();
return 1;
}//end if
else
j+=8+mykeysize+mydatasize-1;
}//end for j
}//end for i
inout.close();//close the file
cout<<"don't find "<<key<<", so we can't delete it "<<endl<<endl;//output to the screen
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// db_firstkey()
////////////////////////////////////////////////////////////////////////////////////////////////////////
DBT DB::db_firstkey()
{ if(my_data=="")
{
cout<<"no data in the database!"<<endl;
return my_data;
}
else
{
fstream inout(dbname.data(),ios::binary|ios::in|ios::out);
//find the key
inout.seekg(0,ios::beg);
inout.read(buff,pagesize);
string youwant="",temp;
int mykeysize;
int mydatasize;
memcpy(&mykeysize,&buff[0],4);
memcpy(&mydatasize,&buff[4+mykeysize],4);
for(int k=0;k<mykeysize;k++)// get the first key
{
temp=buff[k+4];
youwant+=temp;
}
my_key=youwant;
youwant=""; // note here
for(k=0;k<mydatasize;k++)
{
temp=buff[k+8+mykeysize];
youwant+=temp;
}
my_data=youwant;
pagecondition[8]=mykeyblock=0;//store my_key block;
pagecondition[9]=mykeyoffset=0;//store my_key offset;
db_close();
cout<<my_key<<" "<<my_data<<endl;
return my_key;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//// db_nextkey()
///////////////////////////////////////////////////////////////////////////////////////////////////////
DBT DB::db_nextkey()
{ //
int i=pagecondition[8];
int j=pagecondition[9]+my_key.totalsize()+my_data.totalsize();
string youwant="",temp;
int mykeysize;
int mydatasize;
if(j==(pagesize-pagecondition[i])) // judge whethere to the end of database
{
if(i==(pageno-1))
{
cout<<"no key in the database already!!"<<endl;
return my_key;
}
i++;
j=0;
}
fstream inout(dbname.data(),ios::binary|ios::in|ios::out);
//find the key
inout.seekg(i*pagesize,ios::beg);
inout.read(buff,pagesize);
memcpy(&mykeysize,&buff[j],4);
memcpy(&mydatasize,&buff[4+mykeysize+j],4);
for(int k=0;k<mykeysize;k++)// get the next key
{
temp=buff[k+4+j];
youwant+=temp;
}
my_key=youwant;
youwant=""; // note here
for(k=0;k<mydatasize;k++)// get the next data
{
temp=buff[k+8+mykeysize+j];
youwant+=temp;
}
my_data=youwant;
pagecondition[8]=mykeyblock=i;//store my_key block;
pagecondition[9]=mykeyoffset=j;//store my_key offset;
db_close();
cout<<my_key<<" "<<my_data<<endl;
return my_key;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
///// db_print()
//////////////////////////////////////////////////////////////////////////////////////////////////////////
int DB::db_print()
{
cout<< "print all the data:"<<endl;
if(my_data.getstring()=="")
{
cout<<"no data in the database !"<<endl;
return 0;
}
else
{
DBT p=db_firstkey(),q;
while(p.getstring()!=q.getstring())
{ // cout<<p<<endl; //////////////////////////////////////////
q=p;
p=db_nextkey();
}
return 1;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////// int db_createindex(string& indexName)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int DB::db_createindex(DBT& indexkey,int indexblock)
{
//initiate
string youwant="",temp;
int keysize;
int pos=0; //the position youwant to insert
if(pagecondition[6]<indexkey.totalsize()+4)
{
cout<< "there is no room for the index!"<<endl;
return 0;
}
else
{
while(pos<(pagesize-pagecondition[6]))//// look for the position you want to insert
{
memcpy(&keysize,&indexbuff[pos+4],4);
// get the key and compare
for(int k=0;k<keysize;k++)
{
temp=indexbuff[pos+8+k];
youwant+=temp;
}
if(indexkey.getstring()<youwant) break;
else
{
pos+=(8+keysize);
}
}
}
//for(int k=0;k<pagesize-pagecondition[6]-pos;k++)
// {
// indexbuff[pos+indexkey.totalsize()+k]=indexbuff[pos+k];
// }
memcpy(&indexbuff[pos+indexkey.totalsize()+4],&indexbuff[pos],pagesize-pagecondition[6]-pos);
memcpy(&indexbuff[pos],&indexblock,4);
memcpy(&indexbuff[pos+4],(char*)indexkey,indexkey.totalsize());
//memcpy(&indexbuff[pos+indexkey.totalsize()],(char*)indexkey,indexkey.totalsize());
//n memcpy(&indexbuff[pos+indexkey.totalsize()],&indexblock,4);
cout<<indexkey<<" index have been created !!"<<endl<<endl;
pagecondition[6]-=indexkey.totalsize()+4;
db_close();
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////db_dropindex(DBT& indexName)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int DB::db_dropindex(DBT& indexName)
{
//initiate
string youwant="",temp;
int keysize;
int pos=0; //the position youwant to delete index
while(pos<(pagesize-pagecondition[6]))//// look for the position you want to insert
{
memcpy(&keysize,&indexbuff[pos+4],4);
// get the key and compare
for(int k=0;k<keysize;k++)
{
temp=indexbuff[pos+8+k];
youwant+=temp;
}
if(indexName.getstring()==youwant) break;
else
{
pos+=(8+keysize);
}
}// end while
memcpy(&indexbuff[pos],&indexbuff[pos+8+keysize],pagesize-pagecondition[6]-pos);
cout<<indexName<<" index have been delete !!"<<endl<<endl;
pagecondition[6]+=indexName.totalsize()+4;
db_close();
return 1;
}
DB::db_close()
{
memcpy(buff,pagecondition,40);
memcpy(&buff[40],(char*)my_key,my_key.totalsize());
memcpy(&buff[40+my_key.totalsize()],(char*)my_data,my_data.totalsize());
ofstream indexout(index_name.data(),ios::binary|ios::out);
if(!indexout)
cout<<"sorry,close db failure "<<endl;
else
{
indexout.write(buff,pagesize);
indexout.write(indexbuff,pagesize);
}
indexout.close();
}
//////////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -