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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2007-2020, VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(rdf_cache,
[ rdf_set_cache_options/1, % +Options
rdf_cache_file/3 % +URL, +RW, -File
]).
:- autoload(library(error),[must_be/2,domain_error/2]).
:- autoload(library(filesex),[make_directory_path/1]).
/** <module> Cache RDF triples
The library library(semweb/rdf_cache) defines the caching strategy for
triples sources. When using large RDF sources, caching triples greatly
speedup loading RDF documents. The cache library implements two caching
strategies that are controlled by rdf_set_cache_options/1.
*|Local caching|* This approach applies to files only. Triples are
cached in a sub-directory of the directory holding the source. This
directory is called =|.cache|= (=|_cache|= on Windows). If the cache
option =create_local_directory= is =true=, a cache directory is created
if posible.
*|Global caching|* This approach applies to all sources, except for
unnamed streams. Triples are cached in directory defined by the cache
option =global_directory=.
When loading an RDF file, the system scans the configured cache files
unless cache(false) is specified as option to rdf_load/2 or caching is
disabled. If caching is enabled but no cache exists, the system will try
to create a cache file. First it will try to do this locally. On failure
it will try to configured global cache.
*/
:- dynamic
cache_option/1.
set_setfault_options :-
assert(cache_option(enabled(true))),
( current_prolog_flag(windows, true)
-> assert(cache_option(local_directory('_cache')))
; assert(cache_option(local_directory('.cache')))
).
:- set_setfault_options. % _only_ when loading!
%! rdf_set_cache_options(+Options)
%
% Change the cache policy. Provided options are:
%
% * enabled(Boolean)
% If =true=, caching is enabled.
%
% * local_directory(Name).
% Plain name of local directory. Default =|.cache|=
% (=|_cache|= on Windows).
%
% * create_local_directory(Bool)
% If =true=, try to create local cache directories
%
% * global_directory(Dir)
% Writeable directory for storing cached parsed files.
%
% * create_global_directory(Bool)
% If =true=, try to create the global cache directory.
rdf_set_cache_options([]) :- !.
rdf_set_cache_options([H|T]) :-
!,
rdf_set_cache_options(H),
rdf_set_cache_options(T).
rdf_set_cache_options(Opt) :-
functor(Opt, Name, Arity),
arg(1, Opt, Value),
( cache_option(Name, Type)
-> must_be(Type, Value)
; domain_error(cache_option, Opt)
),
functor(Gen, Name, Arity),
retractall(cache_option(Gen)),
expand_option(Opt, EOpt),
assert(cache_option(EOpt)).
cache_option(enabled, boolean).
cache_option(local_directory, atom).
cache_option(create_local_directory, boolean).
cache_option(global_directory, atom).
cache_option(create_global_directory, boolean).
expand_option(global_directory(Local), global_directory(Global)) :-
!,
absolute_file_name(Local, Global).
expand_option(Opt, Opt).
%! rdf_cache_file(+URL, +ReadWrite, -File) is semidet.
%
% File is the cache file for URL. If ReadWrite is =read=, it
% returns the name of an existing file. If =write= it returns
% where a new cache file can be overwritten or created.
rdf_cache_file(_URL, _, _File) :-
cache_option(enabled(false)),
!,
fail.
rdf_cache_file(URL, read, File) :-
!,
( atom_concat('file://', Path, URL),
cache_option(local_directory(Local)),
file_directory_name(Path, Dir),
local_cache_file(URL, LocalFile),
atomic_list_concat([Dir, Local, LocalFile], /, File)
; cache_option(global_directory(Dir)),
url_cache_file(URL, Dir, trp, read, File)
),
access_file(File, read),
!.
rdf_cache_file(URL, write, File) :-
!,
( atom_concat('file://', Path, URL),
cache_option(local_directory(Local)),
file_directory_name(Path, Dir),
( cache_option(create_local_directory(true))
-> RWDir = write
; RWDir = read
),
ensure_dir(Dir, Local, RWDir, CacheDir),
local_cache_file(URL, LocalFile),
atomic_list_concat([CacheDir, LocalFile], /, File)
; cache_option(global_directory(Dir)),
ensure_global_cache(Dir),
url_cache_file(URL, Dir, trp, write, File)
),
access_file(File, write),
!.
ensure_global_cache(Dir) :-
exists_directory(Dir),
!.
ensure_global_cache(Dir) :-
cache_option(create_global_directory(true)),
make_directory_path(Dir),
print_message(informational, rdf(cache_created(Dir))).
/*******************************
* LOCAL CACHE *
*******************************/
%! local_cache_file(+FileURL, -File) is det.
%
% Return the name of the cache file for FileURL. The name is the
% plain filename with the .trp extension. As the URL is a file
% URL, it is guaranteed to be a valid filename. Assumes the
% hosting OS can handle multiple exensions (=|.x.y|=) though.
% These days thats even true on Windows.
local_cache_file(URL, File) :-
file_base_name(URL, Name),
file_name_extension(Name, trp, File).
/*******************************
* GLOBAL CACHE *
*******************************/
%! url_cache_file(+URL, +Dir, +Ext, +RW, -Path) is semidet.
%
% Determine location of cache-file for the given URL in Dir. If
% Ext is provided, the returned Path is ensured to have the
% specified extension.
%
% @param RW If =read=, no directories are created and the call
% fails if URL is not in the cache.
url_cache_file(URL, Dir, Ext, RW, Path) :-
term_hash(URL, Hash0),
Hash is Hash0 + 100000, % make sure > 4 characters
format(string(Hex), '~16r', [Hash]),
sub_atom(Hex, _, 2, 0, L1),
ensure_dir(Dir, L1, RW, Dir1),
sub_atom(Hex, _, 2, 2, L2),
ensure_dir(Dir1, L2, RW, Dir2),
url_to_file(URL, File),
ensure_ext(File, Ext, FileExt),
atomic_list_concat([Dir2, /, FileExt], Path).
ensure_dir(D0, Sub, RW, Dir) :-
atomic_list_concat([D0, /, Sub], Dir),
( exists_directory(Dir)
-> true
; RW == write
-> catch(make_directory(Dir), _, fail)
).
ensure_ext(File, '', File) :- !.
ensure_ext(File, Ext, File) :-
file_name_extension(_, Ext, File),
!.
ensure_ext(File, Ext, FileExt) :-
file_name_extension(File, Ext, FileExt).
%! url_to_file(+URL, -File)
%
% Convert a URL in something that fits in a file, i.e. avoiding /
% and :. We simply replace these by -. We could also use
% www_form_encode/2, but confusion when to replace as well as the
% fact that we loose the '.' (extension) makes this a less ideal
% choice. We could also consider base64 encoding of the name.
url_to_file(URL, File) :-
atom_codes(URL, Codes),
phrase(safe_file_name(Codes), FileCodes),
atom_codes(File, FileCodes).
safe_file_name([]) -->
[].
safe_file_name([H|T]) -->
replace(H),
!,
safe_file_name(T).
safe_file_name([H|T]) -->
[H],
safe_file_name(T).
%! replace(+Code)//
%
% Replace a character code that cannot safely be put in a
% filename. Should we use %XX?
replace(0'/) --> "-". % directory separator
replace(0'\\) --> "-". % not allowed in Windows filename
replace(0':) --> "-". % idem
replace(0'?) --> "-". % idem
replace(0'*) --> "-". % idem
/*******************************
* MESSAGES *
*******************************/
:- multifile prolog:message/3.
prolog:message(rdf(cache_created(Dir))) -->
[ 'Created RDF cache directory ~w'-[Dir] ].
|