Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Timestamp a special SATS type #1836

Merged
merged 36 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
dece63c
WIP: Make `Timestamp` a special SATS type
gefjon Oct 10, 2024
998db08
Restore `Timestamp` to user-facing status
gefjon Oct 11, 2024
1a6566a
Clippy
gefjon Oct 11, 2024
9ecc60f
Fix Rust smoketests; offer backcompat methods
gefjon Oct 14, 2024
75a664e
Update TypeScript codegen to use new `Timestamp` class
gefjon Oct 16, 2024
9571e0e
Also add `TimeDuration`, which is like `Duration`
gefjon Oct 17, 2024
75ae9d9
Merge branch 'master' into phoebe/timestamp-special-type
gefjon Oct 21, 2024
5c64b9a
fmt
gefjon Oct 21, 2024
2934b59
Update WebSocket schema to use `TimeDuration`
gefjon Oct 21, 2024
ae88395
Implement Timestamp and TimeDuration in C#
kazimuth Oct 15, 2024
627c35b
Become more special
kazimuth Oct 23, 2024
36bb5bd
Consistent formatting for Timestamp and TimeDuration (please bikeshed)
kazimuth Oct 23, 2024
072c2ea
Prioritize BSATN and BFLATN compat with 0.12
gefjon Oct 30, 2024
8d9715c
Merge branch 'master' into phoebe/timestamp-special-type
kazimuth Nov 22, 2024
c50cbba
Fix extant compile error
kazimuth Nov 22, 2024
22a7355
Fix type error
kazimuth Nov 22, 2024
436cb73
Fix conflicts
kazimuth Nov 22, 2024
436c776
Merge branch 'master' into phoebe/timestamp-special-type
gefjon Jan 24, 2025
4b89003
Rework Timestamp declarations in BSATN.Runtime
kazimuth Jan 29, 2025
5f4c3aa
Start fixing sdk-test, whoops
kazimuth Jan 29, 2025
ffeadfa
Remove outdated API from tests
kazimuth Jan 30, 2025
cb81c9b
Better comments
kazimuth Feb 3, 2025
1cddd58
Merge remote-tracking branch 'origin/master' into phoebe/timestamp-sp…
gefjon Feb 4, 2025
c7c8de5
Fix smoketests
gefjon Feb 4, 2025
fe9998c
format c#
gefjon Feb 4, 2025
2950991
use timestamp in c# benchmark modules
gefjon Feb 4, 2025
dfb0e93
Apply James' suggestions
gefjon Feb 6, 2025
9f469e5
Merge remote-tracking branch 'origin/master' into phoebe/timestamp-sp…
gefjon Feb 6, 2025
0b99e1c
Merge branch 'master' into phoebe/timestamp-special-type
kazimuth Feb 6, 2025
7e12c3e
Allow adding TimeDurations to Timestamps similar to std API
kazimuth Feb 6, 2025
596a10c
Add rest of Timestamp functions to C# and purge DateTimeOffset
kazimuth Feb 6, 2025
d0b210e
Teach CLI about mysterious tildes that sometimes appear in C# bin fol…
kazimuth Feb 6, 2025
f50f238
Merge remote-tracking branch 'origin/master' into phoebe/timestamp-sp…
gefjon Feb 7, 2025
aa7abdc
Dotnet format
kazimuth Feb 7, 2025
46ee2ea
Try formatting with `dotnet csharpier` instead?
gefjon Feb 7, 2025
48b47ce
Add comment re: `bin/` and `bin~/`
gefjon Feb 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/bindings-csharp/BSATN.Runtime/BSATN.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<!-- Note: the binary produced by this package is used in Unity too, which is limited to .NET Standard 2.1. -->
<TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks>
<RootNamespace>SpacetimeDB</RootNamespace>
<!-- You can enable this when debugging codegen problems. Outputs in obj/debug/[version]/generated. -->
<!-- <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> -->
</PropertyGroup>

<ItemGroup>
Expand Down
96 changes: 32 additions & 64 deletions crates/bindings-csharp/BSATN.Runtime/Builtins.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace SpacetimeDB;

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SpacetimeDB.BSATN;
using SpacetimeDB.Internal;
Expand All @@ -13,6 +12,11 @@ internal static class Util
public static string ToHex<T>(T val)
where T : struct =>
BitConverter.ToString(MemoryMarshal.AsBytes([val]).ToArray()).Replace("-", "");

// Similarly, we need some constants that are not available in .NET Standard.
public const long NanosecondsPerTick = 100;
public const long NanosecondsPerMicrosecond = 1000;

}

public readonly partial struct Unit
Expand Down Expand Up @@ -137,80 +141,44 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
public override string ToString() => Util.ToHex(value);
}

// We store time information in microseconds in internal usages.
//
// These utils allow to encode it as such in FFI and BSATN contexts
// and convert to standard C# types.

[StructLayout(LayoutKind.Sequential)] // we should be able to use it in FFI
[SpacetimeDB.Type] // we should be able to encode it to BSATN too
public partial struct DateTimeOffsetRepr(DateTimeOffset time)
public partial struct Timestamp(long nanosecondsSinceUnixEpoch)
{
public ulong MicrosecondsSinceEpoch = (ulong)time.Ticks / 10;
// This has a slightly wonky name, so just use the name directly.
private long __timestamp_nanos_since_unix_epoch = nanosecondsSinceUnixEpoch;

public readonly DateTimeOffset ToStd() =>
DateTimeOffset.UnixEpoch.AddTicks(10 * (long)MicrosecondsSinceEpoch);
}
public readonly long NanosecondsSinceUnixEpoch => __timestamp_nanos_since_unix_epoch;

public static implicit operator DateTimeOffset(Timestamp t) => DateTimeOffset.UnixEpoch.AddTicks(t.__timestamp_nanos_since_unix_epoch / Util.NanosecondsPerTick);
public static implicit operator Timestamp(DateTimeOffset offset) => new Timestamp(offset.Subtract(DateTimeOffset.UnixEpoch).Ticks * Util.NanosecondsPerTick);

[StructLayout(LayoutKind.Sequential)] // we should be able to use it in FFI
[SpacetimeDB.Type] // we should be able to encode it to BSATN too
public partial struct TimeSpanRepr(TimeSpan duration)
{
public ulong Microseconds = (ulong)duration.Ticks / 10;

public readonly TimeSpan ToStd() => TimeSpan.FromTicks(10 * (long)Microseconds);
// For backwards-compatibility.
public readonly DateTimeOffset ToStd() => this;
}

// [SpacetimeDB.Type] - we have custom representation of time in microseconds, so implementing BSATN manually
public abstract partial record ScheduleAt
: SpacetimeDB.TaggedEnum<(DateTimeOffset Time, TimeSpan Interval)>
[StructLayout(LayoutKind.Sequential)]
[SpacetimeDB.Type]
public partial struct TimeDuration(long nanoseconds)
{
// Manual expansion of what would be otherwise generated by the [SpacetimeDB.Type] codegen.
public sealed record Time(DateTimeOffset Time_) : ScheduleAt;
private long __time_duration_nanoseconds = nanoseconds;

public sealed record Interval(TimeSpan Interval_) : ScheduleAt;
public readonly long Nanoseconds => __time_duration_nanoseconds;

public static implicit operator ScheduleAt(DateTimeOffset time) => new Time(time);

public static implicit operator ScheduleAt(TimeSpan interval) => new Interval(interval);
public static implicit operator TimeSpan(TimeDuration d) => new TimeSpan(d.__time_duration_nanoseconds / Util.NanosecondsPerTick);
public static implicit operator TimeDuration(TimeSpan timeSpan) => new TimeDuration(timeSpan.Ticks * Util.NanosecondsPerTick);

public readonly partial struct BSATN : IReadWrite<ScheduleAt>
{
[SpacetimeDB.Type]
private partial record ScheduleAtRepr
: SpacetimeDB.TaggedEnum<(DateTimeOffsetRepr Time, TimeSpanRepr Interval)>;

private static readonly ScheduleAtRepr.BSATN ReprBSATN = new();

public ScheduleAt Read(BinaryReader reader) =>
ReprBSATN.Read(reader) switch
{
ScheduleAtRepr.Time(var timeRepr) => new Time(timeRepr.ToStd()),
ScheduleAtRepr.Interval(var intervalRepr) => new Interval(intervalRepr.ToStd()),
_ => throw new SwitchExpressionException(),
};

public void Write(BinaryWriter writer, ScheduleAt value)
{
ReprBSATN.Write(
writer,
value switch
{
Time(var time) => new ScheduleAtRepr.Time(new(time)),
Interval(var interval) => new ScheduleAtRepr.Interval(new(interval)),
_ => throw new SwitchExpressionException(),
}
);
}
// For backwards-compatibility.
public readonly TimeSpan ToStd() => this;
}

public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
// Constructing a custom one instead of ScheduleAtRepr.GetAlgebraicType()
// to avoid leaking the internal *Repr wrappers in generated SATS.
new AlgebraicType.Sum(
[
new("Time", new AlgebraicType.U64(default)),
new("Interval", new AlgebraicType.U64(default)),
]
);
}
[SpacetimeDB.Type]
public partial record ScheduleAt
: SpacetimeDB.TaggedEnum<(Timestamp Time, TimeDuration Interval)>
{
public static implicit operator ScheduleAt(TimeDuration duration) => new Interval(duration);
public static implicit operator ScheduleAt(TimeSpan duration) => new Interval(duration);
public static implicit operator ScheduleAt(Timestamp time) => new Time(time);
public static implicit operator ScheduleAt(DateTimeOffset time) => new Time(time);
}
5 changes: 3 additions & 2 deletions crates/bindings-csharp/Runtime/Internal/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static Errno __call_reducer__(
ulong sender_3,
ulong address_0,
ulong address_1,
DateTimeOffsetRepr timestamp,
Timestamp timestamp,
BytesSource args,
BytesSink error
)
Expand All @@ -190,7 +190,8 @@ BytesSink error
var senderAddress = Address.From(
MemoryMarshal.AsBytes([address_0, address_1]).ToArray()
);
var random = new Random((int)timestamp.MicrosecondsSinceEpoch);
var microsecondsSinceUnixEpoch = (int)timestamp.NanosecondsSinceUnixEpoch / 1000;
var random = new Random(microsecondsSinceUnixEpoch);
var time = timestamp.ToStd();

var ctx = newContext!(senderIdentity, senderAddress, random, time);
Expand Down
2 changes: 1 addition & 1 deletion crates/bindings-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ pub mod raw {
mod module_exports {
type Encoded<T> = Buffer;
type Identity = Encoded<[u8; 32]>;
/// microseconds since the unix epoch
/// Nanos since the unix epoch
type Timestamp = u64;
/// Buffer::INVALID => Ok(()); else errmsg => Err(errmsg)
type Result = Buffer;
Expand Down
3 changes: 1 addition & 2 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod rng;
pub mod rt;
#[doc(hidden)]
pub mod table;
mod timestamp;

use spacetimedb_lib::bsatn;
use std::cell::RefCell;
Expand All @@ -36,10 +35,10 @@ pub use spacetimedb_lib::Address;
pub use spacetimedb_lib::AlgebraicValue;
pub use spacetimedb_lib::Identity;
pub use spacetimedb_lib::ScheduleAt;
pub use spacetimedb_lib::Timestamp;
pub use spacetimedb_primitives::TableId;
pub use sys::Errno;
pub use table::{AutoIncOverflow, BTreeIndex, Table, TryInsertError, UniqueColumn, UniqueConstraintViolation};
pub use timestamp::Timestamp;

pub type ReducerResult = core::result::Result<(), Box<str>>;

Expand Down
2 changes: 1 addition & 1 deletion crates/bindings/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ReducerContext {
/// For more information, see [`StdbRng`] and [`rand::Rng`].
pub fn rng(&self) -> &StdbRng {
self.rng.get_or_init(|| StdbRng {
rng: StdRng::seed_from_u64(self.timestamp.micros_since_epoch).into(),
rng: StdRng::seed_from_u64(self.timestamp.to_nanos_since_unix_epoch() as u64).into(),
_marker: PhantomData,
})
}
Expand Down
29 changes: 5 additions & 24 deletions crates/bindings/src/rt.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::timestamp::with_timestamp_set;
use crate::{sys, IterBuf, ReducerContext, ReducerResult, SpacetimeType, Table, Timestamp};
use crate::{sys, IterBuf, ReducerContext, ReducerResult, SpacetimeType, Table};
pub use spacetimedb_lib::db::raw_def::v9::Lifecycle as LifecycleReducer;
use spacetimedb_lib::db::raw_def::v9::{RawIndexAlgorithm, RawModuleDefV9Builder, TableType};
use spacetimedb_lib::de::{self, Deserialize, SeqProductAccess};
use spacetimedb_lib::sats::typespace::TypespaceBuilder;
use spacetimedb_lib::sats::{impl_deserialize, impl_serialize, ProductTypeElement};
use spacetimedb_lib::ser::{Serialize, SerializeSeqProduct};
use spacetimedb_lib::{bsatn, Address, Identity, ProductType, RawModuleDef};
use spacetimedb_lib::{bsatn, Address, Identity, ProductType, RawModuleDef, Timestamp};
use spacetimedb_primitives::*;
use std::fmt;
use std::marker::PhantomData;
use std::sync::{Mutex, OnceLock};
use std::time::Duration;
use sys::raw::{BytesSink, BytesSource};

/// The `sender` invokes `reducer` at `timestamp` and provides it with the given `args`.
Expand All @@ -28,8 +26,7 @@ pub fn invoke_reducer<'a, A: Args<'a>>(
// Deserialize the arguments from a bsatn encoding.
let SerDeArgs(args) = bsatn::from_slice(args).expect("unable to decode args");

// Run the reducer with the environment all set up.
with_timestamp_set(ctx.timestamp, || reducer.invoke(&ctx, args))
reducer.invoke(&ctx, args)
}
/// A trait for types representing the *execution logic* of a reducer.
#[diagnostic::on_unimplemented(
Expand Down Expand Up @@ -288,22 +285,6 @@ impl_serialize!(['de, A: Args<'de>] SerDeArgs<A>, (self, ser) => {
prod.end()
});

/// A trait for types representing repeater arguments.
pub trait RepeaterArgs: for<'de> Args<'de> {
/// Returns a notion of now in time.
fn get_now() -> Self;
}

impl RepeaterArgs for () {
fn get_now() -> Self {}
}

impl RepeaterArgs for (Timestamp,) {
fn get_now() -> Self {
(Timestamp::now(),)
}
}

/// A trait for types that can *describe* a row-level security policy.
pub trait RowLevelSecurityInfo {
/// The SQL expression for the row-level security policy.
Expand Down Expand Up @@ -391,7 +372,7 @@ struct ModuleBuilder {
// Not actually a mutex; because WASM is single-threaded this basically just turns into a refcell.
static DESCRIBERS: Mutex<Vec<fn(&mut ModuleBuilder)>> = Mutex::new(Vec::new());

/// A reducer function takes in `(Sender, Timestamp, Args)`
/// A reducer function takes in `(ReducerContext, Args)`
/// and returns a result with a possible error message.
pub type ReducerFn = fn(ReducerContext, &[u8]) -> ReducerResult;
static REDUCERS: OnceLock<Vec<ReducerFn>> = OnceLock::new();
Expand Down Expand Up @@ -485,7 +466,7 @@ extern "C" fn __call_reducer__(
let address = (address != Address::__DUMMY).then_some(address);

// Assemble the `ReducerContext`.
let timestamp = Timestamp::UNIX_EPOCH + Duration::from_micros(timestamp);
let timestamp = Timestamp::from_nanos_since_unix_epoch(timestamp as i64);
let ctx = ReducerContext {
db: crate::Local {},
sender,
Expand Down
110 changes: 0 additions & 110 deletions crates/bindings/src/timestamp.rs

This file was deleted.

6 changes: 6 additions & 0 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ fn ty_fmt<'a>(ctx: &'a GenCtx, ty: &'a AlgebraicType, namespace: &'a str) -> imp
fmt_fn(move |f| match ty {
ty if ty.is_identity() => f.write_str("SpacetimeDB.Identity"),
ty if ty.is_address() => f.write_str("SpacetimeDB.Address"),
ty if ty.is_timestamp() => todo!("Emit Timestamp"),
ty if ty.is_time_duration() => todo!("Emit TimeDuration"),
ty if ty.is_schedule_at() => f.write_str("SpacetimeDB.ScheduleAt"),
AlgebraicType::Sum(sum_type) => {
// This better be an option type
Expand Down Expand Up @@ -373,6 +375,10 @@ fn csharp_field_type(field_type: &AlgebraicType) -> Option<&str> {
Some("SpacetimeDB.Identity")
} else if product.is_address() {
Some("SpacetimeDB.Address")
} else if product.is_timestamp() {
todo!("Emit Timestamp")
} else if product.is_time_duration() {
todo!("Emit TimeDuration")
} else {
None
}
Expand Down
Loading
Loading