Skip to content

Commit

Permalink
[centril/websocket-use-ids]: Merge commit 'origin/master~1' into cent…
Browse files Browse the repository at this point in the history
…ril/websocket-use-ids
  • Loading branch information
bfops committed Nov 4, 2024
2 parents 6cc4313 + 1657fd2 commit 7915b10
Show file tree
Hide file tree
Showing 20 changed files with 679 additions and 587 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 1 addition & 24 deletions crates/bindings-csharp/BSATN.Runtime/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace SpacetimeDB;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SpacetimeDB.BSATN;
using SpacetimeDB.Internal;

internal static class Util
{
Expand Down Expand Up @@ -93,30 +94,6 @@ 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 ulong MicrosecondsSinceEpoch = (ulong)time.Ticks / 10;

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

[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);
}

// [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)>
Expand Down
27 changes: 27 additions & 0 deletions crates/bindings-csharp/BSATN.Runtime/Repr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace SpacetimeDB.Internal;

using System.Runtime.InteropServices;

// 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 ulong MicrosecondsSinceEpoch = (ulong)time.Ticks / 10;

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

[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);
}
11 changes: 1 addition & 10 deletions crates/bindings-csharp/Runtime/Internal/ITable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ITable<T> : IStructuralReadWrite

internal abstract class RawTableIterBase
{
public class Enumerator(FFI.RowIter handle) : IDisposable
public sealed class Enumerator(FFI.RowIter handle) : IDisposable
{
byte[] buffer = new byte[0x20_000];
public byte[] Current { get; private set; } = [];
Expand Down Expand Up @@ -65,18 +65,9 @@ public void Dispose()
{
FFI.row_iter_bsatn_close(handle);
handle = FFI.RowIter.INVALID;
// Avoid running ~RowIter if Dispose was executed successfully.
GC.SuppressFinalize(this);
}
}

// Free unmanaged resource just in case user hasn't disposed for some reason.
~Enumerator()
{
// we already guard against double-free in Dispose.
Dispose();
}

public void Reset()
{
throw new NotImplementedException();
Expand Down
25 changes: 17 additions & 8 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct RawConfig {
server_configs: Vec<ServerConfig>,
// TODO: Consider how these tokens should look to be backwards-compatible with the future changes (e.g. we may want to allow users to `login` to switch between multiple accounts - what will we cache and where?)
// TODO: Move these IDs/tokens out of config so we're no longer storing sensitive tokens in a human-edited file.
web_session_id: Option<String>,
web_session_token: Option<String>,
spacetimedb_token: Option<String>,
}

Expand Down Expand Up @@ -113,7 +113,7 @@ impl RawConfig {
RawConfig {
default_server: local.nickname.clone(),
server_configs: vec![local, testnet],
web_session_id: None,
web_session_token: None,
spacetimedb_token: None,
}
}
Expand Down Expand Up @@ -410,13 +410,18 @@ Fetch the server's fingerprint with:
Ok(())
}

pub fn set_web_session_id(&mut self, token: String) {
self.web_session_id = Some(token);
pub fn set_web_session_token(&mut self, token: String) {
self.web_session_token = Some(token);
}

pub fn set_spacetimedb_token(&mut self, token: String) {
self.spacetimedb_token = Some(token);
}

pub fn clear_login_tokens(&mut self) {
self.web_session_token = None;
self.spacetimedb_token = None;
}
}

impl Config {
Expand Down Expand Up @@ -704,16 +709,20 @@ Update the server's fingerprint with:
}
}

pub fn set_web_session_id(&mut self, token: String) {
self.home.set_web_session_id(token);
pub fn set_web_session_token(&mut self, token: String) {
self.home.set_web_session_token(token);
}

pub fn set_spacetimedb_token(&mut self, token: String) {
self.home.set_spacetimedb_token(token);
}

pub fn web_session_id(&self) -> Option<&String> {
self.home.web_session_id.as_ref()
pub fn clear_login_tokens(&mut self) {
self.home.clear_login_tokens();
}

pub fn web_session_token(&self) -> Option<&String> {
self.home.web_session_token.as_ref()
}

pub fn spacetimedb_token(&self) -> Option<&String> {
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn get_subcommands() -> Vec<Command> {
generate::cli(),
list::cli(),
login::cli(),
logout::cli(),
init::cli(),
build::cli(),
server::cli(),
Expand Down Expand Up @@ -61,6 +62,7 @@ pub async fn exec_subcommand(config: Config, cmd: &str, args: &ArgMatches) -> Re
#[cfg(feature = "standalone")]
"start" => start::exec(args).await,
"login" => login::exec(config, args).await,
"logout" => logout::exec(config, args).await,
"upgrade" => upgrade::exec(config, args).await,
unknown => Err(anyhow::anyhow!("Invalid subcommand: {}", unknown)),
}
Expand Down
27 changes: 11 additions & 16 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,28 +408,28 @@ fn autogen_csharp_access_funcs_for_struct(
None => continue,
Some(x) => x,
};
writeln!(
output,
"public readonly ref struct {csharp_field_name_pascal}UniqueIndex"
);
writeln!(output, "public class {csharp_field_name_pascal}UniqueIndex");
indented_block(output, |output| {
write!(
output,
"internal readonly Dictionary<{csharp_field_type}, {struct_name_pascal_case}> Cache = new(16);"
);
writeln!(output);

writeln!(
output,
"public {struct_name_pascal_case}? Find({csharp_field_type} value)"
);
indented_block(output, |output| {
writeln!(
output,
"{csharp_field_name_pascal}_Index.TryGetValue(value, out var r);"
);
writeln!(output, "Cache.TryGetValue(value, out var r);");
writeln!(output, "return r;");
});
writeln!(output);
});
writeln!(output);
writeln!(
output,
"public {csharp_field_name_pascal}UniqueIndex {csharp_field_name_pascal} => new();"
"public {csharp_field_name_pascal}UniqueIndex {csharp_field_name_pascal} = new();"
);
writeln!(output);
}
Expand Down Expand Up @@ -595,11 +595,6 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
if !constraints[&ColList::new(col.col_pos)].has_unique() {
continue;
}
let type_name = ty_fmt(ctx, &col.col_type, namespace);
writeln!(
output,
"private static Dictionary<{type_name}, {table_type}> {field_name}_Index = new(16);"
);
unique_indexes.push(field_name);
}
if !unique_indexes.is_empty() {
Expand All @@ -616,7 +611,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
if !constraints[&ColList::new(col.col_pos)].has_unique() {
continue;
}
writeln!(output, "{field_name}_Index[value.{field_name}] = value;");
writeln!(output, "{field_name}.Cache[value.{field_name}] = value;");
}
});
writeln!(output);
Expand All @@ -631,7 +626,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
if !constraints[&ColList::new(col.col_pos)].has_unique() {
continue;
}
writeln!(output, "{field_name}_Index.Remove((({table_type})row).{field_name});");
writeln!(output, "{field_name}.Cache.Remove((({table_type})row).{field_name});");
}
});
writeln!(output);
Expand Down
Loading

0 comments on commit 7915b10

Please sign in to comment.