Skip to main content

ElectrumSV Database

Licence: MIT License
Maintainers: Roger Taylor, AustEcon
Project Lead: Roger Taylor
Homepage: https://electrumsv.io/

Overview

This is the database support library for ElectrumSV. This functionality has been extracted into an independent package so that it can be used by other projects.

Usage

Reads

It is envisioned that most reads will be done with the aid of the replace_db_context_with_connection decorator. The calling logic will have a reference to a database context, and the decorator will inject a database connection as the first argument to the wrapped function. These can happen inline in the calling context.

If a read query is one that will take more than a nominal amount of time, the developer should use worker threads to farm out the query. There is a good argument that we should add that to this library in order to deal with the typing complications.

Writes

SQLite has a well known limitation in that only one connection can be making changes, or writes, at a time. What this package does is use one writer thread to apply write queries sequentially through it's connection. This is all managed as part of the DatabaseContext class, which creates the SqliteWriteDispatcher and SqliteExecutor for you.

Creating a database context:

from electrumsv_database import DatabaseContext
database_context = DatabaseContext(database_path)

Block executing a writer callback as a transaction:

def write(a: int, s: str, db: Optional[sqlite3.Connection]=None) -> str:
    assert db is not None and isinstance(db, sqlite3.Connection)
    db.execute("INSERT INTO SomeTable(a, s) VALUES (?, ?)", (a, s))
    return "whatever return value"

s = database_context.run_in_thread(write, 5, "test")
assert s == "whatever return value"

Post a writer callback to be executed as a transaction:

def write(a: int, s: str, db: Optional[sqlite3.Connection]=None) -> str:
    assert db is not None and isinstance(db, sqlite3.Connection)
    db.execute("INSERT INTO SomeTable(a, s) VALUES (?, ?)", (a, s))
    return "whatever return value"

future = database_context.post_to_thread(write, 5, "test")
# Perform whatever other logic.
s = future.result()
assert s == "whatever return value"

Asynchronously block executing a writer callback as a transaction:

def write(a: int, s: str, db: Optional[sqlite3.Connection]=None) -> str:
    assert db is not None and isinstance(db, sqlite3.Connection)
    db.execute("INSERT INTO SomeTable(a, s) VALUES (?, ?)", (a, s))
    return "whatever return value"

s = await database_context.run_in_thread_async(write, 5, "test")
assert s == "whatever return value"

Typing

Python has flawed problematic typing. It is very easy to have code that is wrong and not being checked, but be unaware of it. This package makes various choices to try and ensure that all of it's operations are typed.

Write functions

Queries that do write operations are executed using callbacks, and this means that we want to check the types of the arguments in the application logic. We use ParamSpec for this, but it has a limitation in that the typing of its args and kwargs attributes are atomic.

P1 = ParamSpec("P1")
T1 = TypeVar("T1")

    async def run_in_thread_async(self, func: Callable[P1, T1], *args: P1.args, \
            **kwargs: P1.kwargs) -> T1:
        ...

It is not possible to remove or add arguments to take into account perhaps extra ones added in the writer thread - like a reference to the database connection which the write callback should use to execute it's query. For this reason we use the following pattern, the write callback adds an optional db keyword argument to the end of it's argument list, the write dispatcher provides that adding it as an extra argument over the one the application provided.

The following pattern should be used:

def set_payment_channel_closed(channel_id: int, channel_state: ChannelState,
        db: Optional[sqlite3.Connection]=None) -> None:
    assert db is not None and isinstance(db, sqlite3.Connection)
    ...

Metadata

Release files for electrumsv-database 1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for electrumsv-database 1.7
File Interpreter ABI Platform
electrumsv_database-1.7-py3-none-any.whl Python 3 none any Details

Release files / electrumsv_database-1.7-py3-none-any.whl

Download URL electrumsv_database-1.7-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2fa89c0404c30c0d4ef6f250eff37fc01eb370cd0483e436385ef9aaeadaadc
BLAKE2b-256 checksum
How to use checksums
7692ec42d96faaf3aa3cf09c305e5ea8d2381ddf08a7ea13803b3e046ccc5e06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.2

Release history Release notifications | RSS feed

This release

1.7 This release

1 release file

1.6

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page