Count number of records in lmdb databse with python
Asked Answered
E

4

8

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?

Eating answered 22/12, 2015 at 16:32 Comment(1)
I don't understand the close votes. Just because a question is short or basic, that doesn't mean it's too broad or unclear. This is a straightforward, specific question about coding that can have a definite correct answer, and I see no reason why it's inappropriate for Stack Overflow's format. I'm voting to reopen. I respectfully ask in advance that anyone who votes to leave closed please post a comment explaining why.Pewter
E
1

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.

Eating answered 24/12, 2015 at 11:40 Comment(1)
sum(1 for _ in lmdb_env.cursor() )Alten
B
8

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.

Bluefield answered 9/3, 2016 at 20:16 Comment(0)
A
3
    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.

Allier answered 3/11, 2020 at 8:48 Comment(0)
P
1

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}
Phia answered 22/12, 2015 at 16:42 Comment(0)
E
1

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.

Eating answered 24/12, 2015 at 11:40 Comment(1)
sum(1 for _ in lmdb_env.cursor() )Alten

© 2022 - 2024 — McMap. All rights reserved.