LMDB Python Binding

2.2.0 · active · verified Thu Apr 09

lmdb (py-lmdb) is a universal Python binding for the LMDB 'Lightning' Database, a fast, memory-efficient, embedded key-value store. It provides an ordered map interface, multi-version concurrency control (MVCC) with reader/writer transactions, and utilizes memory-mapped files for zero-copy operations. The library is actively maintained, with frequent releases; the current version is 2.2.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open an LMDB environment, write key-value pairs, read specific values, iterate through all entries using a cursor, and properly close the environment. Note the use of byte strings for keys and values, and the importance of `map_size` and `max_dbs` configuration. The example also includes basic cleanup.

import lmdb
import os
import shutil

db_path = './my_lmdb_data'

# Ensure cleanup for demonstration
if os.path.exists(db_path):
    shutil.rmtree(db_path)

# 1. Open an LMDB environment
# map_size: Maximum size of the database. Crucial to set correctly.
# max_dbs: Max number of named databases (sub-databases).
env = lmdb.open(db_path, map_size=10*1024*1024, max_dbs=10, subdir=True)

# 2. Write data to the database
with env.begin(write=True) as txn:
    txn.put(b'my_key_1', b'my_value_1')
    txn.put(b'my_key_2', b'my_value_2')
    txn.put(b'another_key', b'another_value')
    print("Wrote data to LMDB.")

# 3. Read data from the database
with env.begin() as txn:
    value1 = txn.get(b'my_key_1')
    value_non_existent = txn.get(b'non_existent_key')
    print(f"Value for my_key_1: {value1.decode('utf-8') if value1 else None}")
    print(f"Value for non_existent_key: {value_non_existent}")

    # Iterate through all key-value pairs
    print("\nAll items in DB:")
    for key, value in txn.cursor():
        print(f"  {key.decode('utf-8')}: {value.decode('utf-8')}")

# 4. Close the environment
env.close()
print("\nLMDB environment closed.")

# Cleanup (optional, for examples)
if os.path.exists(db_path):
    shutil.rmtree(db_path)
    print(f"Cleaned up database at {db_path}")

view raw JSON →