-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCNetworkCache.m
144 lines (125 loc) · 5.54 KB
/
CNetworkCache.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 CNetworkCache
% Cache arbitrary files/dirs from one path to a local scratch path.
%
% Usage: (simple demo case)
%
% c = CNetworkCache('/tmp/cache/');
%
% % if out of date, copy files from networkmnt to /tmp/cache
% data = load(c.get('/networkmnt/files1'));
%
% Supports locking/unlocking for concurrent access from multiple machines.
% ======================================================================
% 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)
data_root
stream
seed
end
methods
function f = CNetworkCache(dir, seed)
f.data_root = dir;
if ~exist(dir, 'dir')
mkdir(dir);
end
if nargin==1
fid = fopen('/dev/urandom','r');
f.seed = sum(floor([fread(fid, 5)'/255*5]).*10.^[1:5]);
fclose(fid);
end
f.stream = RandStream('mt19937ar', 'Seed', f.seed);
end
function r = waitrandom(f, max_sec)
r = rand(f.stream, 1)*max_sec;
pause(r);
end
function cachepath = get(f, filepath)
f.waitrandom(0.1);
if ~exist(filepath, 'dir') && ~exist(filepath, 'file')
error('path ''%s'' does not exist', filepath);
end
[path name ext] = fileparts(filepath);
cachepath = fullfile(f.data_root, [name ext]);
cachelock = fullfile(f.data_root, [name '.cached']);
cachingfile = fullfile(f.data_root, [name '.caching']);
total_delay = 0;
while exist(cachingfile, 'file') && total_delay < 120
fprintf('waiting for cache...\n');
total_delay = total_delay + f.waitrandom(5);
end
if exist(cachingfile, 'file')
error('timeout in waiting for cache! removing lock...');
delete(cachingfile);
cachepath = f.get(filepath);
end
% check for already cached or updated version of file/dir
already_cached = exist(cachelock, 'file');
if already_cached && exist(cachepath, 'dir')
subfiles = dir(path); subfiles2 = dir(f.data_root);
file = subfiles(find(arrayfun(@(x)isequal(x.name, name), subfiles)));
file2 = subfiles2(find(arrayfun(@(x)isequal(x.name, name), subfiles2)));
if ~isequal(file.date, file2.date)
already_cached = false;
fprintf('cache is out of date: %s vs. %s\n', file.date, file2.date);
end
elseif already_cached && exist(cachepath, 'file')
file = dir(filepath); file2 = dir(cachepath);
if ~isequal(file.date, file2.date)
already_cached = false;
fprintf('cache is out of date: %s vs. %s\n', file.date, file2.date);
end
else
already_cached = false;
end
if already_cached
fprintf('loading from cache: %s\n', cachepath);
else
fprintf('creating new cache: %s...\n', cachepath);
f.waitrandom(5);
if exist(cachingfile, 'file')
fprintf('abort! someone else caching...\n');
cachepath = f.get(filepath);
else
tic;
unix(sprintf('rm -rf %s', cachepath));
unix(sprintf('rm -f %s', cachelock));
unix(sprintf('touch %s', cachingfile));
unix(sprintf('cp -raf %s %s', filepath, cachepath));
% if exist(filepath,'dir')
% unix(sprintf('rsync -atE %s/ %s/', filepath, cachepath));
% else
% unix(sprintf('rsync -atE %s %s', filepath, cachepath));
% end
unix(sprintf('touch %s', cachelock));
delete(cachingfile);
toc;
end
end
end
function clear(f, filepath)
[path name ext] = fileparts(filepath);
cachepath = fullfile(f.data_root, [name ext]);
unix(sprintf('rm -rf %s', cachepath));
end
end
end