Hex Artifact Content
Not logged in

Artifact 6b8e09987a847440a2ebf772e4ddcb62c447306a:

File src/content.c part of check-in [61ddd63b72] - Work toward making fossil work better on large repositories. This version implements a cache in the content manager. It is not clear yet if this is necessarily a good idea - this check-in might end up on an abandoned branch at some point. by drh on 2008-03-06 22:58: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 69 6e 74 20 73 72 63 69 64 20  d){.  int srcid 
0ab0: 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c  = db_int(0, "SEL
0ac0: 45 43 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64  ECT srcid FROM d
0ad0: 65 6c 74 61 20 57 48 45 52 45 20 72 69 64 3d 25  elta WHERE rid=%
0ae0: 64 22 2c 20 72 69 64 29 3b 0a 20 20 72 65 74 75  d", rid);.  retu
0af0: 72 6e 20 73 72 63 69 64 3b 0a 7d 0a 0a 2f 2a 0a  rn srcid;.}../*.
0b00: 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
0b10: 69 66 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 76  if content is av
0b20: 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 72 74 69  ailable for arti
0b30: 66 61 63 74 20 22 72 69 64 22 2e 20 20 52 65 74  fact "rid".  Ret
0b40: 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 69  urn.** true if i
0b50: 74 20 69 73 2e 20 20 52 65 74 75 72 6e 20 66 61  t is.  Return fa
0b60: 6c 73 65 20 69 66 20 72 69 64 20 69 73 20 61 20  lse if rid is a 
0b70: 70 68 61 6e 74 6f 6d 20 6f 72 20 64 65 70 65 6e  phantom or depen
0b80: 64 73 20 6f 6e 0a 2a 2a 20 61 20 70 68 61 6e 74  ds on.** a phant
0b90: 6f 6d 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65  om..*/.int conte
0ba0: 6e 74 5f 69 73 5f 61 76 61 69 6c 61 62 6c 65 28  nt_is_available(
0bb0: 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 6e 74 20  int rid){.  int 
0bc0: 73 72 63 69 64 3b 0a 20 20 69 66 28 20 62 61 67  srcid;.  if( bag
0bd0: 5f 66 69 6e 64 28 26 63 6f 6e 74 65 6e 74 43 61  _find(&contentCa
0be0: 63 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64  che.missing, rid
0bf0: 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
0c00: 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 62 61 67  0;.  }.  if( bag
0c10: 5f 66 69 6e 64 28 26 63 6f 6e 74 65 6e 74 43 61  _find(&contentCa
0c20: 63 68 65 2e 61 76 61 69 6c 61 62 6c 65 2c 20 72  che.available, r
0c30: 69 64 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  id) ){.    retur
0c40: 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64  n 1;.  }.  if( d
0c50: 62 5f 69 6e 74 28 2d 31 2c 20 22 53 45 4c 45 43  b_int(-1, "SELEC
0c60: 54 20 73 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62  T size FROM blob
0c70: 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20   WHERE rid=%d", 
0c80: 72 69 64 29 3c 30 20 29 7b 0a 20 20 20 20 62 61  rid)<0 ){.    ba
0c90: 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e 74 65 6e  g_insert(&conten
0ca0: 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c 20  tCache.missing, 
0cb0: 72 69 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  rid);.    return
0cc0: 20 30 3b 0a 20 20 7d 0a 20 20 73 72 63 69 64 20   0;.  }.  srcid 
0cd0: 3d 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 29  = findSrcid(rid)
0ce0: 3b 0a 20 20 69 66 28 20 73 72 63 69 64 3d 3d 30  ;.  if( srcid==0
0cf0: 20 29 7b 0a 20 20 20 20 62 61 67 5f 69 6e 73 65   ){.    bag_inse
0d00: 72 74 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65  rt(&contentCache
0d10: 2e 61 76 61 69 6c 61 62 6c 65 2c 20 72 69 64 29  .available, rid)
0d20: 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
0d30: 20 20 7d 0a 20 20 69 66 28 20 63 6f 6e 74 65 6e    }.  if( conten
0d40: 74 5f 69 73 5f 61 76 61 69 6c 61 62 6c 65 28 73  t_is_available(s
0d50: 72 63 69 64 29 20 29 7b 0a 20 20 20 20 62 61 67  rcid) ){.    bag
0d60: 5f 69 6e 73 65 72 74 28 26 63 6f 6e 74 65 6e 74  _insert(&content
0d70: 43 61 63 68 65 2e 61 76 61 69 6c 61 62 6c 65 2c  Cache.available,
0d80: 20 72 69 64 29 3b 0a 20 20 20 20 72 65 74 75 72   rid);.    retur
0d90: 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 1;.  }else{.  
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 6d 69 73 73 69  ntentCache.missi
0dc0: 6e 67 2c 20 72 69 64 29 3b 0a 20 20 20 20 72 65  ng, rid);.    re
0dd0: 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
0de0: 2a 0a 2a 2a 20 4d 61 72 6b 20 61 72 74 69 66 61  *.** Mark artifa
0df0: 63 74 20 72 69 64 20 61 73 20 62 65 69 6e 67 20  ct rid as being 
0e00: 61 76 61 69 6c 61 62 6c 65 20 6e 6f 77 2e 20 20  available now.  
0e10: 55 70 64 61 74 65 20 74 68 65 20 63 61 63 68 65  Update the cache
0e20: 20 74 6f 0a 2a 2a 20 73 68 6f 77 20 74 68 61 74   to.** show that
0e30: 20 65 76 65 72 79 74 68 69 6e 67 20 74 68 61 74   everything that
0e40: 20 77 61 73 20 66 6f 72 6d 65 72 6c 79 20 75 6e   was formerly un
0e50: 61 76 61 69 6c 61 62 6c 65 20 62 65 63 61 75 73  available becaus
0e60: 65 20 72 69 64 0a 2a 2a 20 77 61 73 20 6d 69 73  e rid.** was mis
0e70: 73 69 6e 67 20 69 73 20 6e 6f 77 20 61 76 61 69  sing is now avai
0e80: 6c 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  lable..*/.static
0e90: 20 76 6f 69 64 20 63 6f 6e 74 65 6e 74 5f 6d 61   void content_ma
0ea0: 72 6b 5f 61 76 61 69 6c 61 62 6c 65 28 69 6e 74  rk_available(int
0eb0: 20 72 69 64 29 7b 0a 20 20 42 61 67 20 70 65 6e   rid){.  Bag pen
0ec0: 64 69 6e 67 3b 0a 20 20 53 74 6d 74 20 71 3b 0a  ding;.  Stmt q;.
0ed0: 20 20 69 66 28 20 62 61 67 5f 66 69 6e 64 28 26    if( bag_find(&
0ee0: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 76 61  contentCache.ava
0ef0: 69 6c 61 62 6c 65 2c 20 72 69 64 29 20 29 20 72  ilable, rid) ) r
0f00: 65 74 75 72 6e 3b 0a 20 20 62 61 67 5f 69 6e 69  eturn;.  bag_ini
0f10: 74 28 26 70 65 6e 64 69 6e 67 29 3b 0a 20 20 62  t(&pending);.  b
0f20: 61 67 5f 69 6e 73 65 72 74 28 26 70 65 6e 64 69  ag_insert(&pendi
0f30: 6e 67 2c 20 72 69 64 29 3b 0a 20 20 77 68 69 6c  ng, rid);.  whil
0f40: 65 28 20 28 72 69 64 20 3d 20 62 61 67 5f 66 69  e( (rid = bag_fi
0f50: 72 73 74 28 26 70 65 6e 64 69 6e 67 29 29 21 3d  rst(&pending))!=
0f60: 30 20 29 7b 0a 20 20 20 20 62 61 67 5f 72 65 6d  0 ){.    bag_rem
0f70: 6f 76 65 28 26 70 65 6e 64 69 6e 67 2c 20 72 69  ove(&pending, ri
0f80: 64 29 3b 0a 20 20 20 20 62 61 67 5f 72 65 6d 6f  d);.    bag_remo
0f90: 76 65 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65  ve(&contentCache
0fa0: 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64 29 3b 0a  .missing, rid);.
0fb0: 20 20 20 20 62 61 67 5f 69 6e 73 65 72 74 28 26      bag_insert(&
0fc0: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 76 61  contentCache.ava
0fd0: 69 6c 61 62 6c 65 2c 20 72 69 64 29 3b 0a 20 20  ilable, rid);.  
0fe0: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c    db_prepare(&q,
0ff0: 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f   "SELECT rid FRO
1000: 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 73 72  M delta WHERE sr
1010: 63 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20  cid=%d", rid);. 
1020: 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65     while( db_ste
1030: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
1040: 57 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  W ){.      int n
1050: 78 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  x = db_column_in
1060: 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 20  t(&q, 0);.      
1070: 62 61 67 5f 69 6e 73 65 72 74 28 26 70 65 6e 64  bag_insert(&pend
1080: 69 6e 67 2c 20 6e 78 29 3b 0a 20 20 20 20 7d 0a  ing, nx);.    }.
1090: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28      db_finalize(
10a0: 26 71 29 3b 0a 20 20 7d 0a 20 20 62 61 67 5f 63  &q);.  }.  bag_c
10b0: 6c 65 61 72 28 26 70 65 6e 64 69 6e 67 29 3b 0a  lear(&pending);.
10c0: 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
10d0: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72   the content for
10e0: 20 49 44 20 72 69 64 20 61 6e 64 20 70 75 74 20   ID rid and put 
10f0: 69 74 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 75  it into the.** u
1100: 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 6c 6f  ninitialized blo
1110: 62 2e 20 20 52 65 74 75 72 6e 20 31 20 6f 6e 20  b.  Return 1 on 
1120: 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65  success.  If the
1130: 20 72 65 63 6f 72 64 0a 2a 2a 20 69 73 20 61 20   record.** is a 
1140: 70 68 61 6e 74 6f 6d 2c 20 7a 65 72 6f 20 70 42  phantom, zero pB
1150: 6c 6f 62 20 61 6e 64 20 72 65 74 75 72 6e 20 30  lob and return 0
1160: 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e 74  ..*/.int content
1170: 5f 67 65 74 28 69 6e 74 20 72 69 64 2c 20 42 6c  _get(int rid, Bl
1180: 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 53 74  ob *pBlob){.  St
1190: 6d 74 20 71 3b 0a 20 20 42 6c 6f 62 20 73 72 63  mt q;.  Blob src
11a0: 3b 0a 20 20 69 6e 74 20 73 72 63 69 64 3b 0a 20  ;.  int srcid;. 
11b0: 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 69   int rc = 0;.  i
11c0: 6e 74 20 69 3b 0a 20 20 73 74 61 74 69 63 20 42  nt i;.  static B
11d0: 61 67 20 69 6e 50 72 6f 63 65 73 73 3b 0a 0a 20  ag inProcess;.. 
11e0: 20 61 73 73 65 72 74 28 20 67 2e 72 65 70 6f 73   assert( g.repos
11f0: 69 74 6f 72 79 4f 70 65 6e 20 29 3b 0a 20 20 62  itoryOpen );.  b
1200: 6c 6f 62 5f 7a 65 72 6f 28 70 42 6c 6f 62 29 3b  lob_zero(pBlob);
1210: 0a 0a 20 20 2f 2a 20 45 61 72 6c 79 20 6f 75 74  ..  /* Early out
1220: 20 69 66 20 77 65 20 6b 6e 6f 77 20 74 68 65 20   if we know the 
1230: 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 61  content is not a
1240: 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 69 66  vailable */.  if
1250: 28 20 62 61 67 5f 66 69 6e 64 28 26 63 6f 6e 74  ( bag_find(&cont
1260: 65 6e 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67  entCache.missing
1270: 2c 20 72 69 64 29 20 29 7b 0a 20 20 20 20 43 4f  , rid) ){.    CO
1280: 4e 54 45 4e 54 5f 54 52 41 43 45 28 28 22 25 2a  NTENT_TRACE(("%*
1290: 73 6d 69 73 73 20 66 72 6f 6d 20 63 61 63 68 65  smiss from cache
12a0: 3a 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  : %d\n",.       
12b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 62 61 67               bag
12c0: 5f 63 6f 75 6e 74 28 26 69 6e 50 72 6f 63 65 73  _count(&inProces
12d0: 73 29 2c 20 22 22 2c 20 72 69 64 29 29 0a 20 20  s), "", rid)).  
12e0: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
12f0: 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 74  .  /* Look for t
1300: 68 65 20 61 72 74 69 66 61 63 74 20 69 6e 20 74  he artifact in t
1310: 68 65 20 63 61 63 68 65 20 66 69 72 73 74 20 2a  he cache first *
1320: 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 63  /.  for(i=0; i<c
1330: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 3b 20 69  ontentCache.n; i
1340: 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 63 6f 6e  ++){.    if( con
1350: 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d 2e 72  tentCache.a[i].r
1360: 69 64 3d 3d 72 69 64 20 29 7b 0a 20 20 20 20 20  id==rid ){.     
1370: 20 2a 70 42 6c 6f 62 20 3d 20 63 6f 6e 74 65 6e   *pBlob = conten
1380: 74 43 61 63 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74  tCache.a[i].cont
1390: 65 6e 74 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  ent;.      blob_
13a0: 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 43 61 63  zero(&contentCac
13b0: 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74 65 6e 74 29  he.a[i].content)
13c0: 3b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 43  ;.      contentC
13d0: 61 63 68 65 2e 6e 2d 2d 3b 0a 20 20 20 20 20 20  ache.n--;.      
13e0: 69 66 28 20 69 3c 63 6f 6e 74 65 6e 74 43 61 63  if( i<contentCac
13f0: 68 65 2e 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  he.n ){.        
1400: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69  contentCache.a[i
1410: 5d 20 3d 20 63 6f 6e 74 65 6e 74 43 61 63 68 65  ] = contentCache
1420: 2e 61 5b 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e  .a[contentCache.
1430: 6e 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n];.      }.    
1440: 20 20 43 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28    CONTENT_TRACE(
1450: 28 22 25 2a 73 68 69 74 20 63 61 63 68 65 3a 20  ("%*shit cache: 
1460: 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20  %d\n", .        
1470: 20 20 20 20 20 20 20 20 20 20 20 20 62 61 67 5f              bag_
1480: 63 6f 75 6e 74 28 26 69 6e 50 72 6f 63 65 73 73  count(&inProcess
1490: 29 2c 20 22 22 2c 20 72 69 64 29 29 0a 20 20 20  ), "", rid)).   
14a0: 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
14b0: 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65   }.  }..  /* See
14c0: 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61   if we need to a
14d0: 70 70 6c 79 20 61 20 64 65 6c 74 61 20 74 6f 20  pply a delta to 
14e0: 66 69 6e 64 20 74 68 69 73 20 61 72 74 69 66 61  find this artifa
14f0: 63 74 20 2a 2f 0a 20 20 73 72 63 69 64 20 3d 20  ct */.  srcid = 
1500: 66 69 6e 64 53 72 63 69 64 28 72 69 64 29 3b 0a  findSrcid(rid);.
1510: 20 20 43 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28    CONTENT_TRACE(
1520: 28 22 25 2a 73 73 65 61 72 63 68 69 6e 67 20 66  ("%*ssearching f
1530: 6f 72 20 25 64 2e 20 20 4e 65 65 64 20 25 64 2e  or %d.  Need %d.
1540: 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
1550: 20 20 20 20 20 20 62 61 67 5f 63 6f 75 6e 74 28        bag_count(
1560: 26 69 6e 50 72 6f 63 65 73 73 29 2c 20 22 22 2c  &inProcess), "",
1570: 20 72 69 64 2c 20 73 72 63 69 64 29 29 0a 0a 0a   rid, srcid))...
1580: 20 20 69 66 28 20 73 72 63 69 64 20 29 7b 0a 20    if( srcid ){. 
1590: 20 20 20 2f 2a 20 59 65 73 2c 20 61 20 64 65 6c     /* Yes, a del
15a0: 74 61 20 69 73 20 72 65 71 75 69 72 65 64 20 2a  ta is required *
15b0: 2f 0a 20 20 20 20 69 66 28 20 62 61 67 5f 66 69  /.    if( bag_fi
15c0: 6e 64 28 26 69 6e 50 72 6f 63 65 73 73 2c 20 73  nd(&inProcess, s
15d0: 72 63 69 64 29 20 29 7b 0a 20 20 20 20 20 20 64  rcid) ){.      d
15e0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
15f0: 20 20 20 20 20 20 22 55 50 44 41 54 45 20 62 6c        "UPDATE bl
1600: 6f 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 4e  ob SET content=N
1610: 55 4c 4c 2c 20 73 69 7a 65 3d 2d 31 20 57 48 45  ULL, size=-1 WHE
1620: 52 45 20 72 69 64 3d 25 64 3b 22 0a 20 20 20 20  RE rid=%d;".    
1630: 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
1640: 20 64 65 6c 74 61 20 57 48 45 52 45 20 72 69 64   delta WHERE rid
1650: 3d 25 64 3b 22 0a 20 20 20 20 20 20 20 20 22 49  =%d;".        "I
1660: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20  NSERT OR IGNORE 
1670: 49 4e 54 4f 20 70 68 61 6e 74 6f 6d 20 56 41 4c  INTO phantom VAL
1680: 55 45 53 28 25 64 29 3b 22 2c 0a 20 20 20 20 20  UES(%d);",.     
1690: 20 20 20 73 72 63 69 64 2c 20 73 72 63 69 64 2c     srcid, srcid,
16a0: 20 73 72 63 69 64 0a 20 20 20 20 20 20 29 3b 0a   srcid.      );.
16b0: 20 20 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28        blob_zero(
16c0: 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 72 65  pBlob);.      re
16d0: 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
16e0: 20 20 62 61 67 5f 69 6e 73 65 72 74 28 26 69 6e    bag_insert(&in
16f0: 50 72 6f 63 65 73 73 2c 20 73 72 63 69 64 29 3b  Process, srcid);
1700: 0a 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e  ..    if( conten
1710: 74 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73 72  t_get(srcid, &sr
1720: 63 29 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 70  c) ){.      db_p
1730: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
1740: 43 54 20 63 6f 6e 74 65 6e 74 20 46 52 4f 4d 20  CT content FROM 
1750: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25  blob WHERE rid=%
1760: 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20  d AND size>=0", 
1770: 72 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  rid);.      if( 
1780: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c  db_step(&q)==SQL
1790: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
17a0: 20 20 20 42 6c 6f 62 20 64 65 6c 74 61 3b 0a 20     Blob delta;. 
17b0: 20 20 20 20 20 20 20 64 62 5f 65 70 68 65 6d 65         db_epheme
17c0: 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20  ral_blob(&q, 0, 
17d0: 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 20 20 20  &delta);.       
17e0: 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 73 73   blob_uncompress
17f0: 28 26 64 65 6c 74 61 2c 20 26 64 65 6c 74 61 29  (&delta, &delta)
1800: 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 69  ;.        blob_i
1810: 6e 69 74 28 70 42 6c 6f 62 2c 30 2c 30 29 3b 0a  nit(pBlob,0,0);.
1820: 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 64 65 6c          blob_del
1830: 74 61 5f 61 70 70 6c 79 28 26 73 72 63 2c 20 26  ta_apply(&src, &
1840: 64 65 6c 74 61 2c 20 70 42 6c 6f 62 29 3b 0a 20  delta, pBlob);. 
1850: 20 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65         blob_rese
1860: 74 28 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 20  t(&delta);.     
1870: 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20     rc = 1;.     
1880: 20 7d 0a 20 20 20 20 20 20 64 62 5f 66 69 6e 61   }.      db_fina
1890: 6c 69 7a 65 28 26 71 29 3b 0a 0a 20 20 20 20 20  lize(&q);..     
18a0: 20 2f 2a 20 53 61 76 65 20 74 68 65 20 73 72 63   /* Save the src
18b0: 69 64 20 61 72 74 69 66 61 63 74 20 69 6e 20 74  id artifact in t
18c0: 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20  he cache */.    
18d0: 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 43 61 63    if( contentCac
18e0: 68 65 2e 6e 3c 4d 58 5f 43 41 43 48 45 5f 43 4e  he.n<MX_CACHE_CN
18f0: 54 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20 3d  T ){.        i =
1900: 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 2b   contentCache.n+
1910: 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  +;.      }else i
1920: 66 28 20 28 28 63 6f 6e 74 65 6e 74 43 61 63 68  f( ((contentCach
1930: 65 2e 73 6b 69 70 43 6e 74 2b 2b 29 25 45 58 50  e.skipCnt++)%EXP
1940: 45 4c 4c 5f 49 4e 54 45 52 56 41 4c 29 21 3d 30  ELL_INTERVAL)!=0
1950: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20 3d 20   ){.        i = 
1960: 2d 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  -1;.      }else{
1970: 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 2c 20  .        int j, 
1980: 62 65 73 74 3b 0a 20 20 20 20 20 20 20 20 62 65  best;.        be
1990: 73 74 20 3d 20 63 6f 6e 74 65 6e 74 43 61 63 68  st = contentCach
19a0: 65 2e 6e 65 78 74 41 67 65 2b 31 3b 0a 20 20 20  e.nextAge+1;.   
19b0: 20 20 20 20 20 69 20 3d 20 2d 31 3b 0a 20 20 20       i = -1;.   
19c0: 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
19d0: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 3b 20  contentCache.n; 
19e0: 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
19f0: 69 66 28 20 63 6f 6e 74 65 6e 74 43 61 63 68 65  if( contentCache
1a00: 2e 61 5b 6a 5d 2e 61 67 65 3c 62 65 73 74 20 29  .a[j].age<best )
1a10: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 20  {.            i 
1a20: 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = j;.           
1a30: 20 62 65 73 74 20 3d 20 63 6f 6e 74 65 6e 74 43   best = contentC
1a40: 61 63 68 65 2e 61 5b 6a 5d 2e 61 67 65 3b 0a 20  ache.a[j].age;. 
1a50: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1a60: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 43 4f 4e     }.        CON
1a70: 54 45 4e 54 5f 54 52 41 43 45 28 28 22 25 2a 73  TENT_TRACE(("%*s
1a80: 65 78 70 65 6c 6c 20 25 64 20 66 72 6f 6d 20 63  expell %d from c
1a90: 61 63 68 65 5c 6e 22 2c 0a 20 20 20 20 20 20 20  ache\n",.       
1aa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ab0: 62 61 67 5f 63 6f 75 6e 74 28 26 69 6e 50 72 6f  bag_count(&inPro
1ac0: 63 65 73 73 29 2c 20 22 22 2c 20 63 6f 6e 74 65  cess), "", conte
1ad0: 6e 74 43 61 63 68 65 2e 61 5b 69 5d 2e 72 69 64  ntCache.a[i].rid
1ae0: 29 29 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  )).        blob_
1af0: 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 43 61  reset(&contentCa
1b00: 63 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74 65 6e 74  che.a[i].content
1b10: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1b20: 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a 20 20 20   if( i>=0 ){.   
1b30: 20 20 20 20 20 63 6f 6e 74 65 6e 74 43 61 63 68       contentCach
1b40: 65 2e 61 5b 69 5d 2e 63 6f 6e 74 65 6e 74 20 3d  e.a[i].content =
1b50: 20 73 72 63 3b 0a 20 20 20 20 20 20 20 20 63 6f   src;.        co
1b60: 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d 2e  ntentCache.a[i].
1b70: 61 67 65 20 3d 20 63 6f 6e 74 65 6e 74 43 61 63  age = contentCac
1b80: 68 65 2e 6e 65 78 74 41 67 65 2b 2b 3b 0a 20 20  he.nextAge++;.  
1b90: 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 43 61 63        contentCac
1ba0: 68 65 2e 61 5b 69 5d 2e 72 69 64 20 3d 20 73 72  he.a[i].rid = sr
1bb0: 63 69 64 3b 0a 20 20 20 20 20 20 20 20 43 4f 4e  cid;.        CON
1bc0: 54 45 4e 54 5f 54 52 41 43 45 28 28 22 25 2a 73  TENT_TRACE(("%*s
1bd0: 61 64 64 20 25 64 20 74 6f 20 63 61 63 68 65 5c  add %d to cache\
1be0: 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
1bf0: 20 20 20 20 20 20 20 20 20 20 20 62 61 67 5f 63             bag_c
1c00: 6f 75 6e 74 28 26 69 6e 50 72 6f 63 65 73 73 29  ount(&inProcess)
1c10: 2c 20 22 22 2c 20 73 72 63 69 64 29 29 0a 20 20  , "", srcid)).  
1c20: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1c30: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73     blob_reset(&s
1c40: 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rc);.      }.   
1c50: 20 7d 0a 20 20 20 20 62 61 67 5f 72 65 6d 6f 76   }.    bag_remov
1c60: 65 28 26 69 6e 50 72 6f 63 65 73 73 2c 20 73 72  e(&inProcess, sr
1c70: 63 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  cid);.  }else{. 
1c80: 20 20 20 2f 2a 20 4e 6f 20 64 65 6c 74 61 20 72     /* No delta r
1c90: 65 71 75 69 72 65 64 2e 20 20 52 65 61 64 20 63  equired.  Read c
1ca0: 6f 6e 74 65 6e 74 20 64 69 72 65 63 74 6c 79 20  ontent directly 
1cb0: 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1cc0: 65 20 2a 2f 0a 20 20 20 20 64 62 5f 70 72 65 70  e */.    db_prep
1cd0: 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20  are(&q, "SELECT 
1ce0: 63 6f 6e 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f  content FROM blo
1cf0: 62 20 57 48 45 52 45 20 72 69 64 3d 25 64 20 41  b WHERE rid=%d A
1d00: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64  ND size>=0", rid
1d10: 29 3b 0a 20 20 20 20 69 66 28 20 64 62 5f 73 74  );.    if( db_st
1d20: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(&q)==SQLITE_R
1d30: 4f 57 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 65  OW ){.      db_e
1d40: 70 68 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71  phemeral_blob(&q
1d50: 2c 20 30 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20  , 0, pBlob);.   
1d60: 20 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65     blob_uncompre
1d70: 73 73 28 70 42 6c 6f 62 2c 20 70 42 6c 6f 62 29  ss(pBlob, pBlob)
1d80: 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a  ;.      rc = 1;.
1d90: 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66 69 6e      }.    db_fin
1da0: 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 7d 0a 20  alize(&q);.  }. 
1db0: 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20   if( rc==0 ){.  
1dc0: 20 20 62 61 67 5f 69 6e 73 65 72 74 28 26 63 6f    bag_insert(&co
1dd0: 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69 73 73 69  ntentCache.missi
1de0: 6e 67 2c 20 72 69 64 29 3b 0a 20 20 7d 65 6c 73  ng, rid);.  }els
1df0: 65 7b 0a 20 20 20 20 62 61 67 5f 69 6e 73 65 72  e{.    bag_inser
1e00: 74 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e  t(&contentCache.
1e10: 61 76 61 69 6c 61 62 6c 65 2c 20 72 69 64 29 3b  available, rid);
1e20: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1e30: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74  ;.}../*.** Get t
1e40: 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61  he contents of a
1e50: 20 66 69 6c 65 20 77 69 74 68 69 6e 20 61 20 67   file within a g
1e60: 69 76 65 6e 20 62 61 73 65 6c 69 6e 65 2e 0a 2a  iven baseline..*
1e70: 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 67 65  /.int content_ge
1e80: 74 5f 68 69 73 74 6f 72 69 63 61 6c 5f 66 69 6c  t_historical_fil
1e90: 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e(.  const char 
1ea0: 2a 72 65 76 69 73 69 6f 6e 2c 20 20 20 20 2f 2a  *revision,    /*
1eb0: 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 62 61 73   Name of the bas
1ec0: 65 6c 69 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67  eline containing
1ed0: 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63   the file */.  c
1ee0: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 2c  onst char *file,
1ef0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
1f00: 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20  of the file */. 
1f10: 20 42 6c 6f 62 20 2a 63 6f 6e 74 65 6e 74 20 20   Blob *content  
1f20: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
1f30: 74 65 20 66 69 6c 65 20 63 6f 6e 74 65 6e 74 20  te file content 
1f40: 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 42 6c 6f  here */.){.  Blo
1f50: 62 20 6d 66 69 6c 65 3b 0a 20 20 4d 61 6e 69 66  b mfile;.  Manif
1f60: 65 73 74 20 6d 3b 0a 20 20 69 6e 74 20 69 2c 20  est m;.  int i, 
1f70: 72 69 64 3d 30 3b 0a 20 20 0a 20 20 72 69 64 20  rid=0;.  .  rid 
1f80: 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 72 65  = name_to_rid(re
1f90: 76 69 73 69 6f 6e 29 3b 0a 20 20 63 6f 6e 74 65  vision);.  conte
1fa0: 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 6d 66 69  nt_get(rid, &mfi
1fb0: 6c 65 29 3b 0a 20 20 0a 20 20 69 66 28 20 6d 61  le);.  .  if( ma
1fc0: 6e 69 66 65 73 74 5f 70 61 72 73 65 28 26 6d 2c  nifest_parse(&m,
1fd0: 20 26 6d 66 69 6c 65 29 20 29 7b 0a 20 20 20 20   &mfile) ){.    
1fe0: 66 6f 72 28 69 3d 30 3b 20 69 3c 6d 2e 6e 46 69  for(i=0; i<m.nFi
1ff0: 6c 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  le; i++){.      
2000: 69 66 28 20 73 74 72 63 6d 70 28 6d 2e 61 46 69  if( strcmp(m.aFi
2010: 6c 65 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 66 69 6c  le[i].zName, fil
2020: 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)==0 ){.       
2030: 20 72 69 64 20 3d 20 75 75 69 64 5f 74 6f 5f 72   rid = uuid_to_r
2040: 69 64 28 6d 2e 61 46 69 6c 65 5b 69 5d 2e 7a 55  id(m.aFile[i].zU
2050: 75 69 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  uid, 0);.       
2060: 20 72 65 74 75 72 6e 20 63 6f 6e 74 65 6e 74 5f   return content_
2070: 67 65 74 28 72 69 64 2c 20 63 6f 6e 74 65 6e 74  get(rid, content
2080: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2090: 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69  .    fossil_pani
20a0: 63 28 22 66 69 6c 65 3a 20 25 73 20 64 6f 65 73  c("file: %s does
20b0: 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 72 65   not exist in re
20c0: 76 69 73 69 6f 6e 3a 20 25 73 22 2c 20 66 69 6c  vision: %s", fil
20d0: 65 2c 20 72 65 76 69 73 69 6f 6e 29 3b 0a 20 20  e, revision);.  
20e0: 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 73 73 69  }else{.    fossi
20f0: 6c 5f 70 61 6e 69 63 28 22 63 6f 75 6c 64 20 6e  l_panic("could n
2100: 6f 74 20 70 61 72 73 65 20 6d 61 6e 69 66 65 73  ot parse manifes
2110: 74 20 66 6f 72 20 72 65 76 69 73 69 6f 6e 3a 20  t for revision: 
2120: 25 73 22 2c 20 72 65 76 69 73 69 6f 6e 29 3b 0a  %s", revision);.
2130: 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 20    }.  .  return 
2140: 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d  0;.}../*.** COMM
2150: 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65  AND:  test-conte
2160: 6e 74 2d 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74  nt-get.**.** Ext
2170: 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d  ract a blob from
2180: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
2190: 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20  d write it into 
21a0: 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20  a file..*/.void 
21b0: 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 67 65 74  test_content_get
21c0: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e  _cmd(void){.  in
21d0: 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f  t rid;.  Blob co
21e0: 6e 74 65 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63  ntent;.  const c
21f0: 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66  har *zFile;.  if
2200: 28 20 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67  ( g.argc!=4 && g
2210: 2e 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65  .argc!=3 ) usage
2220: 28 22 52 45 43 4f 52 44 49 44 20 3f 46 49 4c 45  ("RECORDID ?FILE
2230: 4e 41 4d 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65  NAME?");.  zFile
2240: 20 3d 20 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67   = g.argc==4 ? g
2250: 2e 61 72 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a  .argv[3] : "-";.
2260: 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74    db_must_be_wit
2270: 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69  hin_tree();.  ri
2280: 64 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28  d = name_to_rid(
2290: 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 63 6f  g.argv[2]);.  co
22a0: 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26  ntent_get(rid, &
22b0: 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62  content);.  blob
22c0: 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26  _write_to_file(&
22d0: 63 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b  content, zFile);
22e0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e  .}../*.** COMMAN
22f0: 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74  D:  test-content
2300: 2d 72 61 77 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78  -rawget.**.** Ex
2310: 74 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f  tract a blob fro
2320: 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  m the database a
2330: 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f  nd write it into
2340: 20 61 20 66 69 6c 65 2e 20 20 54 68 69 73 0a 2a   a file.  This.*
2350: 2a 20 76 65 72 73 69 6f 6e 20 64 6f 65 73 20 6e  * version does n
2360: 6f 74 20 65 78 70 61 6e 64 20 74 68 65 20 64 65  ot expand the de
2370: 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73  lta..*/.void tes
2380: 74 5f 63 6f 6e 74 65 6e 74 5f 72 61 77 67 65 74  t_content_rawget
2390: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e  _cmd(void){.  in
23a0: 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f  t rid;.  Blob co
23b0: 6e 74 65 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63  ntent;.  const c
23c0: 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66  har *zFile;.  if
23d0: 28 20 67 2e 61 72 67 63 21 3d 34 20 26 26 20 67  ( g.argc!=4 && g
23e0: 2e 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65  .argc!=3 ) usage
23f0: 28 22 52 45 43 4f 52 44 49 44 20 3f 46 49 4c 45  ("RECORDID ?FILE
2400: 4e 41 4d 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65  NAME?");.  zFile
2410: 20 3d 20 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67   = g.argc==4 ? g
2420: 2e 61 72 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a  .argv[3] : "-";.
2430: 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74    db_must_be_wit
2440: 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69  hin_tree();.  ri
2450: 64 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28  d = name_to_rid(
2460: 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 62 6c  g.argv[2]);.  bl
2470: 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74  ob_zero(&content
2480: 29 3b 0a 20 20 64 62 5f 62 6c 6f 62 28 26 63 6f  );.  db_blob(&co
2490: 6e 74 65 6e 74 2c 20 22 53 45 4c 45 43 54 20 63  ntent, "SELECT c
24a0: 6f 6e 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62  ontent FROM blob
24b0: 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20   WHERE rid=%d", 
24c0: 72 69 64 29 3b 0a 20 20 62 6c 6f 62 5f 75 6e 63  rid);.  blob_unc
24d0: 6f 6d 70 72 65 73 73 28 26 63 6f 6e 74 65 6e 74  ompress(&content
24e0: 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 62  , &content);.  b
24f0: 6c 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c  lob_write_to_fil
2500: 65 28 26 63 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c  e(&content, zFil
2510: 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  e);.}../*.** Whe
2520: 6e 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 6f  n a record is co
2530: 6e 76 65 72 74 65 64 20 66 72 6f 6d 20 61 20 70  nverted from a p
2540: 68 61 6e 74 6f 6d 20 74 6f 20 61 20 72 65 61 6c  hantom to a real
2550: 20 72 65 63 6f 72 64 2c 0a 2a 2a 20 69 66 20 74   record,.** if t
2560: 68 61 74 20 72 65 63 6f 72 64 20 68 61 73 20 6f  hat record has o
2570: 74 68 65 72 20 72 65 63 6f 72 64 73 20 74 68 61  ther records tha
2580: 74 20 61 72 65 20 64 65 72 69 76 65 64 20 62 79  t are derived by
2590: 20 64 65 6c 74 61 2c 0a 2a 2a 20 74 68 65 6e 20   delta,.** then 
25a0: 63 61 6c 6c 20 6d 61 6e 69 66 65 73 74 5f 63 72  call manifest_cr
25b0: 6f 73 73 6c 69 6e 6b 28 29 20 6f 6e 20 74 68 6f  osslink() on tho
25c0: 73 65 20 6f 74 68 65 72 20 72 65 63 6f 72 64 73  se other records
25d0: 2e 0a 2a 2f 0a 76 6f 69 64 20 61 66 74 65 72 5f  ..*/.void after_
25e0: 64 65 70 68 61 6e 74 6f 6d 69 7a 65 28 69 6e 74  dephantomize(int
25f0: 20 72 69 64 2c 20 69 6e 74 20 6c 69 6e 6b 46 6c   rid, int linkFl
2600: 61 67 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20  ag){.  Stmt q;. 
2610: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20   db_prepare(&q, 
2620: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d  "SELECT rid FROM
2630: 20 64 65 6c 74 61 20 57 48 45 52 45 20 73 72 63   delta WHERE src
2640: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20  id=%d", rid);.  
2650: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26  while( db_step(&
2660: 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  q)==SQLITE_ROW )
2670: 7b 0a 20 20 20 20 69 6e 74 20 74 69 64 20 3d 20  {.    int tid = 
2680: 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71  db_column_int(&q
2690: 2c 20 30 29 3b 0a 20 20 20 20 61 66 74 65 72 5f  , 0);.    after_
26a0: 64 65 70 68 61 6e 74 6f 6d 69 7a 65 28 74 69 64  dephantomize(tid
26b0: 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66  , 1);.  }.  db_f
26c0: 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 69  inalize(&q);.  i
26d0: 66 28 20 6c 69 6e 6b 46 6c 61 67 20 29 7b 0a 20  f( linkFlag ){. 
26e0: 20 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b     Blob content;
26f0: 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74  .    content_get
2700: 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b  (rid, &content);
2710: 0a 20 20 20 20 6d 61 6e 69 66 65 73 74 5f 63 72  .    manifest_cr
2720: 6f 73 73 6c 69 6e 6b 28 72 69 64 2c 20 26 63 6f  osslink(rid, &co
2730: 6e 74 65 6e 74 29 3b 0a 20 20 20 20 62 6c 6f 62  ntent);.    blob
2740: 5f 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 29  _reset(&content)
2750: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  ;.  }.}../*.** W
2760: 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74  rite content int
2770: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  o the database. 
2780: 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 63 6f   Return the reco
2790: 72 64 20 49 44 2e 20 20 49 66 20 74 68 65 0a 2a  rd ID.  If the.*
27a0: 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 6c 72  * content is alr
27b0: 65 61 64 79 20 69 6e 20 74 68 65 20 64 61 74 61  eady in the data
27c0: 62 61 73 65 2c 20 6a 75 73 74 20 72 65 74 75 72  base, just retur
27d0: 6e 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e  n the record ID.
27e0: 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 49 64 20  .**.** If srcId 
27f0: 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68  is specified, th
2800: 65 6e 20 70 42 6c 6f 62 20 69 73 20 64 65 6c 74  en pBlob is delt
2810: 61 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 0a 2a  a content from.*
2820: 2a 20 74 68 65 20 73 72 63 49 64 20 72 65 63 6f  * the srcId reco
2830: 72 64 2e 20 20 73 72 63 49 64 20 6d 69 67 68 74  rd.  srcId might
2840: 20 62 65 20 61 20 70 68 61 6e 74 6f 6d 2e 0a 2a   be a phantom..*
2850: 2a 0a 2a 2a 20 41 20 70 68 61 6e 74 6f 6d 20 69  *.** A phantom i
2860: 73 20 77 72 69 74 74 65 6e 20 69 66 20 70 42 6c  s written if pBl
2870: 6f 62 3d 3d 30 2e 20 20 49 66 20 70 42 6c 6f 62  ob==0.  If pBlob
2880: 3d 3d 30 20 6f 72 20 69 66 20 73 72 63 49 64 20  ==0 or if srcId 
2890: 69 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  is.** specified 
28a0: 74 68 65 6e 20 74 68 65 20 55 55 49 44 20 69 73  then the UUID is
28b0: 20 73 65 74 20 74 6f 20 7a 55 75 69 64 2e 20 20   set to zUuid.  
28c0: 4f 74 68 65 72 77 69 73 65 20 7a 55 75 69 64 20  Otherwise zUuid 
28d0: 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20  is.** ignored.  
28e0: 49 6e 20 74 68 65 20 66 75 74 75 72 65 20 74 68  In the future th
28f0: 69 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20  is might change 
2900: 73 75 63 68 20 74 68 61 74 20 74 68 65 20 63 6f  such that the co
2910: 6e 74 65 6e 74 0a 2a 2a 20 68 61 73 68 20 69 73  ntent.** hash is
2920: 20 63 68 65 63 6b 65 64 20 61 67 61 69 6e 73 74   checked against
2930: 20 7a 55 75 69 64 20 74 6f 20 6d 61 6b 65 20 73   zUuid to make s
2940: 75 72 65 20 69 74 20 69 73 20 63 6f 72 72 65 63  ure it is correc
2950: 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
2960: 72 65 63 6f 72 64 20 61 6c 72 65 61 64 79 20 65  record already e
2970: 78 69 73 74 73 20 62 75 74 20 69 73 20 61 20 70  xists but is a p
2980: 68 61 6e 74 6f 6d 2c 20 74 68 65 20 70 42 6c 6f  hantom, the pBlo
2990: 62 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20  b content.** is 
29a0: 69 6e 73 65 72 74 65 64 20 61 6e 64 20 74 68 65  inserted and the
29b0: 20 70 68 61 74 6f 6d 20 62 65 63 6f 6d 65 73 20   phatom becomes 
29c0: 61 20 72 65 61 6c 20 72 65 63 6f 72 64 2e 0a 2a  a real record..*
29d0: 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 70 75  /.int content_pu
29e0: 74 28 42 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63  t(Blob *pBlob, c
29f0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64  onst char *zUuid
2a00: 2c 20 69 6e 74 20 73 72 63 49 64 29 7b 0a 20 20  , int srcId){.  
2a10: 69 6e 74 20 73 69 7a 65 3b 0a 20 20 69 6e 74 20  int size;.  int 
2a20: 72 69 64 3b 0a 20 20 53 74 6d 74 20 73 31 3b 0a  rid;.  Stmt s1;.
2a30: 20 20 42 6c 6f 62 20 63 6d 70 72 3b 0a 20 20 42    Blob cmpr;.  B
2a40: 6c 6f 62 20 68 61 73 68 3b 0a 20 20 69 6e 74 20  lob hash;.  int 
2a50: 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74 65 72 65  markAsUnclustere
2a60: 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 73 44  d = 0;.  int isD
2a70: 65 70 68 61 6e 74 6f 6d 69 7a 65 20 3d 20 30 3b  ephantomize = 0;
2a80: 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 67 2e  .  .  assert( g.
2a90: 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29  repositoryOpen )
2aa0: 3b 0a 20 20 69 66 28 20 70 42 6c 6f 62 20 26 26  ;.  if( pBlob &&
2ab0: 20 73 72 63 49 64 3d 3d 30 20 29 7b 0a 20 20 20   srcId==0 ){.   
2ac0: 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 70 42   sha1sum_blob(pB
2ad0: 6c 6f 62 2c 20 26 68 61 73 68 29 3b 0a 20 20 7d  lob, &hash);.  }
2ae0: 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f 69  else{.    blob_i
2af0: 6e 69 74 28 26 68 61 73 68 2c 20 7a 55 75 69 64  nit(&hash, zUuid
2b00: 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28  , -1);.  }.  if(
2b10: 20 70 42 6c 6f 62 3d 3d 30 20 29 7b 0a 20 20 20   pBlob==0 ){.   
2b20: 20 73 69 7a 65 20 3d 20 2d 31 3b 0a 20 20 7d 65   size = -1;.  }e
2b30: 6c 73 65 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20  lse{.    size = 
2b40: 62 6c 6f 62 5f 73 69 7a 65 28 70 42 6c 6f 62 29  blob_size(pBlob)
2b50: 3b 0a 20 20 7d 0a 20 20 64 62 5f 62 65 67 69 6e  ;.  }.  db_begin
2b60: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a  _transaction();.
2b70: 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73  .  /* Check to s
2b80: 65 65 20 69 66 20 74 68 65 20 65 6e 74 72 79 20  ee if the entry 
2b90: 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61  already exists a
2ba0: 6e 64 20 69 66 20 69 74 20 64 6f 65 73 20 77 68  nd if it does wh
2bb0: 65 74 68 65 72 0a 20 20 2a 2a 20 6f 72 20 6e 6f  ether.  ** or no
2bc0: 74 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 61  t the entry is a
2bd0: 20 70 68 61 6e 74 6f 6d 0a 20 20 2a 2f 0a 20 20   phantom.  */.  
2be0: 64 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20  db_prepare(&s1, 
2bf0: 22 53 45 4c 45 43 54 20 72 69 64 2c 20 73 69 7a  "SELECT rid, siz
2c00: 65 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  e FROM blob WHER
2c10: 45 20 75 75 69 64 3d 25 42 22 2c 20 26 68 61 73  E uuid=%B", &has
2c20: 68 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65  h);.  if( db_ste
2c30: 70 28 26 73 31 29 3d 3d 53 51 4c 49 54 45 5f 52  p(&s1)==SQLITE_R
2c40: 4f 57 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20  OW ){.    rid = 
2c50: 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73  db_column_int(&s
2c60: 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64  1, 0);.    if( d
2c70: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 31  b_column_int(&s1
2c80: 2c 20 31 29 3e 3d 30 20 7c 7c 20 70 42 6c 6f 62  , 1)>=0 || pBlob
2c90: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
2ca0: 45 69 74 68 65 72 20 74 68 65 20 65 6e 74 72 79  Either the entry
2cb0: 20 69 73 20 6e 6f 74 20 61 20 70 68 61 6e 74 6f   is not a phanto
2cc0: 6d 20 6f 72 20 69 74 20 69 73 20 61 20 70 68 61  m or it is a pha
2cd0: 6e 74 6f 6d 20 62 75 74 20 77 65 0a 20 20 20 20  ntom but we.    
2ce0: 20 20 2a 2a 20 68 61 76 65 20 6e 6f 20 64 61 74    ** have no dat
2cf0: 61 20 77 69 74 68 20 77 68 69 63 68 20 74 6f 20  a with which to 
2d00: 64 65 70 68 61 6e 74 6f 6d 69 7a 65 20 69 74 2e  dephantomize it.
2d10: 20 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65    In either case
2d20: 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65  ,.      ** there
2d30: 20 69 73 20 6e 6f 74 68 69 6e 67 20 66 6f 72 20   is nothing for 
2d40: 75 73 20 74 6f 20 64 6f 20 6f 74 68 65 72 20 74  us to do other t
2d50: 68 61 6e 20 72 65 74 75 72 6e 20 74 68 65 20 52  han return the R
2d60: 49 44 2e 20 2a 2f 0a 20 20 20 20 20 20 64 62 5f  ID. */.      db_
2d70: 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20  finalize(&s1);. 
2d80: 20 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e       db_end_tran
2d90: 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20 20  saction(0);.    
2da0: 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 20 20    return rid;.  
2db0: 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
2dc0: 20 72 69 64 20 3d 20 30 3b 20 20 2f 2a 20 4e 6f   rid = 0;  /* No
2dd0: 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 65 20   entry with the 
2de0: 73 61 6d 65 20 55 55 49 44 20 63 75 72 72 65 6e  same UUID curren
2df0: 74 6c 79 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  tly exists */.  
2e00: 20 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74 65    markAsUncluste
2e10: 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 64  red = 1;.  }.  d
2e20: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b  b_finalize(&s1);
2e30: 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74  ..  /* Construct
2e40: 20 61 20 72 65 63 65 69 76 65 64 2d 66 72 6f 6d   a received-from
2e50: 20 49 44 20 69 66 20 77 65 20 64 6f 20 6e 6f 74   ID if we do not
2e60: 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 6f 6e   already have on
2e70: 65 20 2a 2f 0a 20 20 69 66 28 20 67 2e 72 63 76  e */.  if( g.rcv
2e80: 69 64 3d 3d 30 20 26 26 20 70 42 6c 6f 62 21 3d  id==0 && pBlob!=
2e90: 30 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74  0 ){.    db_mult
2ea0: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 22  i_exec(.       "
2eb0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 72 63 76 66  INSERT INTO rcvf
2ec0: 72 6f 6d 28 75 69 64 2c 20 6d 74 69 6d 65 2c 20  rom(uid, mtime, 
2ed0: 6e 6f 6e 63 65 2c 20 69 70 61 64 64 72 29 22 0a  nonce, ipaddr)".
2ee0: 20 20 20 20 20 20 20 22 56 41 4c 55 45 53 28 25         "VALUES(%
2ef0: 64 2c 20 6a 75 6c 69 61 6e 64 61 79 28 27 6e 6f  d, julianday('no
2f00: 77 27 29 2c 20 25 51 2c 20 25 51 29 22 2c 0a 20  w'), %Q, %Q)",. 
2f10: 20 20 20 20 20 20 67 2e 75 73 65 72 55 69 64 2c        g.userUid,
2f20: 20 67 2e 7a 4e 6f 6e 63 65 2c 20 67 2e 7a 49 70   g.zNonce, g.zIp
2f30: 41 64 64 72 0a 20 20 20 20 29 3b 0a 20 20 20 20  Addr.    );.    
2f40: 67 2e 72 63 76 69 64 20 3d 20 64 62 5f 6c 61 73  g.rcvid = db_las
2f50: 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
2f60: 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 69 64  ;.  }..  if( rid
2f70: 3e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20  >0 ){.    /* We 
2f80: 61 72 65 20 6a 75 73 74 20 61 64 64 69 6e 67 20  are just adding 
2f90: 64 61 74 61 20 74 6f 20 61 20 70 68 61 6e 74 6f  data to a phanto
2fa0: 6d 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  m */.    assert(
2fb0: 20 70 42 6c 6f 62 21 3d 30 20 29 3b 0a 20 20 20   pBlob!=0 );.   
2fc0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c   db_prepare(&s1,
2fd0: 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 62  .      "UPDATE b
2fe0: 6c 6f 62 20 53 45 54 20 72 63 76 69 64 3d 25 64  lob SET rcvid=%d
2ff0: 2c 20 73 69 7a 65 3d 25 64 2c 20 63 6f 6e 74 65  , size=%d, conte
3000: 6e 74 3d 3a 64 61 74 61 20 57 48 45 52 45 20 72  nt=:data WHERE r
3010: 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 20 67  id=%d",.       g
3020: 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c 20 72 69  .rcvid, size, ri
3030: 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 62 6c 6f  d.    );.    blo
3040: 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f 62  b_compress(pBlob
3050: 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 64 62  , &cmpr);.    db
3060: 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c 20  _bind_blob(&s1, 
3070: 22 3a 64 61 74 61 22 2c 20 26 63 6d 70 72 29 3b  ":data", &cmpr);
3080: 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 73 31  .    db_exec(&s1
3090: 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  );.    db_multi_
30a0: 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f  exec("DELETE FRO
30b0: 4d 20 70 68 61 6e 74 6f 6d 20 57 48 45 52 45 20  M phantom WHERE 
30c0: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20  rid=%d", rid);. 
30d0: 20 20 20 69 66 28 20 73 72 63 49 64 3d 3d 30 20     if( srcId==0 
30e0: 7c 7c 20 63 6f 6e 74 65 6e 74 5f 69 73 5f 61 76  || content_is_av
30f0: 61 69 6c 61 62 6c 65 28 73 72 63 49 64 29 20 29  ailable(srcId) )
3100: 7b 0a 20 20 20 20 20 20 69 73 44 65 70 68 61 6e  {.      isDephan
3110: 74 6f 6d 69 7a 65 20 3d 20 31 3b 0a 20 20 20 20  tomize = 1;.    
3120: 20 20 63 6f 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61    content_mark_a
3130: 76 61 69 6c 61 62 6c 65 28 72 69 64 29 3b 0a 20  vailable(rid);. 
3140: 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
3150: 20 20 2f 2a 20 57 65 20 61 72 65 20 63 72 65 61    /* We are crea
3160: 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79  ting a new entry
3170: 20 2a 2f 0a 20 20 20 20 64 62 5f 70 72 65 70 61   */.    db_prepa
3180: 72 65 28 26 73 31 2c 0a 20 20 20 20 20 20 22 49  re(&s1,.      "I
3190: 4e 53 45 52 54 20 49 4e 54 4f 20 62 6c 6f 62 28  NSERT INTO blob(
31a0: 72 63 76 69 64 2c 73 69 7a 65 2c 75 75 69 64 2c  rcvid,size,uuid,
31b0: 63 6f 6e 74 65 6e 74 29 22 0a 20 20 20 20 20 20  content)".      
31c0: 22 56 41 4c 55 45 53 28 25 64 2c 25 64 2c 27 25  "VALUES(%d,%d,'%
31d0: 62 27 2c 3a 64 61 74 61 29 22 2c 0a 20 20 20 20  b',:data)",.    
31e0: 20 20 20 67 2e 72 63 76 69 64 2c 20 73 69 7a 65     g.rcvid, size
31f0: 2c 20 26 68 61 73 68 0a 20 20 20 20 29 3b 0a 20  , &hash.    );. 
3200: 20 20 20 69 66 28 20 70 42 6c 6f 62 20 29 7b 0a     if( pBlob ){.
3210: 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72        blob_compr
3220: 65 73 73 28 70 42 6c 6f 62 2c 20 26 63 6d 70 72  ess(pBlob, &cmpr
3230: 29 3b 0a 20 20 20 20 20 20 64 62 5f 62 69 6e 64  );.      db_bind
3240: 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74  _blob(&s1, ":dat
3250: 61 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20  a", &cmpr);.    
3260: 7d 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 73  }.    db_exec(&s
3270: 31 29 3b 0a 20 20 20 20 72 69 64 20 3d 20 64 62  1);.    rid = db
3280: 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
3290: 69 64 28 29 3b 0a 20 20 20 20 69 66 28 20 21 70  id();.    if( !p
32a0: 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 64 62  Blob ){.      db
32b0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53  _multi_exec("INS
32c0: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e  ERT OR IGNORE IN
32d0: 54 4f 20 70 68 61 6e 74 6f 6d 20 56 41 4c 55 45  TO phantom VALUE
32e0: 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a 20 20  S(%d)", rid);.  
32f0: 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
3300: 20 74 68 65 20 73 72 63 49 64 20 69 73 20 73 70   the srcId is sp
3310: 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 74 68  ecified, then th
3320: 65 20 64 61 74 61 20 77 65 20 6a 75 73 74 20 61  e data we just a
3330: 64 64 65 64 20 69 73 0a 20 20 2a 2a 20 72 65 61  dded is.  ** rea
3340: 6c 6c 79 20 61 20 64 65 6c 74 61 2e 20 20 52 65  lly a delta.  Re
3350: 63 6f 72 64 20 74 68 69 73 20 66 61 63 74 20 69  cord this fact i
3360: 6e 20 74 68 65 20 64 65 6c 74 61 20 74 61 62 6c  n the delta tabl
3370: 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 72  e..  */.  if( sr
3380: 63 49 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75  cId ){.    db_mu
3390: 6c 74 69 5f 65 78 65 63 28 22 52 45 50 4c 41 43  lti_exec("REPLAC
33a0: 45 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69 64  E INTO delta(rid
33b0: 2c 73 72 63 69 64 29 20 56 41 4c 55 45 53 28 25  ,srcid) VALUES(%
33c0: 64 2c 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63  d,%d)", rid, src
33d0: 49 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  Id);.  }.  if( !
33e0: 69 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65 20 26  isDephantomize &
33f0: 26 20 62 61 67 5f 66 69 6e 64 28 26 63 6f 6e 74  & bag_find(&cont
3400: 65 6e 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67  entCache.missing
3410: 2c 20 72 69 64 29 20 26 26 20 0a 20 20 20 20 20  , rid) && .     
3420: 20 28 73 72 63 49 64 3d 3d 30 20 7c 7c 20 63 6f   (srcId==0 || co
3430: 6e 74 65 6e 74 5f 69 73 5f 61 76 61 69 6c 61 62  ntent_is_availab
3440: 6c 65 28 73 72 63 49 64 29 29 20 29 7b 0a 20 20  le(srcId)) ){.  
3450: 20 20 63 6f 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61    content_mark_a
3460: 76 61 69 6c 61 62 6c 65 28 72 69 64 29 3b 0a 20  vailable(rid);. 
3470: 20 7d 0a 20 20 69 66 28 20 69 73 44 65 70 68 61   }.  if( isDepha
3480: 6e 74 6f 6d 69 7a 65 20 29 7b 0a 20 20 20 20 61  ntomize ){.    a
3490: 66 74 65 72 5f 64 65 70 68 61 6e 74 6f 6d 69 7a  fter_dephantomiz
34a0: 65 28 72 69 64 2c 20 30 29 3b 0a 20 20 7d 0a 20  e(rid, 0);.  }. 
34b0: 20 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65   .  /* Add the e
34c0: 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 6e  lement to the un
34d0: 63 6c 75 73 74 65 72 65 64 20 74 61 62 6c 65 20  clustered table 
34e0: 69 66 20 68 61 73 20 6e 65 76 65 72 20 62 65 65  if has never bee
34f0: 6e 0a 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c  n.  ** previousl
3500: 79 20 73 65 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69  y seen..  */.  i
3510: 66 28 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74  f( markAsUnclust
3520: 65 72 65 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d  ered ){.    db_m
3530: 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52  ulti_exec("INSER
3540: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f  T OR IGNORE INTO
3550: 20 75 6e 63 6c 75 73 74 65 72 65 64 20 56 41 4c   unclustered VAL
3560: 55 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a  UES(%d)", rid);.
3570: 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 73 68    }..  /* Finish
3580: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
3590: 20 61 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f 0a   and cleanup */.
35a0: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
35b0: 31 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61  1);.  db_end_tra
35c0: 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 62  nsaction(0);.  b
35d0: 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29  lob_reset(&hash)
35e0: 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72 72  ;..  /* Make arr
35f0: 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 76 65 72  angements to ver
3600: 69 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74  ify that the dat
3610: 61 20 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72  a can be recover
3620: 65 64 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 77  ed.  ** before w
3630: 65 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 69 66  e commit */.  if
3640: 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 62  ( pBlob ){.    b
3650: 6c 6f 62 5f 72 65 73 65 74 28 26 63 6d 70 72 29  lob_reset(&cmpr)
3660: 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 65 66  ;.    verify_bef
3670: 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b  ore_commit(rid);
3680: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 69  .  }.  return ri
3690: 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d  d;.}../*.** COMM
36a0: 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65  AND:  test-conte
36b0: 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a 20 45 78 74  nt-put.**.** Ext
36c0: 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d  ract a blob from
36d0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
36e0: 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20  d write it into 
36f0: 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20  a file..*/.void 
3700: 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 70 75 74  test_content_put
3710: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e  _cmd(void){.  in
3720: 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f  t rid;.  Blob co
3730: 6e 74 65 6e 74 3b 0a 20 20 69 66 28 20 67 2e 61  ntent;.  if( g.a
3740: 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 22  rgc!=3 ) usage("
3750: 46 49 4c 45 4e 41 4d 45 22 29 3b 0a 20 20 64 62  FILENAME");.  db
3760: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
3770: 74 72 65 65 28 29 3b 0a 20 20 75 73 65 72 5f 73  tree();.  user_s
3780: 65 6c 65 63 74 28 29 3b 0a 20 20 62 6c 6f 62 5f  elect();.  blob_
3790: 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26  read_from_file(&
37a0: 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 72 67 76 5b  content, g.argv[
37b0: 32 5d 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f 6e  2]);.  rid = con
37c0: 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e  tent_put(&conten
37d0: 74 2c 20 30 2c 20 30 29 3b 0a 20 20 70 72 69 6e  t, 0, 0);.  prin
37e0: 74 66 28 22 69 6e 73 65 72 74 65 64 20 61 73 20  tf("inserted as 
37f0: 72 65 63 6f 72 64 20 25 64 5c 6e 22 2c 20 72 69  record %d\n", ri
3800: 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  d);.}../*.** Mak
3810: 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e 74 65  e sure the conte
3820: 6e 74 20 61 74 20 72 69 64 20 69 73 20 74 68 65  nt at rid is the
3830: 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e   original conten
3840: 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 0a 2a  t and is not a.*
3850: 2a 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 64  * delta..*/.void
3860: 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61   content_undelta
3870: 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 66 28  (int rid){.  if(
3880: 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 29 3e   findSrcid(rid)>
3890: 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 78 3b  0 ){.    Blob x;
38a0: 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74  .    if( content
38b0: 5f 67 65 74 28 72 69 64 2c 20 26 78 29 20 29 7b  _get(rid, &x) ){
38c0: 0a 20 20 20 20 20 20 53 74 6d 74 20 73 3b 0a 20  .      Stmt s;. 
38d0: 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28       db_prepare(
38e0: 26 73 2c 20 22 55 50 44 41 54 45 20 62 6c 6f 62  &s, "UPDATE blob
38f0: 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 63 2c   SET content=:c,
3900: 20 73 69 7a 65 3d 25 64 20 57 48 45 52 45 20 72   size=%d WHERE r
3910: 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 20 20  id=%d",.        
3920: 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f               blo
3930: 62 5f 73 69 7a 65 28 26 78 29 2c 20 72 69 64 29  b_size(&x), rid)
3940: 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 6d  ;.      blob_com
3950: 70 72 65 73 73 28 26 78 2c 20 26 78 29 3b 0a 20  press(&x, &x);. 
3960: 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f       db_bind_blo
3970: 62 28 26 73 2c 20 22 3a 63 22 2c 20 26 78 29 3b  b(&s, ":c", &x);
3980: 0a 20 20 20 20 20 20 64 62 5f 65 78 65 63 28 26  .      db_exec(&
3990: 73 29 3b 0a 20 20 20 20 20 20 64 62 5f 66 69 6e  s);.      db_fin
39a0: 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 20 20 20  alize(&s);.     
39b0: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 78 29 3b   blob_reset(&x);
39c0: 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  .      db_multi_
39d0: 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f  exec("DELETE FRO
39e0: 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72 69  M delta WHERE ri
39f0: 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 20  d=%d", rid);.   
3a00: 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
3a10: 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63  COMMAND:  test-c
3a20: 6f 6e 74 65 6e 74 2d 75 6e 64 65 6c 74 61 0a 2a  ontent-undelta.*
3a30: 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  *.** Make sure t
3a40: 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 52 45  he content at RE
3a50: 43 4f 52 44 49 44 20 69 73 20 6e 6f 74 20 61 20  CORDID is not a 
3a60: 64 65 6c 74 61 0a 2a 2f 0a 76 6f 69 64 20 74 65  delta.*/.void te
3a70: 73 74 5f 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c  st_content_undel
3a80: 74 61 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20  ta_cmd(void){.  
3a90: 69 6e 74 20 72 69 64 3b 0a 20 20 69 66 28 20 67  int rid;.  if( g
3aa0: 2e 61 72 67 63 21 3d 32 20 29 20 75 73 61 67 65  .argc!=2 ) usage
3ab0: 28 22 52 45 43 4f 52 44 49 44 22 29 3b 0a 20 20  ("RECORDID");.  
3ac0: 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69  db_must_be_withi
3ad0: 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64 20  n_tree();.  rid 
3ae0: 3d 20 61 74 6f 69 28 67 2e 61 72 67 76 5b 32 5d  = atoi(g.argv[2]
3af0: 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 75 6e 64  );.  content_und
3b00: 65 6c 74 61 28 72 69 64 29 3b 0a 7d 0a 0a 2f 2a  elta(rid);.}../*
3b10: 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73  .** Change the s
3b20: 74 6f 72 61 67 65 20 6f 66 20 72 69 64 20 73 6f  torage of rid so
3b30: 20 74 68 61 74 20 69 74 20 69 73 20 61 20 64 65   that it is a de
3b40: 6c 74 61 20 6f 66 20 73 72 63 69 64 2e 0a 2a 2a  lta of srcid..**
3b50: 0a 2a 2a 20 49 66 20 72 69 64 20 69 73 20 61 6c  .** If rid is al
3b60: 72 65 61 64 79 20 61 20 64 65 6c 74 61 20 66 72  ready a delta fr
3b70: 6f 6d 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 6c  om some other pl
3b80: 61 63 65 20 74 68 65 6e 20 6e 6f 0a 2a 2a 20 63  ace then no.** c
3b90: 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73  onversion occurs
3ba0: 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 6e   and this is a n
3bb0: 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 66 6f 72 63  o-op unless forc
3bc0: 65 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  e==1..**.** If s
3bd0: 72 63 69 64 20 69 73 20 61 20 64 65 6c 74 61 20  rcid is a delta 
3be0: 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f 6e 20  that depends on 
3bf0: 72 69 64 2c 20 74 68 65 6e 20 73 72 63 69 64 20  rid, then srcid 
3c00: 69 73 0a 2a 2a 20 63 6f 6e 76 65 72 74 65 64 20  is.** converted 
3c10: 74 6f 20 75 6e 64 65 6c 74 61 65 64 20 74 65 78  to undeltaed tex
3c20: 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68  t..**.** If eith
3c30: 65 72 20 72 69 64 20 6f 72 20 73 72 63 69 64 20  er rid or srcid 
3c40: 63 6f 6e 74 61 69 6e 20 6c 65 73 73 20 74 68 61  contain less tha
3c50: 6e 20 35 30 20 62 79 74 65 73 2c 20 6f 72 20 69  n 50 bytes, or i
3c60: 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 69  f the.** resulti
3c70: 6e 67 20 64 65 6c 74 61 20 64 6f 65 73 20 6e 6f  ng delta does no
3c80: 74 20 61 63 68 69 65 76 65 20 61 20 63 6f 6d 70  t achieve a comp
3c90: 72 65 73 73 69 6f 6e 20 6f 66 20 61 74 20 6c 65  ression of at le
3ca0: 61 73 74 20 32 35 25 20 6f 6e 0a 2a 2a 20 69 74  ast 25% on.** it
3cb0: 73 20 6f 77 6e 20 74 68 65 20 72 69 64 20 69 73  s own the rid is
3cc0: 20 6c 65 66 74 20 75 6e 74 6f 75 63 68 65 64 2e   left untouched.
3cd0: 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 49 4d 48  .**.** NOTE: IMH
3ce0: 4f 20 74 68 65 20 63 72 65 61 74 69 6f 6e 20 6f  O the creation o
3cf0: 66 20 74 68 65 20 64 65 6c 74 61 20 73 68 6f 75  f the delta shou
3d00: 6c 64 20 62 65 20 64 65 66 65 72 65 64 20 75 6e  ld be defered un
3d10: 74 69 6c 20 61 66 74 65 72 0a 2a 2a 20 74 68 65  til after.** the
3d20: 20 62 6c 6f 62 20 73 69 7a 65 73 20 68 61 76 65   blob sizes have
3d30: 20 62 65 65 6e 20 63 68 65 63 6b 65 64 2e 20 44   been checked. D
3d40: 6f 69 6e 67 20 69 74 20 62 65 66 6f 72 65 20 74  oing it before t
3d50: 68 65 20 63 68 65 63 6b 20 61 73 20 69 73 0a 2a  he check as is.*
3d60: 2a 20 64 6f 6e 65 20 6e 6f 77 20 74 68 65 20 63  * done now the c
3d70: 6f 64 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74  ode will generat
3d80: 65 20 61 20 64 65 6c 74 61 20 6a 75 73 74 20 74  e a delta just t
3d90: 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 68  o immediately th
3da0: 72 6f 77 0a 2a 2a 20 69 74 20 61 77 61 79 2c 20  row.** it away, 
3db0: 77 61 73 74 69 6e 67 20 73 70 61 63 65 20 61 6e  wasting space an
3dc0: 64 20 74 69 6d 65 2e 0a 2a 2f 0a 76 6f 69 64 20  d time..*/.void 
3dd0: 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 28  content_deltify(
3de0: 69 6e 74 20 72 69 64 2c 20 69 6e 74 20 73 72 63  int rid, int src
3df0: 69 64 2c 20 69 6e 74 20 66 6f 72 63 65 29 7b 0a  id, int force){.
3e00: 20 20 69 6e 74 20 73 3b 0a 20 20 42 6c 6f 62 20    int s;.  Blob 
3e10: 64 61 74 61 2c 20 73 72 63 2c 20 64 65 6c 74 61  data, src, delta
3e20: 3b 0a 20 20 53 74 6d 74 20 73 31 2c 20 73 32 3b  ;.  Stmt s1, s2;
3e30: 0a 20 20 69 66 28 20 73 72 63 69 64 3d 3d 72 69  .  if( srcid==ri
3e40: 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  d ) return;.  if
3e50: 28 20 21 66 6f 72 63 65 20 26 26 20 66 69 6e 64  ( !force && find
3e60: 53 72 63 69 64 28 72 69 64 29 3e 30 20 29 20 72  Srcid(rid)>0 ) r
3e70: 65 74 75 72 6e 3b 0a 20 20 73 20 3d 20 73 72 63  eturn;.  s = src
3e80: 69 64 3b 0a 20 20 77 68 69 6c 65 28 20 28 73 20  id;.  while( (s 
3e90: 3d 20 66 69 6e 64 53 72 63 69 64 28 73 29 29 3e  = findSrcid(s))>
3ea0: 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 3d 3d  0 ){.    if( s==
3eb0: 72 69 64 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  rid ){.      con
3ec0: 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 73 72 63  tent_undelta(src
3ed0: 69 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  id);.      break
3ee0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f  ;.    }.  }.  co
3ef0: 6e 74 65 6e 74 5f 67 65 74 28 73 72 63 69 64 2c  ntent_get(srcid,
3f00: 20 26 73 72 63 29 3b 0a 20 20 63 6f 6e 74 65 6e   &src);.  conten
3f10: 74 5f 67 65 74 28 72 69 64 2c 20 26 64 61 74 61  t_get(rid, &data
3f20: 29 3b 0a 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f  );.  blob_delta_
3f30: 63 72 65 61 74 65 28 26 73 72 63 2c 20 26 64 61  create(&src, &da
3f40: 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 69  ta, &delta);.  i
3f50: 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 73 72  f( blob_size(&sr
3f60: 63 29 3e 3d 35 30 20 26 26 20 62 6c 6f 62 5f 73  c)>=50 && blob_s
3f70: 69 7a 65 28 26 64 61 74 61 29 3e 3d 35 30 20 26  ize(&data)>=50 &
3f80: 26 0a 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f  &.           blo
3f90: 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 20 3c  b_size(&delta) <
3fa0: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 61 74 61   blob_size(&data
3fb0: 29 2a 30 2e 37 35 20 29 7b 0a 20 20 20 20 62 6c  )*0.75 ){.    bl
3fc0: 6f 62 5f 63 6f 6d 70 72 65 73 73 28 26 64 65 6c  ob_compress(&del
3fd0: 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20  ta, &delta);.   
3fe0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c   db_prepare(&s1,
3ff0: 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45   "UPDATE blob SE
4000: 54 20 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20  T content=:data 
4010: 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72  WHERE rid=%d", r
4020: 69 64 29 3b 0a 20 20 20 20 64 62 5f 70 72 65 70  id);.    db_prep
4030: 61 72 65 28 26 73 32 2c 20 22 52 45 50 4c 41 43  are(&s2, "REPLAC
4040: 45 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69 64  E INTO delta(rid
4050: 2c 73 72 63 69 64 29 56 41 4c 55 45 53 28 25 64  ,srcid)VALUES(%d
4060: 2c 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63 69  ,%d)", rid, srci
4070: 64 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f  d);.    db_bind_
4080: 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61  blob(&s1, ":data
4090: 22 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  ", &delta);.    
40a0: 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63  db_begin_transac
40b0: 74 69 6f 6e 28 29 3b 0a 20 20 20 20 64 62 5f 65  tion();.    db_e
40c0: 78 65 63 28 26 73 31 29 3b 0a 20 20 20 20 64 62  xec(&s1);.    db
40d0: 5f 65 78 65 63 28 26 73 32 29 3b 0a 20 20 20 20  _exec(&s2);.    
40e0: 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  db_end_transacti
40f0: 6f 6e 28 30 29 3b 0a 20 20 20 20 64 62 5f 66 69  on(0);.    db_fi
4100: 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 20 20  nalize(&s1);.   
4110: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 32   db_finalize(&s2
4120: 29 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 65  );.    verify_be
4130: 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29  fore_commit(rid)
4140: 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73  ;.  }.  blob_res
4150: 65 74 28 26 73 72 63 29 3b 0a 20 20 62 6c 6f 62  et(&src);.  blob
4160: 5f 72 65 73 65 74 28 26 64 61 74 61 29 3b 0a 20  _reset(&data);. 
4170: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c   blob_reset(&del
4180: 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  ta);.}../*.** CO
4190: 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e  MMAND:  test-con
41a0: 74 65 6e 74 2d 64 65 6c 74 69 66 79 0a 2a 2a 0a  tent-deltify.**.
41b0: 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 63  ** Convert the c
41c0: 6f 6e 74 65 6e 74 20 61 74 20 52 49 44 20 69 6e  ontent at RID in
41d0: 74 6f 20 61 20 64 65 6c 74 61 20 66 72 6f 6d 20  to a delta from 
41e0: 53 52 43 49 44 2e 0a 2a 2f 0a 76 6f 69 64 20 74  SRCID..*/.void t
41f0: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 64 65 6c 74  est_content_delt
4200: 69 66 79 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  ify_cmd(void){. 
4210: 20 69 66 28 20 67 2e 61 72 67 63 21 3d 35 20 29   if( g.argc!=5 )
4220: 20 75 73 61 67 65 28 22 52 49 44 20 53 52 43 49   usage("RID SRCI
4230: 44 20 46 4f 52 43 45 22 29 3b 0a 20 20 64 62 5f  D FORCE");.  db_
4240: 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74  must_be_within_t
4250: 72 65 65 28 29 3b 0a 20 20 63 6f 6e 74 65 6e 74  ree();.  content
4260: 5f 64 65 6c 74 69 66 79 28 61 74 6f 69 28 67 2e  _deltify(atoi(g.
4270: 61 72 67 76 5b 32 5d 29 2c 20 61 74 6f 69 28 67  argv[2]), atoi(g
4280: 2e 61 72 67 76 5b 33 5d 29 2c 20 61 74 6f 69 28  .argv[3]), atoi(
4290: 67 2e 61 72 67 76 5b 34 5d 29 29 3b 0a 7d 0a     g.argv[4]));.}.