aboutsummaryrefslogtreecommitdiff
path: root/src/blob.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-08-29 21:45:53 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-08-29 21:45:53 -0700
commit69b101adcffc48e73153f581665f487ee4abe975 (patch)
tree73ccc7eebe885a6b52fd6e0942f1c330d3be1b34 /src/blob.zig
parentbd0f74a6fd756ebcccc629d968a3db2f789e9697 (diff)
downloadbun-69b101adcffc48e73153f581665f487ee4abe975.tar.gz
bun-69b101adcffc48e73153f581665f487ee4abe975.tar.zst
bun-69b101adcffc48e73153f581665f487ee4abe975.zip
Blob
Former-commit-id: 2174c6b769676c50686bb23a5ecca26a1d6eb1bc
Diffstat (limited to 'src/blob.zig')
-rw-r--r--src/blob.zig67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/blob.zig b/src/blob.zig
new file mode 100644
index 000000000..9e0eba407
--- /dev/null
+++ b/src/blob.zig
@@ -0,0 +1,67 @@
+const std = @import("std");
+const Lock = @import("./lock.zig").Lock;
+usingnamespace @import("./global.zig");
+
+const Blob = @This();
+
+ptr: [*]const u8,
+len: usize,
+
+pub const Map = struct {
+ const MapContext = struct {
+ pub fn hash(self: @This(), s: u64) u32 {
+ return @truncate(u32, s);
+ }
+ pub fn eql(self: @This(), a: u64, b: u64) bool {
+ return a == b;
+ }
+ };
+
+ const HashMap = std.ArrayHashMap(u64, Blob, MapContext, false);
+ lock: Lock,
+ map: HashMap,
+ allocator: *std.mem.Allocator,
+
+ pub fn init(allocator: *std.mem.Allocator) Map {
+ return Map{
+ .lock = Lock.init(),
+ .map = HashMap.init(allocator),
+ .allocator = allocator,
+ };
+ }
+
+ pub fn get(this: *Map, key: string) ?Blob {
+ this.lock.lock();
+ defer this.lock.unlock();
+ return this.map.get(std.hash.Wyhash.hash(0, key));
+ }
+
+ pub fn put(this: *Map, key: string, blob: Blob) !void {
+ this.lock.lock();
+ defer this.lock.unlock();
+
+ return try this.map.put(std.hash.Wyhash.hash(0, key), blob);
+ }
+
+ pub fn reset(this: *Map) !void {
+ this.lock.lock();
+ defer this.lock.unlock();
+ this.map.clearRetainingCapacity();
+ }
+};
+
+pub const Group = struct {
+ persistent: Map,
+ temporary: Map,
+ allocator: *std.mem.Allocator,
+
+ pub fn init(allocator: *std.mem.Allocator) !*Group {
+ var group = try allocator.create(Group);
+ group.* = Group{ .persistent = Map.init(allocator), .temporary = Map.init(allocator), .allocator = allocator };
+ return group;
+ }
+
+ pub fn get(this: *Group, key: string) ?Blob {
+ return this.temporary.get(key) orelse this.persistent.get(key);
+ }
+};