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: 20 20 73 74 61 74 69 63 20 42 61 67 20 69 6e 50 static Bag inP
05d0: 72 6f 63 65 73 73 3b 0a 0a 20 20 61 73 73 65 72 rocess;.. asser
05e0: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f t( g.repositoryO
05f0: 70 65 6e 20 29 3b 0a 20 20 73 72 63 69 64 20 3d pen );. srcid =
0600: 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 29 3b findSrcid(rid);
0610: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 70 42 6c . blob_zero(pBl
0620: 6f 62 29 3b 0a 20 20 69 66 28 20 73 72 63 69 64 ob);. if( srcid
0630: 20 29 7b 0a 20 20 20 20 69 66 28 20 62 61 67 5f ){. if( bag_
0640: 66 69 6e 64 28 26 69 6e 50 72 6f 63 65 73 73 2c find(&inProcess,
0650: 20 73 72 63 69 64 29 20 29 7b 0a 20 20 20 20 20 srcid) ){.
0660: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
0670: 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 "UPDATE
0680: 62 6c 6f 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 blob SET content
0690: 3d 4e 55 4c 4c 2c 20 73 69 7a 65 3d 2d 31 20 57 =NULL, size=-1 W
06a0: 48 45 52 45 20 72 69 64 3d 25 64 3b 22 0a 20 20 HERE rid=%d;".
06b0: 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 "DELETE FR
06c0: 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72 OM delta WHERE r
06d0: 69 64 3d 25 64 3b 22 0a 20 20 20 20 20 20 20 20 id=%d;".
06e0: 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 "INSERT OR IGNOR
06f0: 45 20 49 4e 54 4f 20 70 68 61 6e 74 6f 6d 20 56 E INTO phantom V
0700: 41 4c 55 45 53 28 25 64 29 3b 22 2c 0a 20 20 20 ALUES(%d);",.
0710: 20 20 20 20 20 73 72 63 69 64 2c 20 73 72 63 69 srcid, srci
0720: 64 2c 20 73 72 63 69 64 0a 20 20 20 20 20 20 29 d, srcid. )
0730: 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 7a 65 72 ;. blob_zer
0740: 6f 28 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 o(pBlob);.
0750: 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
0760: 20 20 20 20 62 61 67 5f 69 6e 73 65 72 74 28 26 bag_insert(&
0770: 69 6e 50 72 6f 63 65 73 73 2c 20 73 72 63 69 64 inProcess, srcid
0780: 29 3b 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 );. if( conte
0790: 6e 74 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73 nt_get(srcid, &s
07a0: 72 63 29 20 29 7b 0a 20 20 20 20 20 20 64 62 5f rc) ){. db_
07b0: 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c prepare(&q, "SEL
07c0: 45 43 54 20 63 6f 6e 74 65 6e 74 20 46 52 4f 4d ECT content FROM
07d0: 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d blob WHERE rid=
07e0: 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c %d AND size>=0",
07f0: 20 72 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 rid);. if(
0800: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 db_step(&q)==SQ
0810: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 LITE_ROW ){.
0820: 20 20 20 20 42 6c 6f 62 20 64 65 6c 74 61 3b 0a Blob delta;.
0830: 20 20 20 20 20 20 20 20 64 62 5f 65 70 68 65 6d db_ephem
0840: 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c eral_blob(&q, 0,
0850: 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 20 20 &delta);.
0860: 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 73 blob_uncompres
0870: 73 28 26 64 65 6c 74 61 2c 20 26 64 65 6c 74 61 s(&delta, &delta
0880: 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f );. blob_
0890: 69 6e 69 74 28 70 42 6c 6f 62 2c 30 2c 30 29 3b init(pBlob,0,0);
08a0: 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 64 65 . blob_de
08b0: 6c 74 61 5f 61 70 70 6c 79 28 26 73 72 63 2c 20 lta_apply(&src,
08c0: 26 64 65 6c 74 61 2c 20 70 42 6c 6f 62 29 3b 0a &delta, pBlob);.
08d0: 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 blob_res
08e0: 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 et(&delta);.
08f0: 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 rc = 1;.
0900: 20 20 7d 0a 20 20 20 20 20 20 64 62 5f 66 69 6e }. db_fin
0910: 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 20 20 20 alize(&q);.
0920: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72 63 blob_reset(&src
0930: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 61 67 );. }. bag
0940: 5f 72 65 6d 6f 76 65 28 26 69 6e 50 72 6f 63 65 _remove(&inProce
0950: 73 73 2c 20 73 72 63 69 64 29 3b 0a 20 20 7d 65 ss, srcid);. }e
0960: 6c 73 65 7b 0a 20 20 20 20 64 62 5f 70 72 65 70 lse{. db_prep
0970: 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 are(&q, "SELECT
0980: 63 6f 6e 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f content FROM blo
0990: 62 20 57 48 45 52 45 20 72 69 64 3d 25 64 20 41 b WHERE rid=%d A
09a0: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 ND size>=0", rid
09b0: 29 3b 0a 20 20 20 20 69 66 28 20 64 62 5f 73 74 );. if( db_st
09c0: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 ep(&q)==SQLITE_R
09d0: 4f 57 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 65 OW ){. db_e
09e0: 70 68 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71 phemeral_blob(&q
09f0: 2c 20 30 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 , 0, pBlob);.
0a00: 20 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 blob_uncompre
0a10: 73 73 28 70 42 6c 6f 62 2c 20 70 42 6c 6f 62 29 ss(pBlob, pBlob)
0a20: 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a ;. rc = 1;.
0a30: 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66 69 6e }. db_fin
0a40: 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 7d 0a 20 alize(&q);. }.
0a50: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
0a60: 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 63 6f 6e *.** Get the con
0a70: 74 65 6e 74 73 20 6f 66 20 61 20 66 69 6c 65 20 tents of a file
0a80: 77 69 74 68 69 6e 20 61 20 67 69 76 65 6e 20 72 within a given r
0a90: 65 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 69 6e 74 20 evision..*/.int
0aa0: 63 6f 6e 74 65 6e 74 5f 67 65 74 5f 68 69 73 74 content_get_hist
0ab0: 6f 72 69 63 61 6c 5f 66 69 6c 65 28 63 6f 6e 73 orical_file(cons
0ac0: 74 20 63 68 61 72 20 2a 72 65 76 69 73 69 6f 6e t char *revision
0ad0: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 , const char *fi
0ae0: 6c 65 2c 20 42 6c 6f 62 20 2a 63 6f 6e 74 65 6e le, Blob *conten
0af0: 74 29 7b 0a 20 20 42 6c 6f 62 20 6d 66 69 6c 65 t){. Blob mfile
0b00: 3b 0a 20 20 4d 61 6e 69 66 65 73 74 20 6d 3b 0a ;. Manifest m;.
0b10: 20 20 69 6e 74 20 69 2c 20 72 69 64 3d 30 3b 0a int i, rid=0;.
0b20: 20 20 0a 20 20 72 69 64 20 3d 20 6e 61 6d 65 5f . rid = name_
0b30: 74 6f 5f 72 69 64 28 72 65 76 69 73 69 6f 6e 29 to_rid(revision)
0b40: 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 ;. content_get(
0b50: 72 69 64 2c 20 26 6d 66 69 6c 65 29 3b 0a 20 20 rid, &mfile);.
0b60: 0a 20 20 69 66 28 20 6d 61 6e 69 66 65 73 74 5f . if( manifest_
0b70: 70 61 72 73 65 28 26 6d 2c 20 26 6d 66 69 6c 65 parse(&m, &mfile
0b80: 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ) ){. for(i=0
0b90: 3b 20 69 3c 6d 2e 6e 46 69 6c 65 3b 20 69 2b 2b ; i<m.nFile; i++
0ba0: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 ){. if( str
0bb0: 63 6d 70 28 6d 2e 61 46 69 6c 65 5b 69 5d 2e 7a cmp(m.aFile[i].z
0bc0: 4e 61 6d 65 2c 20 66 69 6c 65 29 3d 3d 30 20 29 Name, file)==0 )
0bd0: 7b 0a 20 20 20 20 20 20 20 20 72 69 64 20 3d 20 {. rid =
0be0: 75 75 69 64 5f 74 6f 5f 72 69 64 28 6d 2e 61 46 uuid_to_rid(m.aF
0bf0: 69 6c 65 5b 69 5d 2e 7a 55 75 69 64 2c 20 30 29 ile[i].zUuid, 0)
0c00: 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
0c10: 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 content_get(rid
0c20: 2c 20 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 , content);.
0c30: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f }. }. fo
0c40: 73 73 69 6c 5f 70 61 6e 69 63 28 22 66 69 6c 65 ssil_panic("file
0c50: 3a 20 25 73 20 64 6f 65 73 20 6e 6f 74 20 65 78 : %s does not ex
0c60: 69 73 74 20 69 6e 20 72 65 76 69 73 69 6f 6e 3a ist in revision:
0c70: 20 25 73 22 2c 20 66 69 6c 65 2c 20 72 65 76 69 %s", file, revi
0c80: 73 69 6f 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a sion);. }else{.
0c90: 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 fossil_panic
0ca0: 28 22 63 6f 75 6c 64 20 6e 6f 74 20 70 61 72 73 ("could not pars
0cb0: 65 20 6d 61 6e 69 66 65 73 74 20 66 6f 72 20 72 e manifest for r
0cc0: 65 76 69 73 69 6f 6e 3a 20 25 73 22 2c 20 72 65 evision: %s", re
0cd0: 76 69 73 69 6f 6e 29 3b 0a 20 20 7d 0a 20 20 0a vision);. }. .
0ce0: 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
0cf0: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 *.** COMMAND: t
0d00: 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 67 65 74 0a est-content-get.
0d10: 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61 20 **.** Extract a
0d20: 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20 64 61 blob from the da
0d30: 74 61 62 61 73 65 20 61 6e 64 20 77 72 69 74 65 tabase and write
0d40: 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e it into a file.
0d50: 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f .*/.void test_co
0d60: 6e 74 65 6e 74 5f 67 65 74 5f 63 6d 64 28 76 6f ntent_get_cmd(vo
0d70: 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b 0a id){. int rid;.
0d80: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a Blob content;.
0d90: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
0da0: 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 ile;. if( g.arg
0db0: 63 21 3d 34 20 26 26 20 67 2e 61 72 67 63 21 3d c!=4 && g.argc!=
0dc0: 33 20 29 20 75 73 61 67 65 28 22 52 45 43 4f 52 3 ) usage("RECOR
0dd0: 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45 3f 22 29 DID ?FILENAME?")
0de0: 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67 2e 61 72 ;. zFile = g.ar
0df0: 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67 76 5b 33 gc==4 ? g.argv[3
0e00: 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62 5f 6d 75 ] : "-";. db_mu
0e10: 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 st_be_within_tre
0e20: 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 6e 61 6d e();. rid = nam
0e30: 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76 5b e_to_rid(g.argv[
0e40: 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 2]);. content_g
0e50: 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 et(rid, &content
0e60: 29 3b 0a 20 20 62 6c 6f 62 5f 77 72 69 74 65 5f );. blob_write_
0e70: 74 6f 5f 66 69 6c 65 28 26 63 6f 6e 74 65 6e 74 to_file(&content
0e80: 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a , zFile);.}../*.
0e90: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 ** COMMAND: tes
0ea0: 74 2d 63 6f 6e 74 65 6e 74 2d 72 61 77 67 65 74 t-content-rawget
0eb0: 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61 .**.** Extract a
0ec0: 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20 64 blob from the d
0ed0: 61 74 61 62 61 73 65 20 61 6e 64 20 77 72 69 74 atabase and writ
0ee0: 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c 65 e it into a file
0ef0: 2e 20 20 54 68 69 73 0a 2a 2a 20 76 65 72 73 69 . This.** versi
0f00: 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 70 61 on does not expa
0f10: 6e 64 20 74 68 65 20 64 65 6c 74 61 2e 0a 2a 2f nd the delta..*/
0f20: 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 .void test_conte
0f30: 6e 74 5f 72 61 77 67 65 74 5f 63 6d 64 28 76 6f nt_rawget_cmd(vo
0f40: 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b 0a id){. int rid;.
0f50: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a Blob content;.
0f60: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
0f70: 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 ile;. if( g.arg
0f80: 63 21 3d 34 20 26 26 20 67 2e 61 72 67 63 21 3d c!=4 && g.argc!=
0f90: 33 20 29 20 75 73 61 67 65 28 22 52 45 43 4f 52 3 ) usage("RECOR
0fa0: 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45 3f 22 29 DID ?FILENAME?")
0fb0: 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67 2e 61 72 ;. zFile = g.ar
0fc0: 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67 76 5b 33 gc==4 ? g.argv[3
0fd0: 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62 5f 6d 75 ] : "-";. db_mu
0fe0: 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 st_be_within_tre
0ff0: 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 6e 61 6d e();. rid = nam
1000: 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76 5b e_to_rid(g.argv[
1010: 32 5d 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 2]);. blob_zero
1020: 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 64 62 (&content);. db
1030: 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 2c 20 _blob(&content,
1040: 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74 20 "SELECT content
1050: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 FROM blob WHERE
1060: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 rid=%d", rid);.
1070: 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 73 73 blob_uncompress
1080: 28 26 63 6f 6e 74 65 6e 74 2c 20 26 63 6f 6e 74 (&content, &cont
1090: 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72 69 ent);. blob_wri
10a0: 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e 74 te_to_file(&cont
10b0: 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a 0a ent, zFile);.}..
10c0: 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 72 65 63 /*.** When a rec
10d0: 6f 72 64 20 69 73 20 63 6f 6e 76 65 72 74 65 64 ord is converted
10e0: 20 66 72 6f 6d 20 61 20 70 68 61 6e 74 6f 6d 20 from a phantom
10f0: 74 6f 20 61 20 72 65 61 6c 20 72 65 63 6f 72 64 to a real record
1100: 2c 0a 2a 2a 20 69 66 20 74 68 61 74 20 72 65 63 ,.** if that rec
1110: 6f 72 64 20 68 61 73 20 6f 74 68 65 72 20 72 65 ord has other re
1120: 63 6f 72 64 73 20 74 68 61 74 20 61 72 65 20 64 cords that are d
1130: 65 72 69 76 65 64 20 62 79 20 64 65 6c 74 61 2c erived by delta,
1140: 0a 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20 6d 61 .** then call ma
1150: 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b nifest_crosslink
1160: 28 29 20 6f 6e 20 74 68 6f 73 65 20 6f 74 68 65 () on those othe
1170: 72 20 72 65 63 6f 72 64 73 2e 0a 2a 2f 0a 76 6f r records..*/.vo
1180: 69 64 20 61 66 74 65 72 5f 64 65 70 68 61 6e 74 id after_dephant
1190: 6f 6d 69 7a 65 28 69 6e 74 20 72 69 64 2c 20 69 omize(int rid, i
11a0: 6e 74 20 6c 69 6e 6b 46 6c 61 67 29 7b 0a 20 20 nt linkFlag){.
11b0: 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70 72 65 Stmt q;. db_pre
11c0: 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 pare(&q, "SELECT
11d0: 20 72 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 rid FROM delta
11e0: 57 48 45 52 45 20 73 72 63 69 64 3d 25 64 22 2c WHERE srcid=%d",
11f0: 20 72 69 64 29 3b 0a 20 20 77 68 69 6c 65 28 20 rid);. while(
1200: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c db_step(&q)==SQL
1210: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 69 ITE_ROW ){. i
1220: 6e 74 20 74 69 64 20 3d 20 64 62 5f 63 6f 6c 75 nt tid = db_colu
1230: 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 mn_int(&q, 0);.
1240: 20 20 20 61 66 74 65 72 5f 64 65 70 68 61 6e 74 after_dephant
1250: 6f 6d 69 7a 65 28 74 69 64 2c 20 31 29 3b 0a 20 omize(tid, 1);.
1260: 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 }. db_finalize
1270: 28 26 71 29 3b 0a 20 20 69 66 28 20 6c 69 6e 6b (&q);. if( link
1280: 46 6c 61 67 20 29 7b 0a 20 20 20 20 42 6c 6f 62 Flag ){. Blob
1290: 20 63 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 63 6f content;. co
12a0: 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 ntent_get(rid, &
12b0: 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 6d 61 content);. ma
12c0: 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b nifest_crosslink
12d0: 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b (rid, &content);
12e0: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 . blob_reset(
12f0: 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 7d 0a 7d &content);. }.}
1300: 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63 6f ../*.** Write co
1310: 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 64 ntent into the d
1320: 61 74 61 62 61 73 65 2e 20 20 52 65 74 75 72 6e atabase. Return
1330: 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e 20 the record ID.
1340: 20 49 66 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 If the.** conte
1350: 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e nt is already in
1360: 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 6a the database, j
1370: 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 72 ust return the r
1380: 65 63 6f 72 64 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 ecord ID..**.**
1390: 49 66 20 73 72 63 49 64 20 69 73 20 73 70 65 63 If srcId is spec
13a0: 69 66 69 65 64 2c 20 74 68 65 6e 20 70 42 6c 6f ified, then pBlo
13b0: 62 20 69 73 20 64 65 6c 74 61 20 63 6f 6e 74 65 b is delta conte
13c0: 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 nt from.** the s
13d0: 72 63 49 64 20 72 65 63 6f 72 64 2e 20 20 73 72 rcId record. sr
13e0: 63 49 64 20 6d 69 67 68 74 20 62 65 20 61 20 70 cId might be a p
13f0: 68 61 6e 74 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 20 hantom..**.** A
1400: 70 68 61 6e 74 6f 6d 20 69 73 20 77 72 69 74 74 phantom is writt
1410: 65 6e 20 69 66 20 70 42 6c 6f 62 3d 3d 30 2e 20 en if pBlob==0.
1420: 20 49 66 20 70 42 6c 6f 62 3d 3d 30 20 6f 72 20 If pBlob==0 or
1430: 69 66 20 73 72 63 49 64 20 69 73 0a 2a 2a 20 73 if srcId is.** s
1440: 70 65 63 69 66 69 65 64 20 74 68 65 6e 20 74 68 pecified then th
1450: 65 20 55 55 49 44 20 69 73 20 73 65 74 20 74 6f e UUID is set to
1460: 20 7a 55 75 69 64 2e 20 20 4f 74 68 65 72 77 69 zUuid. Otherwi
1470: 73 65 20 7a 55 75 69 64 20 69 73 0a 2a 2a 20 69 se zUuid is.** i
1480: 67 6e 6f 72 65 64 2e 20 20 49 6e 20 74 68 65 20 gnored. In the
1490: 66 75 74 75 72 65 20 74 68 69 73 20 6d 69 67 68 future this migh
14a0: 74 20 63 68 61 6e 67 65 20 73 75 63 68 20 74 68 t change such th
14b0: 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a at the content.*
14c0: 2a 20 68 61 73 68 20 69 73 20 63 68 65 63 6b 65 * hash is checke
14d0: 64 20 61 67 61 69 6e 73 74 20 7a 55 75 69 64 20 d against zUuid
14e0: 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 to make sure it
14f0: 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a is correct..**.*
1500: 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 * If the record
1510: 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 62 already exists b
1520: 75 74 20 69 73 20 61 20 70 68 61 6e 74 6f 6d 2c ut is a phantom,
1530: 20 74 68 65 20 70 42 6c 6f 62 20 63 6f 6e 74 65 the pBlob conte
1540: 6e 74 0a 2a 2a 20 69 73 20 69 6e 73 65 72 74 65 nt.** is inserte
1550: 64 20 61 6e 64 20 74 68 65 20 70 68 61 74 6f 6d d and the phatom
1560: 20 62 65 63 6f 6d 65 73 20 61 20 72 65 61 6c 20 becomes a real
1570: 72 65 63 6f 72 64 2e 0a 2a 2f 0a 69 6e 74 20 63 record..*/.int c
1580: 6f 6e 74 65 6e 74 5f 70 75 74 28 42 6c 6f 62 20 ontent_put(Blob
1590: 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 63 68 *pBlob, const ch
15a0: 61 72 20 2a 7a 55 75 69 64 2c 20 69 6e 74 20 73 ar *zUuid, int s
15b0: 72 63 49 64 29 7b 0a 20 20 69 6e 74 20 73 69 7a rcId){. int siz
15c0: 65 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20 20 e;. int rid;.
15d0: 53 74 6d 74 20 73 31 3b 0a 20 20 42 6c 6f 62 20 Stmt s1;. Blob
15e0: 63 6d 70 72 3b 0a 20 20 42 6c 6f 62 20 68 61 73 cmpr;. Blob has
15f0: 68 3b 0a 20 20 69 6e 74 20 6d 61 72 6b 41 73 55 h;. int markAsU
1600: 6e 63 6c 75 73 74 65 72 65 64 20 3d 20 30 3b 0a nclustered = 0;.
1610: 20 20 0a 20 20 61 73 73 65 72 74 28 20 67 2e 72 . assert( g.r
1620: 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29 3b epositoryOpen );
1630: 0a 20 20 69 66 28 20 70 42 6c 6f 62 20 26 26 20 . if( pBlob &&
1640: 73 72 63 49 64 3d 3d 30 20 29 7b 0a 20 20 20 20 srcId==0 ){.
1650: 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 70 42 6c sha1sum_blob(pBl
1660: 6f 62 2c 20 26 68 61 73 68 29 3b 0a 20 20 7d 65 ob, &hash);. }e
1670: 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f 69 6e lse{. blob_in
1680: 69 74 28 26 68 61 73 68 2c 20 7a 55 75 69 64 2c it(&hash, zUuid,
1690: 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 -1);. }. if(
16a0: 70 42 6c 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20 pBlob==0 ){.
16b0: 73 69 7a 65 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c size = -1;. }el
16c0: 73 65 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 62 se{. size = b
16d0: 6c 6f 62 5f 73 69 7a 65 28 70 42 6c 6f 62 29 3b lob_size(pBlob);
16e0: 0a 20 20 7d 0a 20 20 64 62 5f 62 65 67 69 6e 5f . }. db_begin_
16f0: 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 0a transaction();..
1700: 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 /* Check to se
1710: 65 20 69 66 20 74 68 65 20 65 6e 74 72 79 20 61 e if the entry a
1720: 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e lready exists an
1730: 64 20 69 66 20 69 74 20 64 6f 65 73 20 77 68 65 d if it does whe
1740: 74 68 65 72 0a 20 20 2a 2a 20 6f 72 20 6e 6f 74 ther. ** or not
1750: 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 61 20 the entry is a
1760: 70 68 61 6e 74 6f 6d 0a 20 20 2a 2f 0a 20 20 64 phantom. */. d
1770: 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20 22 b_prepare(&s1, "
1780: 53 45 4c 45 43 54 20 72 69 64 2c 20 73 69 7a 65 SELECT rid, size
1790: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 FROM blob WHERE
17a0: 20 75 75 69 64 3d 25 42 22 2c 20 26 68 61 73 68 uuid=%B", &hash
17b0: 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 );. if( db_step
17c0: 28 26 73 31 29 3d 3d 53 51 4c 49 54 45 5f 52 4f (&s1)==SQLITE_RO
17d0: 57 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20 64 W ){. rid = d
17e0: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 31 b_column_int(&s1
17f0: 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64 62 , 0);. if( db
1800: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 31 2c _column_int(&s1,
1810: 20 31 29 3e 3d 30 20 7c 7c 20 70 42 6c 6f 62 3d 1)>=0 || pBlob=
1820: 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 45 =0 ){. /* E
1830: 69 74 68 65 72 20 74 68 65 20 65 6e 74 72 79 20 ither the entry
1840: 69 73 20 6e 6f 74 20 61 20 70 68 61 6e 74 6f 6d is not a phantom
1850: 20 6f 72 20 69 74 20 69 73 20 61 20 70 68 61 6e or it is a phan
1860: 74 6f 6d 20 62 75 74 20 77 65 0a 20 20 20 20 20 tom but we.
1870: 20 2a 2a 20 68 61 76 65 20 6e 6f 20 64 61 74 61 ** have no data
1880: 20 77 69 74 68 20 77 68 69 63 68 20 74 6f 20 64 with which to d
1890: 65 70 68 61 6e 74 6f 6d 69 7a 65 20 69 74 2e 20 ephantomize it.
18a0: 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c In either case,
18b0: 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20 . ** there
18c0: 69 73 20 6e 6f 74 68 69 6e 67 20 66 6f 72 20 75 is nothing for u
18d0: 73 20 74 6f 20 64 6f 20 6f 74 68 65 72 20 74 68 s to do other th
18e0: 61 6e 20 72 65 74 75 72 6e 20 74 68 65 20 52 49 an return the RI
18f0: 44 2e 20 2a 2f 0a 20 20 20 20 20 20 64 62 5f 66 D. */. db_f
1900: 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 20 inalize(&s1);.
1910: 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 db_end_trans
1920: 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20 20 20 action(0);.
1930: 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 20 20 20 return rid;.
1940: 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
1950: 72 69 64 20 3d 20 30 3b 20 20 2f 2a 20 4e 6f 20 rid = 0; /* No
1960: 65 6e 74 72 79 20 77 69 74 68 20 74 68 65 20 73 entry with the s
1970: 61 6d 65 20 55 55 49 44 20 63 75 72 72 65 6e 74 ame UUID current
1980: 6c 79 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 ly exists */.
1990: 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74 65 72 markAsUncluster
19a0: 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 64 62 ed = 1;. }. db
19b0: 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a _finalize(&s1);.
19c0: 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 . /* Construct
19d0: 61 20 72 65 63 65 69 76 65 64 2d 66 72 6f 6d 20 a received-from
19e0: 49 44 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 20 ID if we do not
19f0: 61 6c 72 65 61 64 79 20 68 61 76 65 20 6f 6e 65 already have one
1a00: 20 2a 2f 0a 20 20 69 66 28 20 67 2e 72 63 76 69 */. if( g.rcvi
1a10: 64 3d 3d 30 20 26 26 20 70 42 6c 6f 62 21 3d 30 d==0 && pBlob!=0
1a20: 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 ){. db_multi
1a30: 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 22 49 _exec(. "I
1a40: 4e 53 45 52 54 20 49 4e 54 4f 20 72 63 76 66 72 NSERT INTO rcvfr
1a50: 6f 6d 28 75 69 64 2c 20 6d 74 69 6d 65 2c 20 6e om(uid, mtime, n
1a60: 6f 6e 63 65 2c 20 69 70 61 64 64 72 29 22 0a 20 once, ipaddr)".
1a70: 20 20 20 20 20 20 22 56 41 4c 55 45 53 28 25 64 "VALUES(%d
1a80: 2c 20 6a 75 6c 69 61 6e 64 61 79 28 27 6e 6f 77 , julianday('now
1a90: 27 29 2c 20 25 51 2c 20 25 51 29 22 2c 0a 20 20 '), %Q, %Q)",.
1aa0: 20 20 20 20 20 67 2e 75 73 65 72 55 69 64 2c 20 g.userUid,
1ab0: 67 2e 7a 4e 6f 6e 63 65 2c 20 67 2e 7a 49 70 41 g.zNonce, g.zIpA
1ac0: 64 64 72 0a 20 20 20 20 29 3b 0a 20 20 20 20 67 ddr. );. g
1ad0: 2e 72 63 76 69 64 20 3d 20 64 62 5f 6c 61 73 74 .rcvid = db_last
1ae0: 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 3b _insert_rowid();
1af0: 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 69 64 3e . }.. if( rid>
1b00: 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 0 ){. /* We a
1b10: 72 65 20 6a 75 73 74 20 61 64 64 69 6e 67 20 64 re just adding d
1b20: 61 74 61 20 74 6f 20 61 20 70 68 61 6e 74 6f 6d ata to a phantom
1b30: 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
1b40: 70 42 6c 6f 62 21 3d 30 20 29 3b 0a 20 20 20 20 pBlob!=0 );.
1b50: 64 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c 0a db_prepare(&s1,.
1b60: 20 20 20 20 20 20 22 55 50 44 41 54 45 20 62 6c "UPDATE bl
1b70: 6f 62 20 53 45 54 20 72 63 76 69 64 3d 25 64 2c ob SET rcvid=%d,
1b80: 20 73 69 7a 65 3d 25 64 2c 20 63 6f 6e 74 65 6e size=%d, conten
1b90: 74 3d 3a 64 61 74 61 20 57 48 45 52 45 20 72 69 t=:data WHERE ri
1ba0: 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 20 67 2e d=%d",. g.
1bb0: 72 63 76 69 64 2c 20 73 69 7a 65 2c 20 72 69 64 rcvid, size, rid
1bc0: 0a 20 20 20 20 29 3b 0a 20 20 20 20 62 6c 6f 62 . );. blob
1bd0: 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f 62 2c _compress(pBlob,
1be0: 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 64 62 5f &cmpr);. db_
1bf0: 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c 20 22 bind_blob(&s1, "
1c00: 3a 64 61 74 61 22 2c 20 26 63 6d 70 72 29 3b 0a :data", &cmpr);.
1c10: 20 20 20 20 64 62 5f 65 78 65 63 28 26 73 31 29 db_exec(&s1)
1c20: 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 ;. db_multi_e
1c30: 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f 4d xec("DELETE FROM
1c40: 20 70 68 61 6e 74 6f 6d 20 57 48 45 52 45 20 72 phantom WHERE r
1c50: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 id=%d", rid);.
1c60: 20 20 69 66 28 20 73 72 63 49 64 3d 3d 30 20 7c if( srcId==0 |
1c70: 7c 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c | db_int(0, "SEL
1c80: 45 43 54 20 73 69 7a 65 20 46 52 4f 4d 20 62 6c ECT size FROM bl
1c90: 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 ob WHERE rid=%d"
1ca0: 2c 20 73 72 63 49 64 29 3e 30 20 29 7b 0a 20 20 , srcId)>0 ){.
1cb0: 20 20 20 20 61 66 74 65 72 5f 64 65 70 68 61 6e after_dephan
1cc0: 74 6f 6d 69 7a 65 28 72 69 64 2c 20 30 29 3b 0a tomize(rid, 0);.
1cd0: 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1ce0: 20 20 20 2f 2a 20 57 65 20 61 72 65 20 63 72 65 /* We are cre
1cf0: 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 ating a new entr
1d00: 79 20 2a 2f 0a 20 20 20 20 64 62 5f 70 72 65 70 y */. db_prep
1d10: 61 72 65 28 26 73 31 2c 0a 20 20 20 20 20 20 22 are(&s1,. "
1d20: 49 4e 53 45 52 54 20 49 4e 54 4f 20 62 6c 6f 62 INSERT INTO blob
1d30: 28 72 63 76 69 64 2c 73 69 7a 65 2c 75 75 69 64 (rcvid,size,uuid
1d40: 2c 63 6f 6e 74 65 6e 74 29 22 0a 20 20 20 20 20 ,content)".
1d50: 20 22 56 41 4c 55 45 53 28 25 64 2c 25 64 2c 27 "VALUES(%d,%d,'
1d60: 25 62 27 2c 3a 64 61 74 61 29 22 2c 0a 20 20 20 %b',:data)",.
1d70: 20 20 20 20 67 2e 72 63 76 69 64 2c 20 73 69 7a g.rcvid, siz
1d80: 65 2c 20 26 68 61 73 68 0a 20 20 20 20 29 3b 0a e, &hash. );.
1d90: 20 20 20 20 69 66 28 20 70 42 6c 6f 62 20 29 7b if( pBlob ){
1da0: 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 . blob_comp
1db0: 72 65 73 73 28 70 42 6c 6f 62 2c 20 26 63 6d 70 ress(pBlob, &cmp
1dc0: 72 29 3b 0a 20 20 20 20 20 20 64 62 5f 62 69 6e r);. db_bin
1dd0: 64 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 d_blob(&s1, ":da
1de0: 74 61 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 ta", &cmpr);.
1df0: 20 7d 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 }. db_exec(&
1e00: 73 31 29 3b 0a 20 20 20 20 72 69 64 20 3d 20 64 s1);. rid = d
1e10: 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f b_last_insert_ro
1e20: 77 69 64 28 29 3b 0a 20 20 20 20 69 66 28 20 21 wid();. if( !
1e30: 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 64 pBlob ){. d
1e40: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 49 4e b_multi_exec("IN
1e50: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 SERT OR IGNORE I
1e60: 4e 54 4f 20 70 68 61 6e 74 6f 6d 20 56 41 4c 55 NTO phantom VALU
1e70: 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a 20 ES(%d)", rid);.
1e80: 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
1e90: 66 20 74 68 65 20 73 72 63 49 64 20 69 73 20 73 f the srcId is s
1ea0: 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 74 pecified, then t
1eb0: 68 65 20 64 61 74 61 20 77 65 20 6a 75 73 74 20 he data we just
1ec0: 61 64 64 65 64 20 69 73 0a 20 20 2a 2a 20 72 65 added is. ** re
1ed0: 61 6c 6c 79 20 61 20 64 65 6c 74 61 2e 20 20 52 ally a delta. R
1ee0: 65 63 6f 72 64 20 74 68 69 73 20 66 61 63 74 20 ecord this fact
1ef0: 69 6e 20 74 68 65 20 64 65 6c 74 61 20 74 61 62 in the delta tab
1f00: 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 le.. */. if( s
1f10: 72 63 49 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d rcId ){. db_m
1f20: 75 6c 74 69 5f 65 78 65 63 28 22 52 45 50 4c 41 ulti_exec("REPLA
1f30: 43 45 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69 CE INTO delta(ri
1f40: 64 2c 73 72 63 69 64 29 20 56 41 4c 55 45 53 28 d,srcid) VALUES(
1f50: 25 64 2c 25 64 29 22 2c 20 72 69 64 2c 20 73 72 %d,%d)", rid, sr
1f60: 63 49 64 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f cId);. }. . /
1f70: 2a 20 41 64 64 20 74 68 65 20 65 6c 65 6d 65 6e * Add the elemen
1f80: 74 20 74 6f 20 74 68 65 20 75 6e 63 6c 75 73 74 t to the unclust
1f90: 65 72 65 64 20 74 61 62 6c 65 20 69 66 20 68 61 ered table if ha
1fa0: 73 20 6e 65 76 65 72 20 62 65 65 6e 0a 20 20 2a s never been. *
1fb0: 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 65 * previously see
1fc0: 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 61 n.. */. if( ma
1fd0: 72 6b 41 73 55 6e 63 6c 75 73 74 65 72 65 64 20 rkAsUnclustered
1fe0: 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f ){. db_multi_
1ff0: 65 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20 exec("INSERT OR
2000: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 75 6e 63 6c IGNORE INTO uncl
2010: 75 73 74 65 72 65 64 20 56 41 4c 55 45 53 28 25 ustered VALUES(%
2020: 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 0a d)", rid);. }..
2030: 20 20 2f 2a 20 46 69 6e 69 73 68 20 74 68 65 20 /* Finish the
2040: 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 transaction and
2050: 63 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 64 62 5f cleanup */. db_
2060: 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 finalize(&s1);.
2070: 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 db_end_transact
2080: 69 6f 6e 28 30 29 3b 0a 20 20 62 6c 6f 62 5f 72 ion(0);. blob_r
2090: 65 73 65 74 28 26 68 61 73 68 29 3b 0a 0a 20 20 eset(&hash);..
20a0: 2f 2a 20 4d 61 6b 65 20 61 72 72 61 6e 67 65 6d /* Make arrangem
20b0: 65 6e 74 73 20 74 6f 20 76 65 72 69 66 79 20 74 ents to verify t
20c0: 68 61 74 20 74 68 65 20 64 61 74 61 20 63 61 6e hat the data can
20d0: 20 62 65 20 72 65 63 6f 76 65 72 65 64 0a 20 20 be recovered.
20e0: 2a 2a 20 62 65 66 6f 72 65 20 77 65 20 63 6f 6d ** before we com
20f0: 6d 69 74 20 2a 2f 0a 20 20 69 66 28 20 70 42 6c mit */. if( pBl
2100: 6f 62 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 72 ob ){. blob_r
2110: 65 73 65 74 28 26 63 6d 70 72 29 3b 0a 20 20 20 eset(&cmpr);.
2120: 20 76 65 72 69 66 79 5f 62 65 66 6f 72 65 5f 63 verify_before_c
2130: 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20 20 7d 0a ommit(rid);. }.
2140: 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a return rid;.}.
2150: 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 ./*.** COMMAND:
2160: 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 70 75 test-content-pu
2170: 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 t.**.** Extract
2180: 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20 a blob from the
2190: 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 72 69 database and wri
21a0: 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c te it into a fil
21b0: 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f e..*/.void test_
21c0: 63 6f 6e 74 65 6e 74 5f 70 75 74 5f 63 6d 64 28 content_put_cmd(
21d0: 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 void){. int rid
21e0: 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 ;. Blob content
21f0: 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d ;. if( g.argc!=
2200: 33 20 29 20 75 73 61 67 65 28 22 46 49 4c 45 4e 3 ) usage("FILEN
2210: 41 4d 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74 AME");. db_must
2220: 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 _be_within_tree(
2230: 29 3b 0a 20 20 75 73 65 72 5f 73 65 6c 65 63 74 );. user_select
2240: 28 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61 64 5f ();. blob_read_
2250: 66 72 6f 6d 5f 66 69 6c 65 28 26 63 6f 6e 74 65 from_file(&conte
2260: 6e 74 2c 20 67 2e 61 72 67 76 5b 32 5d 29 3b 0a nt, g.argv[2]);.
2270: 20 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e 74 5f rid = content_
2280: 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 30 2c put(&content, 0,
2290: 20 30 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 69 0);. printf("i
22a0: 6e 73 65 72 74 65 64 20 61 73 20 72 65 63 6f 72 nserted as recor
22b0: 64 20 25 64 5c 6e 22 2c 20 72 69 64 29 3b 0a 7d d %d\n", rid);.}
22c0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
22d0: 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 e the content at
22e0: 20 72 69 64 20 69 73 20 74 68 65 20 6f 72 69 67 rid is the orig
22f0: 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 61 6e 64 inal content and
2300: 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 64 65 6c is not a.** del
2310: 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74 ta..*/.void cont
2320: 65 6e 74 5f 75 6e 64 65 6c 74 61 28 69 6e 74 20 ent_undelta(int
2330: 72 69 64 29 7b 0a 20 20 69 66 28 20 66 69 6e 64 rid){. if( find
2340: 53 72 63 69 64 28 72 69 64 29 3e 30 20 29 7b 0a Srcid(rid)>0 ){.
2350: 20 20 20 20 42 6c 6f 62 20 78 3b 0a 20 20 20 20 Blob x;.
2360: 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 if( content_get(
2370: 72 69 64 2c 20 26 78 29 20 29 7b 0a 20 20 20 20 rid, &x) ){.
2380: 20 20 53 74 6d 74 20 73 3b 0a 20 20 20 20 20 20 Stmt s;.
2390: 64 62 5f 70 72 65 70 61 72 65 28 26 73 2c 20 22 db_prepare(&s, "
23a0: 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 UPDATE blob SET
23b0: 63 6f 6e 74 65 6e 74 3d 3a 63 2c 20 73 69 7a 65 content=:c, size
23c0: 3d 25 64 20 57 48 45 52 45 20 72 69 64 3d 25 64 =%d WHERE rid=%d
23d0: 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
23e0: 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 69 7a blob_siz
23f0: 65 28 26 78 29 2c 20 72 69 64 29 3b 0a 20 20 20 e(&x), rid);.
2400: 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 blob_compress
2410: 28 26 78 2c 20 26 78 29 3b 0a 20 20 20 20 20 20 (&x, &x);.
2420: 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 2c db_bind_blob(&s,
2430: 20 22 3a 63 22 2c 20 26 78 29 3b 0a 20 20 20 20 ":c", &x);.
2440: 20 20 64 62 5f 65 78 65 63 28 26 73 29 3b 0a 20 db_exec(&s);.
2450: 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 db_finalize
2460: 28 26 73 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 (&s);. blob
2470: 5f 72 65 73 65 74 28 26 78 29 3b 0a 20 20 20 20 _reset(&x);.
2480: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 db_multi_exec(
2490: 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 64 65 6c "DELETE FROM del
24a0: 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 ta WHERE rid=%d"
24b0: 2c 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 , rid);. }.
24c0: 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 }.}../*.** COMMA
24d0: 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e ND: test-conten
24e0: 74 2d 75 6e 64 65 6c 74 61 0a 2a 2a 0a 2a 2a 20 t-undelta.**.**
24f0: 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f Make sure the co
2500: 6e 74 65 6e 74 20 61 74 20 52 45 43 4f 52 44 49 ntent at RECORDI
2510: 44 20 69 73 20 6e 6f 74 20 61 20 64 65 6c 74 61 D is not a delta
2520: 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f .*/.void test_co
2530: 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 5f 63 6d ntent_undelta_cm
2540: 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 d(void){. int r
2550: 69 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 id;. if( g.argc
2560: 21 3d 32 20 29 20 75 73 61 67 65 28 22 52 45 43 !=2 ) usage("REC
2570: 4f 52 44 49 44 22 29 3b 0a 20 20 64 62 5f 6d 75 ORDID");. db_mu
2580: 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 st_be_within_tre
2590: 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 61 74 6f e();. rid = ato
25a0: 69 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 i(g.argv[2]);.
25b0: 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 content_undelta(
25c0: 72 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rid);.}../*.** C
25d0: 68 61 6e 67 65 20 74 68 65 20 73 74 6f 72 61 67 hange the storag
25e0: 65 20 6f 66 20 72 69 64 20 73 6f 20 74 68 61 74 e of rid so that
25f0: 20 69 74 20 69 73 20 61 20 64 65 6c 74 61 20 6f it is a delta o
2600: 66 20 73 72 63 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 f srcid..**.** I
2610: 66 20 72 69 64 20 69 73 20 61 6c 72 65 61 64 79 f rid is already
2620: 20 61 20 64 65 6c 74 61 20 66 72 6f 6d 20 73 6f a delta from so
2630: 6d 65 20 6f 74 68 65 72 20 70 6c 61 63 65 20 74 me other place t
2640: 68 65 6e 20 6e 6f 0a 2a 2a 20 63 6f 6e 76 65 72 hen no.** conver
2650: 73 69 6f 6e 20 6f 63 63 75 72 73 20 61 6e 64 20 sion occurs and
2660: 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 this is a no-op
2670: 75 6e 6c 65 73 73 20 66 6f 72 63 65 3d 3d 31 2e unless force==1.
2680: 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 69 64 20 .**.** If srcid
2690: 69 73 20 61 20 64 65 6c 74 61 20 74 68 61 74 20 is a delta that
26a0: 64 65 70 65 6e 64 73 20 6f 6e 20 72 69 64 2c 20 depends on rid,
26b0: 74 68 65 6e 20 73 72 63 69 64 20 69 73 0a 2a 2a then srcid is.**
26c0: 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 75 6e converted to un
26d0: 64 65 6c 74 61 65 64 20 74 65 78 74 2e 0a 2a 2a deltaed text..**
26e0: 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 72 69 .** If either ri
26f0: 64 20 6f 72 20 73 72 63 69 64 20 63 6f 6e 74 61 d or srcid conta
2700: 69 6e 20 6c 65 73 73 20 74 68 61 6e 20 35 30 20 in less than 50
2710: 62 79 74 65 73 2c 20 6f 72 20 69 66 20 74 68 65 bytes, or if the
2720: 0a 2a 2a 20 72 65 73 75 6c 74 69 6e 67 20 64 65 .** resulting de
2730: 6c 74 61 20 64 6f 65 73 20 6e 6f 74 20 61 63 68 lta does not ach
2740: 69 65 76 65 20 61 20 63 6f 6d 70 72 65 73 73 69 ieve a compressi
2750: 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 32 on of at least 2
2760: 35 25 20 6f 6e 0a 2a 2a 20 69 74 73 20 6f 77 6e 5% on.** its own
2770: 20 74 68 65 20 72 69 64 20 69 73 20 6c 65 66 74 the rid is left
2780: 20 75 6e 74 6f 75 63 68 65 64 2e 0a 2a 2a 0a 2a untouched..**.*
2790: 2a 20 4e 4f 54 45 3a 20 49 4d 48 4f 20 74 68 65 * NOTE: IMHO the
27a0: 20 63 72 65 61 74 69 6f 6e 20 6f 66 20 74 68 65 creation of the
27b0: 20 64 65 6c 74 61 20 73 68 6f 75 6c 64 20 62 65 delta should be
27c0: 20 64 65 66 65 72 65 64 20 75 6e 74 69 6c 20 61 defered until a
27d0: 66 74 65 72 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 fter.** the blob
27e0: 20 73 69 7a 65 73 20 68 61 76 65 20 62 65 65 6e sizes have been
27f0: 20 63 68 65 63 6b 65 64 2e 20 44 6f 69 6e 67 20 checked. Doing
2800: 69 74 20 62 65 66 6f 72 65 20 74 68 65 20 63 68 it before the ch
2810: 65 63 6b 20 61 73 20 69 73 0a 2a 2a 20 64 6f 6e eck as is.** don
2820: 65 20 6e 6f 77 20 74 68 65 20 63 6f 64 65 20 77 e now the code w
2830: 69 6c 6c 20 67 65 6e 65 72 61 74 65 20 61 20 64 ill generate a d
2840: 65 6c 74 61 20 6a 75 73 74 20 74 6f 20 69 6d 6d elta just to imm
2850: 65 64 69 61 74 65 6c 79 20 74 68 72 6f 77 0a 2a ediately throw.*
2860: 2a 20 69 74 20 61 77 61 79 2c 20 77 61 73 74 69 * it away, wasti
2870: 6e 67 20 73 70 61 63 65 20 61 6e 64 20 74 69 6d ng space and tim
2880: 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74 65 e..*/.void conte
2890: 6e 74 5f 64 65 6c 74 69 66 79 28 69 6e 74 20 72 nt_deltify(int r
28a0: 69 64 2c 20 69 6e 74 20 73 72 63 69 64 2c 20 69 id, int srcid, i
28b0: 6e 74 20 66 6f 72 63 65 29 7b 0a 20 20 69 6e 74 nt force){. int
28c0: 20 73 3b 0a 20 20 42 6c 6f 62 20 64 61 74 61 2c s;. Blob data,
28d0: 20 73 72 63 2c 20 64 65 6c 74 61 3b 0a 20 20 53 src, delta;. S
28e0: 74 6d 74 20 73 31 2c 20 73 32 3b 0a 20 20 69 66 tmt s1, s2;. if
28f0: 28 20 73 72 63 69 64 3d 3d 72 69 64 20 29 20 72 ( srcid==rid ) r
2900: 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 66 6f eturn;. if( !fo
2910: 72 63 65 20 26 26 20 66 69 6e 64 53 72 63 69 64 rce && findSrcid
2920: 28 72 69 64 29 3e 30 20 29 20 72 65 74 75 72 6e (rid)>0 ) return
2930: 3b 0a 20 20 73 20 3d 20 73 72 63 69 64 3b 0a 20 ;. s = srcid;.
2940: 20 77 68 69 6c 65 28 20 28 73 20 3d 20 66 69 6e while( (s = fin
2950: 64 53 72 63 69 64 28 73 29 29 3e 30 20 29 7b 0a dSrcid(s))>0 ){.
2960: 20 20 20 20 69 66 28 20 73 3d 3d 72 69 64 20 29 if( s==rid )
2970: 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f {. content_
2980: 75 6e 64 65 6c 74 61 28 73 72 63 69 64 29 3b 0a undelta(srcid);.
2990: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
29a0: 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 74 }. }. content
29b0: 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73 72 63 _get(srcid, &src
29c0: 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 );. content_get
29d0: 28 72 69 64 2c 20 26 64 61 74 61 29 3b 0a 20 20 (rid, &data);.
29e0: 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61 74 blob_delta_creat
29f0: 65 28 26 73 72 63 2c 20 26 64 61 74 61 2c 20 26 e(&src, &data, &
2a00: 64 65 6c 74 61 29 3b 0a 20 20 69 66 28 20 62 6c delta);. if( bl
2a10: 6f 62 5f 73 69 7a 65 28 26 73 72 63 29 3e 3d 35 ob_size(&src)>=5
2a20: 30 20 26 26 20 62 6c 6f 62 5f 73 69 7a 65 28 26 0 && blob_size(&
2a30: 64 61 74 61 29 3e 3d 35 30 20 26 26 0a 20 20 20 data)>=50 &&.
2a40: 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 69 7a blob_siz
2a50: 65 28 26 64 65 6c 74 61 29 20 3c 20 62 6c 6f 62 e(&delta) < blob
2a60: 5f 73 69 7a 65 28 26 64 61 74 61 29 2a 30 2e 37 _size(&data)*0.7
2a70: 35 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 5 ){. blob_co
2a80: 6d 70 72 65 73 73 28 26 64 65 6c 74 61 2c 20 26 mpress(&delta, &
2a90: 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 70 delta);. db_p
2aa0: 72 65 70 61 72 65 28 26 73 31 2c 20 22 55 50 44 repare(&s1, "UPD
2ab0: 41 54 45 20 62 6c 6f 62 20 53 45 54 20 63 6f 6e ATE blob SET con
2ac0: 74 65 6e 74 3d 3a 64 61 74 61 20 57 48 45 52 45 tent=:data WHERE
2ad0: 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a rid=%d", rid);.
2ae0: 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 db_prepare(&
2af0: 73 32 2c 20 22 52 45 50 4c 41 43 45 20 49 4e 54 s2, "REPLACE INT
2b00: 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72 63 69 O delta(rid,srci
2b10: 64 29 56 41 4c 55 45 53 28 25 64 2c 25 64 29 22 d)VALUES(%d,%d)"
2b20: 2c 20 72 69 64 2c 20 73 72 63 69 64 29 3b 0a 20 , rid, srcid);.
2b30: 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 db_bind_blob(
2b40: 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 64 &s1, ":data", &d
2b50: 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 62 65 elta);. db_be
2b60: 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 gin_transaction(
2b70: 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 );. db_exec(&
2b80: 73 31 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 s1);. db_exec
2b90: 28 26 73 32 29 3b 0a 20 20 20 20 64 62 5f 65 6e (&s2);. db_en
2ba0: 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 d_transaction(0)
2bb0: 3b 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a ;. db_finaliz
2bc0: 65 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 66 e(&s1);. db_f
2bd0: 69 6e 61 6c 69 7a 65 28 26 73 32 29 3b 0a 20 20 inalize(&s2);.
2be0: 20 20 76 65 72 69 66 79 5f 62 65 66 6f 72 65 5f verify_before_
2bf0: 63 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20 20 7d commit(rid);. }
2c00: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 . blob_reset(&s
2c10: 72 63 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 rc);. blob_rese
2c20: 74 28 26 64 61 74 61 29 3b 0a 20 20 62 6c 6f 62 t(&data);. blob
2c30: 5f 72 65 73 65 74 28 26 64 65 6c 74 61 29 3b 0a _reset(&delta);.
2c40: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 }../*.** COMMAND
2c50: 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d : test-content-
2c60: 64 65 6c 74 69 66 79 0a 2a 2a 0a 2a 2a 20 43 6f deltify.**.** Co
2c70: 6e 76 65 72 74 20 74 68 65 20 63 6f 6e 74 65 6e nvert the conten
2c80: 74 20 61 74 20 52 49 44 20 69 6e 74 6f 20 61 20 t at RID into a
2c90: 64 65 6c 74 61 20 66 72 6f 6d 20 53 52 43 49 44 delta from SRCID
2ca0: 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 ..*/.void test_c
2cb0: 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 5f 63 ontent_deltify_c
2cc0: 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 md(void){. if(
2cd0: 67 2e 61 72 67 63 21 3d 35 20 29 20 75 73 61 67 g.argc!=5 ) usag
2ce0: 65 28 22 52 49 44 20 53 52 43 49 44 20 46 4f 52 e("RID SRCID FOR
2cf0: 43 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f CE");. db_must_
2d00: 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 be_within_tree()
2d10: 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 ;. content_delt
2d20: 69 66 79 28 61 74 6f 69 28 67 2e 61 72 67 76 5b ify(atoi(g.argv[
2d30: 32 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67 76 2]), atoi(g.argv
2d40: 5b 33 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67 [3]), atoi(g.arg
2d50: 76 5b 34 5d 29 29 3b 0a 7d 0a v[4]));.}.