Hex Artifact Content
Not logged in

Artifact 52f1a41cd5a6b8dd9591624a0eba4fa2090ff32c:

File src/content.c part of check-in [e1c1877c99] - Sync using clusters appears to work. More testing is needed before we go live. by drh on 2007-09-08 16:01:28.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 36 20 44 2e 20 52 69 63 68  (c) 2006 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 50 72 6f 63 65 64 75 72 65  .**.** Procedure
0370: 73 20 73 74 6f 72 65 20 61 6e 64 20 72 65 74 72  s store and retr
0380: 69 65 76 65 20 72 65 63 6f 72 64 73 20 66 72 6f  ieve records fro
0390: 6d 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79  m the repository
03a0: 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f  .*/.#include "co
03b0: 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65  nfig.h".#include
03c0: 20 22 63 6f 6e 74 65 6e 74 2e 68 22 0a 23 69 6e   "content.h".#in
03d0: 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e  clude <assert.h>
03e0: 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
03f0: 68 65 20 73 72 63 69 64 20 61 73 73 6f 63 69 61  he srcid associa
0400: 74 65 64 20 77 69 74 68 20 72 69 64 2e 20 20 4f  ted with rid.  O
0410: 72 20 72 65 74 75 72 6e 20 30 20 69 66 20 72 69  r return 0 if ri
0420: 64 20 69 73 20 0a 2a 2a 20 6f 72 69 67 69 6e 61  d is .** origina
0430: 6c 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 6e 6f  l content and no
0440: 74 20 61 20 64 65 6c 74 61 2e 0a 2a 2f 0a 73 74  t a delta..*/.st
0450: 61 74 69 63 20 69 6e 74 20 66 69 6e 64 53 72 63  atic int findSrc
0460: 69 64 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69  id(int rid){.  i
0470: 6e 74 20 73 72 63 69 64 20 3d 20 64 62 5f 69 6e  nt srcid = db_in
0480: 74 28 30 2c 20 22 53 45 4c 45 43 54 20 73 72 63  t(0, "SELECT src
0490: 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48  id FROM delta WH
04a0: 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64  ERE rid=%d", rid
04b0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 72 63 69  );.  return srci
04c0: 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  d;.}../*.** Extr
04d0: 61 63 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  act the content 
04e0: 66 6f 72 20 49 44 20 72 69 64 20 61 6e 64 20 70  for ID rid and p
04f0: 75 74 20 69 74 20 69 6e 74 6f 20 74 68 65 0a 2a  ut it into the.*
0500: 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20  * uninitialized 
0510: 62 6c 6f 62 2e 20 20 52 65 74 75 72 6e 20 31 20  blob.  Return 1 
0520: 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20  on success.  If 
0530: 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20 69 73  the record.** is
0540: 20 61 20 70 68 61 6e 74 6f 6d 2c 20 7a 65 72 6f   a phantom, zero
0550: 20 70 42 6c 6f 62 20 61 6e 64 20 72 65 74 75 72   pBlob and retur
0560: 6e 20 30 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74  n 0..*/.int cont
0570: 65 6e 74 5f 67 65 74 28 69 6e 74 20 72 69 64 2c  ent_get(int rid,
0580: 20 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20   Blob *pBlob){. 
0590: 20 53 74 6d 74 20 71 3b 0a 20 20 42 6c 6f 62 20   Stmt q;.  Blob 
05a0: 73 72 63 3b 0a 20 20 69 6e 74 20 73 72 63 69 64  src;.  int srcid
05b0: 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a  ;.  int rc = 0;.
05c0: 0a 20 20 61 73 73 65 72 74 28 20 67 2e 72 65 70  .  assert( g.rep
05d0: 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29 3b 0a 20  ositoryOpen );. 
05e0: 20 73 72 63 69 64 20 3d 20 66 69 6e 64 53 72 63   srcid = findSrc
05f0: 69 64 28 72 69 64 29 3b 0a 20 20 62 6c 6f 62 5f  id(rid);.  blob_
0600: 7a 65 72 6f 28 70 42 6c 6f 62 29 3b 0a 20 20 69  zero(pBlob);.  i
0610: 66 28 20 73 72 63 69 64 20 29 7b 0a 20 20 20 20  f( srcid ){.    
0620: 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28  if( content_get(
0630: 73 72 63 69 64 2c 20 26 73 72 63 29 20 29 7b 0a  srcid, &src) ){.
0640: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65        db_prepare
0650: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e  (&q, "SELECT con
0660: 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57  tent FROM blob W
0670: 48 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20  HERE rid=%d AND 
0680: 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a  size>=0", rid);.
0690: 20 20 20 20 20 20 69 66 28 20 64 62 5f 73 74 65        if( db_ste
06a0: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
06b0: 57 20 29 7b 0a 20 20 20 20 20 20 20 20 42 6c 6f  W ){.        Blo
06c0: 62 20 64 65 6c 74 61 3b 0a 20 20 20 20 20 20 20  b delta;.       
06d0: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c   db_ephemeral_bl
06e0: 6f 62 28 26 71 2c 20 30 2c 20 26 64 65 6c 74 61  ob(&q, 0, &delta
06f0: 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  );.        blob_
0700: 75 6e 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74  uncompress(&delt
0710: 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  a, &delta);.    
0720: 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 70 42      blob_init(pB
0730: 6c 6f 62 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  lob,0,0);.      
0740: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70    blob_delta_app
0750: 6c 79 28 26 73 72 63 2c 20 26 64 65 6c 74 61 2c  ly(&src, &delta,
0760: 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 20   pBlob);.       
0770: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c   blob_reset(&del
0780: 74 61 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ta);.        rc 
0790: 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
07a0: 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26     db_finalize(&
07b0: 71 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72  q);.      blob_r
07c0: 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 20 20  eset(&src);.    
07d0: 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  }.  }else{.    d
07e0: 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53  b_prepare(&q, "S
07f0: 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74 20 46 52  ELECT content FR
0800: 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69  OM blob WHERE ri
0810: 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30  d=%d AND size>=0
0820: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69 66 28  ", rid);.    if(
0830: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51   db_step(&q)==SQ
0840: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
0850: 20 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62    db_ephemeral_b
0860: 6c 6f 62 28 26 71 2c 20 30 2c 20 70 42 6c 6f 62  lob(&q, 0, pBlob
0870: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 75 6e  );.      blob_un
0880: 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f 62 2c 20  compress(pBlob, 
0890: 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 72 63  pBlob);.      rc
08a0: 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
08b0: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b  db_finalize(&q);
08c0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
08d0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41  ;.}../*.** COMMA
08e0: 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e  ND:  test-conten
08f0: 74 2d 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72  t-get.**.** Extr
0900: 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20  act a blob from 
0910: 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64  the database and
0920: 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61   write it into a
0930: 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74   file..*/.void t
0940: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 67 65 74 5f  est_content_get_
0950: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  cmd(void){.  int
0960: 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e   rid;.  Blob con
0970: 74 65 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  tent;.  const ch
0980: 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28  ar *zFile;.  if(
0990: 20 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e   g.argc!=4 && g.
09a0: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28  argc!=3 ) usage(
09b0: 22 52 45 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e  "RECORDID ?FILEN
09c0: 41 4d 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20  AME?");.  zFile 
09d0: 3d 20 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e  = g.argc==4 ? g.
09e0: 61 72 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20  argv[3] : "-";. 
09f0: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68   db_must_be_with
0a00: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64  in_tree();.  rid
0a10: 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67   = name_to_rid(g
0a20: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e  .argv[2]);.  con
0a30: 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 63  tent_get(rid, &c
0a40: 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f  ontent);.  blob_
0a50: 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63  write_to_file(&c
0a60: 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a  ontent, zFile);.
0a70: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44  }../*.** COMMAND
0a80: 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d  :  test-content-
0a90: 72 61 77 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74  rawget.**.** Ext
0aa0: 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d  ract a blob from
0ab0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
0ac0: 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20  d write it into 
0ad0: 61 20 66 69 6c 65 2e 20 20 54 68 69 73 0a 2a 2a  a file.  This.**
0ae0: 20 76 65 72 73 69 6f 6e 20 64 6f 65 73 20 6e 6f   version does no
0af0: 74 20 65 78 70 61 6e 64 20 74 68 65 20 64 65 6c  t expand the del
0b00: 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74  ta..*/.void test
0b10: 5f 63 6f 6e 74 65 6e 74 5f 72 61 77 67 65 74 5f  _content_rawget_
0b20: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  cmd(void){.  int
0b30: 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e   rid;.  Blob con
0b40: 74 65 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  tent;.  const ch
0b50: 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28  ar *zFile;.  if(
0b60: 20 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e   g.argc!=4 && g.
0b70: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28  argc!=3 ) usage(
0b80: 22 52 45 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e  "RECORDID ?FILEN
0b90: 41 4d 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20  AME?");.  zFile 
0ba0: 3d 20 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e  = g.argc==4 ? g.
0bb0: 61 72 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20  argv[3] : "-";. 
0bc0: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68   db_must_be_with
0bd0: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64  in_tree();.  rid
0be0: 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67   = name_to_rid(g
0bf0: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 62 6c 6f  .argv[2]);.  blo
0c00: 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29  b_zero(&content)
0c10: 3b 0a 20 20 64 62 5f 62 6c 6f 62 28 26 63 6f 6e  ;.  db_blob(&con
0c20: 74 65 6e 74 2c 20 22 53 45 4c 45 43 54 20 63 6f  tent, "SELECT co
0c30: 6e 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20  ntent FROM blob 
0c40: 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72  WHERE rid=%d", r
0c50: 69 64 29 3b 0a 20 20 62 6c 6f 62 5f 75 6e 63 6f  id);.  blob_unco
0c60: 6d 70 72 65 73 73 28 26 63 6f 6e 74 65 6e 74 2c  mpress(&content,
0c70: 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c   &content);.  bl
0c80: 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65  ob_write_to_file
0c90: 28 26 63 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65  (&content, zFile
0ca0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  );.}../*.** When
0cb0: 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 6f 6e   a record is con
0cc0: 76 65 72 74 65 64 20 66 72 6f 6d 20 61 20 70 68  verted from a ph
0cd0: 61 6e 74 6f 6d 20 74 6f 20 61 20 72 65 61 6c 20  antom to a real 
0ce0: 72 65 63 6f 72 64 2c 0a 2a 2a 20 69 66 20 74 68  record,.** if th
0cf0: 61 74 20 72 65 63 6f 72 64 20 68 61 73 20 6f 74  at record has ot
0d00: 68 65 72 20 72 65 63 6f 72 64 73 20 74 68 61 74  her records that
0d10: 20 61 72 65 20 64 65 72 69 76 65 64 20 62 79 20   are derived by 
0d20: 64 65 6c 74 61 2c 0a 2a 2a 20 74 68 65 6e 20 63  delta,.** then c
0d30: 61 6c 6c 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f  all manifest_cro
0d40: 73 73 6c 69 6e 6b 28 29 20 6f 6e 20 74 68 6f 73  sslink() on thos
0d50: 65 20 6f 74 68 65 72 20 72 65 63 6f 72 64 73 2e  e other records.
0d60: 0a 2a 2f 0a 76 6f 69 64 20 61 66 74 65 72 5f 64  .*/.void after_d
0d70: 65 70 68 61 6e 74 6f 6d 69 7a 65 28 69 6e 74 20  ephantomize(int 
0d80: 72 69 64 2c 20 69 6e 74 20 6c 69 6e 6b 46 6c 61  rid, int linkFla
0d90: 67 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20  g){.  Stmt q;.  
0da0: 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22  db_prepare(&q, "
0db0: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20  SELECT rid FROM 
0dc0: 64 65 6c 74 61 20 57 48 45 52 45 20 73 72 63 69  delta WHERE srci
0dd0: 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 77  d=%d", rid);.  w
0de0: 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71  hile( db_step(&q
0df0: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
0e00: 0a 20 20 20 20 69 6e 74 20 74 69 64 20 3d 20 64  .    int tid = d
0e10: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c  b_column_int(&q,
0e20: 20 30 29 3b 0a 20 20 20 20 61 66 74 65 72 5f 64   0);.    after_d
0e30: 65 70 68 61 6e 74 6f 6d 69 7a 65 28 74 69 64 2c  ephantomize(tid,
0e40: 20 31 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69   1);.  }.  db_fi
0e50: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 69 66  nalize(&q);.  if
0e60: 28 20 6c 69 6e 6b 46 6c 61 67 20 29 7b 0a 20 20  ( linkFlag ){.  
0e70: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a    Blob content;.
0e80: 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28      content_get(
0e90: 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a  rid, &content);.
0ea0: 20 20 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f      manifest_cro
0eb0: 73 73 6c 69 6e 6b 28 72 69 64 2c 20 26 63 6f 6e  sslink(rid, &con
0ec0: 74 65 6e 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  tent);.    blob_
0ed0: 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b  reset(&content);
0ee0: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  .  }.}../*.** Wr
0ef0: 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f  ite content into
0f00: 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
0f10: 52 65 74 75 72 6e 20 74 68 65 20 72 65 63 6f 72  Return the recor
0f20: 64 20 49 44 2e 20 20 49 66 20 74 68 65 0a 2a 2a  d ID.  If the.**
0f30: 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 6c 72 65   content is alre
0f40: 61 64 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  ady in the datab
0f50: 61 73 65 2c 20 6a 75 73 74 20 72 65 74 75 72 6e  ase, just return
0f60: 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e 0a   the record ID..
0f70: 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 49 64 20 69  **.** If srcId i
0f80: 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65  s specified, the
0f90: 6e 20 70 42 6c 6f 62 20 69 73 20 64 65 6c 74 61  n pBlob is delta
0fa0: 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 0a 2a 2a   content from.**
0fb0: 20 74 68 65 20 73 72 63 49 64 20 72 65 63 6f 72   the srcId recor
0fc0: 64 2e 20 20 73 72 63 49 64 20 6d 69 67 68 74 20  d.  srcId might 
0fd0: 62 65 20 61 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2a  be a phantom..**
0fe0: 0a 2a 2a 20 41 20 70 68 61 6e 74 6f 6d 20 69 73  .** A phantom is
0ff0: 20 77 72 69 74 74 65 6e 20 69 66 20 70 42 6c 6f   written if pBlo
1000: 62 3d 3d 30 2e 20 20 49 66 20 70 42 6c 6f 62 3d  b==0.  If pBlob=
1010: 3d 30 20 6f 72 20 69 66 20 73 72 63 49 64 20 69  =0 or if srcId i
1020: 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 74  s.** specified t
1030: 68 65 6e 20 74 68 65 20 55 55 49 44 20 69 73 20  hen the UUID is 
1040: 73 65 74 20 74 6f 20 7a 55 75 69 64 2e 20 20 4f  set to zUuid.  O
1050: 74 68 65 72 77 69 73 65 20 7a 55 75 69 64 20 69  therwise zUuid i
1060: 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49  s.** ignored.  I
1070: 6e 20 74 68 65 20 66 75 74 75 72 65 20 74 68 69  n the future thi
1080: 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 73  s might change s
1090: 75 63 68 20 74 68 61 74 20 74 68 65 20 63 6f 6e  uch that the con
10a0: 74 65 6e 74 0a 2a 2a 20 68 61 73 68 20 69 73 20  tent.** hash is 
10b0: 63 68 65 63 6b 65 64 20 61 67 61 69 6e 73 74 20  checked against 
10c0: 7a 55 75 69 64 20 74 6f 20 6d 61 6b 65 20 73 75  zUuid to make su
10d0: 72 65 20 69 74 20 69 73 20 63 6f 72 72 65 63 74  re it is correct
10e0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
10f0: 65 63 6f 72 64 20 61 6c 72 65 61 64 79 20 65 78  ecord already ex
1100: 69 73 74 73 20 62 75 74 20 69 73 20 61 20 70 68  ists but is a ph
1110: 61 6e 74 6f 6d 2c 20 74 68 65 20 70 42 6c 6f 62  antom, the pBlob
1120: 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 69   content.** is i
1130: 6e 73 65 72 74 65 64 20 61 6e 64 20 74 68 65 20  nserted and the 
1140: 70 68 61 74 6f 6d 20 62 65 63 6f 6d 65 73 20 61  phatom becomes a
1150: 20 72 65 61 6c 20 72 65 63 6f 72 64 2e 0a 2a 2f   real record..*/
1160: 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 70 75 74  .int content_put
1170: 28 42 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f  (Blob *pBlob, co
1180: 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 2c  nst char *zUuid,
1190: 20 69 6e 74 20 73 72 63 49 64 29 7b 0a 20 20 69   int srcId){.  i
11a0: 6e 74 20 73 69 7a 65 3b 0a 20 20 69 6e 74 20 72  nt size;.  int r
11b0: 69 64 3b 0a 20 20 53 74 6d 74 20 73 31 3b 0a 20  id;.  Stmt s1;. 
11c0: 20 42 6c 6f 62 20 63 6d 70 72 3b 0a 20 20 42 6c   Blob cmpr;.  Bl
11d0: 6f 62 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72  ob hash;.  asser
11e0: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  t( g.repositoryO
11f0: 70 65 6e 20 29 3b 0a 20 20 69 66 28 20 70 42 6c  pen );.  if( pBl
1200: 6f 62 20 26 26 20 73 72 63 49 64 3d 3d 30 20 29  ob && srcId==0 )
1210: 7b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 6c  {.    sha1sum_bl
1220: 6f 62 28 70 42 6c 6f 62 2c 20 26 68 61 73 68 29  ob(pBlob, &hash)
1230: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62  ;.  }else{.    b
1240: 6c 6f 62 5f 69 6e 69 74 28 26 68 61 73 68 2c 20  lob_init(&hash, 
1250: 7a 55 75 69 64 2c 20 2d 31 29 3b 0a 20 20 7d 0a  zUuid, -1);.  }.
1260: 20 20 69 66 28 20 70 42 6c 6f 62 3d 3d 30 20 29    if( pBlob==0 )
1270: 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 2d 31 3b  {.    size = -1;
1280: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 69  .  }else{.    si
1290: 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28 70  ze = blob_size(p
12a0: 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 64 62 5f  Blob);.  }.  db_
12b0: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f  begin_transactio
12c0: 6e 28 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  n();..  /* Check
12d0: 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 65   to see if the e
12e0: 6e 74 72 79 20 61 6c 72 65 61 64 79 20 65 78 69  ntry already exi
12f0: 73 74 73 20 61 6e 64 20 69 66 20 69 74 20 64 6f  sts and if it do
1300: 65 73 20 77 68 65 74 68 65 72 0a 20 20 2a 2a 20  es whether.  ** 
1310: 6f 72 20 6e 6f 74 20 74 68 65 20 65 6e 74 72 79  or not the entry
1320: 20 69 73 20 61 20 70 68 61 6e 74 6f 6d 0a 20 20   is a phantom.  
1330: 2a 2f 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28  */.  db_prepare(
1340: 26 73 31 2c 20 22 53 45 4c 45 43 54 20 72 69 64  &s1, "SELECT rid
1350: 2c 20 73 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62  , size FROM blob
1360: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c   WHERE uuid=%B",
1370: 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 64   &hash);.  if( d
1380: 62 5f 73 74 65 70 28 26 73 31 29 3d 3d 53 51 4c  b_step(&s1)==SQL
1390: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72  ITE_ROW ){.    r
13a0: 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69  id = db_column_i
13b0: 6e 74 28 26 73 31 2c 20 30 29 3b 0a 20 20 20 20  nt(&s1, 0);.    
13c0: 69 66 28 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  if( db_column_in
13d0: 74 28 26 73 31 2c 20 31 29 3e 3d 30 20 7c 7c 20  t(&s1, 1)>=0 || 
13e0: 70 42 6c 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20  pBlob==0 ){.    
13f0: 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20    /* Either the 
1400: 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 61 20 70  entry is not a p
1410: 68 61 6e 74 6f 6d 20 6f 72 20 69 74 20 69 73 20  hantom or it is 
1420: 61 20 70 68 61 6e 74 6f 6d 20 62 75 74 20 77 65  a phantom but we
1430: 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 6e  .      ** have n
1440: 6f 20 64 61 74 61 20 77 69 74 68 20 77 68 69 63  o data with whic
1450: 68 20 74 6f 20 64 65 70 68 61 6e 74 6f 6d 69 7a  h to dephantomiz
1460: 65 20 69 74 2e 20 20 49 6e 20 65 69 74 68 65 72  e it.  In either
1470: 20 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20   case,.      ** 
1480: 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67  there is nothing
1490: 20 66 6f 72 20 75 73 20 74 6f 20 64 6f 20 6f 74   for us to do ot
14a0: 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20  her than return 
14b0: 74 68 65 20 52 49 44 2e 20 2a 2f 0a 20 20 20 20  the RID. */.    
14c0: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
14d0: 31 29 3b 0a 20 20 20 20 20 20 64 62 5f 65 6e 64  1);.      db_end
14e0: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
14f0: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 69  .      return ri
1500: 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  d;.    }.  }else
1510: 7b 0a 20 20 20 20 72 69 64 20 3d 20 30 3b 20 20  {.    rid = 0;  
1520: 2f 2a 20 4e 6f 20 65 6e 74 72 79 20 77 69 74 68  /* No entry with
1530: 20 74 68 65 20 73 61 6d 65 20 55 55 49 44 20 63   the same UUID c
1540: 75 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20  urrently exists 
1550: 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  */.  }.  db_fina
1560: 6c 69 7a 65 28 26 73 31 29 3b 0a 0a 20 20 2f 2a  lize(&s1);..  /*
1570: 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 63   Construct a rec
1580: 65 69 76 65 64 2d 66 72 6f 6d 20 49 44 20 69 66  eived-from ID if
1590: 20 77 65 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61   we do not alrea
15a0: 64 79 20 68 61 76 65 20 6f 6e 65 20 2a 2f 0a 20  dy have one */. 
15b0: 20 69 66 28 20 67 2e 72 63 76 69 64 3d 3d 30 20   if( g.rcvid==0 
15c0: 26 26 20 70 42 6c 6f 62 21 3d 30 20 29 7b 0a 20  && pBlob!=0 ){. 
15d0: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
15e0: 28 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 54  (.       "INSERT
15f0: 20 49 4e 54 4f 20 72 63 76 66 72 6f 6d 28 75 69   INTO rcvfrom(ui
1600: 64 2c 20 6d 74 69 6d 65 2c 20 6e 6f 6e 63 65 2c  d, mtime, nonce,
1610: 20 69 70 61 64 64 72 29 22 0a 20 20 20 20 20 20   ipaddr)".      
1620: 20 22 56 41 4c 55 45 53 28 25 64 2c 20 6a 75 6c   "VALUES(%d, jul
1630: 69 61 6e 64 61 79 28 27 6e 6f 77 27 29 2c 20 25  ianday('now'), %
1640: 51 2c 20 25 51 29 22 2c 0a 20 20 20 20 20 20 20  Q, %Q)",.       
1650: 67 2e 75 73 65 72 55 69 64 2c 20 67 2e 7a 4e 6f  g.userUid, g.zNo
1660: 6e 63 65 2c 20 67 2e 7a 49 70 41 64 64 72 0a 20  nce, g.zIpAddr. 
1670: 20 20 20 29 3b 0a 20 20 20 20 67 2e 72 63 76 69     );.    g.rcvi
1680: 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69 6e 73 65  d = db_last_inse
1690: 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20 20 7d 0a  rt_rowid();.  }.
16a0: 0a 20 20 69 66 28 20 72 69 64 3e 30 20 29 7b 0a  .  if( rid>0 ){.
16b0: 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 6a 75      /* We are ju
16c0: 73 74 20 61 64 64 69 6e 67 20 64 61 74 61 20 74  st adding data t
16d0: 6f 20 61 20 70 68 61 6e 74 6f 6d 20 2a 2f 0a 20  o a phantom */. 
16e0: 20 20 20 61 73 73 65 72 74 28 20 70 42 6c 6f 62     assert( pBlob
16f0: 21 3d 30 20 29 3b 0a 20 20 20 20 64 62 5f 70 72  !=0 );.    db_pr
1700: 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20 20 20  epare(&s1,.     
1710: 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45   "UPDATE blob SE
1720: 54 20 72 63 76 69 64 3d 25 64 2c 20 73 69 7a 65  T rcvid=%d, size
1730: 3d 25 64 2c 20 63 6f 6e 74 65 6e 74 3d 3a 64 61  =%d, content=:da
1740: 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64 22  ta WHERE rid=%d"
1750: 2c 0a 20 20 20 20 20 20 20 67 2e 72 63 76 69 64  ,.       g.rcvid
1760: 2c 20 73 69 7a 65 2c 20 72 69 64 0a 20 20 20 20  , size, rid.    
1770: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70  );.    blob_comp
1780: 72 65 73 73 28 70 42 6c 6f 62 2c 20 26 63 6d 70  ress(pBlob, &cmp
1790: 72 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f  r);.    db_bind_
17a0: 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61  blob(&s1, ":data
17b0: 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 64  ", &cmpr);.    d
17c0: 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20  b_exec(&s1);.   
17d0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
17e0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 68 61 6e  DELETE FROM phan
17f0: 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d 25 64  tom WHERE rid=%d
1800: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69 66 28  ", rid);.    if(
1810: 20 73 72 63 49 64 3d 3d 30 20 7c 7c 20 64 62 5f   srcId==0 || db_
1820: 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 73  int(0, "SELECT s
1830: 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  ize FROM blob WH
1840: 45 52 45 20 72 69 64 3d 25 64 22 2c 20 73 72 63  ERE rid=%d", src
1850: 49 64 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 61  Id)>0 ){.      a
1860: 66 74 65 72 5f 64 65 70 68 61 6e 74 6f 6d 69 7a  fter_dephantomiz
1870: 65 28 72 69 64 2c 20 30 29 3b 0a 20 20 20 20 7d  e(rid, 0);.    }
1880: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1890: 20 57 65 20 61 72 65 20 63 72 65 61 74 69 6e 67   We are creating
18a0: 20 61 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a   a new entry */.
18b0: 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26      db_prepare(&
18c0: 73 31 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 52  s1,.      "INSER
18d0: 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72 63 76 69  T INTO blob(rcvi
18e0: 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63 6f 6e 74  d,size,uuid,cont
18f0: 65 6e 74 29 22 0a 20 20 20 20 20 20 22 56 41 4c  ent)".      "VAL
1900: 55 45 53 28 25 64 2c 25 64 2c 27 25 62 27 2c 3a  UES(%d,%d,'%b',:
1910: 64 61 74 61 29 22 2c 0a 20 20 20 20 20 20 20 67  data)",.       g
1920: 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c 20 26 68  .rcvid, size, &h
1930: 61 73 68 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  ash.    );.    i
1940: 66 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20  f( pBlob ){.    
1950: 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28    blob_compress(
1960: 70 42 6c 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20  pBlob, &cmpr);. 
1970: 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f       db_bind_blo
1980: 62 28 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20  b(&s1, ":data", 
1990: 26 63 6d 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20  &cmpr);.    }.  
19a0: 20 20 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a    db_exec(&s1);.
19b0: 20 20 20 20 72 69 64 20 3d 20 64 62 5f 6c 61 73      rid = db_las
19c0: 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
19d0: 3b 0a 20 20 20 20 69 66 28 20 21 70 42 6c 6f 62  ;.    if( !pBlob
19e0: 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c   ){.      db_mul
19f0: 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20  ti_exec("INSERT 
1a00: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70  OR IGNORE INTO p
1a10: 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53 28 25 64  hantom VALUES(%d
1a20: 29 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a  )", rid);.    }.
1a30: 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1a40: 20 73 72 63 49 64 20 69 73 20 73 70 65 63 69 66   srcId is specif
1a50: 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20 64 61  ied, then the da
1a60: 74 61 20 77 65 20 6a 75 73 74 20 61 64 64 65 64  ta we just added
1a70: 20 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c 79 20   is.  ** really 
1a80: 61 20 64 65 6c 74 61 2e 20 20 52 65 63 6f 72 64  a delta.  Record
1a90: 20 74 68 69 73 20 66 61 63 74 20 69 6e 20 74 68   this fact in th
1aa0: 65 20 64 65 6c 74 61 20 74 61 62 6c 65 2e 0a 20  e delta table.. 
1ab0: 20 2a 2f 0a 20 20 69 66 28 20 73 72 63 49 64 20   */.  if( srcId 
1ac0: 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  ){.    db_multi_
1ad0: 65 78 65 63 28 22 52 45 50 4c 41 43 45 20 49 4e  exec("REPLACE IN
1ae0: 54 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72 63  TO delta(rid,src
1af0: 69 64 29 20 56 41 4c 55 45 53 28 25 64 2c 25 64  id) VALUES(%d,%d
1b00: 29 22 2c 20 72 69 64 2c 20 73 72 63 49 64 29 3b  )", rid, srcId);
1b10: 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 41 64 64  .  }.  .  /* Add
1b20: 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20   the element to 
1b30: 74 68 65 20 75 6e 63 6c 75 73 74 65 72 65 64 20  the unclustered 
1b40: 74 61 62 6c 65 20 69 66 20 69 74 20 69 73 20 6e  table if it is n
1b50: 6f 74 20 61 0a 20 20 2a 2a 20 61 20 70 68 61 6e  ot a.  ** a phan
1b60: 74 6f 6d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  tom.  */.  if( p
1b70: 42 6c 6f 62 20 29 7b 0a 20 20 20 20 64 62 5f 6d  Blob ){.    db_m
1b80: 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52  ulti_exec("INSER
1b90: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f  T OR IGNORE INTO
1ba0: 20 75 6e 63 6c 75 73 74 65 72 65 64 20 56 41 4c   unclustered VAL
1bb0: 55 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a  UES(%d)", rid);.
1bc0: 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 73 68    }..  /* Finish
1bd0: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1be0: 20 61 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f 0a   and cleanup */.
1bf0: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
1c00: 31 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61  1);.  db_end_tra
1c10: 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 62  nsaction(0);.  b
1c20: 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29  lob_reset(&hash)
1c30: 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72 72  ;..  /* Make arr
1c40: 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 76 65 72  angements to ver
1c50: 69 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74  ify that the dat
1c60: 61 20 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72  a can be recover
1c70: 65 64 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 77  ed.  ** before w
1c80: 65 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 69 66  e commit */.  if
1c90: 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 62  ( pBlob ){.    b
1ca0: 6c 6f 62 5f 72 65 73 65 74 28 26 63 6d 70 72 29  lob_reset(&cmpr)
1cb0: 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 65 66  ;.    verify_bef
1cc0: 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b  ore_commit(rid);
1cd0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 69  .  }.  return ri
1ce0: 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d  d;.}../*.** COMM
1cf0: 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65  AND:  test-conte
1d00: 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a 20 45 78 74  nt-put.**.** Ext
1d10: 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d  ract a blob from
1d20: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
1d30: 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20  d write it into 
1d40: 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20  a file..*/.void 
1d50: 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 70 75 74  test_content_put
1d60: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e  _cmd(void){.  in
1d70: 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f  t rid;.  Blob co
1d80: 6e 74 65 6e 74 3b 0a 20 20 69 66 28 20 67 2e 61  ntent;.  if( g.a
1d90: 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 22  rgc!=3 ) usage("
1da0: 46 49 4c 45 4e 41 4d 45 22 29 3b 0a 20 20 64 62  FILENAME");.  db
1db0: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
1dc0: 74 72 65 65 28 29 3b 0a 20 20 75 73 65 72 5f 73  tree();.  user_s
1dd0: 65 6c 65 63 74 28 29 3b 0a 20 20 62 6c 6f 62 5f  elect();.  blob_
1de0: 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26  read_from_file(&
1df0: 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 72 67 76 5b  content, g.argv[
1e00: 32 5d 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f 6e  2]);.  rid = con
1e10: 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e  tent_put(&conten
1e20: 74 2c 20 30 2c 20 30 29 3b 0a 20 20 70 72 69 6e  t, 0, 0);.  prin
1e30: 74 66 28 22 69 6e 73 65 72 74 65 64 20 61 73 20  tf("inserted as 
1e40: 72 65 63 6f 72 64 20 25 64 5c 6e 22 2c 20 72 69  record %d\n", ri
1e50: 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  d);.}../*.** Mak
1e60: 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e 74 65  e sure the conte
1e70: 6e 74 20 61 74 20 72 69 64 20 69 73 20 74 68 65  nt at rid is the
1e80: 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e   original conten
1e90: 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 0a 2a  t and is not a.*
1ea0: 2a 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 64  * delta..*/.void
1eb0: 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61   content_undelta
1ec0: 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 66 28  (int rid){.  if(
1ed0: 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 29 3e   findSrcid(rid)>
1ee0: 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 78 3b  0 ){.    Blob x;
1ef0: 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74  .    if( content
1f00: 5f 67 65 74 28 72 69 64 2c 20 26 78 29 20 29 7b  _get(rid, &x) ){
1f10: 0a 20 20 20 20 20 20 53 74 6d 74 20 73 3b 0a 20  .      Stmt s;. 
1f20: 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28       db_prepare(
1f30: 26 73 2c 20 22 55 50 44 41 54 45 20 62 6c 6f 62  &s, "UPDATE blob
1f40: 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 63 2c   SET content=:c,
1f50: 20 73 69 7a 65 3d 25 64 20 57 48 45 52 45 20 72   size=%d WHERE r
1f60: 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 20 20  id=%d",.        
1f70: 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f               blo
1f80: 62 5f 73 69 7a 65 28 26 78 29 2c 20 72 69 64 29  b_size(&x), rid)
1f90: 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 6d  ;.      blob_com
1fa0: 70 72 65 73 73 28 26 78 2c 20 26 78 29 3b 0a 20  press(&x, &x);. 
1fb0: 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f       db_bind_blo
1fc0: 62 28 26 73 2c 20 22 3a 63 22 2c 20 26 78 29 3b  b(&s, ":c", &x);
1fd0: 0a 20 20 20 20 20 20 64 62 5f 65 78 65 63 28 26  .      db_exec(&
1fe0: 73 29 3b 0a 20 20 20 20 20 20 64 62 5f 66 69 6e  s);.      db_fin
1ff0: 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 20 20 20  alize(&s);.     
2000: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 78 29 3b   blob_reset(&x);
2010: 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  .      db_multi_
2020: 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f  exec("DELETE FRO
2030: 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72 69  M delta WHERE ri
2040: 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 20  d=%d", rid);.   
2050: 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2060: 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63  COMMAND:  test-c
2070: 6f 6e 74 65 6e 74 2d 75 6e 64 65 6c 74 61 0a 2a  ontent-undelta.*
2080: 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  *.** Make sure t
2090: 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 52 45  he content at RE
20a0: 43 4f 52 44 49 44 20 69 73 20 6e 6f 74 20 61 20  CORDID is not a 
20b0: 64 65 6c 74 61 0a 2a 2f 0a 76 6f 69 64 20 74 65  delta.*/.void te
20c0: 73 74 5f 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c  st_content_undel
20d0: 74 61 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20  ta_cmd(void){.  
20e0: 69 6e 74 20 72 69 64 3b 0a 20 20 69 66 28 20 67  int rid;.  if( g
20f0: 2e 61 72 67 63 21 3d 32 20 29 20 75 73 61 67 65  .argc!=2 ) usage
2100: 28 22 52 45 43 4f 52 44 49 44 22 29 3b 0a 20 20  ("RECORDID");.  
2110: 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69  db_must_be_withi
2120: 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64 20  n_tree();.  rid 
2130: 3d 20 61 74 6f 69 28 67 2e 61 72 67 76 5b 32 5d  = atoi(g.argv[2]
2140: 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 75 6e 64  );.  content_und
2150: 65 6c 74 61 28 72 69 64 29 3b 0a 7d 0a 0a 2f 2a  elta(rid);.}../*
2160: 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73  .** Change the s
2170: 74 6f 72 61 67 65 20 6f 66 20 72 69 64 20 73 6f  torage of rid so
2180: 20 74 68 61 74 20 69 74 20 69 73 20 61 20 64 65   that it is a de
2190: 6c 74 61 20 6f 66 20 73 72 63 69 64 2e 0a 2a 2a  lta of srcid..**
21a0: 0a 2a 2a 20 49 66 20 72 69 64 20 69 73 20 61 6c  .** If rid is al
21b0: 72 65 61 64 79 20 61 20 64 65 6c 74 61 20 66 72  ready a delta fr
21c0: 6f 6d 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 6c  om some other pl
21d0: 61 63 65 20 74 68 65 6e 20 6e 6f 0a 2a 2a 20 63  ace then no.** c
21e0: 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73  onversion occurs
21f0: 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 6e   and this is a n
2200: 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 66 6f 72 63  o-op unless forc
2210: 65 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  e==1..**.** If s
2220: 72 63 69 64 20 69 73 20 61 20 64 65 6c 74 61 20  rcid is a delta 
2230: 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f 6e 20  that depends on 
2240: 72 69 64 2c 20 74 68 65 6e 20 73 72 63 69 64 20  rid, then srcid 
2250: 69 73 0a 2a 2a 20 63 6f 6e 76 65 72 74 65 64 20  is.** converted 
2260: 74 6f 20 75 6e 64 65 6c 74 61 65 64 20 74 65 78  to undeltaed tex
2270: 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68  t..**.** If eith
2280: 65 72 20 72 69 64 20 6f 72 20 73 72 63 69 64 20  er rid or srcid 
2290: 63 6f 6e 74 61 69 6e 20 6c 65 73 73 20 74 68 61  contain less tha
22a0: 6e 20 35 30 20 62 79 74 65 73 2c 20 6f 72 20 69  n 50 bytes, or i
22b0: 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 69  f the.** resulti
22c0: 6e 67 20 64 65 6c 74 61 20 64 6f 65 73 20 6e 6f  ng delta does no
22d0: 74 20 61 63 68 69 65 76 65 20 61 20 63 6f 6d 70  t achieve a comp
22e0: 72 65 73 73 69 6f 6e 20 6f 66 20 61 74 20 6c 65  ression of at le
22f0: 61 73 74 20 32 35 25 20 6f 6e 0a 2a 2a 20 69 74  ast 25% on.** it
2300: 73 20 6f 77 6e 20 74 68 65 20 72 69 64 20 69 73  s own the rid is
2310: 20 6c 65 66 74 20 75 6e 74 6f 75 63 68 65 64 2e   left untouched.
2320: 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 49 4d 48  .**.** NOTE: IMH
2330: 4f 20 74 68 65 20 63 72 65 61 74 69 6f 6e 20 6f  O the creation o
2340: 66 20 74 68 65 20 64 65 6c 74 61 20 73 68 6f 75  f the delta shou
2350: 6c 64 20 62 65 20 64 65 66 65 72 65 64 20 75 6e  ld be defered un
2360: 74 69 6c 20 61 66 74 65 72 0a 2a 2a 20 74 68 65  til after.** the
2370: 20 62 6c 6f 62 20 73 69 7a 65 73 20 68 61 76 65   blob sizes have
2380: 20 62 65 65 6e 20 63 68 65 63 6b 65 64 2e 20 44   been checked. D
2390: 6f 69 6e 67 20 69 74 20 62 65 66 6f 72 65 20 74  oing it before t
23a0: 68 65 20 63 68 65 63 6b 20 61 73 20 69 73 0a 2a  he check as is.*
23b0: 2a 20 64 6f 6e 65 20 6e 6f 77 20 74 68 65 20 63  * done now the c
23c0: 6f 64 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74  ode will generat
23d0: 65 20 61 20 64 65 6c 74 61 20 6a 75 73 74 20 74  e a delta just t
23e0: 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 68  o immediately th
23f0: 72 6f 77 0a 2a 2a 20 69 74 20 61 77 61 79 2c 20  row.** it away, 
2400: 77 61 73 74 69 6e 67 20 73 70 61 63 65 20 61 6e  wasting space an
2410: 64 20 74 69 6d 65 2e 0a 2a 2f 0a 76 6f 69 64 20  d time..*/.void 
2420: 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 28  content_deltify(
2430: 69 6e 74 20 72 69 64 2c 20 69 6e 74 20 73 72 63  int rid, int src
2440: 69 64 2c 20 69 6e 74 20 66 6f 72 63 65 29 7b 0a  id, int force){.
2450: 20 20 69 6e 74 20 73 3b 0a 20 20 42 6c 6f 62 20    int s;.  Blob 
2460: 64 61 74 61 2c 20 73 72 63 2c 20 64 65 6c 74 61  data, src, delta
2470: 3b 0a 20 20 53 74 6d 74 20 73 31 2c 20 73 32 3b  ;.  Stmt s1, s2;
2480: 0a 20 20 69 66 28 20 73 72 63 69 64 3d 3d 72 69  .  if( srcid==ri
2490: 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  d ) return;.  if
24a0: 28 20 21 66 6f 72 63 65 20 26 26 20 66 69 6e 64  ( !force && find
24b0: 53 72 63 69 64 28 72 69 64 29 3e 30 20 29 20 72  Srcid(rid)>0 ) r
24c0: 65 74 75 72 6e 3b 0a 20 20 73 20 3d 20 73 72 63  eturn;.  s = src
24d0: 69 64 3b 0a 20 20 77 68 69 6c 65 28 20 28 73 20  id;.  while( (s 
24e0: 3d 20 66 69 6e 64 53 72 63 69 64 28 73 29 29 3e  = findSrcid(s))>
24f0: 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 3d 3d  0 ){.    if( s==
2500: 72 69 64 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  rid ){.      con
2510: 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 73 72 63  tent_undelta(src
2520: 69 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  id);.      break
2530: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f  ;.    }.  }.  co
2540: 6e 74 65 6e 74 5f 67 65 74 28 73 72 63 69 64 2c  ntent_get(srcid,
2550: 20 26 73 72 63 29 3b 0a 20 20 63 6f 6e 74 65 6e   &src);.  conten
2560: 74 5f 67 65 74 28 72 69 64 2c 20 26 64 61 74 61  t_get(rid, &data
2570: 29 3b 0a 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f  );.  blob_delta_
2580: 63 72 65 61 74 65 28 26 73 72 63 2c 20 26 64 61  create(&src, &da
2590: 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 69  ta, &delta);.  i
25a0: 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 73 72  f( blob_size(&sr
25b0: 63 29 3e 3d 35 30 20 26 26 20 62 6c 6f 62 5f 73  c)>=50 && blob_s
25c0: 69 7a 65 28 26 64 61 74 61 29 3e 3d 35 30 20 26  ize(&data)>=50 &
25d0: 26 0a 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f  &.           blo
25e0: 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 20 3c  b_size(&delta) <
25f0: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 61 74 61   blob_size(&data
2600: 29 2a 30 2e 37 35 20 29 7b 0a 20 20 20 20 62 6c  )*0.75 ){.    bl
2610: 6f 62 5f 63 6f 6d 70 72 65 73 73 28 26 64 65 6c  ob_compress(&del
2620: 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20  ta, &delta);.   
2630: 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c   db_prepare(&s1,
2640: 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45   "UPDATE blob SE
2650: 54 20 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20  T content=:data 
2660: 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72  WHERE rid=%d", r
2670: 69 64 29 3b 0a 20 20 20 20 64 62 5f 70 72 65 70  id);.    db_prep
2680: 61 72 65 28 26 73 32 2c 20 22 52 45 50 4c 41 43  are(&s2, "REPLAC
2690: 45 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69 64  E INTO delta(rid
26a0: 2c 73 72 63 69 64 29 56 41 4c 55 45 53 28 25 64  ,srcid)VALUES(%d
26b0: 2c 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63 69  ,%d)", rid, srci
26c0: 64 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f  d);.    db_bind_
26d0: 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61  blob(&s1, ":data
26e0: 22 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  ", &delta);.    
26f0: 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63  db_begin_transac
2700: 74 69 6f 6e 28 29 3b 0a 20 20 20 20 64 62 5f 65  tion();.    db_e
2710: 78 65 63 28 26 73 31 29 3b 0a 20 20 20 20 64 62  xec(&s1);.    db
2720: 5f 65 78 65 63 28 26 73 32 29 3b 0a 20 20 20 20  _exec(&s2);.    
2730: 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  db_end_transacti
2740: 6f 6e 28 30 29 3b 0a 20 20 20 20 64 62 5f 66 69  on(0);.    db_fi
2750: 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 20 20  nalize(&s1);.   
2760: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 32   db_finalize(&s2
2770: 29 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 65  );.    verify_be
2780: 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29  fore_commit(rid)
2790: 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73  ;.  }.  blob_res
27a0: 65 74 28 26 73 72 63 29 3b 0a 20 20 62 6c 6f 62  et(&src);.  blob
27b0: 5f 72 65 73 65 74 28 26 64 61 74 61 29 3b 0a 20  _reset(&data);. 
27c0: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c   blob_reset(&del
27d0: 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  ta);.}../*.** CO
27e0: 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e  MMAND:  test-con
27f0: 74 65 6e 74 2d 64 65 6c 74 69 66 79 0a 2a 2a 0a  tent-deltify.**.
2800: 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 63  ** Convert the c
2810: 6f 6e 74 65 6e 74 20 61 74 20 52 49 44 20 69 6e  ontent at RID in
2820: 74 6f 20 61 20 64 65 6c 74 61 20 66 72 6f 6d 20  to a delta from 
2830: 53 52 43 49 44 2e 0a 2a 2f 0a 76 6f 69 64 20 74  SRCID..*/.void t
2840: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 64 65 6c 74  est_content_delt
2850: 69 66 79 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  ify_cmd(void){. 
2860: 20 69 66 28 20 67 2e 61 72 67 63 21 3d 35 20 29   if( g.argc!=5 )
2870: 20 75 73 61 67 65 28 22 52 49 44 20 53 52 43 49   usage("RID SRCI
2880: 44 20 46 4f 52 43 45 22 29 3b 0a 20 20 64 62 5f  D FORCE");.  db_
2890: 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74  must_be_within_t
28a0: 72 65 65 28 29 3b 0a 20 20 63 6f 6e 74 65 6e 74  ree();.  content
28b0: 5f 64 65 6c 74 69 66 79 28 61 74 6f 69 28 67 2e  _deltify(atoi(g.
28c0: 61 72 67 76 5b 32 5d 29 2c 20 61 74 6f 69 28 67  argv[2]), atoi(g
28d0: 2e 61 72 67 76 5b 33 5d 29 2c 20 61 74 6f 69 28  .argv[3]), atoi(
28e0: 67 2e 61 72 67 76 5b 34 5d 29 29 3b 0a 7d 0a     g.argv[4]));.}.