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

Support arbitrary RHS in C# Query #586

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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.

16 changes: 1 addition & 15 deletions crates/bench/src/spacetime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use spacetimedb_lib::{
sats::{product, ArrayValue},
AlgebraicValue, ProductValue,
};
use spacetimedb_testing::modules::{start_runtime, CompilationMode, CompiledModule, ModuleHandle};
use spacetimedb_testing::modules::{start_runtime, CompilationMode, CompiledModule, LoggerRecord, ModuleHandle};
use tokio::runtime::Runtime;

use crate::{
Expand Down Expand Up @@ -206,17 +206,3 @@ pub struct TableId {
pascal_case: String,
snake_case: String,
}

#[allow(unused)]
/// Used to parse output from module logs.
///
/// Sync with: `core::database_logger::Record`. We can't use it
/// directly because the types are wrong for deserialization.
/// (Rust!)
#[derive(serde::Deserialize)]
struct LoggerRecord {
target: Option<String>,
filename: Option<String>,
line_number: Option<u32>,
message: String,
}
19 changes: 11 additions & 8 deletions crates/bindings-csharp/Runtime/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,33 @@ Expression<Func<T, bool>> rowFilter
expr switch
{
// LINQ inserts spurrious conversions in comparisons, so we need to unwrap them
UnaryExpression { NodeType: ExpressionType.Convert, Operand: var arg } => ExprAsTableField(arg),
MemberExpression { Expression: ParameterExpression, Member: { Name: var memberName }, Type: var type }
UnaryExpression { NodeType: ExpressionType.Convert, Operand: var arg }
=> ExprAsTableField(arg),
MemberExpression
{
Expression: ParameterExpression,
Member: { Name: var memberName },
Type: var type
}
=> ((byte)Array.FindIndex(fieldTypeInfos, pair => pair.Key == memberName), type),
_
=> throw new NotSupportedException(
"expected table field access in the left-hand side of a comparison"
)
};

object? ExprAsConstant(Expression expr) =>
object? ExprAsRhs(Expression expr) =>
expr switch
{
ConstantExpression { Value: var value } => value,
_
=> throw new NotSupportedException(
"expected constant expression in the right-hand side of a comparison"
)
_ => Expression.Lambda(expr).Compile().DynamicInvoke()
};

Cmp HandleCmp(BinaryExpression expr)
{
var (lhsFieldIndex, type) = ExprAsTableField(expr.Left);

var rhs = ExprAsConstant(expr.Right);
var rhs = ExprAsRhs(expr.Right);
rhs = Convert.ChangeType(rhs, type);
var rhsWrite = fieldTypeInfos[lhsFieldIndex].Value.Write;
var erasedRhs = new ErasedValue((writer) => rhsWrite(writer, rhs));
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/database_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct DatabaseLogger {
pub tx: broadcast::Sender<bytes::Bytes>,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, serde::Deserialize)]
pub enum LogLevel {
Error,
Warn,
Expand Down
1 change: 1 addition & 0 deletions crates/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lazy_static.workspace = true
rand.workspace = true
prost.workspace = true
tempfile.workspace = true
serde.workspace = true

[dev-dependencies]
serial_test.workspace = true
16 changes: 16 additions & 0 deletions crates/testing/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use spacetimedb::protobuf::client_api;
use spacetimedb_client_api::{ControlStateReadAccess, ControlStateWriteAccess, DatabaseDef, NodeDelegate};
use spacetimedb_lib::sats;

pub use spacetimedb::database_logger::LogLevel;

use spacetimedb_standalone::StandaloneEnv;

pub fn start_runtime() -> Runtime {
Expand Down Expand Up @@ -180,3 +182,17 @@ pub static DEFAULT_CONFIG: Config = Config {
storage: Storage::Disk,
fsync: FsyncPolicy::Never,
};

/// Used to parse output from module logs.
///
/// Sync with: `core::database_logger::Record`. We can't use it
/// directly because the types are wrong for deserialization.
/// (Rust!)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what Rust! means here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither. This code was just moved around.

#[derive(serde::Deserialize)]
pub struct LoggerRecord {
pub level: LogLevel,
pub target: Option<String>,
pub filename: Option<String>,
pub line_number: Option<u32>,
pub message: String,
}
109 changes: 60 additions & 49 deletions crates/testing/tests/standalone_integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde_json::Value;
use serial_test::serial;
use spacetimedb_testing::modules::{CompilationMode, CompiledModule, DEFAULT_CONFIG};
use spacetimedb_testing::modules::{
CompilationMode, CompiledModule, LogLevel, LoggerRecord, ModuleHandle, DEFAULT_CONFIG,
};

fn init() {
let _ = env_logger::builder()
Expand All @@ -13,6 +14,24 @@ fn init() {
.try_init();
}

async fn read_logs(module: &ModuleHandle) -> Vec<String> {
module
.read_log(None)
.await
.trim()
.split('\n')
.map(|line| {
let record: LoggerRecord = serde_json::from_str(line).unwrap();
if matches!(record.level, LogLevel::Panic | LogLevel::Error | LogLevel::Warn) {
panic!("Found an error-like log line: {line}");
}
record.message
})
.skip_while(|line| line != "Database initialized")
.skip(1)
.collect::<Vec<_>>()
}

// The tests MUST be run in sequence because they read the OS environment
// and can cause a race when run in parallel.

Expand All @@ -22,27 +41,28 @@ fn test_calling_a_reducer_in_module(module_name: &'static str) {
CompiledModule::compile(module_name, CompilationMode::Debug).with_module_async(
DEFAULT_CONFIG,
|module| async move {
let json = r#"{"call": {"fn": "add", "args": ["Tyrion"]}}"#.to_string();
module.send(json).await.unwrap();
let json = r#"{"call": {"fn": "say_hello", "args": []}}"#.to_string();
let json = r#"{"call": {"fn": "add", "args": ["Tyrion", 24]}}"#.to_string();
module.send(json).await.unwrap();

let lines: Vec<Value> = module
.read_log(Some(10))
.await
.trim()
.split('\n')
.map(serde_json::from_str)
.collect::<serde_json::Result<_>>()
.unwrap();
let json = r#"{"call": {"fn": "add", "args": ["Cersei", 31]}}"#.to_string();
module.send(json).await.unwrap();

assert!(lines.len() >= 4);
let json = r#"{"call": {"fn": "say_hello", "args": []}}"#.to_string();
module.send(json).await.unwrap();

assert_eq!(lines[lines.len() - 2]["level"], "Info");
assert_eq!(lines[lines.len() - 2]["message"], "Hello, Tyrion!");
let json = r#"{"call": {"fn": "list_over_age", "args": [30]}}"#.to_string();
module.send(json).await.unwrap();

assert_eq!(lines[lines.len() - 1]["level"], "Info");
assert_eq!(lines[lines.len() - 1]["message"], "Hello, World!");
assert_eq!(
read_logs(&module).await,
[
"Hello, Tyrion!",
"Hello, Cersei!",
"Hello, World!",
"Cersei has age 31 >= 30",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bet she'd deny it.

]
.map(String::from)
);
},
);
}
Expand Down Expand Up @@ -72,15 +92,10 @@ fn test_calling_a_reducer_with_private_table() {
let json = r#"{"call": {"fn": "query_private", "args": []}}"#.to_string();
module.send(json).await.unwrap();

let lines = module.read_log(Some(11)).await;
let lines: Vec<&str> = lines.trim().split('\n').collect();

assert_eq!(lines.len(), 10);

let json: Value = serde_json::from_str(lines[8]).unwrap();
assert_eq!(json["message"], Value::String("Private, Tyrion!".to_string()));
let json: Value = serde_json::from_str(lines[9]).unwrap();
assert_eq!(json["message"], Value::String("Private, World!".to_string()));
assert_eq!(
read_logs(&module).await,
["Private, Tyrion!", "Private, World!",].map(String::from)
);
},
);
}
Expand All @@ -92,38 +107,34 @@ fn test_call_query_macro() {
DEFAULT_CONFIG,
|module| async move {
let json = r#"
{"call": {"fn": "test", "args":[
{"call": {"fn": "test", "args":[
{"x":0, "y":2, "z":"Macro"},
{"foo":"Foo"},
{"Foo": {} }
]}}"#
.to_string();
module.send(json).await.unwrap();

let lines = module.read_log(Some(13)).await;
let lines: Vec<&str> = lines.trim().split('\n').collect();
let logs = read_logs(&module).await;

assert_eq!(lines.len(), 13);
assert_eq!(logs[0], "BEGIN");
assert!(logs[1].starts_with("sender: "));
assert!(logs[2].starts_with("timestamp: "));

let json: Value = serde_json::from_str(lines[6]).unwrap();
assert_eq!(
json["message"],
Value::String("Row count before delete: 1000".to_string())
);
let json: Value = serde_json::from_str(lines[8]).unwrap();
assert_eq!(
json["message"],
Value::String("Row count after delete: 995".to_string())
);
let json: Value = serde_json::from_str(lines[9]).unwrap();
assert_eq!(
json["message"],
Value::String("Row count filtered by condition: 995".to_string())
);
let json: Value = serde_json::from_str(lines[11]).unwrap();
assert_eq!(
json["message"],
Value::String("Row count filtered by multi-column condition: 199".to_string())
logs[3..],
[
r#"bar: "Foo""#,
"Foo",
"Row count before delete: 1000",
r#"Inserted: TestE { id: 1, name: "Tyler" }"#,
"Row count after delete: 995",
"Row count filtered by condition: 995",
"MultiColumn",
"Row count filtered by multi-column condition: 199",
"END",
]
.map(String::from)
);
},
);
Expand Down
14 changes: 12 additions & 2 deletions modules/spacetimedb-quickstart-cs/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static partial class Module
public partial struct Person
{
public string Name;
public byte Age;
}

// Verify that all types compile via codegen successfully.
Expand Down Expand Up @@ -35,9 +36,9 @@ public partial struct Typecheck
}

[SpacetimeDB.Reducer("add")]
public static void Add(string name)
public static void Add(string name, byte age)
{
new Person { Name = name }.Insert();
new Person { Name = name, Age = age }.Insert();
}

[SpacetimeDB.Reducer("say_hello")]
Expand All @@ -49,4 +50,13 @@ public static void SayHello()
}
Log("Hello, World!");
}

[SpacetimeDB.Reducer("list_over_age")]
public static void ListOverAge(byte age)
{
foreach (var person in Person.Query(person => person.Age >= age))
{
Log($"{person.Name} has age {person.Age} >= {age}");
}
}
}
14 changes: 11 additions & 3 deletions modules/spacetimedb-quickstart/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use spacetimedb::{println, spacetimedb};
use spacetimedb::{println, query, spacetimedb};

#[spacetimedb(table)]
pub struct Person {
name: String,
age: u8,
}

#[spacetimedb(reducer)]
pub fn add(name: String) {
Person::insert(Person { name });
pub fn add(name: String, age: u8) {
Person::insert(Person { name, age });
}

#[spacetimedb(reducer)]
Expand All @@ -17,3 +18,10 @@ pub fn say_hello() {
}
println!("Hello, World!");
}

#[spacetimedb(reducer)]
pub fn list_over_age(age: u8) {
for person in query!(|person: Person| person.age >= age) {
println!("{} has age {} >= {}", person.name, person.age, age);
}
}