Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of records in lmdb databse with python

Tags:

python

lmdb

I open a lmdb database using this code:

    lmdb_env = lmdb.open(source_path, readonly=True)

How can I count the number of records in this database?

like image 612
trminh89 Avatar asked Dec 22 '15 16:12

trminh89


4 Answers

I think it should be like this:

lmdb_env = lmdb.open(lmdb_file_name, readonly=True)
print lmdb_env.stat()

Then it prints the directory that Jaco pasted here.

like image 186
GieBur Avatar answered Nov 16 '22 11:11

GieBur


    env = lmdb.open('db file path', max_dbs = ' > 0')
    with env.begin() as tx:
        db = env.open_db(b'db name', txn=tx)
        print(env.stat()) 
        print(tx.stat(db)) # this gives stats about one specific db

env.stat() gives entries of the main database. tx.stat(db) gives entries of one named database.

like image 41
zjk Avatar answered Nov 16 '22 11:11

zjk


You can use event.stat(). It will return the following dictionary with entries detailing the number of records in this database:

{'branch_pages': 1040L,
'depth': 4L,
 'entries': 3761848L,
 'leaf_pages': 73658L,
 'overflow_pages': 0L,
 'psize': 4096L}
like image 1
Alex Avatar answered Nov 16 '22 09:11

Alex


I found a simple solution using for loop. Here it is:

count = 0
for key, value in lmdb_env.cursor():
        count = count + 1  

However, I think there should be a better way using pre-defined function.

like image 1
trminh89 Avatar answered Nov 16 '22 10:11

trminh89