Skip to main content

Python implementation of FoundationDB Record Layer

Project description

⚠️ Disclaimer: This is an AI-generated reimplementation of the original Java FoundationDB Record Layer. It was created using Claude (Anthropic) and has not been officially endorsed by Apple or the FoundationDB team.

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/mirkomikulic/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.0b2.tar.gz (172.0 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.0b2-py3-none-any.whl (181.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fdb_record_layer-0.1.0b2.tar.gz
  • Upload date:
  • Size: 172.0 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.0b2.tar.gz
Algorithm Hash digest
SHA256 b3423681a3e69221759b7645b7a234ac859cd5e59039b39fa78678a7ba33ddb7
MD5 37f2ef90a3b513b4073470220b24aa50
BLAKE2b-256 43fb1003a54176ad0678dfcae08184bddde3e11024631a36c35470ddf137834a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fdb_record_layer-0.1.0b2.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.0b2-py3-none-any.whl.

File metadata

File hashes

Hashes for fdb_record_layer-0.1.0b2-py3-none-any.whl
Algorithm Hash digest
SHA256 637bfc07f52f3f2f97f1cf392007efe028c9f5db1f8f0041f81e8b3cf3b86691
MD5 f3f3d076bc4dab4e506a6415b5651e92
BLAKE2b-256 946640a5b5a6ef0160429af7e74231e1fc605350aff15b5e9a78609408a299a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for fdb_record_layer-0.1.0b2-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