Hex Artifact Content
Not logged in

Artifact 60a9520d479793775a112f75959246fdc8f11d41:

File src/content.c part of check-in [8857e1eabb] - Fixed typos, and extended the documentation of content_deltify. by aku on 2007-08-28 05:00:20.

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 0a 20 20 2f 2a 20 46 69 6e 69 73  .  }..  /* Finis
1b20: 68 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  h the transactio
1b30: 6e 20 61 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f  n and cleanup */
1b40: 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26  .  db_finalize(&
1b50: 73 31 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72  s1);.  db_end_tr
1b60: 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20  ansaction(0);.  
1b70: 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68  blob_reset(&hash
1b80: 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72  );..  /* Make ar
1b90: 72 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 76 65  rangements to ve
1ba0: 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64 61  rify that the da
1bb0: 74 61 20 63 61 6e 20 62 65 20 72 65 63 6f 76 65  ta can be recove
1bc0: 72 65 64 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20  red.  ** before 
1bd0: 77 65 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 69  we commit */.  i
1be0: 66 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20  f( pBlob ){.    
1bf0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6d 70 72  blob_reset(&cmpr
1c00: 29 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 65  );.    verify_be
1c10: 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29  fore_commit(rid)
1c20: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1c30: 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d  id;.}../*.** COM
1c40: 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74  MAND:  test-cont
1c50: 65 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a 20 45 78  ent-put.**.** Ex
1c60: 74 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f  tract a blob fro
1c70: 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  m the database a
1c80: 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f  nd write it into
1c90: 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64   a file..*/.void
1ca0: 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 70 75   test_content_pu
1cb0: 74 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69  t_cmd(void){.  i
1cc0: 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63  nt rid;.  Blob c
1cd0: 6f 6e 74 65 6e 74 3b 0a 20 20 69 66 28 20 67 2e  ontent;.  if( g.
1ce0: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28  argc!=3 ) usage(
1cf0: 22 46 49 4c 45 4e 41 4d 45 22 29 3b 0a 20 20 64  "FILENAME");.  d
1d00: 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e  b_must_be_within
1d10: 5f 74 72 65 65 28 29 3b 0a 20 20 75 73 65 72 5f  _tree();.  user_
1d20: 73 65 6c 65 63 74 28 29 3b 0a 20 20 62 6c 6f 62  select();.  blob
1d30: 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28  _read_from_file(
1d40: 26 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 72 67 76  &content, g.argv
1d50: 5b 32 5d 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f  [2]);.  rid = co
1d60: 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65  ntent_put(&conte
1d70: 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 70 72 69  nt, 0, 0);.  pri
1d80: 6e 74 66 28 22 69 6e 73 65 72 74 65 64 20 61 73  ntf("inserted as
1d90: 20 72 65 63 6f 72 64 20 25 64 5c 6e 22 2c 20 72   record %d\n", r
1da0: 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  id);.}../*.** Ma
1db0: 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e 74  ke sure the cont
1dc0: 65 6e 74 20 61 74 20 72 69 64 20 69 73 20 74 68  ent at rid is th
1dd0: 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65  e original conte
1de0: 6e 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 0a  nt and is not a.
1df0: 2a 2a 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69  ** delta..*/.voi
1e00: 64 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74  d content_undelt
1e10: 61 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 66  a(int rid){.  if
1e20: 28 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 29  ( findSrcid(rid)
1e30: 3e 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 78  >0 ){.    Blob x
1e40: 3b 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e  ;.    if( conten
1e50: 74 5f 67 65 74 28 72 69 64 2c 20 26 78 29 20 29  t_get(rid, &x) )
1e60: 7b 0a 20 20 20 20 20 20 53 74 6d 74 20 73 3b 0a  {.      Stmt s;.
1e70: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65        db_prepare
1e80: 28 26 73 2c 20 22 55 50 44 41 54 45 20 62 6c 6f  (&s, "UPDATE blo
1e90: 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 63  b SET content=:c
1ea0: 2c 20 73 69 7a 65 3d 25 64 20 57 48 45 52 45 20  , size=%d WHERE 
1eb0: 72 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 20  rid=%d",.       
1ec0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c                bl
1ed0: 6f 62 5f 73 69 7a 65 28 26 78 29 2c 20 72 69 64  ob_size(&x), rid
1ee0: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f  );.      blob_co
1ef0: 6d 70 72 65 73 73 28 26 78 2c 20 26 78 29 3b 0a  mpress(&x, &x);.
1f00: 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c        db_bind_bl
1f10: 6f 62 28 26 73 2c 20 22 3a 63 22 2c 20 26 78 29  ob(&s, ":c", &x)
1f20: 3b 0a 20 20 20 20 20 20 64 62 5f 65 78 65 63 28  ;.      db_exec(
1f30: 26 73 29 3b 0a 20 20 20 20 20 20 64 62 5f 66 69  &s);.      db_fi
1f40: 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 20 20  nalize(&s);.    
1f50: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 78 29    blob_reset(&x)
1f60: 3b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69  ;.      db_multi
1f70: 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52  _exec("DELETE FR
1f80: 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72  OM delta WHERE r
1f90: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20  id=%d", rid);.  
1fa0: 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1fb0: 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d   COMMAND:  test-
1fc0: 63 6f 6e 74 65 6e 74 2d 75 6e 64 65 6c 74 61 0a  content-undelta.
1fd0: 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  **.** Make sure 
1fe0: 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 52  the content at R
1ff0: 45 43 4f 52 44 49 44 20 69 73 20 6e 6f 74 20 61  ECORDID is not a
2000: 20 64 65 6c 74 61 0a 2a 2f 0a 76 6f 69 64 20 74   delta.*/.void t
2010: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 75 6e 64 65  est_content_unde
2020: 6c 74 61 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  lta_cmd(void){. 
2030: 20 69 6e 74 20 72 69 64 3b 0a 20 20 69 66 28 20   int rid;.  if( 
2040: 67 2e 61 72 67 63 21 3d 32 20 29 20 75 73 61 67  g.argc!=2 ) usag
2050: 65 28 22 52 45 43 4f 52 44 49 44 22 29 3b 0a 20  e("RECORDID");. 
2060: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68   db_must_be_with
2070: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64  in_tree();.  rid
2080: 20 3d 20 61 74 6f 69 28 67 2e 61 72 67 76 5b 32   = atoi(g.argv[2
2090: 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 75 6e  ]);.  content_un
20a0: 64 65 6c 74 61 28 72 69 64 29 3b 0a 7d 0a 0a 2f  delta(rid);.}../
20b0: 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
20c0: 73 74 6f 72 61 67 65 20 6f 66 20 72 69 64 20 73  storage of rid s
20d0: 6f 20 74 68 61 74 20 69 74 20 69 73 20 61 20 64  o that it is a d
20e0: 65 6c 74 61 20 6f 66 20 73 72 63 69 64 2e 0a 2a  elta of srcid..*
20f0: 2a 0a 2a 2a 20 49 66 20 72 69 64 20 69 73 20 61  *.** If rid is a
2100: 6c 72 65 61 64 79 20 61 20 64 65 6c 74 61 20 66  lready a delta f
2110: 72 6f 6d 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  rom some other p
2120: 6c 61 63 65 20 74 68 65 6e 20 6e 6f 0a 2a 2a 20  lace then no.** 
2130: 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72  conversion occur
2140: 73 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20  s and this is a 
2150: 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 66 6f 72  no-op unless for
2160: 63 65 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ce==1..**.** If 
2170: 73 72 63 69 64 20 69 73 20 61 20 64 65 6c 74 61  srcid is a delta
2180: 20 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f 6e   that depends on
2190: 20 72 69 64 2c 20 74 68 65 6e 20 73 72 63 69 64   rid, then srcid
21a0: 20 69 73 0a 2a 2a 20 63 6f 6e 76 65 72 74 65 64   is.** converted
21b0: 20 74 6f 20 75 6e 64 65 6c 74 61 65 64 20 74 65   to undeltaed te
21c0: 78 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74  xt..**.** If eit
21d0: 68 65 72 20 72 69 64 20 6f 72 20 73 72 63 69 64  her rid or srcid
21e0: 20 63 6f 6e 74 61 69 6e 20 6c 65 73 73 20 74 68   contain less th
21f0: 61 6e 20 35 30 20 62 79 74 65 73 2c 20 6f 72 20  an 50 bytes, or 
2200: 69 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74  if the.** result
2210: 69 6e 67 20 64 65 6c 74 61 20 64 6f 65 73 20 6e  ing delta does n
2220: 6f 74 20 61 63 68 69 65 76 65 20 61 20 63 6f 6d  ot achieve a com
2230: 70 72 65 73 73 69 6f 6e 20 6f 66 20 61 74 20 6c  pression of at l
2240: 65 61 73 74 20 32 35 25 20 6f 6e 0a 2a 2a 20 69  east 25% on.** i
2250: 74 73 20 6f 77 6e 20 74 68 65 20 72 69 64 20 69  ts own the rid i
2260: 73 20 6c 65 66 74 20 75 6e 74 6f 75 63 68 65 64  s left untouched
2270: 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 49 4d  ..**.** NOTE: IM
2280: 48 4f 20 74 68 65 20 63 72 65 61 74 69 6f 6e 20  HO the creation 
2290: 6f 66 20 74 68 65 20 64 65 6c 74 61 20 73 68 6f  of the delta sho
22a0: 75 6c 64 20 62 65 20 64 65 66 65 72 65 64 20 75  uld be defered u
22b0: 6e 74 69 6c 20 61 66 74 65 72 0a 2a 2a 20 74 68  ntil after.** th
22c0: 65 20 62 6c 6f 62 20 73 69 7a 65 73 20 68 61 76  e blob sizes hav
22d0: 65 20 62 65 65 6e 20 63 68 65 63 6b 65 64 2e 20  e been checked. 
22e0: 44 6f 69 6e 67 20 69 74 20 62 65 66 6f 72 65 20  Doing it before 
22f0: 74 68 65 20 63 68 65 63 6b 20 61 73 20 69 73 0a  the check as is.
2300: 2a 2a 20 64 6f 6e 65 20 6e 6f 77 20 74 68 65 20  ** done now the 
2310: 63 6f 64 65 20 77 69 6c 6c 20 67 65 6e 65 72 61  code will genera
2320: 74 65 20 61 20 64 65 6c 74 61 20 6a 75 73 74 20  te a delta just 
2330: 74 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  to immediately t
2340: 68 72 6f 77 0a 2a 2a 20 69 74 20 61 77 61 79 2c  hrow.** it away,
2350: 20 77 61 73 74 69 6e 67 20 73 70 61 63 65 20 61   wasting space a
2360: 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a 76 6f 69 64  nd time..*/.void
2370: 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79   content_deltify
2380: 28 69 6e 74 20 72 69 64 2c 20 69 6e 74 20 73 72  (int rid, int sr
2390: 63 69 64 2c 20 69 6e 74 20 66 6f 72 63 65 29 7b  cid, int force){
23a0: 0a 20 20 69 6e 74 20 73 3b 0a 20 20 42 6c 6f 62  .  int s;.  Blob
23b0: 20 64 61 74 61 2c 20 73 72 63 2c 20 64 65 6c 74   data, src, delt
23c0: 61 3b 0a 20 20 53 74 6d 74 20 73 31 2c 20 73 32  a;.  Stmt s1, s2
23d0: 3b 0a 20 20 69 66 28 20 73 72 63 69 64 3d 3d 72  ;.  if( srcid==r
23e0: 69 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  id ) return;.  i
23f0: 66 28 20 21 66 6f 72 63 65 20 26 26 20 66 69 6e  f( !force && fin
2400: 64 53 72 63 69 64 28 72 69 64 29 3e 30 20 29 20  dSrcid(rid)>0 ) 
2410: 72 65 74 75 72 6e 3b 0a 20 20 73 20 3d 20 73 72  return;.  s = sr
2420: 63 69 64 3b 0a 20 20 77 68 69 6c 65 28 20 28 73  cid;.  while( (s
2430: 20 3d 20 66 69 6e 64 53 72 63 69 64 28 73 29 29   = findSrcid(s))
2440: 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 3d  >0 ){.    if( s=
2450: 3d 72 69 64 20 29 7b 0a 20 20 20 20 20 20 63 6f  =rid ){.      co
2460: 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 73 72  ntent_undelta(sr
2470: 63 69 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61  cid);.      brea
2480: 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63  k;.    }.  }.  c
2490: 6f 6e 74 65 6e 74 5f 67 65 74 28 73 72 63 69 64  ontent_get(srcid
24a0: 2c 20 26 73 72 63 29 3b 0a 20 20 63 6f 6e 74 65  , &src);.  conte
24b0: 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 64 61 74  nt_get(rid, &dat
24c0: 61 29 3b 0a 20 20 62 6c 6f 62 5f 64 65 6c 74 61  a);.  blob_delta
24d0: 5f 63 72 65 61 74 65 28 26 73 72 63 2c 20 26 64  _create(&src, &d
24e0: 61 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20  ata, &delta);.  
24f0: 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 73  if( blob_size(&s
2500: 72 63 29 3e 3d 35 30 20 26 26 20 62 6c 6f 62 5f  rc)>=50 && blob_
2510: 73 69 7a 65 28 26 64 61 74 61 29 3e 3d 35 30 20  size(&data)>=50 
2520: 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 62 6c  &&.           bl
2530: 6f 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 20  ob_size(&delta) 
2540: 3c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 61 74  < blob_size(&dat
2550: 61 29 2a 30 2e 37 35 20 29 7b 0a 20 20 20 20 62  a)*0.75 ){.    b
2560: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 26 64 65  lob_compress(&de
2570: 6c 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20  lta, &delta);.  
2580: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31    db_prepare(&s1
2590: 2c 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53  , "UPDATE blob S
25a0: 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61  ET content=:data
25b0: 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20   WHERE rid=%d", 
25c0: 72 69 64 29 3b 0a 20 20 20 20 64 62 5f 70 72 65  rid);.    db_pre
25d0: 70 61 72 65 28 26 73 32 2c 20 22 52 45 50 4c 41  pare(&s2, "REPLA
25e0: 43 45 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69  CE INTO delta(ri
25f0: 64 2c 73 72 63 69 64 29 56 41 4c 55 45 53 28 25  d,srcid)VALUES(%
2600: 64 2c 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63  d,%d)", rid, src
2610: 69 64 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64  id);.    db_bind
2620: 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74  _blob(&s1, ":dat
2630: 61 22 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20  a", &delta);.   
2640: 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61   db_begin_transa
2650: 63 74 69 6f 6e 28 29 3b 0a 20 20 20 20 64 62 5f  ction();.    db_
2660: 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20 20 64  exec(&s1);.    d
2670: 62 5f 65 78 65 63 28 26 73 32 29 3b 0a 20 20 20  b_exec(&s2);.   
2680: 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74   db_end_transact
2690: 69 6f 6e 28 30 29 3b 0a 20 20 20 20 64 62 5f 66  ion(0);.    db_f
26a0: 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 20  inalize(&s1);.  
26b0: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
26c0: 32 29 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62  2);.    verify_b
26d0: 65 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64  efore_commit(rid
26e0: 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65  );.  }.  blob_re
26f0: 73 65 74 28 26 73 72 63 29 3b 0a 20 20 62 6c 6f  set(&src);.  blo
2700: 62 5f 72 65 73 65 74 28 26 64 61 74 61 29 3b 0a  b_reset(&data);.
2710: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65    blob_reset(&de
2720: 6c 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  lta);.}../*.** C
2730: 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f  OMMAND:  test-co
2740: 6e 74 65 6e 74 2d 64 65 6c 74 69 66 79 0a 2a 2a  ntent-deltify.**
2750: 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20  .** Convert the 
2760: 63 6f 6e 74 65 6e 74 20 61 74 20 52 49 44 20 69  content at RID i
2770: 6e 74 6f 20 61 20 64 65 6c 74 61 20 66 72 6f 6d  nto a delta from
2780: 20 53 52 43 49 44 2e 0a 2a 2f 0a 76 6f 69 64 20   SRCID..*/.void 
2790: 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 64 65 6c  test_content_del
27a0: 74 69 66 79 5f 63 6d 64 28 76 6f 69 64 29 7b 0a  tify_cmd(void){.
27b0: 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 35 20    if( g.argc!=5 
27c0: 29 20 75 73 61 67 65 28 22 52 49 44 20 53 52 43  ) usage("RID SRC
27d0: 49 44 20 46 4f 52 43 45 22 29 3b 0a 20 20 64 62  ID FORCE");.  db
27e0: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
27f0: 74 72 65 65 28 29 3b 0a 20 20 63 6f 6e 74 65 6e  tree();.  conten
2800: 74 5f 64 65 6c 74 69 66 79 28 61 74 6f 69 28 67  t_deltify(atoi(g
2810: 2e 61 72 67 76 5b 32 5d 29 2c 20 61 74 6f 69 28  .argv[2]), atoi(
2820: 67 2e 61 72 67 76 5b 33 5d 29 2c 20 61 74 6f 69  g.argv[3]), atoi
2830: 28 67 2e 61 72 67 76 5b 34 5d 29 29 3b 0a 7d 0a  (g.argv[4]));.}.