-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.zig
More file actions
206 lines (187 loc) · 9 KB
/
main.zig
File metadata and controls
206 lines (187 loc) · 9 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const std = @import("std");
const daemon = @import("src/daemon.zig");
// WILD Database Production Daemon
// Command-line interface for running WILD in production mode
const VERSION = "1.0.0";
const CommandLineArgs = struct {
mode: daemon.WildDaemon.DaemonConfig.ReplicationMode = .none,
capacity: u64 = 100000,
bind_address: []const u8 = "127.0.0.1",
port: u16 = 7878,
enable_wal: bool = false,
wal_path: ?[]const u8 = null,
replication_port: ?u16 = null,
primary_address: ?[]const u8 = null,
primary_port: ?u16 = null,
auth_secret: ?[]const u8 = null,
help: bool = false,
version: bool = false,
};
fn printUsage(program_name: []const u8) void {
std.debug.print("WILD Database v{s} - Production Daemon\n\n", .{VERSION});
std.debug.print("Usage: {s} [OPTIONS]\n\n", .{program_name});
std.debug.print("Options:\n", .{});
std.debug.print(" --mode MODE Operation mode: none, primary, replica (default: none)\n", .{});
std.debug.print(" --capacity NUM Database capacity in records (default: 100000)\n", .{});
std.debug.print(" --bind-address ADDR Server bind address (default: 127.0.0.1)\n", .{});
std.debug.print(" --port PORT Server port (default: 7878)\n", .{});
std.debug.print(" --enable-wal Enable Write-Ahead Log (required for replication)\n", .{});
std.debug.print(" --wal-path PATH Path to WAL file (required if --enable-wal)\n", .{});
std.debug.print(" --replication-port PORT Port for replica connections (primary mode)\n", .{});
std.debug.print(" --primary-address ADDR Primary server address (replica mode)\n", .{});
std.debug.print(" --primary-port PORT Primary server port (replica mode)\n", .{});
std.debug.print(" --auth-secret SECRET Shared authentication secret (required for connections)\n", .{});
std.debug.print(" --help, -h Show this help message\n", .{});
std.debug.print(" --version, -v Show version information\n", .{});
std.debug.print("\nExamples:\n", .{});
std.debug.print(" # Standalone database without replication\n", .{});
std.debug.print(" {s} --capacity 1000000\n\n", .{program_name});
std.debug.print(" # Primary with replication enabled\n", .{});
std.debug.print(" {s} --mode primary --enable-wal --wal-path primary.wal --replication-port 9001 --auth-secret mysecret\n\n", .{program_name});
std.debug.print(" # Replica connecting to primary\n", .{});
std.debug.print(" {s} --mode replica --primary-address 192.168.1.100 --primary-port 9001 --auth-secret mysecret\n\n", .{program_name});
}
fn parseArguments(allocator: std.mem.Allocator, args: [][:0]u8) !CommandLineArgs {
var parsed_args = CommandLineArgs{};
var i: usize = 1; // Skip program name
while (i < args.len) : (i += 1) {
const arg = args[i];
if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) {
parsed_args.help = true;
} else if (std.mem.eql(u8, arg, "--version") or std.mem.eql(u8, arg, "-v")) {
parsed_args.version = true;
} else if (std.mem.eql(u8, arg, "--mode")) {
i += 1;
if (i >= args.len) return error.MissingValue;
const mode_str = args[i];
if (std.mem.eql(u8, mode_str, "none")) {
parsed_args.mode = .none;
} else if (std.mem.eql(u8, mode_str, "primary")) {
parsed_args.mode = .primary;
} else if (std.mem.eql(u8, mode_str, "replica")) {
parsed_args.mode = .replica;
} else {
std.debug.print("Invalid mode: {s}. Use 'none', 'primary', or 'replica'\n", .{mode_str});
return error.InvalidMode;
}
} else if (std.mem.eql(u8, arg, "--capacity")) {
i += 1;
if (i >= args.len) return error.MissingValue;
parsed_args.capacity = try std.fmt.parseInt(u64, args[i], 10);
} else if (std.mem.eql(u8, arg, "--bind-address")) {
i += 1;
if (i >= args.len) return error.MissingValue;
parsed_args.bind_address = try allocator.dupe(u8, args[i]);
} else if (std.mem.eql(u8, arg, "--port")) {
i += 1;
if (i >= args.len) return error.MissingValue;
parsed_args.port = try std.fmt.parseInt(u16, args[i], 10);
} else if (std.mem.eql(u8, arg, "--enable-wal")) {
parsed_args.enable_wal = true;
} else if (std.mem.eql(u8, arg, "--wal-path")) {
i += 1;
if (i >= args.len) return error.MissingValue;
parsed_args.wal_path = try allocator.dupe(u8, args[i]);
} else if (std.mem.eql(u8, arg, "--replication-port")) {
i += 1;
if (i >= args.len) return error.MissingValue;
parsed_args.replication_port = try std.fmt.parseInt(u16, args[i], 10);
} else if (std.mem.eql(u8, arg, "--primary-address")) {
i += 1;
if (i >= args.len) return error.MissingValue;
parsed_args.primary_address = try allocator.dupe(u8, args[i]);
} else if (std.mem.eql(u8, arg, "--primary-port")) {
i += 1;
if (i >= args.len) return error.MissingValue;
parsed_args.primary_port = try std.fmt.parseInt(u16, args[i], 10);
} else if (std.mem.eql(u8, arg, "--auth-secret")) {
i += 1;
if (i >= args.len) return error.MissingValue;
parsed_args.auth_secret = try allocator.dupe(u8, args[i]);
} else {
std.debug.print("Unknown argument: {s}\n", .{arg});
return error.UnknownArgument;
}
}
return parsed_args;
}
fn buildDaemonConfig(args: CommandLineArgs) daemon.WildDaemon.DaemonConfig {
return daemon.WildDaemon.DaemonConfig{
.capacity = args.capacity,
.bind_address = args.bind_address,
.port = args.port,
.enable_wal = args.enable_wal,
.wal_path = args.wal_path,
.replication_mode = args.mode,
.replication_port = args.replication_port,
.primary_address = args.primary_address,
.primary_port = args.primary_port,
.auth_secret = args.auth_secret,
};
}
pub fn main() !void {
const allocator = std.heap.page_allocator;
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 1) {
printUsage("wild");
return;
}
const parsed_args = parseArguments(allocator, args) catch |err| {
switch (err) {
error.MissingValue => std.debug.print("Error: Missing value for argument\n", .{}),
error.InvalidMode => {}, // Already printed
error.UnknownArgument => {}, // Already printed
else => std.debug.print("Error parsing arguments: {}\n", .{err}),
}
std.debug.print("Use --help for usage information\n", .{});
std.process.exit(1);
};
if (parsed_args.help) {
printUsage(args[0]);
return;
}
if (parsed_args.version) {
std.debug.print("WILD Database v{s}\n", .{VERSION});
return;
}
// Build daemon configuration
const config = buildDaemonConfig(parsed_args);
// Validate configuration
config.validate() catch |err| {
switch (err) {
error.WALRequiredForReplication => {
std.debug.print("Error: WAL must be enabled for replication. Use --enable-wal --wal-path <path>\n", .{});
},
error.WALPathRequired => {
std.debug.print("Error: WAL path required when WAL is enabled. Use --wal-path <path>\n", .{});
},
error.ReplicationPortRequired => {
std.debug.print("Error: Replication port required for primary mode. Use --replication-port <port>\n", .{});
},
error.PrimaryAddressRequired => {
std.debug.print("Error: Primary address and port required for replica mode. Use --primary-address <addr> --primary-port <port>\n", .{});
},
}
std.process.exit(1);
};
// Print startup banner
std.debug.print("🗲 WILD Database v{s} Starting Up\n", .{VERSION});
std.debug.print("Configuration:\n", .{});
std.debug.print(" Mode: {s}\n", .{@tagName(config.replication_mode)});
std.debug.print(" Capacity: {} records\n", .{config.capacity});
std.debug.print(" Bind: {s}:{}\n", .{ config.bind_address, config.port });
std.debug.print(" WAL: {s}\n", .{if (config.enable_wal) "enabled" else "disabled"});
if (config.wal_path) |path| {
std.debug.print(" WAL Path: {s}\n", .{path});
}
if (config.replication_mode == .primary and config.replication_port != null) {
std.debug.print(" Replication Port: {}\n", .{config.replication_port.?});
}
if (config.replication_mode == .replica) {
std.debug.print(" Primary: {s}:{}\n", .{ config.primary_address.?, config.primary_port.? });
}
std.debug.print("\n", .{});
// Run the daemon
try daemon.runDaemon(allocator, config);
}