-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.zig
124 lines (102 loc) · 3.17 KB
/
event.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const std = @import("std");
const mem = std.mem;
const testing = std.testing;
const net = std.net;
const protocol = @import("protocol.zig");
const MessageReader = protocol.MessageReader;
const MessageWriter = protocol.MessageWriter;
const testutils = @import("testutils.zig");
pub const TopologyChangeType = enum {
NEW_NODE,
REMOVED_NODE,
};
pub const StatusChangeType = enum {
UP,
DOWN,
};
pub const SchemaChangeType = enum {
CREATED,
UPDATED,
DROPPED,
};
pub const SchemaChangeTarget = enum {
KEYSPACE,
TABLE,
TYPE,
FUNCTION,
AGGREGATE,
};
pub const SchemaChangeOptions = struct {
keyspace: []const u8,
object_name: []const u8,
arguments: ?[]const []const u8,
pub fn init() SchemaChangeOptions {
return SchemaChangeOptions{
.keyspace = &[_]u8{},
.object_name = &[_]u8{},
.arguments = null,
};
}
};
pub const TopologyChange = struct {
type: TopologyChangeType,
node_address: net.Address,
};
pub const StatusChange = struct {
type: StatusChangeType,
node_address: net.Address,
};
pub const SchemaChange = struct {
const Self = @This();
type: SchemaChangeType,
target: SchemaChangeTarget,
options: SchemaChangeOptions,
pub fn read(allocator: mem.Allocator, mr: *MessageReader) !Self {
var change = Self{
.type = undefined,
.target = undefined,
.options = undefined,
};
change.type = std.meta.stringToEnum(SchemaChangeType, (try mr.readString(allocator))) orelse return error.InvalidSchemaChangeType;
change.target = std.meta.stringToEnum(SchemaChangeTarget, (try mr.readString(allocator))) orelse return error.InvalidSchemaChangeTarget;
change.options = SchemaChangeOptions.init();
switch (change.target) {
.KEYSPACE => {
change.options.keyspace = try mr.readString(allocator);
},
.TABLE, .TYPE => {
change.options.keyspace = try mr.readString(allocator);
change.options.object_name = try mr.readString(allocator);
},
.FUNCTION, .AGGREGATE => {
change.options.keyspace = try mr.readString(allocator);
change.options.object_name = try mr.readString(allocator);
change.options.arguments = try mr.readStringList(allocator);
},
}
return change;
}
};
pub const EventType = enum {
TOPOLOGY_CHANGE,
STATUS_CHANGE,
SCHEMA_CHANGE,
};
pub const Event = union(EventType) {
TOPOLOGY_CHANGE: TopologyChange,
STATUS_CHANGE: StatusChange,
SCHEMA_CHANGE: SchemaChange,
};
test "schema change options" {
var arena = testutils.arenaAllocator();
defer arena.deinit();
var options = SchemaChangeOptions.init();
options.keyspace = try arena.allocator().dupe(u8, "foobar");
options.object_name = try arena.allocator().dupe(u8, "barbaz");
var arguments = try arena.allocator().alloc([]const u8, 4);
var i: usize = 0;
while (i < arguments.len) : (i += 1) {
arguments[i] = try arena.allocator().dupe(u8, "hello");
}
options.arguments = arguments;
}