Search in sources :

Example 1 with BlockFile

use of net.metanotion.io.block.BlockFile in project i2p.i2p by i2p.

the class BlockfileNamingService method initNew.

/**
 *  Create a new database and initialize it from the local files
 *  privatehosts.txt, userhosts.txt, and hosts.txt,
 *  creating a skiplist in the database for each.
 */
private BlockFile initNew(RAIFile f) throws IOException {
    long start = _context.clock().now();
    _version = VERSION;
    _destSerializer = _destSerializerV4;
    _isVersion4 = true;
    try {
        BlockFile rv = new BlockFile(f, true);
        SkipList<String, Properties> hdr = rv.makeIndex(INFO_SKIPLIST, _stringSerializer, _infoSerializer);
        Properties info = new Properties();
        info.setProperty(PROP_VERSION, VERSION);
        info.setProperty(PROP_CREATED, Long.toString(_context.clock().now()));
        String list = _context.getProperty(HostsTxtNamingService.PROP_HOSTS_FILE, HostsTxtNamingService.DEFAULT_HOSTS_FILE);
        info.setProperty(PROP_LISTS, list);
        hdr.put(PROP_INFO, info);
        rv.makeIndex(REVERSE_SKIPLIST, _hashIndexSerializer, _infoSerializer);
        int total = 0;
        for (String hostsfile : getFilenames(list)) {
            _lists.add(hostsfile);
            File file = new File(_context.getRouterDir(), hostsfile);
            if ((!file.exists()) || !(file.canRead()))
                continue;
            int count = 0;
            BufferedReader in = null;
            String sourceMsg = "Imported from " + hostsfile + " file";
            try {
                in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), 16 * 1024);
                String line = null;
                while ((line = in.readLine()) != null) {
                    if (line.startsWith("#"))
                        continue;
                    int split = line.indexOf('=');
                    if (split <= 0)
                        continue;
                    String key = line.substring(0, split).toLowerCase(Locale.US);
                    if (line.indexOf('#') > 0) {
                        // trim off any end of line comment
                        line = line.substring(0, line.indexOf('#')).trim();
                        if (line.length() < split + 1)
                            continue;
                    }
                    String b64 = line.substring(split + 1).trim();
                    Destination d = lookupBase64(b64);
                    if (d != null) {
                        addEntry(rv, hostsfile, key, d, sourceMsg);
                        addReverseEntry(rv, key, d, _log);
                        count++;
                    } else {
                        _log.logAlways(Log.WARN, "Unable to import entry for " + key + " from file " + file + " - bad Base 64: " + b64);
                    }
                }
            } catch (IOException ioe) {
                _log.error("Failed to read hosts from " + file, ioe);
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ioe) {
                    }
            }
            total += count;
            _log.logAlways(Log.INFO, "Migrating " + count + " hosts from " + file + " to new hosts database");
        }
        if (_log.shouldLog(Log.INFO))
            _log.info("DB init took " + DataHelper.formatDuration(_context.clock().now() - start));
        if (total <= 0)
            _log.logAlways(Log.WARN, "No hosts.txt files found, Initialized hosts database with zero entries");
        return rv;
    } catch (RuntimeException e) {
        _log.error("Failed to initialize database", e);
        throw new IOException(e.toString());
    }
}
Also used : Destination(net.i2p.data.Destination) InputStreamReader(java.io.InputStreamReader) BlockFile(net.metanotion.io.block.BlockFile) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) RAIFile(net.metanotion.io.RAIFile) BlockFile(net.metanotion.io.block.BlockFile) File(java.io.File)

Example 2 with BlockFile

use of net.metanotion.io.block.BlockFile in project i2p.i2p by i2p.

the class BlockfileNamingService method initExisting.

/**
 *  Read the info block of an existing database.
 */
private BlockFile initExisting(RAIFile raf) throws IOException {
    long start = _context.clock().now();
    try {
        BlockFile bf = new BlockFile(raf, false);
        // TODO all in one skiplist or separate?
        SkipList<String, Properties> hdr = bf.getIndex(INFO_SKIPLIST, _stringSerializer, _infoSerializer);
        if (hdr == null)
            throw new IOException("No db header");
        Properties info = hdr.get(PROP_INFO);
        if (info == null)
            throw new IOException("No header info");
        String list = info.getProperty(PROP_LISTS);
        if (list == null)
            throw new IOException("No lists");
        long createdOn = 0;
        String created = info.getProperty(PROP_CREATED);
        if (created != null) {
            try {
                createdOn = Long.parseLong(created);
            } catch (NumberFormatException nfe) {
            }
        }
        String version = info.getProperty(PROP_VERSION);
        if (version == null)
            throw new IOException("No version");
        if (VersionComparator.comp(version, VERSION) > 0)
            throw new IOException("Database version is " + version + " but this implementation only supports versions 1-" + VERSION + " Did you downgrade I2P??");
        _version = version;
        if (VersionComparator.comp(version, "4") >= 0) {
            _destSerializer = _destSerializerV4;
            _isVersion4 = true;
        }
        _needsUpgrade = needsUpgrade(bf);
        if (_needsUpgrade) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Upgrading database from version " + _version + " to " + VERSION + ", created " + (new Date(createdOn)).toString() + " containing lists: " + list);
        } else {
            if (_log.shouldLog(Log.INFO))
                _log.info("Found database version " + _version + " created " + (new Date(createdOn)).toString() + " containing lists: " + list);
        }
        List<String> skiplists = getFilenames(list);
        if (skiplists.isEmpty())
            skiplists.add(FALLBACK_LIST);
        _lists.addAll(skiplists);
        if (_log.shouldLog(Log.INFO))
            _log.info("DB init took " + DataHelper.formatDuration(_context.clock().now() - start));
        return bf;
    } catch (RuntimeException e) {
        _log.error("Failed to initialize database", e);
        throw new IOException(e.toString());
    }
}
Also used : BlockFile(net.metanotion.io.block.BlockFile) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date)

Aggregations

IOException (java.io.IOException)2 Properties (java.util.Properties)2 BlockFile (net.metanotion.io.block.BlockFile)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Date (java.util.Date)1 Destination (net.i2p.data.Destination)1 RAIFile (net.metanotion.io.RAIFile)1