Skip to content

Commit 2133ca7

Browse files
Feat: Initialize Plugin Type and Hook Types
1 parent ccc69f4 commit 2133ca7

3 files changed

Lines changed: 233 additions & 0 deletions

File tree

build.zig

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const std = @import("std");
2+
3+
// Although this function looks imperative, it does not perform the build
4+
// directly and instead it mutates the build graph (`b`) that will be then
5+
// executed by an external runner. The functions in `std.Build` implement a DSL
6+
// for defining build steps and express dependencies between them, allowing the
7+
// build runner to parallelize the build automatically (and the cache system to
8+
// know when a step doesn't need to be re-run).
9+
pub fn build(b: *std.Build) void {
10+
// Standard target options allow the person running `zig build` to choose
11+
// what target to build for. Here we do not override the defaults, which
12+
// means any target is allowed, and the default is native. Other options
13+
// for restricting supported target set are available.
14+
const target = b.standardTargetOptions(.{});
15+
16+
// This creates a module, which represents a collection of source files alongside
17+
// some compilation options, such as optimization mode and linked system libraries.
18+
// Zig modules are the preferred way of making Zig code available to consumers.
19+
// addModule defines a module that we intend to make available for importing
20+
// to our consumers. We must give it a name because a Zig package can expose
21+
// multiple modules and consumers will need to be able to specify which
22+
// module they want to access.
23+
const mod = b.addModule("plumplugin", .{
24+
// The root source file is the "entry point" of this module. Users of
25+
// this module will only be able to access public declarations contained
26+
// in this file, which means that if you have declarations that you
27+
// intend to expose to consumers that were defined in other files part
28+
// of this module, you will have to make sure to re-export them from
29+
// the root file.
30+
.root_source_file = b.path("src/root.zig"),
31+
// Later on we'll use this module as the root module of a test executable
32+
// which requires us to specify a target.
33+
.target = target,
34+
});
35+
36+
// Creates an executable that will run `test` blocks from the provided module.
37+
// Here `mod` needs to define a target, which is why earlier we made sure to
38+
// set the releative field.
39+
const mod_tests = b.addTest(.{
40+
.root_module = mod,
41+
});
42+
43+
// A run step that will run the test executable.
44+
const run_mod_tests = b.addRunArtifact(mod_tests);
45+
46+
// A top level step for running all tests. dependOn can be called multiple
47+
// times and since the two run steps do not depend on one another, this will
48+
// make the two of them run in parallel.
49+
const test_step = b.step("test", "Run tests");
50+
test_step.dependOn(&run_mod_tests.step);
51+
52+
// Just like flags, top level steps are also listed in the `--help` menu.
53+
//
54+
// The Zig build system is entirely implemented in userland, which means
55+
// that it cannot hook into private compiler APIs. All compilation work
56+
// orchestrated by the build system will result in other Zig compiler
57+
// subcommands being invoked with the right flags defined. You can observe
58+
// these invocations when one fails (or you pass a flag to increase
59+
// verbosity) to validate assumptions and diagnose problems.
60+
//
61+
// Lastly, the Zig build system is relatively simple and self-contained,
62+
// and reading its source code will allow you to master it.
63+
}

build.zig.zon

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
.{
2+
// This is the default name used by packages depending on this one. For
3+
// example, when a user runs `zig fetch --save <url>`, this field is used
4+
// as the key in the `dependencies` table. Although the user can choose a
5+
// different name, most users will stick with this provided value.
6+
//
7+
// It is redundant to include "zig" in this name because it is already
8+
// within the Zig package namespace.
9+
.name = .plumplugin,
10+
// This is a [Semantic Version](https://semver.org/).
11+
// In a future version of Zig it will be used for package deduplication.
12+
.version = "1.0.0",
13+
// Together with name, this represents a globally unique package
14+
// identifier. This field is generated by the Zig toolchain when the
15+
// package is first created, and then *never changes*. This allows
16+
// unambiguous detection of one package being an updated version of
17+
// another.
18+
//
19+
// When forking a Zig project, this id should be regenerated (delete the
20+
// field and run `zig build`) if the upstream project is still maintained.
21+
// Otherwise, the fork is *hostile*, attempting to take control over the
22+
// original project's identity. Thus it is recommended to leave the comment
23+
// on the following line intact, so that it shows up in code reviews that
24+
// modify the field.
25+
.fingerprint = 0xf9e9abb823c14aea, // Changing this has security and trust implications.
26+
// Tracks the earliest Zig version that the package considers to be a
27+
// supported use case.
28+
.minimum_zig_version = "0.15.1",
29+
// This field is optional.
30+
// Each dependency must either provide a `url` and `hash`, or a `path`.
31+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
32+
// Once all dependencies are fetched, `zig build` no longer requires
33+
// internet connectivity.
34+
.dependencies = .{
35+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
36+
//.example = .{
37+
// // When updating this field to a new URL, be sure to delete the corresponding
38+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
39+
// // the new URL. If the contents of a URL change this will result in a hash mismatch
40+
// // which will prevent zig from using it.
41+
// .url = "https://example.com/foo.tar.gz",
42+
//
43+
// // This is computed from the file contents of the directory of files that is
44+
// // obtained after fetching `url` and applying the inclusion rules given by
45+
// // `paths`.
46+
// //
47+
// // This field is the source of truth; packages do not come from a `url`; they
48+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
49+
// // obtain a package matching this `hash`.
50+
// //
51+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
52+
// .hash = "...",
53+
//
54+
// // When this is provided, the package is found in a directory relative to the
55+
// // build root. In this case the package's hash is irrelevant and therefore not
56+
// // computed. This field and `url` are mutually exclusive.
57+
// .path = "foo",
58+
//
59+
// // When this is set to `true`, a package is declared to be lazily
60+
// // fetched. This makes the dependency only get fetched if it is
61+
// // actually used.
62+
// .lazy = false,
63+
//},
64+
},
65+
// Specifies the set of files and directories that are included in this package.
66+
// Only files and directories listed here are included in the `hash` that
67+
// is computed for this package. Only files listed here will remain on disk
68+
// when using the zig package manager. As a rule of thumb, one should list
69+
// files required for compilation plus any license(s).
70+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
71+
// the build root itself.
72+
// A directory listed here means that all files within, recursively, are included.
73+
.paths = .{
74+
"build.zig",
75+
"build.zig.zon",
76+
"src",
77+
// For example...
78+
//"LICENSE",
79+
//"README.md",
80+
},
81+
}

src/root.zig

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//! This module defines the plugin system for PlumCache.
2+
//!
3+
//! The plugin system enables extensibility through hooks that can intercept and modify
4+
//! database operations at various points in their execution lifecycle. This allows
5+
//! for custom functionality such as validation, transformation, logging, and more,
6+
//! without modifying the core database code.
7+
//!
8+
//! The main components are:
9+
//! - `HookType`: An enumeration of the available interception points
10+
//! - `Plugin`: A structure that defines a plugin's metadata and execution function
11+
12+
/// `HookType` defines the points in the database operation lifecycle where plugins can intercept.
13+
///
14+
/// Each hook type represents a specific moment during data operations:
15+
/// - `Before*` hooks run before an operation is performed, allowing for validation or modification
16+
/// - `After*` hooks run after an operation completes, enabling post-processing or side effects
17+
///
18+
/// This enum-based approach allows for type-safe hook registration and dispatch.
19+
const std = @import("std");
20+
const builtin = @import("builtin");
21+
const native = builtin.os;
22+
23+
pub const HookType = enum {
24+
/// Triggered before retrieving a key's value
25+
BeforeGet,
26+
/// Triggered before saving a key-value pair
27+
BeforeSave,
28+
/// Triggered before deleting a key
29+
BeforeDelete,
30+
/// Triggered after retrieving a key's value
31+
AfterGet,
32+
/// Triggered after saving a key-value pair
33+
AfterSave,
34+
/// Triggered after deleting a key
35+
AfterDelete,
36+
};
37+
38+
/// `Plugin` represents an extension module that can hook into the database operations.
39+
///
40+
/// Each plugin has:
41+
/// - A name for identification
42+
/// - A hook type specifying when it should be executed
43+
/// - A function that will be called when the hook triggers
44+
///
45+
/// The plugin's run function receives the operation's key and value as parameters,
46+
/// allowing it to inspect or modify the data being processed.
47+
pub const Plugin = struct {
48+
/// Unique identifier for the plugin
49+
name: []const u8,
50+
/// The point in execution where this plugin should be triggered
51+
hook: HookType,
52+
/// Function to call when the hook is triggered.
53+
///
54+
/// Parameters:
55+
/// - `key`: The key involved in the database operation.
56+
/// - `value`: The value involved in the database operation.
57+
///
58+
/// Returns:
59+
/// - `void`. This function does not return a value.
60+
run: *const fn (key: []u8, value: []u8) void,
61+
62+
fn init(name: []u8) Plugin {
63+
const dynLib = loadSystemLibrary(name);
64+
const run = dynLib.findSymbol("run") orelse return error.SymbolNotFound;
65+
return @ptrCast(run());
66+
}
67+
68+
pub fn loadSystemLibrary(lib_name: []const u8) !std.DynLib {
69+
var buffer: [128]u8 = undefined;
70+
71+
// Construct correct library filename for Linux and macOS
72+
const lib_full_name = switch (std.builtin.os.tag) {
73+
.linux => blk: {
74+
const len = try std.fmt.bufPrint(&buffer, "lib{s}.so", .{lib_name});
75+
break :blk buffer[0..len];
76+
},
77+
.macos => blk: {
78+
const len = try std.fmt.bufPrint(&buffer, "lib{s}.dylib", .{lib_name});
79+
break :blk buffer[0..len];
80+
},
81+
else => return error.UnsupportedOS,
82+
};
83+
84+
std.debug.print("Loading system library: {s}\n", .{lib_full_name});
85+
86+
// Load library from native OS paths
87+
return std.DynLib.open(lib_full_name);
88+
}
89+
};

0 commit comments

Comments
 (0)