Hex Artifact Content
Not logged in

Artifact 310f7c7a1fcc286122bbf7df225db81d1682bac6:

File src/content.c part of check-in [48c4e69d2b] - Cluster-based synchronization appears to be working. by drh on 2007-09-09 17:51:16. Also file src/content.c part of check-in [bbcb6326c9] - Pulled in the navbar and timeline changes. by aku on 2007-09-17 00:58:51.

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