Hex Artifact Content
Not logged in

Artifact 9b11b55b6ff7512e8e709fa599fa929204154d9b:

File src/content.c part of check-in [73bddaebb9] - The delta compress on xfer is working better now, but still needs work. by drh on 2007-08-09 17:42:59.

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 2c 20 63 6f 6e 73  id(int rid, cons
0470: 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20  t char *zDb){.  
0480: 53 74 6d 74 20 71 73 72 63 3b 0a 20 20 69 6e 74  Stmt qsrc;.  int
0490: 20 73 72 63 69 64 3b 0a 20 20 69 66 28 20 7a 44   srcid;.  if( zD
04a0: 62 20 29 7b 0a 20 20 20 20 64 62 5f 70 72 65 70  b ){.    db_prep
04b0: 61 72 65 28 26 71 73 72 63 2c 20 22 53 45 4c 45  are(&qsrc, "SELE
04c0: 43 54 20 73 72 63 69 64 20 46 52 4f 4d 20 25 73  CT srcid FROM %s
04d0: 2e 64 65 6c 74 61 20 57 48 45 52 45 20 72 69 64  .delta WHERE rid
04e0: 3d 25 64 22 2c 20 7a 44 62 2c 20 72 69 64 29 3b  =%d", zDb, rid);
04f0: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62  .  }else{.    db
0500: 5f 70 72 65 70 61 72 65 28 26 71 73 72 63 2c 20  _prepare(&qsrc, 
0510: 22 53 45 4c 45 43 54 20 73 72 63 69 64 20 46 52  "SELECT srcid FR
0520: 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72  OM delta WHERE r
0530: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20  id=%d", rid);.  
0540: 7d 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 28  }.  if( db_step(
0550: 26 71 73 72 63 29 3d 3d 53 51 4c 49 54 45 5f 52  &qsrc)==SQLITE_R
0560: 4f 57 20 29 7b 0a 20 20 20 20 73 72 63 69 64 20  OW ){.    srcid 
0570: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  = db_column_int(
0580: 26 71 73 72 63 2c 20 30 29 3b 0a 20 20 7d 65 6c  &qsrc, 0);.  }el
0590: 73 65 7b 0a 20 20 20 20 73 72 63 69 64 20 3d 20  se{.    srcid = 
05a0: 30 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  0;.  }.  db_fina
05b0: 6c 69 7a 65 28 26 71 73 72 63 29 3b 0a 20 20 72  lize(&qsrc);.  r
05c0: 65 74 75 72 6e 20 73 72 63 69 64 3b 0a 7d 0a 0a  eturn srcid;.}..
05d0: 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
05e0: 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 49 44  e content for ID
05f0: 20 72 69 64 20 61 6e 64 20 70 75 74 20 69 74 20   rid and put it 
0600: 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 75 6e 69 6e  into the.** unin
0610: 69 74 69 61 6c 69 7a 65 64 20 62 6c 6f 62 2e 0a  itialized blob..
0620: 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74 65 6e 74 5f  */.void content_
0630: 67 65 74 5f 66 72 6f 6d 5f 64 62 28 69 6e 74 20  get_from_db(int 
0640: 72 69 64 2c 20 42 6c 6f 62 20 2a 70 42 6c 6f 62  rid, Blob *pBlob
0650: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
0660: 62 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20  b){.  Stmt q;.  
0670: 69 6e 74 20 73 72 63 69 64 3b 0a 20 20 61 73 73  int srcid;.  ass
0680: 65 72 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72  ert( g.repositor
0690: 79 4f 70 65 6e 20 29 3b 0a 20 20 73 72 63 69 64  yOpen );.  srcid
06a0: 20 3d 20 66 69 6e 64 53 72 63 69 64 28 72 69 64   = findSrcid(rid
06b0: 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 7a 44  , zDb);.  if( zD
06c0: 62 20 29 7b 0a 20 20 20 20 64 62 5f 70 72 65 70  b ){.    db_prep
06d0: 61 72 65 28 26 71 2c 20 0a 20 20 20 20 20 20 20  are(&q, .       
06e0: 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74 20  "SELECT content 
06f0: 46 52 4f 4d 20 25 73 2e 62 6c 6f 62 20 57 48 45  FROM %s.blob WHE
0700: 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69  RE rid=%d AND si
0710: 7a 65 3e 3d 30 22 2c 0a 20 20 20 20 20 20 20 7a  ze>=0",.       z
0720: 44 62 2c 20 72 69 64 0a 20 20 20 20 29 3b 0a 20  Db, rid.    );. 
0730: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 5f 70   }else{.    db_p
0740: 72 65 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20  repare(&q, .    
0750: 20 20 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65     "SELECT conte
0760: 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  nt FROM blob WHE
0770: 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69  RE rid=%d AND si
0780: 7a 65 3e 3d 30 22 2c 0a 20 20 20 20 20 20 20 72  ze>=0",.       r
0790: 69 64 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20  id.    );.  }.  
07a0: 69 66 28 20 73 72 63 69 64 20 29 7b 0a 20 20 20  if( srcid ){.   
07b0: 20 42 6c 6f 62 20 73 72 63 3b 0a 20 20 20 20 63   Blob src;.    c
07c0: 6f 6e 74 65 6e 74 5f 67 65 74 5f 66 72 6f 6d 5f  ontent_get_from_
07d0: 64 62 28 73 72 63 69 64 2c 20 26 73 72 63 2c 20  db(srcid, &src, 
07e0: 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 64 62  zDb);.    if( db
07f0: 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54  _step(&q)==SQLIT
0800: 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 42  E_ROW ){.      B
0810: 6c 6f 62 20 64 65 6c 74 61 3b 0a 20 20 20 20 20  lob delta;.     
0820: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c   db_ephemeral_bl
0830: 6f 62 28 26 71 2c 20 30 2c 20 26 64 65 6c 74 61  ob(&q, 0, &delta
0840: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 75 6e  );.      blob_un
0850: 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74 61 2c  compress(&delta,
0860: 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 20 20   &delta);.      
0870: 62 6c 6f 62 5f 69 6e 69 74 28 70 42 6c 6f 62 2c  blob_init(pBlob,
0880: 30 2c 30 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62  0,0);.      blob
0890: 5f 64 65 6c 74 61 5f 61 70 70 6c 79 28 26 73 72  _delta_apply(&sr
08a0: 63 2c 20 26 64 65 6c 74 61 2c 20 70 42 6c 6f 62  c, &delta, pBlob
08b0: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65  );.      blob_re
08c0: 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20 20  set(&delta);.   
08d0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 62 6c   }else{.      bl
08e0: 6f 62 5f 69 6e 69 74 28 70 42 6c 6f 62 2c 20 30  ob_init(pBlob, 0
08f0: 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
0900: 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29  blob_reset(&src)
0910: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
0920: 66 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d  f( db_step(&q)==
0930: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
0940: 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72 61 6c      db_ephemeral
0950: 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20 70 42 6c  _blob(&q, 0, pBl
0960: 6f 62 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  ob);.      blob_
0970: 75 6e 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f 62  uncompress(pBlob
0980: 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 7d 65  , pBlob);.    }e
0990: 6c 73 65 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  lse{.      blob_
09a0: 69 6e 69 74 28 70 42 6c 6f 62 2c 30 2c 20 30 29  init(pBlob,0, 0)
09b0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62  ;.    }.  }.  db
09c0: 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 7d  _finalize(&q);.}
09d0: 0a 76 6f 69 64 20 63 6f 6e 74 65 6e 74 5f 67 65  .void content_ge
09e0: 74 28 69 6e 74 20 72 69 64 2c 20 42 6c 6f 62 20  t(int rid, Blob 
09f0: 2a 70 42 6c 6f 62 29 7b 0a 20 20 63 6f 6e 74 65  *pBlob){.  conte
0a00: 6e 74 5f 67 65 74 5f 66 72 6f 6d 5f 64 62 28 72  nt_get_from_db(r
0a10: 69 64 2c 20 70 42 6c 6f 62 2c 20 30 29 3b 0a 7d  id, pBlob, 0);.}
0a20: 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a  ../*.** COMMAND:
0a30: 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 67    test-content-g
0a40: 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74  et.**.** Extract
0a50: 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65   a blob from the
0a60: 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 72   database and wr
0a70: 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69  ite it into a fi
0a80: 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74  le..*/.void test
0a90: 5f 63 6f 6e 74 65 6e 74 5f 67 65 74 5f 63 6d 64  _content_get_cmd
0aa0: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69  (void){.  int ri
0ab0: 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e  d;.  Blob conten
0ac0: 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t;.  const char 
0ad0: 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e  *zFile;.  if( g.
0ae0: 61 72 67 63 21 3d 34 20 26 26 20 67 2e 61 72 67  argc!=4 && g.arg
0af0: 63 21 3d 33 20 29 20 75 73 61 67 65 28 22 52 45  c!=3 ) usage("RE
0b00: 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45  CORDID ?FILENAME
0b10: 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67  ?");.  zFile = g
0b20: 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67  .argc==4 ? g.arg
0b30: 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62  v[3] : "-";.  db
0b40: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
0b50: 74 72 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20  tree();.  rid = 
0b60: 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72  name_to_rid(g.ar
0b70: 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e  gv[2]);.  conten
0b80: 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74  t_get(rid, &cont
0b90: 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72 69  ent);.  blob_wri
0ba0: 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e 74  te_to_file(&cont
0bb0: 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a 0a  ent, zFile);.}..
0bc0: 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20  /*.** COMMAND:  
0bd0: 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 72 61 77  test-content-raw
0be0: 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63  get.**.** Extrac
0bf0: 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68  t a blob from th
0c00: 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77  e database and w
0c10: 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66  rite it into a f
0c20: 69 6c 65 2e 20 20 54 68 69 73 0a 2a 2a 20 76 65  ile.  This.** ve
0c30: 72 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65  rsion does not e
0c40: 78 70 61 6e 64 20 74 68 65 20 64 65 6c 74 61 2e  xpand the delta.
0c50: 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f  .*/.void test_co
0c60: 6e 74 65 6e 74 5f 72 61 77 67 65 74 5f 63 6d 64  ntent_rawget_cmd
0c70: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69  (void){.  int ri
0c80: 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e  d;.  Blob conten
0c90: 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t;.  const char 
0ca0: 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e  *zFile;.  if( g.
0cb0: 61 72 67 63 21 3d 34 20 26 26 20 67 2e 61 72 67  argc!=4 && g.arg
0cc0: 63 21 3d 33 20 29 20 75 73 61 67 65 28 22 52 45  c!=3 ) usage("RE
0cd0: 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45  CORDID ?FILENAME
0ce0: 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67  ?");.  zFile = g
0cf0: 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67  .argc==4 ? g.arg
0d00: 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62  v[3] : "-";.  db
0d10: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
0d20: 74 72 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20  tree();.  rid = 
0d30: 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72  name_to_rid(g.ar
0d40: 67 76 5b 32 5d 29 3b 0a 20 20 62 6c 6f 62 5f 7a  gv[2]);.  blob_z
0d50: 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20  ero(&content);. 
0d60: 20 64 62 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e   db_blob(&conten
0d70: 74 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65  t, "SELECT conte
0d80: 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  nt FROM blob WHE
0d90: 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29  RE rid=%d", rid)
0da0: 3b 0a 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72  ;.  blob_uncompr
0db0: 65 73 73 28 26 63 6f 6e 74 65 6e 74 2c 20 26 63  ess(&content, &c
0dc0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f  ontent);.  blob_
0dd0: 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63  write_to_file(&c
0de0: 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a  ontent, zFile);.
0df0: 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63  }../*.** Write c
0e00: 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  ontent into the 
0e10: 64 61 74 61 62 61 73 65 2e 20 20 52 65 74 75 72  database.  Retur
0e20: 6e 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e  n the record ID.
0e30: 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 6f 6e 74    If the.** cont
0e40: 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69  ent is already i
0e50: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
0e60: 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  just return the 
0e70: 72 65 63 6f 72 64 20 49 44 2e 0a 2a 2a 0a 2a 2a  record ID..**.**
0e80: 20 41 20 70 68 61 6e 74 6f 6d 20 69 73 20 77 72   A phantom is wr
0e90: 69 74 74 65 6e 20 69 66 20 70 42 6c 6f 62 3d 3d  itten if pBlob==
0ea0: 30 2e 20 20 49 66 20 70 42 6c 6f 62 3d 3d 30 20  0.  If pBlob==0 
0eb0: 74 68 65 6e 20 74 68 65 20 55 55 49 44 20 69 73  then the UUID is
0ec0: 20 73 65 74 0a 2a 2a 20 74 6f 20 7a 55 75 69 64   set.** to zUuid
0ed0: 2e 20 20 4f 74 68 65 72 77 69 73 65 20 7a 55 75  .  Otherwise zUu
0ee0: 69 64 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a  id is ignored..*
0ef0: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f  *.** If the reco
0f00: 72 64 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  rd already exist
0f10: 73 20 62 75 74 20 69 73 20 61 20 70 68 61 6e 74  s but is a phant
0f20: 6f 6d 2c 20 74 68 65 20 70 42 6c 6f 62 20 63 6f  om, the pBlob co
0f30: 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 69 6e 73 65  ntent.** is inse
0f40: 72 74 65 64 20 61 6e 64 20 74 68 65 20 70 68 61  rted and the pha
0f50: 74 6f 6d 20 62 65 63 6f 6d 65 73 20 61 20 72 65  tom becomes a re
0f60: 61 6c 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 69 6e  al record..*/.in
0f70: 74 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 42 6c  t content_put(Bl
0f80: 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74  ob *pBlob, const
0f90: 20 63 68 61 72 20 2a 7a 55 75 69 64 29 7b 0a 20   char *zUuid){. 
0fa0: 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 69 6e 74   int size;.  int
0fb0: 20 72 69 64 3b 0a 20 20 53 74 6d 74 20 73 31 3b   rid;.  Stmt s1;
0fc0: 0a 20 20 42 6c 6f 62 20 63 6d 70 72 3b 0a 20 20  .  Blob cmpr;.  
0fd0: 42 6c 6f 62 20 68 61 73 68 3b 0a 20 20 61 73 73  Blob hash;.  ass
0fe0: 65 72 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72  ert( g.repositor
0ff0: 79 4f 70 65 6e 20 29 3b 0a 20 20 69 66 28 20 70  yOpen );.  if( p
1000: 42 6c 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20 62  Blob==0 ){.    b
1010: 6c 6f 62 5f 69 6e 69 74 28 26 68 61 73 68 2c 20  lob_init(&hash, 
1020: 7a 55 75 69 64 2c 20 2d 31 29 3b 0a 20 20 20 20  zUuid, -1);.    
1030: 73 69 7a 65 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c  size = -1;.  }el
1040: 73 65 7b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f  se{.    sha1sum_
1050: 62 6c 6f 62 28 70 42 6c 6f 62 2c 20 26 68 61 73  blob(pBlob, &has
1060: 68 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 62  h);.    size = b
1070: 6c 6f 62 5f 73 69 7a 65 28 70 42 6c 6f 62 29 3b  lob_size(pBlob);
1080: 0a 20 20 7d 0a 20 20 64 62 5f 62 65 67 69 6e 5f  .  }.  db_begin_
1090: 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 0a  transaction();..
10a0: 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
10b0: 65 20 69 66 20 74 68 65 20 65 6e 74 72 79 20 61  e if the entry a
10c0: 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e  lready exists an
10d0: 64 20 69 66 20 69 74 20 64 6f 65 73 20 77 68 65  d if it does whe
10e0: 74 68 65 72 0a 20 20 2a 2a 20 6f 72 20 6e 6f 74  ther.  ** or not
10f0: 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 61 20   the entry is a 
1100: 70 68 61 6e 74 6f 6d 0a 20 20 2a 2f 0a 20 20 64  phantom.  */.  d
1110: 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20 22  b_prepare(&s1, "
1120: 53 45 4c 45 43 54 20 72 69 64 2c 20 73 69 7a 65  SELECT rid, size
1130: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45   FROM blob WHERE
1140: 20 75 75 69 64 3d 25 42 22 2c 20 26 68 61 73 68   uuid=%B", &hash
1150: 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70  );.  if( db_step
1160: 28 26 73 31 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  (&s1)==SQLITE_RO
1170: 57 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20 64  W ){.    rid = d
1180: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 31  b_column_int(&s1
1190: 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64 62  , 0);.    if( db
11a0: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 31 2c  _column_int(&s1,
11b0: 20 31 29 3e 3d 30 20 7c 7c 20 70 42 6c 6f 62 3d   1)>=0 || pBlob=
11c0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 45  =0 ){.      /* E
11d0: 69 74 68 65 72 20 74 68 65 20 65 6e 74 72 79 20  ither the entry 
11e0: 69 73 20 6e 6f 74 20 61 20 70 68 61 6e 74 6f 6d  is not a phantom
11f0: 20 6f 72 20 69 74 20 69 73 20 61 20 70 68 61 6e   or it is a phan
1200: 74 6f 6d 20 62 75 74 20 77 65 0a 20 20 20 20 20  tom but we.     
1210: 20 2a 2a 20 68 61 76 65 20 6e 6f 20 64 61 74 61   ** have no data
1220: 20 77 69 74 68 20 77 68 69 63 68 20 74 6f 20 64   with which to d
1230: 65 70 68 61 74 68 6f 6d 69 7a 65 20 69 74 2e 20  ephathomize it. 
1240: 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c   In either case,
1250: 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20  .      ** there 
1260: 69 73 20 6e 6f 74 68 69 6e 67 20 66 6f 72 20 75  is nothing for u
1270: 73 65 20 74 6f 20 64 6f 20 6f 74 68 65 72 20 74  se to do other t
1280: 68 61 6e 20 72 65 74 75 72 6e 20 74 68 65 20 52  han return the R
1290: 49 44 2e 20 2a 2f 0a 20 20 20 20 20 20 64 62 5f  ID. */.      db_
12a0: 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20  finalize(&s1);. 
12b0: 20 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e       db_end_tran
12c0: 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20 20  saction(0);.    
12d0: 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 20 20    return rid;.  
12e0: 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
12f0: 20 72 69 64 20 3d 20 30 3b 20 20 2f 2a 20 4e 6f   rid = 0;  /* No
1300: 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 65 20   entry with the 
1310: 73 61 6d 65 20 55 55 49 44 20 63 75 72 72 65 6e  same UUID curren
1320: 74 6c 79 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  tly exists */.  
1330: 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28  }.  db_finalize(
1340: 26 73 31 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73  &s1);..  /* Cons
1350: 74 72 75 63 74 20 61 20 72 65 63 65 69 76 65 64  truct a received
1360: 2d 66 72 6f 6d 20 49 44 20 69 66 20 77 65 20 64  -from ID if we d
1370: 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 61  o not already ha
1380: 76 65 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20  ve one */.  if( 
1390: 67 2e 72 63 76 69 64 3d 3d 30 20 26 26 20 70 42  g.rcvid==0 && pB
13a0: 6c 6f 62 21 3d 30 20 29 7b 0a 20 20 20 20 64 62  lob!=0 ){.    db
13b0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
13c0: 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
13d0: 20 72 63 76 66 72 6f 6d 28 75 69 64 2c 20 6d 74   rcvfrom(uid, mt
13e0: 69 6d 65 2c 20 6e 6f 6e 63 65 2c 20 69 70 61 64  ime, nonce, ipad
13f0: 64 72 29 22 0a 20 20 20 20 20 20 20 22 56 41 4c  dr)".       "VAL
1400: 55 45 53 28 25 64 2c 20 6a 75 6c 69 61 6e 64 61  UES(%d, julianda
1410: 79 28 27 6e 6f 77 27 29 2c 20 25 51 2c 20 25 51  y('now'), %Q, %Q
1420: 29 22 2c 0a 20 20 20 20 20 20 20 67 2e 75 73 65  )",.       g.use
1430: 72 55 69 64 2c 20 67 2e 7a 4e 6f 6e 63 65 2c 20  rUid, g.zNonce, 
1440: 67 2e 7a 49 70 41 64 64 72 0a 20 20 20 20 29 3b  g.zIpAddr.    );
1450: 0a 20 20 20 20 67 2e 72 63 76 69 64 20 3d 20 64  .    g.rcvid = d
1460: 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  b_last_insert_ro
1470: 77 69 64 28 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  wid();.  }..  if
1480: 28 20 72 69 64 3e 30 20 29 7b 0a 20 20 20 20 2f  ( rid>0 ){.    /
1490: 2a 20 57 65 20 61 72 65 20 6a 75 73 74 20 61 64  * We are just ad
14a0: 64 69 6e 67 20 64 61 74 61 20 74 6f 20 61 20 70  ding data to a p
14b0: 68 61 6e 74 6f 6d 20 2a 2f 0a 20 20 20 20 61 73  hantom */.    as
14c0: 73 65 72 74 28 20 70 42 6c 6f 62 21 3d 30 20 29  sert( pBlob!=0 )
14d0: 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65  ;.    db_prepare
14e0: 28 26 73 31 2c 0a 20 20 20 20 20 20 22 55 50 44  (&s1,.      "UPD
14f0: 41 54 45 20 62 6c 6f 62 20 53 45 54 20 72 63 76  ATE blob SET rcv
1500: 69 64 3d 25 64 2c 20 73 69 7a 65 3d 25 64 2c 20  id=%d, size=%d, 
1510: 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57 48  content=:data WH
1520: 45 52 45 20 72 69 64 3d 25 64 22 2c 0a 20 20 20  ERE rid=%d",.   
1530: 20 20 20 20 67 2e 72 63 76 69 64 2c 20 73 69 7a      g.rcvid, siz
1540: 65 2c 20 72 69 64 0a 20 20 20 20 29 3b 0a 20 20  e, rid.    );.  
1550: 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28    blob_compress(
1560: 70 42 6c 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20  pBlob, &cmpr);. 
1570: 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28     db_bind_blob(
1580: 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 63  &s1, ":data", &c
1590: 6d 70 72 29 3b 0a 20 20 20 20 64 62 5f 65 78 65  mpr);.    db_exe
15a0: 63 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 6d  c(&s1);.    db_m
15b0: 75 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54  ulti_exec("DELET
15c0: 45 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 57  E FROM phantom W
15d0: 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69  HERE rid=%d", ri
15e0: 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  d);.  }else{.   
15f0: 20 2f 2a 20 57 65 20 61 72 65 20 63 72 65 61 74   /* We are creat
1600: 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20  ing a new entry 
1610: 2a 2f 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72  */.    db_prepar
1620: 65 28 26 73 31 2c 0a 20 20 20 20 20 20 22 49 4e  e(&s1,.      "IN
1630: 53 45 52 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72  SERT INTO blob(r
1640: 63 76 69 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63  cvid,size,uuid,c
1650: 6f 6e 74 65 6e 74 29 22 0a 20 20 20 20 20 20 22  ontent)".      "
1660: 56 41 4c 55 45 53 28 25 64 2c 25 64 2c 27 25 73  VALUES(%d,%d,'%s
1670: 27 2c 3a 64 61 74 61 29 22 2c 0a 20 20 20 20 20  ',:data)",.     
1680: 20 20 67 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c    g.rcvid, size,
1690: 20 62 6c 6f 62 5f 73 74 72 28 26 68 61 73 68 29   blob_str(&hash)
16a0: 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20  .    );.    if( 
16b0: 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 62  pBlob ){.      b
16c0: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c  lob_compress(pBl
16d0: 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20  ob, &cmpr);.    
16e0: 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26    db_bind_blob(&
16f0: 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 63 6d  s1, ":data", &cm
1700: 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  pr);.    }.    d
1710: 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20  b_exec(&s1);.   
1720: 20 72 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69   rid = db_last_i
1730: 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20  nsert_rowid();. 
1740: 20 20 20 69 66 28 20 21 70 42 6c 6f 62 20 29 7b     if( !pBlob ){
1750: 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  .      db_multi_
1760: 65 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20  exec("INSERT OR 
1770: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 68 61 6e  IGNORE INTO phan
1780: 74 6f 6d 20 56 41 4c 55 45 53 28 25 64 29 22 2c  tom VALUES(%d)",
1790: 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   rid);.    }.  }
17a0: 0a 0a 0a 20 20 2f 2a 20 46 69 6e 69 73 68 20 74  ...  /* Finish t
17b0: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  he transaction a
17c0: 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f 0a 20 20  nd cleanup */.  
17d0: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29  db_finalize(&s1)
17e0: 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73  ;.  db_end_trans
17f0: 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 62 6c 6f  action(0);.  blo
1800: 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b 0a  b_reset(&hash);.
1810: 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72 72 61 6e  .  /* Make arran
1820: 67 65 6d 65 6e 74 73 20 74 6f 20 76 65 72 69 66  gements to verif
1830: 79 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20  y that the data 
1840: 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72 65 64  can be recovered
1850: 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 77 65 20  .  ** before we 
1860: 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 69 66 28 20  commit */.  if( 
1870: 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 62 6c 6f  pBlob ){.    blo
1880: 62 5f 72 65 73 65 74 28 26 63 6d 70 72 29 3b 0a  b_reset(&cmpr);.
1890: 20 20 20 20 76 65 72 69 66 79 5f 62 65 66 6f 72      verify_befor
18a0: 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20  e_commit(rid);. 
18b0: 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 69 64 3b   }.  return rid;
18c0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e  .}../*.** COMMAN
18d0: 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74  D:  test-content
18e0: 2d 70 75 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61  -put.**.** Extra
18f0: 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74  ct a blob from t
1900: 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  he database and 
1910: 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20  write it into a 
1920: 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65  file..*/.void te
1930: 73 74 5f 63 6f 6e 74 65 6e 74 5f 70 75 74 5f 63  st_content_put_c
1940: 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20  md(void){.  int 
1950: 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74  rid;.  Blob cont
1960: 65 6e 74 3b 0a 20 20 69 66 28 20 67 2e 61 72 67  ent;.  if( g.arg
1970: 63 21 3d 33 20 29 20 75 73 61 67 65 28 22 46 49  c!=3 ) usage("FI
1980: 4c 45 4e 41 4d 45 22 29 3b 0a 20 20 64 62 5f 6d  LENAME");.  db_m
1990: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
19a0: 65 65 28 29 3b 0a 20 20 75 73 65 72 5f 73 65 6c  ee();.  user_sel
19b0: 65 63 74 28 29 3b 0a 20 20 62 6c 6f 62 5f 72 65  ect();.  blob_re
19c0: 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 63 6f  ad_from_file(&co
19d0: 6e 74 65 6e 74 2c 20 67 2e 61 72 67 76 5b 32 5d  ntent, g.argv[2]
19e0: 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f 6e 74 65  );.  rid = conte
19f0: 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c  nt_put(&content,
1a00: 20 30 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 69   0);.  printf("i
1a10: 6e 73 65 72 74 65 64 20 61 73 20 72 65 63 6f 72  nserted as recor
1a20: 64 20 25 64 5c 6e 22 2c 20 72 69 64 29 3b 0a 7d  d %d\n", rid);.}
1a30: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
1a40: 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74  e the content at
1a50: 20 72 69 64 20 69 73 20 74 68 65 20 6f 72 69 67   rid is the orig
1a60: 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 61 6e 64  inal content and
1a70: 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 64 65 6c   is not a.** del
1a80: 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74  ta..*/.void cont
1a90: 65 6e 74 5f 75 6e 64 65 6c 74 61 28 69 6e 74 20  ent_undelta(int 
1aa0: 72 69 64 29 7b 0a 20 20 69 66 28 20 66 69 6e 64  rid){.  if( find
1ab0: 53 72 63 69 64 28 72 69 64 2c 20 30 29 3e 30 20  Srcid(rid, 0)>0 
1ac0: 29 7b 0a 20 20 20 20 42 6c 6f 62 20 78 3b 0a 20  ){.    Blob x;. 
1ad0: 20 20 20 53 74 6d 74 20 73 3b 0a 20 20 20 20 63     Stmt s;.    c
1ae0: 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20  ontent_get(rid, 
1af0: 26 78 29 3b 0a 20 20 20 20 64 62 5f 70 72 65 70  &x);.    db_prep
1b00: 61 72 65 28 26 73 2c 20 22 55 50 44 41 54 45 20  are(&s, "UPDATE 
1b10: 62 6c 6f 62 20 53 45 54 20 63 6f 6e 74 65 6e 74  blob SET content
1b20: 3d 3a 63 20 57 48 45 52 45 20 72 69 64 3d 25 64  =:c WHERE rid=%d
1b30: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 62 6c 6f  ", rid);.    blo
1b40: 62 5f 63 6f 6d 70 72 65 73 73 28 26 78 2c 20 26  b_compress(&x, &
1b50: 78 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f  x);.    db_bind_
1b60: 62 6c 6f 62 28 26 73 2c 20 22 3a 63 22 2c 20 26  blob(&s, ":c", &
1b70: 78 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28  x);.    db_exec(
1b80: 26 73 29 3b 0a 20 20 20 20 64 62 5f 66 69 6e 61  &s);.    db_fina
1b90: 6c 69 7a 65 28 26 73 29 3b 0a 20 20 20 20 62 6c  lize(&s);.    bl
1ba0: 6f 62 5f 72 65 73 65 74 28 26 78 29 3b 0a 20 20  ob_reset(&x);.  
1bb0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
1bc0: 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 64 65 6c  "DELETE FROM del
1bd0: 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64 22  ta WHERE rid=%d"
1be0: 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  , rid);.  }.}../
1bf0: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74  *.** COMMAND:  t
1c00: 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 75 6e 64 65  est-content-unde
1c10: 6c 74 61 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 73  lta.**.** Make s
1c20: 75 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ure the content 
1c30: 61 74 20 52 45 43 4f 52 44 49 44 20 69 73 20 6e  at RECORDID is n
1c40: 6f 74 20 61 20 64 65 6c 74 61 0a 2a 2f 0a 76 6f  ot a delta.*/.vo
1c50: 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f  id test_content_
1c60: 75 6e 64 65 6c 74 61 5f 63 6d 64 28 76 6f 69 64  undelta_cmd(void
1c70: 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20 20  ){.  int rid;.  
1c80: 69 66 28 20 67 2e 61 72 67 63 21 3d 32 20 29 20  if( g.argc!=2 ) 
1c90: 75 73 61 67 65 28 22 52 45 43 4f 52 44 49 44 22  usage("RECORDID"
1ca0: 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f  );.  db_must_be_
1cb0: 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20  within_tree();. 
1cc0: 20 72 69 64 20 3d 20 61 74 6f 69 28 67 2e 61 72   rid = atoi(g.ar
1cd0: 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e  gv[2]);.  conten
1ce0: 74 5f 75 6e 64 65 6c 74 61 28 72 69 64 29 3b 0a  t_undelta(rid);.
1cf0: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
1d00: 74 68 65 20 73 74 6f 72 61 67 65 20 6f 66 20 72  the storage of r
1d10: 69 64 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  id so that it is
1d20: 20 61 20 64 65 6c 74 61 20 6f 66 20 73 72 63 69   a delta of srci
1d30: 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 69 64 20  d..**.** If rid 
1d40: 69 73 20 61 6c 72 65 61 64 79 20 61 20 64 65 6c  is already a del
1d50: 74 61 20 66 72 6f 6d 20 73 6f 6d 65 20 6f 74 68  ta from some oth
1d60: 65 72 20 70 6c 61 63 65 20 74 68 65 6e 20 6e 6f  er place then no
1d70: 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f  .** conversion o
1d80: 63 63 75 72 73 20 61 6e 64 20 74 68 69 73 20 69  ccurs and this i
1d90: 73 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73  s a no-op unless
1da0: 20 66 6f 72 63 65 3d 3d 31 2e 0a 2a 2a 0a 2a 2a   force==1..**.**
1db0: 20 49 66 20 73 72 63 69 64 20 69 73 20 61 20 64   If srcid is a d
1dc0: 65 6c 74 61 20 74 68 61 74 20 64 65 70 65 6e 64  elta that depend
1dd0: 73 20 6f 6e 20 72 69 64 2c 20 74 68 65 6e 20 73  s on rid, then s
1de0: 72 63 69 64 20 69 73 0a 2a 2a 20 63 6f 6e 76 65  rcid is.** conve
1df0: 72 74 65 64 20 74 6f 20 75 6e 64 65 6c 74 61 65  rted to undeltae
1e00: 64 20 74 65 78 74 2e 0a 2a 2f 0a 76 6f 69 64 20  d text..*/.void 
1e10: 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 28  content_deltify(
1e20: 69 6e 74 20 72 69 64 2c 20 69 6e 74 20 73 72 63  int rid, int src
1e30: 69 64 2c 20 69 6e 74 20 66 6f 72 63 65 29 7b 0a  id, int force){.
1e40: 20 20 69 6e 74 20 73 3b 0a 20 20 42 6c 6f 62 20    int s;.  Blob 
1e50: 64 61 74 61 2c 20 73 72 63 2c 20 64 65 6c 74 61  data, src, delta
1e60: 3b 0a 20 20 53 74 6d 74 20 73 31 2c 20 73 32 3b  ;.  Stmt s1, s2;
1e70: 0a 20 20 69 66 28 20 73 72 63 69 64 3d 3d 72 69  .  if( srcid==ri
1e80: 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  d ) return;.  if
1e90: 28 20 21 66 6f 72 63 65 20 26 26 20 66 69 6e 64  ( !force && find
1ea0: 53 72 63 69 64 28 72 69 64 2c 20 30 29 3e 30 20  Srcid(rid, 0)>0 
1eb0: 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 20 3d 20  ) return;.  s = 
1ec0: 73 72 63 69 64 3b 0a 20 20 77 68 69 6c 65 28 20  srcid;.  while( 
1ed0: 28 73 20 3d 20 66 69 6e 64 53 72 63 69 64 28 73  (s = findSrcid(s
1ee0: 2c 20 30 29 29 3e 30 20 29 7b 0a 20 20 20 20 69  , 0))>0 ){.    i
1ef0: 66 28 20 73 3d 3d 72 69 64 20 29 7b 0a 20 20 20  f( s==rid ){.   
1f00: 20 20 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c     content_undel
1f10: 74 61 28 73 72 63 69 64 29 3b 0a 20 20 20 20 20  ta(srcid);.     
1f20: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1f30: 7d 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28  }.  content_get(
1f40: 73 72 63 69 64 2c 20 26 73 72 63 29 3b 0a 20 20  srcid, &src);.  
1f50: 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c  content_get(rid,
1f60: 20 26 64 61 74 61 29 3b 0a 20 20 62 6c 6f 62 5f   &data);.  blob_
1f70: 64 65 6c 74 61 5f 63 72 65 61 74 65 28 26 73 72  delta_create(&sr
1f80: 63 2c 20 26 64 61 74 61 2c 20 26 64 65 6c 74 61  c, &data, &delta
1f90: 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62 5f 73 69  );.  if( blob_si
1fa0: 7a 65 28 26 73 72 63 29 3e 3d 35 30 20 26 26 20  ze(&src)>=50 && 
1fb0: 62 6c 6f 62 5f 73 69 7a 65 28 26 64 61 74 61 29  blob_size(&data)
1fc0: 3e 3d 35 30 20 26 26 0a 20 20 20 20 20 20 20 20  >=50 &&.        
1fd0: 20 20 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 65     blob_size(&de
1fe0: 6c 74 61 29 20 3c 20 62 6c 6f 62 5f 73 69 7a 65  lta) < blob_size
1ff0: 28 26 64 61 74 61 29 2a 30 2e 37 35 20 29 7b 0a  (&data)*0.75 ){.
2000: 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73      blob_compres
2010: 73 28 26 64 65 6c 74 61 2c 20 26 64 65 6c 74 61  s(&delta, &delta
2020: 29 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72  );.    db_prepar
2030: 65 28 26 73 31 2c 20 22 55 50 44 41 54 45 20 62  e(&s1, "UPDATE b
2040: 6c 6f 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d  lob SET content=
2050: 3a 64 61 74 61 20 57 48 45 52 45 20 72 69 64 3d  :data WHERE rid=
2060: 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 64  %d", rid);.    d
2070: 62 5f 70 72 65 70 61 72 65 28 26 73 32 2c 20 22  b_prepare(&s2, "
2080: 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 64 65 6c  REPLACE INTO del
2090: 74 61 28 72 69 64 2c 73 72 63 69 64 29 56 41 4c  ta(rid,srcid)VAL
20a0: 55 45 53 28 25 64 2c 25 64 29 22 2c 20 72 69 64  UES(%d,%d)", rid
20b0: 2c 20 73 72 63 69 64 29 3b 0a 20 20 20 20 64 62  , srcid);.    db
20c0: 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c 20  _bind_blob(&s1, 
20d0: 22 3a 64 61 74 61 22 2c 20 26 64 65 6c 74 61 29  ":data", &delta)
20e0: 3b 0a 20 20 20 20 64 62 5f 62 65 67 69 6e 5f 74  ;.    db_begin_t
20f0: 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20  ransaction();.  
2100: 20 20 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a    db_exec(&s1);.
2110: 20 20 20 20 64 62 5f 65 78 65 63 28 26 73 32 29      db_exec(&s2)
2120: 3b 0a 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61  ;.    db_end_tra
2130: 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20  nsaction(0);.   
2140: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31   db_finalize(&s1
2150: 29 3b 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69  );.    db_finali
2160: 7a 65 28 26 73 32 29 3b 0a 20 20 20 20 76 65 72  ze(&s2);.    ver
2170: 69 66 79 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d 69  ify_before_commi
2180: 74 28 72 69 64 29 3b 0a 20 20 7d 0a 20 20 62 6c  t(rid);.  }.  bl
2190: 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29 3b 0a  ob_reset(&src);.
21a0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 61    blob_reset(&da
21b0: 74 61 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65  ta);.  blob_rese
21c0: 74 28 26 64 65 6c 74 61 29 3b 0a 7d 0a 0a 2f 2a  t(&delta);.}../*
21d0: 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65  .** COMMAND:  te
21e0: 73 74 2d 63 6f 6e 74 65 6e 74 2d 64 65 6c 74 69  st-content-delti
21f0: 66 79 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  fy.**.** Convert
2200: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20   the content at 
2210: 52 49 44 20 69 6e 74 6f 20 61 20 64 65 6c 74 61  RID into a delta
2220: 20 66 72 6f 6d 20 53 52 43 49 44 2e 0a 2a 2f 0a   from SRCID..*/.
2230: 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e  void test_conten
2240: 74 5f 64 65 6c 74 69 66 79 5f 63 6d 64 28 76 6f  t_deltify_cmd(vo
2250: 69 64 29 7b 0a 20 20 69 66 28 20 67 2e 61 72 67  id){.  if( g.arg
2260: 63 21 3d 35 20 29 20 75 73 61 67 65 28 22 52 49  c!=5 ) usage("RI
2270: 44 20 53 52 43 49 44 20 46 4f 52 43 45 22 29 3b  D SRCID FORCE");
2280: 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69  .  db_must_be_wi
2290: 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 63  thin_tree();.  c
22a0: 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 28 61  ontent_deltify(a
22b0: 74 6f 69 28 67 2e 61 72 67 76 5b 32 5d 29 2c 20  toi(g.argv[2]), 
22c0: 61 74 6f 69 28 67 2e 61 72 67 76 5b 33 5d 29 2c  atoi(g.argv[3]),
22d0: 20 61 74 6f 69 28 67 2e 61 72 67 76 5b 34 5d 29   atoi(g.argv[4])
22e0: 29 3b 0a 7d 0a                                   );.}.