From 89b09563a32e38028d0a6a99a4ef61bec7b5bc85 Mon Sep 17 00:00:00 2001 From: gardners Date: Sat, 8 Sep 2012 07:55:57 +0930 Subject: [PATCH] Wrote function to get a range of BARs from the Rhizome database for use in Rhizome Direct synchronisation requests. #9 --- rhizome.h | 4 ++ rhizome_direct.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/rhizome.h b/rhizome.h index 850b3383..dbe7ac45 100644 --- a/rhizome.h +++ b/rhizome.h @@ -389,6 +389,10 @@ int rhizome_http_server_start(int (*http_parse_func)(rhizome_http_request *), int port_low,int port_high); int rhizome_direct_process_post_multipart_bytes (rhizome_http_request *r,const char *bytes,int count); +int rhizome_direct_get_bars(const unsigned char *bid_low, + unsigned char *bid_high, + unsigned char *bars_out, + int bars_requested); extern unsigned char favicon_bytes[]; extern int favicon_len; diff --git a/rhizome_direct.c b/rhizome_direct.c index 17efd895..7a741edc 100644 --- a/rhizome_direct.c +++ b/rhizome_direct.c @@ -569,13 +569,114 @@ int app_rhizome_direct_sync(int argc, const char *const *argv, { /* Attempt to connect with a remote Rhizome Direct instance, and negotiate which BARs to synchronise. */ - char *modeName = (argc >= 3 ? argv[2] : "sync"); + const char *modeName = (argc >= 3 ? argv[2] : "sync"); int mode=3; /* two-way sync */ if (!strcasecmp(modeName,"push")) mode=1; /* push only */ if (!strcasecmp(modeName,"pull")) mode=2; /* pull only */ DEBUGF("sync direction = %d",mode); + + unsigned char bid_low[RHIZOME_BAR_BYTES]; + unsigned char bid_high[RHIZOME_BAR_BYTES]; + unsigned char bars_out[RHIZOME_BAR_BYTES*128]; + + /* Start synchronising from the beginning of the BID address space */ + memset(bid_low,0x00,RHIZOME_BAR_BYTES); + int bars_stuffed=rhizome_direct_get_bars(bid_low,bid_high,bars_out, + sizeof(bars_out)/RHIZOME_BAR_BYTES); + DEBUGF("Receied %d BARs",bars_stuffed); + dump("BARs",bars_out,RHIZOME_BAR_BYTES*bars_stuffed); return -1; } + +/* Read upto the next BARs from the Rhizome database, + beginning from the first BAR that corresponds to a manifest with + BID>=. + Sets to the highest BID for which a BAR was returned. + Return value is the number of BARs written into . + + XXX Once the rhizome database gets big, we will need to make sure + that we have suitable indexes. It is tempting to just pack BARs + by row_id, but the far end needs them in an orderly manner so that + it is possible to make provably complete comparison of the contents + of the respective rhizome databases. +*/ +int rhizome_direct_get_bars(const unsigned char *bid_low, + unsigned char *bid_high, + unsigned char *bars_out, + int bars_requested) +{ + sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT; + + /* Get number of bundles available if required */ + char query[1024]; + snprintf(query,1024, + "SELECT COUNT(BAR) FROM MANIFESTS" + " WHERE ID>='%s' ORDER BY BAR LIMIT %d;", + alloca_tohex(bid_low,RHIZOME_MANIFEST_ID_BYTES),bars_requested); + + long long tmp = 0; + if (sqlite_exec_int64_retry(&retry, &tmp, query) != 1) + { return(WHY("Could not count BARs for advertisement")); } + int bundles_available = (int) tmp; + if(1) + DEBUGF("%d matching bundles in database.",bundles_available); + + snprintf(query,1024, + "SELECT BAR,ROWID FROM MANIFESTS" + " WHERE ID>='%s' ORDER BY BAR LIMIT %d;", + alloca_tohex(bid_low,RHIZOME_MANIFEST_ID_BYTES),bars_requested); + + sqlite3_stmt *statement=sqlite_prepare(query); + sqlite3_blob *blob=NULL; + + int bars_written=0; + + while(bars_written