Hex Artifact Content
Not logged in

Artifact 02e1a3dfd6fd5643b0d52a459e5f0f3951f2519e:

File src/content.c part of check-in [574763bab9] - Added revision support to diff and revert by jnc on 2007-09-26 03:37:12. Also file src/content.c part of check-in [8d55aa3597] - Merged in new revision support for diff and revert commands into mainstream by jnc on 2007-09-26 03:38:58.

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 47 65 74 20 74  ;.}../*.** Get t
08e0: 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61  he contents of a
08f0: 20 66 69 6c 65 20 77 69 74 68 69 6e 20 61 20 67   file within a g
0900: 69 76 65 6e 20 72 65 76 69 73 69 6f 6e 2e 0a 2a  iven revision..*
0910: 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 67 65  /.int content_ge
0920: 74 5f 68 69 73 74 6f 72 69 63 61 6c 5f 66 69 6c  t_historical_fil
0930: 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 65  e(const char *re
0940: 76 69 73 69 6f 6e 2c 20 63 6f 6e 73 74 20 63 68  vision, const ch
0950: 61 72 20 2a 66 69 6c 65 2c 20 42 6c 6f 62 20 2a  ar *file, Blob *
0960: 63 6f 6e 74 65 6e 74 29 7b 0a 20 20 42 6c 6f 62  content){.  Blob
0970: 20 6d 66 69 6c 65 3b 0a 20 20 4d 61 6e 69 66 65   mfile;.  Manife
0980: 73 74 20 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 72  st m;.  int i, r
0990: 69 64 3d 30 3b 0a 20 20 0a 20 20 72 69 64 20 3d  id=0;.  .  rid =
09a0: 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 72 65 76   name_to_rid(rev
09b0: 69 73 69 6f 6e 29 3b 0a 20 20 63 6f 6e 74 65 6e  ision);.  conten
09c0: 74 5f 67 65 74 28 72 69 64 2c 20 26 6d 66 69 6c  t_get(rid, &mfil
09d0: 65 29 3b 0a 20 20 0a 20 20 69 66 28 20 6d 61 6e  e);.  .  if( man
09e0: 69 66 65 73 74 5f 70 61 72 73 65 28 26 6d 2c 20  ifest_parse(&m, 
09f0: 26 6d 66 69 6c 65 29 20 29 7b 0a 20 20 20 20 66  &mfile) ){.    f
0a00: 6f 72 28 69 3d 30 3b 20 69 3c 6d 2e 6e 46 69 6c  or(i=0; i<m.nFil
0a10: 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  e; i++){.      i
0a20: 66 28 20 73 74 72 63 6d 70 28 6d 2e 61 46 69 6c  f( strcmp(m.aFil
0a30: 65 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 66 69 6c 65  e[i].zName, file
0a40: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
0a50: 72 69 64 20 3d 20 75 75 69 64 5f 74 6f 5f 72 69  rid = uuid_to_ri
0a60: 64 28 6d 2e 61 46 69 6c 65 5b 69 5d 2e 7a 55 75  d(m.aFile[i].zUu
0a70: 69 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  id, 0);.        
0a80: 72 65 74 75 72 6e 20 63 6f 6e 74 65 6e 74 5f 67  return content_g
0a90: 65 74 28 72 69 64 2c 20 63 6f 6e 74 65 6e 74 29  et(rid, content)
0aa0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
0ab0: 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63      fossil_panic
0ac0: 28 22 66 69 6c 65 3a 20 25 73 20 64 6f 65 73 20  ("file: %s does 
0ad0: 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 72 65 76  not exist in rev
0ae0: 69 73 69 6f 6e 3a 20 25 73 22 2c 20 66 69 6c 65  ision: %s", file
0af0: 2c 20 72 65 76 69 73 69 6f 6e 29 3b 0a 20 20 7d  , revision);.  }
0b00: 65 6c 73 65 7b 0a 20 20 20 20 66 6f 73 73 69 6c  else{.    fossil
0b10: 5f 70 61 6e 69 63 28 22 63 6f 75 6c 64 20 6e 6f  _panic("could no
0b20: 74 20 70 61 72 73 65 20 6d 61 6e 69 66 65 73 74  t parse manifest
0b30: 20 66 6f 72 20 72 65 76 69 73 69 6f 6e 3a 20 25   for revision: %
0b40: 73 22 2c 20 72 65 76 69 73 69 6f 6e 29 3b 0a 20  s", revision);. 
0b50: 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 30   }.  .  return 0
0b60: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41  ;.}../*.** COMMA
0b70: 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e  ND:  test-conten
0b80: 74 2d 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72  t-get.**.** Extr
0b90: 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20  act a blob from 
0ba0: 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64  the database and
0bb0: 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61   write it into a
0bc0: 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74   file..*/.void t
0bd0: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 67 65 74 5f  est_content_get_
0be0: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  cmd(void){.  int
0bf0: 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e   rid;.  Blob con
0c00: 74 65 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  tent;.  const ch
0c10: 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28  ar *zFile;.  if(
0c20: 20 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e   g.argc!=4 && g.
0c30: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28  argc!=3 ) usage(
0c40: 22 52 45 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e  "RECORDID ?FILEN
0c50: 41 4d 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20  AME?");.  zFile 
0c60: 3d 20 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e  = g.argc==4 ? g.
0c70: 61 72 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20  argv[3] : "-";. 
0c80: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68   db_must_be_with
0c90: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64  in_tree();.  rid
0ca0: 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67   = name_to_rid(g
0cb0: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e  .argv[2]);.  con
0cc0: 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 63  tent_get(rid, &c
0cd0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f  ontent);.  blob_
0ce0: 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63  write_to_file(&c
0cf0: 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a  ontent, zFile);.
0d00: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44  }../*.** COMMAND
0d10: 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d  :  test-content-
0d20: 72 61 77 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74  rawget.**.** Ext
0d30: 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d  ract a blob from
0d40: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
0d50: 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20  d write it into 
0d60: 61 20 66 69 6c 65 2e 20 20 54 68 69 73 0a 2a 2a  a file.  This.**
0d70: 20 76 65 72 73 69 6f 6e 20 64 6f 65 73 20 6e 6f   version does no
0d80: 74 20 65 78 70 61 6e 64 20 74 68 65 20 64 65 6c  t expand the del
0d90: 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74  ta..*/.void test
0da0: 5f 63 6f 6e 74 65 6e 74 5f 72 61 77 67 65 74 5f  _content_rawget_
0db0: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  cmd(void){.  int
0dc0: 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e   rid;.  Blob con
0dd0: 74 65 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  tent;.  const ch
0de0: 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28  ar *zFile;.  if(
0df0: 20 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e   g.argc!=4 && g.
0e00: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28  argc!=3 ) usage(
0e10: 22 52 45 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e  "RECORDID ?FILEN
0e20: 41 4d 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20  AME?");.  zFile 
0e30: 3d 20 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e  = g.argc==4 ? g.
0e40: 61 72 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20  argv[3] : "-";. 
0e50: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68   db_must_be_with
0e60: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64  in_tree();.  rid
0e70: 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67   = name_to_rid(g
0e80: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 62 6c 6f  .argv[2]);.  blo
0e90: 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29  b_zero(&content)
0ea0: 3b 0a 20 20 64 62 5f 62 6c 6f 62 28 26 63 6f 6e  ;.  db_blob(&con
0eb0: 74 65 6e 74 2c 20 22 53 45 4c 45 43 54 20 63 6f  tent, "SELECT co
0ec0: 6e 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20  ntent FROM blob 
0ed0: 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72  WHERE rid=%d", r
0ee0: 69 64 29 3b 0a 20 20 62 6c 6f 62 5f 75 6e 63 6f  id);.  blob_unco
0ef0: 6d 70 72 65 73 73 28 26 63 6f 6e 74 65 6e 74 2c  mpress(&content,
0f00: 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c   &content);.  bl
0f10: 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65  ob_write_to_file
0f20: 28 26 63 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65  (&content, zFile
0f30: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  );.}../*.** When
0f40: 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 6f 6e   a record is con
0f50: 76 65 72 74 65 64 20 66 72 6f 6d 20 61 20 70 68  verted from a ph
0f60: 61 6e 74 6f 6d 20 74 6f 20 61 20 72 65 61 6c 20  antom to a real 
0f70: 72 65 63 6f 72 64 2c 0a 2a 2a 20 69 66 20 74 68  record,.** if th
0f80: 61 74 20 72 65 63 6f 72 64 20 68 61 73 20 6f 74  at record has ot
0f90: 68 65 72 20 72 65 63 6f 72 64 73 20 74 68 61 74  her records that
0fa0: 20 61 72 65 20 64 65 72 69 76 65 64 20 62 79 20   are derived by 
0fb0: 64 65 6c 74 61 2c 0a 2a 2a 20 74 68 65 6e 20 63  delta,.** then c
0fc0: 61 6c 6c 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f  all manifest_cro
0fd0: 73 73 6c 69 6e 6b 28 29 20 6f 6e 20 74 68 6f 73  sslink() on thos
0fe0: 65 20 6f 74 68 65 72 20 72 65 63 6f 72 64 73 2e  e other records.
0ff0: 0a 2a 2f 0a 76 6f 69 64 20 61 66 74 65 72 5f 64  .*/.void after_d
1000: 65 70 68 61 6e 74 6f 6d 69 7a 65 28 69 6e 74 20  ephantomize(int 
1010: 72 69 64 2c 20 69 6e 74 20 6c 69 6e 6b 46 6c 61  rid, int linkFla
1020: 67 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20  g){.  Stmt q;.  
1030: 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22  db_prepare(&q, "
1040: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20  SELECT rid FROM 
1050: 64 65 6c 74 61 20 57 48 45 52 45 20 73 72 63 69  delta WHERE srci
1060: 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 77  d=%d", rid);.  w
1070: 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71  hile( db_step(&q
1080: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
1090: 0a 20 20 20 20 69 6e 74 20 74 69 64 20 3d 20 64  .    int tid = d
10a0: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c  b_column_int(&q,
10b0: 20 30 29 3b 0a 20 20 20 20 61 66 74 65 72 5f 64   0);.    after_d
10c0: 65 70 68 61 6e 74 6f 6d 69 7a 65 28 74 69 64 2c  ephantomize(tid,
10d0: 20 31 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69   1);.  }.  db_fi
10e0: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 69 66  nalize(&q);.  if
10f0: 28 20 6c 69 6e 6b 46 6c 61 67 20 29 7b 0a 20 20  ( linkFlag ){.  
1100: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a    Blob content;.
1110: 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28      content_get(
1120: 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a  rid, &content);.
1130: 20 20 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f      manifest_cro
1140: 73 73 6c 69 6e 6b 28 72 69 64 2c 20 26 63 6f 6e  sslink(rid, &con
1150: 74 65 6e 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  tent);.    blob_
1160: 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b  reset(&content);
1170: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  .  }.}../*.** Wr
1180: 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f  ite content into
1190: 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
11a0: 52 65 74 75 72 6e 20 74 68 65 20 72 65 63 6f 72  Return the recor
11b0: 64 20 49 44 2e 20 20 49 66 20 74 68 65 0a 2a 2a  d ID.  If the.**
11c0: 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 6c 72 65   content is alre
11d0: 61 64 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  ady in the datab
11e0: 61 73 65 2c 20 6a 75 73 74 20 72 65 74 75 72 6e  ase, just return
11f0: 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e 0a   the record ID..
1200: 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 49 64 20 69  **.** If srcId i
1210: 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65  s specified, the
1220: 6e 20 70 42 6c 6f 62 20 69 73 20 64 65 6c 74 61  n pBlob is delta
1230: 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 0a 2a 2a   content from.**
1240: 20 74 68 65 20 73 72 63 49 64 20 72 65 63 6f 72   the srcId recor
1250: 64 2e 20 20 73 72 63 49 64 20 6d 69 67 68 74 20  d.  srcId might 
1260: 62 65 20 61 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2a  be a phantom..**
1270: 0a 2a 2a 20 41 20 70 68 61 6e 74 6f 6d 20 69 73  .** A phantom is
1280: 20 77 72 69 74 74 65 6e 20 69 66 20 70 42 6c 6f   written if pBlo
1290: 62 3d 3d 30 2e 20 20 49 66 20 70 42 6c 6f 62 3d  b==0.  If pBlob=
12a0: 3d 30 20 6f 72 20 69 66 20 73 72 63 49 64 20 69  =0 or if srcId i
12b0: 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 74  s.** specified t
12c0: 68 65 6e 20 74 68 65 20 55 55 49 44 20 69 73 20  hen the UUID is 
12d0: 73 65 74 20 74 6f 20 7a 55 75 69 64 2e 20 20 4f  set to zUuid.  O
12e0: 74 68 65 72 77 69 73 65 20 7a 55 75 69 64 20 69  therwise zUuid i
12f0: 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49  s.** ignored.  I
1300: 6e 20 74 68 65 20 66 75 74 75 72 65 20 74 68 69  n the future thi
1310: 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 73  s might change s
1320: 75 63 68 20 74 68 61 74 20 74 68 65 20 63 6f 6e  uch that the con
1330: 74 65 6e 74 0a 2a 2a 20 68 61 73 68 20 69 73 20  tent.** hash is 
1340: 63 68 65 63 6b 65 64 20 61 67 61 69 6e 73 74 20  checked against 
1350: 7a 55 75 69 64 20 74 6f 20 6d 61 6b 65 20 73 75  zUuid to make su
1360: 72 65 20 69 74 20 69 73 20 63 6f 72 72 65 63 74  re it is correct
1370: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
1380: 65 63 6f 72 64 20 61 6c 72 65 61 64 79 20 65 78  ecord already ex
1390: 69 73 74 73 20 62 75 74 20 69 73 20 61 20 70 68  ists but is a ph
13a0: 61 6e 74 6f 6d 2c 20 74 68 65 20 70 42 6c 6f 62  antom, the pBlob
13b0: 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 69   content.** is i
13c0: 6e 73 65 72 74 65 64 20 61 6e 64 20 74 68 65 20  nserted and the 
13d0: 70 68 61 74 6f 6d 20 62 65 63 6f 6d 65 73 20 61  phatom becomes a
13e0: 20 72 65 61 6c 20 72 65 63 6f 72 64 2e 0a 2a 2f   real record..*/
13f0: 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 70 75 74  .int content_put
1400: 28 42 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f  (Blob *pBlob, co
1410: 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 2c  nst char *zUuid,
1420: 20 69 6e 74 20 73 72 63 49 64 29 7b 0a 20 20 69   int srcId){.  i
1430: 6e 74 20 73 69 7a 65 3b 0a 20 20 69 6e 74 20 72  nt size;.  int r
1440: 69 64 3b 0a 20 20 53 74 6d 74 20 73 31 3b 0a 20  id;.  Stmt s1;. 
1450: 20 42 6c 6f 62 20 63 6d 70 72 3b 0a 20 20 42 6c   Blob cmpr;.  Bl
1460: 6f 62 20 68 61 73 68 3b 0a 20 20 69 6e 74 20 6d  ob hash;.  int m
1470: 61 72 6b 41 73 55 6e 63 6c 75 73 74 65 72 65 64  arkAsUnclustered
1480: 20 3d 20 30 3b 0a 20 20 0a 20 20 61 73 73 65 72   = 0;.  .  asser
1490: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  t( g.repositoryO
14a0: 70 65 6e 20 29 3b 0a 20 20 69 66 28 20 70 42 6c  pen );.  if( pBl
14b0: 6f 62 20 26 26 20 73 72 63 49 64 3d 3d 30 20 29  ob && srcId==0 )
14c0: 7b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 6c  {.    sha1sum_bl
14d0: 6f 62 28 70 42 6c 6f 62 2c 20 26 68 61 73 68 29  ob(pBlob, &hash)
14e0: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62  ;.  }else{.    b
14f0: 6c 6f 62 5f 69 6e 69 74 28 26 68 61 73 68 2c 20  lob_init(&hash, 
1500: 7a 55 75 69 64 2c 20 2d 31 29 3b 0a 20 20 7d 0a  zUuid, -1);.  }.
1510: 20 20 69 66 28 20 70 42 6c 6f 62 3d 3d 30 20 29    if( pBlob==0 )
1520: 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 2d 31 3b  {.    size = -1;
1530: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 69  .  }else{.    si
1540: 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28 70  ze = blob_size(p
1550: 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 64 62 5f  Blob);.  }.  db_
1560: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f  begin_transactio
1570: 6e 28 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  n();..  /* Check
1580: 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 65   to see if the e
1590: 6e 74 72 79 20 61 6c 72 65 61 64 79 20 65 78 69  ntry already exi
15a0: 73 74 73 20 61 6e 64 20 69 66 20 69 74 20 64 6f  sts and if it do
15b0: 65 73 20 77 68 65 74 68 65 72 0a 20 20 2a 2a 20  es whether.  ** 
15c0: 6f 72 20 6e 6f 74 20 74 68 65 20 65 6e 74 72 79  or not the entry
15d0: 20 69 73 20 61 20 70 68 61 6e 74 6f 6d 0a 20 20   is a phantom.  
15e0: 2a 2f 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28  */.  db_prepare(
15f0: 26 73 31 2c 20 22 53 45 4c 45 43 54 20 72 69 64  &s1, "SELECT rid
1600: 2c 20 73 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62  , size FROM blob
1610: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c   WHERE uuid=%B",
1620: 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 64   &hash);.  if( d
1630: 62 5f 73 74 65 70 28 26 73 31 29 3d 3d 53 51 4c  b_step(&s1)==SQL
1640: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72  ITE_ROW ){.    r
1650: 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69  id = db_column_i
1660: 6e 74 28 26 73 31 2c 20 30 29 3b 0a 20 20 20 20  nt(&s1, 0);.    
1670: 69 66 28 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  if( db_column_in
1680: 74 28 26 73 31 2c 20 31 29 3e 3d 30 20 7c 7c 20  t(&s1, 1)>=0 || 
1690: 70 42 6c 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20  pBlob==0 ){.    
16a0: 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20    /* Either the 
16b0: 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 61 20 70  entry is not a p
16c0: 68 61 6e 74 6f 6d 20 6f 72 20 69 74 20 69 73 20  hantom or it is 
16d0: 61 20 70 68 61 6e 74 6f 6d 20 62 75 74 20 77 65  a phantom but we
16e0: 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 6e  .      ** have n
16f0: 6f 20 64 61 74 61 20 77 69 74 68 20 77 68 69 63  o data with whic
1700: 68 20 74 6f 20 64 65 70 68 61 6e 74 6f 6d 69 7a  h to dephantomiz
1710: 65 20 69 74 2e 20 20 49 6e 20 65 69 74 68 65 72  e it.  In either
1720: 20 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20   case,.      ** 
1730: 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67  there is nothing
1740: 20 66 6f 72 20 75 73 20 74 6f 20 64 6f 20 6f 74   for us to do ot
1750: 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20  her than return 
1760: 74 68 65 20 52 49 44 2e 20 2a 2f 0a 20 20 20 20  the RID. */.    
1770: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
1780: 31 29 3b 0a 20 20 20 20 20 20 64 62 5f 65 6e 64  1);.      db_end
1790: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
17a0: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 69  .      return ri
17b0: 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  d;.    }.  }else
17c0: 7b 0a 20 20 20 20 72 69 64 20 3d 20 30 3b 20 20  {.    rid = 0;  
17d0: 2f 2a 20 4e 6f 20 65 6e 74 72 79 20 77 69 74 68  /* No entry with
17e0: 20 74 68 65 20 73 61 6d 65 20 55 55 49 44 20 63   the same UUID c
17f0: 75 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20  urrently exists 
1800: 2a 2f 0a 20 20 20 20 6d 61 72 6b 41 73 55 6e 63  */.    markAsUnc
1810: 6c 75 73 74 65 72 65 64 20 3d 20 31 3b 0a 20 20  lustered = 1;.  
1820: 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28  }.  db_finalize(
1830: 26 73 31 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73  &s1);..  /* Cons
1840: 74 72 75 63 74 20 61 20 72 65 63 65 69 76 65 64  truct a received
1850: 2d 66 72 6f 6d 20 49 44 20 69 66 20 77 65 20 64  -from ID if we d
1860: 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 61  o not already ha
1870: 76 65 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20  ve one */.  if( 
1880: 67 2e 72 63 76 69 64 3d 3d 30 20 26 26 20 70 42  g.rcvid==0 && pB
1890: 6c 6f 62 21 3d 30 20 29 7b 0a 20 20 20 20 64 62  lob!=0 ){.    db
18a0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
18b0: 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
18c0: 20 72 63 76 66 72 6f 6d 28 75 69 64 2c 20 6d 74   rcvfrom(uid, mt
18d0: 69 6d 65 2c 20 6e 6f 6e 63 65 2c 20 69 70 61 64  ime, nonce, ipad
18e0: 64 72 29 22 0a 20 20 20 20 20 20 20 22 56 41 4c  dr)".       "VAL
18f0: 55 45 53 28 25 64 2c 20 6a 75 6c 69 61 6e 64 61  UES(%d, julianda
1900: 79 28 27 6e 6f 77 27 29 2c 20 25 51 2c 20 25 51  y('now'), %Q, %Q
1910: 29 22 2c 0a 20 20 20 20 20 20 20 67 2e 75 73 65  )",.       g.use
1920: 72 55 69 64 2c 20 67 2e 7a 4e 6f 6e 63 65 2c 20  rUid, g.zNonce, 
1930: 67 2e 7a 49 70 41 64 64 72 0a 20 20 20 20 29 3b  g.zIpAddr.    );
1940: 0a 20 20 20 20 67 2e 72 63 76 69 64 20 3d 20 64  .    g.rcvid = d
1950: 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  b_last_insert_ro
1960: 77 69 64 28 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  wid();.  }..  if
1970: 28 20 72 69 64 3e 30 20 29 7b 0a 20 20 20 20 2f  ( rid>0 ){.    /
1980: 2a 20 57 65 20 61 72 65 20 6a 75 73 74 20 61 64  * We are just ad
1990: 64 69 6e 67 20 64 61 74 61 20 74 6f 20 61 20 70  ding data to a p
19a0: 68 61 6e 74 6f 6d 20 2a 2f 0a 20 20 20 20 61 73  hantom */.    as
19b0: 73 65 72 74 28 20 70 42 6c 6f 62 21 3d 30 20 29  sert( pBlob!=0 )
19c0: 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65  ;.    db_prepare
19d0: 28 26 73 31 2c 0a 20 20 20 20 20 20 22 55 50 44  (&s1,.      "UPD
19e0: 41 54 45 20 62 6c 6f 62 20 53 45 54 20 72 63 76  ATE blob SET rcv
19f0: 69 64 3d 25 64 2c 20 73 69 7a 65 3d 25 64 2c 20  id=%d, size=%d, 
1a00: 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57 48  content=:data WH
1a10: 45 52 45 20 72 69 64 3d 25 64 22 2c 0a 20 20 20  ERE rid=%d",.   
1a20: 20 20 20 20 67 2e 72 63 76 69 64 2c 20 73 69 7a      g.rcvid, siz
1a30: 65 2c 20 72 69 64 0a 20 20 20 20 29 3b 0a 20 20  e, rid.    );.  
1a40: 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28    blob_compress(
1a50: 70 42 6c 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20  pBlob, &cmpr);. 
1a60: 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28     db_bind_blob(
1a70: 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 63  &s1, ":data", &c
1a80: 6d 70 72 29 3b 0a 20 20 20 20 64 62 5f 65 78 65  mpr);.    db_exe
1a90: 63 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 6d  c(&s1);.    db_m
1aa0: 75 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54  ulti_exec("DELET
1ab0: 45 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 57  E FROM phantom W
1ac0: 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69  HERE rid=%d", ri
1ad0: 64 29 3b 0a 20 20 20 20 69 66 28 20 73 72 63 49  d);.    if( srcI
1ae0: 64 3d 3d 30 20 7c 7c 20 64 62 5f 69 6e 74 28 30  d==0 || db_int(0
1af0: 2c 20 22 53 45 4c 45 43 54 20 73 69 7a 65 20 46  , "SELECT size F
1b00: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72  ROM blob WHERE r
1b10: 69 64 3d 25 64 22 2c 20 73 72 63 49 64 29 3e 30  id=%d", srcId)>0
1b20: 20 29 7b 0a 20 20 20 20 20 20 61 66 74 65 72 5f   ){.      after_
1b30: 64 65 70 68 61 6e 74 6f 6d 69 7a 65 28 72 69 64  dephantomize(rid
1b40: 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  , 0);.    }.  }e
1b50: 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61  lse{.    /* We a
1b60: 72 65 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65  re creating a ne
1b70: 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20 20 64  w entry */.    d
1b80: 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c 0a 20  b_prepare(&s1,. 
1b90: 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54       "INSERT INT
1ba0: 4f 20 62 6c 6f 62 28 72 63 76 69 64 2c 73 69 7a  O blob(rcvid,siz
1bb0: 65 2c 75 75 69 64 2c 63 6f 6e 74 65 6e 74 29 22  e,uuid,content)"
1bc0: 0a 20 20 20 20 20 20 22 56 41 4c 55 45 53 28 25  .      "VALUES(%
1bd0: 64 2c 25 64 2c 27 25 62 27 2c 3a 64 61 74 61 29  d,%d,'%b',:data)
1be0: 22 2c 0a 20 20 20 20 20 20 20 67 2e 72 63 76 69  ",.       g.rcvi
1bf0: 64 2c 20 73 69 7a 65 2c 20 26 68 61 73 68 0a 20  d, size, &hash. 
1c00: 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 70 42     );.    if( pB
1c10: 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f  lob ){.      blo
1c20: 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f 62  b_compress(pBlob
1c30: 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 20 20  , &cmpr);.      
1c40: 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31  db_bind_blob(&s1
1c50: 2c 20 22 3a 64 61 74 61 22 2c 20 26 63 6d 70 72  , ":data", &cmpr
1c60: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f  );.    }.    db_
1c70: 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20 20 72  exec(&s1);.    r
1c80: 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69 6e 73  id = db_last_ins
1c90: 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20 20 20  ert_rowid();.   
1ca0: 20 69 66 28 20 21 70 42 6c 6f 62 20 29 7b 0a 20   if( !pBlob ){. 
1cb0: 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78       db_multi_ex
1cc0: 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20 49 47  ec("INSERT OR IG
1cd0: 4e 4f 52 45 20 49 4e 54 4f 20 70 68 61 6e 74 6f  NORE INTO phanto
1ce0: 6d 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72  m VALUES(%d)", r
1cf0: 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  id);.    }.  }..
1d00: 20 20 2f 2a 20 49 66 20 74 68 65 20 73 72 63 49    /* If the srcI
1d10: 64 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20  d is specified, 
1d20: 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 77 65  then the data we
1d30: 20 6a 75 73 74 20 61 64 64 65 64 20 69 73 0a 20   just added is. 
1d40: 20 2a 2a 20 72 65 61 6c 6c 79 20 61 20 64 65 6c   ** really a del
1d50: 74 61 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73  ta.  Record this
1d60: 20 66 61 63 74 20 69 6e 20 74 68 65 20 64 65 6c   fact in the del
1d70: 74 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20  ta table..  */. 
1d80: 20 69 66 28 20 73 72 63 49 64 20 29 7b 0a 20 20   if( srcId ){.  
1d90: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
1da0: 22 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 64 65  "REPLACE INTO de
1db0: 6c 74 61 28 72 69 64 2c 73 72 63 69 64 29 20 56  lta(rid,srcid) V
1dc0: 41 4c 55 45 53 28 25 64 2c 25 64 29 22 2c 20 72  ALUES(%d,%d)", r
1dd0: 69 64 2c 20 73 72 63 49 64 29 3b 0a 20 20 7d 0a  id, srcId);.  }.
1de0: 20 20 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20    .  /* Add the 
1df0: 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 75  element to the u
1e00: 6e 63 6c 75 73 74 65 72 65 64 20 74 61 62 6c 65  nclustered table
1e10: 20 69 66 20 68 61 73 20 6e 65 76 65 72 20 62 65   if has never be
1e20: 65 6e 0a 20 20 2a 2a 20 70 72 65 76 69 6f 75 73  en.  ** previous
1e30: 6c 79 20 73 65 65 6e 2e 0a 20 20 2a 2f 0a 20 20  ly seen..  */.  
1e40: 69 66 28 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73  if( markAsUnclus
1e50: 74 65 72 65 64 20 29 7b 0a 20 20 20 20 64 62 5f  tered ){.    db_
1e60: 6d 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45  multi_exec("INSE
1e70: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54  RT OR IGNORE INT
1e80: 4f 20 75 6e 63 6c 75 73 74 65 72 65 64 20 56 41  O unclustered VA
1e90: 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b  LUES(%d)", rid);
1ea0: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 73  .  }..  /* Finis
1eb0: 68 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  h the transactio
1ec0: 6e 20 61 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f  n and cleanup */
1ed0: 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26  .  db_finalize(&
1ee0: 73 31 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72  s1);.  db_end_tr
1ef0: 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20  ansaction(0);.  
1f00: 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68  blob_reset(&hash
1f10: 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72  );..  /* Make ar
1f20: 72 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 76 65  rangements to ve
1f30: 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64 61  rify that the da
1f40: 74 61 20 63 61 6e 20 62 65 20 72 65 63 6f 76 65  ta can be recove
1f50: 72 65 64 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20  red.  ** before 
1f60: 77 65 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 69  we commit */.  i
1f70: 66 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20  f( pBlob ){.    
1f80: 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6d 70 72  blob_reset(&cmpr
1f90: 29 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 65  );.    verify_be
1fa0: 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29  fore_commit(rid)
1fb0: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1fc0: 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d  id;.}../*.** COM
1fd0: 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74  MAND:  test-cont
1fe0: 65 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a 20 45 78  ent-put.**.** Ex
1ff0: 74 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f  tract a blob fro
2000: 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  m the database a
2010: 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f  nd write it into
2020: 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64   a file..*/.void
2030: 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 70 75   test_content_pu
2040: 74 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69  t_cmd(void){.  i
2050: 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63  nt rid;.  Blob c
2060: 6f 6e 74 65 6e 74 3b 0a 20 20 69 66 28 20 67 2e  ontent;.  if( g.
2070: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28  argc!=3 ) usage(
2080: 22 46 49 4c 45 4e 41 4d 45 22 29 3b 0a 20 20 64  "FILENAME");.  d
2090: 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e  b_must_be_within
20a0: 5f 74 72 65 65 28 29 3b 0a 20 20 75 73 65 72 5f  _tree();.  user_
20b0: 73 65 6c 65 63 74 28 29 3b 0a 20 20 62 6c 6f 62  select();.  blob
20c0: 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28  _read_from_file(
20d0: 26 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 72 67 76  &content, g.argv
20e0: 5b 32 5d 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f  [2]);.  rid = co
20f0: 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65  ntent_put(&conte
2100: 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 70 72 69  nt, 0, 0);.  pri
2110: 6e 74 66 28 22 69 6e 73 65 72 74 65 64 20 61 73  ntf("inserted as
2120: 20 72 65 63 6f 72 64 20 25 64 5c 6e 22 2c 20 72   record %d\n", r
2130: 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  id);.}../*.** Ma
2140: 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e 74  ke sure the cont
2150: 65 6e 74 20 61 74 20 72 69 64 20 69 73 20 74 68  ent at rid is th
2160: 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65  e original conte
2170: 6e 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 0a  nt and is not a.
2180: 2a 2a 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69  ** delta..*/.voi
2190: 64 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74  d content_undelt
21a0: 61 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 66  a(int rid){.  if
21b0: 28 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 29  ( findSrcid(rid)
21c0: 3e 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 78  >0 ){.    Blob x
21d0: 3b 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e  ;.    if( conten
21e0: 74 5f 67 65 74 28 72 69 64 2c 20 26 78 29 20 29  t_get(rid, &x) )
21f0: 7b 0a 20 20 20 20 20 20 53 74 6d 74 20 73 3b 0a  {.      Stmt s;.
2200: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65        db_prepare
2210: 28 26 73 2c 20 22 55 50 44 41 54 45 20 62 6c 6f  (&s, "UPDATE blo
2220: 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 63  b SET content=:c
2230: 2c 20 73 69 7a 65 3d 25 64 20 57 48 45 52 45 20  , size=%d WHERE 
2240: 72 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 20  rid=%d",.       
2250: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c                bl
2260: 6f 62 5f 73 69 7a 65 28 26 78 29 2c 20 72 69 64  ob_size(&x), rid
2270: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f  );.      blob_co
2280: 6d 70 72 65 73 73 28 26 78 2c 20 26 78 29 3b 0a  mpress(&x, &x);.
2290: 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c        db_bind_bl
22a0: 6f 62 28 26 73 2c 20 22 3a 63 22 2c 20 26 78 29  ob(&s, ":c", &x)
22b0: 3b 0a 20 20 20 20 20 20 64 62 5f 65 78 65 63 28  ;.      db_exec(
22c0: 26 73 29 3b 0a 20 20 20 20 20 20 64 62 5f 66 69  &s);.      db_fi
22d0: 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 20 20  nalize(&s);.    
22e0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 78 29    blob_reset(&x)
22f0: 3b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69  ;.      db_multi
2300: 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52  _exec("DELETE FR
2310: 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72  OM delta WHERE r
2320: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20  id=%d", rid);.  
2330: 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2340: 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d   COMMAND:  test-
2350: 63 6f 6e 74 65 6e 74 2d 75 6e 64 65 6c 74 61 0a  content-undelta.
2360: 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  **.** Make sure 
2370: 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 52  the content at R
2380: 45 43 4f 52 44 49 44 20 69 73 20 6e 6f 74 20 61  ECORDID is not a
2390: 20 64 65 6c 74 61 0a 2a 2f 0a 76 6f 69 64 20 74   delta.*/.void t
23a0: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 75 6e 64 65  est_content_unde
23b0: 6c 74 61 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  lta_cmd(void){. 
23c0: 20 69 6e 74 20 72 69 64 3b 0a 20 20 69 66 28 20   int rid;.  if( 
23d0: 67 2e 61 72 67 63 21 3d 32 20 29 20 75 73 61 67  g.argc!=2 ) usag
23e0: 65 28 22 52 45 43 4f 52 44 49 44 22 29 3b 0a 20  e("RECORDID");. 
23f0: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68   db_must_be_with
2400: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64  in_tree();.  rid
2410: 20 3d 20 61 74 6f 69 28 67 2e 61 72 67 76 5b 32   = atoi(g.argv[2
2420: 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 75 6e  ]);.  content_un
2430: 64 65 6c 74 61 28 72 69 64 29 3b 0a 7d 0a 0a 2f  delta(rid);.}../
2440: 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
2450: 73 74 6f 72 61 67 65 20 6f 66 20 72 69 64 20 73  storage of rid s
2460: 6f 20 74 68 61 74 20 69 74 20 69 73 20 61 20 64  o that it is a d
2470: 65 6c 74 61 20 6f 66 20 73 72 63 69 64 2e 0a 2a  elta of srcid..*
2480: 2a 0a 2a 2a 20 49 66 20 72 69 64 20 69 73 20 61  *.** If rid is a
2490: 6c 72 65 61 64 79 20 61 20 64 65 6c 74 61 20 66  lready a delta f
24a0: 72 6f 6d 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  rom some other p
24b0: 6c 61 63 65 20 74 68 65 6e 20 6e 6f 0a 2a 2a 20  lace then no.** 
24c0: 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72  conversion occur
24d0: 73 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20  s and this is a 
24e0: 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 66 6f 72  no-op unless for
24f0: 63 65 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ce==1..**.** If 
2500: 73 72 63 69 64 20 69 73 20 61 20 64 65 6c 74 61  srcid is a delta
2510: 20 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f 6e   that depends on
2520: 20 72 69 64 2c 20 74 68 65 6e 20 73 72 63 69 64   rid, then srcid
2530: 20 69 73 0a 2a 2a 20 63 6f 6e 76 65 72 74 65 64   is.** converted
2540: 20 74 6f 20 75 6e 64 65 6c 74 61 65 64 20 74 65   to undeltaed te
2550: 78 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74  xt..**.** If eit
2560: 68 65 72 20 72 69 64 20 6f 72 20 73 72 63 69 64  her rid or srcid
2570: 20 63 6f 6e 74 61 69 6e 20 6c 65 73 73 20 74 68   contain less th
2580: 61 6e 20 35 30 20 62 79 74 65 73 2c 20 6f 72 20  an 50 bytes, or 
2590: 69 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74  if the.** result
25a0: 69 6e 67 20 64 65 6c 74 61 20 64 6f 65 73 20 6e  ing delta does n
25b0: 6f 74 20 61 63 68 69 65 76 65 20 61 20 63 6f 6d  ot achieve a com
25c0: 70 72 65 73 73 69 6f 6e 20 6f 66 20 61 74 20 6c  pression of at l
25d0: 65 61 73 74 20 32 35 25 20 6f 6e 0a 2a 2a 20 69  east 25% on.** i
25e0: 74 73 20 6f 77 6e 20 74 68 65 20 72 69 64 20 69  ts own the rid i
25f0: 73 20 6c 65 66 74 20 75 6e 74 6f 75 63 68 65 64  s left untouched
2600: 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 49 4d  ..**.** NOTE: IM
2610: 48 4f 20 74 68 65 20 63 72 65 61 74 69 6f 6e 20  HO the creation 
2620: 6f 66 20 74 68 65 20 64 65 6c 74 61 20 73 68 6f  of the delta sho
2630: 75 6c 64 20 62 65 20 64 65 66 65 72 65 64 20 75  uld be defered u
2640: 6e 74 69 6c 20 61 66 74 65 72 0a 2a 2a 20 74 68  ntil after.** th
2650: 65 20 62 6c 6f 62 20 73 69 7a 65 73 20 68 61 76  e blob sizes hav
2660: 65 20 62 65 65 6e 20 63 68 65 63 6b 65 64 2e 20  e been checked. 
2670: 44 6f 69 6e 67 20 69 74 20 62 65 66 6f 72 65 20  Doing it before 
2680: 74 68 65 20 63 68 65 63 6b 20 61 73 20 69 73 0a  the check as is.
2690: 2a 2a 20 64 6f 6e 65 20 6e 6f 77 20 74 68 65 20  ** done now the 
26a0: 63 6f 64 65 20 77 69 6c 6c 20 67 65 6e 65 72 61  code will genera
26b0: 74 65 20 61 20 64 65 6c 74 61 20 6a 75 73 74 20  te a delta just 
26c0: 74 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  to immediately t
26d0: 68 72 6f 77 0a 2a 2a 20 69 74 20 61 77 61 79 2c  hrow.** it away,
26e0: 20 77 61 73 74 69 6e 67 20 73 70 61 63 65 20 61   wasting space a
26f0: 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a 76 6f 69 64  nd time..*/.void
2700: 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79   content_deltify
2710: 28 69 6e 74 20 72 69 64 2c 20 69 6e 74 20 73 72  (int rid, int sr
2720: 63 69 64 2c 20 69 6e 74 20 66 6f 72 63 65 29 7b  cid, int force){
2730: 0a 20 20 69 6e 74 20 73 3b 0a 20 20 42 6c 6f 62  .  int s;.  Blob
2740: 20 64 61 74 61 2c 20 73 72 63 2c 20 64 65 6c 74   data, src, delt
2750: 61 3b 0a 20 20 53 74 6d 74 20 73 31 2c 20 73 32  a;.  Stmt s1, s2
2760: 3b 0a 20 20 69 66 28 20 73 72 63 69 64 3d 3d 72  ;.  if( srcid==r
2770: 69 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  id ) return;.  i
2780: 66 28 20 21 66 6f 72 63 65 20 26 26 20 66 69 6e  f( !force && fin
2790: 64 53 72 63 69 64 28 72 69 64 29 3e 30 20 29 20  dSrcid(rid)>0 ) 
27a0: 72 65 74 75 72 6e 3b 0a 20 20 73 20 3d 20 73 72  return;.  s = sr
27b0: 63 69 64 3b 0a 20 20 77 68 69 6c 65 28 20 28 73  cid;.  while( (s
27c0: 20 3d 20 66 69 6e 64 53 72 63 69 64 28 73 29 29   = findSrcid(s))
27d0: 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 3d  >0 ){.    if( s=
27e0: 3d 72 69 64 20 29 7b 0a 20 20 20 20 20 20 63 6f  =rid ){.      co
27f0: 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 73 72  ntent_undelta(sr
2800: 63 69 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61  cid);.      brea
2810: 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63  k;.    }.  }.  c
2820: 6f 6e 74 65 6e 74 5f 67 65 74 28 73 72 63 69 64  ontent_get(srcid
2830: 2c 20 26 73 72 63 29 3b 0a 20 20 63 6f 6e 74 65  , &src);.  conte
2840: 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 64 61 74  nt_get(rid, &dat
2850: 61 29 3b 0a 20 20 62 6c 6f 62 5f 64 65 6c 74 61  a);.  blob_delta
2860: 5f 63 72 65 61 74 65 28 26 73 72 63 2c 20 26 64  _create(&src, &d
2870: 61 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20  ata, &delta);.  
2880: 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 73  if( blob_size(&s
2890: 72 63 29 3e 3d 35 30 20 26 26 20 62 6c 6f 62 5f  rc)>=50 && blob_
28a0: 73 69 7a 65 28 26 64 61 74 61 29 3e 3d 35 30 20  size(&data)>=50 
28b0: 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 62 6c  &&.           bl
28c0: 6f 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 20  ob_size(&delta) 
28d0: 3c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 61 74  < blob_size(&dat
28e0: 61 29 2a 30 2e 37 35 20 29 7b 0a 20 20 20 20 62  a)*0.75 ){.    b
28f0: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 26 64 65  lob_compress(&de
2900: 6c 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20  lta, &delta);.  
2910: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31    db_prepare(&s1
2920: 2c 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53  , "UPDATE blob S
2930: 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61  ET content=:data
2940: 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20   WHERE rid=%d", 
2950: 72 69 64 29 3b 0a 20 20 20 20 64 62 5f 70 72 65  rid);.    db_pre
2960: 70 61 72 65 28 26 73 32 2c 20 22 52 45 50 4c 41  pare(&s2, "REPLA
2970: 43 45 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69  CE INTO delta(ri
2980: 64 2c 73 72 63 69 64 29 56 41 4c 55 45 53 28 25  d,srcid)VALUES(%
2990: 64 2c 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63  d,%d)", rid, src
29a0: 69 64 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64  id);.    db_bind
29b0: 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74  _blob(&s1, ":dat
29c0: 61 22 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20  a", &delta);.   
29d0: 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61   db_begin_transa
29e0: 63 74 69 6f 6e 28 29 3b 0a 20 20 20 20 64 62 5f  ction();.    db_
29f0: 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20 20 64  exec(&s1);.    d
2a00: 62 5f 65 78 65 63 28 26 73 32 29 3b 0a 20 20 20  b_exec(&s2);.   
2a10: 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74   db_end_transact
2a20: 69 6f 6e 28 30 29 3b 0a 20 20 20 20 64 62 5f 66  ion(0);.    db_f
2a30: 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 20  inalize(&s1);.  
2a40: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
2a50: 32 29 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62  2);.    verify_b
2a60: 65 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64  efore_commit(rid
2a70: 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65  );.  }.  blob_re
2a80: 73 65 74 28 26 73 72 63 29 3b 0a 20 20 62 6c 6f  set(&src);.  blo
2a90: 62 5f 72 65 73 65 74 28 26 64 61 74 61 29 3b 0a  b_reset(&data);.
2aa0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65    blob_reset(&de
2ab0: 6c 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  lta);.}../*.** C
2ac0: 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f  OMMAND:  test-co
2ad0: 6e 74 65 6e 74 2d 64 65 6c 74 69 66 79 0a 2a 2a  ntent-deltify.**
2ae0: 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20  .** Convert the 
2af0: 63 6f 6e 74 65 6e 74 20 61 74 20 52 49 44 20 69  content at RID i
2b00: 6e 74 6f 20 61 20 64 65 6c 74 61 20 66 72 6f 6d  nto a delta from
2b10: 20 53 52 43 49 44 2e 0a 2a 2f 0a 76 6f 69 64 20   SRCID..*/.void 
2b20: 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 64 65 6c  test_content_del
2b30: 74 69 66 79 5f 63 6d 64 28 76 6f 69 64 29 7b 0a  tify_cmd(void){.
2b40: 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 35 20    if( g.argc!=5 
2b50: 29 20 75 73 61 67 65 28 22 52 49 44 20 53 52 43  ) usage("RID SRC
2b60: 49 44 20 46 4f 52 43 45 22 29 3b 0a 20 20 64 62  ID FORCE");.  db
2b70: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
2b80: 74 72 65 65 28 29 3b 0a 20 20 63 6f 6e 74 65 6e  tree();.  conten
2b90: 74 5f 64 65 6c 74 69 66 79 28 61 74 6f 69 28 67  t_deltify(atoi(g
2ba0: 2e 61 72 67 76 5b 32 5d 29 2c 20 61 74 6f 69 28  .argv[2]), atoi(
2bb0: 67 2e 61 72 67 76 5b 33 5d 29 2c 20 61 74 6f 69  g.argv[3]), atoi
2bc0: 28 67 2e 61 72 67 76 5b 34 5d 29 29 3b 0a 7d 0a  (g.argv[4]));.}.