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 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d : test-content-
2270: 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 get.**.** Extrac
2280: 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 t a blob from th
2290: 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 e database and w
22a0: 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 rite it into a f
22b0: 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 ile..*/.void tes
22c0: 74 5f 63 6f 6e 74 65 6e 74 5f 67 65 74 5f 63 6d t_content_get_cm
22d0: 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 d(void){. int r
22e0: 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 id;. Blob conte
22f0: 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 nt;. const char
2300: 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 *zFile;. if( g
2310: 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e 61 72 .argc!=4 && g.ar
2320: 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 22 52 gc!=3 ) usage("R
2330: 45 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d ECORDID ?FILENAM
2340: 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 E?");. zFile =
2350: 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 g.argc==4 ? g.ar
2360: 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 gv[3] : "-";. d
2370: 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e b_must_be_within
2380: 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64 20 3d _tree();. rid =
2390: 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 name_to_rid(g.a
23a0: 72 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 rgv[2]);. conte
23b0: 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e nt_get(rid, &con
23c0: 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72 tent);. blob_wr
23d0: 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e ite_to_file(&con
23e0: 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a tent, zFile);.}.
23f0: 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 ./*.** COMMAND:
2400: 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 72 61 test-content-ra
2410: 77 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 wget.**.** Extra
2420: 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 ct a blob from t
2430: 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 he database and
2440: 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 write it into a
2450: 66 69 6c 65 2e 20 20 54 68 69 73 0a 2a 2a 20 76 file. This.** v
2460: 65 72 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 ersion does not
2470: 65 78 70 61 6e 64 20 74 68 65 20 64 65 6c 74 61 expand the delta
2480: 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 ..*/.void test_c
2490: 6f 6e 74 65 6e 74 5f 72 61 77 67 65 74 5f 63 6d ontent_rawget_cm
24a0: 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 d(void){. int r
24b0: 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 id;. Blob conte
24c0: 6e 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 nt;. const char
24d0: 20 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 *zFile;. if( g
24e0: 2e 61 72 67 63 21 3d 34 20 26 26 20 67 2e 61 72 .argc!=4 && g.ar
24f0: 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 22 52 gc!=3 ) usage("R
2500: 45 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d ECORDID ?FILENAM
2510: 45 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 E?");. zFile =
2520: 67 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 g.argc==4 ? g.ar
2530: 67 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 gv[3] : "-";. d
2540: 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e b_must_be_within
2550: 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64 20 3d _tree();. rid =
2560: 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 name_to_rid(g.a
2570: 72 67 76 5b 32 5d 29 3b 0a 20 20 62 6c 6f 62 5f rgv[2]);. blob_
2580: 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a zero(&content);.
2590: 20 20 64 62 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 db_blob(&conte
25a0: 6e 74 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 nt, "SELECT cont
25b0: 65 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 ent FROM blob WH
25c0: 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64 ERE rid=%d", rid
25d0: 29 3b 0a 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 );. blob_uncomp
25e0: 72 65 73 73 28 26 63 6f 6e 74 65 6e 74 2c 20 26 ress(&content, &
25f0: 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 content);. blob
2600: 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 _write_to_file(&
2610: 63 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b content, zFile);
2620: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 .}../*.** When a
2630: 20 72 65 63 6f 72 64 20 69 73 20 63 6f 6e 76 65 record is conve
2640: 72 74 65 64 20 66 72 6f 6d 20 61 20 70 68 61 6e rted from a phan
2650: 74 6f 6d 20 74 6f 20 61 20 72 65 61 6c 20 72 65 tom to a real re
2660: 63 6f 72 64 2c 0a 2a 2a 20 69 66 20 74 68 61 74 cord,.** if that
2670: 20 72 65 63 6f 72 64 20 68 61 73 20 6f 74 68 65 record has othe
2680: 72 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 61 r records that a
2690: 72 65 20 64 65 72 69 76 65 64 20 62 79 20 64 65 re derived by de
26a0: 6c 74 61 2c 0a 2a 2a 20 74 68 65 6e 20 63 61 6c lta,.** then cal
26b0: 6c 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f 73 73 l manifest_cross
26c0: 6c 69 6e 6b 28 29 20 6f 6e 20 74 68 6f 73 65 20 link() on those
26d0: 6f 74 68 65 72 20 72 65 63 6f 72 64 73 2e 0a 2a other records..*
26e0: 2f 0a 76 6f 69 64 20 61 66 74 65 72 5f 64 65 70 /.void after_dep
26f0: 68 61 6e 74 6f 6d 69 7a 65 28 69 6e 74 20 72 69 hantomize(int ri
2700: 64 2c 20 69 6e 74 20 6c 69 6e 6b 46 6c 61 67 29 d, int linkFlag)
2710: 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 64 62 {. Stmt q;. db
2720: 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 _prepare(&q, "SE
2730: 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 64 65 LECT rid FROM de
2740: 6c 74 61 20 57 48 45 52 45 20 73 72 63 69 64 3d lta WHERE srcid=
2750: 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 77 68 69 %d", rid);. whi
2760: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d le( db_step(&q)=
2770: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 =SQLITE_ROW ){.
2780: 20 20 20 69 6e 74 20 74 69 64 20 3d 20 64 62 5f int tid = db_
2790: 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 column_int(&q, 0
27a0: 29 3b 0a 20 20 20 20 61 66 74 65 72 5f 64 65 70 );. after_dep
27b0: 68 61 6e 74 6f 6d 69 7a 65 28 74 69 64 2c 20 31 hantomize(tid, 1
27c0: 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 );. }. db_fina
27d0: 6c 69 7a 65 28 26 71 29 3b 0a 20 20 69 66 28 20 lize(&q);. if(
27e0: 6c 69 6e 6b 46 6c 61 67 20 29 7b 0a 20 20 20 20 linkFlag ){.
27f0: 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a 20 20 Blob content;.
2800: 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 content_get(ri
2810: 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 d, &content);.
2820: 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f 73 73 manifest_cross
2830: 6c 69 6e 6b 28 72 69 64 2c 20 26 63 6f 6e 74 65 link(rid, &conte
2840: 6e 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 nt);. blob_re
2850: 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 set(&content);.
2860: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 }.}../*.** Writ
2870: 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 e content into t
2880: 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 52 65 he database. Re
2890: 74 75 72 6e 20 74 68 65 20 72 65 63 6f 72 64 20 turn the record
28a0: 49 44 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 ID. If the.** c
28b0: 6f 6e 74 65 6e 74 20 69 73 20 61 6c 72 65 61 64 ontent is alread
28c0: 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
28d0: 65 2c 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 e, just return t
28e0: 68 65 20 72 65 63 6f 72 64 20 49 44 2e 0a 2a 2a he record ID..**
28f0: 0a 2a 2a 20 49 66 20 73 72 63 49 64 20 69 73 20 .** If srcId is
2900: 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 specified, then
2910: 70 42 6c 6f 62 20 69 73 20 64 65 6c 74 61 20 63 pBlob is delta c
2920: 6f 6e 74 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 ontent from.** t
2930: 68 65 20 73 72 63 49 64 20 72 65 63 6f 72 64 2e he srcId record.
2940: 20 20 73 72 63 49 64 20 6d 69 67 68 74 20 62 65 srcId might be
2950: 20 61 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2a 0a 2a a phantom..**.*
2960: 2a 20 7a 55 75 69 64 20 69 73 20 74 68 65 20 55 * zUuid is the U
2970: 55 49 44 20 6f 66 20 74 68 65 20 61 72 74 69 66 UID of the artif
2980: 61 63 74 2c 20 69 66 20 69 74 20 69 73 20 73 70 act, if it is sp
2990: 65 63 69 66 69 65 64 2e 20 20 57 68 65 6e 20 73 ecified. When s
29a0: 72 63 49 64 20 69 73 0a 2a 2a 20 73 70 65 63 69 rcId is.** speci
29b0: 66 69 65 64 20 74 68 65 6e 20 7a 55 75 69 64 20 fied then zUuid
29c0: 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 20 73 must always be s
29d0: 70 65 63 69 66 69 65 64 2e 20 20 49 66 20 73 72 pecified. If sr
29e0: 63 49 64 20 69 73 20 7a 65 72 6f 2c 0a 2a 2a 20 cId is zero,.**
29f0: 61 6e 64 20 7a 55 75 69 64 20 69 73 20 7a 65 72 and zUuid is zer
2a00: 6f 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65 o then the corre
2a10: 63 74 20 7a 55 75 69 64 20 69 73 20 63 6f 6d 70 ct zUuid is comp
2a20: 75 74 65 64 20 66 72 6f 6d 20 70 42 6c 6f 62 2e uted from pBlob.
2a30: 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 .**.** If the re
2a40: 63 6f 72 64 20 61 6c 72 65 61 64 79 20 65 78 69 cord already exi
2a50: 73 74 73 20 62 75 74 20 69 73 20 61 20 70 68 61 sts but is a pha
2a60: 6e 74 6f 6d 2c 20 74 68 65 20 70 42 6c 6f 62 20 ntom, the pBlob
2a70: 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 69 6e content.** is in
2a80: 73 65 72 74 65 64 20 61 6e 64 20 74 68 65 20 70 serted and the p
2a90: 68 61 74 6f 6d 20 62 65 63 6f 6d 65 73 20 61 20 hatom becomes a
2aa0: 72 65 61 6c 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a real record..*/.
2ab0: 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 int content_put(
2ac0: 42 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e Blob *pBlob, con
2ad0: 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 2c 20 st char *zUuid,
2ae0: 69 6e 74 20 73 72 63 49 64 29 7b 0a 20 20 69 6e int srcId){. in
2af0: 74 20 73 69 7a 65 3b 0a 20 20 69 6e 74 20 72 69 t size;. int ri
2b00: 64 3b 0a 20 20 53 74 6d 74 20 73 31 3b 0a 20 20 d;. Stmt s1;.
2b10: 42 6c 6f 62 20 63 6d 70 72 3b 0a 20 20 42 6c 6f Blob cmpr;. Blo
2b20: 62 20 68 61 73 68 3b 0a 20 20 69 6e 74 20 6d 61 b hash;. int ma
2b30: 72 6b 41 73 55 6e 63 6c 75 73 74 65 72 65 64 20 rkAsUnclustered
2b40: 3d 20 30 3b 0a 20 20 69 6e 74 20 69 73 44 65 70 = 0;. int isDep
2b50: 68 61 6e 74 6f 6d 69 7a 65 20 3d 20 30 3b 0a 20 hantomize = 0;.
2b60: 20 0a 20 20 61 73 73 65 72 74 28 20 67 2e 72 65 . assert( g.re
2b70: 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29 3b 0a positoryOpen );.
2b80: 20 20 61 73 73 65 72 74 28 20 70 42 6c 6f 62 21 assert( pBlob!
2b90: 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
2ba0: 73 72 63 49 64 3d 3d 30 20 7c 7c 20 7a 55 75 69 srcId==0 || zUui
2bb0: 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a 55 d!=0 );. if( zU
2bc0: 75 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 uid==0 ){. as
2bd0: 73 65 72 74 28 20 70 42 6c 6f 62 21 3d 30 20 29 sert( pBlob!=0 )
2be0: 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 6c ;. sha1sum_bl
2bf0: 6f 62 28 70 42 6c 6f 62 2c 20 26 68 61 73 68 29 ob(pBlob, &hash)
2c00: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 ;. }else{. b
2c10: 6c 6f 62 5f 69 6e 69 74 28 26 68 61 73 68 2c 20 lob_init(&hash,
2c20: 7a 55 75 69 64 2c 20 2d 31 29 3b 0a 20 20 7d 0a zUuid, -1);. }.
2c30: 20 20 73 69 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 size = blob_si
2c40: 7a 65 28 70 42 6c 6f 62 29 3b 0a 20 20 64 62 5f ze(pBlob);. db_
2c50: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f begin_transactio
2c60: 6e 28 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b n();.. /* Check
2c70: 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 65 to see if the e
2c80: 6e 74 72 79 20 61 6c 72 65 61 64 79 20 65 78 69 ntry already exi
2c90: 73 74 73 20 61 6e 64 20 69 66 20 69 74 20 64 6f sts and if it do
2ca0: 65 73 20 77 68 65 74 68 65 72 0a 20 20 2a 2a 20 es whether. **
2cb0: 6f 72 20 6e 6f 74 20 74 68 65 20 65 6e 74 72 79 or not the entry
2cc0: 20 69 73 20 61 20 70 68 61 6e 74 6f 6d 0a 20 20 is a phantom.
2cd0: 2a 2f 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 */. db_prepare(
2ce0: 26 73 31 2c 20 22 53 45 4c 45 43 54 20 72 69 64 &s1, "SELECT rid
2cf0: 2c 20 73 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62 , size FROM blob
2d00: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c WHERE uuid=%B",
2d10: 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 64 &hash);. if( d
2d20: 62 5f 73 74 65 70 28 26 73 31 29 3d 3d 53 51 4c b_step(&s1)==SQL
2d30: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 ITE_ROW ){. r
2d40: 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 id = db_column_i
2d50: 6e 74 28 26 73 31 2c 20 30 29 3b 0a 20 20 20 20 nt(&s1, 0);.
2d60: 69 66 28 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e if( db_column_in
2d70: 74 28 26 73 31 2c 20 31 29 3e 3d 30 20 7c 7c 20 t(&s1, 1)>=0 ||
2d80: 70 42 6c 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20 pBlob==0 ){.
2d90: 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 /* Either the
2da0: 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 61 20 70 entry is not a p
2db0: 68 61 6e 74 6f 6d 20 6f 72 20 69 74 20 69 73 20 hantom or it is
2dc0: 61 20 70 68 61 6e 74 6f 6d 20 62 75 74 20 77 65 a phantom but we
2dd0: 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 6e . ** have n
2de0: 6f 20 64 61 74 61 20 77 69 74 68 20 77 68 69 63 o data with whic
2df0: 68 20 74 6f 20 64 65 70 68 61 6e 74 6f 6d 69 7a h to dephantomiz
2e00: 65 20 69 74 2e 20 20 49 6e 20 65 69 74 68 65 72 e it. In either
2e10: 20 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 case,. **
2e20: 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 there is nothing
2e30: 20 66 6f 72 20 75 73 20 74 6f 20 64 6f 20 6f 74 for us to do ot
2e40: 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20 her than return
2e50: 74 68 65 20 52 49 44 2e 20 2a 2f 0a 20 20 20 20 the RID. */.
2e60: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 db_finalize(&s
2e70: 31 29 3b 0a 20 20 20 20 20 20 64 62 5f 65 6e 64 1);. db_end
2e80: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b _transaction(0);
2e90: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 69 . return ri
2ea0: 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 d;. }. }else
2eb0: 7b 0a 20 20 20 20 72 69 64 20 3d 20 30 3b 20 20 {. rid = 0;
2ec0: 2f 2a 20 4e 6f 20 65 6e 74 72 79 20 77 69 74 68 /* No entry with
2ed0: 20 74 68 65 20 73 61 6d 65 20 55 55 49 44 20 63 the same UUID c
2ee0: 75 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20 urrently exists
2ef0: 2a 2f 0a 20 20 20 20 6d 61 72 6b 41 73 55 6e 63 */. markAsUnc
2f00: 6c 75 73 74 65 72 65 64 20 3d 20 31 3b 0a 20 20 lustered = 1;.
2f10: 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 }. db_finalize(
2f20: 26 73 31 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 &s1);.. /* Cons
2f30: 74 72 75 63 74 20 61 20 72 65 63 65 69 76 65 64 truct a received
2f40: 2d 66 72 6f 6d 20 49 44 20 69 66 20 77 65 20 64 -from ID if we d
2f50: 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 61 o not already ha
2f60: 76 65 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 ve one */. if(
2f70: 67 2e 72 63 76 69 64 3d 3d 30 20 29 7b 0a 20 20 g.rcvid==0 ){.
2f80: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 db_multi_exec(
2f90: 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 . "INSERT
2fa0: 49 4e 54 4f 20 72 63 76 66 72 6f 6d 28 75 69 64 INTO rcvfrom(uid
2fb0: 2c 20 6d 74 69 6d 65 2c 20 6e 6f 6e 63 65 2c 20 , mtime, nonce,
2fc0: 69 70 61 64 64 72 29 22 0a 20 20 20 20 20 20 20 ipaddr)".
2fd0: 22 56 41 4c 55 45 53 28 25 64 2c 20 6a 75 6c 69 "VALUES(%d, juli
2fe0: 61 6e 64 61 79 28 27 6e 6f 77 27 29 2c 20 25 51 anday('now'), %Q
2ff0: 2c 20 25 51 29 22 2c 0a 20 20 20 20 20 20 20 67 , %Q)",. g
3000: 2e 75 73 65 72 55 69 64 2c 20 67 2e 7a 4e 6f 6e .userUid, g.zNon
3010: 63 65 2c 20 67 2e 7a 49 70 41 64 64 72 0a 20 20 ce, g.zIpAddr.
3020: 20 20 29 3b 0a 20 20 20 20 67 2e 72 63 76 69 64 );. g.rcvid
3030: 20 3d 20 64 62 5f 6c 61 73 74 5f 69 6e 73 65 72 = db_last_inser
3040: 74 5f 72 6f 77 69 64 28 29 3b 0a 20 20 7d 0a 0a t_rowid();. }..
3050: 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 blob_compress(
3060: 70 42 6c 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20 pBlob, &cmpr);.
3070: 20 69 66 28 20 72 69 64 3e 30 20 29 7b 0a 20 20 if( rid>0 ){.
3080: 20 20 2f 2a 20 57 65 20 61 72 65 20 6a 75 73 74 /* We are just
3090: 20 61 64 64 69 6e 67 20 64 61 74 61 20 74 6f 20 adding data to
30a0: 61 20 70 68 61 6e 74 6f 6d 20 2a 2f 0a 20 20 20 a phantom */.
30b0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c db_prepare(&s1,
30c0: 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 62 . "UPDATE b
30d0: 6c 6f 62 20 53 45 54 20 72 63 76 69 64 3d 25 64 lob SET rcvid=%d
30e0: 2c 20 73 69 7a 65 3d 25 64 2c 20 63 6f 6e 74 65 , size=%d, conte
30f0: 6e 74 3d 3a 64 61 74 61 20 57 48 45 52 45 20 72 nt=:data WHERE r
3100: 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 20 67 id=%d",. g
3110: 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c 20 72 69 .rcvid, size, ri
3120: 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 64 62 5f d. );. db_
3130: 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c 20 22 bind_blob(&s1, "
3140: 3a 64 61 74 61 22 2c 20 26 63 6d 70 72 29 3b 0a :data", &cmpr);.
3150: 20 20 20 20 64 62 5f 65 78 65 63 28 26 73 31 29 db_exec(&s1)
3160: 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 ;. db_multi_e
3170: 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f 4d xec("DELETE FROM
3180: 20 70 68 61 6e 74 6f 6d 20 57 48 45 52 45 20 72 phantom WHERE r
3190: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 id=%d", rid);.
31a0: 20 20 69 66 28 20 73 72 63 49 64 3d 3d 30 20 7c if( srcId==0 |
31b0: 7c 20 63 6f 6e 74 65 6e 74 5f 69 73 5f 61 76 61 | content_is_ava
31c0: 69 6c 61 62 6c 65 28 73 72 63 49 64 29 20 29 7b ilable(srcId) ){
31d0: 0a 20 20 20 20 20 20 69 73 44 65 70 68 61 6e 74 . isDephant
31e0: 6f 6d 69 7a 65 20 3d 20 31 3b 0a 20 20 20 20 20 omize = 1;.
31f0: 20 63 6f 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61 76 content_mark_av
3200: 61 69 6c 61 62 6c 65 28 72 69 64 29 3b 0a 20 20 ailable(rid);.
3210: 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
3220: 20 2f 2a 20 57 65 20 61 72 65 20 63 72 65 61 74 /* We are creat
3230: 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20 ing a new entry
3240: 2a 2f 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 */. db_prepar
3250: 65 28 26 73 31 2c 0a 20 20 20 20 20 20 22 49 4e e(&s1,. "IN
3260: 53 45 52 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72 SERT INTO blob(r
3270: 63 76 69 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63 cvid,size,uuid,c
3280: 6f 6e 74 65 6e 74 29 22 0a 20 20 20 20 20 20 22 ontent)". "
3290: 56 41 4c 55 45 53 28 25 64 2c 25 64 2c 27 25 62 VALUES(%d,%d,'%b
32a0: 27 2c 3a 64 61 74 61 29 22 2c 0a 20 20 20 20 20 ',:data)",.
32b0: 20 20 67 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c g.rcvid, size,
32c0: 20 26 68 61 73 68 0a 20 20 20 20 29 3b 0a 20 20 &hash. );.
32d0: 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 db_bind_blob(&
32e0: 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 63 6d s1, ":data", &cm
32f0: 70 72 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 pr);. db_exec
3300: 28 26 73 31 29 3b 0a 20 20 20 20 72 69 64 20 3d (&s1);. rid =
3310: 20 64 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f db_last_insert_
3320: 72 6f 77 69 64 28 29 3b 0a 20 20 20 20 69 66 28 rowid();. if(
3330: 20 21 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 !pBlob ){.
3340: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 db_multi_exec("
3350: 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 INSERT OR IGNORE
3360: 20 49 4e 54 4f 20 70 68 61 6e 74 6f 6d 20 56 41 INTO phantom VA
3370: 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b LUES(%d)", rid);
3380: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 67 . }. if( g
3390: 2e 6d 61 72 6b 50 72 69 76 61 74 65 20 29 7b 0a .markPrivate ){.
33a0: 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 db_multi_e
33b0: 78 65 63 28 22 49 4e 53 45 52 54 20 49 4e 54 4f xec("INSERT INTO
33c0: 20 70 72 69 76 61 74 65 20 56 41 4c 55 45 53 28 private VALUES(
33d0: 25 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 %d)", rid);.
33e0: 20 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74 65 markAsUncluste
33f0: 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 red = 0;. }.
3400: 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 }. blob_reset(
3410: 26 63 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 49 66 &cmpr);.. /* If
3420: 20 74 68 65 20 73 72 63 49 64 20 69 73 20 73 70 the srcId is sp
3430: 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 74 68 ecified, then th
3440: 65 20 64 61 74 61 20 77 65 20 6a 75 73 74 20 61 e data we just a
3450: 64 64 65 64 20 69 73 0a 20 20 2a 2a 20 72 65 61 dded is. ** rea
3460: 6c 6c 79 20 61 20 64 65 6c 74 61 2e 20 20 52 65 lly a delta. Re
3470: 63 6f 72 64 20 74 68 69 73 20 66 61 63 74 20 69 cord this fact i
3480: 6e 20 74 68 65 20 64 65 6c 74 61 20 74 61 62 6c n the delta tabl
3490: 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 72 e.. */. if( sr
34a0: 63 49 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 cId ){. db_mu
34b0: 6c 74 69 5f 65 78 65 63 28 22 52 45 50 4c 41 43 lti_exec("REPLAC
34c0: 45 20 49 4e 54 4f 20 64 65 6c 74 61 28 72 69 64 E INTO delta(rid
34d0: 2c 73 72 63 69 64 29 20 56 41 4c 55 45 53 28 25 ,srcid) VALUES(%
34e0: 64 2c 25 64 29 22 2c 20 72 69 64 2c 20 73 72 63 d,%d)", rid, src
34f0: 49 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 Id);. }. if( !
3500: 69 73 44 65 70 68 61 6e 74 6f 6d 69 7a 65 20 26 isDephantomize &
3510: 26 20 62 61 67 5f 66 69 6e 64 28 26 63 6f 6e 74 & bag_find(&cont
3520: 65 6e 74 43 61 63 68 65 2e 6d 69 73 73 69 6e 67 entCache.missing
3530: 2c 20 72 69 64 29 20 26 26 20 0a 20 20 20 20 20 , rid) && .
3540: 20 28 73 72 63 49 64 3d 3d 30 20 7c 7c 20 63 6f (srcId==0 || co
3550: 6e 74 65 6e 74 5f 69 73 5f 61 76 61 69 6c 61 62 ntent_is_availab
3560: 6c 65 28 73 72 63 49 64 29 29 20 29 7b 0a 20 20 le(srcId)) ){.
3570: 20 20 63 6f 6e 74 65 6e 74 5f 6d 61 72 6b 5f 61 content_mark_a
3580: 76 61 69 6c 61 62 6c 65 28 72 69 64 29 3b 0a 20 vailable(rid);.
3590: 20 7d 0a 20 20 69 66 28 20 69 73 44 65 70 68 61 }. if( isDepha
35a0: 6e 74 6f 6d 69 7a 65 20 29 7b 0a 20 20 20 20 61 ntomize ){. a
35b0: 66 74 65 72 5f 64 65 70 68 61 6e 74 6f 6d 69 7a fter_dephantomiz
35c0: 65 28 72 69 64 2c 20 30 29 3b 0a 20 20 7d 0a 20 e(rid, 0);. }.
35d0: 20 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65 . /* Add the e
35e0: 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 6e lement to the un
35f0: 63 6c 75 73 74 65 72 65 64 20 74 61 62 6c 65 20 clustered table
3600: 69 66 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 if has never bee
3610: 6e 0a 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c n. ** previousl
3620: 79 20 73 65 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 y seen.. */. i
3630: 66 28 20 6d 61 72 6b 41 73 55 6e 63 6c 75 73 74 f( markAsUnclust
3640: 65 72 65 64 20 29 7b 0a 20 20 20 20 64 62 5f 6d ered ){. db_m
3650: 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 ulti_exec("INSER
3660: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f T OR IGNORE INTO
3670: 20 75 6e 63 6c 75 73 74 65 72 65 64 20 56 41 4c unclustered VAL
3680: 55 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a UES(%d)", rid);.
3690: 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 73 68 }.. /* Finish
36a0: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
36b0: 20 61 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f 0a and cleanup */.
36c0: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 db_finalize(&s
36d0: 31 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 1);. db_end_tra
36e0: 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 62 nsaction(0);. b
36f0: 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 lob_reset(&hash)
3700: 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72 72 ;.. /* Make arr
3710: 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 76 65 72 angements to ver
3720: 69 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74 ify that the dat
3730: 61 20 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72 a can be recover
3740: 65 64 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 77 ed. ** before w
3750: 65 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 76 65 e commit */. ve
3760: 72 69 66 79 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d rify_before_comm
3770: 69 74 28 72 69 64 29 3b 0a 20 20 72 65 74 75 72 it(rid);. retur
3780: 6e 20 72 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n rid;.}../*.**
3790: 43 72 65 61 74 65 20 61 20 6e 65 77 20 70 68 61 Create a new pha
37a0: 6e 74 6f 6d 20 77 69 74 68 20 74 68 65 20 67 69 ntom with the gi
37b0: 76 65 6e 20 55 55 49 44 20 61 6e 64 20 72 65 74 ven UUID and ret
37c0: 75 72 6e 20 69 74 73 20 61 72 74 69 66 61 63 74 urn its artifact
37d0: 20 49 44 2e 0a 2a 2f 0a 69 6e 74 20 63 6f 6e 74 ID..*/.int cont
37e0: 65 6e 74 5f 6e 65 77 28 63 6f 6e 73 74 20 63 68 ent_new(const ch
37f0: 61 72 20 2a 7a 55 75 69 64 29 7b 0a 20 20 69 6e ar *zUuid){. in
3800: 74 20 72 69 64 3b 0a 20 20 73 74 61 74 69 63 20 t rid;. static
3810: 53 74 6d 74 20 73 31 2c 20 73 32 2c 20 73 33 3b Stmt s1, s2, s3;
3820: 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 67 2e . . assert( g.
3830: 72 65 70 6f 73 69 74 6f 72 79 4f 70 65 6e 20 29 repositoryOpen )
3840: 3b 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 ;. db_begin_tra
3850: 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 69 66 nsaction();. if
3860: 28 20 75 75 69 64 5f 69 73 5f 73 68 75 6e 6e 65 ( uuid_is_shunne
3870: 64 28 7a 55 75 69 64 29 20 29 7b 0a 20 20 20 20 d(zUuid) ){.
3880: 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 db_end_transacti
3890: 6f 6e 28 30 29 3b 0a 20 20 20 20 72 65 74 75 72 on(0);. retur
38a0: 6e 20 30 3b 0a 20 20 7d 0a 20 20 64 62 5f 73 74 n 0;. }. db_st
38b0: 61 74 69 63 5f 70 72 65 70 61 72 65 28 26 73 31 atic_prepare(&s1
38c0: 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e ,. "INSERT IN
38d0: 54 4f 20 62 6c 6f 62 28 72 63 76 69 64 2c 73 69 TO blob(rcvid,si
38e0: 7a 65 2c 75 75 69 64 2c 63 6f 6e 74 65 6e 74 29 ze,uuid,content)
38f0: 22 0a 20 20 20 20 22 56 41 4c 55 45 53 28 30 2c ". "VALUES(0,
3900: 2d 31 2c 3a 75 75 69 64 2c 4e 55 4c 4c 29 22 0a -1,:uuid,NULL)".
3910: 20 20 29 3b 0a 20 20 64 62 5f 62 69 6e 64 5f 74 );. db_bind_t
3920: 65 78 74 28 26 73 31 2c 20 22 3a 75 75 69 64 22 ext(&s1, ":uuid"
3930: 2c 20 7a 55 75 69 64 29 3b 0a 20 20 64 62 5f 65 , zUuid);. db_e
3940: 78 65 63 28 26 73 31 29 3b 0a 20 20 72 69 64 20 xec(&s1);. rid
3950: 3d 20 64 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 = db_last_insert
3960: 5f 72 6f 77 69 64 28 29 3b 0a 20 20 64 62 5f 73 _rowid();. db_s
3970: 74 61 74 69 63 5f 70 72 65 70 61 72 65 28 26 73 tatic_prepare(&s
3980: 32 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20 49 2,. "INSERT I
3990: 4e 54 4f 20 70 68 61 6e 74 6f 6d 20 56 41 4c 55 NTO phantom VALU
39a0: 45 53 28 3a 72 69 64 29 22 0a 20 20 29 3b 0a 20 ES(:rid)". );.
39b0: 20 64 62 5f 62 69 6e 64 5f 69 6e 74 28 26 73 32 db_bind_int(&s2
39c0: 2c 20 22 3a 72 69 64 22 2c 20 72 69 64 29 3b 0a , ":rid", rid);.
39d0: 20 20 64 62 5f 65 78 65 63 28 26 73 32 29 3b 0a db_exec(&s2);.
39e0: 20 20 69 66 28 20 67 2e 6d 61 72 6b 50 72 69 76 if( g.markPriv
39f0: 61 74 65 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 ate ){. db_mu
3a00: 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54 lti_exec("INSERT
3a10: 20 49 4e 54 4f 20 70 72 69 76 61 74 65 20 56 41 INTO private VA
3a20: 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b LUES(%d)", rid);
3a30: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 . }else{. db
3a40: 5f 73 74 61 74 69 63 5f 70 72 65 70 61 72 65 28 _static_prepare(
3a50: 26 73 33 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 &s3,. "INSE
3a60: 52 54 20 49 4e 54 4f 20 75 6e 63 6c 75 73 74 65 RT INTO uncluste
3a70: 72 65 64 20 56 41 4c 55 45 53 28 3a 72 69 64 29 red VALUES(:rid)
3a80: 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 64 62 5f ". );. db_
3a90: 62 69 6e 64 5f 69 6e 74 28 26 73 33 2c 20 22 3a bind_int(&s3, ":
3aa0: 72 69 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 rid", rid);.
3ab0: 64 62 5f 65 78 65 63 28 26 73 33 29 3b 0a 20 20 db_exec(&s3);.
3ac0: 7d 0a 20 20 62 61 67 5f 69 6e 73 65 72 74 28 26 }. bag_insert(&
3ad0: 63 6f 6e 74 65 6e 74 43 61 63 68 65 2e 6d 69 73 contentCache.mis
3ae0: 73 69 6e 67 2c 20 72 69 64 29 3b 0a 20 20 64 62 sing, rid);. db
3af0: 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
3b00: 28 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 69 (0);. return ri
3b10: 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d d;.}.../*.** COM
3b20: 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 MAND: test-cont
3b30: 65 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a 20 45 78 ent-put.**.** Ex
3b40: 74 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f tract a blob fro
3b50: 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 m the database a
3b60: 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f nd write it into
3b70: 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 a file..*/.void
3b80: 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 70 75 test_content_pu
3b90: 74 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 t_cmd(void){. i
3ba0: 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 nt rid;. Blob c
3bb0: 6f 6e 74 65 6e 74 3b 0a 20 20 69 66 28 20 67 2e ontent;. if( g.
3bc0: 61 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 argc!=3 ) usage(
3bd0: 22 46 49 4c 45 4e 41 4d 45 22 29 3b 0a 20 20 64 "FILENAME");. d
3be0: 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e b_must_be_within
3bf0: 5f 74 72 65 65 28 29 3b 0a 20 20 75 73 65 72 5f _tree();. user_
3c00: 73 65 6c 65 63 74 28 29 3b 0a 20 20 62 6c 6f 62 select();. blob
3c10: 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 _read_from_file(
3c20: 26 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 72 67 76 &content, g.argv
3c30: 5b 32 5d 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f [2]);. rid = co
3c40: 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 ntent_put(&conte
3c50: 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 70 72 69 nt, 0, 0);. pri
3c60: 6e 74 66 28 22 69 6e 73 65 72 74 65 64 20 61 73 ntf("inserted as
3c70: 20 72 65 63 6f 72 64 20 25 64 5c 6e 22 2c 20 72 record %d\n", r
3c80: 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 id);.}../*.** Ma
3c90: 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e 74 ke sure the cont
3ca0: 65 6e 74 20 61 74 20 72 69 64 20 69 73 20 74 68 ent at rid is th
3cb0: 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 e original conte
3cc0: 6e 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 0a nt and is not a.
3cd0: 2a 2a 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 ** delta..*/.voi
3ce0: 64 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 d content_undelt
3cf0: 61 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 66 a(int rid){. if
3d00: 28 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 29 ( findSrcid(rid)
3d10: 3e 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 78 >0 ){. Blob x
3d20: 3b 0a 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e ;. if( conten
3d30: 74 5f 67 65 74 28 72 69 64 2c 20 26 78 29 20 29 t_get(rid, &x) )
3d40: 7b 0a 20 20 20 20 20 20 53 74 6d 74 20 73 3b 0a {. Stmt s;.
3d50: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 db_prepare
3d60: 28 26 73 2c 20 22 55 50 44 41 54 45 20 62 6c 6f (&s, "UPDATE blo
3d70: 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 63 b SET content=:c
3d80: 2c 20 73 69 7a 65 3d 25 64 20 57 48 45 52 45 20 , size=%d WHERE
3d90: 72 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 20 20 rid=%d",.
3da0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c bl
3db0: 6f 62 5f 73 69 7a 65 28 26 78 29 2c 20 72 69 64 ob_size(&x), rid
3dc0: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f );. blob_co
3dd0: 6d 70 72 65 73 73 28 26 78 2c 20 26 78 29 3b 0a mpress(&x, &x);.
3de0: 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c db_bind_bl
3df0: 6f 62 28 26 73 2c 20 22 3a 63 22 2c 20 26 78 29 ob(&s, ":c", &x)
3e00: 3b 0a 20 20 20 20 20 20 64 62 5f 65 78 65 63 28 ;. db_exec(
3e10: 26 73 29 3b 0a 20 20 20 20 20 20 64 62 5f 66 69 &s);. db_fi
3e20: 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 20 20 nalize(&s);.
3e30: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 78 29 blob_reset(&x)
3e40: 3b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 ;. db_multi
3e50: 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 _exec("DELETE FR
3e60: 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72 OM delta WHERE r
3e70: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 id=%d", rid);.
3e80: 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
3e90: 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d COMMAND: test-
3ea0: 63 6f 6e 74 65 6e 74 2d 75 6e 64 65 6c 74 61 0a content-undelta.
3eb0: 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 **.** Make sure
3ec0: 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 52 the content at R
3ed0: 45 43 4f 52 44 49 44 20 69 73 20 6e 6f 74 20 61 ECORDID is not a
3ee0: 20 64 65 6c 74 61 0a 2a 2f 0a 76 6f 69 64 20 74 delta.*/.void t
3ef0: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 est_content_unde
3f00: 6c 74 61 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 lta_cmd(void){.
3f10: 20 69 6e 74 20 72 69 64 3b 0a 20 20 69 66 28 20 int rid;. if(
3f20: 67 2e 61 72 67 63 21 3d 32 20 29 20 75 73 61 67 g.argc!=2 ) usag
3f30: 65 28 22 52 45 43 4f 52 44 49 44 22 29 3b 0a 20 e("RECORDID");.
3f40: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 db_must_be_with
3f50: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 72 69 64 in_tree();. rid
3f60: 20 3d 20 61 74 6f 69 28 67 2e 61 72 67 76 5b 32 = atoi(g.argv[2
3f70: 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 75 6e ]);. content_un
3f80: 64 65 6c 74 61 28 72 69 64 29 3b 0a 7d 0a 0a 2f delta(rid);.}../
3f90: 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
3fa0: 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 52 49 if the given RI
3fb0: 44 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 50 D is marked as P
3fc0: 52 49 56 41 54 45 2e 0a 2a 2f 0a 69 6e 74 20 63 RIVATE..*/.int c
3fd0: 6f 6e 74 65 6e 74 5f 69 73 5f 70 72 69 76 61 74 ontent_is_privat
3fe0: 65 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 73 74 e(int rid){. st
3ff0: 61 74 69 63 20 53 74 6d 74 20 73 31 3b 0a 20 20 atic Stmt s1;.
4000: 69 6e 74 20 72 63 3b 0a 20 20 64 62 5f 73 74 61 int rc;. db_sta
4010: 74 69 63 5f 70 72 65 70 61 72 65 28 26 73 31 2c tic_prepare(&s1,
4020: 0a 20 20 20 20 22 53 45 4c 45 43 54 20 31 20 46 . "SELECT 1 F
4030: 52 4f 4d 20 70 72 69 76 61 74 65 20 57 48 45 52 ROM private WHER
4040: 45 20 72 69 64 3d 3a 72 69 64 22 0a 20 20 29 3b E rid=:rid". );
4050: 0a 20 20 64 62 5f 62 69 6e 64 5f 69 6e 74 28 26 . db_bind_int(&
4060: 73 31 2c 20 22 3a 72 69 64 22 2c 20 72 69 64 29 s1, ":rid", rid)
4070: 3b 0a 20 20 72 63 20 3d 20 64 62 5f 73 74 65 70 ;. rc = db_step
4080: 28 26 73 31 29 3b 0a 20 20 64 62 5f 72 65 73 65 (&s1);. db_rese
4090: 74 28 26 73 31 29 3b 0a 20 20 72 65 74 75 72 6e t(&s1);. return
40a0: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 3b rc==SQLITE_ROW;
40b0: 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 .}../*.** Make
40c0: 20 73 75 72 65 20 61 6e 20 61 72 74 69 66 61 63 sure an artifac
40d0: 74 20 69 73 20 70 75 62 6c 69 63 2e 20 20 0a 2a t is public. .*
40e0: 2f 0a 76 6f 69 64 20 63 6f 6e 74 65 6e 74 5f 6d /.void content_m
40f0: 61 6b 65 5f 70 75 62 6c 69 63 28 69 6e 74 20 72 ake_public(int r
4100: 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53 74 id){. static St
4110: 6d 74 20 73 31 3b 0a 20 20 64 62 5f 73 74 61 74 mt s1;. db_stat
4120: 69 63 5f 70 72 65 70 61 72 65 28 26 73 31 2c 0a ic_prepare(&s1,.
4130: 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d "DELETE FROM
4140: 20 70 72 69 76 61 74 65 20 57 48 45 52 45 20 72 private WHERE r
4150: 69 64 3d 3a 72 69 64 22 0a 20 20 29 3b 0a 20 20 id=:rid". );.
4160: 64 62 5f 62 69 6e 64 5f 69 6e 74 28 26 73 31 2c db_bind_int(&s1,
4170: 20 22 3a 72 69 64 22 2c 20 72 69 64 29 3b 0a 20 ":rid", rid);.
4180: 20 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 7d db_exec(&s1);.}
4190: 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
41a0: 68 65 20 73 74 6f 72 61 67 65 20 6f 66 20 72 69 he storage of ri
41b0: 64 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 d so that it is
41c0: 61 20 64 65 6c 74 61 20 6f 66 20 73 72 63 69 64 a delta of srcid
41d0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 69 64 20 69 ..**.** If rid i
41e0: 73 20 61 6c 72 65 61 64 79 20 61 20 64 65 6c 74 s already a delt
41f0: 61 20 66 72 6f 6d 20 73 6f 6d 65 20 6f 74 68 65 a from some othe
4200: 72 20 70 6c 61 63 65 20 74 68 65 6e 20 6e 6f 0a r place then no.
4210: 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 ** conversion oc
4220: 63 75 72 73 20 61 6e 64 20 74 68 69 73 20 69 73 curs and this is
4230: 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 a no-op unless
4240: 66 6f 72 63 65 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 force==1..**.**
4250: 4e 65 76 65 72 20 67 65 6e 65 72 61 74 65 20 61 Never generate a
4260: 20 64 65 6c 74 61 20 74 68 61 74 20 63 61 72 72 delta that carr
4270: 69 65 73 20 61 20 70 72 69 76 61 74 65 20 61 72 ies a private ar
4280: 74 69 66 61 63 74 20 69 6e 74 6f 20 61 20 70 75 tifact into a pu
4290: 62 6c 69 63 0a 2a 2a 20 61 72 74 69 66 61 63 74 blic.** artifact
42a0: 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 77 68 . Otherwise, wh
42b0: 65 6e 20 77 65 20 67 6f 20 74 6f 20 73 65 6e 64 en we go to send
42c0: 20 74 68 65 20 70 75 62 6c 69 63 20 61 72 74 69 the public arti
42d0: 66 61 63 74 20 6f 6e 20 61 0a 2a 2a 20 73 79 6e fact on a.** syn
42e0: 63 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 c operation, the
42f0: 20 6f 74 68 65 72 20 65 6e 64 20 6f 66 20 74 68 other end of th
4300: 65 20 73 79 6e 63 20 77 69 6c 6c 20 6e 65 76 65 e sync will neve
4310: 72 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 63 r be able to rec
4320: 65 69 76 65 0a 2a 2a 20 74 68 65 20 73 6f 75 72 eive.** the sour
4330: 63 65 20 6f 66 20 74 68 65 20 64 65 6c 74 61 2e ce of the delta.
4340: 20 20 49 74 20 69 73 20 4f 4b 20 74 6f 20 64 65 It is OK to de
4350: 6c 74 61 20 70 72 69 76 61 74 65 2d 3e 70 72 69 lta private->pri
4360: 76 61 74 65 20 61 6e 64 0a 2a 2a 20 70 75 62 6c vate and.** publ
4370: 69 63 2d 3e 70 72 69 76 61 74 65 20 61 6e 64 20 ic->private and
4380: 70 75 62 6c 69 63 2d 3e 70 75 62 6c 69 63 2e 20 public->public.
4390: 20 4a 75 73 74 20 6e 6f 20 70 72 69 76 61 74 65 Just no private
43a0: 2d 3e 70 75 62 6c 69 63 20 64 65 6c 74 61 2e 0a ->public delta..
43b0: 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 69 64 20 69 **.** If srcid i
43c0: 73 20 61 20 64 65 6c 74 61 20 74 68 61 74 20 64 s a delta that d
43d0: 65 70 65 6e 64 73 20 6f 6e 20 72 69 64 2c 20 74 epends on rid, t
43e0: 68 65 6e 20 73 72 63 69 64 20 69 73 0a 2a 2a 20 hen srcid is.**
43f0: 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 75 6e 64 converted to und
4400: 65 6c 74 61 65 64 20 74 65 78 74 2e 0a 2a 2a 0a eltaed text..**.
4410: 2a 2a 20 49 66 20 65 69 74 68 65 72 20 72 69 64 ** If either rid
4420: 20 6f 72 20 73 72 63 69 64 20 63 6f 6e 74 61 69 or srcid contai
4430: 6e 20 6c 65 73 73 20 74 68 61 6e 20 35 30 20 62 n less than 50 b
4440: 79 74 65 73 2c 20 6f 72 20 69 66 20 74 68 65 0a ytes, or if the.
4450: 2a 2a 20 72 65 73 75 6c 74 69 6e 67 20 64 65 6c ** resulting del
4460: 74 61 20 64 6f 65 73 20 6e 6f 74 20 61 63 68 69 ta does not achi
4470: 65 76 65 20 61 20 63 6f 6d 70 72 65 73 73 69 6f eve a compressio
4480: 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 32 35 n of at least 25
4490: 25 20 6f 6e 0a 2a 2a 20 69 74 73 20 6f 77 6e 20 % on.** its own
44a0: 74 68 65 20 72 69 64 20 69 73 20 6c 65 66 74 20 the rid is left
44b0: 75 6e 74 6f 75 63 68 65 64 2e 0a 2a 2f 0a 76 6f untouched..*/.vo
44c0: 69 64 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 id content_delti
44d0: 66 79 28 69 6e 74 20 72 69 64 2c 20 69 6e 74 20 fy(int rid, int
44e0: 73 72 63 69 64 2c 20 69 6e 74 20 66 6f 72 63 65 srcid, int force
44f0: 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 42 6c ){. int s;. Bl
4500: 6f 62 20 64 61 74 61 2c 20 73 72 63 2c 20 64 65 ob data, src, de
4510: 6c 74 61 3b 0a 20 20 53 74 6d 74 20 73 31 2c 20 lta;. Stmt s1,
4520: 73 32 3b 0a 20 20 69 66 28 20 73 72 63 69 64 3d s2;. if( srcid=
4530: 3d 72 69 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 =rid ) return;.
4540: 20 69 66 28 20 21 66 6f 72 63 65 20 26 26 20 66 if( !force && f
4550: 69 6e 64 53 72 63 69 64 28 72 69 64 29 3e 30 20 indSrcid(rid)>0
4560: 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
4570: 63 6f 6e 74 65 6e 74 5f 69 73 5f 70 72 69 76 61 content_is_priva
4580: 74 65 28 73 72 63 69 64 29 20 26 26 20 21 63 6f te(srcid) && !co
4590: 6e 74 65 6e 74 5f 69 73 5f 70 72 69 76 61 74 65 ntent_is_private
45a0: 28 72 69 64 29 20 29 7b 0a 20 20 20 20 72 65 74 (rid) ){. ret
45b0: 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 20 3d 20 73 urn;. }. s = s
45c0: 72 63 69 64 3b 0a 20 20 77 68 69 6c 65 28 20 28 rcid;. while( (
45d0: 73 20 3d 20 66 69 6e 64 53 72 63 69 64 28 73 29 s = findSrcid(s)
45e0: 29 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 )>0 ){. if( s
45f0: 3d 3d 72 69 64 20 29 7b 0a 20 20 20 20 20 20 63 ==rid ){. c
4600: 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 73 ontent_undelta(s
4610: 72 63 69 64 29 3b 0a 20 20 20 20 20 20 62 72 65 rcid);. bre
4620: 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
4630: 63 6f 6e 74 65 6e 74 5f 67 65 74 28 73 72 63 69 content_get(srci
4640: 64 2c 20 26 73 72 63 29 3b 0a 20 20 69 66 28 20 d, &src);. if(
4650: 62 6c 6f 62 5f 73 69 7a 65 28 26 73 72 63 29 3c blob_size(&src)<
4660: 35 30 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 72 50 ){. blob_r
4670: 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 20 20 eset(&src);.
4680: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 63 6f return;. }. co
4690: 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 ntent_get(rid, &
46a0: 64 61 74 61 29 3b 0a 20 20 69 66 28 20 62 6c 6f data);. if( blo
46b0: 62 5f 73 69 7a 65 28 26 64 61 74 61 29 3c 35 30 b_size(&data)<50
46c0: 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 ){. blob_res
46d0: 65 74 28 26 73 72 63 29 3b 0a 20 20 20 20 62 6c et(&src);. bl
46e0: 6f 62 5f 72 65 73 65 74 28 26 64 61 74 61 29 3b ob_reset(&data);
46f0: 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
4700: 0a 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 . blob_delta_cr
4710: 65 61 74 65 28 26 73 72 63 2c 20 26 64 61 74 61 eate(&src, &data
4720: 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 69 66 28 , &delta);. if(
4730: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 65 6c 74 blob_size(&delt
4740: 61 29 20 3c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 a) < blob_size(&
4750: 64 61 74 61 29 2a 30 2e 37 35 20 29 7b 0a 20 20 data)*0.75 ){.
4760: 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 blob_compress(
4770: 26 64 65 6c 74 61 2c 20 26 64 65 6c 74 61 29 3b &delta, &delta);
4780: 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 . db_prepare(
4790: 26 73 31 2c 20 22 55 50 44 41 54 45 20 62 6c 6f &s1, "UPDATE blo
47a0: 62 20 53 45 54 20 63 6f 6e 74 65 6e 74 3d 3a 64 b SET content=:d
47b0: 61 74 61 20 57 48 45 52 45 20 72 69 64 3d 25 64 ata WHERE rid=%d
47c0: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 64 62 5f ", rid);. db_
47d0: 70 72 65 70 61 72 65 28 26 73 32 2c 20 22 52 45 prepare(&s2, "RE
47e0: 50 4c 41 43 45 20 49 4e 54 4f 20 64 65 6c 74 61 PLACE INTO delta
47f0: 28 72 69 64 2c 73 72 63 69 64 29 56 41 4c 55 45 (rid,srcid)VALUE
4800: 53 28 25 64 2c 25 64 29 22 2c 20 72 69 64 2c 20 S(%d,%d)", rid,
4810: 73 72 63 69 64 29 3b 0a 20 20 20 20 64 62 5f 62 srcid);. db_b
4820: 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c 20 22 3a ind_blob(&s1, ":
4830: 64 61 74 61 22 2c 20 26 64 65 6c 74 61 29 3b 0a data", &delta);.
4840: 20 20 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 db_begin_tra
4850: 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 20 20 nsaction();.
4860: 64 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 db_exec(&s1);.
4870: 20 20 64 62 5f 65 78 65 63 28 26 73 32 29 3b 0a db_exec(&s2);.
4880: 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 db_end_trans
4890: 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20 20 64 action(0);. d
48a0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b b_finalize(&s1);
48b0: 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 . db_finalize
48c0: 28 26 73 32 29 3b 0a 20 20 20 20 76 65 72 69 66 (&s2);. verif
48d0: 79 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 y_before_commit(
48e0: 72 69 64 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 rid);. }. blob
48f0: 5f 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 _reset(&src);.
4900: 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 61 74 61 blob_reset(&data
4910: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 );. blob_reset(
4920: 26 64 65 6c 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a &delta);.}../*.*
4930: 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 * COMMAND: test
4940: 2d 63 6f 6e 74 65 6e 74 2d 64 65 6c 74 69 66 79 -content-deltify
4950: 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 .**.** Convert t
4960: 68 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 52 49 he content at RI
4970: 44 20 69 6e 74 6f 20 61 20 64 65 6c 74 61 20 66 D into a delta f
4980: 72 6f 6d 20 53 52 43 49 44 2e 0a 2a 2f 0a 76 6f rom SRCID..*/.vo
4990: 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f id test_content_
49a0: 64 65 6c 74 69 66 79 5f 63 6d 64 28 76 6f 69 64 deltify_cmd(void
49b0: 29 7b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 ){. if( g.argc!
49c0: 3d 35 20 29 20 75 73 61 67 65 28 22 52 49 44 20 =5 ) usage("RID
49d0: 53 52 43 49 44 20 46 4f 52 43 45 22 29 3b 0a 20 SRCID FORCE");.
49e0: 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 db_must_be_with
49f0: 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 63 6f 6e in_tree();. con
4a00: 74 65 6e 74 5f 64 65 6c 74 69 66 79 28 61 74 6f tent_deltify(ato
4a10: 69 28 67 2e 61 72 67 76 5b 32 5d 29 2c 20 61 74 i(g.argv[2]), at
4a20: 6f 69 28 67 2e 61 72 67 76 5b 33 5d 29 2c 20 61 oi(g.argv[3]), a
4a30: 74 6f 69 28 67 2e 61 72 67 76 5b 34 5d 29 29 3b toi(g.argv[4]));
4a40: 0a 7d 0a .}.