Basic Reducer functionality

This commit is contained in:
ookami125 2025-03-09 05:35:17 -04:00
parent 46a832ce42
commit cb2145b882
6 changed files with 628 additions and 445 deletions

View file

@ -0,0 +1,211 @@
const std = @import("std");
pub const types = @import("types.zig");
pub const SumTypeVariant = types.SumTypeVariant;
pub const SumType = types.SumType;
pub const ArrayType = types.ArrayType;
pub const AlgebraicType = types.AlgebraicType;
pub const Typespace = types.Typespace;
pub const RawIdentifier = types.RawIdentifier;
pub const AlgebraicTypeRef = types.AlgebraicTypeRef;
pub const RawIndexAlgorithm = types.RawIndexAlgorithm;
pub const RawIndexDefV9 = types.RawIndexDefV9;
pub const RawUniqueConstraintDataV9 = types.RawUniqueConstraintDataV9;
pub const RawConstraintDataV9 = types.RawConstraintDataV9;
pub const RawConstraintDefV9 = types.RawConstraintDefV9;
pub const RawSequenceDefV9 = types.RawSequenceDefV9;
pub const RawScheduleDefV9 = types.RawScheduleDefV9;
pub const TableType = types.TableType;
pub const TableAccess = types.TableAccess;
pub const RawTableDefV9 = types.RawTableDefV9;
pub const ProductTypeElement = types.ProductTypeElement;
pub const ProductType = types.ProductType;
pub const Lifecycle = types.Lifecycle;
pub const ReducerContext = types.ReducerContext;
pub const ReducerFn = types.ReducerFn;
pub const RawReducerDefV9 = types.RawReducerDefV9;
pub const RawScopedTypeNameV9 = types.RawScopedTypeNameV9;
pub const RawTypeDefV9 = types.RawTypeDefV9;
pub const RawMiscModuleExportV9 = types.RawMiscModuleExportV9;
pub const RawSql = types.RawSql;
pub const RawRowLevelSecurityDefV9 = types.RawRowLevelSecurityDefV9;
pub const RawModuleDefV9 = types.RawModuleDefV9;
fn serialize_raw_table_def_v9(array: *std.ArrayList(u8), val: RawTableDefV9) !void {
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.name.len))));
try array.appendSlice(val.name);
try serialize_algebraic_type_ref(array, val.product_type_ref);
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.primary_key.len))));
try array.appendSlice(std.mem.sliceAsBytes(val.primary_key));
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.indexes.len))));
for(val.indexes) |index| {
try serialize_raw_index_def_v9(array, index);
}
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.constraints.len))));
for(val.constraints) |constraint| {
try serialize_raw_constraint_def_v9(array, constraint);
}
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.sequences.len))));
for(val.sequences) |sequence| {
try serialize_raw_sequence_def_v9(array, sequence);
}
try array.appendSlice(&[_]u8{ @intFromBool(val.schedule == null) });
if(val.schedule) |schedule| {
try serialize_raw_schedule_def_v9(array, schedule);
}
try serialize_table_type(array, val.table_type);
try serialize_table_access(array, val.table_access);
}
fn serialize_raw_reducer_def_v9(array: *std.ArrayList(u8), val: RawReducerDefV9) !void {
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.name.len))));
try array.appendSlice(val.name);
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.params.elements.len))));
for(val.params.elements) |element| {
try serialize_product_type_element(array, element);
}
try array.appendSlice(&[_]u8{ @intFromBool(val.lifecycle == null) });
if(val.lifecycle) |lifecycle| {
try serialize_lifecycle(array, lifecycle);
}
}
fn serialize_lifecycle(array: *std.ArrayList(u8), val: Lifecycle) !void {
try array.appendSlice(&[_]u8{@intFromEnum(val)});
}
fn serialize_algebraic_type_ref(array: *std.ArrayList(u8), val: AlgebraicTypeRef) !void {
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.inner))));
}
fn serialize_raw_type_def_v9(array: *std.ArrayList(u8), val: RawTypeDefV9) !void {
try serialize_raw_scoped_type_name_v9(array, val.name);
try serialize_algebraic_type_ref(array, val.ty);
try serialize_bool(array, val.custom_ordering);
}
fn serialize_raw_scoped_type_name_v9(array: *std.ArrayList(u8), val: RawScopedTypeNameV9) !void {
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.scope.len))));
for(val.scope) |sub_scope| {
try serialize_raw_identifier(array, sub_scope);
}
try serialize_raw_identifier(array, val.name);
}
fn serialize_raw_identifier(array: *std.ArrayList(u8), val: RawIdentifier) !void {
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.len))));
try array.appendSlice(val);
}
fn serialize_bool(array: *std.ArrayList(u8), val: bool) !void {
try array.appendSlice(&[_]u8{@intFromBool(val)});
}
fn serialize_raw_misc_module_export_v9(array: *std.ArrayList(u8), val: RawMiscModuleExportV9) !void {
_ = array;
_ = val;
unreachable;
}
fn serialize_raw_row_level_security_def_v9(array: *std.ArrayList(u8), val: RawRowLevelSecurityDefV9) !void {
_ = array;
_ = val;
unreachable;
}
fn serialize_raw_index_def_v9(array: *std.ArrayList(u8), val: RawIndexDefV9) !void {
_ = array;
_ = val;
unreachable;
}
fn serialize_raw_constraint_def_v9(array: *std.ArrayList(u8), val: RawConstraintDefV9) !void {
_ = array;
_ = val;
unreachable;
}
fn serialize_raw_sequence_def_v9(array: *std.ArrayList(u8), val: RawSequenceDefV9) !void {
_ = array;
_ = val;
unreachable;
}
fn serialize_raw_schedule_def_v9(array: *std.ArrayList(u8), val: RawScheduleDefV9) !void {
_ = array;
_ = val;
unreachable;
}
fn serialize_table_type(array: *std.ArrayList(u8), val: TableType) !void {
try array.appendSlice(&[_]u8{@intFromEnum(val)});
}
fn serialize_table_access(array: *std.ArrayList(u8), val: TableAccess) !void {
try array.appendSlice(&[_]u8{@intFromEnum(val)});
}
fn serialize_product_type_element(array: *std.ArrayList(u8), val: ProductTypeElement) !void {
try array.appendSlice(&[_]u8{ @intFromBool(val.name == null) });
if(val.name) |name| {
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(name.len))));
try array.appendSlice(name);
}
try serialize_algebraic_type(array, val.algebraic_type);
}
fn serialize_product_type(array: *std.ArrayList(u8), val: ProductType) std.mem.Allocator.Error!void {
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.elements.len))));
for(val.elements) |element| {
try serialize_product_type_element(array, element);
}
}
fn serialize_algebraic_type(array: *std.ArrayList(u8), val: AlgebraicType) !void {
switch(val) {
AlgebraicType.Product => |product| {
try array.appendSlice(&[_]u8{@intFromEnum(val)});
try serialize_product_type(array, product);
},
else => try array.appendSlice(&[_]u8{@intFromEnum(val)}),
}
}
fn serialize_typespace(array: *std.ArrayList(u8), val: Typespace) !void {
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.types.len))));
for(val.types) |_type| {
try serialize_algebraic_type(array, _type);
}
}
pub fn serialize_module(array: *std.ArrayList(u8), val: RawModuleDefV9) !void {
try array.appendSlice(&[_]u8{1});
try serialize_typespace(array, val.typespace);
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.tables.len))));
for(val.tables) |table| {
try serialize_raw_table_def_v9(array, table);
}
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.reducers.len))));
for(val.reducers) |reducer| {
try serialize_raw_reducer_def_v9(array, reducer);
}
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.types.len))));
for(val.types) |_type| {
try serialize_raw_type_def_v9(array, _type);
}
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.misc_exports.len))));
for(val.misc_exports) |misc_export| {
try serialize_raw_misc_module_export_v9(array, misc_export);
}
try array.appendSlice(&std.mem.toBytes(@as(u32, @intCast(val.row_level_security.len))));
for(val.row_level_security) |rls| {
try serialize_raw_row_level_security_def_v9(array, rls);
}
}

167
src/spacetime/types.zig Normal file
View file

@ -0,0 +1,167 @@
pub const Str = []const u8;
pub const SumTypeVariant = struct {
name: ?Str,
algebraic_type: AlgebraicType,
};
pub const SumType = struct {
variants: []const SumTypeVariant,
};
pub const ArrayType = struct {
/// The base type every element of the array has.
elem_ty: []const AlgebraicType,
};
pub const AlgebraicType = union(enum) {
Ref: AlgebraicTypeRef,
Sum: SumType,
Product: ProductType,
Array: ArrayType,
String: void,
Bool: void,
I8: void,
U8: void,
I16: void,
U16: void,
I32: void,
U32: void,
I64: void,
U64: void,
I128: void,
U128: void,
I256: void,
U256: void,
F32: void,
F64: void,
};
pub const Typespace = struct {
types: []const AlgebraicType,
};
pub const RawIdentifier = Str;
pub const AlgebraicTypeRef = struct {
inner: u32,
};
pub const RawIndexAlgorithm = union {
BTree: []const u16,
Hash: []const u16,
Direct: u16,
};
pub const RawIndexDefV9 = struct {
name: ?Str,
accessor_name: ?Str,
algorithm: RawIndexAlgorithm,
};
pub const RawUniqueConstraintDataV9 = union {
Columns: u16,
};
pub const RawConstraintDataV9 = union {
unique: RawUniqueConstraintDataV9,
};
pub const RawConstraintDefV9 = struct {
name: ?Str,
data: RawConstraintDataV9
};
pub const RawSequenceDefV9 = struct {
Name: ?Str,
Column: u16,
Start: ?i128,
MinValue: ?i128,
MaxValue: ?i128,
Increment: i128
};
pub const RawScheduleDefV9 = struct {
Name: ?Str,
ReducerName: Str,
ScheduledAtColumn: u16
};
pub const TableType = enum {
System,
User,
};
pub const TableAccess = enum {
Public,
Private,
};
pub const RawTableDefV9 = struct {
name: RawIdentifier,
product_type_ref: AlgebraicTypeRef,
primary_key: []const u16,
indexes: []const RawIndexDefV9,
constraints: []const RawConstraintDefV9,
sequences: []const RawSequenceDefV9,
schedule: ?RawScheduleDefV9,
table_type: TableType,
table_access: TableAccess,
};
pub const ProductTypeElement = struct {
name: ?Str,
algebraic_type: AlgebraicType,
};
pub const ProductType = struct {
elements: []const ProductTypeElement,
};
pub const Lifecycle = enum {
Init,
OnConnect,
OnDisconnect,
};
pub const ReducerContext = struct {
indentity: u256,
};
pub const ReducerFn = fn(*ReducerContext) void;
pub const RawReducerDefV9 = struct {
name: RawIdentifier,
params: ProductType,
lifecycle: ?Lifecycle,
};
pub const RawScopedTypeNameV9 = struct {
scope: []RawIdentifier,
name: RawIdentifier,
};
pub const RawTypeDefV9 = struct {
name: RawScopedTypeNameV9,
ty: AlgebraicTypeRef,
custom_ordering: bool,
};
pub const RawMiscModuleExportV9 = enum {
RESERVED,
};
pub const RawSql = []u8;
pub const RawRowLevelSecurityDefV9 = struct {
sql: RawSql,
};
pub const RawModuleDefV9 = struct {
typespace: Typespace,
tables: []const RawTableDefV9,
reducers: []const RawReducerDefV9,
types: []const RawTypeDefV9,
misc_exports: []const RawMiscModuleExportV9,
row_level_security: []const RawRowLevelSecurityDefV9,
};