Search code examples
python-3.xshelve

How can I recreate 'dir' for shelve db


Everyday I store data to one shelve file . Today I discovered some last keys missing. The dat-file size is not changed. The dir file and the bak file contains all keys except some last keys. How can I recreate this files, or how can I rescan my dat file and restore all stored items?


Solution

  • I solved my problem by hex editor and hands. There is simple steps:

    1. open dat file with hex editor.

    2. open our dir file

    3. find data offset. It should be 0. This is offset 1. Fig 1

    4. find block of ten zero and find the offset at start of this block. This is offset 2. Fig 2

    5. substract offset 1 from offset 2. In my case it be 71768182 - 0 = 71768182. This is data length.

    6. We should write to dir file: '1', (0, 71768182)

      • 1 - key of this item in our db.

      • 0 - offset of start datablock

      • 71768182 - length of datablock;

    7. Go to start of the new datablock. Fig 3

    8. Go to step 3