Skip to main content

This package provides foundational data structures for representing and manipulating tile maps in 2D and 3D environments. Its primary purpose is to enable efficient spatial organization and management of map data for games, simulations, and applications that require robust handling of coordinates, regions, and chunked regions. By offering specialized classes for coordinates, regions, and chunked regions, the package simplifies the development of systems that need precise and scalable map logic.

Project description

udbcore

The udbcore submodule provides comprehensive database abstractions and utilities for working with DuckDB databases in this project. It offers a clean, robust interface for database management with advanced features like connection pooling, transaction management, query building, schema management, and health monitoring.

Contents

Core Components

  • db.py: Defines the DB base class for generic database file management
  • ddb.py: Defines the enhanced DuckDB class with retry logic, health monitoring, and advanced operations

Advanced Features

  • connection_pool.py: Connection pooling utilities for efficient database connection management
  • transactions.py: Transaction management and batch operation utilities
  • query_builder.py: Fluent SQL query builder with support for complex queries
  • schema.py: Database schema management utilities for table/index creation and migration
  • monitoring.py: Database health monitoring and performance statistics

Quick Start

from foundation_packages.udbcore import DuckDB

# Create a DuckDB database object
my_db = DuckDB(path='path/to/database.duckdb', name='database.duckdb')

# Connect and run a query
my_db.connect()
results = my_db.run_query('SELECT * FROM my_table;')
my_db.disconnect()

Core Features

Enhanced DuckDB Class

The DuckDB class provides robust database operations with automatic retry logic and health monitoring:

# Initialize with retry configuration
db = DuckDB('data.duckdb', 'data.duckdb', retry_attempts=3, retry_delay=1.0)

# Execute queries with automatic retry
results = db.run_query("SELECT * FROM users WHERE age > ?", [18])
single_result = db.run_query_single("SELECT COUNT(*) FROM users")

# Execute non-query operations
rows_affected = db.execute_non_query("INSERT INTO users (name, age) VALUES (?, ?)", ['John', 25])

# Batch operations
db.execute_many("INSERT INTO users (name, age) VALUES (?, ?)", 
                [['Alice', 30], ['Bob', 35], ['Carol', 28]])

# Transaction support
with db.transaction():
    db.execute_non_query("INSERT INTO accounts (user_id, balance) VALUES (?, ?)", [1, 100])
    db.execute_non_query("UPDATE users SET account_created = true WHERE id = ?", [1])

Connection Pooling

Efficient connection management for multiple databases:

from foundation_packages.udbcore import ConnectionPool, get_global_pool

# Use global connection pool
pool = get_global_pool()
db = pool.get_connection('/path/to/database.duckdb')
results = db.run_query("SELECT * FROM my_table")
pool.return_connection('/path/to/database.duckdb')

# Create custom pool
custom_pool = ConnectionPool(max_connections=20, connection_timeout=600)
db = custom_pool.get_connection('/path/to/another_db.duckdb')

Transaction Management

Advanced transaction control with automatic rollback:

from foundation_packages.udbcore import TransactionManager, BatchOperationManager

# Transaction management
db = DuckDB('data.duckdb', 'data.duckdb')
tm = TransactionManager(db)

# Manual transaction control
tm.begin_transaction()
try:
    db.execute_non_query("INSERT INTO table1 VALUES (?)", [value1])
    db.execute_non_query("INSERT INTO table2 VALUES (?)", [value2])
    tm.commit()
except Exception:
    tm.rollback()

# Context manager (automatic)
with tm.transaction():
    db.execute_non_query("INSERT INTO table1 VALUES (?)", [value1])
    db.execute_non_query("INSERT INTO table2 VALUES (?)", [value2])

# Batch operations
bm = BatchOperationManager(db, batch_size=1000)
data = [['Alice', 25], ['Bob', 30], ['Carol', 28]]
rows_inserted = bm.batch_insert('users', ['name', 'age'], data)

Query Builder

Fluent interface for building complex SQL queries:

from foundation_packages.udbcore import QueryBuilder, JoinType, OrderDirection

qb = QueryBuilder()

# Simple query
query = (qb.reset()
         .select('name', 'age', 'email')
         .from_table('users')
         .where('age > 18')
         .order_by('name', OrderDirection.ASC)
         .limit(10)
         .build())

# Complex query with joins
query = (qb.reset()
         .select('u.name', 'p.title', 'c.name')
         .from_table('users u')
         .join('posts p', 'p.user_id = u.id', JoinType.LEFT)
         .join('categories c', 'c.id = p.category_id', JoinType.INNER)
         .where('u.active = true')
         .where_in('c.name', ['Tech', 'Science'])
         .group_by('u.id')
         .having('COUNT(p.id) > 5')
         .order_by('u.name')
         .build())

# Execute the query
results = db.run_query(query)

Schema Management

Programmatic database schema creation and management:

from foundation_packages.udbcore import (SchemaManager, TableDefinition, 
                                       ColumnDefinition, IndexDefinition, ColumnType)

# Initialize schema manager
db = DuckDB('data.duckdb', 'data.duckdb')
sm = SchemaManager(db)

# Define table structure
columns = [
    ColumnDefinition('id', ColumnType.INTEGER, primary_key=True),
    ColumnDefinition('name', ColumnType.VARCHAR, nullable=False),
    ColumnDefinition('email', ColumnType.VARCHAR, unique=True),
    ColumnDefinition('age', ColumnType.INTEGER, default=0),
    ColumnDefinition('created_at', ColumnType.TIMESTAMP, default='CURRENT_TIMESTAMP')
]

indexes = [
    IndexDefinition('idx_users_email', 'users', ['email'], unique=True),
    IndexDefinition('idx_users_age', 'users', ['age'])
]

table_def = TableDefinition('users', columns, indexes)

# Create table
sm.create_table(table_def)

# Schema operations
sm.add_column('users', ColumnDefinition('last_login', ColumnType.TIMESTAMP))
sm.create_index(IndexDefinition('idx_users_name', 'users', ['name']))

# Schema inspection
tables = sm.list_tables()
schema_info = sm.get_table_schema('users')
exists = sm.table_exists('users')

Health Monitoring

Comprehensive database health monitoring and performance statistics:

from foundation_packages.udbcore import HealthMonitor

# Get health monitor from database
db = DuckDB('data.duckdb', 'data.duckdb')
monitor = db.get_health_monitor()

# Get statistics
query_stats = monitor.get_query_statistics()
db_stats = monitor.get_database_statistics()
table_stats = monitor.get_table_statistics()

# Health check
health = monitor.health_check()
print(f"Database status: {health['overall_status']}")

# Export comprehensive stats
all_stats = monitor.export_statistics()

# Find slow queries
slow_queries = monitor.get_slow_queries(threshold_seconds=2.0)

Advanced Usage Examples

Tilemap Region Database Management

from foundation_packages.udbcore import DuckDB, SchemaManager, ColumnDefinition, ColumnType

# Create region database with proper schema
region_db = DuckDB(f'regions/region_{rx}_{ry}.duckdb', f'region_{rx}_{ry}.duckdb')
schema_manager = SchemaManager(region_db)

# Define cells table schema
cells_columns = [
    ColumnDefinition('x', ColumnType.INTEGER, nullable=False),
    ColumnDefinition('y', ColumnType.INTEGER, nullable=False),
    ColumnDefinition('z', ColumnType.INTEGER, nullable=False),
    ColumnDefinition('tile', ColumnType.INTEGER, default=0),
    ColumnDefinition('properties', ColumnType.JSON)
]

# Create spatial indexes for efficient lookups
spatial_indexes = [
    IndexDefinition('idx_cells_xyz', 'cells', ['x', 'y', 'z'], unique=True),
    IndexDefinition('idx_cells_xy', 'cells', ['x', 'y']),
    IndexDefinition('idx_cells_tile', 'cells', ['tile'])
]

table_def = TableDefinition('cells', cells_columns, spatial_indexes)
schema_manager.create_table(table_def)

# Efficient batch cell operations
batch_manager = BatchOperationManager(region_db, batch_size=10000)

# Insert many cells efficiently
cell_data = [(x, y, z, tile_id) for x in range(128) for y in range(128) for z in range(128)]
batch_manager.batch_insert('cells', ['x', 'y', 'z', 'tile'], cell_data)

Multi-Database Operations

# Use connection pool for multiple region databases
pool = get_global_pool()

regions_to_process = [(0, 0), (0, 1), (1, 0), (1, 1)]

for rx, ry in regions_to_process:
    db_path = f'regions/region_{rx}_{ry}.duckdb'
    db = pool.get_connection(db_path)
    
    # Process each region
    with db.transaction():
        # Update tiles based on some logic
        updated_tiles = db.run_query(
            "SELECT x, y, z FROM cells WHERE tile = ? AND z > ?", 
            [old_tile_id, min_height]
        )
        
        if updated_tiles:
            db.execute_non_query(
                "UPDATE cells SET tile = ? WHERE tile = ? AND z > ?",
                [new_tile_id, old_tile_id, min_height]
            )
    
    pool.return_connection(db_path)

Requirements

Version History

  • v1.0.0 - Initial release

License

See the main project for license information.

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

udbcore-1.1.1.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

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

udbcore-1.1.1-py3-none-any.whl (33.3 kB view details)

Uploaded Python 3

File details

Details for the file udbcore-1.1.1.tar.gz.

File metadata

  • Download URL: udbcore-1.1.1.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.8

File hashes

Hashes for udbcore-1.1.1.tar.gz
Algorithm Hash digest
SHA256 b670160d1bf0c2f6860ac0d9946484bc477cece12fb03c942fcd386247015e59
MD5 be5e3fef98d140c6f4fafff18fb78551
BLAKE2b-256 a5127dbad268455d7e75ab6fc8ee3adb44f73f837c5985b749dd10a194df0b16

See more details on using hashes here.

File details

Details for the file udbcore-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: udbcore-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 33.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.8

File hashes

Hashes for udbcore-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d042b45e047789189e5d7273d670030a2586cd92e991f77b3f6f47bf9f191b33
MD5 0a20923cc9e6388bcf1b7923b531121f
BLAKE2b-256 fad7f99ac8de65809d82bf429289b69dc3af6d90a6a94798971f04b5cefc061a

See more details on using hashes here.

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