Hex Artifact Content
Not logged in

Artifact dbfb1885ae071615c1ea1b56bff4300c56f4a7be:

File src/content.c part of check-in [043d63d4aa] - Additional speed improvements for clone and rebuild. by drh on 2008-03-08 19:42:53.

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 49  phantom..**.** I
2900: 66 20 73 72 63 49 64 20 69 73 20 73 70 65 63 69  f srcId is speci
2910: 66 69 65 64 20 74 68 65 6e 20 74 68 65 20 55 55  fied then the UU
2920: 49 44 20 69 73 20 73 65 74 20 74 6f 20 7a 55 75  ID is set to zUu
2930: 69 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 7a  id.  Otherwise z
2940: 55 75 69 64 20 69 73 0a 2a 2a 20 69 67 6e 6f 72  Uuid is.** ignor
2950: 65 64 2e 20 20 49 6e 20 74 68 65 20 66 75 74 75  ed.  In the futu
2960: 72 65 20 74 68 69 73 20 6d 69 67 68 74 20 63 68  re this might ch
2970: 61 6e 67 65 20 73 75 63 68 20 74 68 61 74 20 74  ange such that t
2980: 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 68 61  he content.** ha
2990: 73 68 20 69 73 20 63 68 65 63 6b 65 64 20 61 67  sh is checked ag
29a0: 61 69 6e 73 74 20 7a 55 75 69 64 20 74 6f 20 6d  ainst zUuid to m
29b0: 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 63  ake sure it is c
29c0: 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  orrect..**.** If
29d0: 20 74 68 65 20 72 65 63 6f 72 64 20 61 6c 72 65   the record alre
29e0: 61 64 79 20 65 78 69 73 74 73 20 62 75 74 20 69  ady exists but i
29f0: 73 20 61 20 70 68 61 6e 74 6f 6d 2c 20 74 68 65  s a phantom, the
2a00: 20 70 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 0a 2a   pBlob content.*
2a10: 2a 20 69 73 20 69 6e 73 65 72 74 65 64 20 61 6e  * is inserted an
2a20: 64 20 74 68 65 20 70 68 61 74 6f 6d 20 62 65 63  d the phatom bec
2a30: 6f 6d 65 73 20 61 20 72 65 61 6c 20 72 65 63 6f  omes a real reco
2a40: 72 64 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65  rd..*/.int conte
2a50: 6e 74 5f 70 75 74 28 42 6c 6f 62 20 2a 70 42 6c  nt_put(Blob *pBl
2a60: 6f 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ob, const char *
2a70: 7a 55 75 69 64 2c 20 69 6e 74 20 73 72 63 49 64  zUuid, int srcId
2a80: 29 7b 0a 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20  ){.  int size;. 
2a90: 20 69 6e 74 20 72 69 64 3b 0a 20 20 53 74 6d 74   int rid;.  Stmt
2aa0: 20 73 31 3b 0a 20 20 42 6c 6f 62 20 63 6d 70 72   s1;.  Blob cmpr
2ab0: 3b 0a 20 20 42 6c 6f 62 20 68 61 73 68 3b 0a 20  ;.  Blob hash;. 
2ac0: 20 69 6e 74 20 6d 61 72 6b 41 73 55 6e 63 6c 75   int markAsUnclu
2ad0: 73 74 65 72 65 64 20 3d 20 30 3b 0a 20 20 69 6e  stered = 0;.  in
2ae0: 74 20 69 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65  t isDephantomize
2af0: 20 3d 20 30 3b 0a 20 20 0a 20 20 61 73 73 65 72   = 0;.  .  asser
2b00: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  t( g.repositoryO
2b10: 70 65 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28  pen );.  assert(
2b20: 20 70 42 6c 6f 62 21 3d 30 20 29 3b 0a 20 20 69   pBlob!=0 );.  i
2b30: 66 28 20 73 72 63 49 64 3d 3d 30 20 29 7b 0a 20  f( srcId==0 ){. 
2b40: 20 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28     sha1sum_blob(
2b50: 70 42 6c 6f 62 2c 20 26 68 61 73 68 29 3b 0a 20  pBlob, &hash);. 
2b60: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62   }else{.    blob
2b70: 5f 69 6e 69 74 28 26 68 61 73 68 2c 20 7a 55 75  _init(&hash, zUu
2b80: 69 64 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 73  id, -1);.  }.  s
2b90: 69 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28  ize = blob_size(
2ba0: 70 42 6c 6f 62 29 3b 0a 20 20 64 62 5f 62 65 67  pBlob);.  db_beg
2bb0: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
2bc0: 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  ;..  /* Check to
2bd0: 20 73 65 65 20 69 66 20 74 68 65 20 65 6e 74 72   see if the entr
2be0: 79 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  y already exists
2bf0: 20 61 6e 64 20 69 66 20 69 74 20 64 6f 65 73 20   and if it does 
2c00: 77 68 65 74 68 65 72 0a 20 20 2a 2a 20 6f 72 20  whether.  ** or 
2c10: 6e 6f 74 20 74 68 65 20 65 6e 74 72 79 20 69 73  not the entry is
2c20: 20 61 20 70 68 61 6e 74 6f 6d 0a 20 20 2a 2f 0a   a phantom.  */.
2c30: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31    db_prepare(&s1
2c40: 2c 20 22 53 45 4c 45 43 54 20 72 69 64 2c 20 73  , "SELECT rid, s
2c50: 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  ize FROM blob WH
2c60: 45 52 45 20 75 75 69 64 3d 25 42 22 2c 20 26 68  ERE uuid=%B", &h
2c70: 61 73 68 29 3b 0a 20 20 69 66 28 20 64 62 5f 73  ash);.  if( db_s
2c80: 74 65 70 28 26 73 31 29 3d 3d 53 51 4c 49 54 45  tep(&s1)==SQLITE
2c90: 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 69 64 20  _ROW ){.    rid 
2ca0: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  = db_column_int(
2cb0: 26 73 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  &s1, 0);.    if(
2cc0: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26   db_column_int(&
2cd0: 73 31 2c 20 31 29 3e 3d 30 20 7c 7c 20 70 42 6c  s1, 1)>=0 || pBl
2ce0: 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ob==0 ){.      /
2cf0: 2a 20 45 69 74 68 65 72 20 74 68 65 20 65 6e 74  * Either the ent
2d00: 72 79 20 69 73 20 6e 6f 74 20 61 20 70 68 61 6e  ry is not a phan
2d10: 74 6f 6d 20 6f 72 20 69 74 20 69 73 20 61 20 70  tom or it is a p
2d20: 68 61 6e 74 6f 6d 20 62 75 74 20 77 65 0a 20 20  hantom but we.  
2d30: 20 20 20 20 2a 2a 20 68 61 76 65 20 6e 6f 20 64      ** have no d
2d40: 61 74 61 20 77 69 74 68 20 77 68 69 63 68 20 74  ata with which t
2d50: 6f 20 64 65 70 68 61 6e 74 6f 6d 69 7a 65 20 69  o dephantomize i
2d60: 74 2e 20 20 49 6e 20 65 69 74 68 65 72 20 63 61  t.  In either ca
2d70: 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  se,.      ** the
2d80: 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 66 6f  re is nothing fo
2d90: 72 20 75 73 20 74 6f 20 64 6f 20 6f 74 68 65 72  r us to do other
2da0: 20 74 68 61 6e 20 72 65 74 75 72 6e 20 74 68 65   than return the
2db0: 20 52 49 44 2e 20 2a 2f 0a 20 20 20 20 20 20 64   RID. */.      d
2dc0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b  b_finalize(&s1);
2dd0: 0a 20 20 20 20 20 20 64 62 5f 65 6e 64 5f 74 72  .      db_end_tr
2de0: 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20  ansaction(0);.  
2df0: 20 20 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a      return rid;.
2e00: 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
2e10: 20 20 20 72 69 64 20 3d 20 30 3b 20 20 2f 2a 20     rid = 0;  /* 
2e20: 4e 6f 20 65 6e 74 72 79 20 77 69 74 68 20 74 68  No entry with th
2e30: 65 20 73 61 6d 65 20 55 55 49 44 20 63 75 72 72  e same UUID curr
2e40: 65 6e 74 6c 79 20 65 78 69 73 74 73 20 2a 2f 0a  ently exists */.
2e50: 20 20 20 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73      markAsUnclus
2e60: 74 65 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  tered = 1;.  }. 
2e70: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31   db_finalize(&s1
2e80: 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75  );..  /* Constru
2e90: 63 74 20 61 20 72 65 63 65 69 76 65 64 2d 66 72  ct a received-fr
2ea0: 6f 6d 20 49 44 20 69 66 20 77 65 20 64 6f 20 6e  om ID if we do n
2eb0: 6f 74 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  ot already have 
2ec0: 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 67 2e 72  one */.  if( g.r
2ed0: 63 76 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 64  cvid==0 ){.    d
2ee0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
2ef0: 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54       "INSERT INT
2f00: 4f 20 72 63 76 66 72 6f 6d 28 75 69 64 2c 20 6d  O rcvfrom(uid, m
2f10: 74 69 6d 65 2c 20 6e 6f 6e 63 65 2c 20 69 70 61  time, nonce, ipa
2f20: 64 64 72 29 22 0a 20 20 20 20 20 20 20 22 56 41  ddr)".       "VA
2f30: 4c 55 45 53 28 25 64 2c 20 6a 75 6c 69 61 6e 64  LUES(%d, juliand
2f40: 61 79 28 27 6e 6f 77 27 29 2c 20 25 51 2c 20 25  ay('now'), %Q, %
2f50: 51 29 22 2c 0a 20 20 20 20 20 20 20 67 2e 75 73  Q)",.       g.us
2f60: 65 72 55 69 64 2c 20 67 2e 7a 4e 6f 6e 63 65 2c  erUid, g.zNonce,
2f70: 20 67 2e 7a 49 70 41 64 64 72 0a 20 20 20 20 29   g.zIpAddr.    )
2f80: 3b 0a 20 20 20 20 67 2e 72 63 76 69 64 20 3d 20  ;.    g.rcvid = 
2f90: 64 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  db_last_insert_r
2fa0: 6f 77 69 64 28 29 3b 0a 20 20 7d 0a 0a 20 20 62  owid();.  }..  b
2fb0: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c  lob_compress(pBl
2fc0: 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20 20 69 66  ob, &cmpr);.  if
2fd0: 28 20 72 69 64 3e 30 20 29 7b 0a 20 20 20 20 2f  ( rid>0 ){.    /
2fe0: 2a 20 57 65 20 61 72 65 20 6a 75 73 74 20 61 64  * We are just ad
2ff0: 64 69 6e 67 20 64 61 74 61 20 74 6f 20 61 20 70  ding data to a p
3000: 68 61 6e 74 6f 6d 20 2a 2f 0a 20 20 20 20 64 62  hantom */.    db
3010: 5f 70 72 65 70 61 72 65 28 26 73 31 2c 0a 20 20  _prepare(&s1,.  
3020: 20 20 20 20 22 55 50 44 41 54 45 20 62 6c 6f 62      "UPDATE blob
3030: 20 53 45 54 20 72 63 76 69 64 3d 25 64 2c 20 73   SET rcvid=%d, s
3040: 69 7a 65 3d 25 64 2c 20 63 6f 6e 74 65 6e 74 3d  ize=%d, content=
3050: 3a 64 61 74 61 20 57 48 45 52 45 20 72 69 64 3d  :data WHERE rid=
3060: 25 64 22 2c 0a 20 20 20 20 20 20 20 67 2e 72 63  %d",.       g.rc
3070: 76 69 64 2c 20 73 69 7a 65 2c 20 72 69 64 0a 20  vid, size, rid. 
3080: 20 20 20 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e     );.    db_bin
3090: 64 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61  d_blob(&s1, ":da
30a0: 74 61 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20  ta", &cmpr);.   
30b0: 20 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20   db_exec(&s1);. 
30c0: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
30d0: 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 68  ("DELETE FROM ph
30e0: 61 6e 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d  antom WHERE rid=
30f0: 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69  %d", rid);.    i
3100: 66 28 20 73 72 63 49 64 3d 3d 30 20 7c 7c 20 63  f( srcId==0 || c
3110: 6f 6e 74 65 6e 74 5f 69 73 5f 61 76 61 69 6c 61  ontent_is_availa
3120: 62 6c 65 28 73 72 63 49 64 29 20 29 7b 0a 20 20  ble(srcId) ){.  
3130: 20 20 20 20 69 73 44 65 70 68 61 6e 74 6f 6d 69      isDephantomi
3140: 7a 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 6f  ze = 1;.      co
3150: 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61 76 61 69 6c  ntent_mark_avail
3160: 61 62 6c 65 28 72 69 64 29 3b 0a 20 20 20 20 7d  able(rid);.    }
3170: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
3180: 20 57 65 20 61 72 65 20 63 72 65 61 74 69 6e 67   We are creating
3190: 20 61 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a   a new entry */.
31a0: 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26      db_prepare(&
31b0: 73 31 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 52  s1,.      "INSER
31c0: 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72 63 76 69  T INTO blob(rcvi
31d0: 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63 6f 6e 74  d,size,uuid,cont
31e0: 65 6e 74 29 22 0a 20 20 20 20 20 20 22 56 41 4c  ent)".      "VAL
31f0: 55 45 53 28 25 64 2c 25 64 2c 27 25 62 27 2c 3a  UES(%d,%d,'%b',:
3200: 64 61 74 61 29 22 2c 0a 20 20 20 20 20 20 20 67  data)",.       g
3210: 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c 20 26 68  .rcvid, size, &h
3220: 61 73 68 0a 20 20 20 20 29 3b 0a 20 20 20 20 62  ash.    );.    b
3230: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c  lob_compress(pBl
3240: 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20  ob, &cmpr);.    
3250: 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20  db_exec(&s1);.  
3260: 20 20 72 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f    rid = db_last_
3270: 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a  insert_rowid();.
3280: 20 20 20 20 69 66 28 20 21 70 42 6c 6f 62 20 29      if( !pBlob )
3290: 7b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69  {.      db_multi
32a0: 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52  _exec("INSERT OR
32b0: 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 68 61   IGNORE INTO pha
32c0: 6e 74 6f 6d 20 56 41 4c 55 45 53 28 25 64 29 22  ntom VALUES(%d)"
32d0: 2c 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  , rid);.    }.  
32e0: 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  }.  blob_reset(&
32f0: 63 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  cmpr);..  /* If 
3300: 74 68 65 20 73 72 63 49 64 20 69 73 20 73 70 65  the srcId is spe
3310: 63 69 66 69 65 64 2c 20 74 68 65 6e 20 74 68 65  cified, then the
3320: 20 64 61 74 61 20 77 65 20 6a 75 73 74 20 61 64   data we just ad
3330: 64 65 64 20 69 73 0a 20 20 2a 2a 20 72 65 61 6c  ded is.  ** real
3340: 6c 79 20 61 20 64 65 6c 74 61 2e 20 20 52 65 63  ly a delta.  Rec
3350: 6f 72 64 20 74 68 69 73 20 66 61 63 74 20 69 6e  ord this fact in
3360: 20 74 68 65 20 64 65 6c 74 61 20 74 61 62 6c 65   the delta table
3370: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 72 63  ..  */.  if( src
3380: 49 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c  Id ){.    db_mul
3390: 74 69 5f 65 78 65 63 28 22 52 45 50 4c 41 43 45  ti_exec("REPLACE
33a0: 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69 64 2c   INTO delta(rid,
33b0: 73 72 63 69 64 29 20 56 41 4c 55 45 53 28 25 64  srcid) VALUES(%d
33c0: 2c 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63 49  ,%d)", rid, srcI
33d0: 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69  d);.  }.  if( !i
33e0: 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65 20 26 26  sDephantomize &&
33f0: 20 62 61 67 5f 66 69 6e 64 28 26 63 6f 6e 74 65   bag_find(&conte
3400: 6e 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c  ntCache.missing,
3410: 20 72 69 64 29 20 26 26 20 0a 20 20 20 20 20 20   rid) && .      
3420: 28 73 72 63 49 64 3d 3d 30 20 7c 7c 20 63 6f 6e  (srcId==0 || con
3430: 74 65 6e 74 5f 69 73 5f 61 76 61 69 6c 61 62 6c  tent_is_availabl
3440: 65 28 73 72 63 49 64 29 29 20 29 7b 0a 20 20 20  e(srcId)) ){.   
3450: 20 63 6f 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61 76   content_mark_av
3460: 61 69 6c 61 62 6c 65 28 72 69 64 29 3b 0a 20 20  ailable(rid);.  
3470: 7d 0a 20 20 69 66 28 20 69 73 44 65 70 68 61 6e  }.  if( isDephan
3480: 74 6f 6d 69 7a 65 20 29 7b 0a 20 20 20 20 61 66  tomize ){.    af
3490: 74 65 72 5f 64 65 70 68 61 6e 74 6f 6d 69 7a 65  ter_dephantomize
34a0: 28 72 69 64 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  (rid, 0);.  }.  
34b0: 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65 6c  .  /* Add the el
34c0: 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 6e 63  ement to the unc
34d0: 6c 75 73 74 65 72 65 64 20 74 61 62 6c 65 20 69  lustered table i
34e0: 66 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e  f has never been
34f0: 0a 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  .  ** previously
3500: 20 73 65 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66   seen..  */.  if
3510: 28 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74 65  ( markAsUncluste
3520: 72 65 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75  red ){.    db_mu
3530: 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54  lti_exec("INSERT
3540: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20   OR IGNORE INTO 
3550: 75 6e 63 6c 75 73 74 65 72 65 64 20 56 41 4c 55  unclustered VALU
3560: 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a 20  ES(%d)", rid);. 
3570: 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 73 68 20   }..  /* Finish 
3580: 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
3590: 61 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f 0a 20  and cleanup */. 
35a0: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31   db_finalize(&s1
35b0: 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e  );.  db_end_tran
35c0: 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 62 6c  saction(0);.  bl
35d0: 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b  ob_reset(&hash);
35e0: 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72 72 61  ..  /* Make arra
35f0: 6e 67 65 6d 65 6e 74 73 20 74 6f 20 76 65 72 69  ngements to veri
3600: 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74 61  fy that the data
3610: 20 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72 65   can be recovere
3620: 64 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 77 65  d.  ** before we
3630: 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 76 65 72   commit */.  ver
3640: 69 66 79 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d 69  ify_before_commi
3650: 74 28 72 69 64 29 3b 0a 20 20 72 65 74 75 72 6e  t(rid);.  return
3660: 20 72 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   rid;.}../*.** C
3670: 72 65 61 74 65 20 61 20 6e 65 77 20 70 68 61 6e  reate a new phan
3680: 74 6f 6d 20 77 69 74 68 20 74 68 65 20 67 69 76  tom with the giv
3690: 65 6e 20 55 55 49 44 20 61 6e 64 20 72 65 74 75  en UUID and retu
36a0: 72 6e 20 69 74 73 20 61 72 74 69 66 61 63 74 20  rn its artifact 
36b0: 49 44 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65  ID..*/.int conte
36c0: 6e 74 5f 6e 65 77 28 63 6f 6e 73 74 20 63 68 61  nt_new(const cha
36d0: 72 20 2a 7a 55 75 69 64 29 7b 0a 20 20 69 6e 74  r *zUuid){.  int
36e0: 20 72 69 64 3b 0a 20 20 73 74 61 74 69 63 20 53   rid;.  static S
36f0: 74 6d 74 20 73 31 2c 20 73 32 3b 0a 20 20 0a 20  tmt s1, s2;.  . 
3700: 20 61 73 73 65 72 74 28 20 67 2e 72 65 70 6f 73   assert( g.repos
3710: 69 74 6f 72 79 4f 70 65 6e 20 29 3b 0a 20 20 64  itoryOpen );.  d
3720: 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74  b_begin_transact
3730: 69 6f 6e 28 29 3b 0a 20 20 64 62 5f 73 74 61 74  ion();.  db_stat
3740: 69 63 5f 70 72 65 70 61 72 65 28 26 73 31 2c 0a  ic_prepare(&s1,.
3750: 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
3760: 20 62 6c 6f 62 28 72 63 76 69 64 2c 73 69 7a 65   blob(rcvid,size
3770: 2c 75 75 69 64 2c 63 6f 6e 74 65 6e 74 29 22 0a  ,uuid,content)".
3780: 20 20 20 20 22 56 41 4c 55 45 53 28 30 2c 2d 31      "VALUES(0,-1
3790: 2c 3a 75 75 69 64 2c 4e 55 4c 4c 29 22 0a 20 20  ,:uuid,NULL)".  
37a0: 29 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 74 65 78  );.  db_bind_tex
37b0: 74 28 26 73 31 2c 20 22 3a 75 75 69 64 22 2c 20  t(&s1, ":uuid", 
37c0: 7a 55 75 69 64 29 3b 0a 20 20 64 62 5f 65 78 65  zUuid);.  db_exe
37d0: 63 28 26 73 31 29 3b 0a 20 20 72 69 64 20 3d 20  c(&s1);.  rid = 
37e0: 64 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  db_last_insert_r
37f0: 6f 77 69 64 28 29 3b 0a 20 20 64 62 5f 73 74 61  owid();.  db_sta
3800: 74 69 63 5f 70 72 65 70 61 72 65 28 26 73 32 2c  tic_prepare(&s2,
3810: 0a 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54  .    "INSERT INT
3820: 4f 20 70 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53  O phantom VALUES
3830: 28 3a 72 69 64 29 22 0a 20 20 29 3b 0a 20 20 64  (:rid)".  );.  d
3840: 62 5f 62 69 6e 64 5f 69 6e 74 28 26 73 32 2c 20  b_bind_int(&s2, 
3850: 22 3a 72 69 64 22 2c 20 72 69 64 29 3b 0a 20 20  ":rid", rid);.  
3860: 64 62 5f 65 78 65 63 28 26 73 32 29 3b 0a 20 20  db_exec(&s2);.  
3870: 62 61 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e 74  bag_insert(&cont
3880: 65 6e 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67  entCache.missing
3890: 2c 20 72 69 64 29 3b 0a 20 20 64 62 5f 65 6e 64  , rid);.  db_end
38a0: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
38b0: 0a 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d  .  return rid;.}
38c0: 0a 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44  .../*.** COMMAND
38d0: 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d  :  test-content-
38e0: 70 75 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63  put.**.** Extrac
38f0: 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68  t a blob from th
3900: 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77  e database and w
3910: 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66  rite it into a f
3920: 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73  ile..*/.void tes
3930: 74 5f 63 6f 6e 74 65 6e 74 5f 70 75 74 5f 63 6d  t_content_put_cm
3940: 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72  d(void){.  int r
3950: 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65  id;.  Blob conte
3960: 6e 74 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63  nt;.  if( g.argc
3970: 21 3d 33 20 29 20 75 73 61 67 65 28 22 46 49 4c  !=3 ) usage("FIL
3980: 45 4e 41 4d 45 22 29 3b 0a 20 20 64 62 5f 6d 75  ENAME");.  db_mu
3990: 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65  st_be_within_tre
39a0: 65 28 29 3b 0a 20 20 75 73 65 72 5f 73 65 6c 65  e();.  user_sele
39b0: 63 74 28 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61  ct();.  blob_rea
39c0: 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 63 6f 6e  d_from_file(&con
39d0: 74 65 6e 74 2c 20 67 2e 61 72 67 76 5b 32 5d 29  tent, g.argv[2])
39e0: 3b 0a 20 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e  ;.  rid = conten
39f0: 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20  t_put(&content, 
3a00: 30 2c 20 30 29 3b 0a 20 20 70 72 69 6e 74 66 28  0, 0);.  printf(
3a10: 22 69 6e 73 65 72 74 65 64 20 61 73 20 72 65 63  "inserted as rec
3a20: 6f 72 64 20 25 64 5c 6e 22 2c 20 72 69 64 29 3b  ord %d\n", rid);
3a30: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
3a40: 75 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ure the content 
3a50: 61 74 20 72 69 64 20 69 73 20 74 68 65 20 6f 72  at rid is the or
3a60: 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 61  iginal content a
3a70: 6e 64 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 64  nd is not a.** d
3a80: 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f  elta..*/.void co
3a90: 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 69 6e  ntent_undelta(in
3aa0: 74 20 72 69 64 29 7b 0a 20 20 69 66 28 20 66 69  t rid){.  if( fi
3ab0: 6e 64 53 72 63 69 64 28 72 69 64 29 3e 30 20 29  ndSrcid(rid)>0 )
3ac0: 7b 0a 20 20 20 20 42 6c 6f 62 20 78 3b 0a 20 20  {.    Blob x;.  
3ad0: 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65    if( content_ge
3ae0: 74 28 72 69 64 2c 20 26 78 29 20 29 7b 0a 20 20  t(rid, &x) ){.  
3af0: 20 20 20 20 53 74 6d 74 20 73 3b 0a 20 20 20 20      Stmt s;.    
3b00: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 2c    db_prepare(&s,
3b10: 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45   "UPDATE blob SE
3b20: 54 20 63 6f 6e 74 65 6e 74 3d 3a 63 2c 20 73 69  T content=:c, si
3b30: 7a 65 3d 25 64 20 57 48 45 52 45 20 72 69 64 3d  ze=%d WHERE rid=
3b40: 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  %d",.           
3b50: 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73            blob_s
3b60: 69 7a 65 28 26 78 29 2c 20 72 69 64 29 3b 0a 20  ize(&x), rid);. 
3b70: 20 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65       blob_compre
3b80: 73 73 28 26 78 2c 20 26 78 29 3b 0a 20 20 20 20  ss(&x, &x);.    
3b90: 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26    db_bind_blob(&
3ba0: 73 2c 20 22 3a 63 22 2c 20 26 78 29 3b 0a 20 20  s, ":c", &x);.  
3bb0: 20 20 20 20 64 62 5f 65 78 65 63 28 26 73 29 3b      db_exec(&s);
3bc0: 0a 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69  .      db_finali
3bd0: 7a 65 28 26 73 29 3b 0a 20 20 20 20 20 20 62 6c  ze(&s);.      bl
3be0: 6f 62 5f 72 65 73 65 74 28 26 78 29 3b 0a 20 20  ob_reset(&x);.  
3bf0: 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65      db_multi_exe
3c00: 63 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 64  c("DELETE FROM d
3c10: 65 6c 74 61 20 57 48 45 52 45 20 72 69 64 3d 25  elta WHERE rid=%
3c20: 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a  d", rid);.    }.
3c30: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d    }.}../*.** COM
3c40: 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74  MAND:  test-cont
3c50: 65 6e 74 2d 75 6e 64 65 6c 74 61 0a 2a 2a 0a 2a  ent-undelta.**.*
3c60: 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
3c70: 63 6f 6e 74 65 6e 74 20 61 74 20 52 45 43 4f 52  content at RECOR
3c80: 44 49 44 20 69 73 20 6e 6f 74 20 61 20 64 65 6c  DID is not a del
3c90: 74 61 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f  ta.*/.void test_
3ca0: 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 5f  content_undelta_
3cb0: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  cmd(void){.  int
3cc0: 20 72 69 64 3b 0a 20 20 69 66 28 20 67 2e 61 72   rid;.  if( g.ar
3cd0: 67 63 21 3d 32 20 29 20 75 73 61 67 65 28 22 52  gc!=2 ) usage("R
3ce0: 45 43 4f 52 44 49 44 22 29 3b 0a 20 20 64 62 5f  ECORDID");.  db_
3cf0: 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74  must_be_within_t
3d00: 72 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 61  ree();.  rid = a
3d10: 74 6f 69 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a  toi(g.argv[2]);.
3d20: 20 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74    content_undelt
3d30: 61 28 72 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  a(rid);.}../*.**
3d40: 20 43 68 61 6e 67 65 20 74 68 65 20 73 74 6f 72   Change the stor
3d50: 61 67 65 20 6f 66 20 72 69 64 20 73 6f 20 74 68  age of rid so th
3d60: 61 74 20 69 74 20 69 73 20 61 20 64 65 6c 74 61  at it is a delta
3d70: 20 6f 66 20 73 72 63 69 64 2e 0a 2a 2a 0a 2a 2a   of srcid..**.**
3d80: 20 49 66 20 72 69 64 20 69 73 20 61 6c 72 65 61   If rid is alrea
3d90: 64 79 20 61 20 64 65 6c 74 61 20 66 72 6f 6d 20  dy a delta from 
3da0: 73 6f 6d 65 20 6f 74 68 65 72 20 70 6c 61 63 65  some other place
3db0: 20 74 68 65 6e 20 6e 6f 0a 2a 2a 20 63 6f 6e 76   then no.** conv
3dc0: 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 20 61 6e  ersion occurs an
3dd0: 64 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  d this is a no-o
3de0: 70 20 75 6e 6c 65 73 73 20 66 6f 72 63 65 3d 3d  p unless force==
3df0: 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 69  1..**.** If srci
3e00: 64 20 69 73 20 61 20 64 65 6c 74 61 20 74 68 61  d is a delta tha
3e10: 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 72 69 64  t depends on rid
3e20: 2c 20 74 68 65 6e 20 73 72 63 69 64 20 69 73 0a  , then srcid is.
3e30: 2a 2a 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  ** converted to 
3e40: 75 6e 64 65 6c 74 61 65 64 20 74 65 78 74 2e 0a  undeltaed text..
3e50: 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20  **.** If either 
3e60: 72 69 64 20 6f 72 20 73 72 63 69 64 20 63 6f 6e  rid or srcid con
3e70: 74 61 69 6e 20 6c 65 73 73 20 74 68 61 6e 20 35  tain less than 5
3e80: 30 20 62 79 74 65 73 2c 20 6f 72 20 69 66 20 74  0 bytes, or if t
3e90: 68 65 0a 2a 2a 20 72 65 73 75 6c 74 69 6e 67 20  he.** resulting 
3ea0: 64 65 6c 74 61 20 64 6f 65 73 20 6e 6f 74 20 61  delta does not a
3eb0: 63 68 69 65 76 65 20 61 20 63 6f 6d 70 72 65 73  chieve a compres
3ec0: 73 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74  sion of at least
3ed0: 20 32 35 25 20 6f 6e 0a 2a 2a 20 69 74 73 20 6f   25% on.** its o
3ee0: 77 6e 20 74 68 65 20 72 69 64 20 69 73 20 6c 65  wn the rid is le
3ef0: 66 74 20 75 6e 74 6f 75 63 68 65 64 2e 0a 2a 2a  ft untouched..**
3f00: 0a 2a 2a 20 4e 4f 54 45 3a 20 49 4d 48 4f 20 74  .** NOTE: IMHO t
3f10: 68 65 20 63 72 65 61 74 69 6f 6e 20 6f 66 20 74  he creation of t
3f20: 68 65 20 64 65 6c 74 61 20 73 68 6f 75 6c 64 20  he delta should 
3f30: 62 65 20 64 65 66 65 72 65 64 20 75 6e 74 69 6c  be defered until
3f40: 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 62 6c   after.** the bl
3f50: 6f 62 20 73 69 7a 65 73 20 68 61 76 65 20 62 65  ob sizes have be
3f60: 65 6e 20 63 68 65 63 6b 65 64 2e 20 44 6f 69 6e  en checked. Doin
3f70: 67 20 69 74 20 62 65 66 6f 72 65 20 74 68 65 20  g it before the 
3f80: 63 68 65 63 6b 20 61 73 20 69 73 0a 2a 2a 20 64  check as is.** d
3f90: 6f 6e 65 20 6e 6f 77 20 74 68 65 20 63 6f 64 65  one now the code
3fa0: 20 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 20 61   will generate a
3fb0: 20 64 65 6c 74 61 20 6a 75 73 74 20 74 6f 20 69   delta just to i
3fc0: 6d 6d 65 64 69 61 74 65 6c 79 20 74 68 72 6f 77  mmediately throw
3fd0: 0a 2a 2a 20 69 74 20 61 77 61 79 2c 20 77 61 73  .** it away, was
3fe0: 74 69 6e 67 20 73 70 61 63 65 20 61 6e 64 20 74  ting space and t
3ff0: 69 6d 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e  ime..*/.void con
4000: 74 65 6e 74 5f 64 65 6c 74 69 66 79 28 69 6e 74  tent_deltify(int
4010: 20 72 69 64 2c 20 69 6e 74 20 73 72 63 69 64 2c   rid, int srcid,
4020: 20 69 6e 74 20 66 6f 72 63 65 29 7b 0a 20 20 69   int force){.  i
4030: 6e 74 20 73 3b 0a 20 20 42 6c 6f 62 20 64 61 74  nt s;.  Blob dat
4040: 61 2c 20 73 72 63 2c 20 64 65 6c 74 61 3b 0a 20  a, src, delta;. 
4050: 20 53 74 6d 74 20 73 31 2c 20 73 32 3b 0a 20 20   Stmt s1, s2;.  
4060: 69 66 28 20 73 72 63 69 64 3d 3d 72 69 64 20 29  if( srcid==rid )
4070: 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21   return;.  if( !
4080: 66 6f 72 63 65 20 26 26 20 66 69 6e 64 53 72 63  force && findSrc
4090: 69 64 28 72 69 64 29 3e 30 20 29 20 72 65 74 75  id(rid)>0 ) retu
40a0: 72 6e 3b 0a 20 20 73 20 3d 20 73 72 63 69 64 3b  rn;.  s = srcid;
40b0: 0a 20 20 77 68 69 6c 65 28 20 28 73 20 3d 20 66  .  while( (s = f
40c0: 69 6e 64 53 72 63 69 64 28 73 29 29 3e 30 20 29  indSrcid(s))>0 )
40d0: 7b 0a 20 20 20 20 69 66 28 20 73 3d 3d 72 69 64  {.    if( s==rid
40e0: 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e   ){.      conten
40f0: 74 5f 75 6e 64 65 6c 74 61 28 73 72 63 69 64 29  t_undelta(srcid)
4100: 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
4110: 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 74 65     }.  }.  conte
4120: 6e 74 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73  nt_get(srcid, &s
4130: 72 63 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67  rc);.  content_g
4140: 65 74 28 72 69 64 2c 20 26 64 61 74 61 29 3b 0a  et(rid, &data);.
4150: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65    blob_delta_cre
4160: 61 74 65 28 26 73 72 63 2c 20 26 64 61 74 61 2c  ate(&src, &data,
4170: 20 26 64 65 6c 74 61 29 3b 0a 20 20 69 66 28 20   &delta);.  if( 
4180: 62 6c 6f 62 5f 73 69 7a 65 28 26 73 72 63 29 3e  blob_size(&src)>
4190: 3d 35 30 20 26 26 20 62 6c 6f 62 5f 73 69 7a 65  =50 && blob_size
41a0: 28 26 64 61 74 61 29 3e 3d 35 30 20 26 26 0a 20  (&data)>=50 &&. 
41b0: 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73            blob_s
41c0: 69 7a 65 28 26 64 65 6c 74 61 29 20 3c 20 62 6c  ize(&delta) < bl
41d0: 6f 62 5f 73 69 7a 65 28 26 64 61 74 61 29 2a 30  ob_size(&data)*0
41e0: 2e 37 35 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  .75 ){.    blob_
41f0: 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74 61 2c  compress(&delta,
4200: 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62   &delta);.    db
4210: 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20 22 55  _prepare(&s1, "U
4220: 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 63  PDATE blob SET c
4230: 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57 48 45  ontent=:data WHE
4240: 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29  RE rid=%d", rid)
4250: 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65  ;.    db_prepare
4260: 28 26 73 32 2c 20 22 52 45 50 4c 41 43 45 20 49  (&s2, "REPLACE I
4270: 4e 54 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72  NTO delta(rid,sr
4280: 63 69 64 29 56 41 4c 55 45 53 28 25 64 2c 25 64  cid)VALUES(%d,%d
4290: 29 22 2c 20 72 69 64 2c 20 73 72 63 69 64 29 3b  )", rid, srcid);
42a0: 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f  .    db_bind_blo
42b0: 62 28 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20  b(&s1, ":data", 
42c0: 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f  &delta);.    db_
42d0: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f  begin_transactio
42e0: 6e 28 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63  n();.    db_exec
42f0: 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 65 78  (&s1);.    db_ex
4300: 65 63 28 26 73 32 29 3b 0a 20 20 20 20 64 62 5f  ec(&s2);.    db_
4310: 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  end_transaction(
4320: 30 29 3b 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c  0);.    db_final
4330: 69 7a 65 28 26 73 31 29 3b 0a 20 20 20 20 64 62  ize(&s1);.    db
4340: 5f 66 69 6e 61 6c 69 7a 65 28 26 73 32 29 3b 0a  _finalize(&s2);.
4350: 20 20 20 20 76 65 72 69 66 79 5f 62 65 66 6f 72      verify_befor
4360: 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20  e_commit(rid);. 
4370: 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28   }.  blob_reset(
4380: 26 73 72 63 29 3b 0a 20 20 62 6c 6f 62 5f 72 65  &src);.  blob_re
4390: 73 65 74 28 26 64 61 74 61 29 3b 0a 20 20 62 6c  set(&data);.  bl
43a0: 6f 62 5f 72 65 73 65 74 28 26 64 65 6c 74 61 29  ob_reset(&delta)
43b0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41  ;.}../*.** COMMA
43c0: 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e  ND:  test-conten
43d0: 74 2d 64 65 6c 74 69 66 79 0a 2a 2a 0a 2a 2a 20  t-deltify.**.** 
43e0: 43 6f 6e 76 65 72 74 20 74 68 65 20 63 6f 6e 74  Convert the cont
43f0: 65 6e 74 20 61 74 20 52 49 44 20 69 6e 74 6f 20  ent at RID into 
4400: 61 20 64 65 6c 74 61 20 66 72 6f 6d 20 53 52 43  a delta from SRC
4410: 49 44 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74  ID..*/.void test
4420: 5f 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79  _content_deltify
4430: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 66  _cmd(void){.  if
4440: 28 20 67 2e 61 72 67 63 21 3d 35 20 29 20 75 73  ( g.argc!=5 ) us
4450: 61 67 65 28 22 52 49 44 20 53 52 43 49 44 20 46  age("RID SRCID F
4460: 4f 52 43 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73  ORCE");.  db_mus
4470: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65  t_be_within_tree
4480: 28 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 64 65  ();.  content_de
4490: 6c 74 69 66 79 28 61 74 6f 69 28 67 2e 61 72 67  ltify(atoi(g.arg
44a0: 76 5b 32 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72  v[2]), atoi(g.ar
44b0: 67 76 5b 33 5d 29 2c 20 61 74 6f 69 28 67 2e 61  gv[3]), atoi(g.a
44c0: 72 67 76 5b 34 5d 29 29 3b 0a 7d 0a              rgv[4]));.}.