prefix-register
Status: Beta - API may change before 1.0 release.
A PostgreSQL-backed namespace prefix registry for CURIE expansion and prefix management. Available for both Rust and Python.
Features
- Dual language support - Native Rust library with Python bindings via PyO3
- Async-only - Built on tokio for high concurrency
- In-memory caching - Prefixes loaded on startup for fast CURIE expansion
- First-prefix-wins - Each URI can only have one registered prefix
- Batch operations - Efficiently store multiple prefixes in a single round trip
- PostgreSQL backend - Durable, scalable storage with connection pooling
- Startup resilience - Optional retry with exponential backoff for container orchestration
- Input validation - Prevents DoS via length limits (prefix max 64, URI max 2048 chars)
- Tracing instrumentation - Built-in spans and events for observability (configure subscriber in your app)
Use Cases
- CURIE expansion in RDF processing
- Namespace prefix management for semantic web applications
- Prefix discovery from Turtle, JSON-LD, XML documents
Installation
Rust
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["rt-multi-thread"] }
Python
Requires Python 3.10+.
Database Setup
Create the namespaces table in your PostgreSQL database:
(
uri TEXT PRIMARY KEY,
prefix TEXT NOT NULL UNIQUE
);
Usage
Rust
use PrefixRegistry;
async
Python
# Connect to PostgreSQL
= await
# Store a prefix (only if URI doesn't already have one)
= await
# Expand a CURIE
= await
# Output: foaf:Person = http://xmlns.com/foaf/0.1/Person
# Batch store prefixes
=
= await
# Shorten a URI (longest-match wins)
= await
, =
# Output: foaf:Person
# Or get a formatted string (returns original URI if no match)
= await
# Output: foaf:Person
API
PrefixRegistry
| Method | Description |
|---|---|
new(database_url, max_connections) |
Connect to PostgreSQL and load existing prefixes |
new_with_retry(...) |
Connect with retry logic for transient failures |
get_uri_for_prefix(prefix) |
Get the URI for a prefix (cache-first) |
get_prefix_for_uri(uri) |
Get the prefix for a URI |
store_prefix_if_new(prefix, uri) |
Store a prefix if the URI doesn't have one (returns bool) |
store_prefixes_if_new(prefixes) |
Batch store prefixes |
expand_curie(prefix, local_name) |
Expand a CURIE to a full URI |
expand_curie_batch(curies) |
Batch expand CURIEs (returns None for unknown prefixes) |
shorten_uri(uri) |
Shorten a URI to (prefix, local_name) using longest-match |
shorten_uri_or_full(uri) |
Shorten to "prefix:local" or return original URI |
shorten_uri_batch(uris) |
Batch shorten URIs (returns None for unmatched) |
get_all_prefixes() |
Get all registered prefix mappings |
prefix_count() |
Get the number of registered prefixes |
RetryConfig (Rust) / new_with_retry parameters (Python)
Configuration for startup retry behaviour:
Rust:
RetryConfig::default()- 5 retries, 1s initial delay, 30s max delayRetryConfig::none()- No retries (fail immediately)RetryConfig::new(max_retries, initial_delay, max_delay)- Custom configuration
Python:
= await
BatchStoreResult
Returned by store_prefixes_if_new() for detailed reporting:
Rust:
stored: usize- Number of new prefixes storedskipped: usize- Number of prefixes skipped (URI already had a prefix)total()- Total prefixes processedall_stored()- Returns true if all were storednone_stored()- Returns true if none were stored
Python:
= await
# Number of new prefixes stored
# Number skipped (URI already had a prefix)
License
Apache-2.0