| | @@ -19,33 +19,25 @@ |
| 19 | 19 | */ |
| 20 | 20 | #include "config.h" |
| 21 | 21 | #include "content.h" |
| 22 | 22 | #include <assert.h> |
| 23 | 23 | |
| 24 | | -/* |
| 25 | | -** Macros for debugging |
| 26 | | -*/ |
| 27 | | -#if 0 |
| 28 | | -# define CONTENT_TRACE(X) printf X; |
| 29 | | -#else |
| 30 | | -# define CONTENT_TRACE(X) |
| 31 | | -#endif |
| 32 | | - |
| 33 | 24 | /* |
| 34 | 25 | ** The artifact retrival cache |
| 35 | 26 | */ |
| 36 | | -#define MX_CACHE_CNT 50 /* Maximum number of positive cache entries */ |
| 37 | | -#define EXPELL_INTERVAL 5 /* How often to expell from a full cache */ |
| 38 | 27 | static struct { |
| 39 | | - int n; /* Current number of positive cache entries */ |
| 28 | + i64 szTotal; /* Total size of all entries in the cache */ |
| 29 | + int n; /* Current number of eache entries */ |
| 30 | + int nAlloc; /* Number of slots allocated in a[] */ |
| 40 | 31 | int nextAge; /* Age counter for implementing LRU */ |
| 41 | 32 | int skipCnt; /* Used to limit entries expelled from cache */ |
| 42 | | - struct { /* One instance of this for each cache entry */ |
| 33 | + struct cacheLine { /* One instance of this for each cache entry */ |
| 43 | 34 | int rid; /* Artifact id */ |
| 44 | 35 | int age; /* Age. Newer is larger */ |
| 45 | 36 | Blob content; /* Content of the artifact */ |
| 46 | | - } a[MX_CACHE_CNT]; /* The positive cache */ |
| 37 | + } *a; /* The positive cache */ |
| 38 | + Bag inCache; /* Set of artifacts currently in cache */ |
| 47 | 39 | |
| 48 | 40 | /* |
| 49 | 41 | ** The missing artifact cache. |
| 50 | 42 | ** |
| 51 | 43 | ** Artifacts whose record ID are in missingCache cannot be retrieved |
| | @@ -56,10 +48,54 @@ |
| 56 | 48 | */ |
| 57 | 49 | Bag missing; /* Cache of artifacts that are incomplete */ |
| 58 | 50 | Bag available; /* Cache of artifacts that are complete */ |
| 59 | 51 | } contentCache; |
| 60 | 52 | |
| 53 | +/* |
| 54 | +** Remove the oldest element from the content cache |
| 55 | +*/ |
| 56 | +static void content_cache_expire_oldest(void){ |
| 57 | + int i; |
| 58 | + int mnAge = contentCache.nextAge; |
| 59 | + int mn = -1; |
| 60 | + for(i=0; i<contentCache.n; i++){ |
| 61 | + if( contentCache.a[i].age<mnAge ){ |
| 62 | + mnAge = contentCache.a[i].age; |
| 63 | + mn = i; |
| 64 | + } |
| 65 | + } |
| 66 | + if( mn>=0 ){ |
| 67 | + bag_remove(&contentCache.inCache, contentCache.a[mn].rid); |
| 68 | + contentCache.szTotal -= blob_size(&contentCache.a[mn].content); |
| 69 | + blob_reset(&contentCache.a[mn].content); |
| 70 | + contentCache.n--; |
| 71 | + contentCache.a[mn] = contentCache.a[contentCache.n]; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/* |
| 76 | +** Add an entry to the content cache |
| 77 | +*/ |
| 78 | +void content_cache_insert(int rid, Blob *pBlob){ |
| 79 | + struct cacheLine *p; |
| 80 | + if( contentCache.n>500 || contentCache.szTotal>50000000 ){ |
| 81 | + content_cache_expire_oldest(); |
| 82 | + } |
| 83 | + if( contentCache.n>=contentCache.nAlloc ){ |
| 84 | + contentCache.nAlloc = contentCache.nAlloc*2 + 10; |
| 85 | + contentCache.a = realloc(contentCache.a, |
| 86 | + contentCache.nAlloc*sizeof(contentCache.a[0])); |
| 87 | + if( contentCache.a==0 ) fossil_panic("out of memory"); |
| 88 | + } |
| 89 | + p = &contentCache.a[contentCache.n++]; |
| 90 | + p->rid = rid; |
| 91 | + p->age = contentCache.nextAge++; |
| 92 | + contentCache.szTotal += blob_size(pBlob); |
| 93 | + p->content = *pBlob; |
| 94 | + blob_zero(pBlob); |
| 95 | + bag_insert(&contentCache.inCache, rid); |
| 96 | +} |
| 61 | 97 | |
| 62 | 98 | /* |
| 63 | 99 | ** Clear the content cache. |
| 64 | 100 | */ |
| 65 | 101 | void content_clear_cache(void){ |
| | @@ -67,11 +103,13 @@ |
| 67 | 103 | for(i=0; i<contentCache.n; i++){ |
| 68 | 104 | blob_reset(&contentCache.a[i].content); |
| 69 | 105 | } |
| 70 | 106 | bag_clear(&contentCache.missing); |
| 71 | 107 | bag_clear(&contentCache.available); |
| 108 | + bag_clear(&contentCache.inCache); |
| 72 | 109 | contentCache.n = 0; |
| 110 | + contentCache.szTotal = 0; |
| 73 | 111 | } |
| 74 | 112 | |
| 75 | 113 | /* |
| 76 | 114 | ** Return the srcid associated with rid. Or return 0 if rid is |
| 77 | 115 | ** original content and not a delta. |
| | @@ -95,32 +133,31 @@ |
| 95 | 133 | ** true if it is. Return false if rid is a phantom or depends on |
| 96 | 134 | ** a phantom. |
| 97 | 135 | */ |
| 98 | 136 | int content_is_available(int rid){ |
| 99 | 137 | int srcid; |
| 100 | | - if( bag_find(&contentCache.missing, rid) ){ |
| 101 | | - return 0; |
| 102 | | - } |
| 103 | | - if( bag_find(&contentCache.available, rid) ){ |
| 104 | | - return 1; |
| 105 | | - } |
| 106 | | - if( db_int(-1, "SELECT size FROM blob WHERE rid=%d", rid)<0 ){ |
| 107 | | - bag_insert(&contentCache.missing, rid); |
| 108 | | - return 0; |
| 109 | | - } |
| 110 | | - srcid = findSrcid(rid); |
| 111 | | - if( srcid==0 ){ |
| 112 | | - bag_insert(&contentCache.available, rid); |
| 113 | | - return 1; |
| 114 | | - } |
| 115 | | - if( content_is_available(srcid) ){ |
| 116 | | - bag_insert(&contentCache.available, rid); |
| 117 | | - return 1; |
| 118 | | - }else{ |
| 119 | | - bag_insert(&contentCache.missing, rid); |
| 120 | | - return 0; |
| 121 | | - } |
| 138 | + int depth = 0; /* Limit to recursion depth */ |
| 139 | + while( depth++ < 10000000 ){ |
| 140 | + if( bag_find(&contentCache.missing, rid) ){ |
| 141 | + return 0; |
| 142 | + } |
| 143 | + if( bag_find(&contentCache.available, rid) ){ |
| 144 | + return 1; |
| 145 | + } |
| 146 | + if( db_int(-1, "SELECT size FROM blob WHERE rid=%d", rid)<0 ){ |
| 147 | + bag_insert(&contentCache.missing, rid); |
| 148 | + return 0; |
| 149 | + } |
| 150 | + srcid = findSrcid(rid); |
| 151 | + if( srcid==0 ){ |
| 152 | + bag_insert(&contentCache.available, rid); |
| 153 | + return 1; |
| 154 | + } |
| 155 | + rid = srcid; |
| 156 | + } |
| 157 | + fossil_panic("delta-loop in repository"); |
| 158 | + return 0; |
| 122 | 159 | } |
| 123 | 160 | |
| 124 | 161 | /* |
| 125 | 162 | ** Mark artifact rid as being available now. Update the cache to |
| 126 | 163 | ** show that everything that was formerly unavailable because rid |
| | @@ -163,113 +200,84 @@ |
| 163 | 200 | } |
| 164 | 201 | db_reset(&q); |
| 165 | 202 | return rc; |
| 166 | 203 | } |
| 167 | 204 | |
| 168 | | - |
| 169 | 205 | /* |
| 170 | 206 | ** Extract the content for ID rid and put it into the |
| 171 | 207 | ** uninitialized blob. Return 1 on success. If the record |
| 172 | 208 | ** is a phantom, zero pBlob and return 0. |
| 173 | 209 | */ |
| 174 | 210 | int content_get(int rid, Blob *pBlob){ |
| 175 | | - Blob src; |
| 176 | | - int srcid; |
| 177 | | - int rc = 0; |
| 211 | + int rc; |
| 178 | 212 | int i; |
| 179 | | - static Bag inProcess; |
| 213 | + int nextRid; |
| 180 | 214 | |
| 181 | 215 | assert( g.repositoryOpen ); |
| 182 | 216 | blob_zero(pBlob); |
| 183 | 217 | if( rid==0 ) return 0; |
| 184 | 218 | |
| 185 | 219 | /* Early out if we know the content is not available */ |
| 186 | 220 | if( bag_find(&contentCache.missing, rid) ){ |
| 187 | | - CONTENT_TRACE(("%*smiss from cache: %d\n", |
| 188 | | - bag_count(&inProcess), "", rid)) |
| 189 | 221 | return 0; |
| 190 | 222 | } |
| 191 | 223 | |
| 192 | 224 | /* Look for the artifact in the cache first */ |
| 193 | | - for(i=0; i<contentCache.n; i++){ |
| 194 | | - if( contentCache.a[i].rid==rid ){ |
| 195 | | - *pBlob = contentCache.a[i].content; |
| 196 | | - blob_zero(&contentCache.a[i].content); |
| 197 | | - contentCache.n--; |
| 198 | | - if( i<contentCache.n ){ |
| 199 | | - contentCache.a[i] = contentCache.a[contentCache.n]; |
| 200 | | - } |
| 201 | | - CONTENT_TRACE(("%*scache: %d\n", |
| 202 | | - bag_count(&inProcess), "", rid)) |
| 203 | | - return 1; |
| 204 | | - } |
| 205 | | - } |
| 206 | | - |
| 207 | | - /* See if we need to apply a delta to find this artifact */ |
| 208 | | - srcid = findSrcid(rid); |
| 209 | | - CONTENT_TRACE(("%*ssearching for %d. Need %d.\n", |
| 210 | | - bag_count(&inProcess), "", rid, srcid)) |
| 211 | | - |
| 212 | | - |
| 213 | | - if( srcid ){ |
| 214 | | - /* Yes, a delta is required */ |
| 215 | | - if( bag_find(&inProcess, srcid) ){ |
| 216 | | - db_multi_exec( |
| 217 | | - "UPDATE blob SET content=NULL, size=-1 WHERE rid=%d;" |
| 218 | | - "DELETE FROM delta WHERE rid=%d;" |
| 219 | | - "INSERT OR IGNORE INTO phantom VALUES(%d);", |
| 220 | | - srcid, srcid, srcid |
| 221 | | - ); |
| 222 | | - blob_zero(pBlob); |
| 223 | | - return 0; |
| 224 | | - } |
| 225 | | - bag_insert(&inProcess, srcid); |
| 226 | | - |
| 227 | | - if( content_get(srcid, &src) ){ |
| 228 | | - Blob delta; |
| 229 | | - if( content_of_blob(rid, &delta) ){ |
| 230 | | - blob_init(pBlob,0,0); |
| 231 | | - blob_delta_apply(&src, &delta, pBlob); |
| 225 | + if( bag_find(&contentCache.inCache, rid) ){ |
| 226 | + for(i=0; i<contentCache.n; i++){ |
| 227 | + if( contentCache.a[i].rid==rid ){ |
| 228 | + blob_copy(pBlob, &contentCache.a[i].content); |
| 229 | + contentCache.a[i].age = contentCache.nextAge++; |
| 230 | + return 1; |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + nextRid = findSrcid(rid); |
| 236 | + if( nextRid==0 ){ |
| 237 | + rc = content_of_blob(rid, pBlob); |
| 238 | + }else{ |
| 239 | + int n = 1; |
| 240 | + int nAlloc = 10; |
| 241 | + int *a = 0; |
| 242 | + int mx; |
| 243 | + Blob delta, next; |
| 244 | + |
| 245 | + a = malloc( sizeof(a[0])*nAlloc ); |
| 246 | + if( a==0 ) fossil_panic("out of memory"); |
| 247 | + a[0] = rid; |
| 248 | + a[1] = nextRid; |
| 249 | + n = 1; |
| 250 | + while( !bag_find(&contentCache.inCache, nextRid) |
| 251 | + && (nextRid = findSrcid(nextRid))>0 ){ |
| 252 | + n++; |
| 253 | + if( n>=nAlloc ){ |
| 254 | + nAlloc = nAlloc*2 + 10; |
| 255 | + a = realloc(a, nAlloc*sizeof(a[0])); |
| 256 | + if( a==0 ) fossil_panic("out of memory"); |
| 257 | + } |
| 258 | + a[n] = nextRid; |
| 259 | + } |
| 260 | + mx = n; |
| 261 | + rc = content_get(a[n], pBlob); |
| 262 | + n--; |
| 263 | + while( rc && n>=0 ){ |
| 264 | + rc = content_of_blob(a[n], &delta); |
| 265 | + if( rc ){ |
| 266 | + blob_delta_apply(pBlob, &delta, &next); |
| 232 | 267 | blob_reset(&delta); |
| 233 | | - rc = 1; |
| 234 | | - } |
| 235 | | - |
| 236 | | - /* Save the srcid artifact in the cache */ |
| 237 | | - if( contentCache.n<MX_CACHE_CNT ){ |
| 238 | | - i = contentCache.n++; |
| 239 | | - }else if( ((contentCache.skipCnt++)%EXPELL_INTERVAL)!=0 ){ |
| 240 | | - i = -1; |
| 241 | | - }else{ |
| 242 | | - int j, best; |
| 243 | | - best = contentCache.nextAge+1; |
| 244 | | - i = -1; |
| 245 | | - for(j=0; j<contentCache.n; j++){ |
| 246 | | - if( contentCache.a[j].age<best ){ |
| 247 | | - i = j; |
| 248 | | - best = contentCache.a[j].age; |
| 249 | | - } |
| 250 | | - } |
| 251 | | - CONTENT_TRACE(("%*sexpell %d from cache\n", |
| 252 | | - bag_count(&inProcess), "", contentCache.a[i].rid)) |
| 253 | | - blob_reset(&contentCache.a[i].content); |
| 254 | | - } |
| 255 | | - if( i>=0 ){ |
| 256 | | - contentCache.a[i].content = src; |
| 257 | | - contentCache.a[i].age = contentCache.nextAge++; |
| 258 | | - contentCache.a[i].rid = srcid; |
| 259 | | - CONTENT_TRACE(("%*sadd %d to cache\n", |
| 260 | | - bag_count(&inProcess), "", srcid)) |
| 261 | | - }else{ |
| 262 | | - blob_reset(&src); |
| 263 | | - } |
| 264 | | - } |
| 265 | | - bag_remove(&inProcess, srcid); |
| 266 | | - }else{ |
| 267 | | - /* No delta required. Read content directly from the database */ |
| 268 | | - if( content_of_blob(rid, pBlob) ){ |
| 269 | | - rc = 1; |
| 270 | | - } |
| 268 | + if( (mx-n)%8==0 ){ |
| 269 | + content_cache_insert(a[n+1], pBlob); |
| 270 | + }else{ |
| 271 | + blob_reset(pBlob); |
| 272 | + } |
| 273 | + *pBlob = next; |
| 274 | + } |
| 275 | + n--; |
| 276 | + } |
| 277 | + free(a); |
| 278 | + if( !rc ) blob_reset(pBlob); |
| 271 | 279 | } |
| 272 | 280 | if( rc==0 ){ |
| 273 | 281 | bag_insert(&contentCache.missing, rid); |
| 274 | 282 | }else{ |
| 275 | 283 | bag_insert(&contentCache.available, rid); |
| | @@ -320,35 +328,45 @@ |
| 320 | 328 | |
| 321 | 329 | /* |
| 322 | 330 | ** When a record is converted from a phantom to a real record, |
| 323 | 331 | ** if that record has other records that are derived by delta, |
| 324 | 332 | ** then call manifest_crosslink() on those other records. |
| 333 | +** |
| 334 | +** Tail recursion is used to minimize stack depth. |
| 325 | 335 | */ |
| 326 | 336 | void after_dephantomize(int rid, int linkFlag){ |
| 327 | 337 | Stmt q; |
| 328 | | - int prevTid = 0; |
| 329 | | - |
| 330 | | - /* The prevTid variable is used to delay invoking this routine |
| 331 | | - ** recursively, if possible, until after the query has finalized, |
| 332 | | - ** in order to avoid having an excessive number of prepared statements. |
| 333 | | - ** This is most effective in the common case where the query returns |
| 334 | | - ** just one row. |
| 335 | | - */ |
| 336 | | - db_prepare(&q, "SELECT rid FROM delta WHERE srcid=%d", rid); |
| 337 | | - while( db_step(&q)==SQLITE_ROW ){ |
| 338 | | - int tid = db_column_int(&q, 0); |
| 339 | | - if( prevTid ) after_dephantomize(prevTid, 1); |
| 340 | | - prevTid = tid; |
| 341 | | - } |
| 342 | | - db_finalize(&q); |
| 343 | | - if( prevTid ) after_dephantomize(prevTid, 1); |
| 344 | | - if( linkFlag ){ |
| 345 | | - Blob content; |
| 346 | | - content_get(rid, &content); |
| 347 | | - manifest_crosslink(rid, &content); |
| 348 | | - blob_reset(&content); |
| 349 | | - } |
| 338 | + int nChildAlloc = 0; |
| 339 | + int *aChild = 0; |
| 340 | + |
| 341 | + while( rid ){ |
| 342 | + int nChildUsed = 0; |
| 343 | + int i; |
| 344 | + if( linkFlag ){ |
| 345 | + Blob content; |
| 346 | + content_get(rid, &content); |
| 347 | + manifest_crosslink(rid, &content); |
| 348 | + blob_reset(&content); |
| 349 | + } |
| 350 | + db_prepare(&q, "SELECT rid FROM delta WHERE srcid=%d", rid); |
| 351 | + while( db_step(&q)==SQLITE_ROW ){ |
| 352 | + int child = db_column_int(&q, 0); |
| 353 | + if( nChildUsed>=nChildAlloc ){ |
| 354 | + nChildAlloc = nChildAlloc*2 + 10; |
| 355 | + aChild = realloc(aChild, nChildAlloc*sizeof(aChild)); |
| 356 | + if( aChild==0 ) fossil_panic("out of memory"); |
| 357 | + } |
| 358 | + aChild[nChildUsed++] = child; |
| 359 | + } |
| 360 | + db_finalize(&q); |
| 361 | + for(i=1; i<nChildUsed; i++){ |
| 362 | + after_dephantomize(aChild[i], 1); |
| 363 | + } |
| 364 | + rid = nChildUsed>0 ? aChild[0] : 0; |
| 365 | + linkFlag = 1; |
| 366 | + } |
| 367 | + free(aChild); |
| 350 | 368 | } |
| 351 | 369 | |
| 352 | 370 | /* |
| 353 | 371 | ** Write content into the database. Return the record ID. If the |
| 354 | 372 | ** content is already in the database, just return the record ID. |
| 355 | 373 | |