Hex Artifact Content
Not logged in

Artifact 933b11dafd94c00939791121c8ed618b2e873d78:

File src/content.c part of check-in [f763b84198] - Fix a bug introduced by recent optimization efforts. by drh on 2008-03-10 16:46:40.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 36 20 44 2e 20 52 69 63 68  (c) 2006 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 50 72 6f 63 65 64 75 72 65  .**.** Procedure
0370: 73 20 73 74 6f 72 65 20 61 6e 64 20 72 65 74 72  s store and retr
0380: 69 65 76 65 20 72 65 63 6f 72 64 73 20 66 72 6f  ieve records fro
0390: 6d 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79  m the repository
03a0: 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f  .*/.#include "co
03b0: 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65  nfig.h".#include
03c0: 20 22 63 6f 6e 74 65 6e 74 2e 68 22 0a 23 69 6e   "content.h".#in
03d0: 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e  clude <assert.h>
03e0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
03f0: 6f 72 20 64 65 62 75 67 67 69 6e 67 0a 2a 2f 0a  or debugging.*/.
0400: 23 69 66 20 30 0a 23 20 64 65 66 69 6e 65 20 43  #if 0.# define C
0410: 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28 58 29 20  ONTENT_TRACE(X) 
0420: 20 70 72 69 6e 74 66 20 58 3b 0a 23 65 6c 73 65   printf X;.#else
0430: 0a 23 20 64 65 66 69 6e 65 20 43 4f 4e 54 45 4e  .# define CONTEN
0440: 54 5f 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69  T_TRACE(X).#endi
0450: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 74  f../*.** The art
0460: 69 66 61 63 74 20 72 65 74 72 69 76 61 6c 20 63  ifact retrival c
0470: 61 63 68 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ache.*/.#define 
0480: 4d 58 5f 43 41 43 48 45 5f 43 4e 54 20 20 35 30  MX_CACHE_CNT  50
0490: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e      /* Maximum n
04a0: 75 6d 62 65 72 20 6f 66 20 70 6f 73 69 74 69 76  umber of positiv
04b0: 65 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 20  e cache entries 
04c0: 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 45 4c  */.#define EXPEL
04d0: 4c 5f 49 4e 54 45 52 56 41 4c 20 35 20 20 20 2f  L_INTERVAL 5   /
04e0: 2a 20 48 6f 77 20 6f 66 74 65 6e 20 74 6f 20 65  * How often to e
04f0: 78 70 65 6c 6c 20 66 72 6f 6d 20 61 20 66 75 6c  xpell from a ful
0500: 6c 20 63 61 63 68 65 20 2a 2f 0a 73 74 61 74 69  l cache */.stati
0510: 63 20 73 74 72 75 63 74 20 7b 0a 20 20 69 6e 74  c struct {.  int
0520: 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   n;             
0530: 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d    /* Current num
0540: 62 65 72 20 6f 66 20 70 6f 73 69 74 69 76 65 20  ber of positive 
0550: 63 61 63 68 65 20 65 6e 74 72 69 65 73 20 2a 2f  cache entries */
0560: 0a 20 20 69 6e 74 20 6e 65 78 74 41 67 65 3b 20  .  int nextAge; 
0570: 20 20 20 20 20 20 20 20 2f 2a 20 41 67 65 20 63          /* Age c
0580: 6f 75 6e 74 65 72 20 66 6f 72 20 69 6d 70 6c 65  ounter for imple
0590: 6d 65 6e 74 69 6e 67 20 4c 52 55 20 2a 2f 0a 20  menting LRU */. 
05a0: 20 69 6e 74 20 73 6b 69 70 43 6e 74 3b 20 20 20   int skipCnt;   
05b0: 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
05c0: 20 6c 69 6d 69 74 20 65 6e 74 72 69 65 73 20 65   limit entries e
05d0: 78 70 65 6c 6c 65 64 20 66 72 6f 6d 20 63 61 63  xpelled from cac
05e0: 68 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 7b  he */.  struct {
05f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
0600: 4f 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  One instance of 
0610: 74 68 69 73 20 66 6f 72 20 65 61 63 68 20 63 61  this for each ca
0620: 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20  che entry */.   
0630: 20 69 6e 74 20 72 69 64 3b 20 20 20 20 20 20 20   int rid;       
0640: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
0650: 74 69 66 61 63 74 20 69 64 20 2a 2f 0a 20 20 20  tifact id */.   
0660: 20 69 6e 74 20 61 67 65 3b 20 20 20 20 20 20 20   int age;       
0670: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67             /* Ag
0680: 65 2e 20 20 4e 65 77 65 72 20 69 73 20 6c 61 72  e.  Newer is lar
0690: 67 65 72 20 2a 2f 0a 20 20 20 20 42 6c 6f 62 20  ger */.    Blob 
06a0: 63 6f 6e 74 65 6e 74 3b 20 20 20 20 20 20 20 20  content;        
06b0: 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20       /* Content 
06c0: 6f 66 20 74 68 65 20 61 72 74 69 66 61 63 74 20  of the artifact 
06d0: 2a 2f 0a 20 20 7d 20 61 5b 4d 58 5f 43 41 43 48  */.  } a[MX_CACH
06e0: 45 5f 43 4e 54 5d 3b 20 20 20 2f 2a 20 54 68 65  E_CNT];   /* The
06f0: 20 70 6f 73 69 74 69 76 65 20 63 61 63 68 65 20   positive cache 
0700: 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  */..  /*.  ** Th
0710: 65 20 6d 69 73 73 69 6e 67 20 61 72 74 69 66 61  e missing artifa
0720: 63 74 20 63 61 63 68 65 2e 0a 20 20 2a 2a 0a 20  ct cache..  **. 
0730: 20 2a 2a 20 41 72 74 69 66 61 63 74 73 20 77 68   ** Artifacts wh
0740: 6f 73 65 20 72 65 63 6f 72 64 20 49 44 20 61 72  ose record ID ar
0750: 65 20 69 6e 20 6d 69 73 73 69 6e 67 43 61 63 68  e in missingCach
0760: 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 74 72  e cannot be retr
0770: 69 65 76 65 64 0a 20 20 2a 2a 20 65 69 74 68 65  ieved.  ** eithe
0780: 72 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61  r because they a
0790: 72 65 20 70 68 61 6e 74 6f 6d 73 20 6f 72 20 62  re phantoms or b
07a0: 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20  ecause they are 
07b0: 61 20 64 65 6c 74 61 20 74 68 61 74 0a 20 20 2a  a delta that.  *
07c0: 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 61 20 70  * depends on a p
07d0: 68 61 6e 74 6f 6d 2e 20 20 41 72 74 69 66 61 63  hantom.  Artifac
07e0: 74 73 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74  ts whose content
07f0: 20 77 65 20 61 72 65 20 63 65 72 74 61 69 6e 20   we are certain 
0800: 69 73 0a 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c  is.  ** availabl
0810: 65 20 61 72 65 20 69 6e 20 61 76 61 69 6c 61 62  e are in availab
0820: 6c 65 43 61 63 68 65 2e 20 20 49 66 20 61 6e 20  leCache.  If an 
0830: 61 72 74 69 66 61 63 74 20 69 73 20 69 6e 20 6e  artifact is in n
0840: 65 69 74 68 65 72 20 63 61 63 68 65 0a 20 20 2a  either cache.  *
0850: 2a 20 74 68 65 6e 20 69 74 73 20 63 75 72 72 65  * then its curre
0860: 6e 74 20 61 76 61 69 6c 61 62 6c 69 74 79 20 69  nt availablity i
0870: 73 20 75 6e 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a  s unknown..  */.
0880: 20 20 42 61 67 20 6d 69 73 73 69 6e 67 3b 20 20    Bag missing;  
0890: 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20         /* Cache 
08a0: 6f 66 20 61 72 74 69 66 61 63 74 73 20 74 68 61  of artifacts tha
08b0: 74 20 61 72 65 20 69 6e 63 6f 6d 70 6c 65 74 65  t are incomplete
08c0: 20 2a 2f 0a 20 20 42 61 67 20 61 76 61 69 6c 61   */.  Bag availa
08d0: 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 43 61  ble;       /* Ca
08e0: 63 68 65 20 6f 66 20 61 72 74 69 66 61 63 74 73  che of artifacts
08f0: 20 74 68 61 74 20 61 72 65 20 63 6f 6d 70 6c 65   that are comple
0900: 74 65 20 2a 2f 0a 7d 20 63 6f 6e 74 65 6e 74 43  te */.} contentC
0910: 61 63 68 65 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c  ache;.../*.** Cl
0920: 65 61 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ear the content 
0930: 63 61 63 68 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63  cache..*/.void c
0940: 6f 6e 74 65 6e 74 5f 63 6c 65 61 72 5f 63 61 63  ontent_clear_cac
0950: 68 65 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20  he(void){.  int 
0960: 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
0970: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 3b 20  contentCache.n; 
0980: 69 2b 2b 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 72  i++){.    blob_r
0990: 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 43 61 63  eset(&contentCac
09a0: 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74 65 6e 74 29  he.a[i].content)
09b0: 3b 0a 20 20 7d 0a 20 20 62 61 67 5f 63 6c 65 61  ;.  }.  bag_clea
09c0: 72 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e  r(&contentCache.
09d0: 6d 69 73 73 69 6e 67 29 3b 0a 20 20 62 61 67 5f  missing);.  bag_
09e0: 63 6c 65 61 72 28 26 63 6f 6e 74 65 6e 74 43 61  clear(&contentCa
09f0: 63 68 65 2e 61 76 61 69 6c 61 62 6c 65 29 3b 0a  che.available);.
0a00: 20 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e    contentCache.n
0a10: 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   = 0;.}../*.** R
0a20: 65 74 75 72 6e 20 74 68 65 20 73 72 63 69 64 20  eturn the srcid 
0a30: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
0a40: 72 69 64 2e 20 20 4f 72 20 72 65 74 75 72 6e 20  rid.  Or return 
0a50: 30 20 69 66 20 72 69 64 20 69 73 20 0a 2a 2a 20  0 if rid is .** 
0a60: 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74  original content
0a70: 20 61 6e 64 20 6e 6f 74 20 61 20 64 65 6c 74 61   and not a delta
0a80: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
0a90: 66 69 6e 64 53 72 63 69 64 28 69 6e 74 20 72 69  findSrcid(int ri
0aa0: 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53 74 6d  d){.  static Stm
0ab0: 74 20 71 3b 0a 20 20 69 6e 74 20 73 72 63 69 64  t q;.  int srcid
0ac0: 3b 0a 20 20 64 62 5f 73 74 61 74 69 63 5f 70 72  ;.  db_static_pr
0ad0: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
0ae0: 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 6c  T srcid FROM del
0af0: 74 61 20 57 48 45 52 45 20 72 69 64 3d 3a 72 69  ta WHERE rid=:ri
0b00: 64 22 29 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 69  d");.  db_bind_i
0b10: 6e 74 28 26 71 2c 20 22 3a 72 69 64 22 2c 20 72  nt(&q, ":rid", r
0b20: 69 64 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74  id);.  if( db_st
0b30: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(&q)==SQLITE_R
0b40: 4f 57 20 29 7b 0a 20 20 20 20 73 72 63 69 64 20  OW ){.    srcid 
0b50: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  = db_column_int(
0b60: 26 71 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b  &q, 0);.  }else{
0b70: 0a 20 20 20 20 73 72 63 69 64 20 3d 20 30 3b 0a  .    srcid = 0;.
0b80: 20 20 7d 0a 20 20 64 62 5f 72 65 73 65 74 28 26    }.  db_reset(&
0b90: 71 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 72 63  q);.  return src
0ba0: 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  id;.}../*.** Che
0bb0: 63 6b 20 74 6f 20 73 65 65 20 69 66 20 63 6f 6e  ck to see if con
0bc0: 74 65 6e 74 20 69 73 20 61 76 61 69 6c 61 62 6c  tent is availabl
0bd0: 65 20 66 6f 72 20 61 72 74 69 66 61 63 74 20 22  e for artifact "
0be0: 72 69 64 22 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  rid".  Return.**
0bf0: 20 74 72 75 65 20 69 66 20 69 74 20 69 73 2e 20   true if it is. 
0c00: 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 69 66   Return false if
0c10: 20 72 69 64 20 69 73 20 61 20 70 68 61 6e 74 6f   rid is a phanto
0c20: 6d 20 6f 72 20 64 65 70 65 6e 64 73 20 6f 6e 0a  m or depends on.
0c30: 2a 2a 20 61 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2f  ** a phantom..*/
0c40: 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 69 73 5f  .int content_is_
0c50: 61 76 61 69 6c 61 62 6c 65 28 69 6e 74 20 72 69  available(int ri
0c60: 64 29 7b 0a 20 20 69 6e 74 20 73 72 63 69 64 3b  d){.  int srcid;
0c70: 0a 20 20 69 66 28 20 62 61 67 5f 66 69 6e 64 28  .  if( bag_find(
0c80: 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69  &contentCache.mi
0c90: 73 73 69 6e 67 2c 20 72 69 64 29 20 29 7b 0a 20  ssing, rid) ){. 
0ca0: 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
0cb0: 0a 20 20 69 66 28 20 62 61 67 5f 66 69 6e 64 28  .  if( bag_find(
0cc0: 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 76  &contentCache.av
0cd0: 61 69 6c 61 62 6c 65 2c 20 72 69 64 29 20 29 7b  ailable, rid) ){
0ce0: 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
0cf0: 20 7d 0a 20 20 69 66 28 20 64 62 5f 69 6e 74 28   }.  if( db_int(
0d00: 2d 31 2c 20 22 53 45 4c 45 43 54 20 73 69 7a 65  -1, "SELECT size
0d10: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45   FROM blob WHERE
0d20: 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3c 30   rid=%d", rid)<0
0d30: 20 29 7b 0a 20 20 20 20 62 61 67 5f 69 6e 73 65   ){.    bag_inse
0d40: 72 74 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65  rt(&contentCache
0d50: 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64 29 3b 0a  .missing, rid);.
0d60: 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
0d70: 7d 0a 20 20 73 72 63 69 64 20 3d 20 66 69 6e 64  }.  srcid = find
0d80: 53 72 63 69 64 28 72 69 64 29 3b 0a 20 20 69 66  Srcid(rid);.  if
0d90: 28 20 73 72 63 69 64 3d 3d 30 20 29 7b 0a 20 20  ( srcid==0 ){.  
0da0: 20 20 62 61 67 5f 69 6e 73 65 72 74 28 26 63 6f    bag_insert(&co
0db0: 6e 74 65 6e 74 43 61 63 68 65 2e 61 76 61 69 6c  ntentCache.avail
0dc0: 61 62 6c 65 2c 20 72 69 64 29 3b 0a 20 20 20 20  able, rid);.    
0dd0: 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
0de0: 69 66 28 20 63 6f 6e 74 65 6e 74 5f 69 73 5f 61  if( content_is_a
0df0: 76 61 69 6c 61 62 6c 65 28 73 72 63 69 64 29 20  vailable(srcid) 
0e00: 29 7b 0a 20 20 20 20 62 61 67 5f 69 6e 73 65 72  ){.    bag_inser
0e10: 74 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e  t(&contentCache.
0e20: 61 76 61 69 6c 61 62 6c 65 2c 20 72 69 64 29 3b  available, rid);
0e30: 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
0e40: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 61 67 5f   }else{.    bag_
0e50: 69 6e 73 65 72 74 28 26 63 6f 6e 74 65 6e 74 43  insert(&contentC
0e60: 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69  ache.missing, ri
0e70: 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  d);.    return 0
0e80: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
0e90: 61 72 6b 20 61 72 74 69 66 61 63 74 20 72 69 64  ark artifact rid
0ea0: 20 61 73 20 62 65 69 6e 67 20 61 76 61 69 6c 61   as being availa
0eb0: 62 6c 65 20 6e 6f 77 2e 20 20 55 70 64 61 74 65  ble now.  Update
0ec0: 20 74 68 65 20 63 61 63 68 65 20 74 6f 0a 2a 2a   the cache to.**
0ed0: 20 73 68 6f 77 20 74 68 61 74 20 65 76 65 72 79   show that every
0ee0: 74 68 69 6e 67 20 74 68 61 74 20 77 61 73 20 66  thing that was f
0ef0: 6f 72 6d 65 72 6c 79 20 75 6e 61 76 61 69 6c 61  ormerly unavaila
0f00: 62 6c 65 20 62 65 63 61 75 73 65 20 72 69 64 0a  ble because rid.
0f10: 2a 2a 20 77 61 73 20 6d 69 73 73 69 6e 67 20 69  ** was missing i
0f20: 73 20 6e 6f 77 20 61 76 61 69 6c 61 62 6c 65 2e  s now available.
0f30: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
0f40: 63 6f 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61 76 61  content_mark_ava
0f50: 69 6c 61 62 6c 65 28 69 6e 74 20 72 69 64 29 7b  ilable(int rid){
0f60: 0a 20 20 42 61 67 20 70 65 6e 64 69 6e 67 3b 0a  .  Bag pending;.
0f70: 20 20 53 74 6d 74 20 71 3b 0a 20 20 69 66 28 20    Stmt q;.  if( 
0f80: 62 61 67 5f 66 69 6e 64 28 26 63 6f 6e 74 65 6e  bag_find(&conten
0f90: 74 43 61 63 68 65 2e 61 76 61 69 6c 61 62 6c 65  tCache.available
0fa0: 2c 20 72 69 64 29 20 29 20 72 65 74 75 72 6e 3b  , rid) ) return;
0fb0: 0a 20 20 62 61 67 5f 69 6e 69 74 28 26 70 65 6e  .  bag_init(&pen
0fc0: 64 69 6e 67 29 3b 0a 20 20 62 61 67 5f 69 6e 73  ding);.  bag_ins
0fd0: 65 72 74 28 26 70 65 6e 64 69 6e 67 2c 20 72 69  ert(&pending, ri
0fe0: 64 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 72 69  d);.  while( (ri
0ff0: 64 20 3d 20 62 61 67 5f 66 69 72 73 74 28 26 70  d = bag_first(&p
1000: 65 6e 64 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20  ending))!=0 ){. 
1010: 20 20 20 62 61 67 5f 72 65 6d 6f 76 65 28 26 70     bag_remove(&p
1020: 65 6e 64 69 6e 67 2c 20 72 69 64 29 3b 0a 20 20  ending, rid);.  
1030: 20 20 62 61 67 5f 72 65 6d 6f 76 65 28 26 63 6f    bag_remove(&co
1040: 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69 73 73 69  ntentCache.missi
1050: 6e 67 2c 20 72 69 64 29 3b 0a 20 20 20 20 62 61  ng, rid);.    ba
1060: 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e 74 65 6e  g_insert(&conten
1070: 74 43 61 63 68 65 2e 61 76 61 69 6c 61 62 6c 65  tCache.available
1080: 2c 20 72 69 64 29 3b 0a 20 20 20 20 64 62 5f 70  , rid);.    db_p
1090: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
10a0: 43 54 20 72 69 64 20 46 52 4f 4d 20 64 65 6c 74  CT rid FROM delt
10b0: 61 20 57 48 45 52 45 20 73 72 63 69 64 3d 25 64  a WHERE srcid=%d
10c0: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 77 68 69  ", rid);.    whi
10d0: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d  le( db_step(&q)=
10e0: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
10f0: 20 20 20 20 20 69 6e 74 20 6e 78 20 3d 20 64 62       int nx = db
1100: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20  _column_int(&q, 
1110: 30 29 3b 0a 20 20 20 20 20 20 62 61 67 5f 69 6e  0);.      bag_in
1120: 73 65 72 74 28 26 70 65 6e 64 69 6e 67 2c 20 6e  sert(&pending, n
1130: 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  x);.    }.    db
1140: 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20  _finalize(&q);. 
1150: 20 7d 0a 20 20 62 61 67 5f 63 6c 65 61 72 28 26   }.  bag_clear(&
1160: 70 65 6e 64 69 6e 67 29 3b 0a 7d 0a 0a 2f 2a 0a  pending);.}../*.
1170: 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 63  ** Extract the c
1180: 6f 6e 74 65 6e 74 20 66 6f 72 20 49 44 20 72 69  ontent for ID ri
1190: 64 20 61 6e 64 20 70 75 74 20 69 74 20 69 6e 74  d and put it int
11a0: 6f 20 74 68 65 0a 2a 2a 20 75 6e 69 6e 69 74 69  o the.** uniniti
11b0: 61 6c 69 7a 65 64 20 62 6c 6f 62 2e 20 20 52 65  alized blob.  Re
11c0: 74 75 72 6e 20 31 20 6f 6e 20 73 75 63 63 65 73  turn 1 on succes
11d0: 73 2e 20 20 49 66 20 74 68 65 20 72 65 63 6f 72  s.  If the recor
11e0: 64 0a 2a 2a 20 69 73 20 61 20 70 68 61 6e 74 6f  d.** is a phanto
11f0: 6d 2c 20 7a 65 72 6f 20 70 42 6c 6f 62 20 61 6e  m, zero pBlob an
1200: 64 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 69  d return 0..*/.i
1210: 6e 74 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 69  nt content_get(i
1220: 6e 74 20 72 69 64 2c 20 42 6c 6f 62 20 2a 70 42  nt rid, Blob *pB
1230: 6c 6f 62 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a  lob){.  Stmt q;.
1240: 20 20 42 6c 6f 62 20 73 72 63 3b 0a 20 20 69 6e    Blob src;.  in
1250: 74 20 73 72 63 69 64 3b 0a 20 20 69 6e 74 20 72  t srcid;.  int r
1260: 63 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a  c = 0;.  int i;.
1270: 20 20 73 74 61 74 69 63 20 42 61 67 20 69 6e 50    static Bag inP
1280: 72 6f 63 65 73 73 3b 0a 0a 20 20 61 73 73 65 72  rocess;..  asser
1290: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  t( g.repositoryO
12a0: 70 65 6e 20 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65  pen );.  blob_ze
12b0: 72 6f 28 70 42 6c 6f 62 29 3b 0a 0a 20 20 2f 2a  ro(pBlob);..  /*
12c0: 20 45 61 72 6c 79 20 6f 75 74 20 69 66 20 77 65   Early out if we
12d0: 20 6b 6e 6f 77 20 74 68 65 20 63 6f 6e 74 65 6e   know the conten
12e0: 74 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  t is not availab
12f0: 6c 65 20 2a 2f 0a 20 20 69 66 28 20 62 61 67 5f  le */.  if( bag_
1300: 66 69 6e 64 28 26 63 6f 6e 74 65 6e 74 43 61 63  find(&contentCac
1310: 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64 29  he.missing, rid)
1320: 20 29 7b 0a 20 20 20 20 43 4f 4e 54 45 4e 54 5f   ){.    CONTENT_
1330: 54 52 41 43 45 28 28 22 25 2a 73 6d 69 73 73 20  TRACE(("%*smiss 
1340: 66 72 6f 6d 20 63 61 63 68 65 3a 20 25 64 5c 6e  from cache: %d\n
1350: 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
1360: 20 20 20 20 20 20 20 62 61 67 5f 63 6f 75 6e 74         bag_count
1370: 28 26 69 6e 50 72 6f 63 65 73 73 29 2c 20 22 22  (&inProcess), ""
1380: 2c 20 72 69 64 29 29 0a 20 20 20 20 72 65 74 75  , rid)).    retu
1390: 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
13a0: 4c 6f 6f 6b 20 66 6f 72 20 74 68 65 20 61 72 74  Look for the art
13b0: 69 66 61 63 74 20 69 6e 20 74 68 65 20 63 61 63  ifact in the cac
13c0: 68 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 66 6f  he first */.  fo
13d0: 72 28 69 3d 30 3b 20 69 3c 63 6f 6e 74 65 6e 74  r(i=0; i<content
13e0: 43 61 63 68 65 2e 6e 3b 20 69 2b 2b 29 7b 0a 20  Cache.n; i++){. 
13f0: 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 43 61     if( contentCa
1400: 63 68 65 2e 61 5b 69 5d 2e 72 69 64 3d 3d 72 69  che.a[i].rid==ri
1410: 64 20 29 7b 0a 20 20 20 20 20 20 2a 70 42 6c 6f  d ){.      *pBlo
1420: 62 20 3d 20 63 6f 6e 74 65 6e 74 43 61 63 68 65  b = contentCache
1430: 2e 61 5b 69 5d 2e 63 6f 6e 74 65 6e 74 3b 0a 20  .a[i].content;. 
1440: 20 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26       blob_zero(&
1450: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69  contentCache.a[i
1460: 5d 2e 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20  ].content);.    
1470: 20 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e    contentCache.n
1480: 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3c  --;.      if( i<
1490: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 20 29  contentCache.n )
14a0: 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e  {.        conten
14b0: 74 43 61 63 68 65 2e 61 5b 69 5d 20 3d 20 63 6f  tCache.a[i] = co
14c0: 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 63 6f 6e  ntentCache.a[con
14d0: 74 65 6e 74 43 61 63 68 65 2e 6e 5d 3b 0a 20 20  tentCache.n];.  
14e0: 20 20 20 20 7d 0a 20 20 20 20 20 20 43 4f 4e 54      }.      CONT
14f0: 45 4e 54 5f 54 52 41 43 45 28 28 22 25 2a 73 68  ENT_TRACE(("%*sh
1500: 69 74 20 63 61 63 68 65 3a 20 25 64 5c 6e 22 2c  it cache: %d\n",
1510: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1520: 20 20 20 20 20 20 62 61 67 5f 63 6f 75 6e 74 28        bag_count(
1530: 26 69 6e 50 72 6f 63 65 73 73 29 2c 20 22 22 2c  &inProcess), "",
1540: 20 72 69 64 29 29 0a 20 20 20 20 20 20 72 65 74   rid)).      ret
1550: 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
1560: 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65  ..  /* See if we
1570: 20 6e 65 65 64 20 74 6f 20 61 70 70 6c 79 20 61   need to apply a
1580: 20 64 65 6c 74 61 20 74 6f 20 66 69 6e 64 20 74   delta to find t
1590: 68 69 73 20 61 72 74 69 66 61 63 74 20 2a 2f 0a  his artifact */.
15a0: 20 20 73 72 63 69 64 20 3d 20 66 69 6e 64 53 72    srcid = findSr
15b0: 63 69 64 28 72 69 64 29 3b 0a 20 20 43 4f 4e 54  cid(rid);.  CONT
15c0: 45 4e 54 5f 54 52 41 43 45 28 28 22 25 2a 73 73  ENT_TRACE(("%*ss
15d0: 65 61 72 63 68 69 6e 67 20 66 6f 72 20 25 64 2e  earching for %d.
15e0: 20 20 4e 65 65 64 20 25 64 2e 5c 6e 22 2c 0a 20    Need %d.\n",. 
15f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1600: 62 61 67 5f 63 6f 75 6e 74 28 26 69 6e 50 72 6f  bag_count(&inPro
1610: 63 65 73 73 29 2c 20 22 22 2c 20 72 69 64 2c 20  cess), "", rid, 
1620: 73 72 63 69 64 29 29 0a 0a 0a 20 20 69 66 28 20  srcid))...  if( 
1630: 73 72 63 69 64 20 29 7b 0a 20 20 20 20 2f 2a 20  srcid ){.    /* 
1640: 59 65 73 2c 20 61 20 64 65 6c 74 61 20 69 73 20  Yes, a delta is 
1650: 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20  required */.    
1660: 69 66 28 20 62 61 67 5f 66 69 6e 64 28 26 69 6e  if( bag_find(&in
1670: 50 72 6f 63 65 73 73 2c 20 73 72 63 69 64 29 20  Process, srcid) 
1680: 29 7b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74  ){.      db_mult
1690: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 20  i_exec(.        
16a0: 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54  "UPDATE blob SET
16b0: 20 63 6f 6e 74 65 6e 74 3d 4e 55 4c 4c 2c 20 73   content=NULL, s
16c0: 69 7a 65 3d 2d 31 20 57 48 45 52 45 20 72 69 64  ize=-1 WHERE rid
16d0: 3d 25 64 3b 22 0a 20 20 20 20 20 20 20 20 22 44  =%d;".        "D
16e0: 45 4c 45 54 45 20 46 52 4f 4d 20 64 65 6c 74 61  ELETE FROM delta
16f0: 20 57 48 45 52 45 20 72 69 64 3d 25 64 3b 22 0a   WHERE rid=%d;".
1700: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
1710: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70  OR IGNORE INTO p
1720: 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53 28 25 64  hantom VALUES(%d
1730: 29 3b 22 2c 0a 20 20 20 20 20 20 20 20 73 72 63  );",.        src
1740: 69 64 2c 20 73 72 63 69 64 2c 20 73 72 63 69 64  id, srcid, srcid
1750: 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
1760: 62 6c 6f 62 5f 7a 65 72 6f 28 70 42 6c 6f 62 29  blob_zero(pBlob)
1770: 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
1780: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 61 67 5f  ;.    }.    bag_
1790: 69 6e 73 65 72 74 28 26 69 6e 50 72 6f 63 65 73  insert(&inProces
17a0: 73 2c 20 73 72 63 69 64 29 3b 0a 0a 20 20 20 20  s, srcid);..    
17b0: 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28  if( content_get(
17c0: 73 72 63 69 64 2c 20 26 73 72 63 29 20 29 7b 0a  srcid, &src) ){.
17d0: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65        db_prepare
17e0: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e  (&q, "SELECT con
17f0: 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57  tent FROM blob W
1800: 48 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20  HERE rid=%d AND 
1810: 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a  size>=0", rid);.
1820: 20 20 20 20 20 20 69 66 28 20 64 62 5f 73 74 65        if( db_ste
1830: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
1840: 57 20 29 7b 0a 20 20 20 20 20 20 20 20 42 6c 6f  W ){.        Blo
1850: 62 20 64 65 6c 74 61 3b 0a 20 20 20 20 20 20 20  b delta;.       
1860: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c   db_ephemeral_bl
1870: 6f 62 28 26 71 2c 20 30 2c 20 26 64 65 6c 74 61  ob(&q, 0, &delta
1880: 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  );.        blob_
1890: 75 6e 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74  uncompress(&delt
18a0: 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  a, &delta);.    
18b0: 20 20 20 20 62 6c 6f 62 5f 69 6e 69 74 28 70 42      blob_init(pB
18c0: 6c 6f 62 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  lob,0,0);.      
18d0: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70    blob_delta_app
18e0: 6c 79 28 26 73 72 63 2c 20 26 64 65 6c 74 61 2c  ly(&src, &delta,
18f0: 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 20   pBlob);.       
1900: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c   blob_reset(&del
1910: 74 61 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ta);.        rc 
1920: 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
1930: 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26     db_finalize(&
1940: 71 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 61  q);..      /* Sa
1950: 76 65 20 74 68 65 20 73 72 63 69 64 20 61 72 74  ve the srcid art
1960: 69 66 61 63 74 20 69 6e 20 74 68 65 20 63 61 63  ifact in the cac
1970: 68 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  he */.      if( 
1980: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 3c 4d  contentCache.n<M
1990: 58 5f 43 41 43 48 45 5f 43 4e 54 20 29 7b 0a 20  X_CACHE_CNT ){. 
19a0: 20 20 20 20 20 20 20 69 20 3d 20 63 6f 6e 74 65         i = conte
19b0: 6e 74 43 61 63 68 65 2e 6e 2b 2b 3b 0a 20 20 20  ntCache.n++;.   
19c0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 28 63     }else if( ((c
19d0: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 73 6b 69 70  ontentCache.skip
19e0: 43 6e 74 2b 2b 29 25 45 58 50 45 4c 4c 5f 49 4e  Cnt++)%EXPELL_IN
19f0: 54 45 52 56 41 4c 29 21 3d 30 20 29 7b 0a 20 20  TERVAL)!=0 ){.  
1a00: 20 20 20 20 20 20 69 20 3d 20 2d 31 3b 0a 20 20        i = -1;.  
1a10: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a20: 20 20 20 69 6e 74 20 6a 2c 20 62 65 73 74 3b 0a     int j, best;.
1a30: 20 20 20 20 20 20 20 20 62 65 73 74 20 3d 20 63          best = c
1a40: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 65 78 74  ontentCache.next
1a50: 41 67 65 2b 31 3b 0a 20 20 20 20 20 20 20 20 69  Age+1;.        i
1a60: 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 66   = -1;.        f
1a70: 6f 72 28 6a 3d 30 3b 20 6a 3c 63 6f 6e 74 65 6e  or(j=0; j<conten
1a80: 74 43 61 63 68 65 2e 6e 3b 20 6a 2b 2b 29 7b 0a  tCache.n; j++){.
1a90: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6f            if( co
1aa0: 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 6a 5d 2e  ntentCache.a[j].
1ab0: 61 67 65 3c 62 65 73 74 20 29 7b 0a 20 20 20 20  age<best ){.    
1ac0: 20 20 20 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20          i = j;. 
1ad0: 20 20 20 20 20 20 20 20 20 20 20 62 65 73 74 20             best 
1ae0: 3d 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61  = contentCache.a
1af0: 5b 6a 5d 2e 61 67 65 3b 0a 20 20 20 20 20 20 20  [j].age;.       
1b00: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1b10: 20 20 20 20 20 20 20 43 4f 4e 54 45 4e 54 5f 54         CONTENT_T
1b20: 52 41 43 45 28 28 22 25 2a 73 65 78 70 65 6c 6c  RACE(("%*sexpell
1b30: 20 25 64 20 66 72 6f 6d 20 63 61 63 68 65 5c 6e   %d from cache\n
1b40: 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
1b50: 20 20 20 20 20 20 20 20 20 20 62 61 67 5f 63 6f            bag_co
1b60: 75 6e 74 28 26 69 6e 50 72 6f 63 65 73 73 29 2c  unt(&inProcess),
1b70: 20 22 22 2c 20 63 6f 6e 74 65 6e 74 43 61 63 68   "", contentCach
1b80: 65 2e 61 5b 69 5d 2e 72 69 64 29 29 0a 20 20 20  e.a[i].rid)).   
1b90: 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28       blob_reset(
1ba0: 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b  &contentCache.a[
1bb0: 69 5d 2e 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20  i].content);.   
1bc0: 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
1bd0: 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  >=0 ){.        c
1be0: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d  ontentCache.a[i]
1bf0: 2e 63 6f 6e 74 65 6e 74 20 3d 20 73 72 63 3b 0a  .content = src;.
1c00: 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 43          contentC
1c10: 61 63 68 65 2e 61 5b 69 5d 2e 61 67 65 20 3d 20  ache.a[i].age = 
1c20: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e 65 78  contentCache.nex
1c30: 74 41 67 65 2b 2b 3b 0a 20 20 20 20 20 20 20 20  tAge++;.        
1c40: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69  contentCache.a[i
1c50: 5d 2e 72 69 64 20 3d 20 73 72 63 69 64 3b 0a 20  ].rid = srcid;. 
1c60: 20 20 20 20 20 20 20 43 4f 4e 54 45 4e 54 5f 54         CONTENT_T
1c70: 52 41 43 45 28 28 22 25 2a 73 61 64 64 20 25 64  RACE(("%*sadd %d
1c80: 20 74 6f 20 63 61 63 68 65 5c 6e 22 2c 0a 20 20   to cache\n",.  
1c90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ca0: 20 20 20 20 20 62 61 67 5f 63 6f 75 6e 74 28 26       bag_count(&
1cb0: 69 6e 50 72 6f 63 65 73 73 29 2c 20 22 22 2c 20  inProcess), "", 
1cc0: 73 72 63 69 64 29 29 0a 20 20 20 20 20 20 7d 65  srcid)).      }e
1cd0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f  lse{.        blo
1ce0: 62 5f 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20  b_reset(&src);. 
1cf0: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1d00: 20 62 61 67 5f 72 65 6d 6f 76 65 28 26 69 6e 50   bag_remove(&inP
1d10: 72 6f 63 65 73 73 2c 20 73 72 63 69 64 29 3b 0a  rocess, srcid);.
1d20: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1d30: 4e 6f 20 64 65 6c 74 61 20 72 65 71 75 69 72 65  No delta require
1d40: 64 2e 20 20 52 65 61 64 20 63 6f 6e 74 65 6e 74  d.  Read content
1d50: 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74   directly from t
1d60: 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
1d70: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71     db_prepare(&q
1d80: 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 6e  , "SELECT conten
1d90: 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  t FROM blob WHER
1da0: 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69 7a  E rid=%d AND siz
1db0: 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a 20 20 20  e>=0", rid);.   
1dc0: 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 71 29   if( db_step(&q)
1dd0: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
1de0: 20 20 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72        db_ephemer
1df0: 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20 70  al_blob(&q, 0, p
1e00: 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 62 6c 6f  Blob);.      blo
1e10: 62 5f 75 6e 63 6f 6d 70 72 65 73 73 28 70 42 6c  b_uncompress(pBl
1e20: 6f 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20  ob, pBlob);.    
1e30: 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a    rc = 1;.    }.
1e40: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28      db_finalize(
1e50: 26 71 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  &q);.  }.  if( r
1e60: 63 3d 3d 30 20 29 7b 0a 20 20 20 20 62 61 67 5f  c==0 ){.    bag_
1e70: 69 6e 73 65 72 74 28 26 63 6f 6e 74 65 6e 74 43  insert(&contentC
1e80: 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69  ache.missing, ri
1e90: 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  d);.  }else{.   
1ea0: 20 62 61 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e   bag_insert(&con
1eb0: 74 65 6e 74 43 61 63 68 65 2e 61 76 61 69 6c 61  tentCache.availa
1ec0: 62 6c 65 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 20  ble, rid);.  }. 
1ed0: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1ee0: 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 63 6f 6e  *.** Get the con
1ef0: 74 65 6e 74 73 20 6f 66 20 61 20 66 69 6c 65 20  tents of a file 
1f00: 77 69 74 68 69 6e 20 61 20 67 69 76 65 6e 20 62  within a given b
1f10: 61 73 65 6c 69 6e 65 2e 0a 2a 2f 0a 69 6e 74 20  aseline..*/.int 
1f20: 63 6f 6e 74 65 6e 74 5f 67 65 74 5f 68 69 73 74  content_get_hist
1f30: 6f 72 69 63 61 6c 5f 66 69 6c 65 28 0a 20 20 63  orical_file(.  c
1f40: 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 76 69 73  onst char *revis
1f50: 69 6f 6e 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ion,    /* Name 
1f60: 6f 66 20 74 68 65 20 62 61 73 65 6c 69 6e 65 20  of the baseline 
1f70: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 66  containing the f
1f80: 69 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ile */.  const c
1f90: 68 61 72 20 2a 66 69 6c 65 2c 20 20 20 20 20 20  har *file,      
1fa0: 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1fb0: 20 66 69 6c 65 20 2a 2f 0a 20 20 42 6c 6f 62 20   file */.  Blob 
1fc0: 2a 63 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 20  *content        
1fd0: 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66 69 6c      /* Write fil
1fe0: 65 20 63 6f 6e 74 65 6e 74 20 68 65 72 65 20 2a  e content here *
1ff0: 2f 0a 29 7b 0a 20 20 42 6c 6f 62 20 6d 66 69 6c  /.){.  Blob mfil
2000: 65 3b 0a 20 20 4d 61 6e 69 66 65 73 74 20 6d 3b  e;.  Manifest m;
2010: 0a 20 20 69 6e 74 20 69 2c 20 72 69 64 3d 30 3b  .  int i, rid=0;
2020: 0a 20 20 0a 20 20 72 69 64 20 3d 20 6e 61 6d 65  .  .  rid = name
2030: 5f 74 6f 5f 72 69 64 28 72 65 76 69 73 69 6f 6e  _to_rid(revision
2040: 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74  );.  content_get
2050: 28 72 69 64 2c 20 26 6d 66 69 6c 65 29 3b 0a 20  (rid, &mfile);. 
2060: 20 0a 20 20 69 66 28 20 6d 61 6e 69 66 65 73 74   .  if( manifest
2070: 5f 70 61 72 73 65 28 26 6d 2c 20 26 6d 66 69 6c  _parse(&m, &mfil
2080: 65 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  e) ){.    for(i=
2090: 30 3b 20 69 3c 6d 2e 6e 46 69 6c 65 3b 20 69 2b  0; i<m.nFile; i+
20a0: 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74  +){.      if( st
20b0: 72 63 6d 70 28 6d 2e 61 46 69 6c 65 5b 69 5d 2e  rcmp(m.aFile[i].
20c0: 7a 4e 61 6d 65 2c 20 66 69 6c 65 29 3d 3d 30 20  zName, file)==0 
20d0: 29 7b 0a 20 20 20 20 20 20 20 20 72 69 64 20 3d  ){.        rid =
20e0: 20 75 75 69 64 5f 74 6f 5f 72 69 64 28 6d 2e 61   uuid_to_rid(m.a
20f0: 46 69 6c 65 5b 69 5d 2e 7a 55 75 69 64 2c 20 30  File[i].zUuid, 0
2100: 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
2110: 6e 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69  n content_get(ri
2120: 64 2c 20 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20  d, content);.   
2130: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66     }.    }.    f
2140: 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 66 69 6c  ossil_panic("fil
2150: 65 3a 20 25 73 20 64 6f 65 73 20 6e 6f 74 20 65  e: %s does not e
2160: 78 69 73 74 20 69 6e 20 72 65 76 69 73 69 6f 6e  xist in revision
2170: 3a 20 25 73 22 2c 20 66 69 6c 65 2c 20 72 65 76  : %s", file, rev
2180: 69 73 69 6f 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b  ision);.  }else{
2190: 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69  .    fossil_pani
21a0: 63 28 22 63 6f 75 6c 64 20 6e 6f 74 20 70 61 72  c("could not par
21b0: 73 65 20 6d 61 6e 69 66 65 73 74 20 66 6f 72 20  se manifest for 
21c0: 72 65 76 69 73 69 6f 6e 3a 20 25 73 22 2c 20 72  revision: %s", r
21d0: 65 76 69 73 69 6f 6e 29 3b 0a 20 20 7d 0a 20 20  evision);.  }.  
21e0: 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
21f0: 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20  /*.** COMMAND:  
2200: 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 67 65 74  test-content-get
2210: 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61  .**.** Extract a
2220: 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20 64   blob from the d
2230: 61 74 61 62 61 73 65 20 61 6e 64 20 77 72 69 74  atabase and writ
2240: 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c 65  e it into a file
2250: 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63  ..*/.void test_c
2260: 6f 6e 74 65 6e 74 5f 67 65 74 5f 63 6d 64 28 76  ontent_get_cmd(v
2270: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b  oid){.  int rid;
2280: 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b  .  Blob content;
2290: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
22a0: 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e 61 72  File;.  if( g.ar
22b0: 67 63 21 3d 34 20 26 26 20 67 2e 61 72 67 63 21  gc!=4 && g.argc!
22c0: 3d 33 20 29 20 75 73 61 67 65 28 22 52 45 43 4f  =3 ) usage("RECO
22d0: 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45 3f 22  RDID ?FILENAME?"
22e0: 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67 2e 61  );.  zFile = g.a
22f0: 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67 76 5b  rgc==4 ? g.argv[
2300: 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62 5f 6d  3] : "-";.  db_m
2310: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
2320: 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 6e 61  ee();.  rid = na
2330: 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76  me_to_rid(g.argv
2340: 5b 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f  [2]);.  content_
2350: 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e  get(rid, &conten
2360: 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72 69 74 65  t);.  blob_write
2370: 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e 74 65 6e  _to_file(&conten
2380: 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a 0a 2f 2a  t, zFile);.}../*
2390: 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65  .** COMMAND:  te
23a0: 73 74 2d 63 6f 6e 74 65 6e 74 2d 72 61 77 67 65  st-content-rawge
23b0: 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20  t.**.** Extract 
23c0: 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20  a blob from the 
23d0: 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 72 69  database and wri
23e0: 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c  te it into a fil
23f0: 65 2e 20 20 54 68 69 73 0a 2a 2a 20 76 65 72 73  e.  This.** vers
2400: 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 70  ion does not exp
2410: 61 6e 64 20 74 68 65 20 64 65 6c 74 61 2e 0a 2a  and the delta..*
2420: 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74  /.void test_cont
2430: 65 6e 74 5f 72 61 77 67 65 74 5f 63 6d 64 28 76  ent_rawget_cmd(v
2440: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b  oid){.  int rid;
2450: 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b  .  Blob content;
2460: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2470: 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e 61 72  File;.  if( g.ar
2480: 67 63 21 3d 34 20 26 26 20 67 2e 61 72 67 63 21  gc!=4 && g.argc!
2490: 3d 33 20 29 20 75 73 61 67 65 28 22 52 45 43 4f  =3 ) usage("RECO
24a0: 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45 3f 22  RDID ?FILENAME?"
24b0: 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67 2e 61  );.  zFile = g.a
24c0: 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67 76 5b  rgc==4 ? g.argv[
24d0: 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62 5f 6d  3] : "-";.  db_m
24e0: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
24f0: 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 6e 61  ee();.  rid = na
2500: 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76  me_to_rid(g.argv
2510: 5b 32 5d 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  [2]);.  blob_zer
2520: 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 64  o(&content);.  d
2530: 62 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 2c  b_blob(&content,
2540: 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74   "SELECT content
2550: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45   FROM blob WHERE
2560: 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a   rid=%d", rid);.
2570: 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 73    blob_uncompres
2580: 73 28 26 63 6f 6e 74 65 6e 74 2c 20 26 63 6f 6e  s(&content, &con
2590: 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72  tent);.  blob_wr
25a0: 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e  ite_to_file(&con
25b0: 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a  tent, zFile);.}.
25c0: 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 72 65  ./*.** When a re
25d0: 63 6f 72 64 20 69 73 20 63 6f 6e 76 65 72 74 65  cord is converte
25e0: 64 20 66 72 6f 6d 20 61 20 70 68 61 6e 74 6f 6d  d from a phantom
25f0: 20 74 6f 20 61 20 72 65 61 6c 20 72 65 63 6f 72   to a real recor
2600: 64 2c 0a 2a 2a 20 69 66 20 74 68 61 74 20 72 65  d,.** if that re
2610: 63 6f 72 64 20 68 61 73 20 6f 74 68 65 72 20 72  cord has other r
2620: 65 63 6f 72 64 73 20 74 68 61 74 20 61 72 65 20  ecords that are 
2630: 64 65 72 69 76 65 64 20 62 79 20 64 65 6c 74 61  derived by delta
2640: 2c 0a 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20 6d  ,.** then call m
2650: 61 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e  anifest_crosslin
2660: 6b 28 29 20 6f 6e 20 74 68 6f 73 65 20 6f 74 68  k() on those oth
2670: 65 72 20 72 65 63 6f 72 64 73 2e 0a 2a 2f 0a 76  er records..*/.v
2680: 6f 69 64 20 61 66 74 65 72 5f 64 65 70 68 61 6e  oid after_dephan
2690: 74 6f 6d 69 7a 65 28 69 6e 74 20 72 69 64 2c 20  tomize(int rid, 
26a0: 69 6e 74 20 6c 69 6e 6b 46 6c 61 67 29 7b 0a 20  int linkFlag){. 
26b0: 20 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70 72   Stmt q;.  db_pr
26c0: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
26d0: 54 20 72 69 64 20 46 52 4f 4d 20 64 65 6c 74 61  T rid FROM delta
26e0: 20 57 48 45 52 45 20 73 72 63 69 64 3d 25 64 22   WHERE srcid=%d"
26f0: 2c 20 72 69 64 29 3b 0a 20 20 77 68 69 6c 65 28  , rid);.  while(
2700: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51   db_step(&q)==SQ
2710: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
2720: 69 6e 74 20 74 69 64 20 3d 20 64 62 5f 63 6f 6c  int tid = db_col
2730: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a  umn_int(&q, 0);.
2740: 20 20 20 20 61 66 74 65 72 5f 64 65 70 68 61 6e      after_dephan
2750: 74 6f 6d 69 7a 65 28 74 69 64 2c 20 31 29 3b 0a  tomize(tid, 1);.
2760: 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a    }.  db_finaliz
2770: 65 28 26 71 29 3b 0a 20 20 69 66 28 20 6c 69 6e  e(&q);.  if( lin
2780: 6b 46 6c 61 67 20 29 7b 0a 20 20 20 20 42 6c 6f  kFlag ){.    Blo
2790: 62 20 63 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 63  b content;.    c
27a0: 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20  ontent_get(rid, 
27b0: 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 6d  &content);.    m
27c0: 61 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e  anifest_crosslin
27d0: 6b 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29  k(rid, &content)
27e0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
27f0: 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 7d 0a  (&content);.  }.
2800: 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63  }../*.** Write c
2810: 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  ontent into the 
2820: 64 61 74 61 62 61 73 65 2e 20 20 52 65 74 75 72  database.  Retur
2830: 6e 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e  n the record ID.
2840: 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 6f 6e 74    If the.** cont
2850: 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69  ent is already i
2860: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
2870: 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  just return the 
2880: 72 65 63 6f 72 64 20 49 44 2e 0a 2a 2a 0a 2a 2a  record ID..**.**
2890: 20 49 66 20 73 72 63 49 64 20 69 73 20 73 70 65   If srcId is spe
28a0: 63 69 66 69 65 64 2c 20 74 68 65 6e 20 70 42 6c  cified, then pBl
28b0: 6f 62 20 69 73 20 64 65 6c 74 61 20 63 6f 6e 74  ob is delta cont
28c0: 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ent from.** the 
28d0: 73 72 63 49 64 20 72 65 63 6f 72 64 2e 20 20 73  srcId record.  s
28e0: 72 63 49 64 20 6d 69 67 68 74 20 62 65 20 61 20  rcId might be a 
28f0: 70 68 61 6e 74 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 49  phantom..**.** I
2900: 66 20 73 72 63 49 64 20 69 73 20 73 70 65 63 69  f srcId is speci
2910: 66 69 65 64 20 74 68 65 6e 20 74 68 65 20 55 55  fied then the UU
2920: 49 44 20 69 73 20 73 65 74 20 74 6f 20 7a 55 75  ID is set to zUu
2930: 69 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 7a  id.  Otherwise z
2940: 55 75 69 64 20 69 73 0a 2a 2a 20 69 67 6e 6f 72  Uuid is.** ignor
2950: 65 64 2e 20 20 49 6e 20 74 68 65 20 66 75 74 75  ed.  In the futu
2960: 72 65 20 74 68 69 73 20 6d 69 67 68 74 20 63 68  re this might ch
2970: 61 6e 67 65 20 73 75 63 68 20 74 68 61 74 20 74  ange such that t
2980: 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 68 61  he content.** ha
2990: 73 68 20 69 73 20 63 68 65 63 6b 65 64 20 61 67  sh is checked ag
29a0: 61 69 6e 73 74 20 7a 55 75 69 64 20 74 6f 20 6d  ainst zUuid to m
29b0: 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 63  ake sure it is c
29c0: 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  orrect..**.** If
29d0: 20 74 68 65 20 72 65 63 6f 72 64 20 61 6c 72 65   the record alre
29e0: 61 64 79 20 65 78 69 73 74 73 20 62 75 74 20 69  ady exists but i
29f0: 73 20 61 20 70 68 61 6e 74 6f 6d 2c 20 74 68 65  s a phantom, the
2a00: 20 70 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 0a 2a   pBlob content.*
2a10: 2a 20 69 73 20 69 6e 73 65 72 74 65 64 20 61 6e  * is inserted an
2a20: 64 20 74 68 65 20 70 68 61 74 6f 6d 20 62 65 63  d the phatom bec
2a30: 6f 6d 65 73 20 61 20 72 65 61 6c 20 72 65 63 6f  omes a real reco
2a40: 72 64 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65  rd..*/.int conte
2a50: 6e 74 5f 70 75 74 28 42 6c 6f 62 20 2a 70 42 6c  nt_put(Blob *pBl
2a60: 6f 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ob, const char *
2a70: 7a 55 75 69 64 2c 20 69 6e 74 20 73 72 63 49 64  zUuid, int srcId
2a80: 29 7b 0a 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20  ){.  int size;. 
2a90: 20 69 6e 74 20 72 69 64 3b 0a 20 20 53 74 6d 74   int rid;.  Stmt
2aa0: 20 73 31 3b 0a 20 20 42 6c 6f 62 20 63 6d 70 72   s1;.  Blob cmpr
2ab0: 3b 0a 20 20 42 6c 6f 62 20 68 61 73 68 3b 0a 20  ;.  Blob hash;. 
2ac0: 20 69 6e 74 20 6d 61 72 6b 41 73 55 6e 63 6c 75   int markAsUnclu
2ad0: 73 74 65 72 65 64 20 3d 20 30 3b 0a 20 20 69 6e  stered = 0;.  in
2ae0: 74 20 69 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65  t isDephantomize
2af0: 20 3d 20 30 3b 0a 20 20 0a 20 20 61 73 73 65 72   = 0;.  .  asser
2b00: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  t( g.repositoryO
2b10: 70 65 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28  pen );.  assert(
2b20: 20 70 42 6c 6f 62 21 3d 30 20 29 3b 0a 20 20 69   pBlob!=0 );.  i
2b30: 66 28 20 73 72 63 49 64 3d 3d 30 20 29 7b 0a 20  f( srcId==0 ){. 
2b40: 20 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28     sha1sum_blob(
2b50: 70 42 6c 6f 62 2c 20 26 68 61 73 68 29 3b 0a 20  pBlob, &hash);. 
2b60: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62   }else{.    blob
2b70: 5f 69 6e 69 74 28 26 68 61 73 68 2c 20 7a 55 75  _init(&hash, zUu
2b80: 69 64 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 73  id, -1);.  }.  s
2b90: 69 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28  ize = blob_size(
2ba0: 70 42 6c 6f 62 29 3b 0a 20 20 64 62 5f 62 65 67  pBlob);.  db_beg
2bb0: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
2bc0: 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  ;..  /* Check to
2bd0: 20 73 65 65 20 69 66 20 74 68 65 20 65 6e 74 72   see if the entr
2be0: 79 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  y already exists
2bf0: 20 61 6e 64 20 69 66 20 69 74 20 64 6f 65 73 20   and if it does 
2c00: 77 68 65 74 68 65 72 0a 20 20 2a 2a 20 6f 72 20  whether.  ** or 
2c10: 6e 6f 74 20 74 68 65 20 65 6e 74 72 79 20 69 73  not the entry is
2c20: 20 61 20 70 68 61 6e 74 6f 6d 0a 20 20 2a 2f 0a   a phantom.  */.
2c30: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31    db_prepare(&s1
2c40: 2c 20 22 53 45 4c 45 43 54 20 72 69 64 2c 20 73  , "SELECT rid, s
2c50: 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  ize FROM blob WH
2c60: 45 52 45 20 75 75 69 64 3d 25 42 22 2c 20 26 68  ERE uuid=%B", &h
2c70: 61 73 68 29 3b 0a 20 20 69 66 28 20 64 62 5f 73  ash);.  if( db_s
2c80: 74 65 70 28 26 73 31 29 3d 3d 53 51 4c 49 54 45  tep(&s1)==SQLITE
2c90: 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 69 64 20  _ROW ){.    rid 
2ca0: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  = db_column_int(
2cb0: 26 73 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  &s1, 0);.    if(
2cc0: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26   db_column_int(&
2cd0: 73 31 2c 20 31 29 3e 3d 30 20 7c 7c 20 70 42 6c  s1, 1)>=0 || pBl
2ce0: 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ob==0 ){.      /
2cf0: 2a 20 45 69 74 68 65 72 20 74 68 65 20 65 6e 74  * Either the ent
2d00: 72 79 20 69 73 20 6e 6f 74 20 61 20 70 68 61 6e  ry is not a phan
2d10: 74 6f 6d 20 6f 72 20 69 74 20 69 73 20 61 20 70  tom or it is a p
2d20: 68 61 6e 74 6f 6d 20 62 75 74 20 77 65 0a 20 20  hantom but we.  
2d30: 20 20 20 20 2a 2a 20 68 61 76 65 20 6e 6f 20 64      ** have no d
2d40: 61 74 61 20 77 69 74 68 20 77 68 69 63 68 20 74  ata with which t
2d50: 6f 20 64 65 70 68 61 6e 74 6f 6d 69 7a 65 20 69  o dephantomize i
2d60: 74 2e 20 20 49 6e 20 65 69 74 68 65 72 20 63 61  t.  In either ca
2d70: 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  se,.      ** the
2d80: 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 66 6f  re is nothing fo
2d90: 72 20 75 73 20 74 6f 20 64 6f 20 6f 74 68 65 72  r us to do other
2da0: 20 74 68 61 6e 20 72 65 74 75 72 6e 20 74 68 65   than return the
2db0: 20 52 49 44 2e 20 2a 2f 0a 20 20 20 20 20 20 64   RID. */.      d
2dc0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b  b_finalize(&s1);
2dd0: 0a 20 20 20 20 20 20 64 62 5f 65 6e 64 5f 74 72  .      db_end_tr
2de0: 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20  ansaction(0);.  
2df0: 20 20 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a      return rid;.
2e00: 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
2e10: 20 20 20 72 69 64 20 3d 20 30 3b 20 20 2f 2a 20     rid = 0;  /* 
2e20: 4e 6f 20 65 6e 74 72 79 20 77 69 74 68 20 74 68  No entry with th
2e30: 65 20 73 61 6d 65 20 55 55 49 44 20 63 75 72 72  e same UUID curr
2e40: 65 6e 74 6c 79 20 65 78 69 73 74 73 20 2a 2f 0a  ently exists */.
2e50: 20 20 20 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73      markAsUnclus
2e60: 74 65 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  tered = 1;.  }. 
2e70: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31   db_finalize(&s1
2e80: 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75  );..  /* Constru
2e90: 63 74 20 61 20 72 65 63 65 69 76 65 64 2d 66 72  ct a received-fr
2ea0: 6f 6d 20 49 44 20 69 66 20 77 65 20 64 6f 20 6e  om ID if we do n
2eb0: 6f 74 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  ot already have 
2ec0: 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 67 2e 72  one */.  if( g.r
2ed0: 63 76 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 64  cvid==0 ){.    d
2ee0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
2ef0: 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54       "INSERT INT
2f00: 4f 20 72 63 76 66 72 6f 6d 28 75 69 64 2c 20 6d  O rcvfrom(uid, m
2f10: 74 69 6d 65 2c 20 6e 6f 6e 63 65 2c 20 69 70 61  time, nonce, ipa
2f20: 64 64 72 29 22 0a 20 20 20 20 20 20 20 22 56 41  ddr)".       "VA
2f30: 4c 55 45 53 28 25 64 2c 20 6a 75 6c 69 61 6e 64  LUES(%d, juliand
2f40: 61 79 28 27 6e 6f 77 27 29 2c 20 25 51 2c 20 25  ay('now'), %Q, %
2f50: 51 29 22 2c 0a 20 20 20 20 20 20 20 67 2e 75 73  Q)",.       g.us
2f60: 65 72 55 69 64 2c 20 67 2e 7a 4e 6f 6e 63 65 2c  erUid, g.zNonce,
2f70: 20 67 2e 7a 49 70 41 64 64 72 0a 20 20 20 20 29   g.zIpAddr.    )
2f80: 3b 0a 20 20 20 20 67 2e 72 63 76 69 64 20 3d 20  ;.    g.rcvid = 
2f90: 64 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  db_last_insert_r
2fa0: 6f 77 69 64 28 29 3b 0a 20 20 7d 0a 0a 20 20 62  owid();.  }..  b
2fb0: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c  lob_compress(pBl
2fc0: 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20 20 69 66  ob, &cmpr);.  if
2fd0: 28 20 72 69 64 3e 30 20 29 7b 0a 20 20 20 20 2f  ( rid>0 ){.    /
2fe0: 2a 20 57 65 20 61 72 65 20 6a 75 73 74 20 61 64  * We are just ad
2ff0: 64 69 6e 67 20 64 61 74 61 20 74 6f 20 61 20 70  ding data to a p
3000: 68 61 6e 74 6f 6d 20 2a 2f 0a 20 20 20 20 64 62  hantom */.    db
3010: 5f 70 72 65 70 61 72 65 28 26 73 31 2c 0a 20 20  _prepare(&s1,.  
3020: 20 20 20 20 22 55 50 44 41 54 45 20 62 6c 6f 62      "UPDATE blob
3030: 20 53 45 54 20 72 63 76 69 64 3d 25 64 2c 20 73   SET rcvid=%d, s
3040: 69 7a 65 3d 25 64 2c 20 63 6f 6e 74 65 6e 74 3d  ize=%d, content=
3050: 3a 64 61 74 61 20 57 48 45 52 45 20 72 69 64 3d  :data WHERE rid=
3060: 25 64 22 2c 0a 20 20 20 20 20 20 20 67 2e 72 63  %d",.       g.rc
3070: 76 69 64 2c 20 73 69 7a 65 2c 20 72 69 64 0a 20  vid, size, rid. 
3080: 20 20 20 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e     );.    db_bin
3090: 64 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61  d_blob(&s1, ":da
30a0: 74 61 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20  ta", &cmpr);.   
30b0: 20 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20   db_exec(&s1);. 
30c0: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
30d0: 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 68  ("DELETE FROM ph
30e0: 61 6e 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d  antom WHERE rid=
30f0: 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69  %d", rid);.    i
3100: 66 28 20 73 72 63 49 64 3d 3d 30 20 7c 7c 20 63  f( srcId==0 || c
3110: 6f 6e 74 65 6e 74 5f 69 73 5f 61 76 61 69 6c 61  ontent_is_availa
3120: 62 6c 65 28 73 72 63 49 64 29 20 29 7b 0a 20 20  ble(srcId) ){.  
3130: 20 20 20 20 69 73 44 65 70 68 61 6e 74 6f 6d 69      isDephantomi
3140: 7a 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 6f  ze = 1;.      co
3150: 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61 76 61 69 6c  ntent_mark_avail
3160: 61 62 6c 65 28 72 69 64 29 3b 0a 20 20 20 20 7d  able(rid);.    }
3170: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
3180: 20 57 65 20 61 72 65 20 63 72 65 61 74 69 6e 67   We are creating
3190: 20 61 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a   a new entry */.
31a0: 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26      db_prepare(&
31b0: 73 31 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 52  s1,.      "INSER
31c0: 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72 63 76 69  T INTO blob(rcvi
31d0: 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63 6f 6e 74  d,size,uuid,cont
31e0: 65 6e 74 29 22 0a 20 20 20 20 20 20 22 56 41 4c  ent)".      "VAL
31f0: 55 45 53 28 25 64 2c 25 64 2c 27 25 62 27 2c 3a  UES(%d,%d,'%b',:
3200: 64 61 74 61 29 22 2c 0a 20 20 20 20 20 20 20 67  data)",.       g
3210: 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c 20 26 68  .rcvid, size, &h
3220: 61 73 68 0a 20 20 20 20 29 3b 0a 20 20 20 20 64  ash.    );.    d
3230: 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c  b_bind_blob(&s1,
3240: 20 22 3a 64 61 74 61 22 2c 20 26 63 6d 70 72 29   ":data", &cmpr)
3250: 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 73  ;.    db_exec(&s
3260: 31 29 3b 0a 20 20 20 20 72 69 64 20 3d 20 64 62  1);.    rid = db
3270: 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
3280: 69 64 28 29 3b 0a 20 20 20 20 69 66 28 20 21 70  id();.    if( !p
3290: 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 64 62  Blob ){.      db
32a0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53  _multi_exec("INS
32b0: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e  ERT OR IGNORE IN
32c0: 54 4f 20 70 68 61 6e 74 6f 6d 20 56 41 4c 55 45  TO phantom VALUE
32d0: 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a 20 20  S(%d)", rid);.  
32e0: 20 20 7d 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72    }.  }.  blob_r
32f0: 65 73 65 74 28 26 63 6d 70 72 29 3b 0a 0a 20 20  eset(&cmpr);..  
3300: 2f 2a 20 49 66 20 74 68 65 20 73 72 63 49 64 20  /* If the srcId 
3310: 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68  is specified, th
3320: 65 6e 20 74 68 65 20 64 61 74 61 20 77 65 20 6a  en the data we j
3330: 75 73 74 20 61 64 64 65 64 20 69 73 0a 20 20 2a  ust added is.  *
3340: 2a 20 72 65 61 6c 6c 79 20 61 20 64 65 6c 74 61  * really a delta
3350: 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 66  .  Record this f
3360: 61 63 74 20 69 6e 20 74 68 65 20 64 65 6c 74 61  act in the delta
3370: 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
3380: 66 28 20 73 72 63 49 64 20 29 7b 0a 20 20 20 20  f( srcId ){.    
3390: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 52  db_multi_exec("R
33a0: 45 50 4c 41 43 45 20 49 4e 54 4f 20 64 65 6c 74  EPLACE INTO delt
33b0: 61 28 72 69 64 2c 73 72 63 69 64 29 20 56 41 4c  a(rid,srcid) VAL
33c0: 55 45 53 28 25 64 2c 25 64 29 22 2c 20 72 69 64  UES(%d,%d)", rid
33d0: 2c 20 73 72 63 49 64 29 3b 0a 20 20 7d 0a 20 20  , srcId);.  }.  
33e0: 69 66 28 20 21 69 73 44 65 70 68 61 6e 74 6f 6d  if( !isDephantom
33f0: 69 7a 65 20 26 26 20 62 61 67 5f 66 69 6e 64 28  ize && bag_find(
3400: 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69  &contentCache.mi
3410: 73 73 69 6e 67 2c 20 72 69 64 29 20 26 26 20 0a  ssing, rid) && .
3420: 20 20 20 20 20 20 28 73 72 63 49 64 3d 3d 30 20        (srcId==0 
3430: 7c 7c 20 63 6f 6e 74 65 6e 74 5f 69 73 5f 61 76  || content_is_av
3440: 61 69 6c 61 62 6c 65 28 73 72 63 49 64 29 29 20  ailable(srcId)) 
3450: 29 7b 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f 6d  ){.    content_m
3460: 61 72 6b 5f 61 76 61 69 6c 61 62 6c 65 28 72 69  ark_available(ri
3470: 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73  d);.  }.  if( is
3480: 44 65 70 68 61 6e 74 6f 6d 69 7a 65 20 29 7b 0a  Dephantomize ){.
3490: 20 20 20 20 61 66 74 65 72 5f 64 65 70 68 61 6e      after_dephan
34a0: 74 6f 6d 69 7a 65 28 72 69 64 2c 20 30 29 3b 0a  tomize(rid, 0);.
34b0: 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 41 64 64 20    }.  .  /* Add 
34c0: 74 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74  the element to t
34d0: 68 65 20 75 6e 63 6c 75 73 74 65 72 65 64 20 74  he unclustered t
34e0: 61 62 6c 65 20 69 66 20 68 61 73 20 6e 65 76 65  able if has neve
34f0: 72 20 62 65 65 6e 0a 20 20 2a 2a 20 70 72 65 76  r been.  ** prev
3500: 69 6f 75 73 6c 79 20 73 65 65 6e 2e 0a 20 20 2a  iously seen..  *
3510: 2f 0a 20 20 69 66 28 20 6d 61 72 6b 41 73 55 6e  /.  if( markAsUn
3520: 63 6c 75 73 74 65 72 65 64 20 29 7b 0a 20 20 20  clustered ){.   
3530: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
3540: 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45  INSERT OR IGNORE
3550: 20 49 4e 54 4f 20 75 6e 63 6c 75 73 74 65 72 65   INTO unclustere
3560: 64 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72  d VALUES(%d)", r
3570: 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  id);.  }..  /* F
3580: 69 6e 69 73 68 20 74 68 65 20 74 72 61 6e 73 61  inish the transa
3590: 63 74 69 6f 6e 20 61 6e 64 20 63 6c 65 61 6e 75  ction and cleanu
35a0: 70 20 2a 2f 0a 20 20 64 62 5f 66 69 6e 61 6c 69  p */.  db_finali
35b0: 7a 65 28 26 73 31 29 3b 0a 20 20 64 62 5f 65 6e  ze(&s1);.  db_en
35c0: 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29  d_transaction(0)
35d0: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  ;.  blob_reset(&
35e0: 68 61 73 68 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  hash);..  /* Mak
35f0: 65 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 74  e arrangements t
3600: 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68  o verify that th
3610: 65 20 64 61 74 61 20 63 61 6e 20 62 65 20 72 65  e data can be re
3620: 63 6f 76 65 72 65 64 0a 20 20 2a 2a 20 62 65 66  covered.  ** bef
3630: 6f 72 65 20 77 65 20 63 6f 6d 6d 69 74 20 2a 2f  ore we commit */
3640: 0a 20 20 76 65 72 69 66 79 5f 62 65 66 6f 72 65  .  verify_before
3650: 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b 0a 20 20  _commit(rid);.  
3660: 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a 0a 2f  return rid;.}../
3670: 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
3680: 77 20 70 68 61 6e 74 6f 6d 20 77 69 74 68 20 74  w phantom with t
3690: 68 65 20 67 69 76 65 6e 20 55 55 49 44 20 61 6e  he given UUID an
36a0: 64 20 72 65 74 75 72 6e 20 69 74 73 20 61 72 74  d return its art
36b0: 69 66 61 63 74 20 49 44 2e 0a 2a 2f 0a 69 6e 74  ifact ID..*/.int
36c0: 20 63 6f 6e 74 65 6e 74 5f 6e 65 77 28 63 6f 6e   content_new(con
36d0: 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 29 7b  st char *zUuid){
36e0: 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20 20 73 74  .  int rid;.  st
36f0: 61 74 69 63 20 53 74 6d 74 20 73 31 2c 20 73 32  atic Stmt s1, s2
3700: 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 67  ;.  .  assert( g
3710: 2e 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20  .repositoryOpen 
3720: 29 3b 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72  );.  db_begin_tr
3730: 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 64  ansaction();.  d
3740: 62 5f 73 74 61 74 69 63 5f 70 72 65 70 61 72 65  b_static_prepare
3750: 28 26 73 31 2c 0a 20 20 20 20 22 49 4e 53 45 52  (&s1,.    "INSER
3760: 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72 63 76 69  T INTO blob(rcvi
3770: 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63 6f 6e 74  d,size,uuid,cont
3780: 65 6e 74 29 22 0a 20 20 20 20 22 56 41 4c 55 45  ent)".    "VALUE
3790: 53 28 30 2c 2d 31 2c 3a 75 75 69 64 2c 4e 55 4c  S(0,-1,:uuid,NUL
37a0: 4c 29 22 0a 20 20 29 3b 0a 20 20 64 62 5f 62 69  L)".  );.  db_bi
37b0: 6e 64 5f 74 65 78 74 28 26 73 31 2c 20 22 3a 75  nd_text(&s1, ":u
37c0: 75 69 64 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20  uid", zUuid);.  
37d0: 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20  db_exec(&s1);.  
37e0: 72 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69 6e  rid = db_last_in
37f0: 73 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20 20  sert_rowid();.  
3800: 64 62 5f 73 74 61 74 69 63 5f 70 72 65 70 61 72  db_static_prepar
3810: 65 28 26 73 32 2c 0a 20 20 20 20 22 49 4e 53 45  e(&s2,.    "INSE
3820: 52 54 20 49 4e 54 4f 20 70 68 61 6e 74 6f 6d 20  RT INTO phantom 
3830: 56 41 4c 55 45 53 28 3a 72 69 64 29 22 0a 20 20  VALUES(:rid)".  
3840: 29 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 69 6e 74  );.  db_bind_int
3850: 28 26 73 32 2c 20 22 3a 72 69 64 22 2c 20 72 69  (&s2, ":rid", ri
3860: 64 29 3b 0a 20 20 64 62 5f 65 78 65 63 28 26 73  d);.  db_exec(&s
3870: 32 29 3b 0a 20 20 62 61 67 5f 69 6e 73 65 72 74  2);.  bag_insert
3880: 28 26 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d  (&contentCache.m
3890: 69 73 73 69 6e 67 2c 20 72 69 64 29 3b 0a 20 20  issing, rid);.  
38a0: 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  db_end_transacti
38b0: 6f 6e 28 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  on(0);.  return 
38c0: 72 69 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43  rid;.}.../*.** C
38d0: 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f  OMMAND:  test-co
38e0: 6e 74 65 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a 20  ntent-put.**.** 
38f0: 45 78 74 72 61 63 74 20 61 20 62 6c 6f 62 20 66  Extract a blob f
3900: 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
3910: 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e   and write it in
3920: 74 6f 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f  to a file..*/.vo
3930: 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f  id test_content_
3940: 70 75 74 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  put_cmd(void){. 
3950: 20 69 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62   int rid;.  Blob
3960: 20 63 6f 6e 74 65 6e 74 3b 0a 20 20 69 66 28 20   content;.  if( 
3970: 67 2e 61 72 67 63 21 3d 33 20 29 20 75 73 61 67  g.argc!=3 ) usag
3980: 65 28 22 46 49 4c 45 4e 41 4d 45 22 29 3b 0a 20  e("FILENAME");. 
3990: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68   db_must_be_with
39a0: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 75 73 65  in_tree();.  use
39b0: 72 5f 73 65 6c 65 63 74 28 29 3b 0a 20 20 62 6c  r_select();.  bl
39c0: 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c  ob_read_from_fil
39d0: 65 28 26 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 72  e(&content, g.ar
39e0: 67 76 5b 32 5d 29 3b 0a 20 20 72 69 64 20 3d 20  gv[2]);.  rid = 
39f0: 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e  content_put(&con
3a00: 74 65 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 70  tent, 0, 0);.  p
3a10: 72 69 6e 74 66 28 22 69 6e 73 65 72 74 65 64 20  rintf("inserted 
3a20: 61 73 20 72 65 63 6f 72 64 20 25 64 5c 6e 22 2c  as record %d\n",
3a30: 20 72 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   rid);.}../*.** 
3a40: 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f  Make sure the co
3a50: 6e 74 65 6e 74 20 61 74 20 72 69 64 20 69 73 20  ntent at rid is 
3a60: 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e  the original con
3a70: 74 65 6e 74 20 61 6e 64 20 69 73 20 6e 6f 74 20  tent and is not 
3a80: 61 0a 2a 2a 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76  a.** delta..*/.v
3a90: 6f 69 64 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65  oid content_unde
3aa0: 6c 74 61 28 69 6e 74 20 72 69 64 29 7b 0a 20 20  lta(int rid){.  
3ab0: 69 66 28 20 66 69 6e 64 53 72 63 69 64 28 72 69  if( findSrcid(ri
3ac0: 64 29 3e 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62  d)>0 ){.    Blob
3ad0: 20 78 3b 0a 20 20 20 20 69 66 28 20 63 6f 6e 74   x;.    if( cont
3ae0: 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 78 29  ent_get(rid, &x)
3af0: 20 29 7b 0a 20 20 20 20 20 20 53 74 6d 74 20 73   ){.      Stmt s
3b00: 3b 0a 20 20 20 20 20 20 64 62 5f 70 72 65 70 61  ;.      db_prepa
3b10: 72 65 28 26 73 2c 20 22 55 50 44 41 54 45 20 62  re(&s, "UPDATE b
3b20: 6c 6f 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d  lob SET content=
3b30: 3a 63 2c 20 73 69 7a 65 3d 25 64 20 57 48 45 52  :c, size=%d WHER
3b40: 45 20 72 69 64 3d 25 64 22 2c 0a 20 20 20 20 20  E rid=%d",.     
3b50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b60: 62 6c 6f 62 5f 73 69 7a 65 28 26 78 29 2c 20 72  blob_size(&x), r
3b70: 69 64 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  id);.      blob_
3b80: 63 6f 6d 70 72 65 73 73 28 26 78 2c 20 26 78 29  compress(&x, &x)
3b90: 3b 0a 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f  ;.      db_bind_
3ba0: 62 6c 6f 62 28 26 73 2c 20 22 3a 63 22 2c 20 26  blob(&s, ":c", &
3bb0: 78 29 3b 0a 20 20 20 20 20 20 64 62 5f 65 78 65  x);.      db_exe
3bc0: 63 28 26 73 29 3b 0a 20 20 20 20 20 20 64 62 5f  c(&s);.      db_
3bd0: 66 69 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20  finalize(&s);.  
3be0: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26      blob_reset(&
3bf0: 78 29 3b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c  x);.      db_mul
3c00: 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20  ti_exec("DELETE 
3c10: 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45  FROM delta WHERE
3c20: 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a   rid=%d", rid);.
3c30: 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
3c40: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73  ** COMMAND:  tes
3c50: 74 2d 63 6f 6e 74 65 6e 74 2d 75 6e 64 65 6c 74  t-content-undelt
3c60: 61 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  a.**.** Make sur
3c70: 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74  e the content at
3c80: 20 52 45 43 4f 52 44 49 44 20 69 73 20 6e 6f 74   RECORDID is not
3c90: 20 61 20 64 65 6c 74 61 0a 2a 2f 0a 76 6f 69 64   a delta.*/.void
3ca0: 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 75 6e   test_content_un
3cb0: 64 65 6c 74 61 5f 63 6d 64 28 76 6f 69 64 29 7b  delta_cmd(void){
3cc0: 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20 20 69 66  .  int rid;.  if
3cd0: 28 20 67 2e 61 72 67 63 21 3d 32 20 29 20 75 73  ( g.argc!=2 ) us
3ce0: 61 67 65 28 22 52 45 43 4f 52 44 49 44 22 29 3b  age("RECORDID");
3cf0: 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69  .  db_must_be_wi
3d00: 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72  thin_tree();.  r
3d10: 69 64 20 3d 20 61 74 6f 69 28 67 2e 61 72 67 76  id = atoi(g.argv
3d20: 5b 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f  [2]);.  content_
3d30: 75 6e 64 65 6c 74 61 28 72 69 64 29 3b 0a 7d 0a  undelta(rid);.}.
3d40: 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
3d50: 65 20 73 74 6f 72 61 67 65 20 6f 66 20 72 69 64  e storage of rid
3d60: 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 61   so that it is a
3d70: 20 64 65 6c 74 61 20 6f 66 20 73 72 63 69 64 2e   delta of srcid.
3d80: 0a 2a 2a 0a 2a 2a 20 49 66 20 72 69 64 20 69 73  .**.** If rid is
3d90: 20 61 6c 72 65 61 64 79 20 61 20 64 65 6c 74 61   already a delta
3da0: 20 66 72 6f 6d 20 73 6f 6d 65 20 6f 74 68 65 72   from some other
3db0: 20 70 6c 61 63 65 20 74 68 65 6e 20 6e 6f 0a 2a   place then no.*
3dc0: 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63  * conversion occ
3dd0: 75 72 73 20 61 6e 64 20 74 68 69 73 20 69 73 20  urs and this is 
3de0: 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 66  a no-op unless f
3df0: 6f 72 63 65 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 49  orce==1..**.** I
3e00: 66 20 73 72 63 69 64 20 69 73 20 61 20 64 65 6c  f srcid is a del
3e10: 74 61 20 74 68 61 74 20 64 65 70 65 6e 64 73 20  ta that depends 
3e20: 6f 6e 20 72 69 64 2c 20 74 68 65 6e 20 73 72 63  on rid, then src
3e30: 69 64 20 69 73 0a 2a 2a 20 63 6f 6e 76 65 72 74  id is.** convert
3e40: 65 64 20 74 6f 20 75 6e 64 65 6c 74 61 65 64 20  ed to undeltaed 
3e50: 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65  text..**.** If e
3e60: 69 74 68 65 72 20 72 69 64 20 6f 72 20 73 72 63  ither rid or src
3e70: 69 64 20 63 6f 6e 74 61 69 6e 20 6c 65 73 73 20  id contain less 
3e80: 74 68 61 6e 20 35 30 20 62 79 74 65 73 2c 20 6f  than 50 bytes, o
3e90: 72 20 69 66 20 74 68 65 0a 2a 2a 20 72 65 73 75  r if the.** resu
3ea0: 6c 74 69 6e 67 20 64 65 6c 74 61 20 64 6f 65 73  lting delta does
3eb0: 20 6e 6f 74 20 61 63 68 69 65 76 65 20 61 20 63   not achieve a c
3ec0: 6f 6d 70 72 65 73 73 69 6f 6e 20 6f 66 20 61 74  ompression of at
3ed0: 20 6c 65 61 73 74 20 32 35 25 20 6f 6e 0a 2a 2a   least 25% on.**
3ee0: 20 69 74 73 20 6f 77 6e 20 74 68 65 20 72 69 64   its own the rid
3ef0: 20 69 73 20 6c 65 66 74 20 75 6e 74 6f 75 63 68   is left untouch
3f00: 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20  ed..**.** NOTE: 
3f10: 49 4d 48 4f 20 74 68 65 20 63 72 65 61 74 69 6f  IMHO the creatio
3f20: 6e 20 6f 66 20 74 68 65 20 64 65 6c 74 61 20 73  n of the delta s
3f30: 68 6f 75 6c 64 20 62 65 20 64 65 66 65 72 65 64  hould be defered
3f40: 20 75 6e 74 69 6c 20 61 66 74 65 72 0a 2a 2a 20   until after.** 
3f50: 74 68 65 20 62 6c 6f 62 20 73 69 7a 65 73 20 68  the blob sizes h
3f60: 61 76 65 20 62 65 65 6e 20 63 68 65 63 6b 65 64  ave been checked
3f70: 2e 20 44 6f 69 6e 67 20 69 74 20 62 65 66 6f 72  . Doing it befor
3f80: 65 20 74 68 65 20 63 68 65 63 6b 20 61 73 20 69  e the check as i
3f90: 73 0a 2a 2a 20 64 6f 6e 65 20 6e 6f 77 20 74 68  s.** done now th
3fa0: 65 20 63 6f 64 65 20 77 69 6c 6c 20 67 65 6e 65  e code will gene
3fb0: 72 61 74 65 20 61 20 64 65 6c 74 61 20 6a 75 73  rate a delta jus
3fc0: 74 20 74 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79  t to immediately
3fd0: 20 74 68 72 6f 77 0a 2a 2a 20 69 74 20 61 77 61   throw.** it awa
3fe0: 79 2c 20 77 61 73 74 69 6e 67 20 73 70 61 63 65  y, wasting space
3ff0: 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a 76 6f   and time..*/.vo
4000: 69 64 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69  id content_delti
4010: 66 79 28 69 6e 74 20 72 69 64 2c 20 69 6e 74 20  fy(int rid, int 
4020: 73 72 63 69 64 2c 20 69 6e 74 20 66 6f 72 63 65  srcid, int force
4030: 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 42 6c  ){.  int s;.  Bl
4040: 6f 62 20 64 61 74 61 2c 20 73 72 63 2c 20 64 65  ob data, src, de
4050: 6c 74 61 3b 0a 20 20 53 74 6d 74 20 73 31 2c 20  lta;.  Stmt s1, 
4060: 73 32 3b 0a 20 20 69 66 28 20 73 72 63 69 64 3d  s2;.  if( srcid=
4070: 3d 72 69 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  =rid ) return;. 
4080: 20 69 66 28 20 21 66 6f 72 63 65 20 26 26 20 66   if( !force && f
4090: 69 6e 64 53 72 63 69 64 28 72 69 64 29 3e 30 20  indSrcid(rid)>0 
40a0: 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 20 3d 20  ) return;.  s = 
40b0: 73 72 63 69 64 3b 0a 20 20 77 68 69 6c 65 28 20  srcid;.  while( 
40c0: 28 73 20 3d 20 66 69 6e 64 53 72 63 69 64 28 73  (s = findSrcid(s
40d0: 29 29 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20  ))>0 ){.    if( 
40e0: 73 3d 3d 72 69 64 20 29 7b 0a 20 20 20 20 20 20  s==rid ){.      
40f0: 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28  content_undelta(
4100: 73 72 63 69 64 29 3b 0a 20 20 20 20 20 20 62 72  srcid);.      br
4110: 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
4120: 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 73 72 63   content_get(src
4130: 69 64 2c 20 26 73 72 63 29 3b 0a 20 20 63 6f 6e  id, &src);.  con
4140: 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 64  tent_get(rid, &d
4150: 61 74 61 29 3b 0a 20 20 62 6c 6f 62 5f 64 65 6c  ata);.  blob_del
4160: 74 61 5f 63 72 65 61 74 65 28 26 73 72 63 2c 20  ta_create(&src, 
4170: 26 64 61 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a  &data, &delta);.
4180: 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28    if( blob_size(
4190: 26 73 72 63 29 3e 3d 35 30 20 26 26 20 62 6c 6f  &src)>=50 && blo
41a0: 62 5f 73 69 7a 65 28 26 64 61 74 61 29 3e 3d 35  b_size(&data)>=5
41b0: 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  0 &&.           
41c0: 62 6c 6f 62 5f 73 69 7a 65 28 26 64 65 6c 74 61  blob_size(&delta
41d0: 29 20 3c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64  ) < blob_size(&d
41e0: 61 74 61 29 2a 30 2e 37 35 20 29 7b 0a 20 20 20  ata)*0.75 ){.   
41f0: 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 26   blob_compress(&
4200: 64 65 6c 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a  delta, &delta);.
4210: 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26      db_prepare(&
4220: 73 31 2c 20 22 55 50 44 41 54 45 20 62 6c 6f 62  s1, "UPDATE blob
4230: 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 64 61   SET content=:da
4240: 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64 22  ta WHERE rid=%d"
4250: 2c 20 72 69 64 29 3b 0a 20 20 20 20 64 62 5f 70  , rid);.    db_p
4260: 72 65 70 61 72 65 28 26 73 32 2c 20 22 52 45 50  repare(&s2, "REP
4270: 4c 41 43 45 20 49 4e 54 4f 20 64 65 6c 74 61 28  LACE INTO delta(
4280: 72 69 64 2c 73 72 63 69 64 29 56 41 4c 55 45 53  rid,srcid)VALUES
4290: 28 25 64 2c 25 64 29 22 2c 20 72 69 64 2c 20 73  (%d,%d)", rid, s
42a0: 72 63 69 64 29 3b 0a 20 20 20 20 64 62 5f 62 69  rcid);.    db_bi
42b0: 6e 64 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64  nd_blob(&s1, ":d
42c0: 61 74 61 22 2c 20 26 64 65 6c 74 61 29 3b 0a 20  ata", &delta);. 
42d0: 20 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e     db_begin_tran
42e0: 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 20 20 64  saction();.    d
42f0: 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20  b_exec(&s1);.   
4300: 20 64 62 5f 65 78 65 63 28 26 73 32 29 3b 0a 20   db_exec(&s2);. 
4310: 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61     db_end_transa
4320: 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20 20 64 62  ction(0);.    db
4330: 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a  _finalize(&s1);.
4340: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28      db_finalize(
4350: 26 73 32 29 3b 0a 20 20 20 20 76 65 72 69 66 79  &s2);.    verify
4360: 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72  _before_commit(r
4370: 69 64 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f  id);.  }.  blob_
4380: 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 62  reset(&src);.  b
4390: 6c 6f 62 5f 72 65 73 65 74 28 26 64 61 74 61 29  lob_reset(&data)
43a0: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  ;.  blob_reset(&
43b0: 64 65 6c 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  delta);.}../*.**
43c0: 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d   COMMAND:  test-
43d0: 63 6f 6e 74 65 6e 74 2d 64 65 6c 74 69 66 79 0a  content-deltify.
43e0: 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68  **.** Convert th
43f0: 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 52 49 44  e content at RID
4400: 20 69 6e 74 6f 20 61 20 64 65 6c 74 61 20 66 72   into a delta fr
4410: 6f 6d 20 53 52 43 49 44 2e 0a 2a 2f 0a 76 6f 69  om SRCID..*/.voi
4420: 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 64  d test_content_d
4430: 65 6c 74 69 66 79 5f 63 6d 64 28 76 6f 69 64 29  eltify_cmd(void)
4440: 7b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d  {.  if( g.argc!=
4450: 35 20 29 20 75 73 61 67 65 28 22 52 49 44 20 53  5 ) usage("RID S
4460: 52 43 49 44 20 46 4f 52 43 45 22 29 3b 0a 20 20  RCID FORCE");.  
4470: 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69  db_must_be_withi
4480: 6e 5f 74 72 65 65 28 29 3b 0a 20 20 63 6f 6e 74  n_tree();.  cont
4490: 65 6e 74 5f 64 65 6c 74 69 66 79 28 61 74 6f 69  ent_deltify(atoi
44a0: 28 67 2e 61 72 67 76 5b 32 5d 29 2c 20 61 74 6f  (g.argv[2]), ato
44b0: 69 28 67 2e 61 72 67 76 5b 33 5d 29 2c 20 61 74  i(g.argv[3]), at
44c0: 6f 69 28 67 2e 61 72 67 76 5b 34 5d 29 29 3b 0a  oi(g.argv[4]));.
44d0: 7d 0a                                            }.