boltdb icon indicating copy to clipboard operation
boltdb copied to clipboard

KeyError: 0

Open jpambattu opened this issue 1 year ago • 0 comments

When a db is opened and closed multiple times, like you run the file multiple times not altogether, one after; I am getting a key error.

Traceback (most recent call last): File "/mnt/d/python-based-client/test/test.py", line 11, in tx.commit() File "/home/joseph/.local/lib/python3.12/site-packages/boltdb/tx.py", line 47, in commit self.root.spill() File "/home/joseph/.local/lib/python3.12/site-packages/boltdb/bucket.py", line 185, in spill self.root_node.spill() File "/home/joseph/.local/lib/python3.12/site-packages/boltdb/node.py", line 134, in spill p = self.bucket.tx.allocate((n.size()+tx.db.pagesize-1)//tx.db.pagesize) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joseph/.local/lib/python3.12/site-packages/boltdb/tx.py", line 63, in allocate p = self.db.allocate(n) ^^^^^^^^^^^^^^^^^^^ File "/home/joseph/.local/lib/python3.12/site-packages/boltdb/db.py", line 126, in allocate pgid = self.freelist.allocate(n) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joseph/.local/lib/python3.12/site-packages/boltdb/freelist.py", line 40, in allocate self.cache.remove(i+initial) KeyError: 0

This is the error and I am putting my code below. What I did is I just run this python file multiple times using a shell script.

test.py

from boltdb import BoltDB
from datetime import datetime

db = BoltDB('chat_history.db')


with db.update() as tx:
    # bkt = tx.create_bucket(b"new_chats")  run this only first time
    bkt = tx.bucket(b"new_chats")
    bkt.put(str(datetime.now()).encode('utf-8'), "test".encode('utf-8'))
    tx.commit()


with db.update() as tx:
    bkt = tx.bucket(b"new_chats")

    for k, v in bkt:
        print(k.decode('utf-8'))
        print(v.decode('utf-8'))

    tx.commit()

Shell script is:

a=1
while [ $a -lt 100 ];
do
	python3.12 test.py
  a=$((a+1))
  echo $a
	sleep 1
done

jpambattu avatar Nov 28 '23 12:11 jpambattu