tigerbeetle-node 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/package.json +2 -2
  2. package/scripts/postinstall.sh +2 -2
  3. package/src/node.zig +17 -15
  4. package/src/tigerbeetle/scripts/install.sh +1 -1
  5. package/src/tigerbeetle/scripts/install_zig.bat +3 -3
  6. package/src/tigerbeetle/scripts/install_zig.sh +4 -2
  7. package/src/tigerbeetle/scripts/lint.zig +8 -2
  8. package/src/tigerbeetle/src/benchmark.zig +8 -6
  9. package/src/tigerbeetle/src/cli.zig +6 -4
  10. package/src/tigerbeetle/src/config.zig +2 -2
  11. package/src/tigerbeetle/src/demo.zig +119 -97
  12. package/src/tigerbeetle/src/demo_01_create_accounts.zig +5 -3
  13. package/src/tigerbeetle/src/demo_02_lookup_accounts.zig +2 -3
  14. package/src/tigerbeetle/src/demo_03_create_transfers.zig +5 -3
  15. package/src/tigerbeetle/src/demo_04_create_transfers_two_phase_commit.zig +5 -3
  16. package/src/tigerbeetle/src/demo_05_accept_transfers.zig +5 -3
  17. package/src/tigerbeetle/src/demo_06_reject_transfers.zig +5 -3
  18. package/src/tigerbeetle/src/demo_07_lookup_transfers.zig +2 -3
  19. package/src/tigerbeetle/src/io/benchmark.zig +238 -0
  20. package/src/tigerbeetle/src/{io_darwin.zig → io/darwin.zig} +88 -127
  21. package/src/tigerbeetle/src/io/linux.zig +933 -0
  22. package/src/tigerbeetle/src/io/test.zig +621 -0
  23. package/src/tigerbeetle/src/io.zig +7 -1328
  24. package/src/tigerbeetle/src/main.zig +18 -10
  25. package/src/tigerbeetle/src/message_bus.zig +43 -54
  26. package/src/tigerbeetle/src/message_pool.zig +3 -2
  27. package/src/tigerbeetle/src/simulator.zig +41 -37
  28. package/src/tigerbeetle/src/state_machine.zig +58 -27
  29. package/src/tigerbeetle/src/storage.zig +49 -46
  30. package/src/tigerbeetle/src/test/cluster.zig +2 -2
  31. package/src/tigerbeetle/src/test/message_bus.zig +6 -6
  32. package/src/tigerbeetle/src/test/network.zig +3 -3
  33. package/src/tigerbeetle/src/test/packet_simulator.zig +32 -29
  34. package/src/tigerbeetle/src/test/state_checker.zig +1 -1
  35. package/src/tigerbeetle/src/test/state_machine.zig +4 -0
  36. package/src/tigerbeetle/src/test/storage.zig +23 -19
  37. package/src/tigerbeetle/src/test/time.zig +2 -2
  38. package/src/tigerbeetle/src/tigerbeetle.zig +6 -128
  39. package/src/tigerbeetle/src/time.zig +6 -5
  40. package/src/tigerbeetle/src/vsr/client.zig +7 -7
  41. package/src/tigerbeetle/src/vsr/clock.zig +26 -43
  42. package/src/tigerbeetle/src/vsr/journal.zig +7 -6
  43. package/src/tigerbeetle/src/vsr/marzullo.zig +6 -3
  44. package/src/tigerbeetle/src/vsr/replica.zig +49 -46
  45. package/src/tigerbeetle/src/vsr.zig +24 -20
  46. package/src/translate.zig +55 -55
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tigerbeetle-node",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "TigerBeetle Node.js client",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -37,7 +37,7 @@
37
37
  "benchmark": "./scripts/benchmark.sh",
38
38
  "build": "yarn build:tsc && yarn build:zig",
39
39
  "build:tsc": "tsc",
40
- "build:zig": "mkdir -p dist && ZIG_SYSTEM_LINKER_HACK=1 zig/zig build-lib -mcpu=baseline -OReleaseSafe -dynamic -lc -isystem build/node-$(node --version)/include/node src/node.zig -fallow-shlib-undefined -femit-bin=dist/client.node",
40
+ "build:zig": "mkdir -p dist && zig/zig build-lib -mcpu=baseline -OReleaseSafe -dynamic -lc -isystem build/node-$(node --version)/include/node src/node.zig -fallow-shlib-undefined -femit-bin=dist/client.node",
41
41
  "clean": "rm -rf build dist node_modules src/zig-cache zig",
42
42
  "install:zig": "./src/tigerbeetle/scripts/install_zig.sh",
43
43
  "postinstall": "./scripts/postinstall.sh",
@@ -1,6 +1,6 @@
1
1
  #!/bin/bash
2
2
  set -e
3
3
 
4
- ./src/tigerbeetle/scripts/install_zig.sh 0.8.1
4
+ ./src/tigerbeetle/scripts/install_zig.sh
5
5
  ./scripts/download_node_headers.sh
6
- mkdir -p dist && ZIG_SYSTEM_LINKER_HACK=1 zig/zig build-lib -mcpu=baseline -OReleaseSafe -dynamic -lc -isystem build/node-$(node --version)/include/node src/node.zig -fallow-shlib-undefined -femit-bin=dist/client.node
6
+ mkdir -p dist && zig/zig build-lib -mcpu=baseline -OReleaseSafe -dynamic -lc -isystem build/node-$(node --version)/include/node src/node.zig -fallow-shlib-undefined -femit-bin=dist/client.node
package/src/node.zig CHANGED
@@ -45,7 +45,7 @@ export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi
45
45
 
46
46
  const allocator = std.heap.c_allocator;
47
47
  var global = Globals.init(allocator, env) catch {
48
- std.log.emerg("Failed to initialise environment.\n", .{});
48
+ std.log.err("Failed to initialise environment.\n", .{});
49
49
  return null;
50
50
  };
51
51
  errdefer global.deinit();
@@ -57,7 +57,7 @@ export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi
57
57
  // state.
58
58
  translate.set_instance_data(
59
59
  env,
60
- @ptrCast(*c_void, @alignCast(@alignOf(u8), global)),
60
+ @ptrCast(*anyopaque, @alignCast(@alignOf(u8), global)),
61
61
  Globals.destroy,
62
62
  ) catch {
63
63
  global.deinit();
@@ -68,11 +68,11 @@ export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi
68
68
  }
69
69
 
70
70
  const Globals = struct {
71
- allocator: *std.mem.Allocator,
71
+ allocator: std.mem.Allocator,
72
72
  io: IO,
73
73
  napi_undefined: c.napi_value,
74
74
 
75
- pub fn init(allocator: *std.mem.Allocator, env: c.napi_env) !*Globals {
75
+ pub fn init(allocator: std.mem.Allocator, env: c.napi_env) !*Globals {
76
76
  const self = try allocator.create(Globals);
77
77
  errdefer allocator.destroy(self);
78
78
 
@@ -93,7 +93,7 @@ const Globals = struct {
93
93
  };
94
94
  errdefer self.io.deinit();
95
95
 
96
- if (c.napi_get_undefined(env, &self.napi_undefined) != .napi_ok) {
96
+ if (c.napi_get_undefined(env, &self.napi_undefined) != c.napi_ok) {
97
97
  return translate.throw(env, "Failed to capture the value of \"undefined\".");
98
98
  }
99
99
 
@@ -105,13 +105,16 @@ const Globals = struct {
105
105
  self.allocator.destroy(self);
106
106
  }
107
107
 
108
- pub fn destroy(env: c.napi_env, data: ?*c_void, hint: ?*c_void) callconv(.C) void {
108
+ pub fn destroy(env: c.napi_env, data: ?*anyopaque, hint: ?*anyopaque) callconv(.C) void {
109
+ _ = env;
110
+ _ = hint;
111
+
109
112
  const self = globalsCast(data.?);
110
113
  self.deinit();
111
114
  }
112
115
  };
113
116
 
114
- fn globalsCast(globals_raw: *c_void) *Globals {
117
+ fn globalsCast(globals_raw: *anyopaque) *Globals {
115
118
  return @ptrCast(*Globals, @alignCast(@alignOf(Globals), globals_raw));
116
119
  }
117
120
 
@@ -123,7 +126,7 @@ const Context = struct {
123
126
 
124
127
  fn create(
125
128
  env: c.napi_env,
126
- allocator: *std.mem.Allocator,
129
+ allocator: std.mem.Allocator,
127
130
  io: *IO,
128
131
  cluster: u32,
129
132
  addresses_raw: []const u8,
@@ -163,7 +166,7 @@ const Context = struct {
163
166
  }
164
167
  };
165
168
 
166
- fn contextCast(context_raw: *c_void) !*Context {
169
+ fn contextCast(context_raw: *anyopaque) !*Context {
167
170
  return @ptrCast(*Context, @alignCast(@alignOf(Context), context_raw));
168
171
  }
169
172
 
@@ -522,7 +525,7 @@ fn encode_napi_results_array(
522
525
  fn init(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
523
526
  var argc: usize = 1;
524
527
  var argv: [1]c.napi_value = undefined;
525
- if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != .napi_ok) {
528
+ if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != c.napi_ok) {
526
529
  translate.throw(env, "Failed to get args.") catch return null;
527
530
  }
528
531
  if (argc != 1) translate.throw(
@@ -555,7 +558,7 @@ fn init(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
555
558
  fn request(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
556
559
  var argc: usize = 4;
557
560
  var argv: [4]c.napi_value = undefined;
558
- if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != .napi_ok) {
561
+ if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != c.napi_ok) {
559
562
  translate.throw(env, "Failed to get args.") catch return null;
560
563
  }
561
564
 
@@ -609,7 +612,7 @@ fn request(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_valu
609
612
  fn raw_request(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
610
613
  var argc: usize = 4;
611
614
  var argv: [4]c.napi_value = undefined;
612
- if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != .napi_ok) {
615
+ if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != c.napi_ok) {
613
616
  translate.throw(env, "Failed to get args.") catch return null;
614
617
  }
615
618
 
@@ -730,11 +733,10 @@ fn on_result(user_data: u128, operation: Operation, results: Client.Error![]cons
730
733
  fn tick(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
731
734
  var argc: usize = 1;
732
735
  var argv: [1]c.napi_value = undefined;
733
- if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != .napi_ok) {
736
+ if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != c.napi_ok) {
734
737
  translate.throw(env, "Failed to get args.") catch return null;
735
738
  }
736
739
 
737
- const allocator = std.heap.c_allocator;
738
740
  if (argc != 1) translate.throw(
739
741
  env,
740
742
  "Function tick() requires 1 argument exactly.",
@@ -760,7 +762,7 @@ fn tick(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
760
762
  fn deinit(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
761
763
  var argc: usize = 1;
762
764
  var argv: [1]c.napi_value = undefined;
763
- if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != .napi_ok) {
765
+ if (c.napi_get_cb_info(env, info, &argc, &argv, null, null) != c.napi_ok) {
764
766
  translate.throw(env, "Failed to get args.") catch return null;
765
767
  }
766
768
 
@@ -1,6 +1,6 @@
1
1
  #!/bin/bash
2
2
  set -e
3
- scripts/install_zig.sh 0.8.1
3
+ scripts/install_zig.sh
4
4
  echo "Building TigerBeetle..."
5
5
  zig/zig build -Dcpu=baseline -Drelease-safe
6
6
  mv zig-out/bin/tigerbeetle .
@@ -1,10 +1,10 @@
1
1
  @echo off
2
2
 
3
- set DEFAULT_RELEASE=0.8.1
3
+ set ZIG_RELEASE_DEFAULT=0.9.0
4
4
 
5
5
  :: Determine the Zig build:
6
6
  if "%~1"=="" (
7
- set ZIG_RELEASE=%DEFAULT_RELEASE%
7
+ set ZIG_RELEASE=%ZIG_RELEASE_DEFAULT%
8
8
  ) else if "%~1"=="latest" (
9
9
  set ZIG_RELEASE=builds
10
10
  ) else (
@@ -106,4 +106,4 @@ if exist %ZIG_TARBALL% (
106
106
  exit 1
107
107
  )
108
108
 
109
- echo "Congratulations, you have successfully installed Zig version %ZIG_RELEASE%. Enjoy!"
109
+ echo "Congratulations, you have successfully installed Zig version %ZIG_RELEASE%. Enjoy!"
@@ -1,9 +1,11 @@
1
1
  #!/bin/bash
2
2
  set -e
3
3
 
4
- # Default to the 0.8.1 build, or allow the latest dev build, or an explicit release version:
4
+ ZIG_RELEASE_DEFAULT="0.9.0"
5
+
6
+ # Default to the release build, or allow the latest dev build, or an explicit release version:
5
7
  if [ -z "$1" ]; then
6
- ZIG_RELEASE="0.8.1"
8
+ ZIG_RELEASE=$ZIG_RELEASE_DEFAULT
7
9
  elif [ "$1" == "latest" ]; then
8
10
  ZIG_RELEASE="builds"
9
11
  else
@@ -23,7 +23,7 @@ var file_stats = std.ArrayListUnmanaged(Stats){};
23
23
  var seen = std.AutoArrayHashMapUnmanaged(fs.File.INode, void){};
24
24
 
25
25
  var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
26
- const gpa = &general_purpose_allocator.allocator;
26
+ const gpa = general_purpose_allocator.allocator();
27
27
 
28
28
  pub fn main() !void {
29
29
  const argv = std.os.argv;
@@ -119,7 +119,13 @@ fn lint_file(file_path: []const u8, dir: fs.Dir, sub_path: []const u8) LintError
119
119
  // Add to set after no longer possible to get error.IsDir.
120
120
  if (try seen.fetchPut(gpa, stat.inode, {})) |_| return;
121
121
 
122
- const source = try source_file.readToEndAlloc(gpa, math.maxInt(usize));
122
+ const source = try source_file.readToEndAllocOptions(
123
+ gpa,
124
+ math.maxInt(usize),
125
+ null,
126
+ @alignOf(u8),
127
+ 0,
128
+ );
123
129
  try check_line_length(source, file_path);
124
130
 
125
131
  var tree = try std.zig.parse(gpa, source);
@@ -1,4 +1,5 @@
1
1
  const std = @import("std");
2
+ const builtin = @import("builtin");
2
3
  const assert = std.debug.assert;
3
4
  const config = @import("config.zig");
4
5
 
@@ -30,7 +31,7 @@ const BATCHES: f32 = MAX_TRANSFERS / BATCH_SIZE;
30
31
  const TOTAL_BATCHES = @ceil(BATCHES);
31
32
 
32
33
  const log = std.log;
33
- pub const log_level: std.log.Level = .notice;
34
+ pub const log_level: std.log.Level = .info;
34
35
 
35
36
  var accounts = [_]Account{
36
37
  Account{
@@ -65,13 +66,14 @@ pub fn main() !void {
65
66
  const stdout = std.io.getStdOut().writer();
66
67
  const stderr = std.io.getStdErr().writer();
67
68
 
68
- if (std.builtin.mode != .ReleaseSafe and std.builtin.mode != .ReleaseFast) {
69
+ if (builtin.mode != .ReleaseSafe and builtin.mode != .ReleaseFast) {
69
70
  try stderr.print("Benchmark must be built as ReleaseSafe for minimum performance.\n", .{});
70
71
  }
71
72
 
72
73
  var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
73
74
  defer arena.deinit();
74
- const allocator = &arena.allocator;
75
+
76
+ const allocator = arena.allocator();
75
77
 
76
78
  const client_id = std.crypto.random.int(u128);
77
79
  const cluster_id: u32 = 0;
@@ -90,7 +92,7 @@ pub fn main() !void {
90
92
  message_bus.set_on_message(*Client, &client, Client.on_message);
91
93
 
92
94
  // Pre-allocate a million transfers:
93
- var transfers = try arena.allocator.alloc(Transfer, MAX_TRANSFERS);
95
+ const transfers = try arena.allocator().alloc(Transfer, MAX_TRANSFERS);
94
96
  for (transfers) |*transfer, index| {
95
97
  transfer.* = .{
96
98
  .id = index,
@@ -106,7 +108,7 @@ pub fn main() !void {
106
108
  }
107
109
 
108
110
  // Pre-allocate a million commits:
109
- var commits: ?[]Commit = if (IS_TWO_PHASE_COMMIT) try arena.allocator.alloc(Commit, MAX_TRANSFERS) else null;
111
+ const commits: ?[]Commit = if (IS_TWO_PHASE_COMMIT) try arena.allocator().alloc(Commit, MAX_TRANSFERS) else null;
110
112
  if (commits) |all_commits| {
111
113
  for (all_commits) |*commit, index| {
112
114
  commit.* = .{
@@ -254,7 +256,7 @@ const TimedQueue = struct {
254
256
  pub fn lap(user_data: u128, operation: Operation, results: Client.Error![]const u8) void {
255
257
  const now = std.time.milliTimestamp();
256
258
  const value = results catch |err| {
257
- log.emerg("Client returned error={o}", .{@errorName(err)});
259
+ log.err("Client returned error={o}", .{@errorName(err)});
258
260
  @panic("Client returned error during benchmarking.");
259
261
  };
260
262
 
@@ -87,7 +87,7 @@ pub const Command = union(enum) {
87
87
 
88
88
  /// Parse the command line arguments passed to the tigerbeetle binary.
89
89
  /// Exits the program with a non-zero exit code if an error is found.
90
- pub fn parse_args(allocator: *std.mem.Allocator) Command {
90
+ pub fn parse_args(allocator: std.mem.Allocator) Command {
91
91
  var maybe_cluster: ?[]const u8 = null;
92
92
  var maybe_replica: ?[]const u8 = null;
93
93
  var maybe_addresses: ?[]const u8 = null;
@@ -118,8 +118,10 @@ pub fn parse_args(allocator: *std.mem.Allocator) Command {
118
118
  } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
119
119
  std.io.getStdOut().writeAll(usage) catch os.exit(1);
120
120
  os.exit(0);
121
- } else {
121
+ } else if (mem.startsWith(u8, arg, "--")) {
122
122
  fatal("unexpected argument: '{s}'", .{arg});
123
+ } else {
124
+ fatal("unexpected argument: '{s}' (must start with '--')", .{arg});
123
125
  }
124
126
  }
125
127
 
@@ -130,7 +132,7 @@ pub fn parse_args(allocator: *std.mem.Allocator) Command {
130
132
  const replica = parse_replica(raw_replica);
131
133
 
132
134
  const dir_path = maybe_directory orelse config.directory;
133
- const dir_fd = os.openZ(dir_path, os.O_CLOEXEC | os.O_RDONLY, 0) catch |err|
135
+ const dir_fd = os.openZ(dir_path, os.O.CLOEXEC | os.O.RDONLY, 0) catch |err|
134
136
  fatal("failed to open directory '{s}': {}", .{ dir_path, err });
135
137
 
136
138
  switch (command) {
@@ -193,7 +195,7 @@ fn parse_cluster(raw_cluster: []const u8) u32 {
193
195
  }
194
196
 
195
197
  /// Parse and allocate the addresses returning a slice into that array.
196
- fn parse_addresses(allocator: *std.mem.Allocator, raw_addresses: []const u8) []net.Address {
198
+ fn parse_addresses(allocator: std.mem.Allocator, raw_addresses: []const u8) []net.Address {
197
199
  return vsr.parse_addresses(allocator, raw_addresses) catch |err| switch (err) {
198
200
  error.AddressHasTrailingComma => fatal("--addresses: invalid trailing comma", .{}),
199
201
  error.AddressLimitExceeded => {
@@ -1,8 +1,8 @@
1
1
  /// Whether development or production:
2
2
  pub const deployment_environment = .development;
3
3
 
4
- /// The maximum log level in increasing order of verbosity (emergency=0, debug=7):
5
- pub const log_level = 6;
4
+ /// The maximum log level in increasing order of verbosity (emergency=0, debug=3):
5
+ pub const log_level = 2;
6
6
 
7
7
  /// The maximum number of replicas allowed in a cluster.
8
8
  pub const replicas_max = 6;
@@ -1,7 +1,16 @@
1
1
  const std = @import("std");
2
2
  const assert = std.debug.assert;
3
3
 
4
- usingnamespace @import("tigerbeetle.zig");
4
+ const config = @import("config.zig");
5
+
6
+ const tb = @import("tigerbeetle.zig");
7
+ const Account = tb.Account;
8
+ const Transfer = tb.Transfer;
9
+ const Commit = tb.Commit;
10
+
11
+ const CreateAccountsResult = tb.CreateAccountsResult;
12
+ const CreateTransfersResult = tb.CreateTransfersResult;
13
+ const CommitTransfersResult = tb.CommitTransfersResult;
5
14
 
6
15
  const IO = @import("io.zig").IO;
7
16
  const MessageBus = @import("message_bus.zig").MessageBusClient;
@@ -13,104 +22,117 @@ const Client = vsr.Client(StateMachine, MessageBus);
13
22
 
14
23
  pub const log_level: std.log.Level = .alert;
15
24
 
16
- pub const Demo = struct {
17
- pub fn request(
18
- operation: StateMachine.Operation,
19
- batch: anytype,
20
- on_reply: fn (
21
- user_data: u128,
22
- operation: StateMachine.Operation,
23
- results: Client.Error![]const u8,
24
- ) void,
25
- ) !void {
26
- const allocator = std.heap.page_allocator;
27
- const client_id = std.crypto.random.int(u128);
28
- const cluster_id: u32 = 0;
29
- var addresses = [_]std.net.Address{try std.net.Address.parseIp4("127.0.0.1", config.port)};
30
-
31
- var io = try IO.init(32, 0);
32
- defer io.deinit();
33
-
34
- var message_bus = try MessageBus.init(allocator, cluster_id, &addresses, client_id, &io);
35
- defer message_bus.deinit();
36
-
37
- var client = try Client.init(
38
- allocator,
39
- client_id,
40
- cluster_id,
41
- @intCast(u8, addresses.len),
42
- &message_bus,
43
- );
44
- defer client.deinit();
45
-
46
- message_bus.set_on_message(*Client, &client, Client.on_message);
47
-
48
- var message = client.get_message() orelse unreachable;
49
- defer client.unref(message);
50
-
51
- const body = std.mem.asBytes(&batch);
52
- std.mem.copy(u8, message.buffer[@sizeOf(Header)..], body);
53
-
54
- client.request(
55
- 0,
56
- on_reply,
57
- operation,
58
- message,
59
- body.len,
60
- );
61
-
62
- while (client.request_queue.count > 0) {
63
- client.tick();
64
- try io.run_for_ns(config.tick_ms * std.time.ns_per_ms);
65
- }
66
- }
67
-
68
- pub fn on_create_accounts(
69
- user_data: u128,
70
- operation: StateMachine.Operation,
71
- results: Client.Error![]const u8,
72
- ) void {
73
- print_results(CreateAccountsResult, results);
74
- }
75
-
76
- pub fn on_lookup_accounts(
77
- user_data: u128,
78
- operation: StateMachine.Operation,
79
- results: Client.Error![]const u8,
80
- ) void {
81
- print_results(Account, results);
82
- }
83
-
84
- pub fn on_lookup_transfers(
85
- user_data: u128,
86
- operation: StateMachine.Operation,
87
- results: Client.Error![]const u8,
88
- ) void {
89
- print_results(Transfer, results);
90
- }
91
-
92
- pub fn on_create_transfers(
25
+ pub fn request(
26
+ operation: StateMachine.Operation,
27
+ batch: anytype,
28
+ on_reply: fn (
93
29
  user_data: u128,
94
30
  operation: StateMachine.Operation,
95
31
  results: Client.Error![]const u8,
96
- ) void {
97
- print_results(CreateTransfersResult, results);
32
+ ) void,
33
+ ) !void {
34
+ const allocator = std.heap.page_allocator;
35
+ const client_id = std.crypto.random.int(u128);
36
+ const cluster_id: u32 = 0;
37
+ var addresses = [_]std.net.Address{try std.net.Address.parseIp4("127.0.0.1", config.port)};
38
+
39
+ var io = try IO.init(32, 0);
40
+ defer io.deinit();
41
+
42
+ var message_bus = try MessageBus.init(allocator, cluster_id, &addresses, client_id, &io);
43
+ defer message_bus.deinit();
44
+
45
+ var client = try Client.init(
46
+ allocator,
47
+ client_id,
48
+ cluster_id,
49
+ @intCast(u8, addresses.len),
50
+ &message_bus,
51
+ );
52
+ defer client.deinit();
53
+
54
+ message_bus.set_on_message(*Client, &client, Client.on_message);
55
+
56
+ var message = client.get_message() orelse unreachable;
57
+ defer client.unref(message);
58
+
59
+ const body = std.mem.asBytes(&batch);
60
+ std.mem.copy(u8, message.buffer[@sizeOf(Header)..], body);
61
+
62
+ client.request(
63
+ 0,
64
+ on_reply,
65
+ operation,
66
+ message,
67
+ body.len,
68
+ );
69
+
70
+ while (client.request_queue.count > 0) {
71
+ client.tick();
72
+ try io.run_for_ns(config.tick_ms * std.time.ns_per_ms);
98
73
  }
99
-
100
- pub fn on_commit_transfers(
101
- user_data: u128,
102
- operation: StateMachine.Operation,
103
- results: Client.Error![]const u8,
104
- ) void {
105
- print_results(CommitTransfersResult, results);
106
- }
107
-
108
- fn print_results(comptime Results: type, results: Client.Error![]const u8) void {
109
- const body = results catch unreachable;
110
- const slice = std.mem.bytesAsSlice(Results, body);
111
- for (slice) |result| {
112
- std.debug.print("{}\n", .{result});
113
- }
114
- if (slice.len == 0) std.debug.print("OK\n", .{});
74
+ }
75
+
76
+ pub fn on_create_accounts(
77
+ user_data: u128,
78
+ operation: StateMachine.Operation,
79
+ results: Client.Error![]const u8,
80
+ ) void {
81
+ _ = user_data;
82
+ _ = operation;
83
+
84
+ print_results(CreateAccountsResult, results);
85
+ }
86
+
87
+ pub fn on_lookup_accounts(
88
+ user_data: u128,
89
+ operation: StateMachine.Operation,
90
+ results: Client.Error![]const u8,
91
+ ) void {
92
+ _ = user_data;
93
+ _ = operation;
94
+
95
+ print_results(Account, results);
96
+ }
97
+
98
+ pub fn on_lookup_transfers(
99
+ user_data: u128,
100
+ operation: StateMachine.Operation,
101
+ results: Client.Error![]const u8,
102
+ ) void {
103
+ _ = user_data;
104
+ _ = operation;
105
+
106
+ print_results(Transfer, results);
107
+ }
108
+
109
+ pub fn on_create_transfers(
110
+ user_data: u128,
111
+ operation: StateMachine.Operation,
112
+ results: Client.Error![]const u8,
113
+ ) void {
114
+ _ = user_data;
115
+ _ = operation;
116
+
117
+ print_results(CreateTransfersResult, results);
118
+ }
119
+
120
+ pub fn on_commit_transfers(
121
+ user_data: u128,
122
+ operation: StateMachine.Operation,
123
+ results: Client.Error![]const u8,
124
+ ) void {
125
+ _ = user_data;
126
+ _ = operation;
127
+
128
+ print_results(CommitTransfersResult, results);
129
+ }
130
+
131
+ fn print_results(comptime Results: type, results: Client.Error![]const u8) void {
132
+ const body = results catch unreachable;
133
+ const slice = std.mem.bytesAsSlice(Results, body);
134
+ for (slice) |result| {
135
+ std.debug.print("{}\n", .{result});
115
136
  }
116
- };
137
+ if (slice.len == 0) std.debug.print("OK\n", .{});
138
+ }
@@ -1,5 +1,7 @@
1
- usingnamespace @import("tigerbeetle.zig");
2
- usingnamespace @import("demo.zig");
1
+ const tb = @import("tigerbeetle.zig");
2
+ const demo = @import("demo.zig");
3
+
4
+ const Account = tb.Account;
3
5
 
4
6
  pub fn main() !void {
5
7
  const accounts = [_]Account{
@@ -29,5 +31,5 @@ pub fn main() !void {
29
31
  },
30
32
  };
31
33
 
32
- try Demo.request(.create_accounts, accounts, Demo.on_create_accounts);
34
+ try demo.request(.create_accounts, accounts, demo.on_create_accounts);
33
35
  }
@@ -1,8 +1,7 @@
1
- usingnamespace @import("tigerbeetle.zig");
2
- usingnamespace @import("demo.zig");
1
+ const demo = @import("demo.zig");
3
2
 
4
3
  pub fn main() !void {
5
4
  const ids = [_]u128{ 1, 2 };
6
5
 
7
- try Demo.request(.lookup_accounts, ids, Demo.on_lookup_accounts);
6
+ try demo.request(.lookup_accounts, ids, demo.on_lookup_accounts);
8
7
  }
@@ -1,5 +1,7 @@
1
- usingnamespace @import("tigerbeetle.zig");
2
- usingnamespace @import("demo.zig");
1
+ const tb = @import("tigerbeetle.zig");
2
+ const demo = @import("demo.zig");
3
+
4
+ const Transfer = tb.Transfer;
3
5
 
4
6
  pub fn main() !void {
5
7
  const transfers = [_]Transfer{
@@ -16,5 +18,5 @@ pub fn main() !void {
16
18
  },
17
19
  };
18
20
 
19
- try Demo.request(.create_transfers, transfers, Demo.on_create_transfers);
21
+ try demo.request(.create_transfers, transfers, demo.on_create_transfers);
20
22
  }
@@ -1,7 +1,9 @@
1
1
  const std = @import("std");
2
2
 
3
- usingnamespace @import("tigerbeetle.zig");
4
- usingnamespace @import("demo.zig");
3
+ const tb = @import("tigerbeetle.zig");
4
+ const demo = @import("demo.zig");
5
+
6
+ const Transfer = tb.Transfer;
5
7
 
6
8
  pub fn main() !void {
7
9
  const transfers = [_]Transfer{
@@ -49,5 +51,5 @@ pub fn main() !void {
49
51
  },
50
52
  };
51
53
 
52
- try Demo.request(.create_transfers, transfers, Demo.on_create_transfers);
54
+ try demo.request(.create_transfers, transfers, demo.on_create_transfers);
53
55
  }
@@ -1,5 +1,7 @@
1
- usingnamespace @import("tigerbeetle.zig");
2
- usingnamespace @import("demo.zig");
1
+ const tb = @import("tigerbeetle.zig");
2
+ const demo = @import("demo.zig");
3
+
4
+ const Commit = tb.Commit;
3
5
 
4
6
  pub fn main() !void {
5
7
  const commits = [_]Commit{
@@ -17,5 +19,5 @@ pub fn main() !void {
17
19
  },
18
20
  };
19
21
 
20
- try Demo.request(.commit_transfers, commits, Demo.on_commit_transfers);
22
+ try demo.request(.commit_transfers, commits, demo.on_commit_transfers);
21
23
  }
@@ -1,5 +1,7 @@
1
- usingnamespace @import("tigerbeetle.zig");
2
- usingnamespace @import("demo.zig");
1
+ const tb = @import("tigerbeetle.zig");
2
+ const demo = @import("demo.zig");
3
+
4
+ const Commit = tb.Commit;
3
5
 
4
6
  pub fn main() !void {
5
7
  const commits = [_]Commit{
@@ -11,5 +13,5 @@ pub fn main() !void {
11
13
  },
12
14
  };
13
15
 
14
- try Demo.request(.commit_transfers, commits, Demo.on_commit_transfers);
16
+ try demo.request(.commit_transfers, commits, demo.on_commit_transfers);
15
17
  }
@@ -1,8 +1,7 @@
1
- usingnamespace @import("tigerbeetle.zig");
2
- usingnamespace @import("demo.zig");
1
+ const demo = @import("demo.zig");
3
2
 
4
3
  pub fn main() !void {
5
4
  const ids = [_]u128{ 1000, 1001, 1002 };
6
5
 
7
- try Demo.request(.lookup_transfers, ids, Demo.on_lookup_transfers);
6
+ try demo.request(.lookup_transfers, ids, demo.on_lookup_transfers);
8
7
  }