Skip to content

Commit

Permalink
add another comptime generated type to wrap the original type to stor…
Browse files Browse the repository at this point in the history
…e IDs and parent IDs and custom functions
  • Loading branch information
zigster64 committed Jan 4, 2024
1 parent 7f65ab4 commit 5111af6
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 292 deletions.
Binary file modified db/cats.db
Binary file not shown.
Binary file added db/custom.db
Binary file not shown.
Binary file modified db/things.db
Binary file not shown.
19 changes: 8 additions & 11 deletions example/cats.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ const std = @import("std");
const datastor = @import("datastor");

pub const Cat = struct {
id: usize = 0,
breed: []const u8,
color: []const u8,
length: u16,
aggression: f32,

const Self = @This();

pub fn free(self: Self, allocator: std.mem.Allocator) void {
pub fn free(self: Cat, allocator: std.mem.Allocator) void {
allocator.free(self.breed);
allocator.free(self.color);
}

// datastor doesnt need this, but its put here as a util function to print out a Cat
pub fn format(cat: Self, comptime layout: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
pub fn format(cat: Cat, comptime layout: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
_ = options;

if (layout.len != 0 and layout[0] != 's')
@compileError("Unsupported format specifier for Cat type: '" ++ layout ++ "'.");

try std.fmt.format(writer, "ID: {d} Breed: {s} Color: {s} Length: {d}, Aggression Factor: {:.2}", cat);
try std.fmt.format(writer, "Breed: {s} Color: {s} Length: {d}, Aggression Factor: {:.2}", cat);
}
};

Expand All @@ -44,7 +41,7 @@ pub fn createTable() !void {
std.debug.print("\nCats example - save simple data set to table\n\n", .{});

// create a datastor to store the cats
var catDB = try datastor.Table(Cat).init(gpa, "db/cats.db");
var catDB = try datastor.Table(usize, Cat).init(gpa, "db/cats.db");
defer catDB.deinit();

// manually fill in datastor using our example cats seed data, autoincrementing the ID
Expand All @@ -61,7 +58,7 @@ pub fn createTable() !void {
// manually get some cats from the datastore
for (0..4) |i| {
if (catDB.get(i + 1)) |cat| {
std.debug.print("Cat {d} is {s}\n", .{ i, cat });
std.debug.print("Cat {d} id {d} value {s}\n", .{ i, cat.id, cat.value });
} else std.debug.print("No cat found !!\n", .{});
}

Expand All @@ -76,12 +73,12 @@ pub fn loadTable() !void {
std.debug.print("\nCats example - load simple data set from table\n\n", .{});

// create a datastor to store the cats
var catDB = try datastor.Table(Cat).init(gpa, "db/cats.db");
var catDB = try datastor.Table(usize, Cat).init(gpa, "db/cats.db");
defer catDB.deinit();

try catDB.load();
for (catDB.values(), 0..) |cat, i| {
std.debug.print("Cat {d} is {s}\n", .{ i, cat });
std.debug.print("Cat {d} has id {d} value {s}\n", .{ i, cat.id, cat.value });
}

std.debug.print("------------------------------------------------\n", .{});
Expand All @@ -90,7 +87,7 @@ pub fn loadTable() !void {
// calling load again will clear & free the original store and load a fresh new one
try catDB.load();
for (catDB.values(), 0..) |cat, i| {
std.debug.print("Cat {d} is {s}\n", .{ i, cat });
std.debug.print("Cat {d} has id {d} and value {s}\n", .{ i, cat.id, cat.value });
}
}

Expand Down
72 changes: 72 additions & 0 deletions example/custom_id.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const std = @import("std");
const datastor = @import("datastor");

// Example case where the Key that we use to ID each item is some custom function that returns a custom type

const KeyType = [16]u8;
pub const CustomIDThing = struct {
x: usize = 0,
y: usize = 0,

// The ID for our type is a 16 char string with the count encoded
// and data from the internals making up part of the key
pub fn newID(self: CustomIDThing, count: usize) KeyType {
var key: KeyType = undefined;
@memset(&key, 0);
_ = std.fmt.bufPrint(&key, "ABC-{d}-{d}:{d}", .{ count, self.x, self.y }) catch {};
return key;
}
};

pub fn createTable() !void {
// remove the original data file
std.os.unlink("db/custom.db") catch {};

const gpa = std.heap.page_allocator;
std.debug.print("------------------------------------------------\n", .{});
std.debug.print("\nCustom ID example - no allocation per thing\n\n", .{});

// create a datastor to store the things
var db = try datastor.Table(KeyType, CustomIDThing).init(gpa, "db/custom.db");
defer db.deinit();

const things = [_]CustomIDThing{
.{ .x = 1, .y = 2 },
.{ .x = 3, .y = 4 },
.{ .x = 5, .y = 6 },
.{ .x = 7, .y = 8 },
};
// we can use thing here, because there is no heap allocated stuff in the thing,
// and everything is pass by copy
for (things) |thing| {
_ = try db.append(thing);
}

for (db.values(), 0..) |thing, i| {
std.debug.print("Thing {d} has id {s} and value ({d},{d})\n", .{ i, thing.id, thing.value.x, thing.value.y });
}

try db.save();
}

pub fn loadTable() !void {
const gpa = std.heap.page_allocator;
std.debug.print("------------------------------------------------\n", .{});
std.debug.print("\nCustom ID example - load and reload simple data set from table\n\n", .{});

var db = try datastor.Table(KeyType, CustomIDThing).init(gpa, "db/custom.db");
defer db.deinit();

try db.load();
for (db.values(), 0..) |thing, i| {
std.debug.print("Thing {d} has id {s} and value ({d},{d})\n", .{ i, thing.id, thing.value.x, thing.value.y });
}

std.debug.print("------------------------------------------------\n", .{});
std.debug.print("\nReload ... should handle without needing to call free() \n\n", .{});

try db.load();
for (db.values(), 0..) |thing, i| {
std.debug.print("Thing {d} has id {s} and value ({d},{d})\n", .{ i, thing.id, thing.value.x, thing.value.y });
}
}
20 changes: 12 additions & 8 deletions example/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ const dogs = @import("dogs.zig");
const animals = @import("animals.zig");
const forrest = @import("forrest.zig");
const simple = @import("simple.zig");
const custom = @import("custom_id.zig");

pub fn main() !void {
std.debug.print("Datastor examples\n", .{});

try simple.createTable();
try simple.loadTable();

try custom.createTable();
try custom.loadTable();

try cats.createTable();
try cats.loadTable();
try cats.createTimeseries();
try cats.createTimeseriesNoIO(); // for measuring IO performance only
// try cats.createTimeseries();
// try cats.createTimeseriesNoIO(); // for measuring IO performance only

try dogs.createTable();
try dogs.createTimeseries();
// try dogs.createTable();
// try dogs.createTimeseries();

try animals.createTable();
try animals.loadTable();
// try animals.createTable();
// try animals.loadTable();

try forrest.createTable();
try forrest.loadTable();
// try forrest.createTable();
// try forrest.loadTable();
}
14 changes: 5 additions & 9 deletions example/simple.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ const std = @import("std");
const datastor = @import("datastor");

pub const SimpleThing = struct {
const Self = @This();
id: usize = 0,
x: usize = 0,
y: usize = 0,

// nothing gets allocated to create an instance of this, so we dont need a free()
};

pub fn createTable() !void {
Expand All @@ -19,7 +15,7 @@ pub fn createTable() !void {
std.debug.print("\nSimple Things example - no allocation per thing\n\n", .{});

// create a datastor to store the things
var db = try datastor.Table(SimpleThing).init(gpa, "db/things.db");
var db = try datastor.Table(usize, SimpleThing).init(gpa, "db/things.db");
defer db.deinit();

const things = [_]SimpleThing{
Expand All @@ -35,7 +31,7 @@ pub fn createTable() !void {
}

for (db.values(), 0..) |thing, i| {
std.debug.print("Thing {d} is ({d},{d})\n", .{ i, thing.x, thing.y });
std.debug.print("Thing {d} has id {d} is ({d},{d})\n", .{ i, thing.id, thing.value.x, thing.value.y });
}

try db.save();
Expand All @@ -46,19 +42,19 @@ pub fn loadTable() !void {
std.debug.print("------------------------------------------------\n", .{});
std.debug.print("\nThing example - load and reload simple data set from table\n\n", .{});

var db = try datastor.Table(SimpleThing).init(gpa, "db/things.db");
var db = try datastor.Table(usize, SimpleThing).init(gpa, "db/things.db");
defer db.deinit();

try db.load();
for (db.values(), 0..) |thing, i| {
std.debug.print("Thing {d} is ({d},{d})\n", .{ i, thing.x, thing.y });
std.debug.print("Thing {d} has id {d} and value ({d},{d})\n", .{ i, thing.id, thing.value.x, thing.value.y });
}

std.debug.print("------------------------------------------------\n", .{});
std.debug.print("\nReload ... should handle without needing to call free() \n\n", .{});

try db.load();
for (db.values(), 0..) |thing, i| {
std.debug.print("Thing {d} is ({d},{d})\n", .{ i, thing.x, thing.y });
std.debug.print("Thing {d} is ({d},{d})\n", .{ i, thing.value.x, thing.value.y });
}
}
Loading

0 comments on commit 5111af6

Please sign in to comment.