Skip to content

Instantly share code, notes, and snippets.

@likern
Created February 12, 2022 15:18
Show Gist options
  • Save likern/32faa30dd1541454b62b356bb4f7ab8f to your computer and use it in GitHub Desktop.
Save likern/32faa30dd1541454b62b356bb4f7ab8f to your computer and use it in GitHub Desktop.
const std = @import("std");
const expect = std.testing.expect;
const types = @import("./Types.zig");
const HashSize = types.HashSize;
const HashAlgorithm = types.HashAlgorithm;
pub const DatabaseVersion = packed struct {
major: u16,
minor: u16,
patch: u16,
};
pub const FileLayoutVersion = packed struct {
major: u16,
minor: u16,
patch: u16,
};
pub const DatabaseSettings = packed struct {
const Self = @This();
name: [8]u8,
version: DatabaseVersion,
layout: FileLayoutVersion,
// pub fn create(version: DatabaseVersion, layout: FileLayoutVersion) Self {
// const db_settings = DatabaseSettings{
// .name = "\x7a\x65\x6e\x64\x79",
// .version = .{ .major = 0, .minor = 0, .patch = 1 },
// .layout = .{ .major = 0, .minor = 0, .patch = 1 },
// };
// }
};
// Current database settings
pub const database_settings = DatabaseSettings{
// .name = [8]u8{ "\x7a", "\x65", "\x6e", "\x64", "\x79" },
.name = [8]u8{ 0x7a, 0x65, 0x6e, 0x64, 0x79, 0x20, 0x20, 0x20 },
.version = .{ .major = 0, .minor = 0, .patch = 1 },
.layout = .{ .major = 0, .minor = 0, .patch = 1 },
};
pub const DiskSettings = struct { metadata_block_size: u64, block_size: u64, block_hash_size: HashSize, block_hash_algorithm: HashAlgorithm };
pub const disk_settings = DiskSettings{
.metadata_block_size = 4096,
//.metadata_block_size = 512,
.block_size = 4096,
//.block_size = 512,
.block_hash_size = 2,
.block_hash_algorithm = HashAlgorithm.blake3,
};
pub const MemorySettings = struct {
memory_alignment: comptime_int,
};
pub const memory_settings = MemorySettings{
.memory_alignment = 512,
};
//pub const memory_settings
test "expected disk settings" {
// Now we test and support only 4KiB block size
try expect(disk_settings.block_size == 4096);
// Metadata block should be the same same as other blocks
try expect(disk_settings.metadata_block_size == disk_settings.block_size);
// By default we use 16-bit hash (2 bytes)
try expect(disk_settings.block_hash_size == 2);
}
const std = @import("std");
const Allocator = std.mem.Allocator;
const expect = std.testing.expect;
const types = @import("./Types.zig");
const Hash = types.Hash;
const HashSize = types.HashSize;
const HashAlgorithm = types.HashAlgorithm;
const DatabaseSettings = @import("./Info.zig").DatabaseSettings;
const disk_settings = @import("./Info.zig").disk_settings;
const database_settings = @import("./Info.zig").database_settings;
const BlockMapping = packed struct { block_id: u64, offset: u64 };
pub const MetaBlockCreateError = error{
HashAlgorithmNotSupported,
};
const HashInfo = packed struct { block_hash_algorithm: HashAlgorithm, block_hash_size: HashSize };
// const unused_bytes_size = disk_settings.metadata_block_size - @sizeOf(MetadataBlockInfo);
//const UnusedBytes = [unused_bytes_size]u8;
pub const MetaBlock = packed struct {
const Self = @This();
hash_info: HashInfo,
hash: Hash = undefined,
info: DatabaseSettings,
block_size: u64,
block_count: u64,
first_free_block_id: u64,
/// Create metadata block, but without initializing .hash field,
/// .hash field is uninitialized (!).
pub fn createNoHash() Self {
// Allocate meta block of metadata_block_size size (in bytes).
// var memory_block = try allocator.alloc(u8, disk_settings.metadata_block_size);
// Initialize it all to zeroes.
// @memset(memory_block.ptr, 0, memory_block.len);
//const meta = std.mem.bytesAsValue(MetadataBlock, memory_block.ptr);
// const meta = std.mem.bytesToValue(MetadataBlock, memory_block.ptr);
// return meta;
//const meta_block = @ptrCast(*MetadataBlock, &memory_block);
//try allocator.allocAdvanced();
//const self = allocator.alloc();
//std.debug.print("enum value is: {}\n", .{disk_settings.block_hash_algorithm});
const hash_info = HashInfo{
.block_hash_algorithm = disk_settings.block_hash_algorithm,
.block_hash_size = disk_settings.block_hash_size,
};
// const settings = DatabaseSettings {
// .name = database_settings.name,
// .version = database_settings.version,
// .layout = database_settings.layout
// };
var s = Self{
.hash_info = hash_info,
.hash = 0xcafe,
// .hash = undefined,
.info = database_settings,
.block_size = disk_settings.block_size,
.block_count = 0,
.first_free_block_id = 0,
};
std.debug.print("createNoHash(): return is: {}\n", .{s});
return s;
//const unused = [_]u8{0} ** unused_bytes_size;
// return Self{ .info = info, .unused = unused };
// const info = Info{
// .db_name = db_name,
// .db_version = 1,
// .layout_version = 1,
// .page_size = 4096,
// .page_count = 1,
// .first_free_page_id = 1,
// };
// const zeroed_unused_bytes = [_]u8{0} ** unused_bytes_size;
// const header = Header{ .info = info, .unused = _s };
}
};
test "create default metadata block" {
try expect(database_settings.version.major == 0);
var source_meta_block = MetaBlock.createNoHash();
//const meta_block_major_version = source_meta_block.info.version.major;
try expect(source_meta_block.info.version.major == 0);
//try expect(meta_block_major_version == 0);
//std.debug.print("disk_settings: {}\n", .{disk_settings});
//const meta_block = MetaBlock.createNoHash();
//std.debug.print("metadata block: {}\n", .{meta_block});
// std.debug.print("metadata block: {}\n", .{ .val = meta_block.hash_info });
//try expect(meta_block.info.block_size == 4096);
}
// test "test size of info in header" {
// const allocator = std.testing.allocator;
// var alloc_db_name = try allocator.alloc(u8, 8);
// defer allocator.free(alloc_db_name);
// std.mem.copy(u8, alloc_db_name, "zendy");
// const _t = std.mem.toBytes("zendytdkdkdkddkdkdkdkdkddkdkdkd");
// const info = Info{
// .db_name = _t,
// .db_version = 1,
// .layout_version = 1,
// .page_size = 4096,
// .page_count = 1,
// .first_free_page_id = 1,
// };
// const size = @sizeOf(Info);
// const bit_size = @bitSizeOf(Info);
// var alloc_unused_bytes = try allocator.alloc(u8, unused_bytes_size);
// defer allocator.free(alloc_unused_bytes);
// // std.mem.toBytes(u8, alloc_db_name, "zendy");
// //const _s = std.mem.toBytes(alloc_unused_bytes);
// // std.mem.zeroInit(UnusedBytes, alloc_unused_bytes);
// //const header = Header{ .info = info, .unused = alloc_unused_bytes };
// const _s = [_]u8{0} ** unused_bytes_size;
// const header = Header{ .info = info, .unused = _s };
// const header_in_bytes = @sizeOf(Header);
// const header_in_bits = @bitSizeOf(Header);
// std.debug.print("size: {}, bit size: {}", .{ size, bit_size });
// std.debug.print("header size: {}, header bit size: {}", .{ header_in_bytes, header_in_bits });
// std.debug.print("{}", .{info});
// std.debug.print("{}", .{header});
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment