LevelDB Java – Loading LevelDB in Java

leveldb

I am trying to iterate through the ethereum ldb files. When I run this code my iterator believes there is nothing stored on the ropsten's testnet blockchain. There are no errors, and the iter gets to the for loop's test for whether it hasNext(). So it loads the DB properly, just doesn't recognize the keys.

I am using Dain's LevelDB API.

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    Options options = new Options();
    options.createIfMissing(false);
    ReadOptions ROoptions = new ReadOptions();
    try {
        DB db = factory.open(new File("chaindata"), options);
        ROoptions.snapshot(db.getSnapshot());
        try {
            // Use the db in here....
            System.out.println("Creating itr");
            DBIterator iterator = db.iterator(ROoptions);
            try {
                System.out.println("Testing itr");
                for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
                    String key = asString(iterator.peekNext().getKey());
                    String value = asString(iterator.peekNext().getValue());
                    System.out.println(key + " = " + value);
                }
            } finally {
                // Make sure you close the iterator to avoid resource leaks.
                iterator.close();
            }

        } finally {
            // Make sure you close the db to shutdown the
            // database and avoid resource leaks.
            db.close();
        }
    } catch (FileNotFoundException E) {
        System.out.println("File not found! " + E);
        throw new FileNotFoundException();
    }

}

}

Best Answer

Two things were wrong with my code.

  1. The source of the folder was incorrect, so I was not grabbing the right files.

  2. Dain's levelDB only works with .sst files and not .ldb. LevelDB updated because of a conflict with the Windows core files. This caused me issues after I fixed the folder issue.

I hope this helps someone else in the future.

Related Topic