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

news/2024/10/18 0:18:40/

数据管理模块设计之前,我们需要先明确该模块的信息是用来做什么的。根据上文分析该模块信息主要用于判断一个文件是否需要备份,判断条件有两个:1.新增文件 2.被修改过的文件

新增文件好判断,由于我们获得新文件后是先上传文件,再将文件信息插入到数据管理模块中,模块里没有存储该文件信息,则该文件为新增文件

而第二个判断修改文件,我们会设定一个文件的唯一标识,类似于之前服务端构建http协议的ETag关键字,该标识由 文件名-文件大小-文件最后一次修改时间 构成

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

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

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

 

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

 

因此,我们接口设置如下

#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;//备份文件信息 存储文件};
}

代码实现如下:

#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进arryarry->push_back(str.substr(idx));count++;}return count;}private:std::unordered_map<std::string, std::string> _table;//文件信息表std::string _backup_file;//备份文件信息 存储文件};
}


http://www.ppmy.cn/news/1095849.html

相关文章

Fiber 架构实现流程

Fiber 架构是 React 中用于实现虚拟 DOM 的一种算法架构。它的目标是提高渲染性能和用户体验&#xff0c;通过将渲染工作分割成多个小任务&#xff0c;在不阻塞主线程的情况下逐步完成整个渲染过程。 下面是 Fiber 架构的简要实现过程&#xff1a; 构建 Fiber 树&#xff1a;在…

ffmpeg-android studio创建jni项目

一、创建native项目 1.1、选择Native C 1.2、命名项目名称 1.3、选择C标准 1.4、项目结构 1.5、app的build.gradle plugins {id com.android.application }android {compileSdk 32defaultConfig {applicationId "com.anniljing.ffmpegnative"minSdk 25targetSdk 32…

TheRouter 框架原理

TheRouter 框架入口方法 通过InnerTheRouterContentProvider 注册在AndroidManifest.xml中&#xff0c;在应用启动时初始化 <application><providerandroid:name"com.therouter.InnerTheRouterContentProvider"android:authorities"${applicationId}.…

决策单调性优化dp

区间类: P1880 [NOI1995] 石子合并 f i , j m a x ( f i , k f k 1 , j ) w i , j f_{i,j}max(f_{i,k}f_{k1,j})w_{i,j} fi,j​max(fi,k​fk1,j​)wi,j​ 若 w i , j w_{i,j} wi,j​满足区间单调性和四边形不等式&#xff0c;则 f i , j f_{i,j} fi,j​满足四边形不等式 …

springboot 请求https的私有证书验证

一、方案描述 我这里采用RestTemplate的方式调用https请求&#xff0c;请求第三方接口获取数据&#xff0c;证书由第三方私自签发的证书&#xff0c;我们构建的是一个springboot的API项目。 1.pom文件引入jar <dependencies><dependency><groupId>org.spr…

OmniGraffle Pro for Mac 中文正式版(附注册码) 苹果电脑 思维导图软件

OmniGraffle Pro是OmniGraffle的高级版本&#xff0c;它提供了更多的功能和工具&#xff0c;可以帮助用户创建更为复杂和高级的图表和流程图。OmniGraffle Pro支持自定义形状、图形、线条和箭头等&#xff0c;可以让用户创建出更加精细的图表。此外&#xff0c;OmniGraffle Pro…

《热题100》字符串、双指针、贪心算法篇

思路&#xff1a;对于输入的的字符串&#xff0c;只有三种可能&#xff0c;ipv4,ipv6,和neither ipv4:四位&#xff0c;十进制&#xff0c;无前导0&#xff0c;小于256 ipv6:八位&#xff0c;十六进制&#xff0c;无多余0&#xff08;00情况不允许&#xff09;&#xff0c;不…

Kubernetes禁止调度

在Kubernetes中&#xff0c;您可以通过几种方式来禁止某个Pod调度到节点上。以下是一些方法&#xff1a; Node Selector&#xff1a;您可以使用Node Selector来限制Pod只能调度到带有特定标签的节点上。如果您希望完全禁止Pod调度到某些节点上&#xff0c;可以确保这些节点不拥…