Remove references to memdb

This commit is contained in:
jaina heartles 2022-07-16 11:44:46 -07:00
parent 74fb8d2574
commit 90d2dcd4c1

View file

@ -1,13 +1,25 @@
const std = @import("std"); const std = @import("std");
const util = @import("util"); const util = @import("util");
const memdb = @import("./memdb.zig");
const db = @import("./db.zig"); const db = @import("./db.zig");
pub const models = @import("./models.zig"); pub const models = @import("./models.zig");
pub const free = memdb.free;
pub const Uuid = util.Uuid; pub const Uuid = util.Uuid;
// Frees an api struct and its fields allocated from alloc
pub fn free(alloc: std.mem.Allocator, val: anytype) void {
inline for (std.meta.fields(@TypeOf(val))) |f| {
// TODO
if (f.field_type == []u8 or f.field_type == []const u8) {
alloc.free(@field(val, f.name));
} else if (f.field_type == Uuid) {
// nothing
} else {
@compileError("unsupported field type " ++ @typeName(f.field_type));
}
}
}
pub fn CreateInfo(comptime T: type) type { pub fn CreateInfo(comptime T: type) type {
const t_fields = std.meta.fields(T); const t_fields = std.meta.fields(T);
var fields: [t_fields.len - 1]std.builtin.Type.StructField = undefined; var fields: [t_fields.len - 1]std.builtin.Type.StructField = undefined;
@ -38,26 +50,16 @@ fn reify(comptime T: type, id: Uuid, val: CreateInfo(T)) T {
} }
pub const ApiServer = struct { pub const ApiServer = struct {
memdb: memdb.Database,
prng: std.rand.DefaultPrng, prng: std.rand.DefaultPrng,
last_id: u64 = 0,
db: db.Database, db: db.Database,
pub fn init(alloc: std.mem.Allocator) !ApiServer { pub fn init(_: std.mem.Allocator) !ApiServer {
return ApiServer{ return ApiServer{
.memdb = try memdb.Database.init(alloc),
.prng = std.rand.DefaultPrng.init(@bitCast(u64, std.time.milliTimestamp())), .prng = std.rand.DefaultPrng.init(@bitCast(u64, std.time.milliTimestamp())),
.db = try db.Database.init(), .db = try db.Database.init(),
}; };
} }
fn genUuid(self: *ApiServer, alloc: std.mem.Allocator) ![]const u8 {
self.last_id += 1;
return std.fmt.allocPrint(alloc, "{}", .{self.last_id});
}
pub fn createNote(self: *ApiServer, info: CreateInfo(models.Note)) !models.Note { pub fn createNote(self: *ApiServer, info: CreateInfo(models.Note)) !models.Note {
const id = Uuid.randV4(self.prng.random()); const id = Uuid.randV4(self.prng.random());
// TODO: check for dupes // TODO: check for dupes
@ -72,7 +74,6 @@ pub const ApiServer = struct {
const id = Uuid.randV4(self.prng.random()); const id = Uuid.randV4(self.prng.random());
// check for handle dupes // check for handle dupes
//var iter = self.memdb.users.data.iterator();
//while (iter.next()) |it| { //while (iter.next()) |it| {
//if (std.mem.eql(u8, it.value_ptr.handle, info.handle)) { //if (std.mem.eql(u8, it.value_ptr.handle, info.handle)) {
//return error.DuplicateHandle; //return error.DuplicateHandle;