Create Table abstraction
This commit is contained in:
parent
16d093b4f7
commit
f5548c0243
2 changed files with 56 additions and 58 deletions
|
@ -58,7 +58,7 @@ pub const ApiServer = struct {
|
|||
// TODO: check for dupes
|
||||
|
||||
const note = reify(models.Note, id, info);
|
||||
try self.db.putNote(note);
|
||||
try self.db.notes.put(note);
|
||||
|
||||
return note;
|
||||
}
|
||||
|
@ -68,12 +68,12 @@ pub const ApiServer = struct {
|
|||
// TODO: check for dupes
|
||||
|
||||
const user = reify(models.User, id, info);
|
||||
try self.db.putUser(user);
|
||||
try self.db.users.put(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
pub fn getNote(self: *ApiServer, id: Uuid, alloc: std.mem.Allocator) !?models.Note {
|
||||
return self.db.getNote(id, alloc);
|
||||
return self.db.notes.get(id, alloc);
|
||||
}
|
||||
};
|
||||
|
|
108
src/main/db.zig
108
src/main/db.zig
|
@ -48,74 +48,72 @@ pub fn free(alloc: std.mem.Allocator, val: anytype) void {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn Table(comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
internal_alloc: std.mem.Allocator,
|
||||
data: std.AutoHashMap(Uuid, T),
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator) !Self {
|
||||
return Self{
|
||||
.internal_alloc = alloc,
|
||||
.data = std.AutoHashMap(Uuid, T).init(alloc),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
var iter = self.data.iterator();
|
||||
while (iter.next()) |it| {
|
||||
free(self.internal_alloc, it.value_ptr.*);
|
||||
}
|
||||
|
||||
self.data.deinit();
|
||||
}
|
||||
|
||||
pub fn contains(self: *Self, id: Uuid) !bool {
|
||||
return self.data.contains(id);
|
||||
}
|
||||
|
||||
// returns a copy of the note data from storage. memory is allocated with the provided
|
||||
// allocator. can be freed using free() above
|
||||
pub fn get(self: *Self, id: Uuid, alloc: std.mem.Allocator) !?T {
|
||||
const data = self.data.get(id) orelse return null;
|
||||
return try clone(alloc, data);
|
||||
}
|
||||
|
||||
pub fn put(self: *Self, data: T) !void {
|
||||
const copy = try clone(self.internal_alloc, data);
|
||||
errdefer free(self.internal_alloc, copy);
|
||||
|
||||
const key = copy.id;
|
||||
|
||||
if (self.data.fetchRemove(key)) |e| {
|
||||
free(self.internal_alloc, e.value);
|
||||
}
|
||||
try self.data.put(key, copy);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub const Database = struct {
|
||||
internal_alloc: std.mem.Allocator,
|
||||
notes: std.AutoHashMap(Uuid, models.Note),
|
||||
users: std.AutoHashMap(Uuid, models.User),
|
||||
notes: Table(models.Note),
|
||||
users: Table(models.User),
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator) !Database {
|
||||
var db = Database{
|
||||
.internal_alloc = alloc,
|
||||
.notes = std.AutoHashMap(Uuid, models.Note).init(alloc),
|
||||
.users = std.AutoHashMap(Uuid, models.User).init(alloc),
|
||||
.notes = try Table(models.Note).init(alloc),
|
||||
.users = try Table(models.User).init(alloc),
|
||||
};
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Database) void {
|
||||
var iter = self.notes.iterator();
|
||||
while (iter.next()) |it| {
|
||||
free(self.internal_alloc, it.value_ptr.*);
|
||||
}
|
||||
|
||||
self.notes.deinit();
|
||||
}
|
||||
|
||||
pub fn containsNote(self: *Database, id: Uuid) !bool {
|
||||
return self.notes.contains(id);
|
||||
}
|
||||
|
||||
// returns a copy of the note data from storage. memory is allocated with the provided
|
||||
// allocator. can be freed using free() above
|
||||
pub fn getNote(self: *Database, id: Uuid, alloc: std.mem.Allocator) !?models.Note {
|
||||
const note = self.notes.get(id) orelse return null;
|
||||
return try clone(alloc, note);
|
||||
}
|
||||
|
||||
pub fn putNote(self: *Database, note: models.Note) !void {
|
||||
const copy = try clone(self.internal_alloc, note);
|
||||
errdefer free(self.internal_alloc, copy);
|
||||
|
||||
const key = copy.id;
|
||||
|
||||
if (self.notes.fetchRemove(key)) |e| {
|
||||
free(self.internal_alloc, e.value);
|
||||
}
|
||||
try self.notes.put(key, copy);
|
||||
}
|
||||
|
||||
pub fn containsUser(self: *Database, id: Uuid) !bool {
|
||||
return self.users.contains(id);
|
||||
}
|
||||
|
||||
// returns a copy of the note data from storage. memory is allocated with the provided
|
||||
// allocator. can be freed using free() above
|
||||
pub fn getUser(self: *Database, id: Uuid, alloc: std.mem.Allocator) !?models.User {
|
||||
const note = self.users.get(id) orelse return null;
|
||||
return try clone(alloc, note);
|
||||
}
|
||||
|
||||
pub fn putUser(self: *Database, note: models.User) !void {
|
||||
const copy = try clone(self.internal_alloc, note);
|
||||
errdefer free(self.internal_alloc, copy);
|
||||
|
||||
const key = copy.id;
|
||||
|
||||
if (self.users.fetchRemove(key)) |e| {
|
||||
free(self.internal_alloc, e.value);
|
||||
}
|
||||
try self.users.put(key, copy);
|
||||
self.users.deinit();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue