云备份客户端——数据管理模块

数据管理模块设计之前,我们需要先明确该模块的信息是用来做什么的。根据上文分析该模块信息主要用于判断一个文件是否需要备份,判断条件有两个:1.新增文件 2.被修改过的文件
新增文件好判断,由于我们获得新文件后是先上传文件,再将文件信息插入到数据管理模块中,模块里没有存储该文件信息,则该文件为新增文件
而第二个判断修改文件,我们会设定一个文件的唯一标识,类似于之前服务端构建http协议的ETag关键字,该标识由 文件名-文件大小-文件最后一次修改时间 构成

由上可得,我们文件信息表中需要存储的信息只有两个:文件名-唯一标识

因此内存中存储文件信息表我们选择用unordered_map存储这对KV(文件名-文件唯一标识)值

而对于持久化存储,关键点在于自己完成序列化和反序列化,不过由于存储信息简单因此序列化和反序列化也比较简单,我们序列化方式如下

反序列化则是写一个字符串拆分函数,先拆除一个一个的KV键值对,在将KV值分离

因此,我们接口设置如下

cpp 复制代码
#pragma once
#include "util.hpp"
#include <unordered_map>
namespace mjw_cloud
{
	class FileDatas
	{
	public:
		FileDatas(const std::string& backup_file)
			:_backup_file(backup_file)
		{}
		
		bool InitTable()
		{}
		bool Storage()
		{}
		bool Insert(const std::string& key, const std::string& val)
		{}
		bool Updata(const std::string& key, const std::string& val)
		{}
		bool GetoneByKey(const std::string& key, std::string* val)
		{}
	private:
		//解析序列化字符串时需要
		//字符串分割,对序列化字符串进行分割
		//"key val key" -> "key" "val" "key"
		int Split(const std::string& str, const std::string& seq, std::vector<std::string>* arry)
		{}
	private:
		std::unordered_map<std::string, std::string> _table;//文件信息表
		std::string _backup_file;//备份文件信息 存储文件
	};
}

代码实现如下:

cpp 复制代码
#pragma once
#include "util.hpp"
#include <unordered_map>
namespace mjw_cloud
{
	class FileDatas
	{
	public:
		FileDatas(const std::string& backup_file)
			:_backup_file(backup_file)
		{

			InitTable();
		}
		
		bool InitTable()
		{
			//1.从文件中读取备份文件信息序列化字符串
			std::string body;
			FileUtil fu(_backup_file);
			fu.GetContent(&body);
			if (body.empty()) return true;
			//2.对字符串进行反序列化解析
			std::vector<std::string> arry;
			//"key val\nkey val\n" -> "key val" "key val"
			Split(body, "\n", &arry);
			for (auto a : arry)
			{
				std::vector<std::string> tmp;
				//"key val" -> "key" "val"
				Split(a, " ", &tmp);
				if (tmp.size() != 0) continue;
				_table[tmp[0]] = tmp[1];
			}
			return true;
		}
		bool Storage()
		{
			if (_table.empty()) return true;
			//1.构建序列化字符串
			std::string body;
			for (auto& t : _table)
			{
				body += t.first + " " + t.second + "\n";
			}
			//2.将字符串写入指定文件
			FileUtil fu(_backup_file);
			fu.SetContent(body);
			return true;
		}
		bool Insert(const std::string& key, const std::string& val)
		{
			_table[key] = val;
			return true;
		}
		bool Updata(const std::string& key, const std::string& val)
		{
			_table[key] = val;
			return true;
		}
		bool GetoneByKey(const std::string& key, std::string* val)
		{
			auto it = _table.find(key);
			if (it == _table.end())
			{
				return false;
			}
			*val = _table[key];
			return true;
		}
	private:
		//解析序列化字符串时需要
		//字符串分割,对序列化字符串进行分割
		//"key val key" -> "key" "val" "key"
		int Split(const std::string& str, const std::string& seq, std::vector<std::string>* arry)
		{
			int count = 0;
			int pos = 0, idx = 0;
			while (idx < str.size())
			{
				pos = str.find(seq, idx);
				if (pos == std::string::npos) break;
				arry->push_back(str.substr(idx, pos - idx));
				idx = pos + 1;
				count++;
			}

			if (idx < str.size())
			{
				//说明str还有最后一截字符串没有push_back进arry
				arry->push_back(str.substr(idx));
				count++;
			}
			return count;
		}
	private:
		std::unordered_map<std::string, std::string> _table;//文件信息表
		std::string _backup_file;//备份文件信息 存储文件
	};
}
相关推荐
君鼎4 小时前
C++设计模式——单例模式
c++·单例模式·设计模式
不知几秋4 小时前
数字取证-内存取证(volatility)
java·linux·前端
刚入门的大一新生5 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
小冯的编程学习之路6 小时前
【软件测试】:推荐一些接口与自动化测试学习练习网站(API测试与自动化学习全攻略)
c++·selenium·测试工具·jmeter·自动化·测试用例·postman
欧先生^_^7 小时前
Linux内核可配置的参数
linux·服务器·数据库
C++ 老炮儿的技术栈7 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
海尔辛7 小时前
学习黑客5 分钟读懂Linux Permissions 101
linux·学习·安全
猪八戒1.07 小时前
C++ 回调函数和Lambda表达式
c++
源远流长jerry8 小时前
匿名函数lambda、STL与正则表达式
c++
王RuaRua8 小时前
[数据结构]5. 栈-Stack
linux·数据结构·数据库·链表