Hex Artifact Content
Not logged in

Artifact 57def314ab8fba3757cbb7410ce793bd4a836606:

File src/content.c part of check-in [8480dd38d2] - Add the "artifact" command for extracting the content of individual artifacts. The new "artifact" command is really just a renaming of "test-content-get". by drh on 2009-10-31 17:01:35.

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 47 65 74 20 74 68 65 20 62 6c 6f 62 2e  ** Get the blob.
1180: 63 6f 6e 74 65 6e 74 20 76 61 6c 75 65 20 66 6f  content value fo
1190: 72 20 62 6c 6f 62 2e 72 69 64 3d 72 69 64 2e 20  r blob.rid=rid. 
11a0: 20 52 65 74 75 72 6e 20 31 20 6f 6e 20 73 75 63   Return 1 on suc
11b0: 63 65 73 73 20 6f 72 0a 2a 2a 20 30 20 6f 6e 20  cess or.** 0 on 
11c0: 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  failure..*/.stat
11d0: 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 6f  ic int content_o
11e0: 66 5f 62 6c 6f 62 28 69 6e 74 20 72 69 64 2c 20  f_blob(int rid, 
11f0: 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20  Blob *pBlob){.  
1200: 73 74 61 74 69 63 20 53 74 6d 74 20 71 3b 0a 20  static Stmt q;. 
1210: 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 64   int rc = 0;.  d
1220: 62 5f 73 74 61 74 69 63 5f 70 72 65 70 61 72 65  b_static_prepare
1230: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e  (&q, "SELECT con
1240: 74 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57  tent FROM blob W
1250: 48 45 52 45 20 72 69 64 3d 3a 72 69 64 20 41 4e  HERE rid=:rid AN
1260: 44 20 73 69 7a 65 3e 3d 30 22 29 3b 0a 20 20 64  D size>=0");.  d
1270: 62 5f 62 69 6e 64 5f 69 6e 74 28 26 71 2c 20 22  b_bind_int(&q, "
1280: 3a 72 69 64 22 2c 20 72 69 64 29 3b 0a 20 20 69  :rid", rid);.  i
1290: 66 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d  f( db_step(&q)==
12a0: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
12b0: 20 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62    db_ephemeral_b
12c0: 6c 6f 62 28 26 71 2c 20 30 2c 20 70 42 6c 6f 62  lob(&q, 0, pBlob
12d0: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 75 6e 63 6f  );.    blob_unco
12e0: 6d 70 72 65 73 73 28 70 42 6c 6f 62 2c 20 70 42  mpress(pBlob, pB
12f0: 6c 6f 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 31  lob);.    rc = 1
1300: 3b 0a 20 20 7d 0a 20 20 64 62 5f 72 65 73 65 74  ;.  }.  db_reset
1310: 28 26 71 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  (&q);.  return r
1320: 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  c;.}.../*.** Ext
1330: 72 61 63 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  ract the content
1340: 20 66 6f 72 20 49 44 20 72 69 64 20 61 6e 64 20   for ID rid and 
1350: 70 75 74 20 69 74 20 69 6e 74 6f 20 74 68 65 0a  put it into the.
1360: 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  ** uninitialized
1370: 20 62 6c 6f 62 2e 20 20 52 65 74 75 72 6e 20 31   blob.  Return 1
1380: 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66   on success.  If
1390: 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20 69   the record.** i
13a0: 73 20 61 20 70 68 61 6e 74 6f 6d 2c 20 7a 65 72  s a phantom, zer
13b0: 6f 20 70 42 6c 6f 62 20 61 6e 64 20 72 65 74 75  o pBlob and retu
13c0: 72 6e 20 30 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e  rn 0..*/.int con
13d0: 74 65 6e 74 5f 67 65 74 28 69 6e 74 20 72 69 64  tent_get(int rid
13e0: 2c 20 42 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a  , Blob *pBlob){.
13f0: 20 20 42 6c 6f 62 20 73 72 63 3b 0a 20 20 69 6e    Blob src;.  in
1400: 74 20 73 72 63 69 64 3b 0a 20 20 69 6e 74 20 72  t srcid;.  int r
1410: 63 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a  c = 0;.  int i;.
1420: 20 20 73 74 61 74 69 63 20 42 61 67 20 69 6e 50    static Bag inP
1430: 72 6f 63 65 73 73 3b 0a 0a 20 20 61 73 73 65 72  rocess;..  asser
1440: 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 79 4f  t( g.repositoryO
1450: 70 65 6e 20 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65  pen );.  blob_ze
1460: 72 6f 28 70 42 6c 6f 62 29 3b 0a 20 20 69 66 28  ro(pBlob);.  if(
1470: 20 72 69 64 3d 3d 30 20 29 20 72 65 74 75 72 6e   rid==0 ) return
1480: 20 30 3b 0a 0a 20 20 2f 2a 20 45 61 72 6c 79 20   0;..  /* Early 
1490: 6f 75 74 20 69 66 20 77 65 20 6b 6e 6f 77 20 74  out if we know t
14a0: 68 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f  he content is no
14b0: 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20  t available */. 
14c0: 20 69 66 28 20 62 61 67 5f 66 69 6e 64 28 26 63   if( bag_find(&c
14d0: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69 73 73  ontentCache.miss
14e0: 69 6e 67 2c 20 72 69 64 29 20 29 7b 0a 20 20 20  ing, rid) ){.   
14f0: 20 43 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28 28   CONTENT_TRACE((
1500: 22 25 2a 73 6d 69 73 73 20 66 72 6f 6d 20 63 61  "%*smiss from ca
1510: 63 68 65 3a 20 25 64 5c 6e 22 2c 0a 20 20 20 20  che: %d\n",.    
1520: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1530: 62 61 67 5f 63 6f 75 6e 74 28 26 69 6e 50 72 6f  bag_count(&inPro
1540: 63 65 73 73 29 2c 20 22 22 2c 20 72 69 64 29 29  cess), "", rid))
1550: 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
1560: 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f   }..  /* Look fo
1570: 72 20 74 68 65 20 61 72 74 69 66 61 63 74 20 69  r the artifact i
1580: 6e 20 74 68 65 20 63 61 63 68 65 20 66 69 72 73  n the cache firs
1590: 74 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t */.  for(i=0; 
15a0: 69 3c 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6e  i<contentCache.n
15b0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
15c0: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69  contentCache.a[i
15d0: 5d 2e 72 69 64 3d 3d 72 69 64 20 29 7b 0a 20 20  ].rid==rid ){.  
15e0: 20 20 20 20 2a 70 42 6c 6f 62 20 3d 20 63 6f 6e      *pBlob = con
15f0: 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d 2e 63  tentCache.a[i].c
1600: 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 62 6c  ontent;.      bl
1610: 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74  ob_zero(&content
1620: 43 61 63 68 65 2e 61 5b 69 5d 2e 63 6f 6e 74 65  Cache.a[i].conte
1630: 6e 74 29 3b 0a 20 20 20 20 20 20 63 6f 6e 74 65  nt);.      conte
1640: 6e 74 43 61 63 68 65 2e 6e 2d 2d 3b 0a 20 20 20  ntCache.n--;.   
1650: 20 20 20 69 66 28 20 69 3c 63 6f 6e 74 65 6e 74     if( i<content
1660: 43 61 63 68 65 2e 6e 20 29 7b 0a 20 20 20 20 20  Cache.n ){.     
1670: 20 20 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e     contentCache.
1680: 61 5b 69 5d 20 3d 20 63 6f 6e 74 65 6e 74 43 61  a[i] = contentCa
1690: 63 68 65 2e 61 5b 63 6f 6e 74 65 6e 74 43 61 63  che.a[contentCac
16a0: 68 65 2e 6e 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  he.n];.      }. 
16b0: 20 20 20 20 20 43 4f 4e 54 45 4e 54 5f 54 52 41       CONTENT_TRA
16c0: 43 45 28 28 22 25 2a 73 63 61 63 68 65 3a 20 25  CE(("%*scache: %
16d0: 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20  d\n", .         
16e0: 20 20 20 20 20 20 20 20 20 20 20 62 61 67 5f 63             bag_c
16f0: 6f 75 6e 74 28 26 69 6e 50 72 6f 63 65 73 73 29  ount(&inProcess)
1700: 2c 20 22 22 2c 20 72 69 64 29 29 0a 20 20 20 20  , "", rid)).    
1710: 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1720: 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20  }.  }..  /* See 
1730: 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 70  if we need to ap
1740: 70 6c 79 20 61 20 64 65 6c 74 61 20 74 6f 20 66  ply a delta to f
1750: 69 6e 64 20 74 68 69 73 20 61 72 74 69 66 61 63  ind this artifac
1760: 74 20 2a 2f 0a 20 20 73 72 63 69 64 20 3d 20 66  t */.  srcid = f
1770: 69 6e 64 53 72 63 69 64 28 72 69 64 29 3b 0a 20  indSrcid(rid);. 
1780: 20 43 4f 4e 54 45 4e 54 5f 54 52 41 43 45 28 28   CONTENT_TRACE((
1790: 22 25 2a 73 73 65 61 72 63 68 69 6e 67 20 66 6f  "%*ssearching fo
17a0: 72 20 25 64 2e 20 20 4e 65 65 64 20 25 64 2e 5c  r %d.  Need %d.\
17b0: 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
17c0: 20 20 20 20 20 62 61 67 5f 63 6f 75 6e 74 28 26       bag_count(&
17d0: 69 6e 50 72 6f 63 65 73 73 29 2c 20 22 22 2c 20  inProcess), "", 
17e0: 72 69 64 2c 20 73 72 63 69 64 29 29 0a 0a 0a 20  rid, srcid))... 
17f0: 20 69 66 28 20 73 72 63 69 64 20 29 7b 0a 20 20   if( srcid ){.  
1800: 20 20 2f 2a 20 59 65 73 2c 20 61 20 64 65 6c 74    /* Yes, a delt
1810: 61 20 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f  a is required */
1820: 0a 20 20 20 20 69 66 28 20 62 61 67 5f 66 69 6e  .    if( bag_fin
1830: 64 28 26 69 6e 50 72 6f 63 65 73 73 2c 20 73 72  d(&inProcess, sr
1840: 63 69 64 29 20 29 7b 0a 20 20 20 20 20 20 64 62  cid) ){.      db
1850: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
1860: 20 20 20 20 20 22 55 50 44 41 54 45 20 62 6c 6f       "UPDATE blo
1870: 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 4e 55  b SET content=NU
1880: 4c 4c 2c 20 73 69 7a 65 3d 2d 31 20 57 48 45 52  LL, size=-1 WHER
1890: 45 20 72 69 64 3d 25 64 3b 22 0a 20 20 20 20 20  E rid=%d;".     
18a0: 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
18b0: 64 65 6c 74 61 20 57 48 45 52 45 20 72 69 64 3d  delta WHERE rid=
18c0: 25 64 3b 22 0a 20 20 20 20 20 20 20 20 22 49 4e  %d;".        "IN
18d0: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49  SERT OR IGNORE I
18e0: 4e 54 4f 20 70 68 61 6e 74 6f 6d 20 56 41 4c 55  NTO phantom VALU
18f0: 45 53 28 25 64 29 3b 22 2c 0a 20 20 20 20 20 20  ES(%d);",.      
1900: 20 20 73 72 63 69 64 2c 20 73 72 63 69 64 2c 20    srcid, srcid, 
1910: 73 72 63 69 64 0a 20 20 20 20 20 20 29 3b 0a 20  srcid.      );. 
1920: 20 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 70       blob_zero(p
1930: 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 72 65 74  Blob);.      ret
1940: 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 0;.    }.   
1950: 20 62 61 67 5f 69 6e 73 65 72 74 28 26 69 6e 50   bag_insert(&inP
1960: 72 6f 63 65 73 73 2c 20 73 72 63 69 64 29 3b 0a  rocess, srcid);.
1970: 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74  .    if( content
1980: 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73 72 63  _get(srcid, &src
1990: 29 20 29 7b 0a 20 20 20 20 20 20 42 6c 6f 62 20  ) ){.      Blob 
19a0: 64 65 6c 74 61 3b 0a 20 20 20 20 20 20 69 66 28  delta;.      if(
19b0: 20 63 6f 6e 74 65 6e 74 5f 6f 66 5f 62 6c 6f 62   content_of_blob
19c0: 28 72 69 64 2c 20 26 64 65 6c 74 61 29 20 29 7b  (rid, &delta) ){
19d0: 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 69 6e  .        blob_in
19e0: 69 74 28 70 42 6c 6f 62 2c 30 2c 30 29 3b 0a 20  it(pBlob,0,0);. 
19f0: 20 20 20 20 20 20 20 62 6c 6f 62 5f 64 65 6c 74         blob_delt
1a00: 61 5f 61 70 70 6c 79 28 26 73 72 63 2c 20 26 64  a_apply(&src, &d
1a10: 65 6c 74 61 2c 20 70 42 6c 6f 62 29 3b 0a 20 20  elta, pBlob);.  
1a20: 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74        blob_reset
1a30: 28 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 20 20  (&delta);.      
1a40: 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20    rc = 1;.      
1a50: 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 61 76 65  }..      /* Save
1a60: 20 74 68 65 20 73 72 63 69 64 20 61 72 74 69 66   the srcid artif
1a70: 61 63 74 20 69 6e 20 74 68 65 20 63 61 63 68 65  act in the cache
1a80: 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 63 6f   */.      if( co
1a90: 6e 74 65 6e 74 43 61 63 68 65 2e 6e 3c 4d 58 5f  ntentCache.n<MX_
1aa0: 43 41 43 48 45 5f 43 4e 54 20 29 7b 0a 20 20 20  CACHE_CNT ){.   
1ab0: 20 20 20 20 20 69 20 3d 20 63 6f 6e 74 65 6e 74       i = content
1ac0: 43 61 63 68 65 2e 6e 2b 2b 3b 0a 20 20 20 20 20  Cache.n++;.     
1ad0: 20 7d 65 6c 73 65 20 69 66 28 20 28 28 63 6f 6e   }else if( ((con
1ae0: 74 65 6e 74 43 61 63 68 65 2e 73 6b 69 70 43 6e  tentCache.skipCn
1af0: 74 2b 2b 29 25 45 58 50 45 4c 4c 5f 49 4e 54 45  t++)%EXPELL_INTE
1b00: 52 56 41 4c 29 21 3d 30 20 29 7b 0a 20 20 20 20  RVAL)!=0 ){.    
1b10: 20 20 20 20 69 20 3d 20 2d 31 3b 0a 20 20 20 20      i = -1;.    
1b20: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1b30: 20 69 6e 74 20 6a 2c 20 62 65 73 74 3b 0a 20 20   int j, best;.  
1b40: 20 20 20 20 20 20 62 65 73 74 20 3d 20 63 6f 6e        best = con
1b50: 74 65 6e 74 43 61 63 68 65 2e 6e 65 78 74 41 67  tentCache.nextAg
1b60: 65 2b 31 3b 0a 20 20 20 20 20 20 20 20 69 20 3d  e+1;.        i =
1b70: 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72   -1;.        for
1b80: 28 6a 3d 30 3b 20 6a 3c 63 6f 6e 74 65 6e 74 43  (j=0; j<contentC
1b90: 61 63 68 65 2e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20  ache.n; j++){.  
1ba0: 20 20 20 20 20 20 20 20 69 66 28 20 63 6f 6e 74          if( cont
1bb0: 65 6e 74 43 61 63 68 65 2e 61 5b 6a 5d 2e 61 67  entCache.a[j].ag
1bc0: 65 3c 62 65 73 74 20 29 7b 0a 20 20 20 20 20 20  e<best ){.      
1bd0: 20 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 20        i = j;.   
1be0: 20 20 20 20 20 20 20 20 20 62 65 73 74 20 3d 20           best = 
1bf0: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 6a  contentCache.a[j
1c00: 5d 2e 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  ].age;.         
1c10: 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1c20: 20 20 20 20 20 43 4f 4e 54 45 4e 54 5f 54 52 41       CONTENT_TRA
1c30: 43 45 28 28 22 25 2a 73 65 78 70 65 6c 6c 20 25  CE(("%*sexpell %
1c40: 64 20 66 72 6f 6d 20 63 61 63 68 65 5c 6e 22 2c  d from cache\n",
1c50: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1c60: 20 20 20 20 20 20 20 20 62 61 67 5f 63 6f 75 6e          bag_coun
1c70: 74 28 26 69 6e 50 72 6f 63 65 73 73 29 2c 20 22  t(&inProcess), "
1c80: 22 2c 20 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e  ", contentCache.
1c90: 61 5b 69 5d 2e 72 69 64 29 29 0a 20 20 20 20 20  a[i].rid)).     
1ca0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63     blob_reset(&c
1cb0: 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d  ontentCache.a[i]
1cc0: 2e 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20  .content);.     
1cd0: 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3e 3d   }.      if( i>=
1ce0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e  0 ){.        con
1cf0: 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d 2e 63  tentCache.a[i].c
1d00: 6f 6e 74 65 6e 74 20 3d 20 73 72 63 3b 0a 20 20  ontent = src;.  
1d10: 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 43 61 63        contentCac
1d20: 68 65 2e 61 5b 69 5d 2e 61 67 65 20 3d 20 63 6f  he.a[i].age = co
1d30: 6e 74 65 6e 74 43 61 63 68 65 2e 6e 65 78 74 41  ntentCache.nextA
1d40: 67 65 2b 2b 3b 0a 20 20 20 20 20 20 20 20 63 6f  ge++;.        co
1d50: 6e 74 65 6e 74 43 61 63 68 65 2e 61 5b 69 5d 2e  ntentCache.a[i].
1d60: 72 69 64 20 3d 20 73 72 63 69 64 3b 0a 20 20 20  rid = srcid;.   
1d70: 20 20 20 20 20 43 4f 4e 54 45 4e 54 5f 54 52 41       CONTENT_TRA
1d80: 43 45 28 28 22 25 2a 73 61 64 64 20 25 64 20 74  CE(("%*sadd %d t
1d90: 6f 20 63 61 63 68 65 5c 6e 22 2c 0a 20 20 20 20  o cache\n",.    
1da0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1db0: 20 20 20 62 61 67 5f 63 6f 75 6e 74 28 26 69 6e     bag_count(&in
1dc0: 50 72 6f 63 65 73 73 29 2c 20 22 22 2c 20 73 72  Process), "", sr
1dd0: 63 69 64 29 29 0a 20 20 20 20 20 20 7d 65 6c 73  cid)).      }els
1de0: 65 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  e{.        blob_
1df0: 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 20  reset(&src);.   
1e00: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 62     }.    }.    b
1e10: 61 67 5f 72 65 6d 6f 76 65 28 26 69 6e 50 72 6f  ag_remove(&inPro
1e20: 63 65 73 73 2c 20 73 72 63 69 64 29 3b 0a 20 20  cess, srcid);.  
1e30: 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 6f  }else{.    /* No
1e40: 20 64 65 6c 74 61 20 72 65 71 75 69 72 65 64 2e   delta required.
1e50: 20 20 52 65 61 64 20 63 6f 6e 74 65 6e 74 20 64    Read content d
1e60: 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65  irectly from the
1e70: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
1e80: 20 69 66 28 20 63 6f 6e 74 65 6e 74 5f 6f 66 5f   if( content_of_
1e90: 62 6c 6f 62 28 72 69 64 2c 20 70 42 6c 6f 62 29  blob(rid, pBlob)
1ea0: 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 31   ){.      rc = 1
1eb0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1ec0: 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 62  ( rc==0 ){.    b
1ed0: 61 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e 74 65  ag_insert(&conte
1ee0: 6e 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c  ntCache.missing,
1ef0: 20 72 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a   rid);.  }else{.
1f00: 20 20 20 20 62 61 67 5f 69 6e 73 65 72 74 28 26      bag_insert(&
1f10: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 61 76 61  contentCache.ava
1f20: 69 6c 61 62 6c 65 2c 20 72 69 64 29 3b 0a 20 20  ilable, rid);.  
1f30: 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1f40: 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20  ../*.** Get the 
1f50: 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 66 69  contents of a fi
1f60: 6c 65 20 77 69 74 68 69 6e 20 61 20 67 69 76 65  le within a give
1f70: 6e 20 62 61 73 65 6c 69 6e 65 2e 0a 2a 2f 0a 69  n baseline..*/.i
1f80: 6e 74 20 63 6f 6e 74 65 6e 74 5f 67 65 74 5f 68  nt content_get_h
1f90: 69 73 74 6f 72 69 63 61 6c 5f 66 69 6c 65 28 0a  istorical_file(.
1fa0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 65    const char *re
1fb0: 76 69 73 69 6f 6e 2c 20 20 20 20 2f 2a 20 4e 61  vision,    /* Na
1fc0: 6d 65 20 6f 66 20 74 68 65 20 62 61 73 65 6c 69  me of the baseli
1fd0: 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ne containing th
1fe0: 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  e file */.  cons
1ff0: 74 20 63 68 61 72 20 2a 66 69 6c 65 2c 20 20 20  t char *file,   
2000: 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2010: 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 42 6c  the file */.  Bl
2020: 6f 62 20 2a 63 6f 6e 74 65 6e 74 20 20 20 20 20  ob *content     
2030: 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2040: 66 69 6c 65 20 63 6f 6e 74 65 6e 74 20 68 65 72  file content her
2050: 65 20 2a 2f 0a 29 7b 0a 20 20 42 6c 6f 62 20 6d  e */.){.  Blob m
2060: 66 69 6c 65 3b 0a 20 20 4d 61 6e 69 66 65 73 74  file;.  Manifest
2070: 20 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 72 69 64   m;.  int i, rid
2080: 3d 30 3b 0a 20 20 0a 20 20 72 69 64 20 3d 20 6e  =0;.  .  rid = n
2090: 61 6d 65 5f 74 6f 5f 72 69 64 28 72 65 76 69 73  ame_to_rid(revis
20a0: 69 6f 6e 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f  ion);.  content_
20b0: 67 65 74 28 72 69 64 2c 20 26 6d 66 69 6c 65 29  get(rid, &mfile)
20c0: 3b 0a 20 20 0a 20 20 69 66 28 20 6d 61 6e 69 66  ;.  .  if( manif
20d0: 65 73 74 5f 70 61 72 73 65 28 26 6d 2c 20 26 6d  est_parse(&m, &m
20e0: 66 69 6c 65 29 20 29 7b 0a 20 20 20 20 66 6f 72  file) ){.    for
20f0: 28 69 3d 30 3b 20 69 3c 6d 2e 6e 46 69 6c 65 3b  (i=0; i<m.nFile;
2100: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
2110: 20 73 74 72 63 6d 70 28 6d 2e 61 46 69 6c 65 5b   strcmp(m.aFile[
2120: 69 5d 2e 7a 4e 61 6d 65 2c 20 66 69 6c 65 29 3d  i].zName, file)=
2130: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 69  =0 ){.        ri
2140: 64 20 3d 20 75 75 69 64 5f 74 6f 5f 72 69 64 28  d = uuid_to_rid(
2150: 6d 2e 61 46 69 6c 65 5b 69 5d 2e 7a 55 75 69 64  m.aFile[i].zUuid
2160: 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 72 65  , 0);.        re
2170: 74 75 72 6e 20 63 6f 6e 74 65 6e 74 5f 67 65 74  turn content_get
2180: 28 72 69 64 2c 20 63 6f 6e 74 65 6e 74 29 3b 0a  (rid, content);.
2190: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
21a0: 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22    fossil_panic("
21b0: 66 69 6c 65 3a 20 25 73 20 64 6f 65 73 20 6e 6f  file: %s does no
21c0: 74 20 65 78 69 73 74 20 69 6e 20 72 65 76 69 73  t exist in revis
21d0: 69 6f 6e 3a 20 25 73 22 2c 20 66 69 6c 65 2c 20  ion: %s", file, 
21e0: 72 65 76 69 73 69 6f 6e 29 3b 0a 20 20 7d 65 6c  revision);.  }el
21f0: 73 65 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70  se{.    fossil_p
2200: 61 6e 69 63 28 22 63 6f 75 6c 64 20 6e 6f 74 20  anic("could not 
2210: 70 61 72 73 65 20 6d 61 6e 69 66 65 73 74 20 66  parse manifest f
2220: 6f 72 20 72 65 76 69 73 69 6f 6e 3a 20 25 73 22  or revision: %s"
2230: 2c 20 72 65 76 69 73 69 6f 6e 29 3b 0a 20 20 7d  , revision);.  }
2240: 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  .  .  return 0;.
2250: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44  }../*.** COMMAND
2260: 3a 20 20 61 72 74 69 66 61 63 74 0a 2a 2a 0a 2a  :  artifact.**.*
2270: 2a 20 55 73 61 67 65 3a 20 25 66 6f 73 73 69 6c  * Usage: %fossil
2280: 20 61 72 74 69 66 61 63 74 20 41 52 54 49 46 41   artifact ARTIFA
2290: 43 54 2d 49 44 20 20 3f 4f 55 54 50 55 54 2d 46  CT-ID  ?OUTPUT-F
22a0: 49 4c 45 4e 41 4d 45 3f 0a 2a 2a 0a 2a 2a 20 45  ILENAME?.**.** E
22b0: 78 74 72 61 63 74 20 61 6e 20 61 72 74 69 66 61  xtract an artifa
22c0: 63 74 20 62 79 20 69 74 73 20 53 48 41 31 20 68  ct by its SHA1 h
22d0: 61 73 68 20 61 6e 64 20 77 72 69 74 65 20 74 68  ash and write th
22e0: 65 20 72 65 73 75 6c 74 73 20 6f 6e 0a 2a 2a 20  e results on.** 
22f0: 73 74 61 6e 64 61 72 64 20 6f 75 74 70 75 74 2c  standard output,
2300: 20 6f 72 20 69 66 20 74 68 65 20 6f 70 74 69 6f   or if the optio
2310: 6e 61 6c 20 34 74 68 20 61 72 67 75 6d 65 6e 74  nal 4th argument
2320: 20 69 73 20 67 69 76 65 6e 2c 20 69 6e 0a 2a 2a   is given, in.**
2330: 20 74 68 65 20 6e 61 6d 65 64 20 6f 75 74 70 75   the named outpu
2340: 74 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20  t file..*/.void 
2350: 61 72 74 69 66 61 63 74 5f 63 6d 64 28 76 6f 69  artifact_cmd(voi
2360: 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20  d){.  int rid;. 
2370: 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a 20   Blob content;. 
2380: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
2390: 6c 65 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63  le;.  if( g.argc
23a0: 21 3d 34 20 26 26 20 67 2e 61 72 67 63 21 3d 33  !=4 && g.argc!=3
23b0: 20 29 20 75 73 61 67 65 28 22 52 45 43 4f 52 44   ) usage("RECORD
23c0: 49 44 20 3f 46 49 4c 45 4e 41 4d 45 3f 22 29 3b  ID ?FILENAME?");
23d0: 0a 20 20 7a 46 69 6c 65 20 3d 20 67 2e 61 72 67  .  zFile = g.arg
23e0: 63 3d 3d 34 20 3f 20 67 2e 61 72 67 76 5b 33 5d  c==4 ? g.argv[3]
23f0: 20 3a 20 22 2d 22 3b 0a 20 20 64 62 5f 6d 75 73   : "-";.  db_mus
2400: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65  t_be_within_tree
2410: 28 29 3b 0a 20 20 72 69 64 20 3d 20 6e 61 6d 65  ();.  rid = name
2420: 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76 5b 32  _to_rid(g.argv[2
2430: 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 65  ]);.  content_ge
2440: 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29  t(rid, &content)
2450: 3b 0a 20 20 62 6c 6f 62 5f 77 72 69 74 65 5f 74  ;.  blob_write_t
2460: 6f 5f 66 69 6c 65 28 26 63 6f 6e 74 65 6e 74 2c  o_file(&content,
2470: 20 7a 46 69 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   zFile);.}../*.*
2480: 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74  * COMMAND:  test
2490: 2d 63 6f 6e 74 65 6e 74 2d 72 61 77 67 65 74 0a  -content-rawget.
24a0: 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61 20  **.** Extract a 
24b0: 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 20 64 61  blob from the da
24c0: 74 61 62 61 73 65 20 61 6e 64 20 77 72 69 74 65  tabase and write
24d0: 20 69 74 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e   it into a file.
24e0: 20 20 54 68 69 73 0a 2a 2a 20 76 65 72 73 69 6f    This.** versio
24f0: 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 70 61 6e  n does not expan
2500: 64 20 74 68 65 20 64 65 6c 74 61 2e 0a 2a 2f 0a  d the delta..*/.
2510: 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e  void test_conten
2520: 74 5f 72 61 77 67 65 74 5f 63 6d 64 28 76 6f 69  t_rawget_cmd(voi
2530: 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20  d){.  int rid;. 
2540: 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a 20   Blob content;. 
2550: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
2560: 6c 65 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63  le;.  if( g.argc
2570: 21 3d 34 20 26 26 20 67 2e 61 72 67 63 21 3d 33  !=4 && g.argc!=3
2580: 20 29 20 75 73 61 67 65 28 22 52 45 43 4f 52 44   ) usage("RECORD
2590: 49 44 20 3f 46 49 4c 45 4e 41 4d 45 3f 22 29 3b  ID ?FILENAME?");
25a0: 0a 20 20 7a 46 69 6c 65 20 3d 20 67 2e 61 72 67  .  zFile = g.arg
25b0: 63 3d 3d 34 20 3f 20 67 2e 61 72 67 76 5b 33 5d  c==4 ? g.argv[3]
25c0: 20 3a 20 22 2d 22 3b 0a 20 20 64 62 5f 6d 75 73   : "-";.  db_mus
25d0: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65  t_be_within_tree
25e0: 28 29 3b 0a 20 20 72 69 64 20 3d 20 6e 61 6d 65  ();.  rid = name
25f0: 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76 5b 32  _to_rid(g.argv[2
2600: 5d 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28  ]);.  blob_zero(
2610: 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 64 62 5f  &content);.  db_
2620: 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 2c 20 22  blob(&content, "
2630: 53 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74 20 46  SELECT content F
2640: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72  ROM blob WHERE r
2650: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20  id=%d", rid);.  
2660: 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 73 73 28  blob_uncompress(
2670: 26 63 6f 6e 74 65 6e 74 2c 20 26 63 6f 6e 74 65  &content, &conte
2680: 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72 69 74  nt);.  blob_writ
2690: 65 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e 74 65  e_to_file(&conte
26a0: 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a 0a 2f  nt, zFile);.}../
26b0: 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 72 65 63 6f  *.** When a reco
26c0: 72 64 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  rd is converted 
26d0: 66 72 6f 6d 20 61 20 70 68 61 6e 74 6f 6d 20 74  from a phantom t
26e0: 6f 20 61 20 72 65 61 6c 20 72 65 63 6f 72 64 2c  o a real record,
26f0: 0a 2a 2a 20 69 66 20 74 68 61 74 20 72 65 63 6f  .** if that reco
2700: 72 64 20 68 61 73 20 6f 74 68 65 72 20 72 65 63  rd has other rec
2710: 6f 72 64 73 20 74 68 61 74 20 61 72 65 20 64 65  ords that are de
2720: 72 69 76 65 64 20 62 79 20 64 65 6c 74 61 2c 0a  rived by delta,.
2730: 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20 6d 61 6e  ** then call man
2740: 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28  ifest_crosslink(
2750: 29 20 6f 6e 20 74 68 6f 73 65 20 6f 74 68 65 72  ) on those other
2760: 20 72 65 63 6f 72 64 73 2e 0a 2a 2f 0a 76 6f 69   records..*/.voi
2770: 64 20 61 66 74 65 72 5f 64 65 70 68 61 6e 74 6f  d after_dephanto
2780: 6d 69 7a 65 28 69 6e 74 20 72 69 64 2c 20 69 6e  mize(int rid, in
2790: 74 20 6c 69 6e 6b 46 6c 61 67 29 7b 0a 20 20 53  t linkFlag){.  S
27a0: 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70 72 65 70  tmt q;.  db_prep
27b0: 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20  are(&q, "SELECT 
27c0: 72 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57  rid FROM delta W
27d0: 48 45 52 45 20 73 72 63 69 64 3d 25 64 22 2c 20  HERE srcid=%d", 
27e0: 72 69 64 29 3b 0a 20 20 77 68 69 6c 65 28 20 64  rid);.  while( d
27f0: 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49  b_step(&q)==SQLI
2800: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 69 6e  TE_ROW ){.    in
2810: 74 20 74 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d  t tid = db_colum
2820: 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20  n_int(&q, 0);.  
2830: 20 20 61 66 74 65 72 5f 64 65 70 68 61 6e 74 6f    after_dephanto
2840: 6d 69 7a 65 28 74 69 64 2c 20 31 29 3b 0a 20 20  mize(tid, 1);.  
2850: 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28  }.  db_finalize(
2860: 26 71 29 3b 0a 20 20 69 66 28 20 6c 69 6e 6b 46  &q);.  if( linkF
2870: 6c 61 67 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20  lag ){.    Blob 
2880: 63 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 63 6f 6e  content;.    con
2890: 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 63  tent_get(rid, &c
28a0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 6d 61 6e  ontent);.    man
28b0: 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28  ifest_crosslink(
28c0: 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a  rid, &content);.
28d0: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26      blob_reset(&
28e0: 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a  content);.  }.}.
28f0: 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63 6f 6e  ./*.** Write con
2900: 74 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 64 61  tent into the da
2910: 74 61 62 61 73 65 2e 20 20 52 65 74 75 72 6e 20  tabase.  Return 
2920: 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e 20 20  the record ID.  
2930: 49 66 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e  If the.** conten
2940: 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  t is already in 
2950: 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 6a 75  the database, ju
2960: 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 72 65  st return the re
2970: 63 6f 72 64 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 49  cord ID..**.** I
2980: 66 20 73 72 63 49 64 20 69 73 20 73 70 65 63 69  f srcId is speci
2990: 66 69 65 64 2c 20 74 68 65 6e 20 70 42 6c 6f 62  fied, then pBlob
29a0: 20 69 73 20 64 65 6c 74 61 20 63 6f 6e 74 65 6e   is delta conten
29b0: 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 72  t from.** the sr
29c0: 63 49 64 20 72 65 63 6f 72 64 2e 20 20 73 72 63  cId record.  src
29d0: 49 64 20 6d 69 67 68 74 20 62 65 20 61 20 70 68  Id might be a ph
29e0: 61 6e 74 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 7a 55 75  antom..**.** zUu
29f0: 69 64 20 69 73 20 74 68 65 20 55 55 49 44 20 6f  id is the UUID o
2a00: 66 20 74 68 65 20 61 72 74 69 66 61 63 74 2c 20  f the artifact, 
2a10: 69 66 20 69 74 20 69 73 20 73 70 65 63 69 66 69  if it is specifi
2a20: 65 64 2e 20 20 57 68 65 6e 20 73 72 63 49 64 20  ed.  When srcId 
2a30: 69 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  is.** specified 
2a40: 74 68 65 6e 20 7a 55 75 69 64 20 6d 75 73 74 20  then zUuid must 
2a50: 61 6c 77 61 79 73 20 62 65 20 73 70 65 63 69 66  always be specif
2a60: 69 65 64 2e 20 20 49 66 20 73 72 63 49 64 20 69  ied.  If srcId i
2a70: 73 20 7a 65 72 6f 2c 0a 2a 2a 20 61 6e 64 20 7a  s zero,.** and z
2a80: 55 75 69 64 20 69 73 20 7a 65 72 6f 20 74 68 65  Uuid is zero the
2a90: 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 7a 55  n the correct zU
2aa0: 75 69 64 20 69 73 20 63 6f 6d 70 75 74 65 64 20  uid is computed 
2ab0: 66 72 6f 6d 20 70 42 6c 6f 62 2e 0a 2a 2a 0a 2a  from pBlob..**.*
2ac0: 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20  * If the record 
2ad0: 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 62  already exists b
2ae0: 75 74 20 69 73 20 61 20 70 68 61 6e 74 6f 6d 2c  ut is a phantom,
2af0: 20 74 68 65 20 70 42 6c 6f 62 20 63 6f 6e 74 65   the pBlob conte
2b00: 6e 74 0a 2a 2a 20 69 73 20 69 6e 73 65 72 74 65  nt.** is inserte
2b10: 64 20 61 6e 64 20 74 68 65 20 70 68 61 74 6f 6d  d and the phatom
2b20: 20 62 65 63 6f 6d 65 73 20 61 20 72 65 61 6c 20   becomes a real 
2b30: 72 65 63 6f 72 64 2e 0a 2a 2f 0a 69 6e 74 20 63  record..*/.int c
2b40: 6f 6e 74 65 6e 74 5f 70 75 74 28 42 6c 6f 62 20  ontent_put(Blob 
2b50: 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 63 68  *pBlob, const ch
2b60: 61 72 20 2a 7a 55 75 69 64 2c 20 69 6e 74 20 73  ar *zUuid, int s
2b70: 72 63 49 64 29 7b 0a 20 20 69 6e 74 20 73 69 7a  rcId){.  int siz
2b80: 65 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20 20  e;.  int rid;.  
2b90: 53 74 6d 74 20 73 31 3b 0a 20 20 42 6c 6f 62 20  Stmt s1;.  Blob 
2ba0: 63 6d 70 72 3b 0a 20 20 42 6c 6f 62 20 68 61 73  cmpr;.  Blob has
2bb0: 68 3b 0a 20 20 69 6e 74 20 6d 61 72 6b 41 73 55  h;.  int markAsU
2bc0: 6e 63 6c 75 73 74 65 72 65 64 20 3d 20 30 3b 0a  nclustered = 0;.
2bd0: 20 20 69 6e 74 20 69 73 44 65 70 68 61 6e 74 6f    int isDephanto
2be0: 6d 69 7a 65 20 3d 20 30 3b 0a 20 20 0a 20 20 61  mize = 0;.  .  a
2bf0: 73 73 65 72 74 28 20 67 2e 72 65 70 6f 73 69 74  ssert( g.reposit
2c00: 6f 72 79 4f 70 65 6e 20 29 3b 0a 20 20 61 73 73  oryOpen );.  ass
2c10: 65 72 74 28 20 70 42 6c 6f 62 21 3d 30 20 29 3b  ert( pBlob!=0 );
2c20: 0a 20 20 61 73 73 65 72 74 28 20 73 72 63 49 64  .  assert( srcId
2c30: 3d 3d 30 20 7c 7c 20 7a 55 75 69 64 21 3d 30 20  ==0 || zUuid!=0 
2c40: 29 3b 0a 20 20 69 66 28 20 7a 55 75 69 64 3d 3d  );.  if( zUuid==
2c50: 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
2c60: 20 70 42 6c 6f 62 21 3d 30 20 29 3b 0a 20 20 20   pBlob!=0 );.   
2c70: 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 70 42   sha1sum_blob(pB
2c80: 6c 6f 62 2c 20 26 68 61 73 68 29 3b 0a 20 20 7d  lob, &hash);.  }
2c90: 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f 69  else{.    blob_i
2ca0: 6e 69 74 28 26 68 61 73 68 2c 20 7a 55 75 69 64  nit(&hash, zUuid
2cb0: 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 69 7a  , -1);.  }.  siz
2cc0: 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28 70 42  e = blob_size(pB
2cd0: 6c 6f 62 29 3b 0a 20 20 64 62 5f 62 65 67 69 6e  lob);.  db_begin
2ce0: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a  _transaction();.
2cf0: 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73  .  /* Check to s
2d00: 65 65 20 69 66 20 74 68 65 20 65 6e 74 72 79 20  ee if the entry 
2d10: 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61  already exists a
2d20: 6e 64 20 69 66 20 69 74 20 64 6f 65 73 20 77 68  nd if it does wh
2d30: 65 74 68 65 72 0a 20 20 2a 2a 20 6f 72 20 6e 6f  ether.  ** or no
2d40: 74 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 61  t the entry is a
2d50: 20 70 68 61 6e 74 6f 6d 0a 20 20 2a 2f 0a 20 20   phantom.  */.  
2d60: 64 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20  db_prepare(&s1, 
2d70: 22 53 45 4c 45 43 54 20 72 69 64 2c 20 73 69 7a  "SELECT rid, siz
2d80: 65 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  e FROM blob WHER
2d90: 45 20 75 75 69 64 3d 25 42 22 2c 20 26 68 61 73  E uuid=%B", &has
2da0: 68 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65  h);.  if( db_ste
2db0: 70 28 26 73 31 29 3d 3d 53 51 4c 49 54 45 5f 52  p(&s1)==SQLITE_R
2dc0: 4f 57 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20  OW ){.    rid = 
2dd0: 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73  db_column_int(&s
2de0: 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64  1, 0);.    if( d
2df0: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 31  b_column_int(&s1
2e00: 2c 20 31 29 3e 3d 30 20 7c 7c 20 70 42 6c 6f 62  , 1)>=0 || pBlob
2e10: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
2e20: 45 69 74 68 65 72 20 74 68 65 20 65 6e 74 72 79  Either the entry
2e30: 20 69 73 20 6e 6f 74 20 61 20 70 68 61 6e 74 6f   is not a phanto
2e40: 6d 20 6f 72 20 69 74 20 69 73 20 61 20 70 68 61  m or it is a pha
2e50: 6e 74 6f 6d 20 62 75 74 20 77 65 0a 20 20 20 20  ntom but we.    
2e60: 20 20 2a 2a 20 68 61 76 65 20 6e 6f 20 64 61 74    ** have no dat
2e70: 61 20 77 69 74 68 20 77 68 69 63 68 20 74 6f 20  a with which to 
2e80: 64 65 70 68 61 6e 74 6f 6d 69 7a 65 20 69 74 2e  dephantomize it.
2e90: 20 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65    In either case
2ea0: 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65  ,.      ** there
2eb0: 20 69 73 20 6e 6f 74 68 69 6e 67 20 66 6f 72 20   is nothing for 
2ec0: 75 73 20 74 6f 20 64 6f 20 6f 74 68 65 72 20 74  us to do other t
2ed0: 68 61 6e 20 72 65 74 75 72 6e 20 74 68 65 20 52  han return the R
2ee0: 49 44 2e 20 2a 2f 0a 20 20 20 20 20 20 64 62 5f  ID. */.      db_
2ef0: 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20  finalize(&s1);. 
2f00: 20 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e       db_end_tran
2f10: 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20 20  saction(0);.    
2f20: 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 20 20    return rid;.  
2f30: 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
2f40: 20 72 69 64 20 3d 20 30 3b 20 20 2f 2a 20 4e 6f   rid = 0;  /* No
2f50: 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 65 20   entry with the 
2f60: 73 61 6d 65 20 55 55 49 44 20 63 75 72 72 65 6e  same UUID curren
2f70: 74 6c 79 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  tly exists */.  
2f80: 20 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74 65    markAsUncluste
2f90: 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 64  red = 1;.  }.  d
2fa0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b  b_finalize(&s1);
2fb0: 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74  ..  /* Construct
2fc0: 20 61 20 72 65 63 65 69 76 65 64 2d 66 72 6f 6d   a received-from
2fd0: 20 49 44 20 69 66 20 77 65 20 64 6f 20 6e 6f 74   ID if we do not
2fe0: 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 6f 6e   already have on
2ff0: 65 20 2a 2f 0a 20 20 69 66 28 20 67 2e 72 63 76  e */.  if( g.rcv
3000: 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f  id==0 ){.    db_
3010: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
3020: 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
3030: 72 63 76 66 72 6f 6d 28 75 69 64 2c 20 6d 74 69  rcvfrom(uid, mti
3040: 6d 65 2c 20 6e 6f 6e 63 65 2c 20 69 70 61 64 64  me, nonce, ipadd
3050: 72 29 22 0a 20 20 20 20 20 20 20 22 56 41 4c 55  r)".       "VALU
3060: 45 53 28 25 64 2c 20 6a 75 6c 69 61 6e 64 61 79  ES(%d, julianday
3070: 28 27 6e 6f 77 27 29 2c 20 25 51 2c 20 25 51 29  ('now'), %Q, %Q)
3080: 22 2c 0a 20 20 20 20 20 20 20 67 2e 75 73 65 72  ",.       g.user
3090: 55 69 64 2c 20 67 2e 7a 4e 6f 6e 63 65 2c 20 67  Uid, g.zNonce, g
30a0: 2e 7a 49 70 41 64 64 72 0a 20 20 20 20 29 3b 0a  .zIpAddr.    );.
30b0: 20 20 20 20 67 2e 72 63 76 69 64 20 3d 20 64 62      g.rcvid = db
30c0: 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
30d0: 69 64 28 29 3b 0a 20 20 7d 0a 0a 20 20 62 6c 6f  id();.  }..  blo
30e0: 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f 62  b_compress(pBlob
30f0: 2c 20 26 63 6d 70 72 29 3b 0a 20 20 69 66 28 20  , &cmpr);.  if( 
3100: 72 69 64 3e 30 20 29 7b 0a 20 20 20 20 2f 2a 20  rid>0 ){.    /* 
3110: 57 65 20 61 72 65 20 6a 75 73 74 20 61 64 64 69  We are just addi
3120: 6e 67 20 64 61 74 61 20 74 6f 20 61 20 70 68 61  ng data to a pha
3130: 6e 74 6f 6d 20 2a 2f 0a 20 20 20 20 64 62 5f 70  ntom */.    db_p
3140: 72 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20 20  repare(&s1,.    
3150: 20 20 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53    "UPDATE blob S
3160: 45 54 20 72 63 76 69 64 3d 25 64 2c 20 73 69 7a  ET rcvid=%d, siz
3170: 65 3d 25 64 2c 20 63 6f 6e 74 65 6e 74 3d 3a 64  e=%d, content=:d
3180: 61 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64  ata WHERE rid=%d
3190: 22 2c 0a 20 20 20 20 20 20 20 67 2e 72 63 76 69  ",.       g.rcvi
31a0: 64 2c 20 73 69 7a 65 2c 20 72 69 64 0a 20 20 20  d, size, rid.   
31b0: 20 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f   );.    db_bind_
31c0: 62 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61  blob(&s1, ":data
31d0: 22 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 64  ", &cmpr);.    d
31e0: 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20  b_exec(&s1);.   
31f0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
3200: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 68 61 6e  DELETE FROM phan
3210: 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d 25 64  tom WHERE rid=%d
3220: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69 66 28  ", rid);.    if(
3230: 20 73 72 63 49 64 3d 3d 30 20 7c 7c 20 63 6f 6e   srcId==0 || con
3240: 74 65 6e 74 5f 69 73 5f 61 76 61 69 6c 61 62 6c  tent_is_availabl
3250: 65 28 73 72 63 49 64 29 20 29 7b 0a 20 20 20 20  e(srcId) ){.    
3260: 20 20 69 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65    isDephantomize
3270: 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 6f 6e 74   = 1;.      cont
3280: 65 6e 74 5f 6d 61 72 6b 5f 61 76 61 69 6c 61 62  ent_mark_availab
3290: 6c 65 28 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20  le(rid);.    }. 
32a0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57   }else{.    /* W
32b0: 65 20 61 72 65 20 63 72 65 61 74 69 6e 67 20 61  e are creating a
32c0: 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20   new entry */.  
32d0: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31    db_prepare(&s1
32e0: 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 52 54 20  ,.      "INSERT 
32f0: 49 4e 54 4f 20 62 6c 6f 62 28 72 63 76 69 64 2c  INTO blob(rcvid,
3300: 73 69 7a 65 2c 75 75 69 64 2c 63 6f 6e 74 65 6e  size,uuid,conten
3310: 74 29 22 0a 20 20 20 20 20 20 22 56 41 4c 55 45  t)".      "VALUE
3320: 53 28 25 64 2c 25 64 2c 27 25 62 27 2c 3a 64 61  S(%d,%d,'%b',:da
3330: 74 61 29 22 2c 0a 20 20 20 20 20 20 20 67 2e 72  ta)",.       g.r
3340: 63 76 69 64 2c 20 73 69 7a 65 2c 20 26 68 61 73  cvid, size, &has
3350: 68 0a 20 20 20 20 29 3b 0a 20 20 20 20 64 62 5f  h.    );.    db_
3360: 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c 20 22  bind_blob(&s1, "
3370: 3a 64 61 74 61 22 2c 20 26 63 6d 70 72 29 3b 0a  :data", &cmpr);.
3380: 20 20 20 20 64 62 5f 65 78 65 63 28 26 73 31 29      db_exec(&s1)
3390: 3b 0a 20 20 20 20 72 69 64 20 3d 20 64 62 5f 6c  ;.    rid = db_l
33a0: 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
33b0: 28 29 3b 0a 20 20 20 20 69 66 28 20 21 70 42 6c  ();.    if( !pBl
33c0: 6f 62 20 29 7b 0a 20 20 20 20 20 20 64 62 5f 6d  ob ){.      db_m
33d0: 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52  ulti_exec("INSER
33e0: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f  T OR IGNORE INTO
33f0: 20 70 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53 28   phantom VALUES(
3400: 25 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 20 20  %d)", rid);.    
3410: 7d 0a 20 20 20 20 69 66 28 20 67 2e 6d 61 72 6b  }.    if( g.mark
3420: 50 72 69 76 61 74 65 20 29 7b 0a 20 20 20 20 20  Private ){.     
3430: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
3440: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 72 69 76  INSERT INTO priv
3450: 61 74 65 20 56 41 4c 55 45 53 28 25 64 29 22 2c  ate VALUES(%d)",
3460: 20 72 69 64 29 3b 0a 20 20 20 20 20 20 6d 61 72   rid);.      mar
3470: 6b 41 73 55 6e 63 6c 75 73 74 65 72 65 64 20 3d  kAsUnclustered =
3480: 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
3490: 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6d 70 72  blob_reset(&cmpr
34a0: 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
34b0: 73 72 63 49 64 20 69 73 20 73 70 65 63 69 66 69  srcId is specifi
34c0: 65 64 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74  ed, then the dat
34d0: 61 20 77 65 20 6a 75 73 74 20 61 64 64 65 64 20  a we just added 
34e0: 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c 79 20 61  is.  ** really a
34f0: 20 64 65 6c 74 61 2e 20 20 52 65 63 6f 72 64 20   delta.  Record 
3500: 74 68 69 73 20 66 61 63 74 20 69 6e 20 74 68 65  this fact in the
3510: 20 64 65 6c 74 61 20 74 61 62 6c 65 2e 0a 20 20   delta table..  
3520: 2a 2f 0a 20 20 69 66 28 20 73 72 63 49 64 20 29  */.  if( srcId )
3530: 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65  {.    db_multi_e
3540: 78 65 63 28 22 52 45 50 4c 41 43 45 20 49 4e 54  xec("REPLACE INT
3550: 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72 63 69  O delta(rid,srci
3560: 64 29 20 56 41 4c 55 45 53 28 25 64 2c 25 64 29  d) VALUES(%d,%d)
3570: 22 2c 20 72 69 64 2c 20 73 72 63 49 64 29 3b 0a  ", rid, srcId);.
3580: 20 20 7d 0a 20 20 69 66 28 20 21 69 73 44 65 70    }.  if( !isDep
3590: 68 61 6e 74 6f 6d 69 7a 65 20 26 26 20 62 61 67  hantomize && bag
35a0: 5f 66 69 6e 64 28 26 63 6f 6e 74 65 6e 74 43 61  _find(&contentCa
35b0: 63 68 65 2e 6d 69 73 73 69 6e 67 2c 20 72 69 64  che.missing, rid
35c0: 29 20 26 26 20 0a 20 20 20 20 20 20 28 73 72 63  ) && .      (src
35d0: 49 64 3d 3d 30 20 7c 7c 20 63 6f 6e 74 65 6e 74  Id==0 || content
35e0: 5f 69 73 5f 61 76 61 69 6c 61 62 6c 65 28 73 72  _is_available(sr
35f0: 63 49 64 29 29 20 29 7b 0a 20 20 20 20 63 6f 6e  cId)) ){.    con
3600: 74 65 6e 74 5f 6d 61 72 6b 5f 61 76 61 69 6c 61  tent_mark_availa
3610: 62 6c 65 28 72 69 64 29 3b 0a 20 20 7d 0a 20 20  ble(rid);.  }.  
3620: 69 66 28 20 69 73 44 65 70 68 61 6e 74 6f 6d 69  if( isDephantomi
3630: 7a 65 20 29 7b 0a 20 20 20 20 61 66 74 65 72 5f  ze ){.    after_
3640: 64 65 70 68 61 6e 74 6f 6d 69 7a 65 28 72 69 64  dephantomize(rid
3650: 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  , 0);.  }.  .  /
3660: 2a 20 41 64 64 20 74 68 65 20 65 6c 65 6d 65 6e  * Add the elemen
3670: 74 20 74 6f 20 74 68 65 20 75 6e 63 6c 75 73 74  t to the unclust
3680: 65 72 65 64 20 74 61 62 6c 65 20 69 66 20 68 61  ered table if ha
3690: 73 20 6e 65 76 65 72 20 62 65 65 6e 0a 20 20 2a  s never been.  *
36a0: 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 65  * previously see
36b0: 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 61  n..  */.  if( ma
36c0: 72 6b 41 73 55 6e 63 6c 75 73 74 65 72 65 64 20  rkAsUnclustered 
36d0: 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  ){.    db_multi_
36e0: 65 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20  exec("INSERT OR 
36f0: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 75 6e 63 6c  IGNORE INTO uncl
3700: 75 73 74 65 72 65 64 20 56 41 4c 55 45 53 28 25  ustered VALUES(%
3710: 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 0a  d)", rid);.  }..
3720: 20 20 2f 2a 20 46 69 6e 69 73 68 20 74 68 65 20    /* Finish the 
3730: 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20  transaction and 
3740: 63 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 64 62 5f  cleanup */.  db_
3750: 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20  finalize(&s1);. 
3760: 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74   db_end_transact
3770: 69 6f 6e 28 30 29 3b 0a 20 20 62 6c 6f 62 5f 72  ion(0);.  blob_r
3780: 65 73 65 74 28 26 68 61 73 68 29 3b 0a 0a 20 20  eset(&hash);..  
3790: 2f 2a 20 4d 61 6b 65 20 61 72 72 61 6e 67 65 6d  /* Make arrangem
37a0: 65 6e 74 73 20 74 6f 20 76 65 72 69 66 79 20 74  ents to verify t
37b0: 68 61 74 20 74 68 65 20 64 61 74 61 20 63 61 6e  hat the data can
37c0: 20 62 65 20 72 65 63 6f 76 65 72 65 64 0a 20 20   be recovered.  
37d0: 2a 2a 20 62 65 66 6f 72 65 20 77 65 20 63 6f 6d  ** before we com
37e0: 6d 69 74 20 2a 2f 0a 20 20 76 65 72 69 66 79 5f  mit */.  verify_
37f0: 62 65 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69  before_commit(ri
3800: 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 69 64  d);.  return rid
3810: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
3820: 65 20 61 20 6e 65 77 20 70 68 61 6e 74 6f 6d 20  e a new phantom 
3830: 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 55  with the given U
3840: 55 49 44 20 61 6e 64 20 72 65 74 75 72 6e 20 69  UID and return i
3850: 74 73 20 61 72 74 69 66 61 63 74 20 49 44 2e 0a  ts artifact ID..
3860: 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 6e  */.int content_n
3870: 65 77 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ew(const char *z
3880: 55 75 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64  Uuid){.  int rid
3890: 3b 0a 20 20 73 74 61 74 69 63 20 53 74 6d 74 20  ;.  static Stmt 
38a0: 73 31 2c 20 73 32 2c 20 73 33 3b 0a 20 20 0a 20  s1, s2, s3;.  . 
38b0: 20 61 73 73 65 72 74 28 20 67 2e 72 65 70 6f 73   assert( g.repos
38c0: 69 74 6f 72 79 4f 70 65 6e 20 29 3b 0a 20 20 64  itoryOpen );.  d
38d0: 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74  b_begin_transact
38e0: 69 6f 6e 28 29 3b 0a 20 20 69 66 28 20 75 75 69  ion();.  if( uui
38f0: 64 5f 69 73 5f 73 68 75 6e 6e 65 64 28 7a 55 75  d_is_shunned(zUu
3900: 69 64 29 20 29 7b 0a 20 20 20 20 64 62 5f 65 6e  id) ){.    db_en
3910: 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29  d_transaction(0)
3920: 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
3930: 20 20 7d 0a 20 20 64 62 5f 73 74 61 74 69 63 5f    }.  db_static_
3940: 70 72 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20  prepare(&s1,.   
3950: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 62 6c   "INSERT INTO bl
3960: 6f 62 28 72 63 76 69 64 2c 73 69 7a 65 2c 75 75  ob(rcvid,size,uu
3970: 69 64 2c 63 6f 6e 74 65 6e 74 29 22 0a 20 20 20  id,content)".   
3980: 20 22 56 41 4c 55 45 53 28 30 2c 2d 31 2c 3a 75   "VALUES(0,-1,:u
3990: 75 69 64 2c 4e 55 4c 4c 29 22 0a 20 20 29 3b 0a  uid,NULL)".  );.
39a0: 20 20 64 62 5f 62 69 6e 64 5f 74 65 78 74 28 26    db_bind_text(&
39b0: 73 31 2c 20 22 3a 75 75 69 64 22 2c 20 7a 55 75  s1, ":uuid", zUu
39c0: 69 64 29 3b 0a 20 20 64 62 5f 65 78 65 63 28 26  id);.  db_exec(&
39d0: 73 31 29 3b 0a 20 20 72 69 64 20 3d 20 64 62 5f  s1);.  rid = db_
39e0: 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
39f0: 64 28 29 3b 0a 20 20 64 62 5f 73 74 61 74 69 63  d();.  db_static
3a00: 5f 70 72 65 70 61 72 65 28 26 73 32 2c 0a 20 20  _prepare(&s2,.  
3a10: 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 70    "INSERT INTO p
3a20: 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53 28 3a 72  hantom VALUES(:r
3a30: 69 64 29 22 0a 20 20 29 3b 0a 20 20 64 62 5f 62  id)".  );.  db_b
3a40: 69 6e 64 5f 69 6e 74 28 26 73 32 2c 20 22 3a 72  ind_int(&s2, ":r
3a50: 69 64 22 2c 20 72 69 64 29 3b 0a 20 20 64 62 5f  id", rid);.  db_
3a60: 65 78 65 63 28 26 73 32 29 3b 0a 20 20 69 66 28  exec(&s2);.  if(
3a70: 20 67 2e 6d 61 72 6b 50 72 69 76 61 74 65 20 29   g.markPrivate )
3a80: 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65  {.    db_multi_e
3a90: 78 65 63 28 22 49 4e 53 45 52 54 20 49 4e 54 4f  xec("INSERT INTO
3aa0: 20 70 72 69 76 61 74 65 20 56 41 4c 55 45 53 28   private VALUES(
3ab0: 25 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 7d 65  %d)", rid);.  }e
3ac0: 6c 73 65 7b 0a 20 20 20 20 64 62 5f 73 74 61 74  lse{.    db_stat
3ad0: 69 63 5f 70 72 65 70 61 72 65 28 26 73 33 2c 0a  ic_prepare(&s3,.
3ae0: 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e        "INSERT IN
3af0: 54 4f 20 75 6e 63 6c 75 73 74 65 72 65 64 20 56  TO unclustered V
3b00: 41 4c 55 45 53 28 3a 72 69 64 29 22 0a 20 20 20  ALUES(:rid)".   
3b10: 20 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f   );.    db_bind_
3b20: 69 6e 74 28 26 73 33 2c 20 22 3a 72 69 64 22 2c  int(&s3, ":rid",
3b30: 20 72 69 64 29 3b 0a 20 20 20 20 64 62 5f 65 78   rid);.    db_ex
3b40: 65 63 28 26 73 33 29 3b 0a 20 20 7d 0a 20 20 62  ec(&s3);.  }.  b
3b50: 61 67 5f 69 6e 73 65 72 74 28 26 63 6f 6e 74 65  ag_insert(&conte
3b60: 6e 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67 2c  ntCache.missing,
3b70: 20 72 69 64 29 3b 0a 20 20 64 62 5f 65 6e 64 5f   rid);.  db_end_
3b80: 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a  transaction(0);.
3b90: 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a    return rid;.}.
3ba0: 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a  ../*.** COMMAND:
3bb0: 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 70    test-content-p
3bc0: 75 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74  ut.**.** Extract
3bd0: 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65   a blob from the
3be0: 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 72   database and wr
3bf0: 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69  ite it into a fi
3c00: 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74  le..*/.void test
3c10: 5f 63 6f 6e 74 65 6e 74 5f 70 75 74 5f 63 6d 64  _content_put_cmd
3c20: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69  (void){.  int ri
3c30: 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e  d;.  Blob conten
3c40: 74 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21  t;.  if( g.argc!
3c50: 3d 33 20 29 20 75 73 61 67 65 28 22 46 49 4c 45  =3 ) usage("FILE
3c60: 4e 41 4d 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73  NAME");.  db_mus
3c70: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65  t_be_within_tree
3c80: 28 29 3b 0a 20 20 75 73 65 72 5f 73 65 6c 65 63  ();.  user_selec
3c90: 74 28 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61 64  t();.  blob_read
3ca0: 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 63 6f 6e 74  _from_file(&cont
3cb0: 65 6e 74 2c 20 67 2e 61 72 67 76 5b 32 5d 29 3b  ent, g.argv[2]);
3cc0: 0a 20 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e 74  .  rid = content
3cd0: 5f 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 30  _put(&content, 0
3ce0: 2c 20 30 29 3b 0a 20 20 70 72 69 6e 74 66 28 22  , 0);.  printf("
3cf0: 69 6e 73 65 72 74 65 64 20 61 73 20 72 65 63 6f  inserted as reco
3d00: 72 64 20 25 64 5c 6e 22 2c 20 72 69 64 29 3b 0a  rd %d\n", rid);.
3d10: 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
3d20: 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61  re the content a
3d30: 74 20 72 69 64 20 69 73 20 74 68 65 20 6f 72 69  t rid is the ori
3d40: 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 61 6e  ginal content an
3d50: 64 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 64 65  d is not a.** de
3d60: 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f 6e  lta..*/.void con
3d70: 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 69 6e 74  tent_undelta(int
3d80: 20 72 69 64 29 7b 0a 20 20 69 66 28 20 66 69 6e   rid){.  if( fin
3d90: 64 53 72 63 69 64 28 72 69 64 29 3e 30 20 29 7b  dSrcid(rid)>0 ){
3da0: 0a 20 20 20 20 42 6c 6f 62 20 78 3b 0a 20 20 20  .    Blob x;.   
3db0: 20 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74   if( content_get
3dc0: 28 72 69 64 2c 20 26 78 29 20 29 7b 0a 20 20 20  (rid, &x) ){.   
3dd0: 20 20 20 53 74 6d 74 20 73 3b 0a 20 20 20 20 20     Stmt s;.     
3de0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 2c 20   db_prepare(&s, 
3df0: 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54  "UPDATE blob SET
3e00: 20 63 6f 6e 74 65 6e 74 3d 3a 63 2c 20 73 69 7a   content=:c, siz
3e10: 65 3d 25 64 20 57 48 45 52 45 20 72 69 64 3d 25  e=%d WHERE rid=%
3e20: 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d",.            
3e30: 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 69           blob_si
3e40: 7a 65 28 26 78 29 2c 20 72 69 64 29 3b 0a 20 20  ze(&x), rid);.  
3e50: 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73      blob_compres
3e60: 73 28 26 78 2c 20 26 78 29 3b 0a 20 20 20 20 20  s(&x, &x);.     
3e70: 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73   db_bind_blob(&s
3e80: 2c 20 22 3a 63 22 2c 20 26 78 29 3b 0a 20 20 20  , ":c", &x);.   
3e90: 20 20 20 64 62 5f 65 78 65 63 28 26 73 29 3b 0a     db_exec(&s);.
3ea0: 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a        db_finaliz
3eb0: 65 28 26 73 29 3b 0a 20 20 20 20 20 20 62 6c 6f  e(&s);.      blo
3ec0: 62 5f 72 65 73 65 74 28 26 78 29 3b 0a 20 20 20  b_reset(&x);.   
3ed0: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
3ee0: 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 64 65  ("DELETE FROM de
3ef0: 6c 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64  lta WHERE rid=%d
3f00: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20  ", rid);.    }. 
3f10: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d   }.}../*.** COMM
3f20: 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65  AND:  test-conte
3f30: 6e 74 2d 75 6e 64 65 6c 74 61 0a 2a 2a 0a 2a 2a  nt-undelta.**.**
3f40: 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63   Make sure the c
3f50: 6f 6e 74 65 6e 74 20 61 74 20 52 45 43 4f 52 44  ontent at RECORD
3f60: 49 44 20 69 73 20 6e 6f 74 20 61 20 64 65 6c 74  ID is not a delt
3f70: 61 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63  a.*/.void test_c
3f80: 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 5f 63  ontent_undelta_c
3f90: 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20  md(void){.  int 
3fa0: 72 69 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67  rid;.  if( g.arg
3fb0: 63 21 3d 32 20 29 20 75 73 61 67 65 28 22 52 45  c!=2 ) usage("RE
3fc0: 43 4f 52 44 49 44 22 29 3b 0a 20 20 64 62 5f 6d  CORDID");.  db_m
3fd0: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
3fe0: 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 61 74  ee();.  rid = at
3ff0: 6f 69 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20  oi(g.argv[2]);. 
4000: 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61   content_undelta
4010: 28 72 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (rid);.}../*.** 
4020: 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
4030: 68 65 20 67 69 76 65 6e 20 52 49 44 20 69 73 20  he given RID is 
4040: 6d 61 72 6b 65 64 20 61 73 20 50 52 49 56 41 54  marked as PRIVAT
4050: 45 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 65 6e  E..*/.int conten
4060: 74 5f 69 73 5f 70 72 69 76 61 74 65 28 69 6e 74  t_is_private(int
4070: 20 72 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20   rid){.  static 
4080: 53 74 6d 74 20 73 31 3b 0a 20 20 69 6e 74 20 72  Stmt s1;.  int r
4090: 63 3b 0a 20 20 64 62 5f 73 74 61 74 69 63 5f 70  c;.  db_static_p
40a0: 72 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20 20  repare(&s1,.    
40b0: 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 70  "SELECT 1 FROM p
40c0: 72 69 76 61 74 65 20 57 48 45 52 45 20 72 69 64  rivate WHERE rid
40d0: 3d 3a 72 69 64 22 0a 20 20 29 3b 0a 20 20 64 62  =:rid".  );.  db
40e0: 5f 62 69 6e 64 5f 69 6e 74 28 26 73 31 2c 20 22  _bind_int(&s1, "
40f0: 3a 72 69 64 22 2c 20 72 69 64 29 3b 0a 20 20 72  :rid", rid);.  r
4100: 63 20 3d 20 64 62 5f 73 74 65 70 28 26 73 31 29  c = db_step(&s1)
4110: 3b 0a 20 20 64 62 5f 72 65 73 65 74 28 26 73 31  ;.  db_reset(&s1
4120: 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3d 3d  );.  return rc==
4130: 53 51 4c 49 54 45 5f 52 4f 57 3b 20 20 0a 7d 0a  SQLITE_ROW;  .}.
4140: 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
4150: 20 61 6e 20 61 72 74 69 66 61 63 74 20 69 73 20   an artifact is 
4160: 70 75 62 6c 69 63 2e 20 20 0a 2a 2f 0a 76 6f 69  public.  .*/.voi
4170: 64 20 63 6f 6e 74 65 6e 74 5f 6d 61 6b 65 5f 70  d content_make_p
4180: 75 62 6c 69 63 28 69 6e 74 20 72 69 64 29 7b 0a  ublic(int rid){.
4190: 20 20 73 74 61 74 69 63 20 53 74 6d 74 20 73 31    static Stmt s1
41a0: 3b 0a 20 20 64 62 5f 73 74 61 74 69 63 5f 70 72  ;.  db_static_pr
41b0: 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20 20 22  epare(&s1,.    "
41c0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 72 69 76  DELETE FROM priv
41d0: 61 74 65 20 57 48 45 52 45 20 72 69 64 3d 3a 72  ate WHERE rid=:r
41e0: 69 64 22 0a 20 20 29 3b 0a 20 20 64 62 5f 62 69  id".  );.  db_bi
41f0: 6e 64 5f 69 6e 74 28 26 73 31 2c 20 22 3a 72 69  nd_int(&s1, ":ri
4200: 64 22 2c 20 72 69 64 29 3b 0a 20 20 64 62 5f 65  d", rid);.  db_e
4210: 78 65 63 28 26 73 31 29 3b 0a 7d 0a 0a 2f 2a 0a  xec(&s1);.}../*.
4220: 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 74  ** Change the st
4230: 6f 72 61 67 65 20 6f 66 20 72 69 64 20 73 6f 20  orage of rid so 
4240: 74 68 61 74 20 69 74 20 69 73 20 61 20 64 65 6c  that it is a del
4250: 74 61 20 6f 66 20 73 72 63 69 64 2e 0a 2a 2a 0a  ta of srcid..**.
4260: 2a 2a 20 49 66 20 72 69 64 20 69 73 20 61 6c 72  ** If rid is alr
4270: 65 61 64 79 20 61 20 64 65 6c 74 61 20 66 72 6f  eady a delta fro
4280: 6d 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 6c 61  m some other pla
4290: 63 65 20 74 68 65 6e 20 6e 6f 0a 2a 2a 20 63 6f  ce then no.** co
42a0: 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 20  nversion occurs 
42b0: 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 6e 6f  and this is a no
42c0: 2d 6f 70 20 75 6e 6c 65 73 73 20 66 6f 72 63 65  -op unless force
42d0: 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 65 72  ==1..**.** Never
42e0: 20 67 65 6e 65 72 61 74 65 20 61 20 64 65 6c 74   generate a delt
42f0: 61 20 74 68 61 74 20 63 61 72 72 69 65 73 20 61  a that carries a
4300: 20 70 72 69 76 61 74 65 20 61 72 74 69 66 61 63   private artifac
4310: 74 20 69 6e 74 6f 20 61 20 70 75 62 6c 69 63 0a  t into a public.
4320: 2a 2a 20 61 72 74 69 66 61 63 74 2e 20 20 4f 74  ** artifact.  Ot
4330: 68 65 72 77 69 73 65 2c 20 77 68 65 6e 20 77 65  herwise, when we
4340: 20 67 6f 20 74 6f 20 73 65 6e 64 20 74 68 65 20   go to send the 
4350: 70 75 62 6c 69 63 20 61 72 74 69 66 61 63 74 20  public artifact 
4360: 6f 6e 20 61 0a 2a 2a 20 73 79 6e 63 20 6f 70 65  on a.** sync ope
4370: 72 61 74 69 6f 6e 2c 20 74 68 65 20 6f 74 68 65  ration, the othe
4380: 72 20 65 6e 64 20 6f 66 20 74 68 65 20 73 79 6e  r end of the syn
4390: 63 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  c will never be 
43a0: 61 62 6c 65 20 74 6f 20 72 65 63 65 69 76 65 0a  able to receive.
43b0: 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 6f 66  ** the source of
43c0: 20 74 68 65 20 64 65 6c 74 61 2e 20 20 49 74 20   the delta.  It 
43d0: 69 73 20 4f 4b 20 74 6f 20 64 65 6c 74 61 20 70  is OK to delta p
43e0: 72 69 76 61 74 65 2d 3e 70 72 69 76 61 74 65 20  rivate->private 
43f0: 61 6e 64 0a 2a 2a 20 70 75 62 6c 69 63 2d 3e 70  and.** public->p
4400: 72 69 76 61 74 65 20 61 6e 64 20 70 75 62 6c 69  rivate and publi
4410: 63 2d 3e 70 75 62 6c 69 63 2e 20 20 4a 75 73 74  c->public.  Just
4420: 20 6e 6f 20 70 72 69 76 61 74 65 2d 3e 70 75 62   no private->pub
4430: 6c 69 63 20 64 65 6c 74 61 2e 0a 2a 2a 0a 2a 2a  lic delta..**.**
4440: 20 49 66 20 73 72 63 69 64 20 69 73 20 61 20 64   If srcid is a d
4450: 65 6c 74 61 20 74 68 61 74 20 64 65 70 65 6e 64  elta that depend
4460: 73 20 6f 6e 20 72 69 64 2c 20 74 68 65 6e 20 73  s on rid, then s
4470: 72 63 69 64 20 69 73 0a 2a 2a 20 63 6f 6e 76 65  rcid is.** conve
4480: 72 74 65 64 20 74 6f 20 75 6e 64 65 6c 74 61 65  rted to undeltae
4490: 64 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  d text..**.** If
44a0: 20 65 69 74 68 65 72 20 72 69 64 20 6f 72 20 73   either rid or s
44b0: 72 63 69 64 20 63 6f 6e 74 61 69 6e 20 6c 65 73  rcid contain les
44c0: 73 20 74 68 61 6e 20 35 30 20 62 79 74 65 73 2c  s than 50 bytes,
44d0: 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 72 65   or if the.** re
44e0: 73 75 6c 74 69 6e 67 20 64 65 6c 74 61 20 64 6f  sulting delta do
44f0: 65 73 20 6e 6f 74 20 61 63 68 69 65 76 65 20 61  es not achieve a
4500: 20 63 6f 6d 70 72 65 73 73 69 6f 6e 20 6f 66 20   compression of 
4510: 61 74 20 6c 65 61 73 74 20 32 35 25 20 6f 6e 0a  at least 25% on.
4520: 2a 2a 20 69 74 73 20 6f 77 6e 20 74 68 65 20 72  ** its own the r
4530: 69 64 20 69 73 20 6c 65 66 74 20 75 6e 74 6f 75  id is left untou
4540: 63 68 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f  ched..*/.void co
4550: 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 28 69 6e  ntent_deltify(in
4560: 74 20 72 69 64 2c 20 69 6e 74 20 73 72 63 69 64  t rid, int srcid
4570: 2c 20 69 6e 74 20 66 6f 72 63 65 29 7b 0a 20 20  , int force){.  
4580: 69 6e 74 20 73 3b 0a 20 20 42 6c 6f 62 20 64 61  int s;.  Blob da
4590: 74 61 2c 20 73 72 63 2c 20 64 65 6c 74 61 3b 0a  ta, src, delta;.
45a0: 20 20 53 74 6d 74 20 73 31 2c 20 73 32 3b 0a 20    Stmt s1, s2;. 
45b0: 20 69 66 28 20 73 72 63 69 64 3d 3d 72 69 64 20   if( srcid==rid 
45c0: 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
45d0: 21 66 6f 72 63 65 20 26 26 20 66 69 6e 64 53 72  !force && findSr
45e0: 63 69 64 28 72 69 64 29 3e 30 20 29 20 72 65 74  cid(rid)>0 ) ret
45f0: 75 72 6e 3b 0a 20 20 69 66 28 20 63 6f 6e 74 65  urn;.  if( conte
4600: 6e 74 5f 69 73 5f 70 72 69 76 61 74 65 28 73 72  nt_is_private(sr
4610: 63 69 64 29 20 26 26 20 21 63 6f 6e 74 65 6e 74  cid) && !content
4620: 5f 69 73 5f 70 72 69 76 61 74 65 28 72 69 64 29  _is_private(rid)
4630: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
4640: 20 20 7d 0a 20 20 73 20 3d 20 73 72 63 69 64 3b    }.  s = srcid;
4650: 0a 20 20 77 68 69 6c 65 28 20 28 73 20 3d 20 66  .  while( (s = f
4660: 69 6e 64 53 72 63 69 64 28 73 29 29 3e 30 20 29  indSrcid(s))>0 )
4670: 7b 0a 20 20 20 20 69 66 28 20 73 3d 3d 72 69 64  {.    if( s==rid
4680: 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e   ){.      conten
4690: 74 5f 75 6e 64 65 6c 74 61 28 73 72 63 69 64 29  t_undelta(srcid)
46a0: 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
46b0: 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 74 65     }.  }.  conte
46c0: 6e 74 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73  nt_get(srcid, &s
46d0: 72 63 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62 5f  rc);.  if( blob_
46e0: 73 69 7a 65 28 26 73 72 63 29 3c 35 30 20 29 7b  size(&src)<50 ){
46f0: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  .    blob_reset(
4700: 26 73 72 63 29 3b 0a 20 20 20 20 72 65 74 75 72  &src);.    retur
4710: 6e 3b 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 74  n;.  }.  content
4720: 5f 67 65 74 28 72 69 64 2c 20 26 64 61 74 61 29  _get(rid, &data)
4730: 3b 0a 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a  ;.  if( blob_siz
4740: 65 28 26 64 61 74 61 29 3c 35 30 20 29 7b 0a 20  e(&data)<50 ){. 
4750: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73     blob_reset(&s
4760: 72 63 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65  rc);.    blob_re
4770: 73 65 74 28 26 64 61 74 61 29 3b 0a 20 20 20 20  set(&data);.    
4780: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c  return;.  }.  bl
4790: 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61 74 65 28  ob_delta_create(
47a0: 26 73 72 63 2c 20 26 64 61 74 61 2c 20 26 64 65  &src, &data, &de
47b0: 6c 74 61 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62  lta);.  if( blob
47c0: 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 20 3c 20  _size(&delta) < 
47d0: 62 6c 6f 62 5f 73 69 7a 65 28 26 64 61 74 61 29  blob_size(&data)
47e0: 2a 30 2e 37 35 20 29 7b 0a 20 20 20 20 62 6c 6f  *0.75 ){.    blo
47f0: 62 5f 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74  b_compress(&delt
4800: 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  a, &delta);.    
4810: 64 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20  db_prepare(&s1, 
4820: 22 55 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54  "UPDATE blob SET
4830: 20 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57   content=:data W
4840: 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69  HERE rid=%d", ri
4850: 64 29 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61  d);.    db_prepa
4860: 72 65 28 26 73 32 2c 20 22 52 45 50 4c 41 43 45  re(&s2, "REPLACE
4870: 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69 64 2c   INTO delta(rid,
4880: 73 72 63 69 64 29 56 41 4c 55 45 53 28 25 64 2c  srcid)VALUES(%d,
4890: 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63 69 64  %d)", rid, srcid
48a0: 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 62  );.    db_bind_b
48b0: 6c 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61 22  lob(&s1, ":data"
48c0: 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 64  , &delta);.    d
48d0: 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74  b_begin_transact
48e0: 69 6f 6e 28 29 3b 0a 20 20 20 20 64 62 5f 65 78  ion();.    db_ex
48f0: 65 63 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f  ec(&s1);.    db_
4900: 65 78 65 63 28 26 73 32 29 3b 0a 20 20 20 20 64  exec(&s2);.    d
4910: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
4920: 6e 28 30 29 3b 0a 20 20 20 20 64 62 5f 66 69 6e  n(0);.    db_fin
4930: 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 20 20 20  alize(&s1);.    
4940: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 32 29  db_finalize(&s2)
4950: 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 65 66  ;.    verify_bef
4960: 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b  ore_commit(rid);
4970: 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65  .  }.  blob_rese
4980: 74 28 26 73 72 63 29 3b 0a 20 20 62 6c 6f 62 5f  t(&src);.  blob_
4990: 72 65 73 65 74 28 26 64 61 74 61 29 3b 0a 20 20  reset(&data);.  
49a0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c 74  blob_reset(&delt
49b0: 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d  a);.}../*.** COM
49c0: 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74  MAND:  test-cont
49d0: 65 6e 74 2d 64 65 6c 74 69 66 79 0a 2a 2a 0a 2a  ent-deltify.**.*
49e0: 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 63 6f  * Convert the co
49f0: 6e 74 65 6e 74 20 61 74 20 52 49 44 20 69 6e 74  ntent at RID int
4a00: 6f 20 61 20 64 65 6c 74 61 20 66 72 6f 6d 20 53  o a delta from S
4a10: 52 43 49 44 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65  RCID..*/.void te
4a20: 73 74 5f 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69  st_content_delti
4a30: 66 79 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20  fy_cmd(void){.  
4a40: 69 66 28 20 67 2e 61 72 67 63 21 3d 35 20 29 20  if( g.argc!=5 ) 
4a50: 75 73 61 67 65 28 22 52 49 44 20 53 52 43 49 44  usage("RID SRCID
4a60: 20 46 4f 52 43 45 22 29 3b 0a 20 20 64 62 5f 6d   FORCE");.  db_m
4a70: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
4a80: 65 65 28 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f  ee();.  content_
4a90: 64 65 6c 74 69 66 79 28 61 74 6f 69 28 67 2e 61  deltify(atoi(g.a
4aa0: 72 67 76 5b 32 5d 29 2c 20 61 74 6f 69 28 67 2e  rgv[2]), atoi(g.
4ab0: 61 72 67 76 5b 33 5d 29 2c 20 61 74 6f 69 28 67  argv[3]), atoi(g
4ac0: 2e 61 72 67 76 5b 34 5d 29 29 3b 0a 7d 0a        .argv[4]));.}.