Debian Codegraph @

www/sqlar.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>SQLite Archive Files</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 SQLite Archive Files
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#introduction">1. Introduction</a></div>
129 <div class="fancy-toc2"><a href="#database_as_container_object">1.1. Database As Container Object</a></div>
130 <div class="fancy-toc2"><a href="#applications_using_sqlite_archives">1.2. Applications Using SQLite Archives</a></div>
131 <div class="fancy-toc1"><a href="#advantages_of_sqlite_archives">2. Advantages Of SQLite Archives</a></div>
132 <div class="fancy-toc1"><a href="#disadvantages_of_sqlite_archives">3. Disadvantages Of SQLite Archives</a></div>
133 <div class="fancy-toc1"><a href="#managing_an_sqlite_archive_from_the_command_line">4. Managing An SQLite Archive From The Command-Line</a></div>
134 <div class="fancy-toc2"><a href="#other_command_line_tools">4.1. Other command-line tools</a></div>
135 <div class="fancy-toc1"><a href="#managing_sqlite_archives_from_application_code">5. Managing SQLite Archives From Application Code</a></div>
136 </div>
137 </div>
138 <script>
139 function toggle_toc(){
140 var sub = document.getElementById("toc_sub")
141 var mk = document.getElementById("toc_mk")
142 if( sub.style.display!="block" ){
143 sub.style.display = "block";
144 mk.innerHTML = "&#x25bc;";
145 } else {
146 sub.style.display = "none";
147 mk.innerHTML = "&#x25ba;";
148 }
149 }
150 </script>
151 </div>
152
153
154
155
156 <h1 id="introduction"><span>1. </span>Introduction</h1>
157
158 <p>
159 An "SQLite Archive" is a file container similar to a
160 <a href="https://en.wikipedia.org/wiki/Zip_(file_format)">ZIP archive</a> or
161 <a href="https://en.wikipedia.org/wiki/Tar_(computing)">Tarball</a> but
162 based on an SQLite database.
163
164 </p><p>
165 An SQLite Archive is an ordinary SQLite database file that contains the
166 following table as part of its schema:
167
168 </p><div class="codeblock"><pre>CREATE TABLE sqlar(
169 name TEXT PRIMARY KEY, -- name of the file
170 mode INT, -- access permissions
171 mtime INT, -- last modification time
172 sz INT, -- original file size
173 data BLOB -- compressed content
174 );
175 </pre></div>
176
177 <p>
178 Each row of the SQLAR table holds the content of a single file.
179 The filename (the full pathname relative to the root of the archive)
180 is in the "name" field.
181 The "mode" field is an integer which is the unix-style access permissions
182 for the file. "mtime" is the modification time of the file in seconds
183 since 1970. "sz" is the original uncompressed size of the file.
184 The "data" field contains the file content. The content is usually
185 compressed using <a href="http://zlib.net/">Deflate</a>, though not always. If the
186 "sz" field is equal to the size of the "data" field, then the content
187 is stored uncompressed.
188
189 <a name="dbasobj"></a>
190
191 </p><h2 id="database_as_container_object"><span>1.1. </span>Database As Container Object</h2>
192
193 <p>
194 An SQLite Archive is one example of a more general
195 idea that an SQLite database can behave as a container object holding
196 lots of smaller data components.
197
198 </p><p>
199 With client/server databases like PostgreSQL or Oracle, users and
200 developers tend to think of the database as a service or a "node", not
201 as an object. This is because the database content is spread out across
202 multiple files on the server, or possibly across multiple servers in a
203 service cluster. One cannot point to a single file or even a single
204 directory and say "this is the database".
205
206 </p><p>
207 SQLite, in contrast, stores all content in a <a href="fileformat2.html">single file on disk</a>.
208 That single file is something you can point to and say
209 "this is the database". It behaves as an object.
210 An SQLite database file can be copied, renamed, sent as an
211 email attachment, passed as the argument a POST HTTP request,
212 or otherwise treated as other data object such as an image,
213 document, or media file.
214
215 </p><p>
216 Studies show that many applications already use
217 SQLite as a container object. For example,
218 <a href="https://odin.cse.buffalo.edu/papers/2015/TPCTC-sqlite-final.pdf">Kennedy</a>
219 (no relation to the <a href="crew.html#dan">SQLite developer</a>) reports that 14% of
220 Android applications never write to their SQLite databases. It is
221 believed that these applications are downloading entire databases
222 from the cloud and then using the information locally as needed. In other
223 words, the applications are using SQLite not so much as a database but as
224 a queryable wire-transfer format.
225
226 </p><h2 id="applications_using_sqlite_archives"><span>1.2. </span>Applications Using SQLite Archives</h2>
227
228 <p>
229 The <a href="https://fossil-scm.org/">Fossil Distributed Version Control</a> system
230 provides users with the option to download check-ins as either Tarballs,
231 ZIP Archives, or SQLite Archives.
232 </p><h1 id="advantages_of_sqlite_archives"><span>2. </span>Advantages Of SQLite Archives</h1>
233
234 <ol>
235 <li><p>
236 An SQLite Archive is flexible.
237 ZIP Archives and Tarballs are limited to storing only files. An
238 SQLite Archive stores files plus whatever other tabular
239 and/or relational data seems useful to the application.
240
241 </p></li><li><p>
242 An SQLite Archive is transactional.
243 Updates are atomic and durable, even if there are crashes
244 or power losses in the middle of the update.
245 Readers see a consistent and unchanging version of the content even
246 is some other process is simultaneously updating the archive.
247
248 </p></li><li><p>
249 An SQLite Archive can be updated incrementally.
250 Individual files can be added or removed or replaced without having
251 to rewrite the entire archive.
252
253 </p></li><li><p>
254 An SQLite Archive can be queried using a high-level query language (SQL).
255 Some examples:
256 </p><ul>
257 <li> What is the total size of all files in the archive whose names
258 end in ".h" or ".cpp"?
259 </li><li> What percentage of the files are compressed by less than 25%?
260 </li><li> How many executable files are in the archive?
261 </li></ul>
262 Questions like these (and countless others) can be answered without
263 having to uncompress or extract any content.
264
265 </li><li><p>
266 Applications that already use SQLite for other purposes can easily
267 add support for SQLite Archives using a small extension
268 (<a href="https://sqlite.org/src/file/ext/misc/sqlar.c">https://sqlite.org/src/file/ext/misc/sqlar.c</a>) to handle the compression
269 and decompression of content. Even this tiny extension can be omitted
270 if the files in the archive are uncompressed. In contrast, supporting
271 ZIP Archives and/or Tarballs requires either separate libraries or
272 lots of extra custom code, or sometimes both.
273
274 </p></li><li><p>
275 An SQLite Archive can work around firewall-imposed censorship.
276 For example, certain file types that are considered "dangerous"
277 (examples: DLLs) will be
278 <a href="https://support.google.com/mail/answer/6590">blocked by Gmail</a>
279 and probably many other email services and firewalls, even if the
280 files are wrapped inside a ZIP Archive or Tarball.
281 But these firewalls usually do not (yet) know about SQLite Archives and
282 so content can be put inside an SQLite Archive to evade censorship.
283 </p></li></ol>
284
285 <h1 id="disadvantages_of_sqlite_archives"><span>3. </span>Disadvantages Of SQLite Archives</h1>
286
287 <ol>
288 <li><p>
289 The SQLite Archive is a relatively new format. It was first described in
290 in 2014. ZIP Archives and Tarballs, on the other hand, have been around
291 for decades and are well-entrenched as standard formats. Most programmers
292 know what a ZIP Archive or Tarball is, but if you say "SQLite Archive" you
293 are more likely to get a reply of "What?" Tooling to process ZIP Archives
294 and Tarballs is more likely to be installed on stock computers.
295
296 </p></li><li><p>
297 Since an SQLite database is a more general format (it is designed to do
298 much more than simply store a bunch of files) it is not as compact as either
299 the ZIP Archive or Tarball formats. An SQLite Archive is usually about 1%
300 larger than the equivalent ZIP Archive. Tarballs are compressed as a single
301 unit rather than compressing each file separately as is done by both
302 SQLite and ZIP Archives. For these reason, Tarballs tend to be smaller
303 than either ZIP or SQLite Archives.
304 </p><p>
305 As an example, the following table show the relative sizes for an
306 SQLite Archive, a ZIP Archive, and a Tarball of the 1,743 files
307 in the SQLite 3.22.0 source tree:
308 </p><table striped="1" style="margin:1em auto; width:80%; border-spacing:0">
309 <tr style="text-align:left"><td>SQLite Archive</td><td>10,754,048
310 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td>ZIP Archive (using Info-ZIP 3.0)</td><td>10,662,365
311 </td></tr><tr style="text-align:left"><td>ZIP Archive (using <a href="zipfile.html">zipfile</a>)</td><td>10,390,215
312 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td>Tarball</td><td>&#x2007;9,781,109
313 </td></tr></table>
314
315 </li><li><p>
316 An SQLite Archive supports only the <a href="https://zlib.net/">Deflate</a> compression
317 method. Tarballs and ZIP Archive support a wider assortment of
318 compression methods.
319 </p></li></ol>
320
321 <a name="cltools"></a>
322
323 <h1 id="managing_an_sqlite_archive_from_the_command_line"><span>4. </span>Managing An SQLite Archive From The Command-Line</h1>
324
325 <p>
326 The recommended way of creating, updating, listing, and extracting
327 an SQLite Archive is to use the <a href="cli.html">sqlite3.exe command-line shell</a>
328 for SQLite <a href="releaselog/3_23_0.html">version 3.23.0</a> (2018-04-02) or later. This CLI
329 supports the -A command-line option that allows easy management
330 of SQLite Archives.
331 The CLI for SQLite <a href="releaselog/3_22_0.html">version 3.22.0</a> (2018-01-22) has the
332 <a href="cli.html#sqlar">.archive command</a> for managing SQLite Archives, but that requires
333 interacting with the shell.
334
335 </p><p>
336 To list all of the files in an SQLite Archive named "example.sqlar"
337 using one of these commands:
338
339 </p><div class="codeblock"><pre>sqlite3 example.sqlar -At
340 sqlite3 example.sqlar -Atv
341 </pre></div>
342
343 <p>
344 To extract all files from an SQLite Archive named "example.sqlar":
345
346 </p><div class="codeblock"><pre>sqlite3 example.sqlar -Ax
347 </pre></div>
348
349 <p>
350 To create a new SQLite Archive named "alltxt.sqlar" containing all *.txt
351 files in the current directory:
352
353 </p><div class="codeblock"><pre>sqlite3 alltxt.sqlar -Ac *.txt
354 </pre></div>
355
356 <p>
357 To add or update files in an existing SQLite Archive:
358
359 </p><div class="codeblock"><pre>sqlite3 example.sqlar -Au *.md
360 </pre></div>
361
362 <p>
363 For usage hints and a summary of all options, simply give the <a href="cli.html">CLI</a>
364 the -A option with no additional arguments:
365
366 </p><div class="codeblock"><pre>sqlite3 -A
367 </pre></div>
368
369 <p>
370 All of these commands work the same way if the filename argument is
371 is a ZIP Archive instead of an SQLite database.
372
373 </p><h2 id="other_command_line_tools"><span>4.1. </span>Other command-line tools</h2>
374
375 <p>
376 Just as there is the "zip" program to manage ZIP Archives, and the
377 "tar" program to manage Tarballs, the
378 <a href="https://sqlite.org/sqlar">"sqlar" program</a> exists to manage SQL Archives.
379 The "sqlar" program is able to create a new SQLite Archive, list the
380 content of an existing archive, add or remove files from the archive,
381 and/or extract files from the archive.
382 A separate "sqlarfs" program is able to mount the SQLite Archive as
383 a <a href="https://github.com/libfuse/libfuse">Fuse Filesystem</a>.
384
385 </p><h1 id="managing_sqlite_archives_from_application_code"><span>5. </span>Managing SQLite Archives From Application Code</h1>
386
387 <p>
388 Applications can easily read or write SQLite Archives by linking against
389 SQLite and including the
390 <a href="https://sqlite.org/src/file/ext/misc/sqlar.c">ext/misc/sqlar.c</a> extension
391 to handle the compression and decompression. The sqlar.c extension
392 creates two new SQL functions.
393
394 </p><dl>
395 <dt><b>sqlar_compress(X)</b></dt>
396 <dd><p>
397 The sqlar_compress(X) function attempts to compress a copy of the
398 blob X using the <a href="https://zlib.net/">Default</a> algorithm and returns the
399 result as a blob. If the input X is not a compressible blob, then
400 a copy of X is returned. This routine is used when inserting content
401 into an SQLite Archive.
402 </p></dd><dt><b>sqlar_uncompress(Y,SZ)</b></dt>
403 <dd><p>
404 The sqlar_uncompress(Y,SZ) function will undo the compression accomplished
405 by sqlar_compress(X). The Y parameter is the compressed content (the output
406 from a prior call to sqlar_compress()) and SZ is the original uncompressed
407 size of the input X that generated Y. If SZ is less than or equal to the
408 size of Y, that indicates that no compression occurred, and so
409 sqlar_uncompress(Y,SZ) returns a copy of Y. Otherwise, sqlar_uncompress(Y,SZ)
410 runs the Inflate algorithm on Y to uncompress it and restore it to its
411 original form and returns the uncompressed content.
412 This routine is used when extracting content from an SQLite Archive.
413 </p></dd></dl>
414
415 <p>
416 Using the two routines above, it is simple for applications to insert
417 new records into or extract existing records from an SQLite Archive.
418 Insert a new into an SQLite Archive using code like this:
419
420 </p><div class="codeblock"><pre>INSERT INTO sqlar(name,mode,mtime,sz,data)
421 VALUES ($name,$mode,strftime('%s',$mtime),
422 length($content),sqlar_compress($content));
423 </pre></div>
424
425 <p>
426 Extract an entry from the SQLite Archive using code like this:
427
428 </p><div class="codeblock"><pre>SELECT name, mode, datetime(mtime,'unixepoch'), sqlar_uncompress(data,sz)
429 FROM sqlar
430 WHERE ...;
431 </pre></div>
432
433 <p>
434 The code above is for the general case. For the special case of an
435 SQLite Archive that only stores uncompressed or uncompressible content
436 (this might come up, for example, in an SQLite Archive that stores only
437 JPEG, GIF, and/or PNG images) then the content can be inserted into
438 and extracted from the database without using the sqlar_compress()
439 and sqlar_uncompress() functions, and the sqlar.c extension is not
440 required.
441 </p>
442