资源简介
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 < hash_item.count; 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++数据结构编写的航空订票系统
相关资源
- C++获取计算机的CPU ID,硬盘序列号等
- C++头文件转delphi工具 + 源码
- C语言编程常见问题解答.pdf
- GD32通过规则组寄存器 DMA获取多组AD
- 基于MFC的TCP调试助手源码95706
- 国际象棋的qt源代码
- 操作系统c语言模拟文件管理系统844
- C语言开发实战宝典
- C++中头文件与源文件的作用详解
- 基于mfc的多线程文件传输
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- C语言代码高亮html输出工具
- 猜数字游戏 c语言代码
- C语言课程设计
- 数字电位器C语言程序
- CCS FFT c语言算法
- 使用C语言编写的病房管理系统
- 通信过程中的RS编译码程序(c语言)
- 利用C++哈希表的方法实现电话号码查
- 计算机二级C语言上机填空,改错,编
- 用回溯法解决八皇后问题C语言实现
- 移木块游戏,可以自编自玩,vc6.0编写
- 简易教务管理系统c语言开发文档
- 操作系统课设 读写者问题 c语言实现
- 小波变换算法 c语言版
- C流程图生成器,用C语言代码 生成C语
- 3des加密算法C语言实现
- 简单的C语言点对点聊天程序
- 单片机c语言源程序(51定时器 八个按
评论
共有 条评论