Major refactoring of KDB and KIO, more to come.

KDB TODO: A crash recovery mechanism is needed so that we can ensure
the integrity of the file after interruption in the middle of
committing changes.