| | @@ -149,18 +149,36 @@ |
| 149 | 149 | } |
| 150 | 150 | db_finalize(&q); |
| 151 | 151 | } |
| 152 | 152 | bag_clear(&pending); |
| 153 | 153 | } |
| 154 | + |
| 155 | +/* |
| 156 | +** Get the blob.content value for blob.rid=rid. Return 1 on success or |
| 157 | +** 0 on failure. |
| 158 | +*/ |
| 159 | +static int content_of_blob(int rid, Blob *pBlob){ |
| 160 | + static Stmt q; |
| 161 | + int rc = 0; |
| 162 | + db_static_prepare(&q, "SELECT content FROM blob WHERE rid=:rid AND size>=0"); |
| 163 | + db_bind_int(&q, ":rid", rid); |
| 164 | + if( db_step(&q)==SQLITE_ROW ){ |
| 165 | + db_ephemeral_blob(&q, 0, pBlob); |
| 166 | + blob_uncompress(pBlob, pBlob); |
| 167 | + rc = 1; |
| 168 | + } |
| 169 | + db_reset(&q); |
| 170 | + return rc; |
| 171 | +} |
| 172 | + |
| 154 | 173 | |
| 155 | 174 | /* |
| 156 | 175 | ** Extract the content for ID rid and put it into the |
| 157 | 176 | ** uninitialized blob. Return 1 on success. If the record |
| 158 | 177 | ** is a phantom, zero pBlob and return 0. |
| 159 | 178 | */ |
| 160 | 179 | int content_get(int rid, Blob *pBlob){ |
| 161 | | - Stmt q; |
| 162 | 180 | Blob src; |
| 163 | 181 | int srcid; |
| 164 | 182 | int rc = 0; |
| 165 | 183 | int i; |
| 166 | 184 | static Bag inProcess; |
| | @@ -183,11 +201,11 @@ |
| 183 | 201 | blob_zero(&contentCache.a[i].content); |
| 184 | 202 | contentCache.n--; |
| 185 | 203 | if( i<contentCache.n ){ |
| 186 | 204 | contentCache.a[i] = contentCache.a[contentCache.n]; |
| 187 | 205 | } |
| 188 | | - CONTENT_TRACE(("%*shit cache: %d\n", |
| 206 | + CONTENT_TRACE(("%*scache: %d\n", |
| 189 | 207 | bag_count(&inProcess), "", rid)) |
| 190 | 208 | return 1; |
| 191 | 209 | } |
| 192 | 210 | } |
| 193 | 211 | |
| | @@ -210,21 +228,17 @@ |
| 210 | 228 | return 0; |
| 211 | 229 | } |
| 212 | 230 | bag_insert(&inProcess, srcid); |
| 213 | 231 | |
| 214 | 232 | if( content_get(srcid, &src) ){ |
| 215 | | - db_prepare(&q, "SELECT content FROM blob WHERE rid=%d AND size>=0", rid); |
| 216 | | - if( db_step(&q)==SQLITE_ROW ){ |
| 217 | | - Blob delta; |
| 218 | | - db_ephemeral_blob(&q, 0, &delta); |
| 219 | | - blob_uncompress(&delta, &delta); |
| 233 | + Blob delta; |
| 234 | + if( content_of_blob(rid, &delta) ){ |
| 220 | 235 | blob_init(pBlob,0,0); |
| 221 | 236 | blob_delta_apply(&src, &delta, pBlob); |
| 222 | 237 | blob_reset(&delta); |
| 223 | 238 | rc = 1; |
| 224 | 239 | } |
| 225 | | - db_finalize(&q); |
| 226 | 240 | |
| 227 | 241 | /* Save the srcid artifact in the cache */ |
| 228 | 242 | if( contentCache.n<MX_CACHE_CNT ){ |
| 229 | 243 | i = contentCache.n++; |
| 230 | 244 | }else if( ((contentCache.skipCnt++)%EXPELL_INTERVAL)!=0 ){ |
| | @@ -254,17 +268,13 @@ |
| 254 | 268 | } |
| 255 | 269 | } |
| 256 | 270 | bag_remove(&inProcess, srcid); |
| 257 | 271 | }else{ |
| 258 | 272 | /* No delta required. Read content directly from the database */ |
| 259 | | - db_prepare(&q, "SELECT content FROM blob WHERE rid=%d AND size>=0", rid); |
| 260 | | - if( db_step(&q)==SQLITE_ROW ){ |
| 261 | | - db_ephemeral_blob(&q, 0, pBlob); |
| 262 | | - blob_uncompress(pBlob, pBlob); |
| 273 | + if( content_of_blob(rid, pBlob) ){ |
| 263 | 274 | rc = 1; |
| 264 | 275 | } |
| 265 | | - db_finalize(&q); |
| 266 | 276 | } |
| 267 | 277 | if( rc==0 ){ |
| 268 | 278 | bag_insert(&contentCache.missing, rid); |
| 269 | 279 | }else{ |
| 270 | 280 | bag_insert(&contentCache.available, rid); |
| 271 | 281 | |