Search code examples
pythondatabaseshelve

fix corrupted shelve cache


A shelve that I've created isn't allowing me to access the keys without the following failure.

x = shelve.open('my_shelve.pkl') x.keys()

bsddb.db.DBPageNotFoundError: (-30986, 'BDB0075 DB_PAGE_NOTFOUND: Requested page not found')

However, I am able to check if the Shelf contains a key like so:

'some-key' in x,

and additionally, the Shelf will return the correct data with

x['some-key']

I don't have the list of keys elsewhere, so I'd like to somehow retrieve the keys so I can retrieve the data, or otherwise fix the issue with the database.

I'm using Python 2.7.6


Solution

  • When the database file is damaged (e.g, maybe by failing to call close on it in the past), you probably can't recover all of its contents (the file format just doesn't have enough redundancy to support that).

    However, you could perhaps recover a part of it as follows:

    recov = {}
    try:
        for k in x:
            recov[k] = x[k]
    except Exception:
        pass
    

    It's impossible to predict how many keys (and associated values) you'll be able to recover this way, but at least by not asking for all keys (as I imagine you're doing with x.keys() -- you don't tell us which Python version you're using, but I guess it's 2.something) you might be able to recover some of them...