Hex Artifact Content
Not logged in

Artifact 7cc68e90adfbd1a93723419beda501457d0425db:

File src/content.c part of check-in [8010bb41e1] - Speed enhancement in the findSrcid() routine of content.c. Allow 5 digit numbers on counts while syncing. by drh on 2008-03-08 18:59:00.

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 4d 61 63 72 6f 73 20 66  ../*.** Macros f
03f0: 6f 72 20 64 65 62 75 67 67 69 6e 67 0a 2a 2f 0a  or debugging.*/.
0400: 23 69 66 20 30 0a 23 20 64 65 66 69 6e 65 20 43  #if 0.# define C
0410: 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28 58 29 20  ONTENT_TRACE(X) 
0420: 20 70 72 69 6e 74 66 20 58 3b 0a 23 65 6c 73 65   printf X;.#else
0430: 0a 23 20 64 65 66 69 6e 65 20 43 4f 4e 54 45 4e  .# define CONTEN
0440: 54 5f 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69  T_TRACE(X).#endi
0450: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 74  f../*.** The art
0460: 69 66 61 63 74 20 72 65 74 72 69 76 61 6c 20 63  ifact retrival c
0470: 61 63 68 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ache.*/.#define 
0480: 4d 58 5f 43 41 43 48 45 5f 43 4e 54 20 20 35 30  MX_CACHE_CNT  50
0490: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e      /* Maximum n
04a0: 75 6d 62 65 72 20 6f 66 20 70 6f 73 69 74 69 76  umber of positiv
04b0: 65 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 20  e cache entries 
04c0: 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 45 4c  */.#define EXPEL
04d0: 4c 5f 49 4e 54 45 52 56 41 4c 20 35 20 20 20 2f  L_INTERVAL 5   /
04e0: 2a 20 48 6f 77 20 6f 66 74 65 6e 20 74 6f 20 65  * How often to e
04f0: 78 70 65 6c 6c 20 66 72 6f 6d 20 61 20 66 75 6c  xpell from a ful
0500: 6c 20 63 61 63 68 65 20 2a 2f 0a 73 74 61 74 69  l cache */.stati
0510: 63 20 73 74 72 75 63 74 20 7b 0a 20 20 69 6e 74  c struct {.  int
0520: 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   n;             
0530: 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d    /* Current num
0540: 62 65 72 20 6f 66 20 70 6f 73 69 74 69 76 65 20  ber of positive 
0550: 63 61 63 68 65 20 65 6e 74 72 69 65 73 20 2a 2f  cache entries */
0560: 0a 20 20 69 6e 74 20 6e 65 78 74 41 67 65 3b 20  .  int nextAge; 
0570: 20 20 20 20 20 20 20 20 2f 2a 20 41 67 65 20 63          /* Age c
0580: 6f 75 6e 74 65 72 20 66 6f 72 20 69 6d 70 6c 65  ounter for imple
0590: 6d 65 6e 74 69 6e 67 20 4c 52 55 20 2a 2f 0a 20  menting LRU */. 
05a0: 20 69 6e 74 20 73 6b 69 70 43 6e 74 3b 20 20 20   int skipCnt;   
05b0: 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
05c0: 20 6c 69 6d 69 74 20 65 6e 74 72 69 65 73 20 65   limit entries e
05d0: 78 70 65 6c 6c 65 64 20 66 72 6f 6d 20 63 61 63  xpelled from cac
05e0: 68 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 7b  he */.  struct {
05f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
0600: 4f 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  One instance of 
0610: 74 68 69 73 20 66 6f 72 20 65 61 63 68 20 63 61  this for each ca
0620: 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20  che entry */.   
0630: 20 69 6e 74 20 72 69 64 3b 20 20 20 20 20 20 20   int rid;       
0640: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
0650: 74 69 66 61 63 74 20 69 64 20 2a 2f 0a 20 20 20  tifact id */.   
0660: 20 69 6e 74 20 61 67 65 3b 20 20 20 20 20 20 20   int age;       
0670: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67             /* Ag
0680: 65 2e 20 20 4e 65 77 65 72 20 69 73 20 6c 61 72  e.  Newer is lar
0690: 67 65 72 20 2a 2f 0a 20 20 20 20 42 6c 6f 62 20  ger */.    Blob 
06a0: 63 6f 6e 74 65 6e 74 3b 20 20 20 20 20 20 20 20  content;        
06b0: 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20       /* Content 
06c0: 6f 66 20 74 68 65 20 61 72 74 69 66 61 63 74 20  of the artifact 
06d0: 2a 2f 0a 20 20 7d 20 61 5b 4d 58 5f 43 41 43 48  */.  } a[MX_CACH
06e0: 45 5f 43 4e 54 5d 3b 20 20 20 2f 2a 20 54 68 65  E_CNT];   /* The
06f0: 20 70 6f 73 69 74 69 76 65 20 63 61 63 68 65 20   positive cache 
0700: 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  */..  /*.  ** Th
0710: 65 20 6d 69 73 73 69 6e 67 20 61 72 74 69 66 61  e missing artifa
0720: 63 74 20 63 61 63 68 65 2e 0a 20 20 2a 2a 0a 20  ct cache..  **. 
0730: 20 2a 2a 20 41 72 74 69 66 61 63 74 73 20 77 68   ** Artifacts wh
0740: 6f 73 65 20 72 65 63 6f 72 64 20 49 44 20 61 72  ose record ID ar
0750: 65 20 69 6e 20 6d 69 73 73 69 6e 67 43 61 63 68  e in missingCach
0760: 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 74 72  e cannot be retr
0770: 69 65 76 65 64 0a 20 20 2a 2a 20 65 69 74 68 65  ieved.  ** eithe
0780: 72 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61  r because they a
0790: 72 65 20 70 68 61 6e 74 6f 6d 73 20 6f 72 20 62  re phantoms or b
07a0: 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20  ecause they are 
07b0: 61 20 64 65 6c 74 61 20 74 68 61 74 0a 20 20 2a  a delta that.  *
07c0: 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 61 20 70  * depends on a p
07d0: 68 61 6e 74 6f 6d 2e 20 20 41 72 74 69 66 61 63  hantom.  Artifac
07e0: 74 73 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74  ts whose content
07f0: 20 77 65 20 61 72 65 20 63 65 72 74 61 69 6e 20   we are certain 
0800: 69 73 0a 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c  is.  ** availabl
0810: 65 20 61 72 65 20 69 6e 20 61 76 61 69 6c 61 62  e are in availab
0820: 6c 65 43 61 63 68 65 2e 20 20 49 66 20 61 6e 20  leCache.  If an 
0830: 61 72 74 69 66 61 63 74 20 69 73 20 69 6e 20 6e  artifact is in n
0840: 65 69 74 68 65 72 20 63 61 63 68 65 0a 20 20 2a  either cache.  *
0850: 2a 20 74 68 65 6e 20 69 74 73 20 63 75 72 72 65  * then its curre
0860: 6e 74 20 61 76 61 69 6c 61 62 6c 69 74 79 20 69  nt availablity i
0870: 73 20 75 6e 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a  s unknown..  */.
0880: 20 20 42 61 67 20 6d 69 73 73 69 6e 67 3b 20 20    Bag missing;  
0890: 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20         /* Cache 
08a0: 6f 66 20 61 72 74 69 66 61 63 74 73 20 74 68 61  of artifacts tha
08b0: 74 20 61 72 65 20 69 6e 63 6f 6d 70 6c 65 74 65  t are incomplete
08c0: 20 2a 2f 0a 20 20 42 61 67 20 61 76 61 69 6c 61   */.  Bag availa
08d0: 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 43 61  ble;       /* Ca
08e0: 63 68 65 20 6f 66 20 61 72 74 69 66 61 63 74 73  che of artifacts
08f0: 20 74 68 61 74 20 61 72 65 20 63 6f 6d 70 6c 65   that are comple
0900: 74 65 20 2a 2f 0a 7d 20 63 6f 6e 74 65 6e 74 43  te */.} contentC
0910: 61 63 68 65 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c  ache;.../*.** Cl
0920: 65 61 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ear the content 
0930: 63 61 63 68 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63  cache..*/.void c
0940: 6f 6e 74 65 6e 74 5f 63 6c 65 61 72 5f 63 61 63  ontent_clear_cac
0950: 68 65 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20  he(void){.  int 
0960: 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
0970: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 3b 20  contentCache.n; 
0980: 69 2b 2b 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 72  i++){.    blob_r
0990: 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 43 61 63  eset(&contentCac
09a0: 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74 65 6e 74 29  he.a[i].content)
09b0: 3b 0a 20 20 7d 0a 20 20 62 61 67 5f 63 6c 65 61  ;.  }.  bag_clea
09c0: 72 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e  r(&contentCache.
09d0: 6d 69 73 73 69 6e 67 29 3b 0a 20 20 62 61 67 5f  missing);.  bag_
09e0: 63 6c 65 61 72 28 26 63 6f 6e 74 65 6e 74 43 61  clear(&contentCa
09f0: 63 68 65 2e 61 76 61 69 6c 61 62 6c 65 29 3b 0a  che.available);.
0a00: 20 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e    contentCache.n
0a10: 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   = 0;.}../*.** R
0a20: 65 74 75 72 6e 20 74 68 65 20 73 72 63 69 64 20  eturn the srcid 
0a30: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
0a40: 72 69 64 2e 20 20 4f 72 20 72 65 74 75 72 6e 20  rid.  Or return 
0a50: 30 20 69 66 20 72 69 64 20 69 73 20 0a 2a 2a 20  0 if rid is .** 
0a60: 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74  original content
0a70: 20 61 6e 64 20 6e 6f 74 20 61 20 64 65 6c 74 61   and not a delta
0a80: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
0a90: 66 69 6e 64 53 72 63 69 64 28 69 6e 74 20 72 69  findSrcid(int ri
0aa0: 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53 74 6d  d){.  static Stm
0ab0: 74 20 71 3b 0a 20 20 69 6e 74 20 73 72 63 69 64  t q;.  int srcid
0ac0: 3b 0a 20 20 64 62 5f 73 74 61 74 69 63 5f 70 72  ;.  db_static_pr
0ad0: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
0ae0: 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 6c  T srcid FROM del
0af0: 74 61 20 57 48 45 52 45 20 72 69 64 3d 3a 72 69  ta WHERE rid=:ri
0b00: 64 22 29 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 69  d");.  db_bind_i
0b10: 6e 74 28 26 71 2c 20 22 3a 72 69 64 22 2c 20 72  nt(&q, ":rid", r
0b20: 69 64 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74  id);.  if( db_st
0b30: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(&q)==SQLITE_R
0b40: 4f 57 20 29 7b 0a 20 20 20 20 73 72 63 69 64 20  OW ){.    srcid 
0b50: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  = db_column_int(
0b60: 26 71 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b  &q, 0);.  }else{
0b70: 0a 20 20 20 20 73 72 63 69 64 20 3d 20 30 3b 0a  .    srcid = 0;.
0b80: 20 20 7d 0a 20 20 64 62 5f 72 65 73 65 74 28 26    }.  db_reset(&
0b90: 71 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 72 63  q);.  return src
0ba0: 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  id;.}../*.** Che
0bb0: 63 6b 20 74 6f 20 73 65 65 20 69 66 20 63 6f 6e  ck to see if con
0bc0: 74 65 6e 74 20 69 73 20 61 76 61 69 6c 61 62 6c  tent is availabl
0bd0: 65 20 66 6f 72 20 61 72 74 69 66 61 63 74 20 22  e for artifact "
0be0: 72 69 64 22 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  rid".  Return.**
0bf0: 20 74 72 75 65 20 69 66 20 69 74 20 69 73 2e 20   true if it is. 
0c00: 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 69 66   Return false if
0c10: 20 72 69 64 20 69 73 20 61 20 70 68 61 6e 74 6f   rid is a phanto
0c20: 6d 20 6f 72 20 64 65 70 65 6e 64 73 20 6f 6e 0a  m or depends on.
0c30: 2a 2a 20 61 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2f  ** a phantom..*/
0c40: 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 69 73 5f  .int content_is_
0c50: 61 76 61 69 6c 61 62 6c 65 28 69 6e 74 20 72 69  available(int ri
0c60: 64 29 7b 0a 20 20 69 6e 74 20 73 72 63 69 64 3b  d){.  int srcid;
0c70: 0a 20 20 69 66 28 20 62 61 67 5f 66 69 6e 64 28  .  if( bag_find(
0c80: 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69  &contentCache.mi
0c90: 73 73 69 6e 67 2c 20 72 69 64 29 20 29 7b 0a 20  ssing, rid) ){. 
0ca0: 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
0cb0: 0a 20 20 69 66 28 20 62 61 67 5f 66 69 6e 64 28  .  if( bag_find(
0cc0: 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 76  &contentCache.av
0cd0: 61 69 6c 61 62 6c 65 2c 20 72 69 64 29 20 29 7b  ailable, rid) ){
0ce0: 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
0cf0: 20 7d 0a 20 20 69 66 28 20 64 62 5f 69 6e 74 28   }.  if( db_int(
0d00: 2d 31 2c 20 22 53 45 4c 45 43 54 20 73 69 7a 65  -1, "SELECT size
0d10: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45   FROM blob WHERE
0d20: 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3c 30   rid=%d", rid)<0
0d30: 20 29 7b 0a 20 20 20 20 62 61 67 5f 69 6e 73 65   ){.    bag_inse
0d40: 72 74 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65  rt(&contentCache
0d50: 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64 29 3b 0a  .missing, rid);.
0d60: 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
0d70: 7d 0a 20 20 73 72 63 69 64 20 3d 20 66 69 6e 64  }.  srcid = find
0d80: 53 72 63 69 64 28 72 69 64 29 3b 0a 20 20 69 66  Srcid(rid);.  if
0d90: 28 20 73 72 63 69 64 3d 3d 30 20 29 7b 0a 20 20  ( srcid==0 ){.  
0da0: 20 20 62 61 67 5f 69 6e 73 65 72 74 28 26 63 6f    bag_insert(&co
0db0: 6e 74 65 6e 74 43 61 63 68 65 2e 61 76 61 69 6c  ntentCache.avail
0dc0: 61 62 6c 65 2c 20 72 69 64 29 3b 0a 20 20 20 20  able, rid);.    
0dd0: 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
0de0: 69 66 28 20 63 6f 6e 74 65 6e 74 5f 69 73 5f 61  if( content_is_a
0df0: 76 61 69 6c 61 62 6c 65 28 73 72 63 69 64 29 20  vailable(srcid) 
0e00: 29 7b 0a 20 20 20 20 62 61 67 5f 69 6e 73 65 72  ){.    bag_inser
0e10: 74 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e  t(&contentCache.
0e20: 61 76 61 69 6c 61 62 6c 65 2c 20 72 69 64 29 3b  available, rid);
0e30: 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
0e40: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 61 67 5f   }else{.    bag_
0e50: 69 6e 73 65 72 74 28 26 63 6f 6e 74 65 6e 74 43  insert(&contentC
0e60: 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69  ache.missing, ri
0e70: 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  d);.    return 0
0e80: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
0e90: 61 72 6b 20 61 72 74 69 66 61 63 74 20 72 69 64  ark artifact rid
0ea0: 20 61 73 20 62 65 69 6e 67 20 61 76 61 69 6c 61   as being availa
0eb0: 62 6c 65 20 6e 6f 77 2e 20 20 55 70 64 61 74 65  ble now.  Update
0ec0: 20 74 68 65 20 63 61 63 68 65 20 74 6f 0a 2a 2a   the cache to.**
0ed0: 20 73 68 6f 77 20 74 68 61 74 20 65 76 65 72 79   show that every
0ee0: 74 68 69 6e 67 20 74 68 61 74 20 77 61 73 20 66  thing that was f
0ef0: 6f 72 6d 65 72 6c 79 20 75 6e 61 76 61 69 6c 61  ormerly unavaila
0f00: 62 6c 65 20 62 65 63 61 75 73 65 20 72 69 64 0a  ble because rid.
0f10: 2a 2a 20 77 61 73 20 6d 69 73 73 69 6e 67 20 69  ** was missing i
0f20: 73 20 6e 6f 77 20 61 76 61 69 6c 61 62 6c 65 2e  s now available.
0f30: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
0f40: 63 6f 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61 76 61  content_mark_ava
0f50: 69 6c 61 62 6c 65 28 69 6e 74 20 72 69 64 29 7b  ilable(int rid){
0f60: 0a 20 20 42 61 67 20 70 65 6e 64 69 6e 67 3b 0a  .  Bag pending;.
0f70: 20 20 53 74 6d 74 20 71 3b 0a 20 20 69 66 28 20    Stmt q;.  if( 
0f80: 62 61 67 5f 66 69 6e 64 28 26 63 6f 6e 74 65 6e  bag_find(&conten
0f90: 74 43 61 63 68 65 2e 61 76 61 69 6c 61 62 6c 65  tCache.available
0fa0: 2c 20 72 69 64 29 20 29 20 72 65 74 75 72 6e 3b  , rid) ) return;
0fb0: 0a 20 20 62 61 67 5f 69 6e 69 74 28 26 70 65 6e  .  bag_init(&pen
0fc0: 64 69 6e 67 29 3b 0a 20 20 62 61 67 5f 69 6e 73  ding);.  bag_ins
0fd0: 65 72 74 28 26 70 65 6e 64 69 6e 67 2c 20 72 69  ert(&pending, ri
0fe0: 64 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 72 69  d);.  while( (ri
0ff0: 64 20 3d 20 62 61 67 5f 66 69 72 73 74 28 26 70  d = bag_first(&p
1000: 65 6e 64 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20  ending))!=0 ){. 
1010: 20 20 20 62 61 67 5f 72 65 6d 6f 76 65 28 26 70     bag_remove(&p
1020: 65 6e 64 69 6e 67 2c 20 72 69 64 29 3b 0a 20 20  ending, rid);.  
1030: 20 20 62 61 67 5f 72 65 6d 6f 76 65 28 26 63 6f    bag_remove(&co
1040: 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69 73 73 69  ntentCache.missi
1050: 6e 67 2c 20 72 69 64 29 3b 0a 20 20 20 20 62 61  ng, rid);.    ba
1060: 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e 74 65 6e  g_insert(&conten
1070: 74 43 61 63 68 65 2e 61 76 61 69 6c 61 62 6c 65  tCache.available
1080: 2c 20 72 69 64 29 3b 0a 20 20 20 20 64 62 5f 70  , rid);.    db_p
1090: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
10a0: 43 54 20 72 69 64 20 46 52 4f 4d 20 64 65 6c 74  CT rid FROM delt
10b0: 61 20 57 48 45 52 45 20 73 72 63 69 64 3d 25 64  a WHERE srcid=%d
10c0: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 77 68 69  ", rid);.    whi
10d0: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d  le( db_step(&q)=
10e0: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
10f0: 20 20 20 20 20 69 6e 74 20 6e 78 20 3d 20 64 62       int nx = db
1100: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20  _column_int(&q, 
1110: 30 29 3b 0a 20 20 20 20 20 20 62 61 67 5f 69 6e  0);.      bag_in
1120: 73 65 72 74 28 26 70 65 6e 64 69 6e 67 2c 20 6e  sert(&pending, n
1130: 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  x);.    }.    db
1140: 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20  _finalize(&q);. 
1150: 20 7d 0a 20 20 62 61 67 5f 63 6c 65 61 72 28 26   }.  bag_clear(&
1160: 70 65 6e 64 69 6e 67 29 3b 0a 7d 0a 0a 2f 2a 0a  pending);.}../*.
1170: 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 63  ** Extract the c
1180: 6f 6e 74 65 6e 74 20 66 6f 72 20 49 44 20 72 69  ontent for ID ri
1190: 64 20 61 6e 64 20 70 75 74 20 69 74 20 69 6e 74  d and put it int
11a0: 6f 20 74 68 65 0a 2a 2a 20 75 6e 69 6e 69 74 69  o the.** uniniti
11b0: 61 6c 69 7a 65 64 20 62 6c 6f 62 2e 20 20 52 65  alized blob.  Re
11c0: 74 75 72 6e 20 31 20 6f 6e 20 73 75 63 63 65 73  turn 1 on succes
11d0: 73 2e 20 20 49 66 20 74 68 65 20 72 65 63 6f 72  s.  If the recor
11e0: 64 0a 2a 2a 20 69 73 20 61 20 70 68 61 6e 74 6f  d.** is a phanto
11f0: 6d 2c 20 7a 65 72 6f 20 70 42 6c 6f 62 20 61 6e  m, zero pBlob an
1200: 64 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 69  d return 0..*/.i
1210: 6e 74 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 69  nt content_get(i
1220: 6e 74 20 72 69 64 2c 20 42 6c 6f 62 20 2a 70 42  nt rid, Blob *pB
1230: 6c 6f 62 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a  lob){.  Stmt q;.
1240: 20 20 42 6c 6f 62 20 73 72 63 3b 0a 20 20 69 6e    Blob src;.  in
1250: 74 20 73 72 63 69 64 3b 0a 20 20 69 6e 74 20 72  t srcid;.  int r
1260: 63 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a  c = 0;.  int i;.
1270: 20 20 73 74 61 74 69 63 20 42 61 67 20 69 6e 50    static Bag inP
1280: 72 6f 63 65 73 73 3b 0a 0a 20 20 61 73 73 65 72  rocess;..  asser
1290: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  t( g.repositoryO
12a0: 70 65 6e 20 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65  pen );.  blob_ze
12b0: 72 6f 28 70 42 6c 6f 62 29 3b 0a 0a 20 20 2f 2a  ro(pBlob);..  /*
12c0: 20 45 61 72 6c 79 20 6f 75 74 20 69 66 20 77 65   Early out if we
12d0: 20 6b 6e 6f 77 20 74 68 65 20 63 6f 6e 74 65 6e   know the conten
12e0: 74 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  t is not availab
12f0: 6c 65 20 2a 2f 0a 20 20 69 66 28 20 62 61 67 5f  le */.  if( bag_
1300: 66 69 6e 64 28 26 63 6f 6e 74 65 6e 74 43 61 63  find(&contentCac
1310: 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64 29  he.missing, rid)
1320: 20 29 7b 0a 20 20 20 20 43 4f 4e 54 45 4e 54 5f   ){.    CONTENT_
1330: 54 52 41 43 45 28 28 22 25 2a 73 6d 69 73 73 20  TRACE(("%*smiss 
1340: 66 72 6f 6d 20 63 61 63 68 65 3a 20 25 64 5c 6e  from cache: %d\n
1350: 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
1360: 20 20 20 20 20 20 20 62 61 67 5f 63 6f 75 6e 74         bag_count
1370: 28 26 69 6e 50 72 6f 63 65 73 73 29 2c 20 22 22  (&inProcess), ""
1380: 2c 20 72 69 64 29 29 0a 20 20 20 20 72 65 74 75  , rid)).    retu
1390: 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
13a0: 4c 6f 6f 6b 20 66 6f 72 20 74 68 65 20 61 72 74  Look for the art
13b0: 69 66 61 63 74 20 69 6e 20 74 68 65 20 63 61 63  ifact in the cac
13c0: 68 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 66 6f  he first */.  fo
13d0: 72 28 69 3d 30 3b 20 69 3c 63 6f 6e 74 65 6e 74  r(i=0; i<content
13e0: 43 61 63 68 65 2e 6e 3b 20 69 2b 2b 29 7b 0a 20  Cache.n; i++){. 
13f0: 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 43 61     if( contentCa
1400: 63 68 65 2e 61 5b 69 5d 2e 72 69 64 3d 3d 72 69  che.a[i].rid==ri
1410: 64 20 29 7b 0a 20 20 20 20 20 20 2a 70 42 6c 6f  d ){.      *pBlo
1420: 62 20 3d 20 63 6f 6e 74 65 6e 74 43 61 63 68 65  b = contentCache
1430: 2e 61 5b 69 5d 2e 63 6f 6e 74 65 6e 74 3b 0a 20  .a[i].content;. 
1440: 20 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26       blob_zero(&
1450: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69  contentCache.a[i
1460: 5d 2e 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20  ].content);.    
1470: 20 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e    contentCache.n
1480: 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3c  --;.      if( i<
1490: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 20 29  contentCache.n )
14a0: 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e  {.        conten
14b0: 74 43 61 63 68 65 2e 61 5b 69 5d 20 3d 20 63 6f  tCache.a[i] = co
14c0: 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 63 6f 6e  ntentCache.a[con
14d0: 74 65 6e 74 43 61 63 68 65 2e 6e 5d 3b 0a 20 20  tentCache.n];.  
14e0: 20 20 20 20 7d 0a 20 20 20 20 20 20 43 4f 4e 54      }.      CONT
14f0: 45 4e 54 5f 54 52 41 43 45 28 28 22 25 2a 73 68  ENT_TRACE(("%*sh
1500: 69 74 20 63 61 63 68 65 3a 20 25 64 5c 6e 22 2c  it cache: %d\n",
1510: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1520: 20 20 20 20 20 20 62 61 67 5f 63 6f 75 6e 74 28        bag_count(
1530: 26 69 6e 50 72 6f 63 65 73 73 29 2c 20 22 22 2c  &inProcess), "",
1540: 20 72 69 64 29 29 0a 20 20 20 20 20 20 72 65 74   rid)).      ret
1550: 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
1560: 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65  ..  /* See if we
1570: 20 6e 65 65 64 20 74 6f 20 61 70 70 6c 79 20 61   need to apply a
1580: 20 64 65 6c 74 61 20 74 6f 20 66 69 6e 64 20 74   delta to find t
1590: 68 69 73 20 61 72 74 69 66 61 63 74 20 2a 2f 0a  his artifact */.
15a0: 20 20 73 72 63 69 64 20 3d 20 66 69 6e 64 53 72    srcid = findSr
15b0: 63 69 64 28 72 69 64 29 3b 0a 20 20 43 4f 4e 54  cid(rid);.  CONT
15c0: 45 4e 54 5f 54 52 41 43 45 28 28 22 25 2a 73 73  ENT_TRACE(("%*ss
15d0: 65 61 72 63 68 69 6e 67 20 66 6f 72 20 25 64 2e  earching for %d.
15e0: 20 20 4e 65 65 64 20 25 64 2e 5c 6e 22 2c 0a 20    Need %d.\n",. 
15f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1600: 62 61 67 5f 63 6f 75 6e 74 28 26 69 6e 50 72 6f  bag_count(&inPro
1610: 63 65 73 73 29 2c 20 22 22 2c 20 72 69 64 2c 20  cess), "", rid, 
1620: 73 72 63 69 64 29 29 0a 0a 0a 20 20 69 66 28 20  srcid))...  if( 
1630: 73 72 63 69 64 20 29 7b 0a 20 20 20 20 2f 2a 20  srcid ){.    /* 
1640: 59 65 73 2c 20 61 20 64 65 6c 74 61 20 69 73 20  Yes, a delta is 
1650: 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20  required */.    
1660: 69 66 28 20 62 61 67 5f 66 69 6e 64 28 26 69 6e  if( bag_find(&in
1670: 50 72 6f 63 65 73 73 2c 20 73 72 63 69 64 29 20  Process, srcid) 
1680: 29 7b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74  ){.      db_mult
1690: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 20  i_exec(.        
16a0: 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54  "UPDATE blob SET
16b0: 20 63 6f 6e 74 65 6e 74 3d 4e 55 4c 4c 2c 20 73   content=NULL, s
16c0: 69 7a 65 3d 2d 31 20 57 48 45 52 45 20 72 69 64  ize=-1 WHERE rid
16d0: 3d 25 64 3b 22 0a 20 20 20 20 20 20 20 20 22 44  =%d;".        "D
16e0: 45 4c 45 54 45 20 46 52 4f 4d 20 64 65 6c 74 61  ELETE FROM delta
16f0: 20 57 48 45 52 45 20 72 69 64 3d 25 64 3b 22 0a   WHERE rid=%d;".
1700: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
1710: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70  OR IGNORE INTO p
1720: 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53 28 25 64  hantom VALUES(%d
1730: 29 3b 22 2c 0a 20 20 20 20 20 20 20 20 73 72 63  );",.        src
1740: 69 64 2c 20 73 72 63 69 64 2c 20 73 72 63 69 64  id, srcid, srcid
1750: 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
1760: 62 6c 6f 62 5f 7a 65 72 6f 28 70 42 6c 6f 62 29  blob_zero(pBlob)
1770: 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
1780: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 61 67 5f  ;.    }.    bag_
1790: 69 6e 73 65 72 74 28 26 69 6e 50 72 6f 63 65 73  insert(&inProces
17a0: 73 2c 20 73 72 63 69 64 29 3b 0a 0a 20 20 20 20  s, srcid);..    
17b0: 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28  if( content_get(
17c0: 73 72 63 69 64 2c 20 26 73 72 63 29 20 29 7b 0a  srcid, &src) ){.
17d0: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65        db_prepare
17e0: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e  (&q, "SELECT con
17f0: 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57  tent FROM blob W
1800: 48 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20  HERE rid=%d AND 
1810: 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a  size>=0", rid);.
1820: 20 20 20 20 20 20 69 66 28 20 64 62 5f 73 74 65        if( db_ste
1830: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
1840: 57 20 29 7b 0a 20 20 20 20 20 20 20 20 42 6c 6f  W ){.        Blo
1850: 62 20 64 65 6c 74 61 3b 0a 20 20 20 20 20 20 20  b delta;.       
1860: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c   db_ephemeral_bl
1870: 6f 62 28 26 71 2c 20 30 2c 20 26 64 65 6c 74 61  ob(&q, 0, &delta
1880: 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  );.        blob_
1890: 75 6e 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74  uncompress(&delt
18a0: 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  a, &delta);.    
18b0: 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 70 42      blob_init(pB
18c0: 6c 6f 62 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  lob,0,0);.      
18d0: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70    blob_delta_app
18e0: 6c 79 28 26 73 72 63 2c 20 26 64 65 6c 74 61 2c  ly(&src, &delta,
18f0: 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 20   pBlob);.       
1900: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c   blob_reset(&del
1910: 74 61 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ta);.        rc 
1920: 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
1930: 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26     db_finalize(&
1940: 71 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 61  q);..      /* Sa
1950: 76 65 20 74 68 65 20 73 72 63 69 64 20 61 72 74  ve the srcid art
1960: 69 66 61 63 74 20 69 6e 20 74 68 65 20 63 61 63  ifact in the cac
1970: 68 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  he */.      if( 
1980: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 3c 4d  contentCache.n<M
1990: 58 5f 43 41 43 48 45 5f 43 4e 54 20 29 7b 0a 20  X_CACHE_CNT ){. 
19a0: 20 20 20 20 20 20 20 69 20 3d 20 63 6f 6e 74 65         i = conte
19b0: 6e 74 43 61 63 68 65 2e 6e 2b 2b 3b 0a 20 20 20  ntCache.n++;.   
19c0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 28 63     }else if( ((c
19d0: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 73 6b 69 70  ontentCache.skip
19e0: 43 6e 74 2b 2b 29 25 45 58 50 45 4c 4c 5f 49 4e  Cnt++)%EXPELL_IN
19f0: 54 45 52 56 41 4c 29 21 3d 30 20 29 7b 0a 20 20  TERVAL)!=0 ){.  
1a00: 20 20 20 20 20 20 69 20 3d 20 2d 31 3b 0a 20 20        i = -1;.  
1a10: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a20: 20 20 20 69 6e 74 20 6a 2c 20 62 65 73 74 3b 0a     int j, best;.
1a30: 20 20 20 20 20 20 20 20 62 65 73 74 20 3d 20 63          best = c
1a40: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 65 78 74  ontentCache.next
1a50: 41 67 65 2b 31 3b 0a 20 20 20 20 20 20 20 20 69  Age+1;.        i
1a60: 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 66   = -1;.        f
1a70: 6f 72 28 6a 3d 30 3b 20 6a 3c 63 6f 6e 74 65 6e  or(j=0; j<conten
1a80: 74 43 61 63 68 65 2e 6e 3b 20 6a 2b 2b 29 7b 0a  tCache.n; j++){.
1a90: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6f            if( co
1aa0: 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 6a 5d 2e  ntentCache.a[j].
1ab0: 61 67 65 3c 62 65 73 74 20 29 7b 0a 20 20 20 20  age<best ){.    
1ac0: 20 20 20 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20          i = j;. 
1ad0: 20 20 20 20 20 20 20 20 20 20 20 62 65 73 74 20             best 
1ae0: 3d 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61  = contentCache.a
1af0: 5b 6a 5d 2e 61 67 65 3b 0a 20 20 20 20 20 20 20  [j].age;.       
1b00: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1b10: 20 20 20 20 20 20 20 43 4f 4e 54 45 4e 54 5f 54         CONTENT_T
1b20: 52 41 43 45 28 28 22 25 2a 73 65 78 70 65 6c 6c  RACE(("%*sexpell
1b30: 20 25 64 20 66 72 6f 6d 20 63 61 63 68 65 5c 6e   %d from cache\n
1b40: 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
1b50: 20 20 20 20 20 20 20 20 20 20 62 61 67 5f 63 6f            bag_co
1b60: 75 6e 74 28 26 69 6e 50 72 6f 63 65 73 73 29 2c  unt(&inProcess),
1b70: 20 22 22 2c 20 63 6f 6e 74 65 6e 74 43 61 63 68   "", contentCach
1b80: 65 2e 61 5b 69 5d 2e 72 69 64 29 29 0a 20 20 20  e.a[i].rid)).   
1b90: 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28       blob_reset(
1ba0: 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b  &contentCache.a[
1bb0: 69 5d 2e 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20  i].content);.   
1bc0: 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
1bd0: 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  >=0 ){.        c
1be0: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d  ontentCache.a[i]
1bf0: 2e 63 6f 6e 74 65 6e 74 20 3d 20 73 72 63 3b 0a  .content = src;.
1c00: 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 43          contentC
1c10: 61 63 68 65 2e 61 5b 69 5d 2e 61 67 65 20 3d 20  ache.a[i].age = 
1c20: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 65 78  contentCache.nex
1c30: 74 41 67 65 2b 2b 3b 0a 20 20 20 20 20 20 20 20  tAge++;.        
1c40: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69  contentCache.a[i
1c50: 5d 2e 72 69 64 20 3d 20 73 72 63 69 64 3b 0a 20  ].rid = srcid;. 
1c60: 20 20 20 20 20 20 20 43 4f 4e 54 45 4e 54 5f 54         CONTENT_T
1c70: 52 41 43 45 28 28 22 25 2a 73 61 64 64 20 25 64  RACE(("%*sadd %d
1c80: 20 74 6f 20 63 61 63 68 65 5c 6e 22 2c 0a 20 20   to cache\n",.  
1c90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ca0: 20 20 20 20 20 62 61 67 5f 63 6f 75 6e 74 28 26       bag_count(&
1cb0: 69 6e 50 72 6f 63 65 73 73 29 2c 20 22 22 2c 20  inProcess), "", 
1cc0: 73 72 63 69 64 29 29 0a 20 20 20 20 20 20 7d 65  srcid)).      }e
1cd0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f  lse{.        blo
1ce0: 62 5f 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20  b_reset(&src);. 
1cf0: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1d00: 20 62 61 67 5f 72 65 6d 6f 76 65 28 26 69 6e 50   bag_remove(&inP
1d10: 72 6f 63 65 73 73 2c 20 73 72 63 69 64 29 3b 0a  rocess, srcid);.
1d20: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1d30: 4e 6f 20 64 65 6c 74 61 20 72 65 71 75 69 72 65  No delta require
1d40: 64 2e 20 20 52 65 61 64 20 63 6f 6e 74 65 6e 74  d.  Read content
1d50: 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74   directly from t
1d60: 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
1d70: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71     db_prepare(&q
1d80: 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 6e  , "SELECT conten
1d90: 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  t FROM blob WHER
1da0: 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69 7a  E rid=%d AND siz
1db0: 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a 20 20 20  e>=0", rid);.   
1dc0: 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 71 29   if( db_step(&q)
1dd0: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
1de0: 20 20 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72        db_ephemer
1df0: 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20 70  al_blob(&q, 0, p
1e00: 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 62 6c 6f  Blob);.      blo
1e10: 62 5f 75 6e 63 6f 6d 70 72 65 73 73 28 70 42 6c  b_uncompress(pBl
1e20: 6f 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20  ob, pBlob);.    
1e30: 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a    rc = 1;.    }.
1e40: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28      db_finalize(
1e50: 26 71 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  &q);.  }.  if( r
1e60: 63 3d 3d 30 20 29 7b 0a 20 20 20 20 62 61 67 5f  c==0 ){.    bag_
1e70: 69 6e 73 65 72 74 28 26 63 6f 6e 74 65 6e 74 43  insert(&contentC
1e80: 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69  ache.missing, ri
1e90: 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  d);.  }else{.   
1ea0: 20 62 61 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e   bag_insert(&con
1eb0: 74 65 6e 74 43 61 63 68 65 2e 61 76 61 69 6c 61  tentCache.availa
1ec0: 62 6c 65 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 20  ble, rid);.  }. 
1ed0: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1ee0: 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 63 6f 6e  *.** Get the con
1ef0: 74 65 6e 74 73 20 6f 66 20 61 20 66 69 6c 65 20  tents of a file 
1f00: 77 69 74 68 69 6e 20 61 20 67 69 76 65 6e 20 62  within a given b
1f10: 61 73 65 6c 69 6e 65 2e 0a 2a 2f 0a 69 6e 74 20  aseline..*/.int 
1f20: 63 6f 6e 74 65 6e 74 5f 67 65 74 5f 68 69 73 74  content_get_hist
1f30: 6f 72 69 63 61 6c 5f 66 69 6c 65 28 0a 20 20 63  orical_file(.  c
1f40: 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 76 69 73  onst char *revis
1f50: 69 6f 6e 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ion,    /* Name 
1f60: 6f 66 20 74 68 65 20 62 61 73 65 6c 69 6e 65 20  of the baseline 
1f70: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 66  containing the f
1f80: 69 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ile */.  const c
1f90: 68 61 72 20 2a 66 69 6c 65 2c 20 20 20 20 20 20  har *file,      
1fa0: 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1fb0: 20 66 69 6c 65 20 2a 2f 0a 20 20 42 6c 6f 62 20   file */.  Blob 
1fc0: 2a 63 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 20  *content        
1fd0: 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66 69 6c      /* Write fil
1fe0: 65 20 63 6f 6e 74 65 6e 74 20 68 65 72 65 20 2a  e content here *
1ff0: 2f 0a 29 7b 0a 20 20 42 6c 6f 62 20 6d 66 69 6c  /.){.  Blob mfil
2000: 65 3b 0a 20 20 4d 61 6e 69 66 65 73 74 20 6d 3b  e;.  Manifest m;
2010: 0a 20 20 69 6e 74 20 69 2c 20 72 69 64 3d 30 3b  .  int i, rid=0;
2020: 0a 20 20 0a 20 20 72 69 64 20 3d 20 6e 61 6d 65  .  .  rid = name
2030: 5f 74 6f 5f 72 69 64 28 72 65 76 69 73 69 6f 6e  _to_rid(revision
2040: 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74  );.  content_get
2050: 28 72 69 64 2c 20 26 6d 66 69 6c 65 29 3b 0a 20  (rid, &mfile);. 
2060: 20 0a 20 20 69 66 28 20 6d 61 6e 69 66 65 73 74   .  if( manifest
2070: 5f 70 61 72 73 65 28 26 6d 2c 20 26 6d 66 69 6c  _parse(&m, &mfil
2080: 65 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  e) ){.    for(i=
2090: 30 3b 20 69 3c 6d 2e 6e 46 69 6c 65 3b 20 69 2b  0; i<m.nFile; i+
20a0: 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74  +){.      if( st
20b0: 72 63 6d 70 28 6d 2e 61 46 69 6c 65 5b 69 5d 2e  rcmp(m.aFile[i].
20c0: 7a 4e 61 6d 65 2c 20 66 69 6c 65 29 3d 3d 30 20  zName, file)==0 
20d0: 29 7b 0a 20 20 20 20 20 20 20 20 72 69 64 20 3d  ){.        rid =
20e0: 20 75 75 69 64 5f 74 6f 5f 72 69 64 28 6d 2e 61   uuid_to_rid(m.a
20f0: 46 69 6c 65 5b 69 5d 2e 7a 55 75 69 64 2c 20 30  File[i].zUuid, 0
2100: 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
2110: 6e 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69  n content_get(ri
2120: 64 2c 20 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20  d, content);.   
2130: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66     }.    }.    f
2140: 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 66 69 6c  ossil_panic("fil
2150: 65 3a 20 25 73 20 64 6f 65 73 20 6e 6f 74 20 65  e: %s does not e
2160: 78 69 73 74 20 69 6e 20 72 65 76 69 73 69 6f 6e  xist in revision
2170: 3a 20 25 73 22 2c 20 66 69 6c 65 2c 20 72 65 76  : %s", file, rev
2180: 69 73 69 6f 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b  ision);.  }else{
2190: 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69  .    fossil_pani
21a0: 63 28 22 63 6f 75 6c 64 20 6e 6f 74 20 70 61 72  c("could not par
21b0: 73 65 20 6d 61 6e 69 66 65 73 74 20 66 6f 72 20  se manifest for 
21c0: 72 65 76 69 73 69 6f 6e 3a 20 25 73 22 2c 20 72  revision: %s", r
21d0: 65 76 69 73 69 6f 6e 29 3b 0a 20 20 7d 0a 20 20  evision);.  }.  
21e0: 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
21f0: 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20  /*.** COMMAND:  
2200: 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 67 65 74  test-content-get
2210: 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61  .**.** Extract a
2220: 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20 64   blob from the d
2230: 61 74 61 62 61 73 65 20 61 6e 64 20 77 72 69 74  atabase and writ
2240: 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c 65  e it into a file
2250: 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63  ..*/.void test_c
2260: 6f 6e 74 65 6e 74 5f 67 65 74 5f 63 6d 64 28 76  ontent_get_cmd(v
2270: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b  oid){.  int rid;
2280: 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b  .  Blob content;
2290: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
22a0: 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e 61 72  File;.  if( g.ar
22b0: 67 63 21 3d 34 20 26 26 20 67 2e 61 72 67 63 21  gc!=4 && g.argc!
22c0: 3d 33 20 29 20 75 73 61 67 65 28 22 52 45 43 4f  =3 ) usage("RECO
22d0: 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45 3f 22  RDID ?FILENAME?"
22e0: 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67 2e 61  );.  zFile = g.a
22f0: 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67 76 5b  rgc==4 ? g.argv[
2300: 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62 5f 6d  3] : "-";.  db_m
2310: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
2320: 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 6e 61  ee();.  rid = na
2330: 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76  me_to_rid(g.argv
2340: 5b 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f  [2]);.  content_
2350: 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e  get(rid, &conten
2360: 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72 69 74 65  t);.  blob_write
2370: 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e 74 65 6e  _to_file(&conten
2380: 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a 0a 2f 2a  t, zFile);.}../*
2390: 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65  .** COMMAND:  te
23a0: 73 74 2d 63 6f 6e 74 65 6e 74 2d 72 61 77 67 65  st-content-rawge
23b0: 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20  t.**.** Extract 
23c0: 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20  a blob from the 
23d0: 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 72 69  database and wri
23e0: 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c  te it into a fil
23f0: 65 2e 20 20 54 68 69 73 0a 2a 2a 20 76 65 72 73  e.  This.** vers
2400: 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 70  ion does not exp
2410: 61 6e 64 20 74 68 65 20 64 65 6c 74 61 2e 0a 2a  and the delta..*
2420: 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74  /.void test_cont
2430: 65 6e 74 5f 72 61 77 67 65 74 5f 63 6d 64 28 76  ent_rawget_cmd(v
2440: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b  oid){.  int rid;
2450: 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b  .  Blob content;
2460: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2470: 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e 61 72  File;.  if( g.ar
2480: 67 63 21 3d 34 20 26 26 20 67 2e 61 72 67 63 21  gc!=4 && g.argc!
2490: 3d 33 20 29 20 75 73 61 67 65 28 22 52 45 43 4f  =3 ) usage("RECO
24a0: 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45 3f 22  RDID ?FILENAME?"
24b0: 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67 2e 61  );.  zFile = g.a
24c0: 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67 76 5b  rgc==4 ? g.argv[
24d0: 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62 5f 6d  3] : "-";.  db_m
24e0: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
24f0: 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 6e 61  ee();.  rid = na
2500: 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76  me_to_rid(g.argv
2510: 5b 32 5d 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  [2]);.  blob_zer
2520: 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 64  o(&content);.  d
2530: 62 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 2c  b_blob(&content,
2540: 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74   "SELECT content
2550: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45   FROM blob WHERE
2560: 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a   rid=%d", rid);.
2570: 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 73    blob_uncompres
2580: 73 28 26 63 6f 6e 74 65 6e 74 2c 20 26 63 6f 6e  s(&content, &con
2590: 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72  tent);.  blob_wr
25a0: 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e  ite_to_file(&con
25b0: 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a  tent, zFile);.}.
25c0: 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 72 65  ./*.** When a re
25d0: 63 6f 72 64 20 69 73 20 63 6f 6e 76 65 72 74 65  cord is converte
25e0: 64 20 66 72 6f 6d 20 61 20 70 68 61 6e 74 6f 6d  d from a phantom
25f0: 20 74 6f 20 61 20 72 65 61 6c 20 72 65 63 6f 72   to a real recor
2600: 64 2c 0a 2a 2a 20 69 66 20 74 68 61 74 20 72 65  d,.** if that re
2610: 63 6f 72 64 20 68 61 73 20 6f 74 68 65 72 20 72  cord has other r
2620: 65 63 6f 72 64 73 20 74 68 61 74 20 61 72 65 20  ecords that are 
2630: 64 65 72 69 76 65 64 20 62 79 20 64 65 6c 74 61  derived by delta
2640: 2c 0a 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20 6d  ,.** then call m
2650: 61 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e  anifest_crosslin
2660: 6b 28 29 20 6f 6e 20 74 68 6f 73 65 20 6f 74 68  k() on those oth
2670: 65 72 20 72 65 63 6f 72 64 73 2e 0a 2a 2f 0a 76  er records..*/.v
2680: 6f 69 64 20 61 66 74 65 72 5f 64 65 70 68 61 6e  oid after_dephan
2690: 74 6f 6d 69 7a 65 28 69 6e 74 20 72 69 64 2c 20  tomize(int rid, 
26a0: 69 6e 74 20 6c 69 6e 6b 46 6c 61 67 29 7b 0a 20  int linkFlag){. 
26b0: 20 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70 72   Stmt q;.  db_pr
26c0: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
26d0: 54 20 72 69 64 20 46 52 4f 4d 20 64 65 6c 74 61  T rid FROM delta
26e0: 20 57 48 45 52 45 20 73 72 63 69 64 3d 25 64 22   WHERE srcid=%d"
26f0: 2c 20 72 69 64 29 3b 0a 20 20 77 68 69 6c 65 28  , rid);.  while(
2700: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51   db_step(&q)==SQ
2710: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
2720: 69 6e 74 20 74 69 64 20 3d 20 64 62 5f 63 6f 6c  int tid = db_col
2730: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a  umn_int(&q, 0);.
2740: 20 20 20 20 61 66 74 65 72 5f 64 65 70 68 61 6e      after_dephan
2750: 74 6f 6d 69 7a 65 28 74 69 64 2c 20 31 29 3b 0a  tomize(tid, 1);.
2760: 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a    }.  db_finaliz
2770: 65 28 26 71 29 3b 0a 20 20 69 66 28 20 6c 69 6e  e(&q);.  if( lin
2780: 6b 46 6c 61 67 20 29 7b 0a 20 20 20 20 42 6c 6f  kFlag ){.    Blo
2790: 62 20 63 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 63  b content;.    c
27a0: 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20  ontent_get(rid, 
27b0: 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 6d  &content);.    m
27c0: 61 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e  anifest_crosslin
27d0: 6b 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29  k(rid, &content)
27e0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
27f0: 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 7d 0a  (&content);.  }.
2800: 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63  }../*.** Write c
2810: 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  ontent into the 
2820: 64 61 74 61 62 61 73 65 2e 20 20 52 65 74 75 72  database.  Retur
2830: 6e 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e  n the record ID.
2840: 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 6f 6e 74    If the.** cont
2850: 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69  ent is already i
2860: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
2870: 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  just return the 
2880: 72 65 63 6f 72 64 20 49 44 2e 0a 2a 2a 0a 2a 2a  record ID..**.**
2890: 20 49 66 20 73 72 63 49 64 20 69 73 20 73 70 65   If srcId is spe
28a0: 63 69 66 69 65 64 2c 20 74 68 65 6e 20 70 42 6c  cified, then pBl
28b0: 6f 62 20 69 73 20 64 65 6c 74 61 20 63 6f 6e 74  ob is delta cont
28c0: 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ent from.** the 
28d0: 73 72 63 49 64 20 72 65 63 6f 72 64 2e 20 20 73  srcId record.  s
28e0: 72 63 49 64 20 6d 69 67 68 74 20 62 65 20 61 20  rcId might be a 
28f0: 70 68 61 6e 74 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41  phantom..**.** A
2900: 20 70 68 61 6e 74 6f 6d 20 69 73 20 77 72 69 74   phantom is writ
2910: 74 65 6e 20 69 66 20 70 42 6c 6f 62 3d 3d 30 2e  ten if pBlob==0.
2920: 20 20 49 66 20 70 42 6c 6f 62 3d 3d 30 20 6f 72    If pBlob==0 or
2930: 20 69 66 20 73 72 63 49 64 20 69 73 0a 2a 2a 20   if srcId is.** 
2940: 73 70 65 63 69 66 69 65 64 20 74 68 65 6e 20 74  specified then t
2950: 68 65 20 55 55 49 44 20 69 73 20 73 65 74 20 74  he UUID is set t
2960: 6f 20 7a 55 75 69 64 2e 20 20 4f 74 68 65 72 77  o zUuid.  Otherw
2970: 69 73 65 20 7a 55 75 69 64 20 69 73 0a 2a 2a 20  ise zUuid is.** 
2980: 69 67 6e 6f 72 65 64 2e 20 20 49 6e 20 74 68 65  ignored.  In the
2990: 20 66 75 74 75 72 65 20 74 68 69 73 20 6d 69 67   future this mig
29a0: 68 74 20 63 68 61 6e 67 65 20 73 75 63 68 20 74  ht change such t
29b0: 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a  hat the content.
29c0: 2a 2a 20 68 61 73 68 20 69 73 20 63 68 65 63 6b  ** hash is check
29d0: 65 64 20 61 67 61 69 6e 73 74 20 7a 55 75 69 64  ed against zUuid
29e0: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74   to make sure it
29f0: 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a   is correct..**.
2a00: 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  ** If the record
2a10: 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
2a20: 62 75 74 20 69 73 20 61 20 70 68 61 6e 74 6f 6d  but is a phantom
2a30: 2c 20 74 68 65 20 70 42 6c 6f 62 20 63 6f 6e 74  , the pBlob cont
2a40: 65 6e 74 0a 2a 2a 20 69 73 20 69 6e 73 65 72 74  ent.** is insert
2a50: 65 64 20 61 6e 64 20 74 68 65 20 70 68 61 74 6f  ed and the phato
2a60: 6d 20 62 65 63 6f 6d 65 73 20 61 20 72 65 61 6c  m becomes a real
2a70: 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 69 6e 74 20   record..*/.int 
2a80: 63 6f 6e 74 65 6e 74 5f 70 75 74 28 42 6c 6f 62  content_put(Blob
2a90: 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 63   *pBlob, const c
2aa0: 68 61 72 20 2a 7a 55 75 69 64 2c 20 69 6e 74 20  har *zUuid, int 
2ab0: 73 72 63 49 64 29 7b 0a 20 20 69 6e 74 20 73 69  srcId){.  int si
2ac0: 7a 65 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20  ze;.  int rid;. 
2ad0: 20 53 74 6d 74 20 73 31 3b 0a 20 20 42 6c 6f 62   Stmt s1;.  Blob
2ae0: 20 63 6d 70 72 3b 0a 20 20 42 6c 6f 62 20 68 61   cmpr;.  Blob ha
2af0: 73 68 3b 0a 20 20 69 6e 74 20 6d 61 72 6b 41 73  sh;.  int markAs
2b00: 55 6e 63 6c 75 73 74 65 72 65 64 20 3d 20 30 3b  Unclustered = 0;
2b10: 0a 20 20 69 6e 74 20 69 73 44 65 70 68 61 6e 74  .  int isDephant
2b20: 6f 6d 69 7a 65 20 3d 20 30 3b 0a 20 20 0a 20 20  omize = 0;.  .  
2b30: 61 73 73 65 72 74 28 20 67 2e 72 65 70 6f 73 69  assert( g.reposi
2b40: 74 6f 72 79 4f 70 65 6e 20 29 3b 0a 20 20 69 66  toryOpen );.  if
2b50: 28 20 70 42 6c 6f 62 20 26 26 20 73 72 63 49 64  ( pBlob && srcId
2b60: 3d 3d 30 20 29 7b 0a 20 20 20 20 73 68 61 31 73  ==0 ){.    sha1s
2b70: 75 6d 5f 62 6c 6f 62 28 70 42 6c 6f 62 2c 20 26  um_blob(pBlob, &
2b80: 68 61 73 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  hash);.  }else{.
2b90: 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 26 68      blob_init(&h
2ba0: 61 73 68 2c 20 7a 55 75 69 64 2c 20 2d 31 29 3b  ash, zUuid, -1);
2bb0: 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f 62  .  }.  if( pBlob
2bc0: 3d 3d 30 20 29 7b 0a 20 20 20 20 73 69 7a 65 20  ==0 ){.    size 
2bd0: 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  = -1;.  }else{. 
2be0: 20 20 20 73 69 7a 65 20 3d 20 62 6c 6f 62 5f 73     size = blob_s
2bf0: 69 7a 65 28 70 42 6c 6f 62 29 3b 0a 20 20 7d 0a  ize(pBlob);.  }.
2c00: 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73    db_begin_trans
2c10: 61 63 74 69 6f 6e 28 29 3b 0a 0a 20 20 2f 2a 20  action();..  /* 
2c20: 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
2c30: 74 68 65 20 65 6e 74 72 79 20 61 6c 72 65 61 64  the entry alread
2c40: 79 20 65 78 69 73 74 73 20 61 6e 64 20 69 66 20  y exists and if 
2c50: 69 74 20 64 6f 65 73 20 77 68 65 74 68 65 72 0a  it does whether.
2c60: 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 74 68 65 20    ** or not the 
2c70: 65 6e 74 72 79 20 69 73 20 61 20 70 68 61 6e 74  entry is a phant
2c80: 6f 6d 0a 20 20 2a 2f 0a 20 20 64 62 5f 70 72 65  om.  */.  db_pre
2c90: 70 61 72 65 28 26 73 31 2c 20 22 53 45 4c 45 43  pare(&s1, "SELEC
2ca0: 54 20 72 69 64 2c 20 73 69 7a 65 20 46 52 4f 4d  T rid, size FROM
2cb0: 20 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64   blob WHERE uuid
2cc0: 3d 25 42 22 2c 20 26 68 61 73 68 29 3b 0a 20 20  =%B", &hash);.  
2cd0: 69 66 28 20 64 62 5f 73 74 65 70 28 26 73 31 29  if( db_step(&s1)
2ce0: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
2cf0: 20 20 20 20 72 69 64 20 3d 20 64 62 5f 63 6f 6c      rid = db_col
2d00: 75 6d 6e 5f 69 6e 74 28 26 73 31 2c 20 30 29 3b  umn_int(&s1, 0);
2d10: 0a 20 20 20 20 69 66 28 20 64 62 5f 63 6f 6c 75  .    if( db_colu
2d20: 6d 6e 5f 69 6e 74 28 26 73 31 2c 20 31 29 3e 3d  mn_int(&s1, 1)>=
2d30: 30 20 7c 7c 20 70 42 6c 6f 62 3d 3d 30 20 29 7b  0 || pBlob==0 ){
2d40: 0a 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72  .      /* Either
2d50: 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 6e 6f   the entry is no
2d60: 74 20 61 20 70 68 61 6e 74 6f 6d 20 6f 72 20 69  t a phantom or i
2d70: 74 20 69 73 20 61 20 70 68 61 6e 74 6f 6d 20 62  t is a phantom b
2d80: 75 74 20 77 65 0a 20 20 20 20 20 20 2a 2a 20 68  ut we.      ** h
2d90: 61 76 65 20 6e 6f 20 64 61 74 61 20 77 69 74 68  ave no data with
2da0: 20 77 68 69 63 68 20 74 6f 20 64 65 70 68 61 6e   which to dephan
2db0: 74 6f 6d 69 7a 65 20 69 74 2e 20 20 49 6e 20 65  tomize it.  In e
2dc0: 69 74 68 65 72 20 63 61 73 65 2c 0a 20 20 20 20  ither case,.    
2dd0: 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f    ** there is no
2de0: 74 68 69 6e 67 20 66 6f 72 20 75 73 20 74 6f 20  thing for us to 
2df0: 64 6f 20 6f 74 68 65 72 20 74 68 61 6e 20 72 65  do other than re
2e00: 74 75 72 6e 20 74 68 65 20 52 49 44 2e 20 2a 2f  turn the RID. */
2e10: 0a 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69  .      db_finali
2e20: 7a 65 28 26 73 31 29 3b 0a 20 20 20 20 20 20 64  ze(&s1);.      d
2e30: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
2e40: 6e 28 30 29 3b 0a 20 20 20 20 20 20 72 65 74 75  n(0);.      retu
2e50: 72 6e 20 72 69 64 3b 0a 20 20 20 20 7d 0a 20 20  rn rid;.    }.  
2e60: 7d 65 6c 73 65 7b 0a 20 20 20 20 72 69 64 20 3d  }else{.    rid =
2e70: 20 30 3b 20 20 2f 2a 20 4e 6f 20 65 6e 74 72 79   0;  /* No entry
2e80: 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 55   with the same U
2e90: 55 49 44 20 63 75 72 72 65 6e 74 6c 79 20 65 78  UID currently ex
2ea0: 69 73 74 73 20 2a 2f 0a 20 20 20 20 6d 61 72 6b  ists */.    mark
2eb0: 41 73 55 6e 63 6c 75 73 74 65 72 65 64 20 3d 20  AsUnclustered = 
2ec0: 31 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  1;.  }.  db_fina
2ed0: 6c 69 7a 65 28 26 73 31 29 3b 0a 0a 20 20 2f 2a  lize(&s1);..  /*
2ee0: 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 63   Construct a rec
2ef0: 65 69 76 65 64 2d 66 72 6f 6d 20 49 44 20 69 66  eived-from ID if
2f00: 20 77 65 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61   we do not alrea
2f10: 64 79 20 68 61 76 65 20 6f 6e 65 20 2a 2f 0a 20  dy have one */. 
2f20: 20 69 66 28 20 67 2e 72 63 76 69 64 3d 3d 30 20   if( g.rcvid==0 
2f30: 26 26 20 70 42 6c 6f 62 21 3d 30 20 29 7b 0a 20  && pBlob!=0 ){. 
2f40: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
2f50: 28 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 54  (.       "INSERT
2f60: 20 49 4e 54 4f 20 72 63 76 66 72 6f 6d 28 75 69   INTO rcvfrom(ui
2f70: 64 2c 20 6d 74 69 6d 65 2c 20 6e 6f 6e 63 65 2c  d, mtime, nonce,
2f80: 20 69 70 61 64 64 72 29 22 0a 20 20 20 20 20 20   ipaddr)".      
2f90: 20 22 56 41 4c 55 45 53 28 25 64 2c 20 6a 75 6c   "VALUES(%d, jul
2fa0: 69 61 6e 64 61 79 28 27 6e 6f 77 27 29 2c 20 25  ianday('now'), %
2fb0: 51 2c 20 25 51 29 22 2c 0a 20 20 20 20 20 20 20  Q, %Q)",.       
2fc0: 67 2e 75 73 65 72 55 69 64 2c 20 67 2e 7a 4e 6f  g.userUid, g.zNo
2fd0: 6e 63 65 2c 20 67 2e 7a 49 70 41 64 64 72 0a 20  nce, g.zIpAddr. 
2fe0: 20 20 20 29 3b 0a 20 20 20 20 67 2e 72 63 76 69     );.    g.rcvi
2ff0: 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69 6e 73 65  d = db_last_inse
3000: 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20 20 7d 0a  rt_rowid();.  }.
3010: 0a 20 20 69 66 28 20 72 69 64 3e 30 20 29 7b 0a  .  if( rid>0 ){.
3020: 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 6a 75      /* We are ju
3030: 73 74 20 61 64 64 69 6e 67 20 64 61 74 61 20 74  st adding data t
3040: 6f 20 61 20 70 68 61 6e 74 6f 6d 20 2a 2f 0a 20  o a phantom */. 
3050: 20 20 20 61 73 73 65 72 74 28 20 70 42 6c 6f 62     assert( pBlob
3060: 21 3d 30 20 29 3b 0a 20 20 20 20 64 62 5f 70 72  !=0 );.    db_pr
3070: 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20 20 20  epare(&s1,.     
3080: 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45   "UPDATE blob SE
3090: 54 20 72 63 76 69 64 3d 25 64 2c 20 73 69 7a 65  T rcvid=%d, size
30a0: 3d 25 64 2c 20 63 6f 6e 74 65 6e 74 3d 3a 64 61  =%d, content=:da
30b0: 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64 22  ta WHERE rid=%d"
30c0: 2c 0a 20 20 20 20 20 20 20 67 2e 72 63 76 69 64  ,.       g.rcvid
30d0: 2c 20 73 69 7a 65 2c 20 72 69 64 0a 20 20 20 20  , size, rid.    
30e0: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70  );.    blob_comp
30f0: 72 65 73 73 28 70 42 6c 6f 62 2c 20 26 63 6d 70  ress(pBlob, &cmp
3100: 72 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f  r);.    db_bind_
3110: 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61  blob(&s1, ":data
3120: 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 64  ", &cmpr);.    d
3130: 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20  b_exec(&s1);.   
3140: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
3150: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 68 61 6e  DELETE FROM phan
3160: 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d 25 64  tom WHERE rid=%d
3170: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69 66 28  ", rid);.    if(
3180: 20 73 72 63 49 64 3d 3d 30 20 7c 7c 20 63 6f 6e   srcId==0 || con
3190: 74 65 6e 74 5f 69 73 5f 61 76 61 69 6c 61 62 6c  tent_is_availabl
31a0: 65 28 73 72 63 49 64 29 20 29 7b 0a 20 20 20 20  e(srcId) ){.    
31b0: 20 20 69 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65    isDephantomize
31c0: 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 6f 6e 74   = 1;.      cont
31d0: 65 6e 74 5f 6d 61 72 6b 5f 61 76 61 69 6c 61 62  ent_mark_availab
31e0: 6c 65 28 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20  le(rid);.    }. 
31f0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57   }else{.    /* W
3200: 65 20 61 72 65 20 63 72 65 61 74 69 6e 67 20 61  e are creating a
3210: 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20   new entry */.  
3220: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31    db_prepare(&s1
3230: 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 52 54 20  ,.      "INSERT 
3240: 49 4e 54 4f 20 62 6c 6f 62 28 72 63 76 69 64 2c  INTO blob(rcvid,
3250: 73 69 7a 65 2c 75 75 69 64 2c 63 6f 6e 74 65 6e  size,uuid,conten
3260: 74 29 22 0a 20 20 20 20 20 20 22 56 41 4c 55 45  t)".      "VALUE
3270: 53 28 25 64 2c 25 64 2c 27 25 62 27 2c 3a 64 61  S(%d,%d,'%b',:da
3280: 74 61 29 22 2c 0a 20 20 20 20 20 20 20 67 2e 72  ta)",.       g.r
3290: 63 76 69 64 2c 20 73 69 7a 65 2c 20 26 68 61 73  cvid, size, &has
32a0: 68 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  h.    );.    if(
32b0: 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20   pBlob ){.      
32c0: 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 70 42  blob_compress(pB
32d0: 6c 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20  lob, &cmpr);.   
32e0: 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28     db_bind_blob(
32f0: 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 63  &s1, ":data", &c
3300: 6d 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  mpr);.    }.    
3310: 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20  db_exec(&s1);.  
3320: 20 20 72 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f    rid = db_last_
3330: 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a  insert_rowid();.
3340: 20 20 20 20 69 66 28 20 21 70 42 6c 6f 62 20 29      if( !pBlob )
3350: 7b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69  {.      db_multi
3360: 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52  _exec("INSERT OR
3370: 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 68 61   IGNORE INTO pha
3380: 6e 74 6f 6d 20 56 41 4c 55 45 53 28 25 64 29 22  ntom VALUES(%d)"
3390: 2c 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  , rid);.    }.  
33a0: 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  }..  /* If the s
33b0: 72 63 49 64 20 69 73 20 73 70 65 63 69 66 69 65  rcId is specifie
33c0: 64 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  d, then the data
33d0: 20 77 65 20 6a 75 73 74 20 61 64 64 65 64 20 69   we just added i
33e0: 73 0a 20 20 2a 2a 20 72 65 61 6c 6c 79 20 61 20  s.  ** really a 
33f0: 64 65 6c 74 61 2e 20 20 52 65 63 6f 72 64 20 74  delta.  Record t
3400: 68 69 73 20 66 61 63 74 20 69 6e 20 74 68 65 20  his fact in the 
3410: 64 65 6c 74 61 20 74 61 62 6c 65 2e 0a 20 20 2a  delta table..  *
3420: 2f 0a 20 20 69 66 28 20 73 72 63 49 64 20 29 7b  /.  if( srcId ){
3430: 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  .    db_multi_ex
3440: 65 63 28 22 52 45 50 4c 41 43 45 20 49 4e 54 4f  ec("REPLACE INTO
3450: 20 64 65 6c 74 61 28 72 69 64 2c 73 72 63 69 64   delta(rid,srcid
3460: 29 20 56 41 4c 55 45 53 28 25 64 2c 25 64 29 22  ) VALUES(%d,%d)"
3470: 2c 20 72 69 64 2c 20 73 72 63 49 64 29 3b 0a 20  , rid, srcId);. 
3480: 20 7d 0a 20 20 69 66 28 20 21 69 73 44 65 70 68   }.  if( !isDeph
3490: 61 6e 74 6f 6d 69 7a 65 20 26 26 20 62 61 67 5f  antomize && bag_
34a0: 66 69 6e 64 28 26 63 6f 6e 74 65 6e 74 43 61 63  find(&contentCac
34b0: 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64 29  he.missing, rid)
34c0: 20 26 26 20 0a 20 20 20 20 20 20 28 73 72 63 49   && .      (srcI
34d0: 64 3d 3d 30 20 7c 7c 20 63 6f 6e 74 65 6e 74 5f  d==0 || content_
34e0: 69 73 5f 61 76 61 69 6c 61 62 6c 65 28 73 72 63  is_available(src
34f0: 49 64 29 29 20 29 7b 0a 20 20 20 20 63 6f 6e 74  Id)) ){.    cont
3500: 65 6e 74 5f 6d 61 72 6b 5f 61 76 61 69 6c 61 62  ent_mark_availab
3510: 6c 65 28 72 69 64 29 3b 0a 20 20 7d 0a 20 20 69  le(rid);.  }.  i
3520: 66 28 20 69 73 44 65 70 68 61 6e 74 6f 6d 69 7a  f( isDephantomiz
3530: 65 20 29 7b 0a 20 20 20 20 61 66 74 65 72 5f 64  e ){.    after_d
3540: 65 70 68 61 6e 74 6f 6d 69 7a 65 28 72 69 64 2c  ephantomize(rid,
3550: 20 30 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a   0);.  }.  .  /*
3560: 20 41 64 64 20 74 68 65 20 65 6c 65 6d 65 6e 74   Add the element
3570: 20 74 6f 20 74 68 65 20 75 6e 63 6c 75 73 74 65   to the uncluste
3580: 72 65 64 20 74 61 62 6c 65 20 69 66 20 68 61 73  red table if has
3590: 20 6e 65 76 65 72 20 62 65 65 6e 0a 20 20 2a 2a   never been.  **
35a0: 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 65 6e   previously seen
35b0: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 61 72  ..  */.  if( mar
35c0: 6b 41 73 55 6e 63 6c 75 73 74 65 72 65 64 20 29  kAsUnclustered )
35d0: 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65  {.    db_multi_e
35e0: 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20 49  xec("INSERT OR I
35f0: 47 4e 4f 52 45 20 49 4e 54 4f 20 75 6e 63 6c 75  GNORE INTO unclu
3600: 73 74 65 72 65 64 20 56 41 4c 55 45 53 28 25 64  stered VALUES(%d
3610: 29 22 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 0a 20  )", rid);.  }.. 
3620: 20 2f 2a 20 46 69 6e 69 73 68 20 74 68 65 20 74   /* Finish the t
3630: 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 63  ransaction and c
3640: 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 64 62 5f 66  leanup */.  db_f
3650: 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 20  inalize(&s1);.  
3660: 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  db_end_transacti
3670: 6f 6e 28 30 29 3b 0a 20 20 62 6c 6f 62 5f 72 65  on(0);.  blob_re
3680: 73 65 74 28 26 68 61 73 68 29 3b 0a 0a 20 20 2f  set(&hash);..  /
3690: 2a 20 4d 61 6b 65 20 61 72 72 61 6e 67 65 6d 65  * Make arrangeme
36a0: 6e 74 73 20 74 6f 20 76 65 72 69 66 79 20 74 68  nts to verify th
36b0: 61 74 20 74 68 65 20 64 61 74 61 20 63 61 6e 20  at the data can 
36c0: 62 65 20 72 65 63 6f 76 65 72 65 64 0a 20 20 2a  be recovered.  *
36d0: 2a 20 62 65 66 6f 72 65 20 77 65 20 63 6f 6d 6d  * before we comm
36e0: 69 74 20 2a 2f 0a 20 20 69 66 28 20 70 42 6c 6f  it */.  if( pBlo
36f0: 62 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 72 65  b ){.    blob_re
3700: 73 65 74 28 26 63 6d 70 72 29 3b 0a 20 20 20 20  set(&cmpr);.    
3710: 76 65 72 69 66 79 5f 62 65 66 6f 72 65 5f 63 6f  verify_before_co
3720: 6d 6d 69 74 28 72 69 64 29 3b 0a 20 20 7d 0a 20  mmit(rid);.  }. 
3730: 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a 0a   return rid;.}..
3740: 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20  /*.** COMMAND:  
3750: 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 70 75 74  test-content-put
3760: 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61  .**.** Extract a
3770: 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20 64   blob from the d
3780: 61 74 61 62 61 73 65 20 61 6e 64 20 77 72 69 74  atabase and writ
3790: 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c 65  e it into a file
37a0: 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63  ..*/.void test_c
37b0: 6f 6e 74 65 6e 74 5f 70 75 74 5f 63 6d 64 28 76  ontent_put_cmd(v
37c0: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b  oid){.  int rid;
37d0: 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b  .  Blob content;
37e0: 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 33  .  if( g.argc!=3
37f0: 20 29 20 75 73 61 67 65 28 22 46 49 4c 45 4e 41   ) usage("FILENA
3800: 4d 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f  ME");.  db_must_
3810: 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29  be_within_tree()
3820: 3b 0a 20 20 75 73 65 72 5f 73 65 6c 65 63 74 28  ;.  user_select(
3830: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61 64 5f 66  );.  blob_read_f
3840: 72 6f 6d 5f 66 69 6c 65 28 26 63 6f 6e 74 65 6e  rom_file(&conten
3850: 74 2c 20 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20  t, g.argv[2]);. 
3860: 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e 74 5f 70   rid = content_p
3870: 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 30 2c 20  ut(&content, 0, 
3880: 30 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 69 6e  0);.  printf("in
3890: 73 65 72 74 65 64 20 61 73 20 72 65 63 6f 72 64  serted as record
38a0: 20 25 64 5c 6e 22 2c 20 72 69 64 29 3b 0a 7d 0a   %d\n", rid);.}.
38b0: 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
38c0: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20   the content at 
38d0: 72 69 64 20 69 73 20 74 68 65 20 6f 72 69 67 69  rid is the origi
38e0: 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20  nal content and 
38f0: 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 64 65 6c 74  is not a.** delt
3900: 61 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74 65  a..*/.void conte
3910: 6e 74 5f 75 6e 64 65 6c 74 61 28 69 6e 74 20 72  nt_undelta(int r
3920: 69 64 29 7b 0a 20 20 69 66 28 20 66 69 6e 64 53  id){.  if( findS
3930: 72 63 69 64 28 72 69 64 29 3e 30 20 29 7b 0a 20  rcid(rid)>0 ){. 
3940: 20 20 20 42 6c 6f 62 20 78 3b 0a 20 20 20 20 69     Blob x;.    i
3950: 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72  f( content_get(r
3960: 69 64 2c 20 26 78 29 20 29 7b 0a 20 20 20 20 20  id, &x) ){.     
3970: 20 53 74 6d 74 20 73 3b 0a 20 20 20 20 20 20 64   Stmt s;.      d
3980: 62 5f 70 72 65 70 61 72 65 28 26 73 2c 20 22 55  b_prepare(&s, "U
3990: 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 63  PDATE blob SET c
39a0: 6f 6e 74 65 6e 74 3d 3a 63 2c 20 73 69 7a 65 3d  ontent=:c, size=
39b0: 25 64 20 57 48 45 52 45 20 72 69 64 3d 25 64 22  %d WHERE rid=%d"
39c0: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
39d0: 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 69 7a 65         blob_size
39e0: 28 26 78 29 2c 20 72 69 64 29 3b 0a 20 20 20 20  (&x), rid);.    
39f0: 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28    blob_compress(
3a00: 26 78 2c 20 26 78 29 3b 0a 20 20 20 20 20 20 64  &x, &x);.      d
3a10: 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 2c 20  b_bind_blob(&s, 
3a20: 22 3a 63 22 2c 20 26 78 29 3b 0a 20 20 20 20 20  ":c", &x);.     
3a30: 20 64 62 5f 65 78 65 63 28 26 73 29 3b 0a 20 20   db_exec(&s);.  
3a40: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28      db_finalize(
3a50: 26 73 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  &s);.      blob_
3a60: 72 65 73 65 74 28 26 78 29 3b 0a 20 20 20 20 20  reset(&x);.     
3a70: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
3a80: 44 45 4c 45 54 45 20 46 52 4f 4d 20 64 65 6c 74  DELETE FROM delt
3a90: 61 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c  a WHERE rid=%d",
3aa0: 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   rid);.    }.  }
3ab0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e  .}../*.** COMMAN
3ac0: 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74  D:  test-content
3ad0: 2d 75 6e 64 65 6c 74 61 0a 2a 2a 0a 2a 2a 20 4d  -undelta.**.** M
3ae0: 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e  ake sure the con
3af0: 74 65 6e 74 20 61 74 20 52 45 43 4f 52 44 49 44  tent at RECORDID
3b00: 20 69 73 20 6e 6f 74 20 61 20 64 65 6c 74 61 0a   is not a delta.
3b10: 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e  */.void test_con
3b20: 74 65 6e 74 5f 75 6e 64 65 6c 74 61 5f 63 6d 64  tent_undelta_cmd
3b30: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69  (void){.  int ri
3b40: 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21  d;.  if( g.argc!
3b50: 3d 32 20 29 20 75 73 61 67 65 28 22 52 45 43 4f  =2 ) usage("RECO
3b60: 52 44 49 44 22 29 3b 0a 20 20 64 62 5f 6d 75 73  RDID");.  db_mus
3b70: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65  t_be_within_tree
3b80: 28 29 3b 0a 20 20 72 69 64 20 3d 20 61 74 6f 69  ();.  rid = atoi
3b90: 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 63  (g.argv[2]);.  c
3ba0: 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 72  ontent_undelta(r
3bb0: 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  id);.}../*.** Ch
3bc0: 61 6e 67 65 20 74 68 65 20 73 74 6f 72 61 67 65  ange the storage
3bd0: 20 6f 66 20 72 69 64 20 73 6f 20 74 68 61 74 20   of rid so that 
3be0: 69 74 20 69 73 20 61 20 64 65 6c 74 61 20 6f 66  it is a delta of
3bf0: 20 73 72 63 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66   srcid..**.** If
3c00: 20 72 69 64 20 69 73 20 61 6c 72 65 61 64 79 20   rid is already 
3c10: 61 20 64 65 6c 74 61 20 66 72 6f 6d 20 73 6f 6d  a delta from som
3c20: 65 20 6f 74 68 65 72 20 70 6c 61 63 65 20 74 68  e other place th
3c30: 65 6e 20 6e 6f 0a 2a 2a 20 63 6f 6e 76 65 72 73  en no.** convers
3c40: 69 6f 6e 20 6f 63 63 75 72 73 20 61 6e 64 20 74  ion occurs and t
3c50: 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75  his is a no-op u
3c60: 6e 6c 65 73 73 20 66 6f 72 63 65 3d 3d 31 2e 0a  nless force==1..
3c70: 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 69 64 20 69  **.** If srcid i
3c80: 73 20 61 20 64 65 6c 74 61 20 74 68 61 74 20 64  s a delta that d
3c90: 65 70 65 6e 64 73 20 6f 6e 20 72 69 64 2c 20 74  epends on rid, t
3ca0: 68 65 6e 20 73 72 63 69 64 20 69 73 0a 2a 2a 20  hen srcid is.** 
3cb0: 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 75 6e 64  converted to und
3cc0: 65 6c 74 61 65 64 20 74 65 78 74 2e 0a 2a 2a 0a  eltaed text..**.
3cd0: 2a 2a 20 49 66 20 65 69 74 68 65 72 20 72 69 64  ** If either rid
3ce0: 20 6f 72 20 73 72 63 69 64 20 63 6f 6e 74 61 69   or srcid contai
3cf0: 6e 20 6c 65 73 73 20 74 68 61 6e 20 35 30 20 62  n less than 50 b
3d00: 79 74 65 73 2c 20 6f 72 20 69 66 20 74 68 65 0a  ytes, or if the.
3d10: 2a 2a 20 72 65 73 75 6c 74 69 6e 67 20 64 65 6c  ** resulting del
3d20: 74 61 20 64 6f 65 73 20 6e 6f 74 20 61 63 68 69  ta does not achi
3d30: 65 76 65 20 61 20 63 6f 6d 70 72 65 73 73 69 6f  eve a compressio
3d40: 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 32 35  n of at least 25
3d50: 25 20 6f 6e 0a 2a 2a 20 69 74 73 20 6f 77 6e 20  % on.** its own 
3d60: 74 68 65 20 72 69 64 20 69 73 20 6c 65 66 74 20  the rid is left 
3d70: 75 6e 74 6f 75 63 68 65 64 2e 0a 2a 2a 0a 2a 2a  untouched..**.**
3d80: 20 4e 4f 54 45 3a 20 49 4d 48 4f 20 74 68 65 20   NOTE: IMHO the 
3d90: 63 72 65 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  creation of the 
3da0: 64 65 6c 74 61 20 73 68 6f 75 6c 64 20 62 65 20  delta should be 
3db0: 64 65 66 65 72 65 64 20 75 6e 74 69 6c 20 61 66  defered until af
3dc0: 74 65 72 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20  ter.** the blob 
3dd0: 73 69 7a 65 73 20 68 61 76 65 20 62 65 65 6e 20  sizes have been 
3de0: 63 68 65 63 6b 65 64 2e 20 44 6f 69 6e 67 20 69  checked. Doing i
3df0: 74 20 62 65 66 6f 72 65 20 74 68 65 20 63 68 65  t before the che
3e00: 63 6b 20 61 73 20 69 73 0a 2a 2a 20 64 6f 6e 65  ck as is.** done
3e10: 20 6e 6f 77 20 74 68 65 20 63 6f 64 65 20 77 69   now the code wi
3e20: 6c 6c 20 67 65 6e 65 72 61 74 65 20 61 20 64 65  ll generate a de
3e30: 6c 74 61 20 6a 75 73 74 20 74 6f 20 69 6d 6d 65  lta just to imme
3e40: 64 69 61 74 65 6c 79 20 74 68 72 6f 77 0a 2a 2a  diately throw.**
3e50: 20 69 74 20 61 77 61 79 2c 20 77 61 73 74 69 6e   it away, wastin
3e60: 67 20 73 70 61 63 65 20 61 6e 64 20 74 69 6d 65  g space and time
3e70: 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74 65 6e  ..*/.void conten
3e80: 74 5f 64 65 6c 74 69 66 79 28 69 6e 74 20 72 69  t_deltify(int ri
3e90: 64 2c 20 69 6e 74 20 73 72 63 69 64 2c 20 69 6e  d, int srcid, in
3ea0: 74 20 66 6f 72 63 65 29 7b 0a 20 20 69 6e 74 20  t force){.  int 
3eb0: 73 3b 0a 20 20 42 6c 6f 62 20 64 61 74 61 2c 20  s;.  Blob data, 
3ec0: 73 72 63 2c 20 64 65 6c 74 61 3b 0a 20 20 53 74  src, delta;.  St
3ed0: 6d 74 20 73 31 2c 20 73 32 3b 0a 20 20 69 66 28  mt s1, s2;.  if(
3ee0: 20 73 72 63 69 64 3d 3d 72 69 64 20 29 20 72 65   srcid==rid ) re
3ef0: 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 66 6f 72  turn;.  if( !for
3f00: 63 65 20 26 26 20 66 69 6e 64 53 72 63 69 64 28  ce && findSrcid(
3f10: 72 69 64 29 3e 30 20 29 20 72 65 74 75 72 6e 3b  rid)>0 ) return;
3f20: 0a 20 20 73 20 3d 20 73 72 63 69 64 3b 0a 20 20  .  s = srcid;.  
3f30: 77 68 69 6c 65 28 20 28 73 20 3d 20 66 69 6e 64  while( (s = find
3f40: 53 72 63 69 64 28 73 29 29 3e 30 20 29 7b 0a 20  Srcid(s))>0 ){. 
3f50: 20 20 20 69 66 28 20 73 3d 3d 72 69 64 20 29 7b     if( s==rid ){
3f60: 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 75  .      content_u
3f70: 6e 64 65 6c 74 61 28 73 72 63 69 64 29 3b 0a 20  ndelta(srcid);. 
3f80: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3f90: 7d 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 74 5f  }.  }.  content_
3fa0: 67 65 74 28 73 72 63 69 64 2c 20 26 73 72 63 29  get(srcid, &src)
3fb0: 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28  ;.  content_get(
3fc0: 72 69 64 2c 20 26 64 61 74 61 29 3b 0a 20 20 62  rid, &data);.  b
3fd0: 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61 74 65  lob_delta_create
3fe0: 28 26 73 72 63 2c 20 26 64 61 74 61 2c 20 26 64  (&src, &data, &d
3ff0: 65 6c 74 61 29 3b 0a 20 20 69 66 28 20 62 6c 6f  elta);.  if( blo
4000: 62 5f 73 69 7a 65 28 26 73 72 63 29 3e 3d 35 30  b_size(&src)>=50
4010: 20 26 26 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64   && blob_size(&d
4020: 61 74 61 29 3e 3d 35 30 20 26 26 0a 20 20 20 20  ata)>=50 &&.    
4030: 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 69 7a 65         blob_size
4040: 28 26 64 65 6c 74 61 29 20 3c 20 62 6c 6f 62 5f  (&delta) < blob_
4050: 73 69 7a 65 28 26 64 61 74 61 29 2a 30 2e 37 35  size(&data)*0.75
4060: 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 6d   ){.    blob_com
4070: 70 72 65 73 73 28 26 64 65 6c 74 61 2c 20 26 64  press(&delta, &d
4080: 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 70 72  elta);.    db_pr
4090: 65 70 61 72 65 28 26 73 31 2c 20 22 55 50 44 41  epare(&s1, "UPDA
40a0: 54 45 20 62 6c 6f 62 20 53 45 54 20 63 6f 6e 74  TE blob SET cont
40b0: 65 6e 74 3d 3a 64 61 74 61 20 57 48 45 52 45 20  ent=:data WHERE 
40c0: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20  rid=%d", rid);. 
40d0: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73     db_prepare(&s
40e0: 32 2c 20 22 52 45 50 4c 41 43 45 20 49 4e 54 4f  2, "REPLACE INTO
40f0: 20 64 65 6c 74 61 28 72 69 64 2c 73 72 63 69 64   delta(rid,srcid
4100: 29 56 41 4c 55 45 53 28 25 64 2c 25 64 29 22 2c  )VALUES(%d,%d)",
4110: 20 72 69 64 2c 20 73 72 63 69 64 29 3b 0a 20 20   rid, srcid);.  
4120: 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26    db_bind_blob(&
4130: 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 64 65  s1, ":data", &de
4140: 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 62 65 67  lta);.    db_beg
4150: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
4160: 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 73  ;.    db_exec(&s
4170: 31 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28  1);.    db_exec(
4180: 26 73 32 29 3b 0a 20 20 20 20 64 62 5f 65 6e 64  &s2);.    db_end
4190: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
41a0: 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65  .    db_finalize
41b0: 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 66 69  (&s1);.    db_fi
41c0: 6e 61 6c 69 7a 65 28 26 73 32 29 3b 0a 20 20 20  nalize(&s2);.   
41d0: 20 76 65 72 69 66 79 5f 62 65 66 6f 72 65 5f 63   verify_before_c
41e0: 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20 20 7d 0a  ommit(rid);.  }.
41f0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
4200: 63 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74  c);.  blob_reset
4210: 28 26 64 61 74 61 29 3b 0a 20 20 62 6c 6f 62 5f  (&data);.  blob_
4220: 72 65 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 7d  reset(&delta);.}
4230: 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a  ../*.** COMMAND:
4240: 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 64    test-content-d
4250: 65 6c 74 69 66 79 0a 2a 2a 0a 2a 2a 20 43 6f 6e  eltify.**.** Con
4260: 76 65 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  vert the content
4270: 20 61 74 20 52 49 44 20 69 6e 74 6f 20 61 20 64   at RID into a d
4280: 65 6c 74 61 20 66 72 6f 6d 20 53 52 43 49 44 2e  elta from SRCID.
4290: 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f  .*/.void test_co
42a0: 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 5f 63 6d  ntent_deltify_cm
42b0: 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 67  d(void){.  if( g
42c0: 2e 61 72 67 63 21 3d 35 20 29 20 75 73 61 67 65  .argc!=5 ) usage
42d0: 28 22 52 49 44 20 53 52 43 49 44 20 46 4f 52 43  ("RID SRCID FORC
42e0: 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f 62  E");.  db_must_b
42f0: 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b  e_within_tree();
4300: 0a 20 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69  .  content_delti
4310: 66 79 28 61 74 6f 69 28 67 2e 61 72 67 76 5b 32  fy(atoi(g.argv[2
4320: 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67 76 5b  ]), atoi(g.argv[
4330: 33 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67 76  3]), atoi(g.argv
4340: 5b 34 5d 29 29 3b 0a 7d 0a                       [4]));.}.