資源簡介
DBMS 小型數據庫 c語言實現 增刪改查基本功能

代碼片段和文件信息
//
//?Created?by?apple?on?2017/7/21.
//
#include?“database.h“
Database::Database(const?std::string?&db_name)
{
????if?(db_name?!=?““)?{?open(db_name);?}
}
Database::~Database(void)
{
????close();
}
inline?Offset?Database::_hash(const?std::string?&key)
{
????uint32_t?seed?=?13;?//?31?131?1313?13131?131313?etc..
????uint32_t?hash_val?=?0;
????uint32_t?mask?=?hash_table_size?-?1;
????
????for?(char?ch?:?key)?{?hash_val?=?hash_val?*?seed?+?ch;?}
????
????return?hash_val?%?mask;
}
void?Database::open(const?std::string?&db_name)
{
????close();
????_index_file.open(db_name?+?index_file_suffix?std::ios::binary?|?std::ios::in?|?std::ios::out);
????_data_file.open(db_name?+?data_file_suffix?std::ios::binary?|?std::ios::in?|?std::ios::out);
}
void?Database::close(void)
{
????_index_file.close();
????_data_file.close();
}
void?Database::insert(const?std::string?&key?const?std::string?&data)
{
????if?(!_index_file.is_open()?||?!_data_file.is_open())?{?return;?}
????
????Offset?hash_offset?=?_hash(key)?*?sizeof(HashItem);
????HashItem?hash_item?=?_read_hash_item(hash_offset);
????
????if?(_get_index_offset(hash_item?key)?!=?-1)?{?return;?}
????
????_data_file.seekp(0?std::ios::end);
????
????HashItem?hash_head?=?_read_hash_item(0);
????IndexInfo?index_info?=?{?.next?=?hash_item.first?.key_len?=?(Size)(key.size()?+?1)?};
????Offset?index_offset?=?hash_head.first;
????Offset?data_offset?=?_data_file.tellp();
????DataInfo?data_info?=?{?.data_len?=?(Size)(data.length()?+?1)?.deleted?=?0?};
????
????_write_index_info(index_offset?index_info);
????_write_data_offset(index_offset?data_offset);
????_write_key(index_offset?key);
????_write_data_info(data_offset?data_info);
????_write_data(data_offset?data);
????
????hash_head.first?=?_index_file.tellp();
????_write_hash_item(0?hash_head);
????
????hash_item.count++;
????hash_item.first?=?index_offset;
????_write_hash_item(hash_offset?hash_item);
}
void?Database::remove(const?std::string?&key)
{
????if?(!_index_file.is_open()?||?!_data_file.is_open())?{?return;?}
????
????Offset?hash_offset?=?_hash(key)?*?sizeof(HashItem);
????HashItem?hash_item?=?_read_hash_item(hash_offset);
????if?(hash_item.count?==?0)?{?return;?}
????
????IndexInfo?index_info;
????Offset?prev_index_offset?=?-1;
????Offset?curr_index_offset?=?hash_item.first;
????
????bool?found?=?false;
????
????for?(int?i?=?0;?i?????{
????????index_info?=?_read_index_info(curr_index_offset);
????????std::string?index_key?=?_read_key(curr_index_offset?index_info.key_len);
????????
????????if?(index_key?==?key)
????????{
????????????found?=?true;
????????????break;
????????}
????????
????????prev_index_offset?=?curr_index_offset;
????????curr_index_offset?=?index_info.next;
????}
????
????if?(found)
????{
????????Offset?data_offset?=?_read_data_offset(curr_index_offset);
????????DataInfo?data_info?=?_read_data_info(data_offset);
????????
????????data_info.deleted?=?1;
????????_write_data_info(data_offset?data_info);
??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-07-23?21:08??Databa
?????文件?????????232??2017-07-23?21:08??Databa
?????目錄???????????0??2018-02-28?11:58??__MACOSX\
?????目錄???????????0??2018-02-28?11:58??__MACOSX\Databa
?????文件?????????216??2017-07-23?21:08??__MACOSX\Databa
?????文件????????9870??2017-07-23?21:08??Databa
?????文件?????????216??2017-07-23?21:08??__MACOSX\Databa
?????文件????????2467??2017-07-23?21:08??Databa
?????文件?????????216??2017-07-23?21:08??__MACOSX\Databa
?????文件????????2020??2017-07-23?21:08??Databa
?????文件?????????216??2017-07-23?21:08??__MACOSX\Databa
?????文件??????????49??2017-07-23?21:08??Databa
?????文件?????????216??2017-07-23?21:08??__MACOSX\Databa
?????文件?????????216??2017-07-23?21:08??__MACOSX\._Databa
- 上一篇:libtiff頭文件及庫
- 下一篇:基于C++數據結構編寫的航空訂票系統
評論
共有 條評論