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
|
// https://github.com/memcached/memcached/wiki/Commands
// https://ziglang.org/documentation/master/std/#std.hash_map.HashMap
const std = @import("std");
const Error = error{
Failed,
KeyNotFound,
};
const Store = struct {
data: std.StringHashMap([]const u8),
pub fn set(self: *Store, key: []const u8, value: []const u8) !void {
self.data.put(key, value) catch |err| {
std.log.err("Failed adding key `{s}` with error {}", .{ key, err });
return error.Failed;
};
}
pub fn get(self: *Store, key: []const u8) ![]const u8 {
if (self.data.get(key)) |value| {
return value;
}
return error.KeyNotFound;
}
pub fn del(self: *Store, key: []const u8) !void {
if (!self.data.remove(key)) {
std.log.err("Failed deleting key `{s}`", .{key});
return error.KeyNotFound;
}
}
};
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var allocator = arena.allocator();
var store = Store{ .data = std.StringHashMap([]const u8).init(allocator) };
defer store.data.deinit();
// Setting new key.
{
const key = "johhny";
try store.set(key, "blaze");
}
// Retrieving back existing key if we know it will never return an error.
{
const key = "johhny";
const result = store.get(key) catch unreachable;
std.log.info("Value: {s}", .{result});
}
// Retrieving back the value and handling errors.
{
const key = "unknown";
if (store.get(key)) |val| {
std.log.info("Value: {s}", .{val});
} else |err| {
std.log.err("Error: {any}", .{err});
}
}
// Retrieving back key that does not exist and handling specific errors.
{
const key = "unknown";
if (store.get(key)) |val| {
std.log.info("Value: {s}", .{val});
} else |err| switch (err) {
error.KeyNotFound => {
std.log.err("Key `{s}` was not found", .{key});
},
}
}
// Deleting existing key.
{
const key = "johhny";
try store.del(key);
}
// Retrieving back the value and handling errors.
{
const key = "johnny";
if (store.get(key)) |val| {
std.log.info("Value: {s}", .{val});
} else |err| {
std.log.err("Error: {any}", .{err});
}
}
}
|