summaryrefslogtreecommitdiff
path: root/crates/tor-dirserver/src/database.rs
Commit message (Collapse)AuthorAgeFilesLines
* tor-dirserver: Add comment on compression bugClara Engler2026-01-221-0/+4
| | | | | This commit adds a comment explaining why we treat compression errors as a bug.
* tor-dirserver: Make compression failures a bugClara Engler2026-01-221-1/+2
| | | | | | | | | | | | | | This commit removes `DatabaseError::Compression` because it does not fit in. Right now, this single variant makes the error to be call-site oriented which is not nice for error handling. Instead, this error should indicate that something was truly wrong with the database in itself, such as an invalid schema, a low-level SQLite bug, etc. Instead, we now map a compression error to `DatabaseError::Bug` because there is no good reason on why it should fail, given that we compress memory data to memory data. Probably because it uses the `std::io::Writer` interface which itself demands use of `std::io::Result`.
* tor-dirserver: Remove FromStr for DocumentIdClara Engler2026-01-221-10/+5
| | | | | | | | | | | This commit removes FromStr for DocumentId because it was only used in testing anyways. Instead, it replaces it with a simple From<[u8; 32]> only enabled in test builds, which is sufficient for what we are trying to do. An alternative would be to make the inner field pub, but this seems to aggressive for a testing only thing.
* tor-dirserver: Add `sha2` to `router_descriptor`Clara Engler2026-01-191-0/+2
| | | | | | | | | | This comit adds a `sha2` column to `router_descriptor` alongside a `CHECK` to see whether it equals `docid`. The reason for this is simple: Microdescriptors are the only kind of documents that are queriable with a SHA2 hash. Previously, we would have simply used the `docid` column for this, but in order to abstract it better, a distinct column with this hash is better.
* tor-dirserver: Rename `doc_id` to `docid`Clara Engler2026-01-191-14/+13
| | | | | Because we went with `docid` in the database (due to `rowid`), it is only natural to call the code variables `docid` too.
* tor-dirserver: Rename sha256 to docid in schemaClara Engler2026-01-191-31/+31
| | | | | | This commit renames the sha256 column to docid for the reason that we agreed upon making the schema visible to all modules, so if we were to encapsulate docid properly, this change is only natural.
* tor-dirserver: Rename database meta tableClara Engler2026-01-191-16/+13
| | | | | This commit renames arti_dirmirror_schema_version to arti_dirserver_schema_version.
* database: Introduce DocumentIdClara Engler2026-01-191-23/+89
| | | | | | This commit introduces a new type in the database module named `DocumentId` for abstracting the underlying content-addressable hash algorithm we are using.
* tor-dirserver: Small pre-merge fixesClara Engler2026-01-151-1/+0
|
* tor-dirserver: Add TODO wrt hex::encode_upperClara Engler2026-01-151-0/+2
| | | | | Adding a comment suggesting to implement this as part of abstracting `Sha256` behind a more generic identifier.
* tor-dirserver: Fix clippy warningClara Engler2026-01-151-1/+1
|
* tor-dirserver: Use `INSERT OR REPLACE`Clara Engler2026-01-151-2/+43
| | | | | | | | | | | This commit rpelaces the use of `INSERT` with `INSERT OR REPLACE` for handling insertion conflicts in `store_insert`. Because everything is content-addressed anyways, it does not matter to do this, because if we get a conflict on a SHA256, it means the data is equal.[1] [1]: Excluding SHA-2 collisions which are impractical as of 2026.
* tor-dirserver: Change store_insert logicClara Engler2026-01-151-47/+45
| | | | | | This commit modifies the database::store_insert logic to accept an iterator of the encodings to store the document in, instead of encoding it with all encodings we support.
* tor-dirserver: Remove algorithm CHECK constraintClara Engler2026-01-151-2/+1
|
* tor-dirserver: Add TODO for hash agnostic type alias nameClara Engler2026-01-151-0/+1
|
* tor-dirserver: Ensure algorithm with CHECKClara Engler2026-01-151-2/+3
| | | | | | This commit adds a CHECK constraint to the compressed_document table in order to ensure that `algorithm` may only take up a limited set of values.
* tor-dirserver: Move ContentEncoding to databaseClara Engler2026-01-151-1/+17
| | | | | | This commit moves the ContentEncoding enum from the http module to the database module, primarily because the content encoding is more of a matter to the database, as this is where the data actually resides.
* tor-dirserver: Support for inserting into storeClara Engler2026-01-151-3/+215
| | | | | | | | This commit implements support for adding arbitrary documents into the store table. It is non-trivial because the relevant data has to be compressed into various formats, so it can be retrieved without delays.
* tor-dirserver: `From<Timestamp> for SystemTime`Clara Engler2026-01-151-0/+6
| | | | | | The reverse direction already exists and it will be required when we act with functions outside the crate that only accept a `SystemTime` in situations where we just have a `Timestamp`.
* tor-dirserver: Add `dir_key_published` to schemaClara Engler2026-01-151-1/+4
|
* tor-dirserver: Eliminate use of sleep in testsClara Engler2026-01-131-38/+58
| | | | | | | | | | | | This commit eliminates the use of sleep in database tests because those can be flaky, especially when the CI scheduler is overloaded, leading to potential timeout invariants not holding true anymore. We now fix this by using synchronization primitives from Rust in order for threads to communicate. Documentation has been added explaining how these tests work in greater detail. Fixes #2308
* tor-dirserver: Improve database::TimestampClara Engler2025-11-271-14/+59
| | | | | | | | | | This commit improves upon the work made in arti!3460, by establishing the `database::Timestamp` type as a general purpose type for the handling of timestamp throughout the code base. It also replaces all previous occurrences of `SystemTime` within `dirmirror::operation`, which leads to the reduction of code complexity in one particular function, namely `calculate_sync_timeout`.
* tor-dirserver: Use `saturating-time`Clara Engler2025-11-261-2/+49
| | | | | | | This commit replaces the custom implementation of `SaturatingSystemTime` with the `saturating-time` crate, additionally adding a new type to the `database` module called `Timestamp`, in order to have a convenient wrapper around `ToSql` and `FromSql`.
* Fix name of clippy lint to unchecked_time_subtraction (2)Ian Jackson2025-11-061-1/+1
| | | | Run maint/add_warning
* tor-dirserver: Ensure positive timestampsClara Engler2025-10-301-1/+5
| | | | | This commit adds `CHECK` constraints to the database schema in order to ensure all timestamps are positive.
* tor-dirserver: Small database::open() improvementsClara Engler2025-10-301-115/+58
|
* tor-dirserver: Make database full syncClara Engler2025-10-301-136/+125
| | | | | | | | | | This commit makes the entire database operations synchronous, thereby replacing deadpool with r2d2. The full motivation is outlined in a rustdoc comment at the top of the `database` module, but it can be summarized to the fact that SQLite is by design inherently synchronous due to directly interfacing with the file system.
* tor-dirserver: Implement read_tx and rw_txClara Engler2025-10-301-55/+351
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit does a large overhaul in the overall way on how we interact with SQLite. Although SQLite is a fairly straightforward tool, it has one large caveat, namely the handling of the infamous `SQLITE_BUSY` error. In WAL mode, the journaling mode we are using, the respective error may occur if there is an existing write transaction while another thread is trying to either upgrade an existing read transaction or create an exclusive transaction at the same time. In this case, SQLite will fail at the statement that either requires the write operation or at the creation of the transaction respectively. In order to tackle this problem, SQLite provides the `busy_timeout` pragma, which allows specifying a timeout in milliseconds during which SQLite will retry to obtain a write transaction or fail immediately. This only works in the latter case, that is, creating a write transaction from the beginning. To solve this issue, we introduce two new functions: 1. `database::read_tx` 2. `database::rw_tx` Both functions accept a database pool alongside a closure accepting a `Transaction` and returning an arbitrary return value. The first function creates a deferred transaction that gets rolled-back in the end, making it suitable for read-only connections. The latter function creates an exclusive transaction that gets committed in the end, making it suitable for read-write connections. This also honors the `busy_timeout`, i.e. the function retries for up to 1s to acquire a write transaction before failing ultimately.
* tor-dirmirror: Introduce the `sql!` macroClara Engler2025-10-301-2/+19
| | | | | | This commit introduces the no-op sql macro used to mark string literals as SQL statements. It is purely semantical and serves the purpose to quickly identify string literals as SQL statements.
* tor-dirserver: Don't expose rusqlite::Error in PoolClara Engler2025-10-151-2/+11
|
* tor-dirserver: Create database moduleClara Engler2025-10-151-0/+295
This commit renames the schema module to the database module in order to perform better error handling while redesigning the API.