| | @@ -364,12 +364,11 @@ |
| 364 | 364 | ** content is already in the database, just return the record ID. |
| 365 | 365 | ** |
| 366 | 366 | ** If srcId is specified, then pBlob is delta content from |
| 367 | 367 | ** the srcId record. srcId might be a phantom. |
| 368 | 368 | ** |
| 369 | | -** A phantom is written if pBlob==0. If pBlob==0 or if srcId is |
| 370 | | -** specified then the UUID is set to zUuid. Otherwise zUuid is |
| 369 | +** If srcId is specified then the UUID is set to zUuid. Otherwise zUuid is |
| 371 | 370 | ** ignored. In the future this might change such that the content |
| 372 | 371 | ** hash is checked against zUuid to make sure it is correct. |
| 373 | 372 | ** |
| 374 | 373 | ** If the record already exists but is a phantom, the pBlob content |
| 375 | 374 | ** is inserted and the phatom becomes a real record. |
| | @@ -382,20 +381,17 @@ |
| 382 | 381 | Blob hash; |
| 383 | 382 | int markAsUnclustered = 0; |
| 384 | 383 | int isDephantomize = 0; |
| 385 | 384 | |
| 386 | 385 | assert( g.repositoryOpen ); |
| 387 | | - if( pBlob && srcId==0 ){ |
| 386 | + assert( pBlob!=0 ); |
| 387 | + if( srcId==0 ){ |
| 388 | 388 | sha1sum_blob(pBlob, &hash); |
| 389 | 389 | }else{ |
| 390 | 390 | blob_init(&hash, zUuid, -1); |
| 391 | 391 | } |
| 392 | | - if( pBlob==0 ){ |
| 393 | | - size = -1; |
| 394 | | - }else{ |
| 395 | | - size = blob_size(pBlob); |
| 396 | | - } |
| 392 | + size = blob_size(pBlob); |
| 397 | 393 | db_begin_transaction(); |
| 398 | 394 | |
| 399 | 395 | /* Check to see if the entry already exists and if it does whether |
| 400 | 396 | ** or not the entry is a phantom |
| 401 | 397 | */ |
| | @@ -415,27 +411,26 @@ |
| 415 | 411 | markAsUnclustered = 1; |
| 416 | 412 | } |
| 417 | 413 | db_finalize(&s1); |
| 418 | 414 | |
| 419 | 415 | /* Construct a received-from ID if we do not already have one */ |
| 420 | | - if( g.rcvid==0 && pBlob!=0 ){ |
| 416 | + if( g.rcvid==0 ){ |
| 421 | 417 | db_multi_exec( |
| 422 | 418 | "INSERT INTO rcvfrom(uid, mtime, nonce, ipaddr)" |
| 423 | 419 | "VALUES(%d, julianday('now'), %Q, %Q)", |
| 424 | 420 | g.userUid, g.zNonce, g.zIpAddr |
| 425 | 421 | ); |
| 426 | 422 | g.rcvid = db_last_insert_rowid(); |
| 427 | 423 | } |
| 428 | 424 | |
| 425 | + blob_compress(pBlob, &cmpr); |
| 429 | 426 | if( rid>0 ){ |
| 430 | 427 | /* We are just adding data to a phantom */ |
| 431 | | - assert( pBlob!=0 ); |
| 432 | 428 | db_prepare(&s1, |
| 433 | 429 | "UPDATE blob SET rcvid=%d, size=%d, content=:data WHERE rid=%d", |
| 434 | 430 | g.rcvid, size, rid |
| 435 | 431 | ); |
| 436 | | - blob_compress(pBlob, &cmpr); |
| 437 | 432 | db_bind_blob(&s1, ":data", &cmpr); |
| 438 | 433 | db_exec(&s1); |
| 439 | 434 | db_multi_exec("DELETE FROM phantom WHERE rid=%d", rid); |
| 440 | 435 | if( srcId==0 || content_is_available(srcId) ){ |
| 441 | 436 | isDephantomize = 1; |
| | @@ -446,20 +441,18 @@ |
| 446 | 441 | db_prepare(&s1, |
| 447 | 442 | "INSERT INTO blob(rcvid,size,uuid,content)" |
| 448 | 443 | "VALUES(%d,%d,'%b',:data)", |
| 449 | 444 | g.rcvid, size, &hash |
| 450 | 445 | ); |
| 451 | | - if( pBlob ){ |
| 452 | | - blob_compress(pBlob, &cmpr); |
| 453 | | - db_bind_blob(&s1, ":data", &cmpr); |
| 454 | | - } |
| 446 | + blob_compress(pBlob, &cmpr); |
| 455 | 447 | db_exec(&s1); |
| 456 | 448 | rid = db_last_insert_rowid(); |
| 457 | 449 | if( !pBlob ){ |
| 458 | 450 | db_multi_exec("INSERT OR IGNORE INTO phantom VALUES(%d)", rid); |
| 459 | 451 | } |
| 460 | 452 | } |
| 453 | + blob_reset(&cmpr); |
| 461 | 454 | |
| 462 | 455 | /* If the srcId is specified, then the data we just added is |
| 463 | 456 | ** really a delta. Record this fact in the delta table. |
| 464 | 457 | */ |
| 465 | 458 | if( srcId ){ |
| | @@ -485,16 +478,40 @@ |
| 485 | 478 | db_end_transaction(0); |
| 486 | 479 | blob_reset(&hash); |
| 487 | 480 | |
| 488 | 481 | /* Make arrangements to verify that the data can be recovered |
| 489 | 482 | ** before we commit */ |
| 490 | | - if( pBlob ){ |
| 491 | | - blob_reset(&cmpr); |
| 492 | | - verify_before_commit(rid); |
| 493 | | - } |
| 483 | + verify_before_commit(rid); |
| 484 | + return rid; |
| 485 | +} |
| 486 | + |
| 487 | +/* |
| 488 | +** Create a new phantom with the given UUID and return its artifact ID. |
| 489 | +*/ |
| 490 | +int content_new(const char *zUuid){ |
| 491 | + int rid; |
| 492 | + static Stmt s1, s2; |
| 493 | + |
| 494 | + assert( g.repositoryOpen ); |
| 495 | + db_begin_transaction(); |
| 496 | + db_static_prepare(&s1, |
| 497 | + "INSERT INTO blob(rcvid,size,uuid,content)" |
| 498 | + "VALUES(0,-1,:uuid,NULL)" |
| 499 | + ); |
| 500 | + db_bind_text(&s1, ":uuid", zUuid); |
| 501 | + db_exec(&s1); |
| 502 | + rid = db_last_insert_rowid(); |
| 503 | + db_static_prepare(&s2, |
| 504 | + "INSERT INTO phantom VALUES(:rid)" |
| 505 | + ); |
| 506 | + db_bind_int(&s2, ":rid", rid); |
| 507 | + db_exec(&s2); |
| 508 | + bag_insert(&contentCache.missing, rid); |
| 509 | + db_end_transaction(0); |
| 494 | 510 | return rid; |
| 495 | 511 | } |
| 512 | + |
| 496 | 513 | |
| 497 | 514 | /* |
| 498 | 515 | ** COMMAND: test-content-put |
| 499 | 516 | ** |
| 500 | 517 | ** Extract a blob from the database and write it into a file. |
| 501 | 518 | |