use of net.i2p.client.naming.SingleFileNamingService in project i2p.i2p by i2p.
the class Daemon method update.
/**
* Update the router and published address books using remote data from the
* subscribed address books listed in subscriptions.
* Merging of the "master" addressbook is NOT supported.
*
* @param router
* The NamingService to update, generally the root NamingService from the context.
* @param published
* The published AddressBook. This address book is published on
* the user's eepsite so that others may subscribe to it.
* May be null.
* If non-null, overwrite with the new addressbook.
* @param subscriptions
* A SubscriptionList listing the remote address books to update
* from.
* @param log
* The log to write changes and conflicts to.
* May be null.
* @since 0.8.7
*/
public static void update(NamingService router, File published, SubscriptionList subscriptions, Log log) {
// If the NamingService is a database, we look up as we go.
// If it is a text file, we do things differently, to avoid O(n**2) behavior
// when scanning large subscription results (i.e. those that return the whole file, not just the new entries) -
// we load all the known hostnames into a Set one time.
// This also has the advantage of not flushing the NamingService's LRU cache.
String nsClass = router.getClass().getSimpleName();
boolean isTextFile = nsClass.equals("HostsTxtNamingService") || nsClass.equals("SingleFileNamingService");
Set<String> knownNames;
if (isTextFile) {
// load the hostname set
Properties opts = new Properties();
opts.setProperty("file", "hosts.txt");
knownNames = router.getNames(opts);
} else {
knownNames = null;
}
NamingService publishedNS;
if (published != null) {
publishedNS = new SingleFileNamingService(I2PAppContext.getGlobalContext(), published.getAbsolutePath());
} else {
publishedNS = null;
}
Iterator<AddressBook> iter = subscriptions.iterator();
while (iter.hasNext()) {
// yes, the EepGet fetch() is done in next()
long start = System.currentTimeMillis();
AddressBook addressbook = iter.next();
// SubscriptionIterator puts in a dummy AddressBook with no location if no fetch is done
if (DEBUG && log != null && addressbook.getLocation() != null) {
long end = System.currentTimeMillis();
log.append("Fetch of " + addressbook.getLocation() + " took " + (end - start));
}
Iterator<Map.Entry<String, HostTxtEntry>> iter2 = addressbook.iterator();
try {
update(router, knownNames, publishedNS, addressbook, iter2, log);
} finally {
if (iter2 instanceof HostTxtIterator)
((HostTxtIterator) iter2).close();
addressbook.delete();
}
}
// subscriptions
subscriptions.write();
}
Aggregations