Universal Python binding for the LMDB 'Lightning' Database
—
Essential environment and database management functionality for LMDB. This includes opening and configuring databases, environment management, synchronization operations, and basic database introspection.
Opens or creates LMDB environments with extensive configuration options for performance tuning and operational requirements.
def open(path: str, map_size: int = 10485760, subdir: bool = True, readonly: bool = False,
metasync: bool = True, sync: bool = True, map_async: bool = False, mode: int = 0o755,
create: bool = True, readahead: bool = True, writemap: bool = False,
meminit: bool = True, max_readers: int = 126, max_dbs: int = 0,
max_spare_txns: int = 1, lock: bool = True) -> Environment:
"""
Open an LMDB environment.
Parameters:
- path: Directory (if subdir=True) or file prefix for database files
- map_size: Maximum size of memory map (default 10MB)
- subdir: If True, path is directory; if False, path is file prefix
- readonly: Open environment in read-only mode
- metasync: Flush system buffers to disk when committing transactions
- sync: Flush buffers to disk immediately on transaction commit
- map_async: Use asynchronous flushes when sync=True (Unix only)
- mode: File permissions for created files (Unix only)
- create: Create database if it doesn't exist
- readahead: Use read-ahead prefetching
- writemap: Use writeable memory map
- meminit: Initialize memory pages to zeros
- max_readers: Maximum number of read-only transactions
- max_dbs: Maximum number of named databases (0 = default database only)
- max_spare_txns: Number of transaction slots to cache for reuse
- lock: Enable file locking
Returns:
Environment instance
"""Core environment operations for lifecycle management, synchronization, and introspection.
class Environment:
def close(self) -> None:
"""
Close environment, invalidating all open iterators, cursors, and transactions.
Subsequent calls to close() have no effect.
"""
def sync(self, force: bool = False) -> None:
"""
Flush environment buffers to disk.
Parameters:
- force: Force synchronous flush (may be expensive)
"""
def set_mapsize(self, map_size: int) -> None:
"""
Change maximum size of memory map. Fails if any transactions are active.
Parameters:
- map_size: New size in bytes
Warning:
Contains data race - use external locking if multiple processes access database
"""
def path(self) -> str:
"""
Get directory path or file prefix where environment is stored.
Returns:
Environment path string
"""
def copy(self, path: str, compact: bool = False, txn=None) -> None:
"""
Make consistent copy of environment in destination directory.
Parameters:
- path: Destination directory path
- compact: Omit free pages and renumber pages (slower but smaller)
- txn: Optional transaction for consistent snapshot
"""
def copyfd(self, fd: int, compact: bool = False, txn=None) -> None:
"""
Copy environment to file descriptor.
Parameters:
- fd: Target file descriptor
- compact: Omit free pages and renumber pages (slower but smaller)
- txn: Optional transaction for consistent snapshot
"""Retrieve environment configuration, usage statistics, and operational metadata.
class Environment:
def stat(self) -> dict:
"""
Get environment statistics.
Returns:
Dictionary with keys:
- psize: Page size in bytes
- depth: B-tree depth
- branch_pages: Number of internal pages
- leaf_pages: Number of leaf pages
- overflow_pages: Number of overflow pages
- entries: Number of data items
"""
def info(self) -> dict:
"""
Get environment information.
Returns:
Dictionary with keys:
- mapaddr: Address of memory map (if fixed)
- mapsize: Size of memory map
- last_pgno: Last used page number
- last_txnid: Last transaction ID
- maxreaders: Maximum readers allowed
- numreaders: Current number of readers
"""
def flags(self) -> dict:
"""
Get environment flags.
Returns:
Dictionary of boolean flags showing environment configuration
"""
def max_key_size(self) -> int:
"""
Get maximum key size supported by environment.
Returns:
Maximum key size in bytes
"""
def max_readers(self) -> int:
"""
Get maximum number of readers.
Returns:
Maximum reader count
"""
def readers(self) -> str:
"""
Get reader information as formatted string.
Returns:
Multi-line string with reader slot information
"""
def reader_check(self) -> int:
"""
Check for stale reader slots and clear them.
Returns:
Number of stale readers cleared
"""Create and configure named databases within environments for data organization and isolation.
class Environment:
def open_db(self, key: bytes = None, txn=None, reverse_key: bool = False,
dupsort: bool = False, create: bool = True, integerkey: bool = False,
integerdup: bool = False, dupfixed: bool = False) -> _Database:
"""
Open named database within environment.
Parameters:
- key: Database name (None for default database)
- txn: Transaction to use (creates temporary if None)
- reverse_key: Keys are strings in reverse order
- dupsort: Duplicate keys allowed and sorted
- create: Create database if it doesn't exist
- integerkey: Keys are C integers (native byte order)
- integerdup: Duplicate values are C integers (requires dupsort=True)
- dupfixed: Duplicate values are fixed-size (requires dupsort=True)
Returns:
Database handle for use in transactions
"""import lmdb
# Open environment with custom configuration
env = lmdb.open('/path/to/database',
map_size=100*1024*1024, # 100MB
max_dbs=10, # Support 10 named databases
max_readers=50) # Support 50 concurrent readers
# Get environment information
print("Map size:", env.info()['mapsize'])
print("Max key size:", env.max_key_size())
# Synchronize to disk
env.sync()
# Copy database for backup
env.copy('/path/to/backup')
# Close environment
env.close()import lmdb
# Open environment with multiple database support
env = lmdb.open('/path/to/database', max_dbs=5)
# Create named databases
users_db = env.open_db(b'users')
products_db = env.open_db(b'products')
orders_db = env.open_db(b'orders', dupsort=True) # Allow duplicate keys
# Use databases in transactions
with env.begin(write=True) as txn:
txn.put(b'user1', b'John Doe', db=users_db)
txn.put(b'prod1', b'Widget', db=products_db)
txn.put(b'user1', b'order123', db=orders_db) # Duplicate key allowed
txn.put(b'user1', b'order456', db=orders_db)
env.close()Install with Tessl CLI
npx tessl i tessl/pypi-lmdb