Commit Graph

57 Commits

Author SHA1 Message Date
Mazdak Farrokhzad 8544e6cf02 Add Hash indices (#3976)
# Description of Changes

Fixes https://github.com/clockworklabs/SpacetimeDB/issues/1122.

Adds hash indices and exposes them through `#[index(hash)]` for Rust
modules,
with support for typescript and C# to come in follow ups.
On the client/sdk side, for now, any index is backed via a BTree/native
index as it is the most general.

A hash index may only be queried through point scan and never ranged
scans.
Attempting a ranged scan results in an error, with the mechanism
implemented in the previous PR
(https://github.com/clockworklabs/SpacetimeDB/pull/3974).



# API and ABI breaking changes

None

# Expected complexity level and risk

2?

# Testing

A test for ensuring that hash indices cannot be used for range scans is
added.
Tests exercising hash indices will come in the next PR.
2026-01-14 09:44:20 +00:00
Phoebe Goldman 7df8719b61 Add procedure HTTP request API for WASM modules and the Rust module bindings library (#3684)
# Description of Changes

Closes #3517 .

With this PR, procedures (at least, those defined in Rust modules) can
perform HTTP requests! This is performed through a new field on the
`ProcedureContext`, `http: HttpClient`, which has a method `send` for
sending an `http::Request`, as well as a convenience wrapper `get`.

Internally, these methods hit the `procedure_http_request` ABI call /
host function, which uses reqwest to perform an HTTP request. The
request is run with a user-configurable timeout which defaults and is
clamped to 500 ms.
Rather than exposing the HTTP stream to modules, we download the entire
response body immediately, within the same timeout.

I've added an example usage of `get` to `module-test` which performs a
request against `localhost:3000` to read its own schema/moduledef.

This PR also makes all procedure-related definitions in the Rust module
bindings library `#[cfg(feature = "unstable")]`, as per #3644 . The
rename of the `/v1/database/:name/procedure/:name` route is not included
in this PR, so this does not close #3644 .

Left as TODOs are:
- Metrics for recording request and response size.
- Improving performance by stashing a long-lived `reqwest::Client`
someplace.
  Currently we build a new `Client` for each request.
- Improving performance (possibly) by passing the request-future to the
global tokio executor
  rather than running it on the single-threaded database executor.

# API and ABI breaking changes

Adds new APIs, which are marked as unstable. Adds a new ABI, which is
not unstable in any meaningful way (we can't really do that). Marks
unreleased APIs as unstable. Does not affect any pre-existing
already-released APIs or ABIs.

# Expected complexity level and risk

3 or so: networking is scary, and even though we impose a timeout which
prevents these connections from being truly long-lived, they're still
potentially long-lived on the scale of Tokio futures. It's possible that
running them on the database core is problematic in some way, and so
what I've left as a performance TODO could actually be a
concurrency-correctness issue.

# Testing

- [x] Manually wrote and executed some procedures which make HTTP
requests.
- [x] Added two automated tests to the `sdk-test` suite,
`procedure::http_ok` and `procedure::http_err`, which make successful
and failing requests respectively, then return its result. A client then
makes some assertions about the result.

---------

Co-authored-by: Noa <coolreader18@gmail.com>
2025-11-20 20:47:35 +00:00
Mazdak Farrokhzad 0a3251708a Add ProcedureContext::with_tx (#3638)
# Description of Changes

Adds `ProcedureContext::{with_tx, try_with_tx}`.

Fixes https://github.com/clockworklabs/SpacetimeDB/issues/3515.

# API and ABI breaking changes

None

# Expected complexity level and risk

2

# Testing

An integration test `test_calling_with_tx` is added.
2025-11-19 20:33:02 +00:00
joshua-spacetime 0de8910e2a Atomic view update (#3624)
# Description of Changes

Updates views atomically on commit, but before downgrading to a
read-only transaction for subscription evaluation.

What this patch does:
1. Renames `ViewId` to `ViewFnPtr`
2. Renames `ViewDatabaseId` to `ViewId`
3. Removes the `module_rx` module watcher from the subscription manager
4. Refactors read sets to only track table scans (index key tracking
will be added later)
5. Drops read sets and removes rows from `st_view_sub` when dropping a
view in an auto-migrate
6. Re-evaluates and updates views (`call_views_with_tx`) from
`call_reducer_with_tx` for any view whose read set overlaps with the
reducer's write set
7. Does the same for sql dml

# API and ABI breaking changes

None

# Expected complexity level and risk

3

It's a bit of a messy diff.

# Testing

- [x] Integrate with
https://github.com/clockworklabs/SpacetimeDB/pull/3616

---------

Signed-off-by: joshua-spacetime <josh@clockworklabs.io>
Co-authored-by: Shubham Mishra <shivam828787@gmail.com>
2025-11-11 04:26:52 +00:00
Shubham Mishra 75c6e67c3c Views: Host interface for WASM modules (#3548)
# Description of Changes
Host implementation to invoke `call_view` method.
 I also covers:
1. API `MutTxId::is_materialized`to check if existing view exisits and
updated.
 2. Update in readsets logic to remove stale views.
 3. sql caller implmentation.

# API and ABI breaking changes
NA

How complicated do you think these changes are? Grade on a scale from 1
to 5,
where 1 is a trivial change, and 5 is a deep-reaching and complex
change.
3
2025-11-06 21:14:00 +00:00
joshua-spacetime 30b8eaccf1 Decrement view subscriber count on disconnect (#3547)
# Description of Changes

Refactored `st_view_client` and renamed it `st_view_sub` which tracks
the number of clients subscribed to a view. On disconnect, we decrement
the `num_subscribers` column in the appropriate rows. An async task will
be in charge of cleaning up views (and their read sets) whose subscriber
count has gone to zero (not in this patch).

On module init, we clear the entirety of each view table.

# API and ABI breaking changes

None. Technically this updates the schema of a system table, but the
system table was added and modified between releases.

# Expected complexity level and risk

~2

Need to make sure we cover all cases so that we don't leave dangling
data. Making these tables ephemeral in the future should simplify this.

# Testing

Will add tests once we can subscribe to views
2025-11-06 07:55:17 +00:00
joshua-spacetime 542d26d7ff add system tables for views (#3419)
# Description of Changes

<!-- Please describe your change, mention any related tickets, and so on
here. -->

This patch defines the system table schemas for views and allocates IDs
for them. It **does not** populate these tables.

# API and ABI breaking changes

<!-- If this is an API or ABI breaking change, please apply the
corresponding GitHub label. -->

None

# Expected complexity level and risk

<!--
How complicated do you think these changes are? Grade on a scale from 1
to 5,
where 1 is a trivial change, and 5 is a deep-reaching and complex
change.

This complexity rating applies not only to the complexity apparent in
the diff,
but also to its interactions with existing and future code.

If you answered more than a 2, explain what is complex about the PR,
and what other components it interacts with in potentially concerning
ways. -->

1

# Testing

<!-- Describe any testing you've done, and any testing you'd like your
reviewers to do,
so that you're confident that all the changes work as expected! -->

Tests will be added in the patch that populates these tables
2025-10-17 19:01:22 +00:00
Phoebe Goldman 0a6aa7c5d9 ModuleDef and schema changes to support procedures (#3392)
# Description of Changes

This commit extends various schema and schema-adjacent structures to
describe procedures, a new kind of database function which are allowed
to perform side effects.

This includes extending `RawModuleDefV9` with a way to register
`RawProcedureDefV9`s in the `misc_exports`, preserving compatibility
with modules that predate procedures.

The module validation path is reorganized somewhat to validate various
properties related to procedures while preserving code clarity and
maintainability.

Additionally, the `ArgsTuple` machinery for ser/de-ing reducer arguments
using the argument type as a seed is extended to also support procedure
arguments.

All of this is currently unused.

# API and ABI breaking changes

Additive and backwards-compatible additions to `RawModuleDefV9` and
friends.

# Expected complexity level and risk

2 - some minor complexity in schema validation which may have gotten
borked in a merge at some point.

# Testing

Unsure what tests would be useful, open to suggestions from reviewers.

- [ ] <!-- maybe a test you want to do -->
- [ ] <!-- maybe a test you want a reviewer to do, so they can check it
off when they're satisfied. -->
2025-10-17 17:22:14 +00:00
Noa 207e559859 Noa/ts module host changes (#3388)
# Description of Changes

Host-side changes extracted from #3327 

I added AUTO_INC_OVERFLOW even though we don't currently ever return it,
in order to future-proof so it's already there when we start emitting
it.

Prepublish was failing because it was expecting a wasm module
unconditionally, so now it takes ?host_type.

I tweaked JS deser to accept null/undefined when the unit type or an
option type is expected.

I switched to bsatn, because the native sats->js translator wasn't
matching what js was expecting.

I renamed the sys module: my thinking is that `spacetime:` as a scheme
will help disambiguate it, and maybe it could also be used for IMC in
the future or something? And I believe we had discussed wanting this to
be versioned, similar to wasm imports.

Trying to get a borrowed str from deserialize_js doesn't work, because
v8 strings don't store utf8.

# Testing

<!-- Describe any testing you've done, and any testing you'd like your
reviewers to do,
so that you're confident that all the changes work as expected! -->

- [x] All this was done in the course of getting an actual typescript
module to successfully publish.
2025-10-10 18:25:26 +00:00
Zeke Foppa f6f0909ea4 Update all licenses (#3002)
# Description of Changes

We recently merged several repos together. This PR clarifies the license
terms for several subdirectories, as well as the relationship between
the licenses.

The licenses in our subdirectories have become symbolic links to
licenses in our toplevel `licenses` directory. For any particular
subdirectory's license file in the diff, you can click `... -> View
file` and then click on the text that says "Symbolic Link" on that page.
This will take you to the license file that it links to.

I have also updated the `tools/upgrade-version` script to update the
change date in the new `licenses/BSL.txt` file.

# API and ABI breaking changes

None.

# Expected complexity level and risk

1

# Testing

None. Only changes to license files.

---------

Co-authored-by: Zeke Foppa <bfops@users.noreply.github.com>
2025-08-12 18:20:58 +00:00
Noa 742303ca49 Bump rust-toolchain to rust 1.88 (#2749)
Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
2025-07-15 17:39:41 +00:00
Mazdak Farrokhzad 1166d68e65 Extract spacetimedb-memory-usage & Nix bindings dep on spacetimedb-data-structures (#2919) 2025-07-04 12:54:00 +00:00
Mario Montoya f9f38543c8 Add readmes to all implementation crates specifying that they do no offer stable interfaces (#2320) 2025-03-06 19:50:17 +00:00
Mazdak Farrokhzad b50b6841f1 Add direct indices to datastore (#2221) 2025-02-07 01:06:38 +00:00
Mazdak Farrokhzad 1a3f3af255 add direct indices, except for in datastore & C# (#2205) 2025-02-06 00:40:46 +00:00
Mazdak Farrokhzad 5b5a0a6515 Cache whether a Table is a scheduler table, avoiding fetching the schema (#2141) 2025-01-28 19:57:15 +00:00
Noa cf6ac18eaf Lookup lifecycle reducers by lifecycle flag, not by name (#2132) 2025-01-17 23:21:52 +00:00
Mazdak Farrokhzad 5ad2a2efe8 add signature of datastore_update_bsatn host call (#2102) 2025-01-13 16:41:39 +00:00
Noa c4e637e98b Energy metering for persistent memory usage (#766) 2024-10-15 17:58:34 +00:00
james gilles c32f297c47 Update TableSchema & system tables to resemble ABI V9 (#1697) 2024-09-25 20:03:22 +00:00
SteveGibson 14e509859e Stopwatch and reducer rng (#1681)
Signed-off-by: Ingvar Stepanyan <me@rreverser.com>
Co-authored-by: Steve Boytsun <steve@clockwokrlabs.io>
Co-authored-by: Ingvar Stepanyan <me@rreverser.com>
2024-09-18 10:19:32 +00:00
Mazdak Farrokhzad 4f297880ca WASM ABI: add datastore_btree_scan_bsatn & index_id_from_name (#1699)
Signed-off-by: Mazdak Farrokhzad <twingoow@gmail.com>
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
Co-authored-by: Zeke Foppa <bfops@users.noreply.github.com>
2024-09-16 19:03:17 +00:00
james gilles 11283c33a4 Add spacetimedb_primitives::col_list::ColSet (#1691)
Signed-off-by: james gilles <jameshgilles@gmail.com>
Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
2024-09-11 15:38:14 -04:00
Mazdak Farrokhzad df5ef6c81d WASM ABI: implement console ABIs (#1664) 2024-09-05 20:19:21 +00:00
Mazdak Farrokhzad da71d0f9b1 WASM ABI: insert -> datastore_insert_bsatn & impl new semantics (#1639) 2024-09-05 19:32:26 +00:00
Mazdak Farrokhzad b62569462d WASM ABI: delete_by_rel -> datastore_delete_all_by_eq_bsatn (#1638) 2024-09-02 17:43:40 +00:00
james gilles 2be42156b2 Allow converting new ModuleDef to old TableSchema (#1630) 2024-08-29 18:17:29 +00:00
Mazdak Farrokhzad 2b69583f76 WASM ABI: get_table_id -> table_id_from_name (#1634) 2024-08-27 22:33:47 +00:00
Mazdak Farrokhzad 1dd1e633f3 WASM ABI: implement row_iter_bsatn_advance & row_iter_bsatn_close (#1622) 2024-08-27 22:01:06 +00:00
Mazdak Farrokhzad 4c086fef96 [WASM ABI 1.0] impl __call_reducer__ & __describe_module__ using bytes_sink_write (#1615) 2024-08-27 15:48:40 +00:00
Mazdak Farrokhzad 9ea5a2f243 [WASM ABI 1.0] impl __call_reducer__ using bytes_source_read (#1609) 2024-08-20 09:33:29 +00:00
Mazdak Farrokhzad 1ca9b1a933 [WASM ABI 1.0] Change ColId from u32 to u16 (#1597) 2024-08-19 17:56:28 +00:00
Mazdak Farrokhzad 6a08674ccb Allow empty ColList (#1588) 2024-08-15 16:08:51 +00:00
james gilles 45b2ceee9a Move schemas to schema crate, rename Def to RawDefV8 (#1498) 2024-07-24 17:38:30 +00:00
Mazdak Farrokhzad cbff725d58 ColList: preserve list order on list.push(..) (#1474) 2024-07-08 17:55:42 +00:00
Mazdak Farrokhzad a93bd493d1 Fix index removal and additions + add smoketest (#1444) 2024-06-19 08:29:46 +00:00
Noa a54399495d Prune bindings deps (#1290) 2024-06-07 20:36:33 +00:00
Noa 471f4ff2ca recv-style abi (#1002)
* Bump module abi version

* recv abi module side

* recv abi host side

* Update csharp module sdk

* Fix name

* Address comments
2024-05-22 23:01:44 +00:00
Mazdak Farrokhzad 670625ed11 add fn ColList::last(&self) -> ColId (#1267) 2024-05-22 12:04:32 +00:00
Mazdak Farrokhzad d6815ebf9c Shrink AV and AT to 24 & 16 bytes respectively, and also friends. (#1047) 2024-04-13 16:51:18 +00:00
Mazdak Farrokhzad 344861f290 use nohasher_hash and ahash instead of siphash13 (#1040)
* use nohasher_hash and ahash instead of siphash13

* re-export types in spacetimedb_data_structures::map
2024-04-05 17:30:51 +00:00
Shubham Mishra 84071b5629 Batch metrics with ctx (#916)
* Commit iter metrics

* test: Make full-join benchmark async

* use Vec instead of Hash

* Revert "test: Make full-join benchmark async"

This reverts commit eed5aa8a55.

* rebase on master

* lint

* refector api

* remove lifetime from ctx (#1017)

* move ctx to higher level

* fix boilterplate

* batch more

---------

Co-authored-by: joshua-spacetime <josh@clockworklabs.io>
2024-04-05 06:57:40 +00:00
Noa 37658aae7e Add rust-version to Cargo.toml (#802)
* Add rust-version to Cargo.toml

* Use workspace inheritance to make bumping the spacetime version easier
2024-02-07 20:02:05 +00:00
Phoebe Goldman b8cecb3caf BTreeIndex: Specialize on primitive key types for great performance (#793)
Rewrite of Tyler's #771, because I thought this would be easier than rebasing.

This commit hoists branching on key types outside of comparisons and searches
in `BTreeIndex`,
so that in many cases we can use e.g. `u64::cmp`
instead of the much slower `AlgebraicValue::cmp`.

This design is kind of ugly, and will likely change in the future,
but for now it's good enough, and is a meaningful performance improvement.
2024-02-06 17:57:45 +00:00
joshua-spacetime befe8b7b3f feat(query): Make current plan type hashable (#782)
A prerequisite for #747.
2024-02-02 17:46:18 +00:00
Mazdak Farrokhzad 9436544c3d misc ColList usage improvements (#738) 2024-01-22 18:55:23 +00:00
Mazdak Farrokhzad a68ad8f85f add ColList: a compact repr of NonEmpty<ColId>, 8 vs 32 bytes (#730) 2024-01-19 23:44:19 +00:00
John Detter 8524d244f3 Version upgrade to 0.8.1 (#729)
Co-authored-by: John Detter <no-reply@boppygames.gg>
2024-01-18 18:16:58 +00:00
John Detter 79ff6dffc6 Version upgrade to 0.8.0 (#622)
Co-authored-by: John Detter <no-reply@boppygames.gg>
2023-12-04 13:19:25 -06:00
Mario Montoya 4ef1bd5296 Refactoring bootstrapping and adding full support for constraints & multi-column indexes (#596)
* Refactoring bootstrapping and adding full support for constraints & multi-column indexes

* Removed deleted code and add some comments

* Add comment for Constraints

* Update crates/core/src/db/datastore/locking_tx_datastore/mod.rs

Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
Signed-off-by: Mario Montoya <mamcx@elmalabarista.com>

* Update crates/core/src/db/datastore/locking_tx_datastore/mod.rs

Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
Signed-off-by: Mario Montoya <mamcx@elmalabarista.com>

* Addressing some PR comments & remove cloning

* Adding details for when fail to decode rows in replay

* Apply Kim's fix for replay bug

* Addressing some PR comments

* Merge

* Fix conflicts

* Fix conflicts

* Fix test

* Fix generation of code for csharp & typescript, now it generate FilterBy for all columns

* Fix for index generation

* Fixing C# index generation

* Merge

* Fix compilation on csharp bindings

---------

Signed-off-by: Mario Montoya <mamcx@elmalabarista.com>
Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
Co-authored-by: John Detter <no-reply@boppygames.gg>
2023-12-04 12:04:38 -05:00