Skip to main content

Python implementation of FoundationDB Record Layer

Project description

FDB Record Layer for Python

A Python implementation of the FoundationDB Record Layer, providing a structured record-oriented store with secondary indexes, query planning, and SQL support on top of FoundationDB.

Features

  • Record Store: Type-safe storage and retrieval of Protocol Buffer messages
  • Secondary Indexes: Automatic index maintenance with VALUE, COUNT, RANK, and TEXT index types
  • Query System: Declarative query API with cost-based optimization (Cascades planner)
  • SQL Support: SQL parsing and execution with full DML/DDL support
  • Async/Await: Native Python asyncio support throughout
  • Production Ready: Connection pooling, circuit breakers, health checks, graceful shutdown

Installation

pip install fdb-record-layer

For SQL support:

pip install fdb-record-layer[sql]

For all optional dependencies:

pip install fdb-record-layer[all]

Requirements

  • Python 3.10+
  • FoundationDB 7.1+
  • Protocol Buffers 3.20+

Quick Start

Define Your Schema

from fdb_record_layer import RecordMetaDataBuilder, Index, IndexTypes
from your_proto_pb2 import Person  # Your protobuf message

# Build metadata with indexes
metadata = (
    RecordMetaDataBuilder()
    .add_record_type(Person)
    .add_index(Index("person_by_name", "name"))
    .add_index(Index("person_by_age", "age", index_type=IndexTypes.VALUE))
    .build()
)

Store and Query Records

from fdb_record_layer import FDBDatabase, FDBRecordStore

async def main():
    # Connect to FoundationDB
    db = FDBDatabase.open()

    async with db.transaction() as tr:
        # Open record store
        store = await FDBRecordStore.open(tr, metadata, key_space_path=("myapp",))

        # Save a record
        person = Person(id=1, name="Alice", age=30)
        await store.save_record(person)

        # Query by index
        async for record in store.scan_index("person_by_name", equals="Alice"):
            print(f"Found: {record.name}")

Using the Query Builder

from fdb_record_layer.query import Query, Field

# Build a query
query = (
    Query.from_type("Person")
    .where(Field("age").greater_than(25))
    .where(Field("name").starts_with("A"))
    .build()
)

# Execute
async for record in store.execute_query(query):
    print(record)

SQL Queries

from fdb_record_layer.relational import RelationalDatabase

async def main():
    db = RelationalDatabase.open()

    # Execute SQL
    result = await db.execute("""
        SELECT name, age FROM Person
        WHERE age > 25
        ORDER BY name
    """)

    async for row in result:
        print(f"{row['name']}: {row['age']}")

Key Expressions

Key expressions define how to extract keys from records for indexing:

from fdb_record_layer.expressions import field, concat, record_type

# Simple field
field("name")

# Composite key
concat(field("last_name"), field("first_name"))

# Nested field
field("address").nest("city")

# Include record type in key (for union indexes)
concat(record_type(), field("id"))

Index Types

Type Description Use Case
VALUE Standard B-tree index Equality and range queries
COUNT Aggregate count index Fast COUNT(*) queries
SUM Aggregate sum index Fast SUM() queries
RANK Skip-list based ranking Leaderboards, percentiles
TEXT Full-text search Text search with tokenization

Production Features

Connection Pooling

from fdb_record_layer.utils import ConnectionPool

pool = ConnectionPool(min_size=5, max_size=20)
async with pool.acquire() as conn:
    # Use connection
    pass

Circuit Breaker

from fdb_record_layer.utils import get_circuit_breaker

breaker = get_circuit_breaker("fdb")
async with breaker:
    await store.save_record(record)

Health Checks

from fdb_record_layer.utils import get_health_checker

checker = get_health_checker()
report = await checker.check_health()
print(f"Status: {report.status}")

Graceful Shutdown

from fdb_record_layer.utils import init_lifecycle

lifecycle = init_lifecycle()
# Handles SIGTERM/SIGINT, drains connections, runs cleanup hooks

Documentation

Development

# Clone and install
git clone https://github.com/foundationdb/fdb-record-layer-python
cd fdb-record-layer-python
pip install -e .[dev]

# Run tests
pytest

# Type checking
mypy fdb_record_layer

# Linting
ruff check fdb_record_layer

License

Apache License 2.0. See LICENSE for details.

Contributing

Contributions are welcome! Please read our contributing guidelines and submit pull requests.

Acknowledgments

This project is a Python port of the FoundationDB Record Layer, originally developed by Apple.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fdb_record_layer-0.1.0b1.tar.gz (171.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fdb_record_layer-0.1.0b1-py3-none-any.whl (181.1 kB view details)

Uploaded Python 3

File details

Details for the file fdb_record_layer-0.1.0b1.tar.gz.

File metadata

  • Download URL: fdb_record_layer-0.1.0b1.tar.gz
  • Upload date:
  • Size: 171.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fdb_record_layer-0.1.0b1.tar.gz
Algorithm Hash digest
SHA256 fe0a11cfffb63dad475db23499891bf0eafb2ad17c335d8dfebe0b26f47fd2ea
MD5 2defd47182e21dfb2cf116d21f99054a
BLAKE2b-256 e249f64e6f5a9dcdf18faa26e1a2b591018d0787733af0c7d6e4d744f6fe078f

See more details on using hashes here.

Provenance

The following attestation bundles were made for fdb_record_layer-0.1.0b1.tar.gz:

Publisher: release.yml on mirkomikulic/fdb-record-layer-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fdb_record_layer-0.1.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for fdb_record_layer-0.1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f9f2b8add1ae28ce0edc95dc8af4274f1956abc116b7c80b810a94acf44f558
MD5 62073802bd8fa427cf1635a694318dc8
BLAKE2b-256 17555e3f14a2049f180ad7d04cf85af404836e84392c88655bcc8dacfb4e95de

See more details on using hashes here.

Provenance

The following attestation bundles were made for fdb_record_layer-0.1.0b1-py3-none-any.whl:

Publisher: release.yml on mirkomikulic/fdb-record-layer-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page