0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 36 20 44 2e 20 52 69 63 68 (c) 2006 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 50 72 6f 63 65 64 75 72 65 .**.** Procedure
0370: 73 20 73 74 6f 72 65 20 61 6e 64 20 72 65 74 72 s store and retr
0380: 69 65 76 65 20 72 65 63 6f 72 64 73 20 66 72 6f ieve records fro
0390: 6d 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 m the repository
03a0: 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f .*/.#include "co
03b0: 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 nfig.h".#include
03c0: 20 22 63 6f 6e 74 65 6e 74 2e 68 22 0a 23 69 6e "content.h".#in
03d0: 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e clude <assert.h>
03e0: 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
03f0: 68 65 20 73 72 63 69 64 20 61 73 73 6f 63 69 61 he srcid associa
0400: 74 65 64 20 77 69 74 68 20 72 69 64 2e 20 20 4f ted with rid. O
0410: 72 20 72 65 74 75 72 6e 20 30 20 69 66 20 72 69 r return 0 if ri
0420: 64 20 69 73 20 0a 2a 2a 20 6f 72 69 67 69 6e 61 d is .** origina
0430: 6c 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 6e 6f l content and no
0440: 74 20 61 20 64 65 6c 74 61 2e 0a 2a 2f 0a 73 74 t a delta..*/.st
0450: 61 74 69 63 20 69 6e 74 20 66 69 6e 64 53 72 63 atic int findSrc
0460: 69 64 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 id(int rid){. i
0470: 6e 74 20 73 72 63 69 64 20 3d 20 64 62 5f 69 6e nt srcid = db_in
0480: 74 28 30 2c 20 22 53 45 4c 45 43 54 20 73 72 63 t(0, "SELECT src
0490: 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 id FROM delta WH
04a0: 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64 ERE rid=%d", rid
04b0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 72 63 69 );. return srci
04c0: 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 d;.}../*.** Extr
04d0: 61 63 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 act the content
04e0: 66 6f 72 20 49 44 20 72 69 64 20 61 6e 64 20 70 for ID rid and p
04f0: 75 74 20 69 74 20 69 6e 74 6f 20 74 68 65 0a 2a ut it into the.*
0500: 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 * uninitialized
0510: 62 6c 6f 62 2e 20 20 52 65 74 75 72 6e 20 31 20 blob. Return 1
0520: 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 on success. If
0530: 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20 69 73 the record.** is
0540: 20 61 20 70 68 61 6e 74 6f 6d 2c 20 7a 65 72 6f a phantom, zero
0550: 20 70 42 6c 6f 62 20 61 6e 64 20 72 65 74 75 72 pBlob and retur
0560: 6e 20 30 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 n 0..*/.int cont
0570: 65 6e 74 5f 67 65 74 28 69 6e 74 20 72 69 64 2c ent_get(int rid,
0580: 20 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 Blob *pBlob){.
0590: 20 53 74 6d 74 20 71 3b 0a 20 20 42 6c 6f 62 20 Stmt q;. Blob
05a0: 73 72 63 3b 0a 20 20 69 6e 74 20 73 72 63 69 64 src;. int srcid
05b0: 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a ;. int rc = 0;.
05c0: 0a 20 20 61 73 73 65 72 74 28 20 67 2e 72 65 70 . assert( g.rep
05d0: 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29 3b 0a 20 ositoryOpen );.
05e0: 20 73 72 63 69 64 20 3d 20 66 69 6e 64 53 72 63 srcid = findSrc
05f0: 69 64 28 72 69 64 29 3b 0a 20 20 62 6c 6f 62 5f id(rid);. blob_
0600: 7a 65 72 6f 28 70 42 6c 6f 62 29 3b 0a 20 20 69 zero(pBlob);. i
0610: 66 28 20 73 72 63 69 64 20 29 7b 0a 20 20 20 20 f( srcid ){.
0620: 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 if( content_get(
0630: 73 72 63 69 64 2c 20 26 73 72 63 29 20 29 7b 0a srcid, &src) ){.
0640: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 db_prepare
0650: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e (&q, "SELECT con
0660: 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57 tent FROM blob W
0670: 48 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 HERE rid=%d AND
0680: 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a size>=0", rid);.
0690: 20 20 20 20 20 20 69 66 28 20 64 62 5f 73 74 65 if( db_ste
06a0: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f p(&q)==SQLITE_RO
06b0: 57 20 29 7b 0a 20 20 20 20 20 20 20 20 42 6c 6f W ){. Blo
06c0: 62 20 64 65 6c 74 61 3b 0a 20 20 20 20 20 20 20 b delta;.
06d0: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c db_ephemeral_bl
06e0: 6f 62 28 26 71 2c 20 30 2c 20 26 64 65 6c 74 61 ob(&q, 0, &delta
06f0: 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f );. blob_
0700: 75 6e 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74 uncompress(&delt
0710: 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 a, &delta);.
0720: 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 70 42 blob_init(pB
0730: 6c 6f 62 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 lob,0,0);.
0740: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70 blob_delta_app
0750: 6c 79 28 26 73 72 63 2c 20 26 64 65 6c 74 61 2c ly(&src, &delta,
0760: 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 20 pBlob);.
0770: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c blob_reset(&del
0780: 74 61 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 ta);. rc
0790: 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 1;. }.
07a0: 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 db_finalize(&
07b0: 71 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 q);. blob_r
07c0: 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 20 20 eset(&src);.
07d0: 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 }. }else{. d
07e0: 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 b_prepare(&q, "S
07f0: 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74 20 46 52 ELECT content FR
0800: 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 OM blob WHERE ri
0810: 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 d=%d AND size>=0
0820: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69 66 28 ", rid);. if(
0830: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 db_step(&q)==SQ
0840: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 LITE_ROW ){.
0850: 20 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 db_ephemeral_b
0860: 6c 6f 62 28 26 71 2c 20 30 2c 20 70 42 6c 6f 62 lob(&q, 0, pBlob
0870: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 75 6e );. blob_un
0880: 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f 62 2c 20 compress(pBlob,
0890: 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 72 63 pBlob);. rc
08a0: 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 1;. }.
08b0: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b db_finalize(&q);
08c0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
08d0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 ;.}../*.** COMMA
08e0: 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e ND: test-conten
08f0: 74 2d 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 t-get.**.** Extr
0900: 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 act a blob from
0910: 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 the database and
0920: 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61 write it into a
0930: 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 file..*/.void t
0940: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 67 65 74 5f est_content_get_
0950: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 cmd(void){. int
0960: 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e rid;. Blob con
0970: 74 65 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 tent;. const ch
0980: 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28 ar *zFile;. if(
0990: 20 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e g.argc!=4 && g.
09a0: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 argc!=3 ) usage(
09b0: 22 52 45 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e "RECORDID ?FILEN
09c0: 41 4d 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20 AME?");. zFile
09d0: 3d 20 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e = g.argc==4 ? g.
09e0: 61 72 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20 argv[3] : "-";.
09f0: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 db_must_be_with
0a00: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64 in_tree();. rid
0a10: 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 = name_to_rid(g
0a20: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e .argv[2]);. con
0a30: 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 63 tent_get(rid, &c
0a40: 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f ontent);. blob_
0a50: 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63 write_to_file(&c
0a60: 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a ontent, zFile);.
0a70: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 }../*.** COMMAND
0a80: 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d : test-content-
0a90: 72 61 77 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 rawget.**.** Ext
0aa0: 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d ract a blob from
0ab0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e the database an
0ac0: 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 d write it into
0ad0: 61 20 66 69 6c 65 2e 20 20 54 68 69 73 0a 2a 2a a file. This.**
0ae0: 20 76 65 72 73 69 6f 6e 20 64 6f 65 73 20 6e 6f version does no
0af0: 74 20 65 78 70 61 6e 64 20 74 68 65 20 64 65 6c t expand the del
0b00: 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 ta..*/.void test
0b10: 5f 63 6f 6e 74 65 6e 74 5f 72 61 77 67 65 74 5f _content_rawget_
0b20: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 cmd(void){. int
0b30: 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e rid;. Blob con
0b40: 74 65 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 tent;. const ch
0b50: 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28 ar *zFile;. if(
0b60: 20 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e g.argc!=4 && g.
0b70: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 argc!=3 ) usage(
0b80: 22 52 45 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e "RECORDID ?FILEN
0b90: 41 4d 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20 AME?");. zFile
0ba0: 3d 20 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e = g.argc==4 ? g.
0bb0: 61 72 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20 argv[3] : "-";.
0bc0: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 db_must_be_with
0bd0: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64 in_tree();. rid
0be0: 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 = name_to_rid(g
0bf0: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 62 6c 6f .argv[2]);. blo
0c00: 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29 b_zero(&content)
0c10: 3b 0a 20 20 64 62 5f 62 6c 6f 62 28 26 63 6f 6e ;. db_blob(&con
0c20: 74 65 6e 74 2c 20 22 53 45 4c 45 43 54 20 63 6f tent, "SELECT co
0c30: 6e 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 ntent FROM blob
0c40: 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 WHERE rid=%d", r
0c50: 69 64 29 3b 0a 20 20 62 6c 6f 62 5f 75 6e 63 6f id);. blob_unco
0c60: 6d 70 72 65 73 73 28 26 63 6f 6e 74 65 6e 74 2c mpress(&content,
0c70: 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c &content);. bl
0c80: 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 ob_write_to_file
0c90: 28 26 63 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65 (&content, zFile
0ca0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e );.}../*.** When
0cb0: 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 6f 6e a record is con
0cc0: 76 65 72 74 65 64 20 66 72 6f 6d 20 61 20 70 68 verted from a ph
0cd0: 61 6e 74 6f 6d 20 74 6f 20 61 20 72 65 61 6c 20 antom to a real
0ce0: 72 65 63 6f 72 64 2c 0a 2a 2a 20 69 66 20 74 68 record,.** if th
0cf0: 61 74 20 72 65 63 6f 72 64 20 68 61 73 20 6f 74 at record has ot
0d00: 68 65 72 20 72 65 63 6f 72 64 73 20 74 68 61 74 her records that
0d10: 20 61 72 65 20 64 65 72 69 76 65 64 20 62 79 20 are derived by
0d20: 64 65 6c 74 61 2c 0a 2a 2a 20 74 68 65 6e 20 63 delta,.** then c
0d30: 61 6c 6c 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f all manifest_cro
0d40: 73 73 6c 69 6e 6b 28 29 20 6f 6e 20 74 68 6f 73 sslink() on thos
0d50: 65 20 6f 74 68 65 72 20 72 65 63 6f 72 64 73 2e e other records.
0d60: 0a 2a 2f 0a 76 6f 69 64 20 61 66 74 65 72 5f 64 .*/.void after_d
0d70: 65 70 68 61 6e 74 6f 6d 69 7a 65 28 69 6e 74 20 ephantomize(int
0d80: 72 69 64 2c 20 69 6e 74 20 6c 69 6e 6b 46 6c 61 rid, int linkFla
0d90: 67 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 g){. Stmt q;.
0da0: 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 db_prepare(&q, "
0db0: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 SELECT rid FROM
0dc0: 64 65 6c 74 61 20 57 48 45 52 45 20 73 72 63 69 delta WHERE srci
0dd0: 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 77 d=%d", rid);. w
0de0: 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 hile( db_step(&q
0df0: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b )==SQLITE_ROW ){
0e00: 0a 20 20 20 20 69 6e 74 20 74 69 64 20 3d 20 64 . int tid = d
0e10: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c b_column_int(&q,
0e20: 20 30 29 3b 0a 20 20 20 20 61 66 74 65 72 5f 64 0);. after_d
0e30: 65 70 68 61 6e 74 6f 6d 69 7a 65 28 74 69 64 2c ephantomize(tid,
0e40: 20 31 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 1);. }. db_fi
0e50: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 69 66 nalize(&q);. if
0e60: 28 20 6c 69 6e 6b 46 6c 61 67 20 29 7b 0a 20 20 ( linkFlag ){.
0e70: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a Blob content;.
0e80: 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 content_get(
0e90: 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a rid, &content);.
0ea0: 20 20 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f manifest_cro
0eb0: 73 73 6c 69 6e 6b 28 72 69 64 2c 20 26 63 6f 6e sslink(rid, &con
0ec0: 74 65 6e 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f tent);. blob_
0ed0: 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b reset(&content);
0ee0: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 . }.}../*.** Wr
0ef0: 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f ite content into
0f00: 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
0f10: 52 65 74 75 72 6e 20 74 68 65 20 72 65 63 6f 72 Return the recor
0f20: 64 20 49 44 2e 20 20 49 66 20 74 68 65 0a 2a 2a d ID. If the.**
0f30: 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 6c 72 65 content is alre
0f40: 61 64 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 ady in the datab
0f50: 61 73 65 2c 20 6a 75 73 74 20 72 65 74 75 72 6e ase, just return
0f60: 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e 0a the record ID..
0f70: 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 49 64 20 69 **.** If srcId i
0f80: 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 s specified, the
0f90: 6e 20 70 42 6c 6f 62 20 69 73 20 64 65 6c 74 61 n pBlob is delta
0fa0: 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 0a 2a 2a content from.**
0fb0: 20 74 68 65 20 73 72 63 49 64 20 72 65 63 6f 72 the srcId recor
0fc0: 64 2e 20 20 73 72 63 49 64 20 6d 69 67 68 74 20 d. srcId might
0fd0: 62 65 20 61 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2a be a phantom..**
0fe0: 0a 2a 2a 20 41 20 70 68 61 6e 74 6f 6d 20 69 73 .** A phantom is
0ff0: 20 77 72 69 74 74 65 6e 20 69 66 20 70 42 6c 6f written if pBlo
1000: 62 3d 3d 30 2e 20 20 49 66 20 70 42 6c 6f 62 3d b==0. If pBlob=
1010: 3d 30 20 6f 72 20 69 66 20 73 72 63 49 64 20 69 =0 or if srcId i
1020: 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 74 s.** specified t
1030: 68 65 6e 20 74 68 65 20 55 55 49 44 20 69 73 20 hen the UUID is
1040: 73 65 74 20 74 6f 20 7a 55 75 69 64 2e 20 20 4f set to zUuid. O
1050: 74 68 65 72 77 69 73 65 20 7a 55 75 69 64 20 69 therwise zUuid i
1060: 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49 s.** ignored. I
1070: 6e 20 74 68 65 20 66 75 74 75 72 65 20 74 68 69 n the future thi
1080: 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 73 s might change s
1090: 75 63 68 20 74 68 61 74 20 74 68 65 20 63 6f 6e uch that the con
10a0: 74 65 6e 74 0a 2a 2a 20 68 61 73 68 20 69 73 20 tent.** hash is
10b0: 63 68 65 63 6b 65 64 20 61 67 61 69 6e 73 74 20 checked against
10c0: 7a 55 75 69 64 20 74 6f 20 6d 61 6b 65 20 73 75 zUuid to make su
10d0: 72 65 20 69 74 20 69 73 20 63 6f 72 72 65 63 74 re it is correct
10e0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ..**.** If the r
10f0: 65 63 6f 72 64 20 61 6c 72 65 61 64 79 20 65 78 ecord already ex
1100: 69 73 74 73 20 62 75 74 20 69 73 20 61 20 70 68 ists but is a ph
1110: 61 6e 74 6f 6d 2c 20 74 68 65 20 70 42 6c 6f 62 antom, the pBlob
1120: 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 69 content.** is i
1130: 6e 73 65 72 74 65 64 20 61 6e 64 20 74 68 65 20 nserted and the
1140: 70 68 61 74 6f 6d 20 62 65 63 6f 6d 65 73 20 61 phatom becomes a
1150: 20 72 65 61 6c 20 72 65 63 6f 72 64 2e 0a 2a 2f real record..*/
1160: 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 70 75 74 .int content_put
1170: 28 42 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f (Blob *pBlob, co
1180: 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 2c nst char *zUuid,
1190: 20 69 6e 74 20 73 72 63 49 64 29 7b 0a 20 20 69 int srcId){. i
11a0: 6e 74 20 73 69 7a 65 3b 0a 20 20 69 6e 74 20 72 nt size;. int r
11b0: 69 64 3b 0a 20 20 53 74 6d 74 20 73 31 3b 0a 20 id;. Stmt s1;.
11c0: 20 42 6c 6f 62 20 63 6d 70 72 3b 0a 20 20 42 6c Blob cmpr;. Bl
11d0: 6f 62 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 ob hash;. asser
11e0: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f t( g.repositoryO
11f0: 70 65 6e 20 29 3b 0a 20 20 69 66 28 20 70 42 6c pen );. if( pBl
1200: 6f 62 20 26 26 20 73 72 63 49 64 3d 3d 30 20 29 ob && srcId==0 )
1210: 7b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 6c {. sha1sum_bl
1220: 6f 62 28 70 42 6c 6f 62 2c 20 26 68 61 73 68 29 ob(pBlob, &hash)
1230: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 ;. }else{. b
1240: 6c 6f 62 5f 69 6e 69 74 28 26 68 61 73 68 2c 20 lob_init(&hash,
1250: 7a 55 75 69 64 2c 20 2d 31 29 3b 0a 20 20 7d 0a zUuid, -1);. }.
1260: 20 20 69 66 28 20 70 42 6c 6f 62 3d 3d 30 20 29 if( pBlob==0 )
1270: 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 2d 31 3b {. size = -1;
1280: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 69 . }else{. si
1290: 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28 70 ze = blob_size(p
12a0: 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 64 62 5f Blob);. }. db_
12b0: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f begin_transactio
12c0: 6e 28 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b n();.. /* Check
12d0: 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 65 to see if the e
12e0: 6e 74 72 79 20 61 6c 72 65 61 64 79 20 65 78 69 ntry already exi
12f0: 73 74 73 20 61 6e 64 20 69 66 20 69 74 20 64 6f sts and if it do
1300: 65 73 20 77 68 65 74 68 65 72 0a 20 20 2a 2a 20 es whether. **
1310: 6f 72 20 6e 6f 74 20 74 68 65 20 65 6e 74 72 79 or not the entry
1320: 20 69 73 20 61 20 70 68 61 6e 74 6f 6d 0a 20 20 is a phantom.
1330: 2a 2f 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 */. db_prepare(
1340: 26 73 31 2c 20 22 53 45 4c 45 43 54 20 72 69 64 &s1, "SELECT rid
1350: 2c 20 73 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62 , size FROM blob
1360: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c WHERE uuid=%B",
1370: 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 64 &hash);. if( d
1380: 62 5f 73 74 65 70 28 26 73 31 29 3d 3d 53 51 4c b_step(&s1)==SQL
1390: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 ITE_ROW ){. r
13a0: 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 id = db_column_i
13b0: 6e 74 28 26 73 31 2c 20 30 29 3b 0a 20 20 20 20 nt(&s1, 0);.
13c0: 69 66 28 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e if( db_column_in
13d0: 74 28 26 73 31 2c 20 31 29 3e 3d 30 20 7c 7c 20 t(&s1, 1)>=0 ||
13e0: 70 42 6c 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20 pBlob==0 ){.
13f0: 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 /* Either the
1400: 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 61 20 70 entry is not a p
1410: 68 61 6e 74 6f 6d 20 6f 72 20 69 74 20 69 73 20 hantom or it is
1420: 61 20 70 68 61 6e 74 6f 6d 20 62 75 74 20 77 65 a phantom but we
1430: 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 6e . ** have n
1440: 6f 20 64 61 74 61 20 77 69 74 68 20 77 68 69 63 o data with whic
1450: 68 20 74 6f 20 64 65 70 68 61 74 68 6f 6d 69 7a h to dephathomiz
1460: 65 20 69 74 2e 20 20 49 6e 20 65 69 74 68 65 72 e it. In either
1470: 20 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 case,. **
1480: 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 there is nothing
1490: 20 66 6f 72 20 75 73 65 20 74 6f 20 64 6f 20 6f for use to do o
14a0: 74 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e ther than return
14b0: 20 74 68 65 20 52 49 44 2e 20 2a 2f 0a 20 20 20 the RID. */.
14c0: 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 db_finalize(&
14d0: 73 31 29 3b 0a 20 20 20 20 20 20 64 62 5f 65 6e s1);. db_en
14e0: 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 d_transaction(0)
14f0: 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
1500: 69 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 id;. }. }els
1510: 65 7b 0a 20 20 20 20 72 69 64 20 3d 20 30 3b 20 e{. rid = 0;
1520: 20 2f 2a 20 4e 6f 20 65 6e 74 72 79 20 77 69 74 /* No entry wit
1530: 68 20 74 68 65 20 73 61 6d 65 20 55 55 49 44 20 h the same UUID
1540: 63 75 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73 currently exists
1550: 20 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e */. }. db_fin
1560: 61 6c 69 7a 65 28 26 73 31 29 3b 0a 0a 20 20 2f alize(&s1);.. /
1570: 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 * Construct a re
1580: 63 65 69 76 65 64 2d 66 72 6f 6d 20 49 44 20 69 ceived-from ID i
1590: 66 20 77 65 20 64 6f 20 6e 6f 74 20 61 6c 72 65 f we do not alre
15a0: 61 64 79 20 68 61 76 65 20 6f 6e 65 20 2a 2f 0a ady have one */.
15b0: 20 20 69 66 28 20 67 2e 72 63 76 69 64 3d 3d 30 if( g.rcvid==0
15c0: 20 26 26 20 70 42 6c 6f 62 21 3d 30 20 29 7b 0a && pBlob!=0 ){.
15d0: 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 db_multi_exe
15e0: 63 28 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 c(. "INSER
15f0: 54 20 49 4e 54 4f 20 72 63 76 66 72 6f 6d 28 75 T INTO rcvfrom(u
1600: 69 64 2c 20 6d 74 69 6d 65 2c 20 6e 6f 6e 63 65 id, mtime, nonce
1610: 2c 20 69 70 61 64 64 72 29 22 0a 20 20 20 20 20 , ipaddr)".
1620: 20 20 22 56 41 4c 55 45 53 28 25 64 2c 20 6a 75 "VALUES(%d, ju
1630: 6c 69 61 6e 64 61 79 28 27 6e 6f 77 27 29 2c 20 lianday('now'),
1640: 25 51 2c 20 25 51 29 22 2c 0a 20 20 20 20 20 20 %Q, %Q)",.
1650: 20 67 2e 75 73 65 72 55 69 64 2c 20 67 2e 7a 4e g.userUid, g.zN
1660: 6f 6e 63 65 2c 20 67 2e 7a 49 70 41 64 64 72 0a once, g.zIpAddr.
1670: 20 20 20 20 29 3b 0a 20 20 20 20 67 2e 72 63 76 );. g.rcv
1680: 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69 6e 73 id = db_last_ins
1690: 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20 20 7d ert_rowid();. }
16a0: 0a 0a 20 20 69 66 28 20 72 69 64 3e 30 20 29 7b .. if( rid>0 ){
16b0: 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 6a . /* We are j
16c0: 75 73 74 20 61 64 64 69 6e 67 20 64 61 74 61 20 ust adding data
16d0: 74 6f 20 61 20 70 68 61 6e 74 6f 6d 20 2a 2f 0a to a phantom */.
16e0: 20 20 20 20 61 73 73 65 72 74 28 20 70 42 6c 6f assert( pBlo
16f0: 62 21 3d 30 20 29 3b 0a 20 20 20 20 64 62 5f 70 b!=0 );. db_p
1700: 72 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20 20 repare(&s1,.
1710: 20 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 "UPDATE blob S
1720: 45 54 20 72 63 76 69 64 3d 25 64 2c 20 73 69 7a ET rcvid=%d, siz
1730: 65 3d 25 64 2c 20 63 6f 6e 74 65 6e 74 3d 3a 64 e=%d, content=:d
1740: 61 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64 ata WHERE rid=%d
1750: 22 2c 0a 20 20 20 20 20 20 20 67 2e 72 63 76 69 ",. g.rcvi
1760: 64 2c 20 73 69 7a 65 2c 20 72 69 64 0a 20 20 20 d, size, rid.
1770: 20 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 6d );. blob_com
1780: 70 72 65 73 73 28 70 42 6c 6f 62 2c 20 26 63 6d press(pBlob, &cm
1790: 70 72 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 pr);. db_bind
17a0: 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 _blob(&s1, ":dat
17b0: 61 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 a", &cmpr);.
17c0: 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 db_exec(&s1);.
17d0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 db_multi_exec(
17e0: 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 68 61 "DELETE FROM pha
17f0: 6e 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d 25 ntom WHERE rid=%
1800: 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69 66 d", rid);. if
1810: 28 20 73 72 63 49 64 3d 3d 30 20 7c 7c 20 64 62 ( srcId==0 || db
1820: 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 _int(0, "SELECT
1830: 73 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62 20 57 size FROM blob W
1840: 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 73 72 HERE rid=%d", sr
1850: 63 49 64 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 cId)>0 ){.
1860: 61 66 74 65 72 5f 64 65 70 68 61 6e 74 6f 6d 69 after_dephantomi
1870: 7a 65 28 72 69 64 2c 20 30 29 3b 0a 20 20 20 20 ze(rid, 0);.
1880: 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f }. }else{. /
1890: 2a 20 57 65 20 61 72 65 20 63 72 65 61 74 69 6e * We are creatin
18a0: 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f g a new entry */
18b0: 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 . db_prepare(
18c0: 26 73 31 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 &s1,. "INSE
18d0: 52 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72 63 76 RT INTO blob(rcv
18e0: 69 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63 6f 6e id,size,uuid,con
18f0: 74 65 6e 74 29 22 0a 20 20 20 20 20 20 22 56 41 tent)". "VA
1900: 4c 55 45 53 28 25 64 2c 25 64 2c 27 25 62 27 2c LUES(%d,%d,'%b',
1910: 3a 64 61 74 61 29 22 2c 0a 20 20 20 20 20 20 20 :data)",.
1920: 67 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c 20 26 g.rcvid, size, &
1930: 68 61 73 68 0a 20 20 20 20 29 3b 0a 20 20 20 20 hash. );.
1940: 69 66 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 if( pBlob ){.
1950: 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 blob_compress
1960: 28 70 42 6c 6f 62 2c 20 26 63 6d 70 72 29 3b 0a (pBlob, &cmpr);.
1970: 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c db_bind_bl
1980: 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61 22 2c ob(&s1, ":data",
1990: 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 7d 0a 20 &cmpr);. }.
19a0: 20 20 20 64 62 5f 65 78 65 63 28 26 73 31 29 3b db_exec(&s1);
19b0: 0a 20 20 20 20 72 69 64 20 3d 20 64 62 5f 6c 61 . rid = db_la
19c0: 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 st_insert_rowid(
19d0: 29 3b 0a 20 20 20 20 69 66 28 20 21 70 42 6c 6f );. if( !pBlo
19e0: 62 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 6d 75 b ){. db_mu
19f0: 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54 lti_exec("INSERT
1a00: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 OR IGNORE INTO
1a10: 70 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53 28 25 phantom VALUES(%
1a20: 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 7d d)", rid);. }
1a30: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
1a40: 65 20 73 72 63 49 64 20 69 73 20 73 70 65 63 69 e srcId is speci
1a50: 66 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20 64 fied, then the d
1a60: 61 74 61 20 77 65 20 6a 75 73 74 20 61 64 64 65 ata we just adde
1a70: 64 20 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c 79 d is. ** really
1a80: 20 61 20 64 65 6c 74 61 2e 20 20 52 65 63 6f 72 a delta. Recor
1a90: 64 20 74 68 69 73 20 66 61 63 74 20 69 6e 20 74 d this fact in t
1aa0: 68 65 20 64 65 6c 74 61 20 74 61 62 6c 65 2e 0a he delta table..
1ab0: 20 20 2a 2f 0a 20 20 69 66 28 20 73 72 63 49 64 */. if( srcId
1ac0: 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 ){. db_multi
1ad0: 5f 65 78 65 63 28 22 52 45 50 4c 41 43 45 20 49 _exec("REPLACE I
1ae0: 4e 54 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72 NTO delta(rid,sr
1af0: 63 69 64 29 20 56 41 4c 55 45 53 28 25 64 2c 25 cid) VALUES(%d,%
1b00: 64 29 22 2c 20 72 69 64 2c 20 73 72 63 49 64 29 d)", rid, srcId)
1b10: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 ;. }.. /* Fini
1b20: 73 68 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 sh the transacti
1b30: 6f 6e 20 61 6e 64 20 63 6c 65 61 6e 75 70 20 2a on and cleanup *
1b40: 2f 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 /. db_finalize(
1b50: 26 73 31 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 &s1);. db_end_t
1b60: 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 ransaction(0);.
1b70: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 blob_reset(&has
1b80: 68 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 h);.. /* Make a
1b90: 72 72 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 76 rrangements to v
1ba0: 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64 erify that the d
1bb0: 61 74 61 20 63 61 6e 20 62 65 20 72 65 63 6f 76 ata can be recov
1bc0: 65 72 65 64 0a 20 20 2a 2a 20 62 65 66 6f 72 65 ered. ** before
1bd0: 20 77 65 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 we commit */.
1be0: 69 66 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 if( pBlob ){.
1bf0: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6d 70 blob_reset(&cmp
1c00: 72 29 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 r);. verify_b
1c10: 65 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 efore_commit(rid
1c20: 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1c30: 72 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f rid;.}../*.** CO
1c40: 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e MMAND: test-con
1c50: 74 65 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a 20 45 tent-put.**.** E
1c60: 78 74 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 xtract a blob fr
1c70: 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
1c80: 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 and write it int
1c90: 6f 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 o a file..*/.voi
1ca0: 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 70 d test_content_p
1cb0: 75 74 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 ut_cmd(void){.
1cc0: 69 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 int rid;. Blob
1cd0: 63 6f 6e 74 65 6e 74 3b 0a 20 20 69 66 28 20 67 content;. if( g
1ce0: 2e 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 .argc!=3 ) usage
1cf0: 28 22 46 49 4c 45 4e 41 4d 45 22 29 3b 0a 20 20 ("FILENAME");.
1d00: 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 db_must_be_withi
1d10: 6e 5f 74 72 65 65 28 29 3b 0a 20 20 75 73 65 72 n_tree();. user
1d20: 5f 73 65 6c 65 63 74 28 29 3b 0a 20 20 62 6c 6f _select();. blo
1d30: 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 b_read_from_file
1d40: 28 26 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 72 67 (&content, g.arg
1d50: 76 5b 32 5d 29 3b 0a 20 20 72 69 64 20 3d 20 63 v[2]);. rid = c
1d60: 6f 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 ontent_put(&cont
1d70: 65 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 70 72 ent, 0, 0);. pr
1d80: 69 6e 74 66 28 22 69 6e 73 65 72 74 65 64 20 61 intf("inserted a
1d90: 73 20 72 65 63 6f 72 64 20 25 64 5c 6e 22 2c 20 s record %d\n",
1da0: 72 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d rid);.}../*.** M
1db0: 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e ake sure the con
1dc0: 74 65 6e 74 20 61 74 20 72 69 64 20 69 73 20 74 tent at rid is t
1dd0: 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 he original cont
1de0: 65 6e 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 ent and is not a
1df0: 0a 2a 2a 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76 6f .** delta..*/.vo
1e00: 69 64 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c id content_undel
1e10: 74 61 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 ta(int rid){. i
1e20: 66 28 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 f( findSrcid(rid
1e30: 29 3e 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 )>0 ){. Blob
1e40: 78 3b 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 x;. if( conte
1e50: 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 78 29 20 nt_get(rid, &x)
1e60: 29 7b 0a 20 20 20 20 20 20 53 74 6d 74 20 73 3b ){. Stmt s;
1e70: 0a 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 . db_prepar
1e80: 65 28 26 73 2c 20 22 55 50 44 41 54 45 20 62 6c e(&s, "UPDATE bl
1e90: 6f 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 3a ob SET content=:
1ea0: 63 2c 20 73 69 7a 65 3d 25 64 20 57 48 45 52 45 c, size=%d WHERE
1eb0: 20 72 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 rid=%d",.
1ec0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 b
1ed0: 6c 6f 62 5f 73 69 7a 65 28 26 78 29 2c 20 72 69 lob_size(&x), ri
1ee0: 64 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 d);. blob_c
1ef0: 6f 6d 70 72 65 73 73 28 26 78 2c 20 26 78 29 3b ompress(&x, &x);
1f00: 0a 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 . db_bind_b
1f10: 6c 6f 62 28 26 73 2c 20 22 3a 63 22 2c 20 26 78 lob(&s, ":c", &x
1f20: 29 3b 0a 20 20 20 20 20 20 64 62 5f 65 78 65 63 );. db_exec
1f30: 28 26 73 29 3b 0a 20 20 20 20 20 20 64 62 5f 66 (&s);. db_f
1f40: 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 20 inalize(&s);.
1f50: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 78 blob_reset(&x
1f60: 29 3b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 );. db_mult
1f70: 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 i_exec("DELETE F
1f80: 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 ROM delta WHERE
1f90: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 rid=%d", rid);.
1fa0: 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1fb0: 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 * COMMAND: test
1fc0: 2d 63 6f 6e 74 65 6e 74 2d 75 6e 64 65 6c 74 61 -content-undelta
1fd0: 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 .**.** Make sure
1fe0: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 the content at
1ff0: 52 45 43 4f 52 44 49 44 20 69 73 20 6e 6f 74 20 RECORDID is not
2000: 61 20 64 65 6c 74 61 0a 2a 2f 0a 76 6f 69 64 20 a delta.*/.void
2010: 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 75 6e 64 test_content_und
2020: 65 6c 74 61 5f 63 6d 64 28 76 6f 69 64 29 7b 0a elta_cmd(void){.
2030: 20 20 69 6e 74 20 72 69 64 3b 0a 20 20 69 66 28 int rid;. if(
2040: 20 67 2e 61 72 67 63 21 3d 32 20 29 20 75 73 61 g.argc!=2 ) usa
2050: 67 65 28 22 52 45 43 4f 52 44 49 44 22 29 3b 0a ge("RECORDID");.
2060: 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 db_must_be_wit
2070: 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 hin_tree();. ri
2080: 64 20 3d 20 61 74 6f 69 28 67 2e 61 72 67 76 5b d = atoi(g.argv[
2090: 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 75 2]);. content_u
20a0: 6e 64 65 6c 74 61 28 72 69 64 29 3b 0a 7d 0a 0a ndelta(rid);.}..
20b0: 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
20c0: 20 73 74 6f 72 61 67 65 20 6f 66 20 72 69 64 20 storage of rid
20d0: 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 61 20 so that it is a
20e0: 64 65 6c 74 61 20 6f 66 20 73 72 63 69 64 2e 0a delta of srcid..
20f0: 2a 2a 0a 2a 2a 20 49 66 20 72 69 64 20 69 73 20 **.** If rid is
2100: 61 6c 72 65 61 64 79 20 61 20 64 65 6c 74 61 20 already a delta
2110: 66 72 6f 6d 20 73 6f 6d 65 20 6f 74 68 65 72 20 from some other
2120: 70 6c 61 63 65 20 74 68 65 6e 20 6e 6f 0a 2a 2a place then no.**
2130: 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 conversion occu
2140: 72 73 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 rs and this is a
2150: 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 66 6f no-op unless fo
2160: 72 63 65 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 rce==1..**.** If
2170: 20 73 72 63 69 64 20 69 73 20 61 20 64 65 6c 74 srcid is a delt
2180: 61 20 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f a that depends o
2190: 6e 20 72 69 64 2c 20 74 68 65 6e 20 73 72 63 69 n rid, then srci
21a0: 64 20 69 73 0a 2a 2a 20 63 6f 6e 76 65 72 74 65 d is.** converte
21b0: 64 20 74 6f 20 75 6e 64 65 6c 74 61 65 64 20 74 d to undeltaed t
21c0: 65 78 74 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e ext..*/.void con
21d0: 74 65 6e 74 5f 64 65 6c 74 69 66 79 28 69 6e 74 tent_deltify(int
21e0: 20 72 69 64 2c 20 69 6e 74 20 73 72 63 69 64 2c rid, int srcid,
21f0: 20 69 6e 74 20 66 6f 72 63 65 29 7b 0a 20 20 69 int force){. i
2200: 6e 74 20 73 3b 0a 20 20 42 6c 6f 62 20 64 61 74 nt s;. Blob dat
2210: 61 2c 20 73 72 63 2c 20 64 65 6c 74 61 3b 0a 20 a, src, delta;.
2220: 20 53 74 6d 74 20 73 31 2c 20 73 32 3b 0a 20 20 Stmt s1, s2;.
2230: 69 66 28 20 73 72 63 69 64 3d 3d 72 69 64 20 29 if( srcid==rid )
2240: 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 return;. if( !
2250: 66 6f 72 63 65 20 26 26 20 66 69 6e 64 53 72 63 force && findSrc
2260: 69 64 28 72 69 64 29 3e 30 20 29 20 72 65 74 75 id(rid)>0 ) retu
2270: 72 6e 3b 0a 20 20 73 20 3d 20 73 72 63 69 64 3b rn;. s = srcid;
2280: 0a 20 20 77 68 69 6c 65 28 20 28 73 20 3d 20 66 . while( (s = f
2290: 69 6e 64 53 72 63 69 64 28 73 29 29 3e 30 20 29 indSrcid(s))>0 )
22a0: 7b 0a 20 20 20 20 69 66 28 20 73 3d 3d 72 69 64 {. if( s==rid
22b0: 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e ){. conten
22c0: 74 5f 75 6e 64 65 6c 74 61 28 73 72 63 69 64 29 t_undelta(srcid)
22d0: 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
22e0: 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 }. }. conte
22f0: 6e 74 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73 nt_get(srcid, &s
2300: 72 63 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 rc);. content_g
2310: 65 74 28 72 69 64 2c 20 26 64 61 74 61 29 3b 0a et(rid, &data);.
2320: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 blob_delta_cre
2330: 61 74 65 28 26 73 72 63 2c 20 26 64 61 74 61 2c ate(&src, &data,
2340: 20 26 64 65 6c 74 61 29 3b 0a 20 20 69 66 28 20 &delta);. if(
2350: 62 6c 6f 62 5f 73 69 7a 65 28 26 73 72 63 29 3e blob_size(&src)>
2360: 3d 35 30 20 26 26 20 62 6c 6f 62 5f 73 69 7a 65 =50 && blob_size
2370: 28 26 64 61 74 61 29 3e 3d 35 30 20 26 26 0a 20 (&data)>=50 &&.
2380: 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 blob_s
2390: 69 7a 65 28 26 64 65 6c 74 61 29 20 3c 20 62 6c ize(&delta) < bl
23a0: 6f 62 5f 73 69 7a 65 28 26 64 61 74 61 29 2a 30 ob_size(&data)*0
23b0: 2e 37 35 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f .75 ){. blob_
23c0: 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74 61 2c compress(&delta,
23d0: 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 &delta);. db
23e0: 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20 22 55 _prepare(&s1, "U
23f0: 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 63 PDATE blob SET c
2400: 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57 48 45 ontent=:data WHE
2410: 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 RE rid=%d", rid)
2420: 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 ;. db_prepare
2430: 28 26 73 32 2c 20 22 52 45 50 4c 41 43 45 20 49 (&s2, "REPLACE I
2440: 4e 54 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72 NTO delta(rid,sr
2450: 63 69 64 29 56 41 4c 55 45 53 28 25 64 2c 25 64 cid)VALUES(%d,%d
2460: 29 22 2c 20 72 69 64 2c 20 73 72 63 69 64 29 3b )", rid, srcid);
2470: 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f . db_bind_blo
2480: 62 28 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 b(&s1, ":data",
2490: 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f &delta);. db_
24a0: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f begin_transactio
24b0: 6e 28 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 n();. db_exec
24c0: 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 65 78 (&s1);. db_ex
24d0: 65 63 28 26 73 32 29 3b 0a 20 20 20 20 64 62 5f ec(&s2);. db_
24e0: 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
24f0: 30 29 3b 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 0);. db_final
2500: 69 7a 65 28 26 73 31 29 3b 0a 20 20 20 20 64 62 ize(&s1);. db
2510: 5f 66 69 6e 61 6c 69 7a 65 28 26 73 32 29 3b 0a _finalize(&s2);.
2520: 20 20 20 20 76 65 72 69 66 79 5f 62 65 66 6f 72 verify_befor
2530: 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20 e_commit(rid);.
2540: 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 }. blob_reset(
2550: 26 73 72 63 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 &src);. blob_re
2560: 73 65 74 28 26 64 61 74 61 29 3b 0a 20 20 62 6c set(&data);. bl
2570: 6f 62 5f 72 65 73 65 74 28 26 64 65 6c 74 61 29 ob_reset(&delta)
2580: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 ;.}../*.** COMMA
2590: 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e ND: test-conten
25a0: 74 2d 64 65 6c 74 69 66 79 0a 2a 2a 0a 2a 2a 20 t-deltify.**.**
25b0: 43 6f 6e 76 65 72 74 20 74 68 65 20 63 6f 6e 74 Convert the cont
25c0: 65 6e 74 20 61 74 20 52 49 44 20 69 6e 74 6f 20 ent at RID into
25d0: 61 20 64 65 6c 74 61 20 66 72 6f 6d 20 53 52 43 a delta from SRC
25e0: 49 44 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 ID..*/.void test
25f0: 5f 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 _content_deltify
2600: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 _cmd(void){. if
2610: 28 20 67 2e 61 72 67 63 21 3d 35 20 29 20 75 73 ( g.argc!=5 ) us
2620: 61 67 65 28 22 52 49 44 20 53 52 43 49 44 20 46 age("RID SRCID F
2630: 4f 52 43 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73 ORCE");. db_mus
2640: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 t_be_within_tree
2650: 28 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 64 65 ();. content_de
2660: 6c 74 69 66 79 28 61 74 6f 69 28 67 2e 61 72 67 ltify(atoi(g.arg
2670: 76 5b 32 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 v[2]), atoi(g.ar
2680: 67 76 5b 33 5d 29 2c 20 61 74 6f 69 28 67 2e 61 gv[3]), atoi(g.a
2690: 72 67 76 5b 34 5d 29 29 3b 0a 7d 0a rgv[4]));.}.