tigerbeetle 0.0.34 → 0.0.37
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/ext/tb_client/extconf.rb +13 -13
- data/ext/tb_client/tigerbeetle/LICENSE +177 -0
- data/ext/tb_client/tigerbeetle/build.zig +2327 -0
- data/ext/tb_client/tigerbeetle/src/aof.zig +1000 -0
- data/ext/tb_client/tigerbeetle/src/build_multiversion.zig +808 -0
- data/ext/tb_client/tigerbeetle/src/cdc/amqp/protocol.zig +1283 -0
- data/ext/tb_client/tigerbeetle/src/cdc/amqp/spec.zig +1704 -0
- data/ext/tb_client/tigerbeetle/src/cdc/amqp/types.zig +341 -0
- data/ext/tb_client/tigerbeetle/src/cdc/amqp.zig +1450 -0
- data/ext/tb_client/tigerbeetle/src/cdc/runner.zig +1659 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/samples/main.c +406 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/context.zig +1084 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/echo_client.zig +286 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/packet.zig +158 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/signal.zig +229 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client/signal_fuzz.zig +110 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client.h +386 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client.zig +34 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client_exports.zig +281 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client_header.zig +312 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/tb_client_header_test.zig +138 -0
- data/ext/tb_client/tigerbeetle/src/clients/c/test.zig +466 -0
- data/ext/tb_client/tigerbeetle/src/clients/docs_samples.zig +157 -0
- data/ext/tb_client/tigerbeetle/src/clients/docs_types.zig +90 -0
- data/ext/tb_client/tigerbeetle/src/clients/dotnet/ci.zig +203 -0
- data/ext/tb_client/tigerbeetle/src/clients/dotnet/docs.zig +79 -0
- data/ext/tb_client/tigerbeetle/src/clients/dotnet/dotnet_bindings.zig +542 -0
- data/ext/tb_client/tigerbeetle/src/clients/go/ci.zig +109 -0
- data/ext/tb_client/tigerbeetle/src/clients/go/docs.zig +86 -0
- data/ext/tb_client/tigerbeetle/src/clients/go/go_bindings.zig +370 -0
- data/ext/tb_client/tigerbeetle/src/clients/go/pkg/native/tb_client.h +386 -0
- data/ext/tb_client/tigerbeetle/src/clients/java/ci.zig +167 -0
- data/ext/tb_client/tigerbeetle/src/clients/java/docs.zig +126 -0
- data/ext/tb_client/tigerbeetle/src/clients/java/java_bindings.zig +996 -0
- data/ext/tb_client/tigerbeetle/src/clients/java/src/client.zig +748 -0
- data/ext/tb_client/tigerbeetle/src/clients/java/src/jni.zig +3238 -0
- data/ext/tb_client/tigerbeetle/src/clients/java/src/jni_tests.zig +1718 -0
- data/ext/tb_client/tigerbeetle/src/clients/java/src/jni_thread_cleaner.zig +190 -0
- data/ext/tb_client/tigerbeetle/src/clients/node/ci.zig +104 -0
- data/ext/tb_client/tigerbeetle/src/clients/node/docs.zig +75 -0
- data/ext/tb_client/tigerbeetle/src/clients/node/node.zig +522 -0
- data/ext/tb_client/tigerbeetle/src/clients/node/node_bindings.zig +267 -0
- data/ext/tb_client/tigerbeetle/src/clients/node/src/c.zig +3 -0
- data/ext/tb_client/tigerbeetle/src/clients/node/src/translate.zig +379 -0
- data/ext/tb_client/tigerbeetle/src/clients/python/ci.zig +131 -0
- data/ext/tb_client/tigerbeetle/src/clients/python/docs.zig +63 -0
- data/ext/tb_client/tigerbeetle/src/clients/python/python_bindings.zig +588 -0
- data/ext/tb_client/tigerbeetle/src/clients/rust/assets/tb_client.h +386 -0
- data/ext/tb_client/tigerbeetle/src/clients/rust/ci.zig +73 -0
- data/ext/tb_client/tigerbeetle/src/clients/rust/docs.zig +106 -0
- data/ext/tb_client/tigerbeetle/src/clients/rust/rust_bindings.zig +305 -0
- data/ext/tb_client/tigerbeetle/src/config.zig +296 -0
- data/ext/tb_client/tigerbeetle/src/constants.zig +790 -0
- data/ext/tb_client/tigerbeetle/src/copyhound.zig +202 -0
- data/ext/tb_client/tigerbeetle/src/counting_allocator.zig +72 -0
- data/ext/tb_client/tigerbeetle/src/direction.zig +11 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/build.zig +158 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/content.zig +156 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/docs.zig +252 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/file_checker.zig +313 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/html.zig +87 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/page_writer.zig +63 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/redirects.zig +47 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/search_index_writer.zig +28 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/service_worker_writer.zig +61 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/single_page_writer.zig +169 -0
- data/ext/tb_client/tigerbeetle/src/docs_website/src/website.zig +46 -0
- data/ext/tb_client/tigerbeetle/src/ewah.zig +445 -0
- data/ext/tb_client/tigerbeetle/src/ewah_benchmark.zig +128 -0
- data/ext/tb_client/tigerbeetle/src/ewah_fuzz.zig +171 -0
- data/ext/tb_client/tigerbeetle/src/fuzz_tests.zig +179 -0
- data/ext/tb_client/tigerbeetle/src/integration_tests.zig +662 -0
- data/ext/tb_client/tigerbeetle/src/io/common.zig +155 -0
- data/ext/tb_client/tigerbeetle/src/io/darwin.zig +1093 -0
- data/ext/tb_client/tigerbeetle/src/io/linux.zig +1880 -0
- data/ext/tb_client/tigerbeetle/src/io/test.zig +1005 -0
- data/ext/tb_client/tigerbeetle/src/io/windows.zig +1598 -0
- data/ext/tb_client/tigerbeetle/src/io.zig +34 -0
- data/ext/tb_client/tigerbeetle/src/iops.zig +134 -0
- data/ext/tb_client/tigerbeetle/src/list.zig +236 -0
- data/ext/tb_client/tigerbeetle/src/lsm/binary_search.zig +848 -0
- data/ext/tb_client/tigerbeetle/src/lsm/binary_search_benchmark.zig +179 -0
- data/ext/tb_client/tigerbeetle/src/lsm/cache_map.zig +424 -0
- data/ext/tb_client/tigerbeetle/src/lsm/cache_map_fuzz.zig +420 -0
- data/ext/tb_client/tigerbeetle/src/lsm/compaction.zig +2117 -0
- data/ext/tb_client/tigerbeetle/src/lsm/composite_key.zig +182 -0
- data/ext/tb_client/tigerbeetle/src/lsm/forest.zig +1119 -0
- data/ext/tb_client/tigerbeetle/src/lsm/forest_fuzz.zig +1102 -0
- data/ext/tb_client/tigerbeetle/src/lsm/forest_table_iterator.zig +200 -0
- data/ext/tb_client/tigerbeetle/src/lsm/groove.zig +1495 -0
- data/ext/tb_client/tigerbeetle/src/lsm/k_way_merge.zig +739 -0
- data/ext/tb_client/tigerbeetle/src/lsm/k_way_merge_benchmark.zig +166 -0
- data/ext/tb_client/tigerbeetle/src/lsm/manifest.zig +754 -0
- data/ext/tb_client/tigerbeetle/src/lsm/manifest_level.zig +1294 -0
- data/ext/tb_client/tigerbeetle/src/lsm/manifest_level_fuzz.zig +510 -0
- data/ext/tb_client/tigerbeetle/src/lsm/manifest_log.zig +1263 -0
- data/ext/tb_client/tigerbeetle/src/lsm/manifest_log_fuzz.zig +628 -0
- data/ext/tb_client/tigerbeetle/src/lsm/node_pool.zig +247 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scan_buffer.zig +116 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scan_builder.zig +543 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scan_fuzz.zig +938 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scan_lookup.zig +293 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scan_merge.zig +362 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scan_range.zig +99 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scan_state.zig +17 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scan_tree.zig +1036 -0
- data/ext/tb_client/tigerbeetle/src/lsm/schema.zig +617 -0
- data/ext/tb_client/tigerbeetle/src/lsm/scratch_memory.zig +84 -0
- data/ext/tb_client/tigerbeetle/src/lsm/segmented_array.zig +1500 -0
- data/ext/tb_client/tigerbeetle/src/lsm/segmented_array_benchmark.zig +149 -0
- data/ext/tb_client/tigerbeetle/src/lsm/segmented_array_fuzz.zig +7 -0
- data/ext/tb_client/tigerbeetle/src/lsm/set_associative_cache.zig +865 -0
- data/ext/tb_client/tigerbeetle/src/lsm/table.zig +607 -0
- data/ext/tb_client/tigerbeetle/src/lsm/table_memory.zig +843 -0
- data/ext/tb_client/tigerbeetle/src/lsm/table_value_iterator.zig +105 -0
- data/ext/tb_client/tigerbeetle/src/lsm/timestamp_range.zig +40 -0
- data/ext/tb_client/tigerbeetle/src/lsm/tree.zig +630 -0
- data/ext/tb_client/tigerbeetle/src/lsm/tree_fuzz.zig +933 -0
- data/ext/tb_client/tigerbeetle/src/lsm/zig_zag_merge.zig +557 -0
- data/ext/tb_client/tigerbeetle/src/message_buffer.zig +469 -0
- data/ext/tb_client/tigerbeetle/src/message_bus.zig +1214 -0
- data/ext/tb_client/tigerbeetle/src/message_bus_fuzz.zig +936 -0
- data/ext/tb_client/tigerbeetle/src/message_pool.zig +343 -0
- data/ext/tb_client/tigerbeetle/src/multiversion.zig +2195 -0
- data/ext/tb_client/tigerbeetle/src/queue.zig +390 -0
- data/ext/tb_client/tigerbeetle/src/repl/completion.zig +201 -0
- data/ext/tb_client/tigerbeetle/src/repl/parser.zig +1356 -0
- data/ext/tb_client/tigerbeetle/src/repl/terminal.zig +496 -0
- data/ext/tb_client/tigerbeetle/src/repl.zig +1034 -0
- data/ext/tb_client/tigerbeetle/src/scripts/amqp.zig +973 -0
- data/ext/tb_client/tigerbeetle/src/scripts/cfo.zig +1866 -0
- data/ext/tb_client/tigerbeetle/src/scripts/changelog.zig +304 -0
- data/ext/tb_client/tigerbeetle/src/scripts/ci.zig +227 -0
- data/ext/tb_client/tigerbeetle/src/scripts/client_readmes.zig +658 -0
- data/ext/tb_client/tigerbeetle/src/scripts/devhub.zig +466 -0
- data/ext/tb_client/tigerbeetle/src/scripts/release.zig +1058 -0
- data/ext/tb_client/tigerbeetle/src/scripts.zig +105 -0
- data/ext/tb_client/tigerbeetle/src/shell.zig +1195 -0
- data/ext/tb_client/tigerbeetle/src/stack.zig +260 -0
- data/ext/tb_client/tigerbeetle/src/state_machine/auditor.zig +911 -0
- data/ext/tb_client/tigerbeetle/src/state_machine/workload.zig +2079 -0
- data/ext/tb_client/tigerbeetle/src/state_machine.zig +4872 -0
- data/ext/tb_client/tigerbeetle/src/state_machine_fuzz.zig +288 -0
- data/ext/tb_client/tigerbeetle/src/state_machine_tests.zig +3128 -0
- data/ext/tb_client/tigerbeetle/src/static_allocator.zig +82 -0
- data/ext/tb_client/tigerbeetle/src/stdx/bit_set.zig +157 -0
- data/ext/tb_client/tigerbeetle/src/stdx/bounded_array.zig +292 -0
- data/ext/tb_client/tigerbeetle/src/stdx/debug.zig +65 -0
- data/ext/tb_client/tigerbeetle/src/stdx/flags.zig +1414 -0
- data/ext/tb_client/tigerbeetle/src/stdx/mlock.zig +92 -0
- data/ext/tb_client/tigerbeetle/src/stdx/prng.zig +677 -0
- data/ext/tb_client/tigerbeetle/src/stdx/radix.zig +336 -0
- data/ext/tb_client/tigerbeetle/src/stdx/ring_buffer.zig +511 -0
- data/ext/tb_client/tigerbeetle/src/stdx/sort_test.zig +112 -0
- data/ext/tb_client/tigerbeetle/src/stdx/stdx.zig +1160 -0
- data/ext/tb_client/tigerbeetle/src/stdx/testing/low_level_hash_vectors.zig +142 -0
- data/ext/tb_client/tigerbeetle/src/stdx/testing/snaptest.zig +361 -0
- data/ext/tb_client/tigerbeetle/src/stdx/time_units.zig +275 -0
- data/ext/tb_client/tigerbeetle/src/stdx/unshare.zig +295 -0
- data/ext/tb_client/tigerbeetle/src/stdx/vendored/aegis.zig +436 -0
- data/ext/tb_client/tigerbeetle/src/stdx/windows.zig +48 -0
- data/ext/tb_client/tigerbeetle/src/stdx/zipfian.zig +402 -0
- data/ext/tb_client/tigerbeetle/src/storage.zig +489 -0
- data/ext/tb_client/tigerbeetle/src/storage_fuzz.zig +180 -0
- data/ext/tb_client/tigerbeetle/src/testing/bench.zig +146 -0
- data/ext/tb_client/tigerbeetle/src/testing/cluster/grid_checker.zig +53 -0
- data/ext/tb_client/tigerbeetle/src/testing/cluster/journal_checker.zig +61 -0
- data/ext/tb_client/tigerbeetle/src/testing/cluster/manifest_checker.zig +76 -0
- data/ext/tb_client/tigerbeetle/src/testing/cluster/message_bus.zig +110 -0
- data/ext/tb_client/tigerbeetle/src/testing/cluster/network.zig +412 -0
- data/ext/tb_client/tigerbeetle/src/testing/cluster/state_checker.zig +331 -0
- data/ext/tb_client/tigerbeetle/src/testing/cluster/storage_checker.zig +458 -0
- data/ext/tb_client/tigerbeetle/src/testing/cluster.zig +1198 -0
- data/ext/tb_client/tigerbeetle/src/testing/exhaustigen.zig +128 -0
- data/ext/tb_client/tigerbeetle/src/testing/fixtures.zig +181 -0
- data/ext/tb_client/tigerbeetle/src/testing/fuzz.zig +144 -0
- data/ext/tb_client/tigerbeetle/src/testing/id.zig +97 -0
- data/ext/tb_client/tigerbeetle/src/testing/io.zig +317 -0
- data/ext/tb_client/tigerbeetle/src/testing/marks.zig +126 -0
- data/ext/tb_client/tigerbeetle/src/testing/packet_simulator.zig +533 -0
- data/ext/tb_client/tigerbeetle/src/testing/reply_sequence.zig +154 -0
- data/ext/tb_client/tigerbeetle/src/testing/state_machine.zig +389 -0
- data/ext/tb_client/tigerbeetle/src/testing/storage.zig +1247 -0
- data/ext/tb_client/tigerbeetle/src/testing/table.zig +249 -0
- data/ext/tb_client/tigerbeetle/src/testing/time.zig +98 -0
- data/ext/tb_client/tigerbeetle/src/testing/tmp_tigerbeetle.zig +212 -0
- data/ext/tb_client/tigerbeetle/src/testing/vortex/constants.zig +26 -0
- data/ext/tb_client/tigerbeetle/src/testing/vortex/faulty_network.zig +580 -0
- data/ext/tb_client/tigerbeetle/src/testing/vortex/java_driver/ci.zig +39 -0
- data/ext/tb_client/tigerbeetle/src/testing/vortex/logged_process.zig +214 -0
- data/ext/tb_client/tigerbeetle/src/testing/vortex/rust_driver/ci.zig +34 -0
- data/ext/tb_client/tigerbeetle/src/testing/vortex/supervisor.zig +766 -0
- data/ext/tb_client/tigerbeetle/src/testing/vortex/workload.zig +543 -0
- data/ext/tb_client/tigerbeetle/src/testing/vortex/zig_driver.zig +181 -0
- data/ext/tb_client/tigerbeetle/src/tidy.zig +1448 -0
- data/ext/tb_client/tigerbeetle/src/tigerbeetle/benchmark_driver.zig +227 -0
- data/ext/tb_client/tigerbeetle/src/tigerbeetle/benchmark_load.zig +1069 -0
- data/ext/tb_client/tigerbeetle/src/tigerbeetle/cli.zig +1422 -0
- data/ext/tb_client/tigerbeetle/src/tigerbeetle/inspect.zig +1658 -0
- data/ext/tb_client/tigerbeetle/src/tigerbeetle/inspect_integrity.zig +518 -0
- data/ext/tb_client/tigerbeetle/src/tigerbeetle/libtb_client.zig +36 -0
- data/ext/tb_client/tigerbeetle/src/tigerbeetle/main.zig +646 -0
- data/ext/tb_client/tigerbeetle/src/tigerbeetle.zig +958 -0
- data/ext/tb_client/tigerbeetle/src/time.zig +236 -0
- data/ext/tb_client/tigerbeetle/src/trace/event.zig +745 -0
- data/ext/tb_client/tigerbeetle/src/trace/statsd.zig +462 -0
- data/ext/tb_client/tigerbeetle/src/trace.zig +556 -0
- data/ext/tb_client/tigerbeetle/src/unit_tests.zig +321 -0
- data/ext/tb_client/tigerbeetle/src/vopr.zig +1785 -0
- data/ext/tb_client/tigerbeetle/src/vortex.zig +101 -0
- data/ext/tb_client/tigerbeetle/src/vsr/checkpoint_trailer.zig +473 -0
- data/ext/tb_client/tigerbeetle/src/vsr/checksum.zig +208 -0
- data/ext/tb_client/tigerbeetle/src/vsr/checksum_benchmark.zig +43 -0
- data/ext/tb_client/tigerbeetle/src/vsr/client.zig +768 -0
- data/ext/tb_client/tigerbeetle/src/vsr/client_replies.zig +532 -0
- data/ext/tb_client/tigerbeetle/src/vsr/client_sessions.zig +338 -0
- data/ext/tb_client/tigerbeetle/src/vsr/clock.zig +1019 -0
- data/ext/tb_client/tigerbeetle/src/vsr/fault_detector.zig +279 -0
- data/ext/tb_client/tigerbeetle/src/vsr/free_set.zig +1381 -0
- data/ext/tb_client/tigerbeetle/src/vsr/free_set_fuzz.zig +315 -0
- data/ext/tb_client/tigerbeetle/src/vsr/grid.zig +1460 -0
- data/ext/tb_client/tigerbeetle/src/vsr/grid_blocks_missing.zig +757 -0
- data/ext/tb_client/tigerbeetle/src/vsr/grid_scrubber.zig +797 -0
- data/ext/tb_client/tigerbeetle/src/vsr/journal.zig +2586 -0
- data/ext/tb_client/tigerbeetle/src/vsr/marzullo.zig +308 -0
- data/ext/tb_client/tigerbeetle/src/vsr/message_header.zig +1777 -0
- data/ext/tb_client/tigerbeetle/src/vsr/multi_batch.zig +715 -0
- data/ext/tb_client/tigerbeetle/src/vsr/multi_batch_fuzz.zig +185 -0
- data/ext/tb_client/tigerbeetle/src/vsr/repair_budget.zig +333 -0
- data/ext/tb_client/tigerbeetle/src/vsr/replica.zig +12355 -0
- data/ext/tb_client/tigerbeetle/src/vsr/replica_format.zig +416 -0
- data/ext/tb_client/tigerbeetle/src/vsr/replica_reformat.zig +165 -0
- data/ext/tb_client/tigerbeetle/src/vsr/replica_test.zig +2910 -0
- data/ext/tb_client/tigerbeetle/src/vsr/routing.zig +1075 -0
- data/ext/tb_client/tigerbeetle/src/vsr/superblock.zig +1603 -0
- data/ext/tb_client/tigerbeetle/src/vsr/superblock_fuzz.zig +484 -0
- data/ext/tb_client/tigerbeetle/src/vsr/superblock_quorums.zig +405 -0
- data/ext/tb_client/tigerbeetle/src/vsr/superblock_quorums_fuzz.zig +355 -0
- data/ext/tb_client/tigerbeetle/src/vsr/sync.zig +29 -0
- data/ext/tb_client/tigerbeetle/src/vsr.zig +1727 -0
- data/lib/tb_client/shared_lib.rb +12 -5
- data/lib/tigerbeetle/client.rb +1 -1
- data/lib/tigerbeetle/platforms.rb +9 -0
- data/lib/tigerbeetle/version.rb +2 -2
- data/tigerbeetle.gemspec +22 -5
- metadata +242 -3
- data/ext/tb_client/pkg.tar.gz +0 -0
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
///! Java Native Interfaces for TigerBeetle Client
|
|
2
|
+
///! Please refer to the JNI Best Practices Guide:
|
|
3
|
+
///! https://developer.ibm.com/articles/j-jni/
|
|
4
|
+
|
|
5
|
+
// IMPORTANT: Running code from a native thread, the JVM will
|
|
6
|
+
// never automatically free local references until the thread detaches.
|
|
7
|
+
// To avoid leaks, we *ALWAYS* free all references we acquire,
|
|
8
|
+
// even local references, so we don't need to distinguish if the code
|
|
9
|
+
// is called from the JVM or the native thread via callback.
|
|
10
|
+
|
|
11
|
+
const std = @import("std");
|
|
12
|
+
const builtin = @import("builtin");
|
|
13
|
+
const jni = @import("jni.zig");
|
|
14
|
+
const JNIThreadCleaner = @import("jni_thread_cleaner.zig").JNIThreadCleaner;
|
|
15
|
+
|
|
16
|
+
comptime {
|
|
17
|
+
if (!builtin.link_libc) {
|
|
18
|
+
@compileError("JNI client must be built with libc");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
pub const vsr = @import("vsr");
|
|
23
|
+
|
|
24
|
+
const tb = vsr.tb_client;
|
|
25
|
+
|
|
26
|
+
const log = std.log.scoped(.tb_client_jni);
|
|
27
|
+
const assert = std.debug.assert;
|
|
28
|
+
|
|
29
|
+
const jni_version = jni.jni_version_10;
|
|
30
|
+
|
|
31
|
+
const global_allocator = std.heap.c_allocator;
|
|
32
|
+
|
|
33
|
+
pub const std_options: std.Options = .{
|
|
34
|
+
.log_level = .debug,
|
|
35
|
+
.logFn = tb.exports.Logging.application_logger,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/// NativeClient implementation.
|
|
39
|
+
const NativeClient = struct {
|
|
40
|
+
/// On JVM loads this library.
|
|
41
|
+
fn on_load(vm: *jni.JavaVM) jni.JInt {
|
|
42
|
+
const env = JNIHelper.get_env(vm);
|
|
43
|
+
ReflectionHelper.load(env);
|
|
44
|
+
return jni_version;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// On JVM unloads this library.
|
|
48
|
+
fn on_unload(vm: *jni.JavaVM) void {
|
|
49
|
+
const env = JNIHelper.get_env(vm);
|
|
50
|
+
ReflectionHelper.unload(env);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// Native clientInit and clientInitEcho implementation.
|
|
54
|
+
fn client_init(
|
|
55
|
+
comptime echo_client: bool,
|
|
56
|
+
env: *jni.JNIEnv,
|
|
57
|
+
client: *tb.ClientInterface,
|
|
58
|
+
cluster_id: u128,
|
|
59
|
+
addresses_obj: jni.JString,
|
|
60
|
+
) void {
|
|
61
|
+
const addresses = JNIHelper.get_string_utf(env, addresses_obj) orelse {
|
|
62
|
+
ReflectionHelper.initialization_exception_throw(
|
|
63
|
+
env,
|
|
64
|
+
tb.exports.tb_init_status.address_invalid,
|
|
65
|
+
);
|
|
66
|
+
return;
|
|
67
|
+
};
|
|
68
|
+
defer env.release_string_utf_chars(addresses_obj, addresses.ptr);
|
|
69
|
+
|
|
70
|
+
const init_fn = if (echo_client) tb.init_echo else tb.init;
|
|
71
|
+
const jvm = JNIHelper.get_java_vm(env);
|
|
72
|
+
init_fn(
|
|
73
|
+
global_allocator,
|
|
74
|
+
client,
|
|
75
|
+
cluster_id,
|
|
76
|
+
addresses,
|
|
77
|
+
@intFromPtr(jvm),
|
|
78
|
+
on_completion,
|
|
79
|
+
) catch |err| {
|
|
80
|
+
const status = tb.exports.init_error_to_status(err);
|
|
81
|
+
ReflectionHelper.initialization_exception_throw(env, status);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// Native clientDeinit implementation.
|
|
86
|
+
fn client_deinit(client: *tb.ClientInterface) void {
|
|
87
|
+
client.deinit() catch {
|
|
88
|
+
// Ignore multiple calls to `deinit`.
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// Native submit implementation.
|
|
93
|
+
fn submit(
|
|
94
|
+
env: *jni.JNIEnv,
|
|
95
|
+
client: *tb.ClientInterface,
|
|
96
|
+
request_obj: jni.JObject,
|
|
97
|
+
) void {
|
|
98
|
+
assert(request_obj != null);
|
|
99
|
+
|
|
100
|
+
const operation = ReflectionHelper.get_request_operation(env, request_obj);
|
|
101
|
+
const send_buffer: []u8 = ReflectionHelper.get_send_buffer_slice(env, request_obj) orelse {
|
|
102
|
+
ReflectionHelper.assertion_error_throw(
|
|
103
|
+
env,
|
|
104
|
+
"Request.sendBuffer is null or invalid",
|
|
105
|
+
);
|
|
106
|
+
return;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const packet: *tb.Packet = global_allocator.create(tb.Packet) catch {
|
|
110
|
+
ReflectionHelper.assertion_error_throw(env, "Request could not allocate a packet");
|
|
111
|
+
return;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Holds a global reference to prevent GC before the callback.
|
|
115
|
+
const global_ref = JNIHelper.new_global_reference(env, request_obj);
|
|
116
|
+
packet.* = .{
|
|
117
|
+
.user_data = global_ref,
|
|
118
|
+
.operation = operation,
|
|
119
|
+
.data_size = @intCast(send_buffer.len),
|
|
120
|
+
.data = send_buffer.ptr,
|
|
121
|
+
.user_tag = 0,
|
|
122
|
+
.status = undefined,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
client.submit(packet) catch |err| switch (err) {
|
|
126
|
+
error.ClientInvalid => ReflectionHelper.client_closed_exception_throw(env),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// Completion callback, always called from the native thread.
|
|
131
|
+
fn on_completion(
|
|
132
|
+
context_ptr: usize,
|
|
133
|
+
packet: *tb.Packet,
|
|
134
|
+
timestamp: u64,
|
|
135
|
+
result: ?[*]const u8,
|
|
136
|
+
result_size: u32,
|
|
137
|
+
) callconv(.c) void {
|
|
138
|
+
const jvm: *jni.JavaVM = @ptrFromInt(context_ptr);
|
|
139
|
+
|
|
140
|
+
const env = JNIHelper.try_get_env(jvm) orelse
|
|
141
|
+
JNIThreadCleaner.attach_current_thread_with_cleanup(jvm);
|
|
142
|
+
|
|
143
|
+
// Retrieves the request instance, and drops the GC reference.
|
|
144
|
+
assert(packet.user_data != null);
|
|
145
|
+
const request_obj: jni.JObject = @ptrCast(packet.user_data);
|
|
146
|
+
defer env.delete_global_ref(request_obj);
|
|
147
|
+
|
|
148
|
+
// Extract the packet details before freeing it.
|
|
149
|
+
const packet_operation = packet.operation;
|
|
150
|
+
const packet_status = packet.status;
|
|
151
|
+
global_allocator.destroy(packet);
|
|
152
|
+
|
|
153
|
+
if (packet_status != .ok) {
|
|
154
|
+
assert(timestamp == 0);
|
|
155
|
+
assert(result == null);
|
|
156
|
+
assert(result_size == 0);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (result_size > 0) {
|
|
160
|
+
switch (packet_status) {
|
|
161
|
+
.ok => if (result) |ptr| {
|
|
162
|
+
// Copying the reply before returning from the callback.
|
|
163
|
+
ReflectionHelper.set_reply_buffer(
|
|
164
|
+
env,
|
|
165
|
+
request_obj,
|
|
166
|
+
ptr[0..result_size],
|
|
167
|
+
);
|
|
168
|
+
},
|
|
169
|
+
else => {},
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
ReflectionHelper.end_request(
|
|
174
|
+
env,
|
|
175
|
+
request_obj,
|
|
176
|
+
packet_operation,
|
|
177
|
+
packet_status,
|
|
178
|
+
timestamp,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// Declares and exports all functions using the JNI naming/calling convention.
|
|
184
|
+
comptime {
|
|
185
|
+
// https://docs.oracle.com/en/java/javase/17/docs/specs/jni/design.html#compiling-loading-and-linking-native-methods.
|
|
186
|
+
const prefix = "Java_com_tigerbeetle_NativeClient_";
|
|
187
|
+
|
|
188
|
+
const Exports = struct {
|
|
189
|
+
fn on_load(vm: *jni.JavaVM) callconv(.c) jni.JInt {
|
|
190
|
+
return NativeClient.on_load(vm);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
fn on_unload(vm: *jni.JavaVM) callconv(.c) void {
|
|
194
|
+
NativeClient.on_unload(vm);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
fn client_init(
|
|
198
|
+
env: *jni.JNIEnv,
|
|
199
|
+
class: jni.JClass,
|
|
200
|
+
tb_client_buffer: jni.JObject,
|
|
201
|
+
cluster_id: jni.JByteArray,
|
|
202
|
+
addresses: jni.JString,
|
|
203
|
+
) callconv(.c) void {
|
|
204
|
+
_ = class;
|
|
205
|
+
assert(env.get_array_length(cluster_id) == 16);
|
|
206
|
+
|
|
207
|
+
const cluster_id_elements = env.get_byte_array_elements(cluster_id, null).?;
|
|
208
|
+
defer env.release_byte_array_elements(cluster_id, cluster_id_elements, .abort);
|
|
209
|
+
|
|
210
|
+
NativeClient.client_init(
|
|
211
|
+
false,
|
|
212
|
+
env,
|
|
213
|
+
ReflectionHelper.get_client_from_buffer(env, tb_client_buffer),
|
|
214
|
+
@bitCast(cluster_id_elements[0..16].*),
|
|
215
|
+
addresses,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
fn client_init_echo(
|
|
220
|
+
env: *jni.JNIEnv,
|
|
221
|
+
class: jni.JClass,
|
|
222
|
+
tb_client_buffer: jni.JObject,
|
|
223
|
+
cluster_id: jni.JByteArray,
|
|
224
|
+
addresses: jni.JString,
|
|
225
|
+
) callconv(.c) void {
|
|
226
|
+
_ = class;
|
|
227
|
+
assert(env.get_array_length(cluster_id) == 16);
|
|
228
|
+
|
|
229
|
+
const cluster_id_elements = env.get_byte_array_elements(cluster_id, null).?;
|
|
230
|
+
defer env.release_byte_array_elements(cluster_id, cluster_id_elements, .abort);
|
|
231
|
+
|
|
232
|
+
NativeClient.client_init(
|
|
233
|
+
true,
|
|
234
|
+
env,
|
|
235
|
+
ReflectionHelper.get_client_from_buffer(env, tb_client_buffer),
|
|
236
|
+
@as(u128, @bitCast(cluster_id_elements[0..16].*)),
|
|
237
|
+
addresses,
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
fn client_deinit(
|
|
242
|
+
env: *jni.JNIEnv,
|
|
243
|
+
class: jni.JClass,
|
|
244
|
+
tb_client_buffer: jni.JObject,
|
|
245
|
+
) callconv(.c) void {
|
|
246
|
+
_ = class;
|
|
247
|
+
NativeClient.client_deinit(
|
|
248
|
+
ReflectionHelper.get_client_from_buffer(env, tb_client_buffer),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
fn submit(
|
|
253
|
+
env: *jni.JNIEnv,
|
|
254
|
+
class: jni.JClass,
|
|
255
|
+
tb_client_buffer: jni.JObject,
|
|
256
|
+
request_obj: jni.JObject,
|
|
257
|
+
) callconv(.c) void {
|
|
258
|
+
_ = class;
|
|
259
|
+
NativeClient.submit(
|
|
260
|
+
env,
|
|
261
|
+
ReflectionHelper.get_client_from_buffer(env, tb_client_buffer),
|
|
262
|
+
request_obj,
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
@export(&Exports.on_load, .{ .name = "JNI_OnLoad", .linkage = .strong });
|
|
268
|
+
@export(&Exports.on_unload, .{ .name = "JNI_OnUnload", .linkage = .strong });
|
|
269
|
+
|
|
270
|
+
@export(&Exports.client_init, .{ .name = prefix ++ "clientInit", .linkage = .strong });
|
|
271
|
+
@export(&Exports.client_init_echo, .{ .name = prefix ++ "clientInitEcho", .linkage = .strong });
|
|
272
|
+
@export(&Exports.client_deinit, .{ .name = prefix ++ "clientDeinit", .linkage = .strong });
|
|
273
|
+
@export(&Exports.submit, .{ .name = prefix ++ "submit", .linkage = .strong });
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/// Reflection helper and metadata cache.
|
|
277
|
+
const ReflectionHelper = struct {
|
|
278
|
+
var initialization_exception_class: jni.JClass = null;
|
|
279
|
+
var initialization_exception_ctor_id: jni.JMethodID = null;
|
|
280
|
+
var client_closed_exception_class: jni.JClass = null;
|
|
281
|
+
var assertion_error_class: jni.JClass = null;
|
|
282
|
+
|
|
283
|
+
var request_class: jni.JClass = null;
|
|
284
|
+
var request_send_buffer_field_id: jni.JFieldID = null;
|
|
285
|
+
var request_send_buffer_len_field_id: jni.JFieldID = null;
|
|
286
|
+
var request_reply_buffer_field_id: jni.JFieldID = null;
|
|
287
|
+
var request_operation_method_id: jni.JMethodID = null;
|
|
288
|
+
var request_end_request_method_id: jni.JMethodID = null;
|
|
289
|
+
|
|
290
|
+
pub fn load(env: *jni.JNIEnv) void {
|
|
291
|
+
// Asserting we are not initialized yet:
|
|
292
|
+
assert(initialization_exception_class == null);
|
|
293
|
+
assert(initialization_exception_ctor_id == null);
|
|
294
|
+
assert(client_closed_exception_class == null);
|
|
295
|
+
assert(assertion_error_class == null);
|
|
296
|
+
assert(request_class == null);
|
|
297
|
+
assert(request_send_buffer_field_id == null);
|
|
298
|
+
assert(request_send_buffer_len_field_id == null);
|
|
299
|
+
assert(request_reply_buffer_field_id == null);
|
|
300
|
+
assert(request_operation_method_id == null);
|
|
301
|
+
assert(request_end_request_method_id == null);
|
|
302
|
+
|
|
303
|
+
initialization_exception_class = JNIHelper.find_class(
|
|
304
|
+
env,
|
|
305
|
+
"com/tigerbeetle/InitializationException",
|
|
306
|
+
);
|
|
307
|
+
initialization_exception_ctor_id = JNIHelper.find_method(
|
|
308
|
+
env,
|
|
309
|
+
initialization_exception_class,
|
|
310
|
+
"<init>",
|
|
311
|
+
"(I)V",
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
client_closed_exception_class = JNIHelper.find_class(
|
|
315
|
+
env,
|
|
316
|
+
"com/tigerbeetle/ClientClosedException",
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
assertion_error_class = JNIHelper.find_class(
|
|
320
|
+
env,
|
|
321
|
+
"com/tigerbeetle/AssertionError",
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
request_class = JNIHelper.find_class(
|
|
325
|
+
env,
|
|
326
|
+
"com/tigerbeetle/Request",
|
|
327
|
+
);
|
|
328
|
+
request_send_buffer_field_id = JNIHelper.find_field(
|
|
329
|
+
env,
|
|
330
|
+
request_class,
|
|
331
|
+
"sendBuffer",
|
|
332
|
+
"Ljava/nio/ByteBuffer;",
|
|
333
|
+
);
|
|
334
|
+
request_send_buffer_len_field_id = JNIHelper.find_field(
|
|
335
|
+
env,
|
|
336
|
+
request_class,
|
|
337
|
+
"sendBufferLen",
|
|
338
|
+
"J",
|
|
339
|
+
);
|
|
340
|
+
request_reply_buffer_field_id = JNIHelper.find_field(
|
|
341
|
+
env,
|
|
342
|
+
request_class,
|
|
343
|
+
"replyBuffer",
|
|
344
|
+
"[B",
|
|
345
|
+
);
|
|
346
|
+
request_operation_method_id = JNIHelper.find_method(
|
|
347
|
+
env,
|
|
348
|
+
request_class,
|
|
349
|
+
"getOperation",
|
|
350
|
+
"()B",
|
|
351
|
+
);
|
|
352
|
+
request_end_request_method_id = JNIHelper.find_method(
|
|
353
|
+
env,
|
|
354
|
+
request_class,
|
|
355
|
+
"endRequest",
|
|
356
|
+
"(BBJ)V",
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
// Asserting we are full initialized:
|
|
360
|
+
assert(initialization_exception_class != null);
|
|
361
|
+
assert(initialization_exception_ctor_id != null);
|
|
362
|
+
assert(client_closed_exception_class != null);
|
|
363
|
+
assert(assertion_error_class != null);
|
|
364
|
+
assert(request_class != null);
|
|
365
|
+
assert(request_send_buffer_field_id != null);
|
|
366
|
+
assert(request_send_buffer_len_field_id != null);
|
|
367
|
+
assert(request_reply_buffer_field_id != null);
|
|
368
|
+
assert(request_operation_method_id != null);
|
|
369
|
+
assert(request_end_request_method_id != null);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
pub fn unload(env: *jni.JNIEnv) void {
|
|
373
|
+
env.delete_global_ref(initialization_exception_class);
|
|
374
|
+
env.delete_global_ref(client_closed_exception_class);
|
|
375
|
+
env.delete_global_ref(assertion_error_class);
|
|
376
|
+
env.delete_global_ref(request_class);
|
|
377
|
+
|
|
378
|
+
initialization_exception_class = null;
|
|
379
|
+
initialization_exception_ctor_id = null;
|
|
380
|
+
client_closed_exception_class = null;
|
|
381
|
+
assertion_error_class = null;
|
|
382
|
+
request_class = null;
|
|
383
|
+
request_send_buffer_field_id = null;
|
|
384
|
+
request_send_buffer_len_field_id = null;
|
|
385
|
+
request_reply_buffer_field_id = null;
|
|
386
|
+
request_operation_method_id = null;
|
|
387
|
+
request_end_request_method_id = null;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
pub fn initialization_exception_throw(
|
|
391
|
+
env: *jni.JNIEnv,
|
|
392
|
+
status: tb.exports.tb_init_status,
|
|
393
|
+
) void {
|
|
394
|
+
assert(initialization_exception_class != null);
|
|
395
|
+
assert(initialization_exception_ctor_id != null);
|
|
396
|
+
|
|
397
|
+
const exception = env.new_object(
|
|
398
|
+
initialization_exception_class,
|
|
399
|
+
initialization_exception_ctor_id,
|
|
400
|
+
&[_]jni.JValue{jni.JValue.to_jvalue(@as(jni.JInt, @bitCast(@intFromEnum(status))))},
|
|
401
|
+
) orelse {
|
|
402
|
+
// It's unexpected here: we did not initialize correctly or the JVM is out of memory.
|
|
403
|
+
JNIHelper.vm_panic(
|
|
404
|
+
env,
|
|
405
|
+
"Unexpected error creating a new InitializationException.",
|
|
406
|
+
.{},
|
|
407
|
+
);
|
|
408
|
+
};
|
|
409
|
+
defer env.delete_local_ref(exception);
|
|
410
|
+
|
|
411
|
+
const jni_result = env.throw(exception);
|
|
412
|
+
JNIHelper.check_jni_result(
|
|
413
|
+
env,
|
|
414
|
+
jni_result,
|
|
415
|
+
"Unexpected error throwing InitializationException.",
|
|
416
|
+
.{},
|
|
417
|
+
);
|
|
418
|
+
assert(env.exception_check() == .jni_true);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
pub fn client_closed_exception_throw(
|
|
422
|
+
env: *jni.JNIEnv,
|
|
423
|
+
) void {
|
|
424
|
+
assert(client_closed_exception_class != null);
|
|
425
|
+
|
|
426
|
+
const jni_result = env.throw_new(client_closed_exception_class, null);
|
|
427
|
+
JNIHelper.check_jni_result(
|
|
428
|
+
env,
|
|
429
|
+
jni_result,
|
|
430
|
+
"Unexpected error throwing ClientClosedException.",
|
|
431
|
+
.{},
|
|
432
|
+
);
|
|
433
|
+
assert(env.exception_check() == .jni_true);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
pub fn assertion_error_throw(env: *jni.JNIEnv, message: [:0]const u8) void {
|
|
437
|
+
assert(assertion_error_class != null);
|
|
438
|
+
|
|
439
|
+
const jni_result = env.throw_new(assertion_error_class, message.ptr);
|
|
440
|
+
JNIHelper.check_jni_result(
|
|
441
|
+
env,
|
|
442
|
+
jni_result,
|
|
443
|
+
"Unexpected error throwing AssertionError.",
|
|
444
|
+
.{},
|
|
445
|
+
);
|
|
446
|
+
assert(env.exception_check() == .jni_true);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
pub fn get_client_from_buffer(env: *jni.JNIEnv, buffer: jni.JObject) *tb.ClientInterface {
|
|
450
|
+
// The Java buffer isn't aligned,
|
|
451
|
+
// so it is allocated with extra bytes to accommodate a potential `alignForward`.
|
|
452
|
+
const buffer_capacity = env.get_direct_buffer_capacity(buffer);
|
|
453
|
+
assert(buffer_capacity >= @sizeOf(tb.ClientInterface) + @alignOf(tb.ClientInterface));
|
|
454
|
+
|
|
455
|
+
const address = env.get_direct_buffer_address(buffer) orelse {
|
|
456
|
+
// Unexpected here: `tb_client_buffer` should be initialized by the Java side.
|
|
457
|
+
JNIHelper.vm_panic(
|
|
458
|
+
env,
|
|
459
|
+
"Unexpected tb_client direct nio buffer.",
|
|
460
|
+
.{},
|
|
461
|
+
);
|
|
462
|
+
};
|
|
463
|
+
return @ptrFromInt(std.mem.alignForward(
|
|
464
|
+
usize,
|
|
465
|
+
@intFromPtr(address),
|
|
466
|
+
@alignOf(tb.ClientInterface),
|
|
467
|
+
));
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
pub fn get_send_buffer_slice(env: *jni.JNIEnv, this_obj: jni.JObject) ?[]u8 {
|
|
471
|
+
assert(this_obj != null);
|
|
472
|
+
assert(request_send_buffer_field_id != null);
|
|
473
|
+
assert(request_send_buffer_len_field_id != null);
|
|
474
|
+
|
|
475
|
+
const buffer_obj = env.get_object_field(this_obj, request_send_buffer_field_id) orelse
|
|
476
|
+
return null;
|
|
477
|
+
defer env.delete_local_ref(buffer_obj);
|
|
478
|
+
|
|
479
|
+
const direct_buffer: []u8 = JNIHelper.get_direct_buffer(env, buffer_obj) orelse
|
|
480
|
+
return null;
|
|
481
|
+
|
|
482
|
+
const buffer_len = env.get_long_field(this_obj, request_send_buffer_len_field_id);
|
|
483
|
+
if (buffer_len < 0 or buffer_len > direct_buffer.len)
|
|
484
|
+
return null;
|
|
485
|
+
|
|
486
|
+
return direct_buffer[0..@as(usize, @intCast(buffer_len))];
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
pub fn set_reply_buffer(
|
|
490
|
+
env: *jni.JNIEnv,
|
|
491
|
+
this_obj: jni.JObject,
|
|
492
|
+
reply: []const u8,
|
|
493
|
+
) void {
|
|
494
|
+
assert(this_obj != null);
|
|
495
|
+
assert(request_reply_buffer_field_id != null);
|
|
496
|
+
assert(reply.len > 0);
|
|
497
|
+
|
|
498
|
+
const reply_buffer_obj = env.new_byte_array(
|
|
499
|
+
@intCast(reply.len),
|
|
500
|
+
) orelse {
|
|
501
|
+
// Cannot allocate an array, it's likely the JVM has run out of resources.
|
|
502
|
+
// Printing the buffer size here just to help diagnosing how much memory was required.
|
|
503
|
+
JNIHelper.vm_panic(
|
|
504
|
+
env,
|
|
505
|
+
"Unexpected error calling NewByteArray len={}",
|
|
506
|
+
.{reply.len},
|
|
507
|
+
);
|
|
508
|
+
};
|
|
509
|
+
defer env.delete_local_ref(reply_buffer_obj);
|
|
510
|
+
|
|
511
|
+
env.set_byte_array_region(
|
|
512
|
+
reply_buffer_obj,
|
|
513
|
+
0,
|
|
514
|
+
@intCast(reply.len),
|
|
515
|
+
@ptrCast(reply.ptr),
|
|
516
|
+
);
|
|
517
|
+
|
|
518
|
+
if (env.exception_check() == .jni_true) {
|
|
519
|
+
// Since out-of-bounds isn't expected here, we can only panic if it fails.
|
|
520
|
+
JNIHelper.vm_panic(
|
|
521
|
+
env,
|
|
522
|
+
"Unexpected exception calling JNIEnv.SetByteArrayRegion len={}",
|
|
523
|
+
.{reply.len},
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// Setting the request with the reply.
|
|
528
|
+
env.set_object_field(
|
|
529
|
+
this_obj,
|
|
530
|
+
request_reply_buffer_field_id,
|
|
531
|
+
reply_buffer_obj,
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
pub fn get_request_operation(env: *jni.JNIEnv, this_obj: jni.JObject) u8 {
|
|
536
|
+
assert(this_obj != null);
|
|
537
|
+
assert(request_class != null);
|
|
538
|
+
assert(request_operation_method_id != null);
|
|
539
|
+
|
|
540
|
+
const value = env.call_nonvirtual_byte_method(
|
|
541
|
+
this_obj,
|
|
542
|
+
request_class,
|
|
543
|
+
request_operation_method_id,
|
|
544
|
+
null,
|
|
545
|
+
);
|
|
546
|
+
|
|
547
|
+
if (env.exception_check() == .jni_true) {
|
|
548
|
+
// This method isn't expected to throw any exception.
|
|
549
|
+
JNIHelper.vm_panic(
|
|
550
|
+
env,
|
|
551
|
+
"Unexpected exception calling NativeClient.getOperation",
|
|
552
|
+
.{},
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
return @bitCast(value);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
pub fn end_request(
|
|
559
|
+
env: *jni.JNIEnv,
|
|
560
|
+
this_obj: jni.JObject,
|
|
561
|
+
packet_operation: u8,
|
|
562
|
+
packet_status: tb.PacketStatus,
|
|
563
|
+
timestamp: u64,
|
|
564
|
+
) void {
|
|
565
|
+
assert(this_obj != null);
|
|
566
|
+
assert(request_class != null);
|
|
567
|
+
assert(request_end_request_method_id != null);
|
|
568
|
+
assert((timestamp > 0) == (packet_status == .ok));
|
|
569
|
+
|
|
570
|
+
env.call_nonvirtual_void_method(
|
|
571
|
+
this_obj,
|
|
572
|
+
request_class,
|
|
573
|
+
request_end_request_method_id,
|
|
574
|
+
&[_]jni.JValue{
|
|
575
|
+
jni.JValue.to_jvalue(@as(jni.JByte, @bitCast(packet_operation))),
|
|
576
|
+
jni.JValue.to_jvalue(@as(jni.JByte, @bitCast(@intFromEnum(packet_status)))),
|
|
577
|
+
jni.JValue.to_jvalue(@as(jni.JLong, @bitCast(timestamp))),
|
|
578
|
+
},
|
|
579
|
+
);
|
|
580
|
+
|
|
581
|
+
if (env.exception_check() == .jni_true) {
|
|
582
|
+
// The "endRequest" method isn't expected to throw any exception,
|
|
583
|
+
// We can't rethrow here, since this function is called from the native callback.
|
|
584
|
+
JNIHelper.vm_panic(
|
|
585
|
+
env,
|
|
586
|
+
"Unexpected exception calling NativeClient.endRequest",
|
|
587
|
+
.{},
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
/// Common functions for handling errors and results in JNI calls.
|
|
594
|
+
const JNIHelper = struct {
|
|
595
|
+
pub inline fn get_env(vm: *jni.JavaVM) *jni.JNIEnv {
|
|
596
|
+
var env: *jni.JNIEnv = undefined;
|
|
597
|
+
const jni_result = vm.get_env(&env, jni_version);
|
|
598
|
+
if (jni_result != .ok) {
|
|
599
|
+
const message = "Unexpected result calling JavaVM.GetEnv";
|
|
600
|
+
log.err(
|
|
601
|
+
message ++ "; Error = {} ({s})",
|
|
602
|
+
.{ @intFromEnum(jni_result), @tagName(jni_result) },
|
|
603
|
+
);
|
|
604
|
+
@panic("JNI: " ++ message);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
return env;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
pub inline fn try_get_env(vm: *jni.JavaVM) ?*jni.JNIEnv {
|
|
611
|
+
var env: *jni.JNIEnv = undefined;
|
|
612
|
+
const jni_result = vm.get_env(&env, jni_version);
|
|
613
|
+
return switch (jni_result) {
|
|
614
|
+
.ok => env,
|
|
615
|
+
.thread_detached => null,
|
|
616
|
+
else => {
|
|
617
|
+
const message = "Unexpected result calling JavaVM.GetEnv";
|
|
618
|
+
log.err(
|
|
619
|
+
message ++ "; Error = {} ({s})",
|
|
620
|
+
.{ @intFromEnum(jni_result), @tagName(jni_result) },
|
|
621
|
+
);
|
|
622
|
+
@panic("JNI: " ++ message);
|
|
623
|
+
},
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
pub inline fn get_java_vm(env: *jni.JNIEnv) *jni.JavaVM {
|
|
628
|
+
var jvm: *jni.JavaVM = undefined;
|
|
629
|
+
const jni_result = env.get_java_vm(&jvm);
|
|
630
|
+
check_jni_result(
|
|
631
|
+
env,
|
|
632
|
+
jni_result,
|
|
633
|
+
"Unexpected result calling JNIEnv.GetJavaVM",
|
|
634
|
+
.{},
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
return jvm;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
pub inline fn vm_panic(
|
|
641
|
+
env: *jni.JNIEnv,
|
|
642
|
+
comptime fmt: []const u8,
|
|
643
|
+
args: anytype,
|
|
644
|
+
) noreturn {
|
|
645
|
+
env.exception_describe();
|
|
646
|
+
log.err(fmt, args);
|
|
647
|
+
|
|
648
|
+
var buf: [256]u8 = undefined;
|
|
649
|
+
const message: [:0]const u8 = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| switch (err) {
|
|
650
|
+
error.NoSpaceLeft => blk: {
|
|
651
|
+
buf[255] = 0;
|
|
652
|
+
break :blk @ptrCast(buf[0..255]);
|
|
653
|
+
},
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
env.fatal_error(message.ptr);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
pub inline fn check_jni_result(
|
|
660
|
+
env: *jni.JNIEnv,
|
|
661
|
+
jni_result: jni.JNIResultType,
|
|
662
|
+
comptime fmt: []const u8,
|
|
663
|
+
args: anytype,
|
|
664
|
+
) void {
|
|
665
|
+
if (jni_result != .ok) {
|
|
666
|
+
vm_panic(
|
|
667
|
+
env,
|
|
668
|
+
fmt ++ "; Error = {} ({s})",
|
|
669
|
+
args ++ .{ @intFromEnum(jni_result), @tagName(jni_result) },
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
pub inline fn find_class(env: *jni.JNIEnv, comptime class_name: [:0]const u8) jni.JClass {
|
|
675
|
+
const class_obj = env.find_class(class_name.ptr) orelse {
|
|
676
|
+
vm_panic(
|
|
677
|
+
env,
|
|
678
|
+
"Unexpected result calling JNIEnv.FindClass for {s}",
|
|
679
|
+
.{class_name},
|
|
680
|
+
);
|
|
681
|
+
};
|
|
682
|
+
defer env.delete_local_ref(class_obj);
|
|
683
|
+
|
|
684
|
+
return env.new_global_ref(class_obj) orelse {
|
|
685
|
+
vm_panic(
|
|
686
|
+
env,
|
|
687
|
+
"Unexpected result calling JNIEnv.NewGlobalRef for {s}",
|
|
688
|
+
.{class_name},
|
|
689
|
+
);
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
pub inline fn find_field(
|
|
694
|
+
env: *jni.JNIEnv,
|
|
695
|
+
class: jni.JClass,
|
|
696
|
+
comptime name: [:0]const u8,
|
|
697
|
+
comptime signature: [:0]const u8,
|
|
698
|
+
) jni.JFieldID {
|
|
699
|
+
return env.get_field_id(class, name.ptr, signature.ptr) orelse
|
|
700
|
+
vm_panic(
|
|
701
|
+
env,
|
|
702
|
+
"Field could not be found {s} {s}",
|
|
703
|
+
.{ name, signature },
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
pub inline fn find_method(
|
|
708
|
+
env: *jni.JNIEnv,
|
|
709
|
+
class: jni.JClass,
|
|
710
|
+
comptime name: [:0]const u8,
|
|
711
|
+
comptime signature: [:0]const u8,
|
|
712
|
+
) jni.JMethodID {
|
|
713
|
+
return env.get_method_id(class, name.ptr, signature.ptr) orelse
|
|
714
|
+
vm_panic(
|
|
715
|
+
env,
|
|
716
|
+
"Method could not be found {s} {s}",
|
|
717
|
+
.{ name, signature },
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
pub inline fn get_direct_buffer(
|
|
722
|
+
env: *jni.JNIEnv,
|
|
723
|
+
buffer_obj: jni.JObject,
|
|
724
|
+
) ?[]u8 {
|
|
725
|
+
const buffer_capacity = env.get_direct_buffer_capacity(buffer_obj);
|
|
726
|
+
if (buffer_capacity < 0) return null;
|
|
727
|
+
|
|
728
|
+
const buffer_address = env.get_direct_buffer_address(buffer_obj) orelse return null;
|
|
729
|
+
return buffer_address[0..@as(u32, @intCast(buffer_capacity))];
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
pub inline fn new_global_reference(env: *jni.JNIEnv, obj: jni.JObject) jni.JObject {
|
|
733
|
+
return env.new_global_ref(obj) orelse {
|
|
734
|
+
// NewGlobalRef fails only when the JVM runs out of memory.
|
|
735
|
+
JNIHelper.vm_panic(env, "Unexpected result calling JNIEnv.NewGlobalRef", .{});
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
pub inline fn get_string_utf(env: *jni.JNIEnv, string: jni.JString) ?[:0]const u8 {
|
|
740
|
+
if (string == null) return null;
|
|
741
|
+
|
|
742
|
+
const address = env.get_string_utf_chars(string, null) orelse return null;
|
|
743
|
+
const length = env.get_string_utf_length(string);
|
|
744
|
+
if (length < 0) return null;
|
|
745
|
+
|
|
746
|
+
return @ptrCast(address[0..@as(usize, @intCast(length))]);
|
|
747
|
+
}
|
|
748
|
+
};
|