Hex Artifact Content
Not logged in

Artifact c3fcdbc5dd02cce86e40bd7e51b434a8bdd98e56:

File src/content.c part of check-in [243e02bfbd] - Improvements to cloning performance. by drh on 2008-05-18 15:51:05.

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 20 20 69 66 28  ro(pBlob);.  if(
12c0: 20 72 69 64 3d 3d 30 20 29 20 72 65 74 75 72 6e   rid==0 ) return
12d0: 20 30 3b 0a 0a 20 20 2f 2a 20 45 61 72 6c 79 20   0;..  /* Early 
12e0: 6f 75 74 20 69 66 20 77 65 20 6b 6e 6f 77 20 74  out if we know t
12f0: 68 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f  he content is no
1300: 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20  t available */. 
1310: 20 69 66 28 20 62 61 67 5f 66 69 6e 64 28 26 63   if( bag_find(&c
1320: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69 73 73  ontentCache.miss
1330: 69 6e 67 2c 20 72 69 64 29 20 29 7b 0a 20 20 20  ing, rid) ){.   
1340: 20 43 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28 28   CONTENT_TRACE((
1350: 22 25 2a 73 6d 69 73 73 20 66 72 6f 6d 20 63 61  "%*smiss from ca
1360: 63 68 65 3a 20 25 64 5c 6e 22 2c 0a 20 20 20 20  che: %d\n",.    
1370: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1380: 62 61 67 5f 63 6f 75 6e 74 28 26 69 6e 50 72 6f  bag_count(&inPro
1390: 63 65 73 73 29 2c 20 22 22 2c 20 72 69 64 29 29  cess), "", rid))
13a0: 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
13b0: 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f   }..  /* Look fo
13c0: 72 20 74 68 65 20 61 72 74 69 66 61 63 74 20 69  r the artifact i
13d0: 6e 20 74 68 65 20 63 61 63 68 65 20 66 69 72 73  n the cache firs
13e0: 74 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t */.  for(i=0; 
13f0: 69 3c 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e  i<contentCache.n
1400: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
1410: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69  contentCache.a[i
1420: 5d 2e 72 69 64 3d 3d 72 69 64 20 29 7b 0a 20 20  ].rid==rid ){.  
1430: 20 20 20 20 2a 70 42 6c 6f 62 20 3d 20 63 6f 6e      *pBlob = con
1440: 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d 2e 63  tentCache.a[i].c
1450: 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 62 6c  ontent;.      bl
1460: 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74  ob_zero(&content
1470: 43 61 63 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74 65  Cache.a[i].conte
1480: 6e 74 29 3b 0a 20 20 20 20 20 20 63 6f 6e 74 65  nt);.      conte
1490: 6e 74 43 61 63 68 65 2e 6e 2d 2d 3b 0a 20 20 20  ntCache.n--;.   
14a0: 20 20 20 69 66 28 20 69 3c 63 6f 6e 74 65 6e 74     if( i<content
14b0: 43 61 63 68 65 2e 6e 20 29 7b 0a 20 20 20 20 20  Cache.n ){.     
14c0: 20 20 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e     contentCache.
14d0: 61 5b 69 5d 20 3d 20 63 6f 6e 74 65 6e 74 43 61  a[i] = contentCa
14e0: 63 68 65 2e 61 5b 63 6f 6e 74 65 6e 74 43 61 63  che.a[contentCac
14f0: 68 65 2e 6e 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  he.n];.      }. 
1500: 20 20 20 20 20 43 4f 4e 54 45 4e 54 5f 54 52 41       CONTENT_TRA
1510: 43 45 28 28 22 25 2a 73 68 69 74 20 63 61 63 68  CE(("%*shit cach
1520: 65 3a 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20  e: %d\n", .     
1530: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
1540: 61 67 5f 63 6f 75 6e 74 28 26 69 6e 50 72 6f 63  ag_count(&inProc
1550: 65 73 73 29 2c 20 22 22 2c 20 72 69 64 29 29 0a  ess), "", rid)).
1560: 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1570: 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1580: 53 65 65 20 69 66 20 77 65 20 6e 65 65 64 20 74  See if we need t
1590: 6f 20 61 70 70 6c 79 20 61 20 64 65 6c 74 61 20  o apply a delta 
15a0: 74 6f 20 66 69 6e 64 20 74 68 69 73 20 61 72 74  to find this art
15b0: 69 66 61 63 74 20 2a 2f 0a 20 20 73 72 63 69 64  ifact */.  srcid
15c0: 20 3d 20 66 69 6e 64 53 72 63 69 64 28 72 69 64   = findSrcid(rid
15d0: 29 3b 0a 20 20 43 4f 4e 54 45 4e 54 5f 54 52 41  );.  CONTENT_TRA
15e0: 43 45 28 28 22 25 2a 73 73 65 61 72 63 68 69 6e  CE(("%*ssearchin
15f0: 67 20 66 6f 72 20 25 64 2e 20 20 4e 65 65 64 20  g for %d.  Need 
1600: 25 64 2e 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  %d.\n",.        
1610: 20 20 20 20 20 20 20 20 20 62 61 67 5f 63 6f 75           bag_cou
1620: 6e 74 28 26 69 6e 50 72 6f 63 65 73 73 29 2c 20  nt(&inProcess), 
1630: 22 22 2c 20 72 69 64 2c 20 73 72 63 69 64 29 29  "", rid, srcid))
1640: 0a 0a 0a 20 20 69 66 28 20 73 72 63 69 64 20 29  ...  if( srcid )
1650: 7b 0a 20 20 20 20 2f 2a 20 59 65 73 2c 20 61 20  {.    /* Yes, a 
1660: 64 65 6c 74 61 20 69 73 20 72 65 71 75 69 72 65  delta is require
1670: 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 61 67  d */.    if( bag
1680: 5f 66 69 6e 64 28 26 69 6e 50 72 6f 63 65 73 73  _find(&inProcess
1690: 2c 20 73 72 63 69 64 29 20 29 7b 0a 20 20 20 20  , srcid) ){.    
16a0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
16b0: 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45  .        "UPDATE
16c0: 20 62 6c 6f 62 20 53 45 54 20 63 6f 6e 74 65 6e   blob SET conten
16d0: 74 3d 4e 55 4c 4c 2c 20 73 69 7a 65 3d 2d 31 20  t=NULL, size=-1 
16e0: 57 48 45 52 45 20 72 69 64 3d 25 64 3b 22 0a 20  WHERE rid=%d;". 
16f0: 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46         "DELETE F
1700: 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20  ROM delta WHERE 
1710: 72 69 64 3d 25 64 3b 22 0a 20 20 20 20 20 20 20  rid=%d;".       
1720: 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f   "INSERT OR IGNO
1730: 52 45 20 49 4e 54 4f 20 70 68 61 6e 74 6f 6d 20  RE INTO phantom 
1740: 56 41 4c 55 45 53 28 25 64 29 3b 22 2c 0a 20 20  VALUES(%d);",.  
1750: 20 20 20 20 20 20 73 72 63 69 64 2c 20 73 72 63        srcid, src
1760: 69 64 2c 20 73 72 63 69 64 0a 20 20 20 20 20 20  id, srcid.      
1770: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 7a 65  );.      blob_ze
1780: 72 6f 28 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20  ro(pBlob);.     
1790: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
17a0: 0a 20 20 20 20 62 61 67 5f 69 6e 73 65 72 74 28  .    bag_insert(
17b0: 26 69 6e 50 72 6f 63 65 73 73 2c 20 73 72 63 69  &inProcess, srci
17c0: 64 29 3b 0a 0a 20 20 20 20 69 66 28 20 63 6f 6e  d);..    if( con
17d0: 74 65 6e 74 5f 67 65 74 28 73 72 63 69 64 2c 20  tent_get(srcid, 
17e0: 26 73 72 63 29 20 29 7b 0a 20 20 20 20 20 20 64  &src) ){.      d
17f0: 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53  b_prepare(&q, "S
1800: 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74 20 46 52  ELECT content FR
1810: 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69  OM blob WHERE ri
1820: 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30  d=%d AND size>=0
1830: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20 69  ", rid);.      i
1840: 66 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d  f( db_step(&q)==
1850: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
1860: 20 20 20 20 20 20 42 6c 6f 62 20 64 65 6c 74 61        Blob delta
1870: 3b 0a 20 20 20 20 20 20 20 20 64 62 5f 65 70 68  ;.        db_eph
1880: 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20  emeral_blob(&q, 
1890: 30 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  0, &delta);.    
18a0: 20 20 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72      blob_uncompr
18b0: 65 73 73 28 26 64 65 6c 74 61 2c 20 26 64 65 6c  ess(&delta, &del
18c0: 74 61 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f  ta);.        blo
18d0: 62 5f 69 6e 69 74 28 70 42 6c 6f 62 2c 30 2c 30  b_init(pBlob,0,0
18e0: 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  );.        blob_
18f0: 64 65 6c 74 61 5f 61 70 70 6c 79 28 26 73 72 63  delta_apply(&src
1900: 2c 20 26 64 65 6c 74 61 2c 20 70 42 6c 6f 62 29  , &delta, pBlob)
1910: 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 72  ;.        blob_r
1920: 65 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20  eset(&delta);.  
1930: 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20        rc = 1;.  
1940: 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 5f 66      }.      db_f
1950: 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 0a 20 20  inalize(&q);..  
1960: 20 20 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20      /* Save the 
1970: 73 72 63 69 64 20 61 72 74 69 66 61 63 74 20 69  srcid artifact i
1980: 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20  n the cache */. 
1990: 20 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74       if( content
19a0: 43 61 63 68 65 2e 6e 3c 4d 58 5f 43 41 43 48 45  Cache.n<MX_CACHE
19b0: 5f 43 4e 54 20 29 7b 0a 20 20 20 20 20 20 20 20  _CNT ){.        
19c0: 69 20 3d 20 63 6f 6e 74 65 6e 74 43 61 63 68 65  i = contentCache
19d0: 2e 6e 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  .n++;.      }els
19e0: 65 20 69 66 28 20 28 28 63 6f 6e 74 65 6e 74 43  e if( ((contentC
19f0: 61 63 68 65 2e 73 6b 69 70 43 6e 74 2b 2b 29 25  ache.skipCnt++)%
1a00: 45 58 50 45 4c 4c 5f 49 4e 54 45 52 56 41 4c 29  EXPELL_INTERVAL)
1a10: 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  !=0 ){.        i
1a20: 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 65 6c   = -1;.      }el
1a30: 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  se{.        int 
1a40: 6a 2c 20 62 65 73 74 3b 0a 20 20 20 20 20 20 20  j, best;.       
1a50: 20 62 65 73 74 20 3d 20 63 6f 6e 74 65 6e 74 43   best = contentC
1a60: 61 63 68 65 2e 6e 65 78 74 41 67 65 2b 31 3b 0a  ache.nextAge+1;.
1a70: 20 20 20 20 20 20 20 20 69 20 3d 20 2d 31 3b 0a          i = -1;.
1a80: 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
1a90: 20 6a 3c 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e   j<contentCache.
1aa0: 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; j++){.       
1ab0: 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 43 61     if( contentCa
1ac0: 63 68 65 2e 61 5b 6a 5d 2e 61 67 65 3c 62 65 73  che.a[j].age<bes
1ad0: 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
1ae0: 20 69 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20   i = j;.        
1af0: 20 20 20 20 62 65 73 74 20 3d 20 63 6f 6e 74 65      best = conte
1b00: 6e 74 43 61 63 68 65 2e 61 5b 6a 5d 2e 61 67 65  ntCache.a[j].age
1b10: 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1b20: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1b30: 43 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28 28 22  CONTENT_TRACE(("
1b40: 25 2a 73 65 78 70 65 6c 6c 20 25 64 20 66 72 6f  %*sexpell %d fro
1b50: 6d 20 63 61 63 68 65 5c 6e 22 2c 0a 20 20 20 20  m cache\n",.    
1b60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b70: 20 20 20 62 61 67 5f 63 6f 75 6e 74 28 26 69 6e     bag_count(&in
1b80: 50 72 6f 63 65 73 73 29 2c 20 22 22 2c 20 63 6f  Process), "", co
1b90: 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d 2e  ntentCache.a[i].
1ba0: 72 69 64 29 29 0a 20 20 20 20 20 20 20 20 62 6c  rid)).        bl
1bb0: 6f 62 5f 72 65 73 65 74 28 26 63 6f 6e 74 65 6e  ob_reset(&conten
1bc0: 74 43 61 63 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74  tCache.a[i].cont
1bd0: 65 6e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ent);.      }.  
1be0: 20 20 20 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a      if( i>=0 ){.
1bf0: 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 43          contentC
1c00: 61 63 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74 65 6e  ache.a[i].conten
1c10: 74 20 3d 20 73 72 63 3b 0a 20 20 20 20 20 20 20  t = src;.       
1c20: 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b   contentCache.a[
1c30: 69 5d 2e 61 67 65 20 3d 20 63 6f 6e 74 65 6e 74  i].age = content
1c40: 43 61 63 68 65 2e 6e 65 78 74 41 67 65 2b 2b 3b  Cache.nextAge++;
1c50: 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74  .        content
1c60: 43 61 63 68 65 2e 61 5b 69 5d 2e 72 69 64 20 3d  Cache.a[i].rid =
1c70: 20 73 72 63 69 64 3b 0a 20 20 20 20 20 20 20 20   srcid;.        
1c80: 43 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28 28 22  CONTENT_TRACE(("
1c90: 25 2a 73 61 64 64 20 25 64 20 74 6f 20 63 61 63  %*sadd %d to cac
1ca0: 68 65 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  he\n",.         
1cb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 61                ba
1cc0: 67 5f 63 6f 75 6e 74 28 26 69 6e 50 72 6f 63 65  g_count(&inProce
1cd0: 73 73 29 2c 20 22 22 2c 20 73 72 63 69 64 29 29  ss), "", srcid))
1ce0: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1cf0: 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74        blob_reset
1d00: 28 26 73 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a  (&src);.      }.
1d10: 20 20 20 20 7d 0a 20 20 20 20 62 61 67 5f 72 65      }.    bag_re
1d20: 6d 6f 76 65 28 26 69 6e 50 72 6f 63 65 73 73 2c  move(&inProcess,
1d30: 20 73 72 63 69 64 29 3b 0a 20 20 7d 65 6c 73 65   srcid);.  }else
1d40: 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 64 65 6c 74  {.    /* No delt
1d50: 61 20 72 65 71 75 69 72 65 64 2e 20 20 52 65 61  a required.  Rea
1d60: 64 20 63 6f 6e 74 65 6e 74 20 64 69 72 65 63 74  d content direct
1d70: 6c 79 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ly from the data
1d80: 62 61 73 65 20 2a 2f 0a 20 20 20 20 64 62 5f 70  base */.    db_p
1d90: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
1da0: 43 54 20 63 6f 6e 74 65 6e 74 20 46 52 4f 4d 20  CT content FROM 
1db0: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25  blob WHERE rid=%
1dc0: 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20  d AND size>=0", 
1dd0: 72 69 64 29 3b 0a 20 20 20 20 69 66 28 20 64 62  rid);.    if( db
1de0: 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54  _step(&q)==SQLIT
1df0: 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 64  E_ROW ){.      d
1e00: 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c 6f 62  b_ephemeral_blob
1e10: 28 26 71 2c 20 30 2c 20 70 42 6c 6f 62 29 3b 0a  (&q, 0, pBlob);.
1e20: 20 20 20 20 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d        blob_uncom
1e30: 70 72 65 73 73 28 70 42 6c 6f 62 2c 20 70 42 6c  press(pBlob, pBl
1e40: 6f 62 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ob);.      rc = 
1e50: 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f  1;.    }.    db_
1e60: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20  finalize(&q);.  
1e70: 7d 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b  }.  if( rc==0 ){
1e80: 0a 20 20 20 20 62 61 67 5f 69 6e 73 65 72 74 28  .    bag_insert(
1e90: 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69  &contentCache.mi
1ea0: 73 73 69 6e 67 2c 20 72 69 64 29 3b 0a 20 20 7d  ssing, rid);.  }
1eb0: 65 6c 73 65 7b 0a 20 20 20 20 62 61 67 5f 69 6e  else{.    bag_in
1ec0: 73 65 72 74 28 26 63 6f 6e 74 65 6e 74 43 61 63  sert(&contentCac
1ed0: 68 65 2e 61 76 61 69 6c 61 62 6c 65 2c 20 72 69  he.available, ri
1ee0: 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  d);.  }.  return
1ef0: 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65   rc;.}../*.** Ge
1f00: 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  t the contents o
1f10: 66 20 61 20 66 69 6c 65 20 77 69 74 68 69 6e 20  f a file within 
1f20: 61 20 67 69 76 65 6e 20 62 61 73 65 6c 69 6e 65  a given baseline
1f30: 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e 74  ..*/.int content
1f40: 5f 67 65 74 5f 68 69 73 74 6f 72 69 63 61 6c 5f  _get_historical_
1f50: 66 69 6c 65 28 0a 20 20 63 6f 6e 73 74 20 63 68  file(.  const ch
1f60: 61 72 20 2a 72 65 76 69 73 69 6f 6e 2c 20 20 20  ar *revision,   
1f70: 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1f80: 62 61 73 65 6c 69 6e 65 20 63 6f 6e 74 61 69 6e  baseline contain
1f90: 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  ing the file */.
1fa0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69    const char *fi
1fb0: 6c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  le,        /* Na
1fc0: 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  me of the file *
1fd0: 2f 0a 20 20 42 6c 6f 62 20 2a 63 6f 6e 74 65 6e  /.  Blob *conten
1fe0: 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t            /* 
1ff0: 57 72 69 74 65 20 66 69 6c 65 20 63 6f 6e 74 65  Write file conte
2000: 6e 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  nt here */.){.  
2010: 42 6c 6f 62 20 6d 66 69 6c 65 3b 0a 20 20 4d 61  Blob mfile;.  Ma
2020: 6e 69 66 65 73 74 20 6d 3b 0a 20 20 69 6e 74 20  nifest m;.  int 
2030: 69 2c 20 72 69 64 3d 30 3b 0a 20 20 0a 20 20 72  i, rid=0;.  .  r
2040: 69 64 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64  id = name_to_rid
2050: 28 72 65 76 69 73 69 6f 6e 29 3b 0a 20 20 63 6f  (revision);.  co
2060: 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26  ntent_get(rid, &
2070: 6d 66 69 6c 65 29 3b 0a 20 20 0a 20 20 69 66 28  mfile);.  .  if(
2080: 20 6d 61 6e 69 66 65 73 74 5f 70 61 72 73 65 28   manifest_parse(
2090: 26 6d 2c 20 26 6d 66 69 6c 65 29 20 29 7b 0a 20  &m, &mfile) ){. 
20a0: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6d 2e     for(i=0; i<m.
20b0: 6e 46 69 6c 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  nFile; i++){.   
20c0: 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 6d 2e     if( strcmp(m.
20d0: 61 46 69 6c 65 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  aFile[i].zName, 
20e0: 66 69 6c 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  file)==0 ){.    
20f0: 20 20 20 20 72 69 64 20 3d 20 75 75 69 64 5f 74      rid = uuid_t
2100: 6f 5f 72 69 64 28 6d 2e 61 46 69 6c 65 5b 69 5d  o_rid(m.aFile[i]
2110: 2e 7a 55 75 69 64 2c 20 30 29 3b 0a 20 20 20 20  .zUuid, 0);.    
2120: 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 65      return conte
2130: 6e 74 5f 67 65 74 28 72 69 64 2c 20 63 6f 6e 74  nt_get(rid, cont
2140: 65 6e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ent);.      }.  
2150: 20 20 7d 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70    }.    fossil_p
2160: 61 6e 69 63 28 22 66 69 6c 65 3a 20 25 73 20 64  anic("file: %s d
2170: 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e  oes not exist in
2180: 20 72 65 76 69 73 69 6f 6e 3a 20 25 73 22 2c 20   revision: %s", 
2190: 66 69 6c 65 2c 20 72 65 76 69 73 69 6f 6e 29 3b  file, revision);
21a0: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f  .  }else{.    fo
21b0: 73 73 69 6c 5f 70 61 6e 69 63 28 22 63 6f 75 6c  ssil_panic("coul
21c0: 64 20 6e 6f 74 20 70 61 72 73 65 20 6d 61 6e 69  d not parse mani
21d0: 66 65 73 74 20 66 6f 72 20 72 65 76 69 73 69 6f  fest for revisio
21e0: 6e 3a 20 25 73 22 2c 20 72 65 76 69 73 69 6f 6e  n: %s", revision
21f0: 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 72 65 74 75  );.  }.  .  retu
2200: 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rn 0;.}../*.** C
2210: 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f  OMMAND:  test-co
2220: 6e 74 65 6e 74 2d 67 65 74 0a 2a 2a 0a 2a 2a 20  ntent-get.**.** 
2230: 45 78 74 72 61 63 74 20 61 20 62 6c 6f 62 20 66  Extract a blob f
2240: 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
2250: 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e   and write it in
2260: 74 6f 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f  to a file..*/.vo
2270: 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f  id test_content_
2280: 67 65 74 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  get_cmd(void){. 
2290: 20 69 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62   int rid;.  Blob
22a0: 20 63 6f 6e 74 65 6e 74 3b 0a 20 20 63 6f 6e 73   content;.  cons
22b0: 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20  t char *zFile;. 
22c0: 20 69 66 28 20 67 2e 61 72 67 63 21 3d 34 20 26   if( g.argc!=4 &
22d0: 26 20 67 2e 61 72 67 63 21 3d 33 20 29 20 75 73  & g.argc!=3 ) us
22e0: 61 67 65 28 22 52 45 43 4f 52 44 49 44 20 3f 46  age("RECORDID ?F
22f0: 49 4c 45 4e 41 4d 45 3f 22 29 3b 0a 20 20 7a 46  ILENAME?");.  zF
2300: 69 6c 65 20 3d 20 67 2e 61 72 67 63 3d 3d 34 20  ile = g.argc==4 
2310: 3f 20 67 2e 61 72 67 76 5b 33 5d 20 3a 20 22 2d  ? g.argv[3] : "-
2320: 22 3b 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f  ";.  db_must_be_
2330: 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20  within_tree();. 
2340: 20 72 69 64 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72   rid = name_to_r
2350: 69 64 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20  id(g.argv[2]);. 
2360: 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64   content_get(rid
2370: 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 62  , &content);.  b
2380: 6c 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c  lob_write_to_fil
2390: 65 28 26 63 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c  e(&content, zFil
23a0: 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d  e);.}../*.** COM
23b0: 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74  MAND:  test-cont
23c0: 65 6e 74 2d 72 61 77 67 65 74 0a 2a 2a 0a 2a 2a  ent-rawget.**.**
23d0: 20 45 78 74 72 61 63 74 20 61 20 62 6c 6f 62 20   Extract a blob 
23e0: 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
23f0: 65 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69  e and write it i
2400: 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 54 68 69  nto a file.  Thi
2410: 73 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 64 6f 65  s.** version doe
2420: 73 20 6e 6f 74 20 65 78 70 61 6e 64 20 74 68 65  s not expand the
2430: 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20   delta..*/.void 
2440: 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 72 61 77  test_content_raw
2450: 67 65 74 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  get_cmd(void){. 
2460: 20 69 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62   int rid;.  Blob
2470: 20 63 6f 6e 74 65 6e 74 3b 0a 20 20 63 6f 6e 73   content;.  cons
2480: 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20  t char *zFile;. 
2490: 20 69 66 28 20 67 2e 61 72 67 63 21 3d 34 20 26   if( g.argc!=4 &
24a0: 26 20 67 2e 61 72 67 63 21 3d 33 20 29 20 75 73  & g.argc!=3 ) us
24b0: 61 67 65 28 22 52 45 43 4f 52 44 49 44 20 3f 46  age("RECORDID ?F
24c0: 49 4c 45 4e 41 4d 45 3f 22 29 3b 0a 20 20 7a 46  ILENAME?");.  zF
24d0: 69 6c 65 20 3d 20 67 2e 61 72 67 63 3d 3d 34 20  ile = g.argc==4 
24e0: 3f 20 67 2e 61 72 67 76 5b 33 5d 20 3a 20 22 2d  ? g.argv[3] : "-
24f0: 22 3b 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f  ";.  db_must_be_
2500: 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20  within_tree();. 
2510: 20 72 69 64 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72   rid = name_to_r
2520: 69 64 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20  id(g.argv[2]);. 
2530: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74   blob_zero(&cont
2540: 65 6e 74 29 3b 0a 20 20 64 62 5f 62 6c 6f 62 28  ent);.  db_blob(
2550: 26 63 6f 6e 74 65 6e 74 2c 20 22 53 45 4c 45 43  &content, "SELEC
2560: 54 20 63 6f 6e 74 65 6e 74 20 46 52 4f 4d 20 62  T content FROM b
2570: 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25 64  lob WHERE rid=%d
2580: 22 2c 20 72 69 64 29 3b 0a 20 20 62 6c 6f 62 5f  ", rid);.  blob_
2590: 75 6e 63 6f 6d 70 72 65 73 73 28 26 63 6f 6e 74  uncompress(&cont
25a0: 65 6e 74 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a  ent, &content);.
25b0: 20 20 62 6c 6f 62 5f 77 72 69 74 65 5f 74 6f 5f    blob_write_to_
25c0: 66 69 6c 65 28 26 63 6f 6e 74 65 6e 74 2c 20 7a  file(&content, z
25d0: 46 69 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  File);.}../*.** 
25e0: 57 68 65 6e 20 61 20 72 65 63 6f 72 64 20 69 73  When a record is
25f0: 20 63 6f 6e 76 65 72 74 65 64 20 66 72 6f 6d 20   converted from 
2600: 61 20 70 68 61 6e 74 6f 6d 20 74 6f 20 61 20 72  a phantom to a r
2610: 65 61 6c 20 72 65 63 6f 72 64 2c 0a 2a 2a 20 69  eal record,.** i
2620: 66 20 74 68 61 74 20 72 65 63 6f 72 64 20 68 61  f that record ha
2630: 73 20 6f 74 68 65 72 20 72 65 63 6f 72 64 73 20  s other records 
2640: 74 68 61 74 20 61 72 65 20 64 65 72 69 76 65 64  that are derived
2650: 20 62 79 20 64 65 6c 74 61 2c 0a 2a 2a 20 74 68   by delta,.** th
2660: 65 6e 20 63 61 6c 6c 20 6d 61 6e 69 66 65 73 74  en call manifest
2670: 5f 63 72 6f 73 73 6c 69 6e 6b 28 29 20 6f 6e 20  _crosslink() on 
2680: 74 68 6f 73 65 20 6f 74 68 65 72 20 72 65 63 6f  those other reco
2690: 72 64 73 2e 0a 2a 2f 0a 76 6f 69 64 20 61 66 74  rds..*/.void aft
26a0: 65 72 5f 64 65 70 68 61 6e 74 6f 6d 69 7a 65 28  er_dephantomize(
26b0: 69 6e 74 20 72 69 64 2c 20 69 6e 74 20 6c 69 6e  int rid, int lin
26c0: 6b 46 6c 61 67 29 7b 0a 20 20 53 74 6d 74 20 71  kFlag){.  Stmt q
26d0: 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26  ;.  db_prepare(&
26e0: 71 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 46  q, "SELECT rid F
26f0: 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20  ROM delta WHERE 
2700: 73 72 63 69 64 3d 25 64 22 2c 20 72 69 64 29 3b  srcid=%d", rid);
2710: 0a 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65  .  while( db_ste
2720: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
2730: 57 20 29 7b 0a 20 20 20 20 69 6e 74 20 74 69 64  W ){.    int tid
2740: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74   = db_column_int
2750: 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 61 66 74  (&q, 0);.    aft
2760: 65 72 5f 64 65 70 68 61 6e 74 6f 6d 69 7a 65 28  er_dephantomize(
2770: 74 69 64 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 64  tid, 1);.  }.  d
2780: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a  b_finalize(&q);.
2790: 20 20 69 66 28 20 6c 69 6e 6b 46 6c 61 67 20 29    if( linkFlag )
27a0: 7b 0a 20 20 20 20 42 6c 6f 62 20 63 6f 6e 74 65  {.    Blob conte
27b0: 6e 74 3b 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f  nt;.    content_
27c0: 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e  get(rid, &conten
27d0: 74 29 3b 0a 20 20 20 20 6d 61 6e 69 66 65 73 74  t);.    manifest
27e0: 5f 63 72 6f 73 73 6c 69 6e 6b 28 72 69 64 2c 20  _crosslink(rid, 
27f0: 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 62  &content);.    b
2800: 6c 6f 62 5f 72 65 73 65 74 28 26 63 6f 6e 74 65  lob_reset(&conte
2810: 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  nt);.  }.}../*.*
2820: 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20  * Write content 
2830: 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
2840: 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72  e.  Return the r
2850: 65 63 6f 72 64 20 49 44 2e 20 20 49 66 20 74 68  ecord ID.  If th
2860: 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20  e.** content is 
2870: 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 64  already in the d
2880: 61 74 61 62 61 73 65 2c 20 6a 75 73 74 20 72 65  atabase, just re
2890: 74 75 72 6e 20 74 68 65 20 72 65 63 6f 72 64 20  turn the record 
28a0: 49 44 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63  ID..**.** If src
28b0: 49 64 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  Id is specified,
28c0: 20 74 68 65 6e 20 70 42 6c 6f 62 20 69 73 20 64   then pBlob is d
28d0: 65 6c 74 61 20 63 6f 6e 74 65 6e 74 20 66 72 6f  elta content fro
28e0: 6d 0a 2a 2a 20 74 68 65 20 73 72 63 49 64 20 72  m.** the srcId r
28f0: 65 63 6f 72 64 2e 20 20 73 72 63 49 64 20 6d 69  ecord.  srcId mi
2900: 67 68 74 20 62 65 20 61 20 70 68 61 6e 74 6f 6d  ght be a phantom
2910: 2e 0a 2a 2a 0a 2a 2a 20 7a 55 75 69 64 20 69 73  ..**.** zUuid is
2920: 20 74 68 65 20 55 55 49 44 20 6f 66 20 74 68 65   the UUID of the
2930: 20 61 72 74 69 66 61 63 74 2c 20 69 66 20 69 74   artifact, if it
2940: 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 20 20   is specified.  
2950: 57 68 65 6e 20 73 72 63 49 64 20 69 73 0a 2a 2a  When srcId is.**
2960: 20 73 70 65 63 69 66 69 65 64 20 74 68 65 6e 20   specified then 
2970: 7a 55 75 69 64 20 6d 75 73 74 20 61 6c 77 61 79  zUuid must alway
2980: 73 20 62 65 20 73 70 65 63 69 66 69 65 64 2e 20  s be specified. 
2990: 20 49 66 20 73 72 63 49 64 20 69 73 20 7a 65 72   If srcId is zer
29a0: 6f 2c 0a 2a 2a 20 61 6e 64 20 7a 55 75 69 64 20  o,.** and zUuid 
29b0: 69 73 20 7a 65 72 6f 20 74 68 65 6e 20 74 68 65  is zero then the
29c0: 20 63 6f 72 72 65 63 74 20 7a 55 75 69 64 20 69   correct zUuid i
29d0: 73 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d 20  s computed from 
29e0: 70 42 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  pBlob..**.** If 
29f0: 74 68 65 20 72 65 63 6f 72 64 20 61 6c 72 65 61  the record alrea
2a00: 64 79 20 65 78 69 73 74 73 20 62 75 74 20 69 73  dy exists but is
2a10: 20 61 20 70 68 61 6e 74 6f 6d 2c 20 74 68 65 20   a phantom, the 
2a20: 70 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 0a 2a 2a  pBlob content.**
2a30: 20 69 73 20 69 6e 73 65 72 74 65 64 20 61 6e 64   is inserted and
2a40: 20 74 68 65 20 70 68 61 74 6f 6d 20 62 65 63 6f   the phatom beco
2a50: 6d 65 73 20 61 20 72 65 61 6c 20 72 65 63 6f 72  mes a real recor
2a60: 64 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e  d..*/.int conten
2a70: 74 5f 70 75 74 28 42 6c 6f 62 20 2a 70 42 6c 6f  t_put(Blob *pBlo
2a80: 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2a90: 55 75 69 64 2c 20 69 6e 74 20 73 72 63 49 64 29  Uuid, int srcId)
2aa0: 7b 0a 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20  {.  int size;.  
2ab0: 69 6e 74 20 72 69 64 3b 0a 20 20 53 74 6d 74 20  int rid;.  Stmt 
2ac0: 73 31 3b 0a 20 20 42 6c 6f 62 20 63 6d 70 72 3b  s1;.  Blob cmpr;
2ad0: 0a 20 20 42 6c 6f 62 20 68 61 73 68 3b 0a 20 20  .  Blob hash;.  
2ae0: 69 6e 74 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73  int markAsUnclus
2af0: 74 65 72 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74  tered = 0;.  int
2b00: 20 69 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65 20   isDephantomize 
2b10: 3d 20 30 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  = 0;.  .  assert
2b20: 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70  ( g.repositoryOp
2b30: 65 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  en );.  assert( 
2b40: 70 42 6c 6f 62 21 3d 30 20 29 3b 0a 20 20 61 73  pBlob!=0 );.  as
2b50: 73 65 72 74 28 20 73 72 63 49 64 3d 3d 30 20 7c  sert( srcId==0 |
2b60: 7c 20 7a 55 75 69 64 21 3d 30 20 29 3b 0a 20 20  | zUuid!=0 );.  
2b70: 69 66 28 20 7a 55 75 69 64 3d 3d 30 20 29 7b 0a  if( zUuid==0 ){.
2b80: 20 20 20 20 61 73 73 65 72 74 28 20 70 42 6c 6f      assert( pBlo
2b90: 62 21 3d 30 20 29 3b 0a 20 20 20 20 73 68 61 31  b!=0 );.    sha1
2ba0: 73 75 6d 5f 62 6c 6f 62 28 70 42 6c 6f 62 2c 20  sum_blob(pBlob, 
2bb0: 26 68 61 73 68 29 3b 0a 20 20 7d 65 6c 73 65 7b  &hash);.  }else{
2bc0: 0a 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 26  .    blob_init(&
2bd0: 68 61 73 68 2c 20 7a 55 75 69 64 2c 20 2d 31 29  hash, zUuid, -1)
2be0: 3b 0a 20 20 7d 0a 20 20 73 69 7a 65 20 3d 20 62  ;.  }.  size = b
2bf0: 6c 6f 62 5f 73 69 7a 65 28 70 42 6c 6f 62 29 3b  lob_size(pBlob);
2c00: 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e  .  db_begin_tran
2c10: 73 61 63 74 69 6f 6e 28 29 3b 0a 0a 20 20 2f 2a  saction();..  /*
2c20: 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
2c30: 20 74 68 65 20 65 6e 74 72 79 20 61 6c 72 65 61   the entry alrea
2c40: 64 79 20 65 78 69 73 74 73 20 61 6e 64 20 69 66  dy exists and if
2c50: 20 69 74 20 64 6f 65 73 20 77 68 65 74 68 65 72   it does whether
2c60: 0a 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 74 68 65  .  ** or not the
2c70: 20 65 6e 74 72 79 20 69 73 20 61 20 70 68 61 6e   entry is a phan
2c80: 74 6f 6d 0a 20 20 2a 2f 0a 20 20 64 62 5f 70 72  tom.  */.  db_pr
2c90: 65 70 61 72 65 28 26 73 31 2c 20 22 53 45 4c 45  epare(&s1, "SELE
2ca0: 43 54 20 72 69 64 2c 20 73 69 7a 65 20 46 52 4f  CT rid, size FRO
2cb0: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69  M blob WHERE uui
2cc0: 64 3d 25 42 22 2c 20 26 68 61 73 68 29 3b 0a 20  d=%B", &hash);. 
2cd0: 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 73 31   if( db_step(&s1
2ce0: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
2cf0: 0a 20 20 20 20 72 69 64 20 3d 20 64 62 5f 63 6f  .    rid = db_co
2d00: 6c 75 6d 6e 5f 69 6e 74 28 26 73 31 2c 20 30 29  lumn_int(&s1, 0)
2d10: 3b 0a 20 20 20 20 69 66 28 20 64 62 5f 63 6f 6c  ;.    if( db_col
2d20: 75 6d 6e 5f 69 6e 74 28 26 73 31 2c 20 31 29 3e  umn_int(&s1, 1)>
2d30: 3d 30 20 7c 7c 20 70 42 6c 6f 62 3d 3d 30 20 29  =0 || pBlob==0 )
2d40: 7b 0a 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65  {.      /* Eithe
2d50: 72 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 6e  r the entry is n
2d60: 6f 74 20 61 20 70 68 61 6e 74 6f 6d 20 6f 72 20  ot a phantom or 
2d70: 69 74 20 69 73 20 61 20 70 68 61 6e 74 6f 6d 20  it is a phantom 
2d80: 62 75 74 20 77 65 0a 20 20 20 20 20 20 2a 2a 20  but we.      ** 
2d90: 68 61 76 65 20 6e 6f 20 64 61 74 61 20 77 69 74  have no data wit
2da0: 68 20 77 68 69 63 68 20 74 6f 20 64 65 70 68 61  h which to depha
2db0: 6e 74 6f 6d 69 7a 65 20 69 74 2e 20 20 49 6e 20  ntomize it.  In 
2dc0: 65 69 74 68 65 72 20 63 61 73 65 2c 0a 20 20 20  either case,.   
2dd0: 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e     ** there is n
2de0: 6f 74 68 69 6e 67 20 66 6f 72 20 75 73 20 74 6f  othing for us to
2df0: 20 64 6f 20 6f 74 68 65 72 20 74 68 61 6e 20 72   do other than r
2e00: 65 74 75 72 6e 20 74 68 65 20 52 49 44 2e 20 2a  eturn the RID. *
2e10: 2f 0a 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c  /.      db_final
2e20: 69 7a 65 28 26 73 31 29 3b 0a 20 20 20 20 20 20  ize(&s1);.      
2e30: 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  db_end_transacti
2e40: 6f 6e 28 30 29 3b 0a 20 20 20 20 20 20 72 65 74  on(0);.      ret
2e50: 75 72 6e 20 72 69 64 3b 0a 20 20 20 20 7d 0a 20  urn rid;.    }. 
2e60: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 69 64 20   }else{.    rid 
2e70: 3d 20 30 3b 20 20 2f 2a 20 4e 6f 20 65 6e 74 72  = 0;  /* No entr
2e80: 79 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  y with the same 
2e90: 55 55 49 44 20 63 75 72 72 65 6e 74 6c 79 20 65  UUID currently e
2ea0: 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 6d 61 72  xists */.    mar
2eb0: 6b 41 73 55 6e 63 6c 75 73 74 65 72 65 64 20 3d  kAsUnclustered =
2ec0: 20 31 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e   1;.  }.  db_fin
2ed0: 61 6c 69 7a 65 28 26 73 31 29 3b 0a 0a 20 20 2f  alize(&s1);..  /
2ee0: 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65  * Construct a re
2ef0: 63 65 69 76 65 64 2d 66 72 6f 6d 20 49 44 20 69  ceived-from ID i
2f00: 66 20 77 65 20 64 6f 20 6e 6f 74 20 61 6c 72 65  f we do not alre
2f10: 61 64 79 20 68 61 76 65 20 6f 6e 65 20 2a 2f 0a  ady have one */.
2f20: 20 20 69 66 28 20 67 2e 72 63 76 69 64 3d 3d 30    if( g.rcvid==0
2f30: 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69   ){.    db_multi
2f40: 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 22 49  _exec(.       "I
2f50: 4e 53 45 52 54 20 49 4e 54 4f 20 72 63 76 66 72  NSERT INTO rcvfr
2f60: 6f 6d 28 75 69 64 2c 20 6d 74 69 6d 65 2c 20 6e  om(uid, mtime, n
2f70: 6f 6e 63 65 2c 20 69 70 61 64 64 72 29 22 0a 20  once, ipaddr)". 
2f80: 20 20 20 20 20 20 22 56 41 4c 55 45 53 28 25 64        "VALUES(%d
2f90: 2c 20 6a 75 6c 69 61 6e 64 61 79 28 27 6e 6f 77  , julianday('now
2fa0: 27 29 2c 20 25 51 2c 20 25 51 29 22 2c 0a 20 20  '), %Q, %Q)",.  
2fb0: 20 20 20 20 20 67 2e 75 73 65 72 55 69 64 2c 20       g.userUid, 
2fc0: 67 2e 7a 4e 6f 6e 63 65 2c 20 67 2e 7a 49 70 41  g.zNonce, g.zIpA
2fd0: 64 64 72 0a 20 20 20 20 29 3b 0a 20 20 20 20 67  ddr.    );.    g
2fe0: 2e 72 63 76 69 64 20 3d 20 64 62 5f 6c 61 73 74  .rcvid = db_last
2ff0: 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 3b  _insert_rowid();
3000: 0a 20 20 7d 0a 0a 20 20 62 6c 6f 62 5f 63 6f 6d  .  }..  blob_com
3010: 70 72 65 73 73 28 70 42 6c 6f 62 2c 20 26 63 6d  press(pBlob, &cm
3020: 70 72 29 3b 0a 20 20 69 66 28 20 72 69 64 3e 30  pr);.  if( rid>0
3030: 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72   ){.    /* We ar
3040: 65 20 6a 75 73 74 20 61 64 64 69 6e 67 20 64 61  e just adding da
3050: 74 61 20 74 6f 20 61 20 70 68 61 6e 74 6f 6d 20  ta to a phantom 
3060: 2a 2f 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72  */.    db_prepar
3070: 65 28 26 73 31 2c 0a 20 20 20 20 20 20 22 55 50  e(&s1,.      "UP
3080: 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 72 63  DATE blob SET rc
3090: 76 69 64 3d 25 64 2c 20 73 69 7a 65 3d 25 64 2c  vid=%d, size=%d,
30a0: 20 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57   content=:data W
30b0: 48 45 52 45 20 72 69 64 3d 25 64 22 2c 0a 20 20  HERE rid=%d",.  
30c0: 20 20 20 20 20 67 2e 72 63 76 69 64 2c 20 73 69       g.rcvid, si
30d0: 7a 65 2c 20 72 69 64 0a 20 20 20 20 29 3b 0a 20  ze, rid.    );. 
30e0: 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28     db_bind_blob(
30f0: 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 63  &s1, ":data", &c
3100: 6d 70 72 29 3b 0a 20 20 20 20 64 62 5f 65 78 65  mpr);.    db_exe
3110: 63 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 6d  c(&s1);.    db_m
3120: 75 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54  ulti_exec("DELET
3130: 45 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 57  E FROM phantom W
3140: 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69  HERE rid=%d", ri
3150: 64 29 3b 0a 20 20 20 20 69 66 28 20 73 72 63 49  d);.    if( srcI
3160: 64 3d 3d 30 20 7c 7c 20 63 6f 6e 74 65 6e 74 5f  d==0 || content_
3170: 69 73 5f 61 76 61 69 6c 61 62 6c 65 28 73 72 63  is_available(src
3180: 49 64 29 20 29 7b 0a 20 20 20 20 20 20 69 73 44  Id) ){.      isD
3190: 65 70 68 61 6e 74 6f 6d 69 7a 65 20 3d 20 31 3b  ephantomize = 1;
31a0: 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 6d  .      content_m
31b0: 61 72 6b 5f 61 76 61 69 6c 61 62 6c 65 28 72 69  ark_available(ri
31c0: 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  d);.    }.  }els
31d0: 65 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65  e{.    /* We are
31e0: 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20   creating a new 
31f0: 65 6e 74 72 79 20 2a 2f 0a 20 20 20 20 64 62 5f  entry */.    db_
3200: 70 72 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20  prepare(&s1,.   
3210: 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
3220: 62 6c 6f 62 28 72 63 76 69 64 2c 73 69 7a 65 2c  blob(rcvid,size,
3230: 75 75 69 64 2c 63 6f 6e 74 65 6e 74 29 22 0a 20  uuid,content)". 
3240: 20 20 20 20 20 22 56 41 4c 55 45 53 28 25 64 2c       "VALUES(%d,
3250: 25 64 2c 27 25 62 27 2c 3a 64 61 74 61 29 22 2c  %d,'%b',:data)",
3260: 0a 20 20 20 20 20 20 20 67 2e 72 63 76 69 64 2c  .       g.rcvid,
3270: 20 73 69 7a 65 2c 20 26 68 61 73 68 0a 20 20 20   size, &hash.   
3280: 20 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f   );.    db_bind_
3290: 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61  blob(&s1, ":data
32a0: 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 64  ", &cmpr);.    d
32b0: 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20  b_exec(&s1);.   
32c0: 20 72 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69   rid = db_last_i
32d0: 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20  nsert_rowid();. 
32e0: 20 20 20 69 66 28 20 21 70 42 6c 6f 62 20 29 7b     if( !pBlob ){
32f0: 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  .      db_multi_
3300: 65 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20  exec("INSERT OR 
3310: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 68 61 6e  IGNORE INTO phan
3320: 74 6f 6d 20 56 41 4c 55 45 53 28 25 64 29 22 2c  tom VALUES(%d)",
3330: 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   rid);.    }.  }
3340: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63  .  blob_reset(&c
3350: 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  mpr);..  /* If t
3360: 68 65 20 73 72 63 49 64 20 69 73 20 73 70 65 63  he srcId is spec
3370: 69 66 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20  ified, then the 
3380: 64 61 74 61 20 77 65 20 6a 75 73 74 20 61 64 64  data we just add
3390: 65 64 20 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c  ed is.  ** reall
33a0: 79 20 61 20 64 65 6c 74 61 2e 20 20 52 65 63 6f  y a delta.  Reco
33b0: 72 64 20 74 68 69 73 20 66 61 63 74 20 69 6e 20  rd this fact in 
33c0: 74 68 65 20 64 65 6c 74 61 20 74 61 62 6c 65 2e  the delta table.
33d0: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 72 63 49  .  */.  if( srcI
33e0: 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74  d ){.    db_mult
33f0: 69 5f 65 78 65 63 28 22 52 45 50 4c 41 43 45 20  i_exec("REPLACE 
3400: 49 4e 54 4f 20 64 65 6c 74 61 28 72 69 64 2c 73  INTO delta(rid,s
3410: 72 63 69 64 29 20 56 41 4c 55 45 53 28 25 64 2c  rcid) VALUES(%d,
3420: 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63 49 64  %d)", rid, srcId
3430: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73  );.  }.  if( !is
3440: 44 65 70 68 61 6e 74 6f 6d 69 7a 65 20 26 26 20  Dephantomize && 
3450: 62 61 67 5f 66 69 6e 64 28 26 63 6f 6e 74 65 6e  bag_find(&conten
3460: 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c 20  tCache.missing, 
3470: 72 69 64 29 20 26 26 20 0a 20 20 20 20 20 20 28  rid) && .      (
3480: 73 72 63 49 64 3d 3d 30 20 7c 7c 20 63 6f 6e 74  srcId==0 || cont
3490: 65 6e 74 5f 69 73 5f 61 76 61 69 6c 61 62 6c 65  ent_is_available
34a0: 28 73 72 63 49 64 29 29 20 29 7b 0a 20 20 20 20  (srcId)) ){.    
34b0: 63 6f 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61 76 61  content_mark_ava
34c0: 69 6c 61 62 6c 65 28 72 69 64 29 3b 0a 20 20 7d  ilable(rid);.  }
34d0: 0a 20 20 69 66 28 20 69 73 44 65 70 68 61 6e 74  .  if( isDephant
34e0: 6f 6d 69 7a 65 20 29 7b 0a 20 20 20 20 61 66 74  omize ){.    aft
34f0: 65 72 5f 64 65 70 68 61 6e 74 6f 6d 69 7a 65 28  er_dephantomize(
3500: 72 69 64 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 0a  rid, 0);.  }.  .
3510: 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65 6c 65    /* Add the ele
3520: 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 6e 63 6c  ment to the uncl
3530: 75 73 74 65 72 65 64 20 74 61 62 6c 65 20 69 66  ustered table if
3540: 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e 0a   has never been.
3550: 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20    ** previously 
3560: 73 65 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  seen..  */.  if(
3570: 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74 65 72   markAsUncluster
3580: 65 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c  ed ){.    db_mul
3590: 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20  ti_exec("INSERT 
35a0: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 75  OR IGNORE INTO u
35b0: 6e 63 6c 75 73 74 65 72 65 64 20 56 41 4c 55 45  nclustered VALUE
35c0: 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a 20 20  S(%d)", rid);.  
35d0: 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 73 68 20 74  }..  /* Finish t
35e0: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  he transaction a
35f0: 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f 0a 20 20  nd cleanup */.  
3600: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29  db_finalize(&s1)
3610: 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73  ;.  db_end_trans
3620: 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 62 6c 6f  action(0);.  blo
3630: 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b 0a  b_reset(&hash);.
3640: 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72 72 61 6e  .  /* Make arran
3650: 67 65 6d 65 6e 74 73 20 74 6f 20 76 65 72 69 66  gements to verif
3660: 79 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20  y that the data 
3670: 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72 65 64  can be recovered
3680: 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 77 65 20  .  ** before we 
3690: 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 76 65 72 69  commit */.  veri
36a0: 66 79 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d 69 74  fy_before_commit
36b0: 28 72 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  (rid);.  return 
36c0: 72 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  rid;.}../*.** Cr
36d0: 65 61 74 65 20 61 20 6e 65 77 20 70 68 61 6e 74  eate a new phant
36e0: 6f 6d 20 77 69 74 68 20 74 68 65 20 67 69 76 65  om with the give
36f0: 6e 20 55 55 49 44 20 61 6e 64 20 72 65 74 75 72  n UUID and retur
3700: 6e 20 69 74 73 20 61 72 74 69 66 61 63 74 20 49  n its artifact I
3710: 44 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e  D..*/.int conten
3720: 74 5f 6e 65 77 28 63 6f 6e 73 74 20 63 68 61 72  t_new(const char
3730: 20 2a 7a 55 75 69 64 29 7b 0a 20 20 69 6e 74 20   *zUuid){.  int 
3740: 72 69 64 3b 0a 20 20 73 74 61 74 69 63 20 53 74  rid;.  static St
3750: 6d 74 20 73 31 2c 20 73 32 3b 0a 20 20 0a 20 20  mt s1, s2;.  .  
3760: 61 73 73 65 72 74 28 20 67 2e 72 65 70 6f 73 69  assert( g.reposi
3770: 74 6f 72 79 4f 70 65 6e 20 29 3b 0a 20 20 64 62  toryOpen );.  db
3780: 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69  _begin_transacti
3790: 6f 6e 28 29 3b 0a 20 20 64 62 5f 73 74 61 74 69  on();.  db_stati
37a0: 63 5f 70 72 65 70 61 72 65 28 26 73 31 2c 0a 20  c_prepare(&s1,. 
37b0: 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
37c0: 62 6c 6f 62 28 72 63 76 69 64 2c 73 69 7a 65 2c  blob(rcvid,size,
37d0: 75 75 69 64 2c 63 6f 6e 74 65 6e 74 29 22 0a 20  uuid,content)". 
37e0: 20 20 20 22 56 41 4c 55 45 53 28 30 2c 2d 31 2c     "VALUES(0,-1,
37f0: 3a 75 75 69 64 2c 4e 55 4c 4c 29 22 0a 20 20 29  :uuid,NULL)".  )
3800: 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 74 65 78 74  ;.  db_bind_text
3810: 28 26 73 31 2c 20 22 3a 75 75 69 64 22 2c 20 7a  (&s1, ":uuid", z
3820: 55 75 69 64 29 3b 0a 20 20 64 62 5f 65 78 65 63  Uuid);.  db_exec
3830: 28 26 73 31 29 3b 0a 20 20 72 69 64 20 3d 20 64  (&s1);.  rid = d
3840: 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  b_last_insert_ro
3850: 77 69 64 28 29 3b 0a 20 20 64 62 5f 73 74 61 74  wid();.  db_stat
3860: 69 63 5f 70 72 65 70 61 72 65 28 26 73 32 2c 0a  ic_prepare(&s2,.
3870: 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
3880: 20 70 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53 28   phantom VALUES(
3890: 3a 72 69 64 29 22 0a 20 20 29 3b 0a 20 20 64 62  :rid)".  );.  db
38a0: 5f 62 69 6e 64 5f 69 6e 74 28 26 73 32 2c 20 22  _bind_int(&s2, "
38b0: 3a 72 69 64 22 2c 20 72 69 64 29 3b 0a 20 20 64  :rid", rid);.  d
38c0: 62 5f 65 78 65 63 28 26 73 32 29 3b 0a 20 20 62  b_exec(&s2);.  b
38d0: 61 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e 74 65  ag_insert(&conte
38e0: 6e 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c  ntCache.missing,
38f0: 20 72 69 64 29 3b 0a 20 20 64 62 5f 65 6e 64 5f   rid);.  db_end_
3900: 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a  transaction(0);.
3910: 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a    return rid;.}.
3920: 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a  ../*.** COMMAND:
3930: 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 70    test-content-p
3940: 75 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74  ut.**.** Extract
3950: 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65   a blob from the
3960: 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 72   database and wr
3970: 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69  ite it into a fi
3980: 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74  le..*/.void test
3990: 5f 63 6f 6e 74 65 6e 74 5f 70 75 74 5f 63 6d 64  _content_put_cmd
39a0: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69  (void){.  int ri
39b0: 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e  d;.  Blob conten
39c0: 74 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21  t;.  if( g.argc!
39d0: 3d 33 20 29 20 75 73 61 67 65 28 22 46 49 4c 45  =3 ) usage("FILE
39e0: 4e 41 4d 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73  NAME");.  db_mus
39f0: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65  t_be_within_tree
3a00: 28 29 3b 0a 20 20 75 73 65 72 5f 73 65 6c 65 63  ();.  user_selec
3a10: 74 28 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61 64  t();.  blob_read
3a20: 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 63 6f 6e 74  _from_file(&cont
3a30: 65 6e 74 2c 20 67 2e 61 72 67 76 5b 32 5d 29 3b  ent, g.argv[2]);
3a40: 0a 20 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e 74  .  rid = content
3a50: 5f 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 30  _put(&content, 0
3a60: 2c 20 30 29 3b 0a 20 20 70 72 69 6e 74 66 28 22  , 0);.  printf("
3a70: 69 6e 73 65 72 74 65 64 20 61 73 20 72 65 63 6f  inserted as reco
3a80: 72 64 20 25 64 5c 6e 22 2c 20 72 69 64 29 3b 0a  rd %d\n", rid);.
3a90: 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
3aa0: 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61  re the content a
3ab0: 74 20 72 69 64 20 69 73 20 74 68 65 20 6f 72 69  t rid is the ori
3ac0: 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 61 6e  ginal content an
3ad0: 64 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 64 65  d is not a.** de
3ae0: 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e  lta..*/.void con
3af0: 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 69 6e 74  tent_undelta(int
3b00: 20 72 69 64 29 7b 0a 20 20 69 66 28 20 66 69 6e   rid){.  if( fin
3b10: 64 53 72 63 69 64 28 72 69 64 29 3e 30 20 29 7b  dSrcid(rid)>0 ){
3b20: 0a 20 20 20 20 42 6c 6f 62 20 78 3b 0a 20 20 20  .    Blob x;.   
3b30: 20 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74   if( content_get
3b40: 28 72 69 64 2c 20 26 78 29 20 29 7b 0a 20 20 20  (rid, &x) ){.   
3b50: 20 20 20 53 74 6d 74 20 73 3b 0a 20 20 20 20 20     Stmt s;.     
3b60: 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 2c 20   db_prepare(&s, 
3b70: 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54  "UPDATE blob SET
3b80: 20 63 6f 6e 74 65 6e 74 3d 3a 63 2c 20 73 69 7a   content=:c, siz
3b90: 65 3d 25 64 20 57 48 45 52 45 20 72 69 64 3d 25  e=%d WHERE rid=%
3ba0: 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d",.            
3bb0: 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 69           blob_si
3bc0: 7a 65 28 26 78 29 2c 20 72 69 64 29 3b 0a 20 20  ze(&x), rid);.  
3bd0: 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73      blob_compres
3be0: 73 28 26 78 2c 20 26 78 29 3b 0a 20 20 20 20 20  s(&x, &x);.     
3bf0: 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73   db_bind_blob(&s
3c00: 2c 20 22 3a 63 22 2c 20 26 78 29 3b 0a 20 20 20  , ":c", &x);.   
3c10: 20 20 20 64 62 5f 65 78 65 63 28 26 73 29 3b 0a     db_exec(&s);.
3c20: 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a        db_finaliz
3c30: 65 28 26 73 29 3b 0a 20 20 20 20 20 20 62 6c 6f  e(&s);.      blo
3c40: 62 5f 72 65 73 65 74 28 26 78 29 3b 0a 20 20 20  b_reset(&x);.   
3c50: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
3c60: 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 64 65  ("DELETE FROM de
3c70: 6c 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64  lta WHERE rid=%d
3c80: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20  ", rid);.    }. 
3c90: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d   }.}../*.** COMM
3ca0: 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65  AND:  test-conte
3cb0: 6e 74 2d 75 6e 64 65 6c 74 61 0a 2a 2a 0a 2a 2a  nt-undelta.**.**
3cc0: 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63   Make sure the c
3cd0: 6f 6e 74 65 6e 74 20 61 74 20 52 45 43 4f 52 44  ontent at RECORD
3ce0: 49 44 20 69 73 20 6e 6f 74 20 61 20 64 65 6c 74  ID is not a delt
3cf0: 61 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63  a.*/.void test_c
3d00: 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 5f 63  ontent_undelta_c
3d10: 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20  md(void){.  int 
3d20: 72 69 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67  rid;.  if( g.arg
3d30: 63 21 3d 32 20 29 20 75 73 61 67 65 28 22 52 45  c!=2 ) usage("RE
3d40: 43 4f 52 44 49 44 22 29 3b 0a 20 20 64 62 5f 6d  CORDID");.  db_m
3d50: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
3d60: 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 61 74  ee();.  rid = at
3d70: 6f 69 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20  oi(g.argv[2]);. 
3d80: 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61   content_undelta
3d90: 28 72 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (rid);.}../*.** 
3da0: 43 68 61 6e 67 65 20 74 68 65 20 73 74 6f 72 61  Change the stora
3db0: 67 65 20 6f 66 20 72 69 64 20 73 6f 20 74 68 61  ge of rid so tha
3dc0: 74 20 69 74 20 69 73 20 61 20 64 65 6c 74 61 20  t it is a delta 
3dd0: 6f 66 20 73 72 63 69 64 2e 0a 2a 2a 0a 2a 2a 20  of srcid..**.** 
3de0: 49 66 20 72 69 64 20 69 73 20 61 6c 72 65 61 64  If rid is alread
3df0: 79 20 61 20 64 65 6c 74 61 20 66 72 6f 6d 20 73  y a delta from s
3e00: 6f 6d 65 20 6f 74 68 65 72 20 70 6c 61 63 65 20  ome other place 
3e10: 74 68 65 6e 20 6e 6f 0a 2a 2a 20 63 6f 6e 76 65  then no.** conve
3e20: 72 73 69 6f 6e 20 6f 63 63 75 72 73 20 61 6e 64  rsion occurs and
3e30: 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   this is a no-op
3e40: 20 75 6e 6c 65 73 73 20 66 6f 72 63 65 3d 3d 31   unless force==1
3e50: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 69 64  ..**.** If srcid
3e60: 20 69 73 20 61 20 64 65 6c 74 61 20 74 68 61 74   is a delta that
3e70: 20 64 65 70 65 6e 64 73 20 6f 6e 20 72 69 64 2c   depends on rid,
3e80: 20 74 68 65 6e 20 73 72 63 69 64 20 69 73 0a 2a   then srcid is.*
3e90: 2a 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 75  * converted to u
3ea0: 6e 64 65 6c 74 61 65 64 20 74 65 78 74 2e 0a 2a  ndeltaed text..*
3eb0: 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 72  *.** If either r
3ec0: 69 64 20 6f 72 20 73 72 63 69 64 20 63 6f 6e 74  id or srcid cont
3ed0: 61 69 6e 20 6c 65 73 73 20 74 68 61 6e 20 35 30  ain less than 50
3ee0: 20 62 79 74 65 73 2c 20 6f 72 20 69 66 20 74 68   bytes, or if th
3ef0: 65 0a 2a 2a 20 72 65 73 75 6c 74 69 6e 67 20 64  e.** resulting d
3f00: 65 6c 74 61 20 64 6f 65 73 20 6e 6f 74 20 61 63  elta does not ac
3f10: 68 69 65 76 65 20 61 20 63 6f 6d 70 72 65 73 73  hieve a compress
3f20: 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20  ion of at least 
3f30: 32 35 25 20 6f 6e 0a 2a 2a 20 69 74 73 20 6f 77  25% on.** its ow
3f40: 6e 20 74 68 65 20 72 69 64 20 69 73 20 6c 65 66  n the rid is lef
3f50: 74 20 75 6e 74 6f 75 63 68 65 64 2e 0a 2a 2a 0a  t untouched..**.
3f60: 2a 2a 20 4e 4f 54 45 3a 20 49 4d 48 4f 20 74 68  ** NOTE: IMHO th
3f70: 65 20 63 72 65 61 74 69 6f 6e 20 6f 66 20 74 68  e creation of th
3f80: 65 20 64 65 6c 74 61 20 73 68 6f 75 6c 64 20 62  e delta should b
3f90: 65 20 64 65 66 65 72 65 64 20 75 6e 74 69 6c 20  e defered until 
3fa0: 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 62 6c 6f  after.** the blo
3fb0: 62 20 73 69 7a 65 73 20 68 61 76 65 20 62 65 65  b sizes have bee
3fc0: 6e 20 63 68 65 63 6b 65 64 2e 20 44 6f 69 6e 67  n checked. Doing
3fd0: 20 69 74 20 62 65 66 6f 72 65 20 74 68 65 20 63   it before the c
3fe0: 68 65 63 6b 20 61 73 20 69 73 0a 2a 2a 20 64 6f  heck as is.** do
3ff0: 6e 65 20 6e 6f 77 20 74 68 65 20 63 6f 64 65 20  ne now the code 
4000: 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 20 61 20  will generate a 
4010: 64 65 6c 74 61 20 6a 75 73 74 20 74 6f 20 69 6d  delta just to im
4020: 6d 65 64 69 61 74 65 6c 79 20 74 68 72 6f 77 0a  mediately throw.
4030: 2a 2a 20 69 74 20 61 77 61 79 2c 20 77 61 73 74  ** it away, wast
4040: 69 6e 67 20 73 70 61 63 65 20 61 6e 64 20 74 69  ing space and ti
4050: 6d 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74  me..*/.void cont
4060: 65 6e 74 5f 64 65 6c 74 69 66 79 28 69 6e 74 20  ent_deltify(int 
4070: 72 69 64 2c 20 69 6e 74 20 73 72 63 69 64 2c 20  rid, int srcid, 
4080: 69 6e 74 20 66 6f 72 63 65 29 7b 0a 20 20 69 6e  int force){.  in
4090: 74 20 73 3b 0a 20 20 42 6c 6f 62 20 64 61 74 61  t s;.  Blob data
40a0: 2c 20 73 72 63 2c 20 64 65 6c 74 61 3b 0a 20 20  , src, delta;.  
40b0: 53 74 6d 74 20 73 31 2c 20 73 32 3b 0a 20 20 69  Stmt s1, s2;.  i
40c0: 66 28 20 73 72 63 69 64 3d 3d 72 69 64 20 29 20  f( srcid==rid ) 
40d0: 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 66  return;.  if( !f
40e0: 6f 72 63 65 20 26 26 20 66 69 6e 64 53 72 63 69  orce && findSrci
40f0: 64 28 72 69 64 29 3e 30 20 29 20 72 65 74 75 72  d(rid)>0 ) retur
4100: 6e 3b 0a 20 20 73 20 3d 20 73 72 63 69 64 3b 0a  n;.  s = srcid;.
4110: 20 20 77 68 69 6c 65 28 20 28 73 20 3d 20 66 69    while( (s = fi
4120: 6e 64 53 72 63 69 64 28 73 29 29 3e 30 20 29 7b  ndSrcid(s))>0 ){
4130: 0a 20 20 20 20 69 66 28 20 73 3d 3d 72 69 64 20  .    if( s==rid 
4140: 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74  ){.      content
4150: 5f 75 6e 64 65 6c 74 61 28 73 72 63 69 64 29 3b  _undelta(srcid);
4160: 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
4170: 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e    }.  }.  conten
4180: 74 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73 72  t_get(srcid, &sr
4190: 63 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65  c);.  content_ge
41a0: 74 28 72 69 64 2c 20 26 64 61 74 61 29 3b 0a 20  t(rid, &data);. 
41b0: 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61   blob_delta_crea
41c0: 74 65 28 26 73 72 63 2c 20 26 64 61 74 61 2c 20  te(&src, &data, 
41d0: 26 64 65 6c 74 61 29 3b 0a 20 20 69 66 28 20 62  &delta);.  if( b
41e0: 6c 6f 62 5f 73 69 7a 65 28 26 73 72 63 29 3e 3d  lob_size(&src)>=
41f0: 35 30 20 26 26 20 62 6c 6f 62 5f 73 69 7a 65 28  50 && blob_size(
4200: 26 64 61 74 61 29 3e 3d 35 30 20 26 26 0a 20 20  &data)>=50 &&.  
4210: 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 69           blob_si
4220: 7a 65 28 26 64 65 6c 74 61 29 20 3c 20 62 6c 6f  ze(&delta) < blo
4230: 62 5f 73 69 7a 65 28 26 64 61 74 61 29 2a 30 2e  b_size(&data)*0.
4240: 37 35 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 63  75 ){.    blob_c
4250: 6f 6d 70 72 65 73 73 28 26 64 65 6c 74 61 2c 20  ompress(&delta, 
4260: 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f  &delta);.    db_
4270: 70 72 65 70 61 72 65 28 26 73 31 2c 20 22 55 50  prepare(&s1, "UP
4280: 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 63 6f  DATE blob SET co
4290: 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57 48 45 52  ntent=:data WHER
42a0: 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b  E rid=%d", rid);
42b0: 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28  .    db_prepare(
42c0: 26 73 32 2c 20 22 52 45 50 4c 41 43 45 20 49 4e  &s2, "REPLACE IN
42d0: 54 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72 63  TO delta(rid,src
42e0: 69 64 29 56 41 4c 55 45 53 28 25 64 2c 25 64 29  id)VALUES(%d,%d)
42f0: 22 2c 20 72 69 64 2c 20 73 72 63 69 64 29 3b 0a  ", rid, srcid);.
4300: 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62      db_bind_blob
4310: 28 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26  (&s1, ":data", &
4320: 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 62  delta);.    db_b
4330: 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e  egin_transaction
4340: 28 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28  ();.    db_exec(
4350: 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 65 78 65  &s1);.    db_exe
4360: 63 28 26 73 32 29 3b 0a 20 20 20 20 64 62 5f 65  c(&s2);.    db_e
4370: 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30  nd_transaction(0
4380: 29 3b 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69  );.    db_finali
4390: 7a 65 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f  ze(&s1);.    db_
43a0: 66 69 6e 61 6c 69 7a 65 28 26 73 32 29 3b 0a 20  finalize(&s2);. 
43b0: 20 20 20 76 65 72 69 66 79 5f 62 65 66 6f 72 65     verify_before
43c0: 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20 20  _commit(rid);.  
43d0: 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  }.  blob_reset(&
43e0: 73 72 63 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73  src);.  blob_res
43f0: 65 74 28 26 64 61 74 61 29 3b 0a 20 20 62 6c 6f  et(&data);.  blo
4400: 62 5f 72 65 73 65 74 28 26 64 65 6c 74 61 29 3b  b_reset(&delta);
4410: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e  .}../*.** COMMAN
4420: 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74  D:  test-content
4430: 2d 64 65 6c 74 69 66 79 0a 2a 2a 0a 2a 2a 20 43  -deltify.**.** C
4440: 6f 6e 76 65 72 74 20 74 68 65 20 63 6f 6e 74 65  onvert the conte
4450: 6e 74 20 61 74 20 52 49 44 20 69 6e 74 6f 20 61  nt at RID into a
4460: 20 64 65 6c 74 61 20 66 72 6f 6d 20 53 52 43 49   delta from SRCI
4470: 44 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f  D..*/.void test_
4480: 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 5f  content_deltify_
4490: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  cmd(void){.  if(
44a0: 20 67 2e 61 72 67 63 21 3d 35 20 29 20 75 73 61   g.argc!=5 ) usa
44b0: 67 65 28 22 52 49 44 20 53 52 43 49 44 20 46 4f  ge("RID SRCID FO
44c0: 52 43 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74  RCE");.  db_must
44d0: 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28  _be_within_tree(
44e0: 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 64 65 6c  );.  content_del
44f0: 74 69 66 79 28 61 74 6f 69 28 67 2e 61 72 67 76  tify(atoi(g.argv
4500: 5b 32 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67  [2]), atoi(g.arg
4510: 76 5b 33 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72  v[3]), atoi(g.ar
4520: 67 76 5b 34 5d 29 29 3b 0a 7d 0a                 gv[4]));.}.