/* ** Change the storage of rid so that it is a delta of srcid. ** ** If rid is already a delta from some other place then no ** conversion occurs and this is a no-op unless force==1. ** ** Never generate a delta that carries a private artifact into a public ** artifact. Otherwise, when we go to send the public artifact on a ** sync operation, the other end of the sync will never be able to receive ** the source of the delta. It is OK to delta private->private and ** public->private and public->public. Just no private->public delta. ** ** If srcid is a delta that depends on rid, then srcid is ** converted to undeltaed text. ** ** If either rid or srcid contain less than 50 bytes, or if the ** resulting delta does not achieve a compression of at least 25% ** the rid is left untouched. ** ** Return 1 if a delta is made and 0 if no delta occurs. */ int content_deltify(int rid, int srcid, int force){ int s; Blob data, src, delta; Stmt s1, s2; int rc = 0; if( srcid==rid ) return 0; if( !force && findSrcid(rid)>0 ) return 0; if( content_is_private(srcid) && !content_is_private(rid) ){ return 0; } s = srcid; while( (s = findSrcid(s))>0 ){ if( s==rid ){ content_undelta(srcid); break; } } content_get(srcid, &src); if( blob_size(&src)<50 ){ blob_reset(&src); return 0; } content_get(rid, &data); if( blob_size(&data)<50 ){ blob_reset(&src); blob_reset(&data); return 0; } blob_delta_create(&src, &data, &delta); if( blob_size(&delta) <= blob_size(&data)*0.75 ){ blob_compress(&delta, &delta); db_prepare(&s1, "UPDATE blob SET content=:data WHERE rid=%d", rid); db_prepare(&s2, "REPLACE INTO delta(rid,srcid)VALUES(%d,%d)", rid, srcid); db_bind_blob(&s1, ":data", &delta); db_begin_transaction(); db_exec(&s1); db_exec(&s2); db_end_transaction(0); db_finalize(&s1); db_finalize(&s2); verify_before_commit(rid); rc = 1; } blob_reset(&src); blob_reset(&data); blob_reset(&delta); return rc; }
/* fossil bundle export BUNDLE ?OPTIONS? ** ** OPTIONS: ** --branch BRANCH --from TAG --to TAG ** --checkin TAG ** --standalone */ static void bundle_export_cmd(void){ int bStandalone = find_option("standalone",0,0)!=0; int mnToBundle; /* Minimum RID in the bundle */ Stmt q; /* Decode the arguments (like --branch) that specify which artifacts ** should be in the bundle */ db_multi_exec("CREATE TEMP TABLE tobundle(rid INTEGER PRIMARY KEY);"); subtree_from_arguments("tobundle"); find_checkin_associates("tobundle", 0); verify_all_options(); describe_artifacts("IN tobundle"); if( g.argc!=4 ) usage("export BUNDLE ?OPTIONS?"); /* Create the new bundle */ bundle_attach_file(g.argv[3], "b1", 1); db_begin_transaction(); /* Add 'mtime' and 'project-code' entries to the bconfig table */ db_multi_exec( "INSERT INTO bconfig(bcname,bcvalue)" " VALUES('mtime',datetime('now'));" ); db_multi_exec( "INSERT INTO bconfig(bcname,bcvalue)" " SELECT name, value FROM config" " WHERE name IN ('project-code');" ); /* Directly copy content from the repository into the bundle as long ** as the repository content is a delta from some other artifact that ** is also in the bundle. */ db_multi_exec( "REPLACE INTO bblob(blobid,uuid,sz,delta,data,notes) " " SELECT" " tobundle.rid," " blob.uuid," " blob.size," " delta.srcid," " blob.content," " (SELECT summary FROM description WHERE rid=blob.rid)" " FROM tobundle, blob, delta" " WHERE blob.rid=tobundle.rid" " AND delta.rid=tobundle.rid" " AND delta.srcid IN tobundle;" ); /* For all the remaining artifacts, we need to construct their deltas ** manually. */ mnToBundle = db_int(0,"SELECT min(rid) FROM tobundle"); db_prepare(&q, "SELECT rid FROM tobundle" " WHERE rid NOT IN (SELECT blobid FROM bblob)" " ORDER BY +rid;" ); while( db_step(&q)==SQLITE_ROW ){ Blob content; int rid = db_column_int(&q,0); int deltaFrom = 0; /* Get the raw, uncompressed content of the artifact into content */ content_get(rid, &content); /* Try to find another artifact, not within the bundle, that is a ** plausible candidate for being a delta basis for the content. Set ** deltaFrom to the RID of that other artifact. Leave deltaFrom set ** to zero if the content should not be delta-compressed */ if( !bStandalone ){ if( db_exists("SELECT 1 FROM plink WHERE cid=%d",rid) ){ deltaFrom = db_int(0, "SELECT max(cid) FROM plink" " WHERE cid<%d", mnToBundle); }else{ deltaFrom = db_int(0, "SELECT max(fid) FROM mlink" " WHERE fnid=(SELECT fnid FROM mlink WHERE fid=%d)" " AND fid<%d", rid, mnToBundle); } } /* Try to insert the insert the artifact as a delta */ if( deltaFrom ){ Blob basis, delta; content_get(deltaFrom, &basis); blob_delta_create(&basis, &content, &delta); if( blob_size(&delta)>0.9*blob_size(&content) ){ deltaFrom = 0; }else{ Stmt ins; blob_compress(&delta, &delta); db_prepare(&ins, "REPLACE INTO bblob(blobid,uuid,sz,delta,data,notes)" " SELECT %d, uuid, size, (SELECT uuid FROM blob WHERE rid=%d)," " :delta, (SELECT summary FROM description WHERE rid=blob.rid)" " FROM blob WHERE rid=%d", rid, deltaFrom, rid); db_bind_blob(&ins, ":delta", &delta); db_step(&ins); db_finalize(&ins); } blob_reset(&basis); blob_reset(&delta); } /* If unable to insert the artifact as a delta, insert full-text */ if( deltaFrom==0 ){ Stmt ins; blob_compress(&content, &content); db_prepare(&ins, "REPLACE INTO bblob(blobid,uuid,sz,delta,data,notes)" " SELECT rid, uuid, size, NULL, :content," " (SELECT summary FROM description WHERE rid=blob.rid)" " FROM blob WHERE rid=%d", rid); db_bind_blob(&ins, ":content", &content); db_step(&ins); db_finalize(&ins); } blob_reset(&content); } db_finalize(&q); db_end_transaction(0); }