Hex Artifact Content
Not logged in

Artifact ebd984372a2339b73a5e383f4d4f09b6687dab05:

File src/content.c part of check-in [02a584f7f5] - Add the --private option to the "fossil commit" command. This option creates a private branch which is never pushed. by drh on 2009-08-26 18:25:48.

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 20   rid);.    }.   
3340: 20 69 66 28 20 67 2e 6d 61 72 6b 50 72 69 76 61   if( g.markPriva
3350: 74 65 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 6d  te ){.      db_m
3360: 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52  ulti_exec("INSER
3370: 54 20 49 4e 54 4f 20 70 72 69 76 61 74 65 20 56  T INTO private V
3380: 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 29  ALUES(%d)", rid)
3390: 3b 0a 20 20 20 20 20 20 6d 61 72 6b 41 73 55 6e  ;.      markAsUn
33a0: 63 6c 75 73 74 65 72 65 64 20 3d 20 30 3b 0a 20  clustered = 0;. 
33b0: 20 20 20 7d 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f     }.  }.  blob_
33c0: 72 65 73 65 74 28 26 63 6d 70 72 29 3b 0a 0a 20  reset(&cmpr);.. 
33d0: 20 2f 2a 20 49 66 20 74 68 65 20 73 72 63 49 64   /* If the srcId
33e0: 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 74   is specified, t
33f0: 68 65 6e 20 74 68 65 20 64 61 74 61 20 77 65 20  hen the data we 
3400: 6a 75 73 74 20 61 64 64 65 64 20 69 73 0a 20 20  just added is.  
3410: 2a 2a 20 72 65 61 6c 6c 79 20 61 20 64 65 6c 74  ** really a delt
3420: 61 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 20  a.  Record this 
3430: 66 61 63 74 20 69 6e 20 74 68 65 20 64 65 6c 74  fact in the delt
3440: 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  a table..  */.  
3450: 69 66 28 20 73 72 63 49 64 20 29 7b 0a 20 20 20  if( srcId ){.   
3460: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
3470: 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 64 65 6c  REPLACE INTO del
3480: 74 61 28 72 69 64 2c 73 72 63 69 64 29 20 56 41  ta(rid,srcid) VA
3490: 4c 55 45 53 28 25 64 2c 25 64 29 22 2c 20 72 69  LUES(%d,%d)", ri
34a0: 64 2c 20 73 72 63 49 64 29 3b 0a 20 20 7d 0a 20  d, srcId);.  }. 
34b0: 20 69 66 28 20 21 69 73 44 65 70 68 61 6e 74 6f   if( !isDephanto
34c0: 6d 69 7a 65 20 26 26 20 62 61 67 5f 66 69 6e 64  mize && bag_find
34d0: 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d  (&contentCache.m
34e0: 69 73 73 69 6e 67 2c 20 72 69 64 29 20 26 26 20  issing, rid) && 
34f0: 0a 20 20 20 20 20 20 28 73 72 63 49 64 3d 3d 30  .      (srcId==0
3500: 20 7c 7c 20 63 6f 6e 74 65 6e 74 5f 69 73 5f 61   || content_is_a
3510: 76 61 69 6c 61 62 6c 65 28 73 72 63 49 64 29 29  vailable(srcId))
3520: 20 29 7b 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f   ){.    content_
3530: 6d 61 72 6b 5f 61 76 61 69 6c 61 62 6c 65 28 72  mark_available(r
3540: 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  id);.  }.  if( i
3550: 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65 20 29 7b  sDephantomize ){
3560: 0a 20 20 20 20 61 66 74 65 72 5f 64 65 70 68 61  .    after_depha
3570: 6e 74 6f 6d 69 7a 65 28 72 69 64 2c 20 30 29 3b  ntomize(rid, 0);
3580: 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 41 64 64  .  }.  .  /* Add
3590: 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20   the element to 
35a0: 74 68 65 20 75 6e 63 6c 75 73 74 65 72 65 64 20  the unclustered 
35b0: 74 61 62 6c 65 20 69 66 20 68 61 73 20 6e 65 76  table if has nev
35c0: 65 72 20 62 65 65 6e 0a 20 20 2a 2a 20 70 72 65  er been.  ** pre
35d0: 76 69 6f 75 73 6c 79 20 73 65 65 6e 2e 0a 20 20  viously seen..  
35e0: 2a 2f 0a 20 20 69 66 28 20 6d 61 72 6b 41 73 55  */.  if( markAsU
35f0: 6e 63 6c 75 73 74 65 72 65 64 20 29 7b 0a 20 20  nclustered ){.  
3600: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
3610: 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52  "INSERT OR IGNOR
3620: 45 20 49 4e 54 4f 20 75 6e 63 6c 75 73 74 65 72  E INTO uncluster
3630: 65 64 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20  ed VALUES(%d)", 
3640: 72 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rid);.  }..  /* 
3650: 46 69 6e 69 73 68 20 74 68 65 20 74 72 61 6e 73  Finish the trans
3660: 61 63 74 69 6f 6e 20 61 6e 64 20 63 6c 65 61 6e  action and clean
3670: 75 70 20 2a 2f 0a 20 20 64 62 5f 66 69 6e 61 6c  up */.  db_final
3680: 69 7a 65 28 26 73 31 29 3b 0a 20 20 64 62 5f 65  ize(&s1);.  db_e
3690: 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30  nd_transaction(0
36a0: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  );.  blob_reset(
36b0: 26 68 61 73 68 29 3b 0a 0a 20 20 2f 2a 20 4d 61  &hash);..  /* Ma
36c0: 6b 65 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20  ke arrangements 
36d0: 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74  to verify that t
36e0: 68 65 20 64 61 74 61 20 63 61 6e 20 62 65 20 72  he data can be r
36f0: 65 63 6f 76 65 72 65 64 0a 20 20 2a 2a 20 62 65  ecovered.  ** be
3700: 66 6f 72 65 20 77 65 20 63 6f 6d 6d 69 74 20 2a  fore we commit *
3710: 2f 0a 20 20 76 65 72 69 66 79 5f 62 65 66 6f 72  /.  verify_befor
3720: 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20  e_commit(rid);. 
3730: 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a 0a   return rid;.}..
3740: 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
3750: 65 77 20 70 68 61 6e 74 6f 6d 20 77 69 74 68 20  ew phantom with 
3760: 74 68 65 20 67 69 76 65 6e 20 55 55 49 44 20 61  the given UUID a
3770: 6e 64 20 72 65 74 75 72 6e 20 69 74 73 20 61 72  nd return its ar
3780: 74 69 66 61 63 74 20 49 44 2e 0a 2a 2f 0a 69 6e  tifact ID..*/.in
3790: 74 20 63 6f 6e 74 65 6e 74 5f 6e 65 77 28 63 6f  t content_new(co
37a0: 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 29  nst char *zUuid)
37b0: 7b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20 20 73  {.  int rid;.  s
37c0: 74 61 74 69 63 20 53 74 6d 74 20 73 31 2c 20 73  tatic Stmt s1, s
37d0: 32 2c 20 73 33 3b 0a 20 20 0a 20 20 61 73 73 65  2, s3;.  .  asse
37e0: 72 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79  rt( g.repository
37f0: 4f 70 65 6e 20 29 3b 0a 20 20 64 62 5f 62 65 67  Open );.  db_beg
3800: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
3810: 3b 0a 20 20 69 66 28 20 75 75 69 64 5f 69 73 5f  ;.  if( uuid_is_
3820: 73 68 75 6e 6e 65 64 28 7a 55 75 69 64 29 20 29  shunned(zUuid) )
3830: 7b 0a 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61  {.    db_end_tra
3840: 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20  nsaction(0);.   
3850: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
3860: 20 64 62 5f 73 74 61 74 69 63 5f 70 72 65 70 61   db_static_prepa
3870: 72 65 28 26 73 31 2c 0a 20 20 20 20 22 49 4e 53  re(&s1,.    "INS
3880: 45 52 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72 63  ERT INTO blob(rc
3890: 76 69 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63 6f  vid,size,uuid,co
38a0: 6e 74 65 6e 74 29 22 0a 20 20 20 20 22 56 41 4c  ntent)".    "VAL
38b0: 55 45 53 28 30 2c 2d 31 2c 3a 75 75 69 64 2c 4e  UES(0,-1,:uuid,N
38c0: 55 4c 4c 29 22 0a 20 20 29 3b 0a 20 20 64 62 5f  ULL)".  );.  db_
38d0: 62 69 6e 64 5f 74 65 78 74 28 26 73 31 2c 20 22  bind_text(&s1, "
38e0: 3a 75 75 69 64 22 2c 20 7a 55 75 69 64 29 3b 0a  :uuid", zUuid);.
38f0: 20 20 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a    db_exec(&s1);.
3900: 20 20 72 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f    rid = db_last_
3910: 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a  insert_rowid();.
3920: 20 20 64 62 5f 73 74 61 74 69 63 5f 70 72 65 70    db_static_prep
3930: 61 72 65 28 26 73 32 2c 0a 20 20 20 20 22 49 4e  are(&s2,.    "IN
3940: 53 45 52 54 20 49 4e 54 4f 20 70 68 61 6e 74 6f  SERT INTO phanto
3950: 6d 20 56 41 4c 55 45 53 28 3a 72 69 64 29 22 0a  m VALUES(:rid)".
3960: 20 20 29 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 69    );.  db_bind_i
3970: 6e 74 28 26 73 32 2c 20 22 3a 72 69 64 22 2c 20  nt(&s2, ":rid", 
3980: 72 69 64 29 3b 0a 20 20 64 62 5f 65 78 65 63 28  rid);.  db_exec(
3990: 26 73 32 29 3b 0a 20 20 69 66 28 20 67 2e 6d 61  &s2);.  if( g.ma
39a0: 72 6b 50 72 69 76 61 74 65 20 29 7b 0a 20 20 20  rkPrivate ){.   
39b0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
39c0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 72 69 76  INSERT INTO priv
39d0: 61 74 65 20 56 41 4c 55 45 53 28 25 64 29 22 2c  ate VALUES(%d)",
39e0: 20 72 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a   rid);.  }else{.
39f0: 20 20 20 20 64 62 5f 73 74 61 74 69 63 5f 70 72      db_static_pr
3a00: 65 70 61 72 65 28 26 73 33 2c 0a 20 20 20 20 20  epare(&s3,.     
3a10: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 75 6e   "INSERT INTO un
3a20: 63 6c 75 73 74 65 72 65 64 20 56 41 4c 55 45 53  clustered VALUES
3a30: 28 3a 72 69 64 29 22 0a 20 20 20 20 29 3b 0a 20  (:rid)".    );. 
3a40: 20 20 20 64 62 5f 62 69 6e 64 5f 69 6e 74 28 26     db_bind_int(&
3a50: 73 33 2c 20 22 3a 72 69 64 22 2c 20 72 69 64 29  s3, ":rid", rid)
3a60: 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 73  ;.    db_exec(&s
3a70: 33 29 3b 0a 20 20 7d 0a 20 20 62 61 67 5f 69 6e  3);.  }.  bag_in
3a80: 73 65 72 74 28 26 63 6f 6e 74 65 6e 74 43 61 63  sert(&contentCac
3a90: 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64 29  he.missing, rid)
3aa0: 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73  ;.  db_end_trans
3ab0: 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 72 65 74  action(0);.  ret
3ac0: 75 72 6e 20 72 69 64 3b 0a 7d 0a 0a 0a 2f 2a 0a  urn rid;.}.../*.
3ad0: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73  ** COMMAND:  tes
3ae0: 74 2d 63 6f 6e 74 65 6e 74 2d 70 75 74 0a 2a 2a  t-content-put.**
3af0: 0a 2a 2a 20 45 78 74 72 61 63 74 20 61 20 62 6c  .** Extract a bl
3b00: 6f 62 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ob from the data
3b10: 62 61 73 65 20 61 6e 64 20 77 72 69 74 65 20 69  base and write i
3b20: 74 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 0a 2a  t into a file..*
3b30: 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74  /.void test_cont
3b40: 65 6e 74 5f 70 75 74 5f 63 6d 64 28 76 6f 69 64  ent_put_cmd(void
3b50: 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20 20  ){.  int rid;.  
3b60: 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a 20 20  Blob content;.  
3b70: 69 66 28 20 67 2e 61 72 67 63 21 3d 33 20 29 20  if( g.argc!=3 ) 
3b80: 75 73 61 67 65 28 22 46 49 4c 45 4e 41 4d 45 22  usage("FILENAME"
3b90: 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f  );.  db_must_be_
3ba0: 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20  within_tree();. 
3bb0: 20 75 73 65 72 5f 73 65 6c 65 63 74 28 29 3b 0a   user_select();.
3bc0: 20 20 62 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d    blob_read_from
3bd0: 5f 66 69 6c 65 28 26 63 6f 6e 74 65 6e 74 2c 20  _file(&content, 
3be0: 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 72 69  g.argv[2]);.  ri
3bf0: 64 20 3d 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28  d = content_put(
3c00: 26 63 6f 6e 74 65 6e 74 2c 20 30 2c 20 30 29 3b  &content, 0, 0);
3c10: 0a 20 20 70 72 69 6e 74 66 28 22 69 6e 73 65 72  .  printf("inser
3c20: 74 65 64 20 61 73 20 72 65 63 6f 72 64 20 25 64  ted as record %d
3c30: 5c 6e 22 2c 20 72 69 64 29 3b 0a 7d 0a 0a 2f 2a  \n", rid);.}../*
3c40: 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
3c50: 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 72 69 64  e content at rid
3c60: 20 69 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   is the original
3c70: 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 69 73 20   content and is 
3c80: 6e 6f 74 20 61 0a 2a 2a 20 64 65 6c 74 61 2e 0a  not a.** delta..
3c90: 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74 65 6e 74 5f  */.void content_
3ca0: 75 6e 64 65 6c 74 61 28 69 6e 74 20 72 69 64 29  undelta(int rid)
3cb0: 7b 0a 20 20 69 66 28 20 66 69 6e 64 53 72 63 69  {.  if( findSrci
3cc0: 64 28 72 69 64 29 3e 30 20 29 7b 0a 20 20 20 20  d(rid)>0 ){.    
3cd0: 42 6c 6f 62 20 78 3b 0a 20 20 20 20 69 66 28 20  Blob x;.    if( 
3ce0: 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c  content_get(rid,
3cf0: 20 26 78 29 20 29 7b 0a 20 20 20 20 20 20 53 74   &x) ){.      St
3d00: 6d 74 20 73 3b 0a 20 20 20 20 20 20 64 62 5f 70  mt s;.      db_p
3d10: 72 65 70 61 72 65 28 26 73 2c 20 22 55 50 44 41  repare(&s, "UPDA
3d20: 54 45 20 62 6c 6f 62 20 53 45 54 20 63 6f 6e 74  TE blob SET cont
3d30: 65 6e 74 3d 3a 63 2c 20 73 69 7a 65 3d 25 64 20  ent=:c, size=%d 
3d40: 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 0a 20  WHERE rid=%d",. 
3d50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d60: 20 20 20 20 62 6c 6f 62 5f 73 69 7a 65 28 26 78      blob_size(&x
3d70: 29 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20 62  ), rid);.      b
3d80: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 26 78 2c  lob_compress(&x,
3d90: 20 26 78 29 3b 0a 20 20 20 20 20 20 64 62 5f 62   &x);.      db_b
3da0: 69 6e 64 5f 62 6c 6f 62 28 26 73 2c 20 22 3a 63  ind_blob(&s, ":c
3db0: 22 2c 20 26 78 29 3b 0a 20 20 20 20 20 20 64 62  ", &x);.      db
3dc0: 5f 65 78 65 63 28 26 73 29 3b 0a 20 20 20 20 20  _exec(&s);.     
3dd0: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 29   db_finalize(&s)
3de0: 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 73  ;.      blob_res
3df0: 65 74 28 26 78 29 3b 0a 20 20 20 20 20 20 64 62  et(&x);.      db
3e00: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 44 45 4c  _multi_exec("DEL
3e10: 45 54 45 20 46 52 4f 4d 20 64 65 6c 74 61 20 57  ETE FROM delta W
3e20: 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69  HERE rid=%d", ri
3e30: 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  d);.    }.  }.}.
3e40: 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20  ./*.** COMMAND: 
3e50: 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 75 6e   test-content-un
3e60: 64 65 6c 74 61 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65  delta.**.** Make
3e70: 20 73 75 72 65 20 74 68 65 20 63 6f 6e 74 65 6e   sure the conten
3e80: 74 20 61 74 20 52 45 43 4f 52 44 49 44 20 69 73  t at RECORDID is
3e90: 20 6e 6f 74 20 61 20 64 65 6c 74 61 0a 2a 2f 0a   not a delta.*/.
3ea0: 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e  void test_conten
3eb0: 74 5f 75 6e 64 65 6c 74 61 5f 63 6d 64 28 76 6f  t_undelta_cmd(vo
3ec0: 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b 0a  id){.  int rid;.
3ed0: 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 32 20    if( g.argc!=2 
3ee0: 29 20 75 73 61 67 65 28 22 52 45 43 4f 52 44 49  ) usage("RECORDI
3ef0: 44 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f 62  D");.  db_must_b
3f00: 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b  e_within_tree();
3f10: 0a 20 20 72 69 64 20 3d 20 61 74 6f 69 28 67 2e  .  rid = atoi(g.
3f20: 61 72 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e 74  argv[2]);.  cont
3f30: 65 6e 74 5f 75 6e 64 65 6c 74 61 28 72 69 64 29  ent_undelta(rid)
3f40: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
3f50: 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 67 69  n true if the gi
3f60: 76 65 6e 20 52 49 44 20 69 73 20 6d 61 72 6b 65  ven RID is marke
3f70: 64 20 61 73 20 50 52 49 56 41 54 45 2e 0a 2a 2f  d as PRIVATE..*/
3f80: 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 69 73 5f  .int content_is_
3f90: 70 72 69 76 61 74 65 28 69 6e 74 20 72 69 64 29  private(int rid)
3fa0: 7b 0a 20 20 73 74 61 74 69 63 20 53 74 6d 74 20  {.  static Stmt 
3fb0: 73 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  s1;.  int rc;.  
3fc0: 64 62 5f 73 74 61 74 69 63 5f 70 72 65 70 61 72  db_static_prepar
3fd0: 65 28 26 73 31 2c 0a 20 20 20 20 22 53 45 4c 45  e(&s1,.    "SELE
3fe0: 43 54 20 31 20 46 52 4f 4d 20 70 72 69 76 61 74  CT 1 FROM privat
3ff0: 65 20 57 48 45 52 45 20 72 69 64 3d 3a 72 69 64  e WHERE rid=:rid
4000: 22 0a 20 20 29 3b 0a 20 20 64 62 5f 62 69 6e 64  ".  );.  db_bind
4010: 5f 69 6e 74 28 26 73 31 2c 20 22 3a 72 69 64 22  _int(&s1, ":rid"
4020: 2c 20 72 69 64 29 3b 0a 20 20 72 63 20 3d 20 64  , rid);.  rc = d
4030: 62 5f 73 74 65 70 28 26 73 31 29 3b 0a 20 20 64  b_step(&s1);.  d
4040: 62 5f 72 65 73 65 74 28 26 73 31 29 3b 0a 20 20  b_reset(&s1);.  
4050: 72 65 74 75 72 6e 20 72 63 3d 3d 53 51 4c 49 54  return rc==SQLIT
4060: 45 5f 52 4f 57 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a  E_ROW;  .}../*.*
4070: 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6e 20 61  * Make sure an a
4080: 72 74 69 66 61 63 74 20 69 73 20 70 75 62 6c 69  rtifact is publi
4090: 63 2e 20 20 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e  c.  .*/.void con
40a0: 74 65 6e 74 5f 6d 61 6b 65 5f 70 75 62 6c 69 63  tent_make_public
40b0: 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 73 74 61  (int rid){.  sta
40c0: 74 69 63 20 53 74 6d 74 20 73 31 3b 0a 20 20 64  tic Stmt s1;.  d
40d0: 62 5f 73 74 61 74 69 63 5f 70 72 65 70 61 72 65  b_static_prepare
40e0: 28 26 73 31 2c 0a 20 20 20 20 22 44 45 4c 45 54  (&s1,.    "DELET
40f0: 45 20 46 52 4f 4d 20 70 72 69 76 61 74 65 20 57  E FROM private W
4100: 48 45 52 45 20 72 69 64 3d 3a 72 69 64 22 0a 20  HERE rid=:rid". 
4110: 20 29 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 69 6e   );.  db_bind_in
4120: 74 28 26 73 31 2c 20 22 3a 72 69 64 22 2c 20 72  t(&s1, ":rid", r
4130: 69 64 29 3b 0a 20 20 64 62 5f 65 78 65 63 28 26  id);.  db_exec(&
4140: 73 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  s1);.}../*.** Ch
4150: 61 6e 67 65 20 74 68 65 20 73 74 6f 72 61 67 65  ange the storage
4160: 20 6f 66 20 72 69 64 20 73 6f 20 74 68 61 74 20   of rid so that 
4170: 69 74 20 69 73 20 61 20 64 65 6c 74 61 20 6f 66  it is a delta of
4180: 20 73 72 63 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66   srcid..**.** If
4190: 20 72 69 64 20 69 73 20 61 6c 72 65 61 64 79 20   rid is already 
41a0: 61 20 64 65 6c 74 61 20 66 72 6f 6d 20 73 6f 6d  a delta from som
41b0: 65 20 6f 74 68 65 72 20 70 6c 61 63 65 20 74 68  e other place th
41c0: 65 6e 20 6e 6f 0a 2a 2a 20 63 6f 6e 76 65 72 73  en no.** convers
41d0: 69 6f 6e 20 6f 63 63 75 72 73 20 61 6e 64 20 74  ion occurs and t
41e0: 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75  his is a no-op u
41f0: 6e 6c 65 73 73 20 66 6f 72 63 65 3d 3d 31 2e 0a  nless force==1..
4200: 2a 2a 0a 2a 2a 20 4e 65 76 65 72 20 67 65 6e 65  **.** Never gene
4210: 72 61 74 65 20 61 20 64 65 6c 74 61 20 74 68 61  rate a delta tha
4220: 74 20 63 61 72 72 69 65 73 20 61 20 70 72 69 76  t carries a priv
4230: 61 74 65 20 61 72 74 69 66 61 63 74 20 69 6e 74  ate artifact int
4240: 6f 20 61 20 70 75 62 6c 69 63 0a 2a 2a 20 61 72  o a public.** ar
4250: 74 69 66 61 63 74 2e 20 20 4f 74 68 65 72 77 69  tifact.  Otherwi
4260: 73 65 2c 20 77 68 65 6e 20 77 65 20 67 6f 20 74  se, when we go t
4270: 6f 20 73 65 6e 64 20 74 68 65 20 70 75 62 6c 69  o send the publi
4280: 63 20 61 72 74 69 66 61 63 74 20 6f 6e 20 61 0a  c artifact on a.
4290: 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f  ** sync operatio
42a0: 6e 2c 20 74 68 65 20 6f 74 68 65 72 20 65 6e 64  n, the other end
42b0: 20 6f 66 20 74 68 65 20 73 79 6e 63 20 77 69 6c   of the sync wil
42c0: 6c 20 6e 65 76 65 72 20 62 65 20 61 62 6c 65 20  l never be able 
42d0: 74 6f 20 72 65 63 65 69 76 65 0a 2a 2a 20 74 68  to receive.** th
42e0: 65 20 73 6f 75 72 63 65 20 6f 66 20 74 68 65 20  e source of the 
42f0: 64 65 6c 74 61 2e 20 20 49 74 20 69 73 20 4f 4b  delta.  It is OK
4300: 20 74 6f 20 64 65 6c 74 61 20 70 72 69 76 61 74   to delta privat
4310: 65 2d 3e 70 72 69 76 61 74 65 20 61 6e 64 0a 2a  e->private and.*
4320: 2a 20 70 75 62 6c 69 63 2d 3e 70 72 69 76 61 74  * public->privat
4330: 65 20 61 6e 64 20 70 75 62 6c 69 63 2d 3e 70 75  e and public->pu
4340: 62 6c 69 63 2e 20 20 4a 75 73 74 20 6e 6f 20 70  blic.  Just no p
4350: 72 69 76 61 74 65 2d 3e 70 75 62 6c 69 63 20 64  rivate->public d
4360: 65 6c 74 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  elta..**.** If s
4370: 72 63 69 64 20 69 73 20 61 20 64 65 6c 74 61 20  rcid is a delta 
4380: 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f 6e 20  that depends on 
4390: 72 69 64 2c 20 74 68 65 6e 20 73 72 63 69 64 20  rid, then srcid 
43a0: 69 73 0a 2a 2a 20 63 6f 6e 76 65 72 74 65 64 20  is.** converted 
43b0: 74 6f 20 75 6e 64 65 6c 74 61 65 64 20 74 65 78  to undeltaed tex
43c0: 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68  t..**.** If eith
43d0: 65 72 20 72 69 64 20 6f 72 20 73 72 63 69 64 20  er rid or srcid 
43e0: 63 6f 6e 74 61 69 6e 20 6c 65 73 73 20 74 68 61  contain less tha
43f0: 6e 20 35 30 20 62 79 74 65 73 2c 20 6f 72 20 69  n 50 bytes, or i
4400: 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 69  f the.** resulti
4410: 6e 67 20 64 65 6c 74 61 20 64 6f 65 73 20 6e 6f  ng delta does no
4420: 74 20 61 63 68 69 65 76 65 20 61 20 63 6f 6d 70  t achieve a comp
4430: 72 65 73 73 69 6f 6e 20 6f 66 20 61 74 20 6c 65  ression of at le
4440: 61 73 74 20 32 35 25 20 6f 6e 0a 2a 2a 20 69 74  ast 25% on.** it
4450: 73 20 6f 77 6e 20 74 68 65 20 72 69 64 20 69 73  s own the rid is
4460: 20 6c 65 66 74 20 75 6e 74 6f 75 63 68 65 64 2e   left untouched.
4470: 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74 65 6e 74  .*/.void content
4480: 5f 64 65 6c 74 69 66 79 28 69 6e 74 20 72 69 64  _deltify(int rid
4490: 2c 20 69 6e 74 20 73 72 63 69 64 2c 20 69 6e 74  , int srcid, int
44a0: 20 66 6f 72 63 65 29 7b 0a 20 20 69 6e 74 20 73   force){.  int s
44b0: 3b 0a 20 20 42 6c 6f 62 20 64 61 74 61 2c 20 73  ;.  Blob data, s
44c0: 72 63 2c 20 64 65 6c 74 61 3b 0a 20 20 53 74 6d  rc, delta;.  Stm
44d0: 74 20 73 31 2c 20 73 32 3b 0a 20 20 69 66 28 20  t s1, s2;.  if( 
44e0: 73 72 63 69 64 3d 3d 72 69 64 20 29 20 72 65 74  srcid==rid ) ret
44f0: 75 72 6e 3b 0a 20 20 69 66 28 20 21 66 6f 72 63  urn;.  if( !forc
4500: 65 20 26 26 20 66 69 6e 64 53 72 63 69 64 28 72  e && findSrcid(r
4510: 69 64 29 3e 30 20 29 20 72 65 74 75 72 6e 3b 0a  id)>0 ) return;.
4520: 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 5f 69 73    if( content_is
4530: 5f 70 72 69 76 61 74 65 28 73 72 63 69 64 29 20  _private(srcid) 
4540: 26 26 20 21 63 6f 6e 74 65 6e 74 5f 69 73 5f 70  && !content_is_p
4550: 72 69 76 61 74 65 28 72 69 64 29 20 29 7b 0a 20  rivate(rid) ){. 
4560: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
4570: 20 73 20 3d 20 73 72 63 69 64 3b 0a 20 20 77 68   s = srcid;.  wh
4580: 69 6c 65 28 20 28 73 20 3d 20 66 69 6e 64 53 72  ile( (s = findSr
4590: 63 69 64 28 73 29 29 3e 30 20 29 7b 0a 20 20 20  cid(s))>0 ){.   
45a0: 20 69 66 28 20 73 3d 3d 72 69 64 20 29 7b 0a 20   if( s==rid ){. 
45b0: 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 75 6e 64       content_und
45c0: 65 6c 74 61 28 73 72 63 69 64 29 3b 0a 20 20 20  elta(srcid);.   
45d0: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
45e0: 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65    }.  content_ge
45f0: 74 28 73 72 63 69 64 2c 20 26 73 72 63 29 3b 0a  t(srcid, &src);.
4600: 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28    if( blob_size(
4610: 26 73 72 63 29 3c 35 30 20 29 7b 0a 20 20 20 20  &src)<50 ){.    
4620: 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29  blob_reset(&src)
4630: 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
4640: 7d 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28  }.  content_get(
4650: 72 69 64 2c 20 26 64 61 74 61 29 3b 0a 20 20 69  rid, &data);.  i
4660: 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 61  f( blob_size(&da
4670: 74 61 29 3c 35 30 20 29 7b 0a 20 20 20 20 62 6c  ta)<50 ){.    bl
4680: 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29 3b 0a  ob_reset(&src);.
4690: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26      blob_reset(&
46a0: 64 61 74 61 29 3b 0a 20 20 20 20 72 65 74 75 72  data);.    retur
46b0: 6e 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 64 65  n;.  }.  blob_de
46c0: 6c 74 61 5f 63 72 65 61 74 65 28 26 73 72 63 2c  lta_create(&src,
46d0: 20 26 64 61 74 61 2c 20 26 64 65 6c 74 61 29 3b   &data, &delta);
46e0: 0a 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65  .  if( blob_size
46f0: 28 26 64 65 6c 74 61 29 20 3c 20 62 6c 6f 62 5f  (&delta) < blob_
4700: 73 69 7a 65 28 26 64 61 74 61 29 2a 30 2e 37 35  size(&data)*0.75
4710: 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 6d   ){.    blob_com
4720: 70 72 65 73 73 28 26 64 65 6c 74 61 2c 20 26 64  press(&delta, &d
4730: 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 70 72  elta);.    db_pr
4740: 65 70 61 72 65 28 26 73 31 2c 20 22 55 50 44 41  epare(&s1, "UPDA
4750: 54 45 20 62 6c 6f 62 20 53 45 54 20 63 6f 6e 74  TE blob SET cont
4760: 65 6e 74 3d 3a 64 61 74 61 20 57 48 45 52 45 20  ent=:data WHERE 
4770: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20  rid=%d", rid);. 
4780: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73     db_prepare(&s
4790: 32 2c 20 22 52 45 50 4c 41 43 45 20 49 4e 54 4f  2, "REPLACE INTO
47a0: 20 64 65 6c 74 61 28 72 69 64 2c 73 72 63 69 64   delta(rid,srcid
47b0: 29 56 41 4c 55 45 53 28 25 64 2c 25 64 29 22 2c  )VALUES(%d,%d)",
47c0: 20 72 69 64 2c 20 73 72 63 69 64 29 3b 0a 20 20   rid, srcid);.  
47d0: 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26    db_bind_blob(&
47e0: 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 64 65  s1, ":data", &de
47f0: 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 62 65 67  lta);.    db_beg
4800: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
4810: 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 73  ;.    db_exec(&s
4820: 31 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28  1);.    db_exec(
4830: 26 73 32 29 3b 0a 20 20 20 20 64 62 5f 65 6e 64  &s2);.    db_end
4840: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
4850: 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65  .    db_finalize
4860: 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 66 69  (&s1);.    db_fi
4870: 6e 61 6c 69 7a 65 28 26 73 32 29 3b 0a 20 20 20  nalize(&s2);.   
4880: 20 76 65 72 69 66 79 5f 62 65 66 6f 72 65 5f 63   verify_before_c
4890: 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20 20 7d 0a  ommit(rid);.  }.
48a0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
48b0: 63 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74  c);.  blob_reset
48c0: 28 26 64 61 74 61 29 3b 0a 20 20 62 6c 6f 62 5f  (&data);.  blob_
48d0: 72 65 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 7d  reset(&delta);.}
48e0: 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a  ../*.** COMMAND:
48f0: 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 64    test-content-d
4900: 65 6c 74 69 66 79 0a 2a 2a 0a 2a 2a 20 43 6f 6e  eltify.**.** Con
4910: 76 65 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  vert the content
4920: 20 61 74 20 52 49 44 20 69 6e 74 6f 20 61 20 64   at RID into a d
4930: 65 6c 74 61 20 66 72 6f 6d 20 53 52 43 49 44 2e  elta from SRCID.
4940: 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f  .*/.void test_co
4950: 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 5f 63 6d  ntent_deltify_cm
4960: 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 67  d(void){.  if( g
4970: 2e 61 72 67 63 21 3d 35 20 29 20 75 73 61 67 65  .argc!=5 ) usage
4980: 28 22 52 49 44 20 53 52 43 49 44 20 46 4f 52 43  ("RID SRCID FORC
4990: 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f 62  E");.  db_must_b
49a0: 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b  e_within_tree();
49b0: 0a 20 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69  .  content_delti
49c0: 66 79 28 61 74 6f 69 28 67 2e 61 72 67 76 5b 32  fy(atoi(g.argv[2
49d0: 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67 76 5b  ]), atoi(g.argv[
49e0: 33 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67 76  3]), atoi(g.argv
49f0: 5b 34 5d 29 29 3b 0a 7d 0a                       [4]));.}.