一个基于Key/Value方式存取二进制数据的接口设计

  • 支持对单个条目进行加密
  • 增删改查方法
  • 辅助方法
  • 能够使用以下5种方式进行序列化
    • "SQLITE" Sqlite数据库
    • "XML" XML文件
    • "INI" INI文件
    • "DIR" 文件系统(目录结构)
    • "REG" Windows注册表
  • 支持拷贝
  • 完整的错误处理
  • 支持尽可能多的STL容器类型作为buf
  • 线程安全(可选)

C++

要求能通过不低于此测试用例强度的单元测试:http://git.koal.com/chenlei/blobstore/blob/master/blobstore/blobstore_test.cc

BlobStore 使用范例(C++伪代码)


XMLBlobStore xml_store("/path/to/xml"); char in_buf1[] = "11111111111111111111111111";
std::string in_buf2 = "222222222222222222222222";
std::vector<char> in_buf3(in_buf1, in_buf1 + sizeof(in_buf1)); // 设置明文
xml_store.setBlob("testkey1", in_buf1, sizeof(in_buf1));
xml_store.setBlob("testkey2", in_buf2);
xml_store.setBlob("testkey3", in_buf3)
// 以明文方式取出
char out_buf1[MAX];
std::string out_buf2;
std::vector<char> outbuf2;
xml_store.getBlob("testkey1", out_buf1, sizeof(out_buf1));
xml_store.getBlob("testkey2", out_buf2);
xml_store.getBlob("testkey2", out_buf3); // 设置密文
xml_store.setEncryptedBlob("testkey1", passwd, in_buf1, sizeof(in_buf1));
xml_store.setBlob("testkey2", passwd, in_buf2, in_buf2.size());
// 以密文方式取出
char out_buf1[MAX];
std::string out_buf2;
xml_store.getBlob("testkey1", passwd, out_buf1, sizeof(out_buf1));
xml_store.getBlob("testkey2", passwd, out_buf2); // 辅助方法
std::vector<std::string> keys = xml_store.keys();
bool has_key = xml_store.hasKey("testkey1");
bool is_encryped = xml_store.isEncrypted("testkey1"); // 序列化
xml_store.load(); // 从XML文件加载
xml_store.save(); // 序列化到XML文件 // 拷贝
XMLBlobStore new_xml_store("/path/to/new/xml");
blobstore_copy(new_xml_store, xml_store);
new_xml_store.save(); DBBlobStore db_store("/path/to/db");
blobstore_copy(db_store, xml_store);
db_store.save();

一个可能的接口设计示例(C++)

//一个基于Key/Value方式存取二进制数据的接口设计,支持对单个条目进行加密

/* 支持加密的二进制数据存储 */
class BlobStore {
public:
/**
* 构造器
* @param type,支持以下4种
* "DIR" 文件系统(目录结构)
* "REG" Windows注册表
* "SQLITE" Sqlite数据库
* "XML" XML文件
* @param storePath,对应不同type的路径定义如下
* "DIR" 文件目录路径
* "REG" Windows注册表路径
* "SQLITE" Sqlite数据库文件路径
* "XML" XML文件路径
*/
static BlobStore createInstance(string type, string storePath); string getType(); /* 持久化 */
int load();
int save(); /* 将所有Key/Value复制到另一个Store(可以是不同类型的Store) */
int copyTo(BlobStore &other); public:
/* 遍历与查找 */
int size(); //获取容量
set<string> listAliases(); //获取名称列表
boolean containsAlias(string alias); //判断存在性
boolean isEncrypted(string alias); //判断条目是否加密
string findAlias(const vector<BYTE>&); //根据二进制内容查找名称 /* 明文数据操作 */
int getBlob(string alias, vector<BYTE> & );
int setBlob(string alias, const vector<BYTE>& ); /* 密文数据操作(固定使用SM4或AES256-CBC加密,密钥使用password的MD5摘要值) */
int getEncryptedBlob(string alias, string entryPassword, vector<BYTE> &);
int setEncryptedBlob(string alias, string entryPassword, const vector<BYTE>&); /* 删除 */
int deleteBlob(string alias);
int clearAll();
}

Java

要求使用JUNIT对以下所有接口进行单元测试(包括异常流)

BlobStore 使用范例(Java伪代码)


IBlobStore xs = BlobStoreFactory.createInstance("XML", "/path/to/xml"); String in1 = "11111111111111111111111111";
byte[] in2 = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; String passwd = "abcdefgh"; // 设置明文
xs.setBlob("testkey1", in1);
xs.setBlob("testkey2", in2); String out1 = xs.getBlobAsString("testkey1");
byte[] out2 = xs.getBlob("testkey2"); // 设置密文
xs.setEncryptedBlob("testkey1", passwd, in1);
xs.setEncryptedBlob("testkey2", passwd, in2); // 以密文方式取出
out1 = xs.getEncryptedBlobAsString("testkey1", passwd);
out2 = xs.getEncryptedBlob("testkey2", passwd); // 辅助方法
String[] keys = xs.listAliases();
boolean hasKey = xs.containsAlias("testkey1");
boolean isEncrypted = xs.isEncrypted("testkey2"); // 序列化
xs.load(); // 从XML文件加载
xs.save(); // 序列化到XML文件 // 拷贝
IBlobStore xs2 = BlobStore.createInstance("XML", "/path/to/new_xml");
xs.copyTo(xs2);
xs2.save(); IBlobStore ds = BlobStore.createInstance("DB", "/path/to/test.db");
xs.copyTo(xs2);
xs2.save();

一个可能的接口设计示例(Java)

interface IBlobStore {
public String getType(); /* 持久化 */
public int load();
public int save(); /* 将所有Key/Value复制到另一个Store(可以是不同类型的Store) */
public int copyTo(IBlobStore other); /* 遍历与查找 */
public int size(); //获取容量
public String[] listAliases(); //获取名称列表
public boolean containsAlias(String alias); //判断存在性
public boolean isEncrypted(String alias); //判断条目是否加密
public String findAlias(byte[] value); //根据二进制内容查找名称 /* 明文数据操作 */
public int setBlob(String alias, String value);
public int setBlob(String alias, byte[] value);
public byte[] getBlob(String alias);
public String getBlobAsString(String alias); /* 密文数据操作(固定使用SM4或AES256-CBC加密,密钥使用password的MD5摘要值) */
public int setEncryptedBlob(String alias, String entryPassword, String value);
public int setEncryptedBlob(String alias, String entryPassword, byte[] value);
public byte[] getEncryptedBlob(String alias, String entryPassword);
public String getEncryptedBlobAsString(String alias, String entryPassword); /* 删除 */
int deleteBlob(String alias);
int clearAll();
} class BlobStoreFactory { /**
* 构造器
* @param type,支持以下4种
* "DIR" 文件系统(目录结构)
* "REG" Windows注册表
* "SQLITE" Sqlite数据库
* "XML" XML文件
* @param storePath,对应不同type的路径定义如下
* "DIR" 文件目录路径
* "REG" Windows注册表路径
* "SQLITE" Sqlite数据库文件路径
* "XML" XML文件路径
*/
static IBlobStore createInstance(String type, String storePath) {
...
}
} class XMLBlobStore implements IBlobStore {
...
} class DbBlobStore implements IBlobStore {
...
}

Practice encryptedblobstore的更多相关文章

  1. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  2. Atitit 数据存储视图的最佳实际best practice attilax总结

    Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论  本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...

  3. The Practice of .NET Cross-Platforms

    0x01 Preface This post is mainly to share the technologies on my practice about the .NET Cross-Platf ...

  4. Exercise 24: More Practice

    puts "Let's practice everything." puts 'You\'d need to know \'bout escapes with \\ that do ...

  5. ConCurrent in Practice小记 (3)

    ConCurrent in Practice小记 (3) 高级同步技巧 Semaphore Semaphore信号量,据说是Dijkstra大神发明的.内部维护一个许可集(Permits Set),用 ...

  6. ConCurrent in Practice小记 (2)

    Java-ConCurrent2.html :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0 ...

  7. ConCurrent in Practice小记 (1)

    ConCurrent in Practice小记 (1) 杂记,随书自己写的笔记: 综述问题 1.线程允许在同一个进程中的资源,包括共享内存,内存句柄,文件句柄.但是每个进程有自己的程序计数器,栈和局 ...

  8. 1.2 基础知识——关于猪皮(GP,Generic Practice)

    摘要: 这是<CMMI快乐之旅>系列文章之一.说起猪皮(GP,Generic Practice),真的让人又爱又恨,中文翻译叫通用实践.CMMI标准中每个级别包含几个PA,每个PA又包含几 ...

  9. 2015年第2本(英文第1本):《The Practice of Programming》

    2015年计划透析10本英文原著,最开始选定的第一本英文书是<Who Moved my Cheese>,可是这本书实在是太短.太简单了,总体的意思就是要顺应变化,要跳出自己的舒适区,全文不 ...

随机推荐

  1. maven---工程建立及目录添加--

    刚开始建立maven工程目录1: 添加web能力: 选中工程鼠标右击点MyEclipse添加web能力: 然后: 关键点:remove掉Excluded:** 添加javaEE5库 确保web: 建p ...

  2. java 学习众多API和手册资源下载

    这个资源包里面有jdk api 还有SSH框架的手册,数据库的手册,Jquery手册等等,还有正则表达式等, 希望可以帮助有需要的人 链接:http://希望pan.baidu.com/s/1pJ60 ...

  3. Add lombok to IntelliJ IDEA

    Lombok study link: https://www.jianshu.com/p/365ea41b3573 Add below dependency code to pom.xml <d ...

  4. Angular07 路由的工作流程、路由参数、子路由、利用路由加载模块、模块懒加载???

    1 Angular路由的工作流程 用户在浏览器输入一个URL -> Angular将获取到这个URL并将其解析成一个UrlTree实例 -> Angular会到路由配置中去寻找并激活与Ur ...

  5. 微观SOA:服务设计原则及其实践方式

    大 量互联网公司都在拥抱SOA和服务化,但业界对SOA的很多讨论都比较偏向高大上.本文试图从稍微不同的角度,以相对接地气的方式来讨论SOA, 集中讨论SOA在微观实践层面中的缘起.本质和具体操作方式, ...

  6. 33、生鲜电商平台-定时器,定时任务quartz的设计与架构

    说明:任何业务有时候需要系统在某个定点的时刻执行某些任务,比如:凌晨2点统计昨天的报表,早上6点抽取用户下单的佣金. 对于Java开源生鲜电商平台而言,有定时推送客户备货,定时计算卖家今日的收益,定时 ...

  7. 深入浅出HTML PDF扫描版

    <深入浅出HTML>是一部讲述现代Web标准的优秀教程,彻底摒弃了过时的内容,始终贯彻三层分离的思想.书中结合实例讲述如何使用HTML.CSS设计符合现代Web标准的网页,并讲解了如何使用 ...

  8. Loadrunner监控服务器资源

    LoadRunner 加载监听服务器的步骤如下: 1.在 LoadRunner Controller 下,将工作面板切换到 Run状态,Available Graphs 栏 ,System Resou ...

  9. loj#6053. 简单的函数(Min_25筛)

    传送门 题解 \(Min\_25\)筛有毒啊--肝了一个下午才看懂是个什么东西-- \(zsy\)巨巨强无敌-- //minamoto #include<bits/stdc++.h> #d ...

  10. MCP|WJ|Identification of candidate plasma protein biomarkers for cervical cancer using the multiplex proximity extension assay(利用多重邻位延伸分析技术进行宫颈癌血浆蛋白候选生物标记物的鉴定研究)

    文献名:Identification of candidate plasma protein biomarkers for cervical cancer using the multiplex pr ...