-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyValDb.m
109 lines (100 loc) · 3.52 KB
/
KeyValDb.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
%
% Copyright (c) 2018 James Pritts, Denys Rozumnyi, CTU in Prague
% Licensed under the MIT License (see LICENSE for details)
%
% Written by James Pritts and Denys Rozumnyi
%
classdef KeyValDb < handle
properties
db
end
methods (Static)
function obj = getObj(varargin)
persistent localObjKeyValDb;
if isempty(localObjKeyValDb) || ~isvalid(localObjKeyValDb)
localObjKeyValDb = KeyValDb(varargin{:});
end
obj = localObjKeyValDb;
end
end
methods (Access = private)
function this = KeyValDb(varargin)
p = inputParser;
dbfile = fullfile([pwd '/features.db']);
addOptional(p,'dbfile',dbfile);
parse(p,varargin{:});
if ~exist(p.Results.dbfile,'file')
try
this.db = sqlite(p.Results.dbpath,'create');
exec(this.db, ...
['CREATE TABLE `cid_table` (`k` TEXT NOT NULL, `v` ' ...
'BLOB NOT NULL, PRIMARY KEY(`k`))']);
catch
disp('Database does not exist. Could not create the database.');
end
else
this.db = sqlite(p.Results.dbfile,'connect');
end
end
end
methods
% function cid = put_img(this,url)
% filecontents = get_native_img(url);
% cid = HASH.hash(filecontents,'MD5');
% this.put('image',cid,'raw',filecontents);
% end
%
% function img = get_img(this,cid)
% img = [];
% has_img = this.check('image',cid,'raw');
% if has_img
% filecontent = this.get('image',cid,'raw');
% try
% img = readim(filecontent);
% catch
% if exist('/tmp','dir') == 7
% tmpurl = ['/tmp/tmpimpng' cid];
% else
% tmpurl = ['tmpimpng' cid];
% end
% fid = fopen(tmpurl,'w');
% fwrite(fid,filecontent);
% fclose(fid);
% img = imread(tmpurl);
% delete(tmpurl);
% end
% end
% end
function [] = put(this,table,cid,key,data)
k = KEY.hash([cid ':' key],'MD5');
v = hlp_serialize(data);
row = cell2table({k,char(v)},'VariableNames',{'k','v'});
insert(this.db,'cid_table', {'k','v'}, row);
end
function [data,is_found] = get(this,table,cid,key)
is_found = false;
data = [];
k = KEY.hash([cid ':' key],'MD5');
v = fetch(this.db, ...
['SELECT * FROM cid_table WHERE k like ''' k '''']);
if ~isempty(v)
is_found = true;
data = hlp_deserialize(v{2});
end
end
function [] = remove(this,table,cid,key)
k = KEY.hash([cid ':' key],'MD5');
exec(this.db, ...
['DELETE FROM cid_table WHERE k like ''' k '''']);
end
function is_found = check(this,table,cid,key)
is_found = false;
k = KEY.hash([cid ':' key],'MD5');
v = fetch(this.db, ...
['SELECT count(1) FROM cid_table WHERE k like ''' k '''']);
if v{1} == 1
is_found = true;
end
end
end
end