-
Notifications
You must be signed in to change notification settings - Fork 1
/
CFileCache.m
143 lines (121 loc) · 4.68 KB
/
CFileCache.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
classdef CFileCache < handle
% Manage caching and retrieval of various precomputations
%
% Usage:
%
% (simple usage demo case)
%
% cache = CFileCache('/tmp/mywork');
% cache.register('data1', @runDataFunction1);
% cache.register('data2', @runDataFunction2);
%
% % will run computation this time
% data1 = cache.get('file1', 'data1');
% % will load from cache this time
% data1 = cache.get('file1', 'data1');
%
% ======================================================================
% Copyright (c) 2012 David Weiss
%
% Permission is hereby granted, free of charge, to any person obtaining
% a copy of this software and associated documentation files (the
% "Software"), to deal in the Software without restriction, including
% without limitation the rights to use, copy, modify, merge, publish,
% distribute, sublicense, and/or sell copies of the Software, and to
% permit persons to whom the Software is furnished to do so, subject to
% the following conditions:
%
% The above copyright notice and this permission notice shall be
% included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
% LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
% OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
% ======================================================================
properties (Hidden = true)
funcmap
namefield = 'name';
funclist = {};
end
properties
data_root
end
methods
function f = CFileCache(dir, namefield)
f.data_root = dir;
f.funcmap = java.util.HashMap();
f.funclist = {};
if nargin>1
f.namefield = namefield;
end
if ~exist(f.data_root, 'dir')
unixf('mkdir -p %s', f.data_root); %%error('directory ''%s'' does not exist', f.data_root);
end
end
function register(f, name, func)
f.funcmap.put(f.getName(name), numel(f.funclist)+1);
f.funclist{end+1} = func;
end
function [name] = getName(f, name)
if isstruct(name)
name = name.(f.namefield);
end
end
function [list] = registered(f)
it = f.funcmap.keySet().iterator();
list = {};
while it.hasNext() == 1
list{end+1} = char(it.next());
end
end
function [filename subdir] = getFilename(f, name, target)
name = f.getName(name);
[~,cname,~] = fileparts(name); % strip original filename
hashcode = mod(sum(double(name).*[1:cols(name)])*11, 676);
subdir = char(97 + [floor(hashcode/26) hashcode-26*floor(hashcode/26)]);
filename = fullfile(f.data_root, [subdir '/' cname '-' target '.mat']);
end
function set(f, name, target, data)
filename = f.getFilename(name, target);
fprintf('caching to: ''%s''\n', filename);
savedir = fileparts(filename);
if ~exist(savedir, 'dir')
mkdir(savedir);
end
if isstruct(data) && numel(data) == 1
save(filename, '-struct', 'data');
else
save(filename, 'data');
end
end
function data = get(f, name, target, varargin)
filename = f.getFilename(name, target);
if exist(filename,'file')
%fprintf('cache exists, loading from: ''%s''\n', filename);
data = loadvar(filename);
else
idx = f.funcmap.get(target);
if isempty(idx)
error('no function registered for ''%s''', target);
end
func = f.funclist{idx};
data = func(f, name, varargin{:});
f.set(name, target, data);
data = loadvar(filename);
end
end
function clear(f, name, target)
filename =f.getFilename(name, target);
fprintf('clearing ''%s''\n', filename);
if exist(filename,'file')
delete(filename);
end
end
end
methods (Hidden = true)
end
end