Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
dirty
zig-cache
zig-out
.zig-cache
boltdb.tmp
35 changes: 19 additions & 16 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,36 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

// Create module
_ = b.addModule("boltdb", .{
// Create module for the library with target and optimize options
const lib_module = b.addModule("boltdb", .{
.root_source_file = b.path("src/namespace.zig"),
.target = target,
.optimize = optimize,
});

const lib = b.addStaticLibrary(.{
// Create static library using the module
const lib = b.addLibrary(.{
.name = "boltdb-zig",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.linkage = .static,
.root_module = lib_module,
});
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);

const exe = b.addExecutable(.{
.name = "boltdb-zig",
// Create module for the executable with target and optimize options
const exe_module = b.addModule("boltdb-zig-exe", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

const exe = b.addExecutable(.{
.name = "boltdb-zig",
.root_module = exe_module,
});

// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
Expand Down Expand Up @@ -71,17 +76,15 @@ pub fn build(b: *std.Build) void {
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.name = "lib-tests",
.root_module = lib_module,
});

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.name = "exe-tests",
.root_module = exe_module,
});

const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
Expand Down
5 changes: 4 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.name = "boltdb-zig",
.name = .boltdb_zig,
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",
Expand All @@ -8,6 +8,9 @@
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",
.minimum_zig_version = "0.15.0",

.fingerprint = 0xf7570c8463436009,

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
Expand Down
43 changes: 26 additions & 17 deletions example/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,45 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const lib = b.addStaticLibrary(.{
.name = "example",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
// Get the boltdb-zig dependency
const boltdbDep = b.dependency("boltdb-zig", .{
.target = target,
.optimize = optimize,
});
const boltdbModule = boltdbDep.module("boltdb");

// Create module for the example with target and optimize options
const lib_module = b.addModule("example", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
lib_module.addImport("boltdb", boltdbModule);

// Create static library using the module
const lib = b.addLibrary(.{
.name = "example",
.linkage = .static,
.root_module = lib_module,
});
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
const boltdbDep = b.dependency("boltdb-zig", .{

// Create module for the executable with target and optimize options
const exe_module = b.addModule("example-exe", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe_module.addImport("boltdb", boltdbModule);

const exe = b.addExecutable(.{
.name = "example",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.root_module = exe_module,
});

exe.root_module.addImport("boltdb", boltdbDep.module("boltdb"));
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
Expand Down Expand Up @@ -72,20 +85,16 @@ pub fn build(b: *std.Build) void {
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.name = "lib-tests",
.root_module = lib_module,
});
lib_unit_tests.root_module.addImport("boltdb", boltdbDep.module("boltdb"));

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.name = "exe-tests",
.root_module = exe_module,
});
exe_unit_tests.root_module.addImport("boltdb", boltdbDep.module("boltdb"));

const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

Expand Down
7 changes: 4 additions & 3 deletions example/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = "example",
.name = .example,

// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",

.fingerprint = 0x6eec9b9f51bef188,

// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
Expand All @@ -24,8 +26,7 @@
// internet connectivity.
.dependencies = .{
.@"boltdb-zig" = .{
.url = "git+https://github.com/laohanlinux/boltdb-zig.git/?ref=align#3ec605eca6a9f85959f81900542fea59cc3eaef6",
.hash = "122056bd20aba7365380a500c315f4a03aa5ea2134685b11fa7e32e312344c28175c",
.path = "..",
},
},
.paths = .{
Expand Down
4 changes: 2 additions & 2 deletions src/bucket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ pub const Bucket = struct {
var itr = self.buckets.?.iterator();
var arenaAllocator = std.heap.ArenaAllocator.init(self.getAllocator());
defer arenaAllocator.deinit();
var valueBytes = std.ArrayList(u8).init(arenaAllocator.allocator());
var valueBytes = std.array_list.Managed(u8).init(arenaAllocator.allocator());
while (itr.next()) |entry| {
// std.log.info("\t\tRun at bucket({s}) spill!\t\t", .{entry.key_ptr.*});
// If the child bucket is small enough and it has no child buckets then
Expand Down Expand Up @@ -729,7 +729,7 @@ pub const Bucket = struct {
// Update parent node.
var c = self.cursor();
const keyPairRef = c._seek(entry.key_ptr.*);
assert(std.mem.eql(u8, entry.key_ptr.*, keyPairRef.key.?), "misplaced bucket header: {s} -> {s}", .{ std.fmt.fmtSliceHexLower(entry.key_ptr.*), std.fmt.fmtSliceHexLower(keyPairRef.key.?) });
assert(std.mem.eql(u8, entry.key_ptr.*, keyPairRef.key.?), "misplaced bucket header: {s} -> {s}", .{ entry.key_ptr.*, keyPairRef.key.? });
assert(keyPairRef.flag & consts.BucketLeafFlag != 0, "unexpeced bucket header flag: 0x{x}", .{keyPairRef.flag});
const keyNode = c.node().?;
// TODO if the newKey == oldKey, then no need to dupe the key.
Expand Down
2 changes: 1 addition & 1 deletion src/consts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub const PgidType = u64;
/// A slice of page ids.
pub const PgIds = []PgidType;
/// The size of a page.
pub const PageSize: usize = std.mem.page_size;
pub const PageSize: usize = std.heap.page_size_min;
// pub const PageSize: usize = 4096;

/// Represents the options that can be set when opening a database.
Expand Down
4 changes: 2 additions & 2 deletions src/cursor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Error = @import("error.zig").Error;

pub const Cursor = struct {
_bucket: *Bucket,
stack: std.ArrayList(ElementRef),
stack: std.array_list.Managed(ElementRef),

allocator: std.mem.Allocator,
arenaAllocator: ?std.heap.ArenaAllocator,
Expand All @@ -28,7 +28,7 @@ pub const Cursor = struct {
const allocator = _bt.getAllocator();
return Cursor{
._bucket = _bt,
.stack = std.ArrayList(ElementRef).init(allocator),
.stack = std.array_list.Managed(ElementRef).init(allocator),
.allocator = allocator,
.arenaAllocator = null,
};
Expand Down
12 changes: 6 additions & 6 deletions src/db.zig
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub const DB = struct {
datasz: usize,

rwtx: ?*tx.TX = null,
txs: std.ArrayList(*tx.TX),
txs: std.array_list.Managed(*tx.TX),
// the freelist is used to manage the *dirty* pages in the transaction, it only changes at writable transaction, but has only writable transaction once.
// So we don't need to lock the freelist and it is safe by rwlock.
freelist: *freelist.FreeList,
Expand Down Expand Up @@ -130,7 +130,7 @@ pub const DB = struct {

/// Returns the string representation of the database.
pub fn string(self: *const Self, _allocator: std.mem.Allocator) []u8 {
var buf = std.ArrayList(u8).init(_allocator);
var buf = std.array_list.Managed(u8).init(_allocator);
defer buf.deinit();
const writer = buf.writer();
writer.print("meta0: {}\n", .{self.meta0.*}) catch unreachable;
Expand All @@ -144,7 +144,7 @@ pub const DB = struct {

/// Returns db pretty format string.
pub fn pageString(self: *const Self, _allocator: std.mem.Allocator) []u8 {
var buf = std.ArrayList(u8).init(_allocator);
var buf = std.array_list.Managed(u8).init(_allocator);
defer buf.deinit();
const writer = buf.writer();
writer.print("meta0:{}\n", .{self.pageById(0).*}) catch unreachable;
Expand Down Expand Up @@ -179,7 +179,7 @@ pub const DB = struct {
if (db.pageSize == 0) {
db.pageSize = consts.PageSize;
}
db.txs = std.ArrayList(*TX).init(allocator);
db.txs = std.array_list.Managed(*tx.TX).initCapacity(allocator, 0) catch unreachable;
db.stats = Stats{};
db.readOnly = options.readOnly;
db.strictMode = options.strictMode;
Expand Down Expand Up @@ -333,9 +333,9 @@ pub const DB = struct {
// log.err("mmap minsz: {}", .{minsz});
self.mmaplock.lock();
defer self.mmaplock.unlock();
const fileInfo = try self.file.metadata();
const fileInfo = try self.file.stat();
// ensure the size is at least the minmum size.
var size = @as(usize, fileInfo.size());
var size = fileInfo.size;
// TODO Delete it
// assert(size >= minsz, "the size of file is less than the minsz: {d}, file size: {d}", .{ minsz, size });
if (size < minsz) {
Expand Down
28 changes: 14 additions & 14 deletions src/freelist.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const log = std.log.scoped(.BoltFreeList);
// It also tracks pages that have been freed but are still in use by open transactions.
pub const FreeList = struct {
// all free and available free page ids.
ids: std.ArrayList(PgidType),
ids: std.array_list.Managed(PgidType),
// mapping of soon-to-be free page ids by tx.
pending: std.AutoHashMap(consts.TxId, std.ArrayList(PgidType)),
pending: std.AutoHashMap(consts.TxId, std.array_list.Managed(PgidType)),
// fast lookup of all free and pending pgae ids.
cache: std.AutoHashMap(PgidType, bool),

Expand All @@ -26,8 +26,8 @@ pub const FreeList = struct {
/// init freelist
pub fn init(allocator: std.mem.Allocator) *Self {
const f = allocator.create(Self) catch unreachable;
f.ids = std.ArrayList(PgidType).init(allocator);
f.pending = std.AutoHashMap(TxId, std.ArrayList(PgidType)).init(allocator);
f.ids = std.array_list.Managed(PgidType).init(allocator);
f.pending = std.AutoHashMap(TxId, std.array_list.Managed(PgidType)).init(allocator);
f.cache = std.AutoHashMap(PgidType, bool).init(allocator);
f.allocator = allocator;
return f;
Expand Down Expand Up @@ -81,7 +81,7 @@ pub const FreeList = struct {

/// Copies into dst a list of all free ids and all pending ids in one sorted list.
pub fn copyAll(self: *Self, dst: []PgidType) void {
var array = std.ArrayList(PgidType).initCapacity(self.allocator, self.pendingCount()) catch unreachable;
var array = std.array_list.Managed(PgidType).initCapacity(self.allocator, self.pendingCount()) catch unreachable;
defer array.deinit();
var itr = self.pending.valueIterator();
while (itr.next()) |entries| {
Expand Down Expand Up @@ -147,7 +147,7 @@ pub const FreeList = struct {
pub fn free(self: *Self, txid: TxId, p: *const Page) !void {
assert(p.id > 1, "can not free 0 or 1 page", .{});
// Free page and all its overflow pages.
const ids = try self.pending.getOrPutValue(txid, std.ArrayList(PgidType).init(self.allocator));
const ids = try self.pending.getOrPutValue(txid, std.array_list.Managed(PgidType).init(self.allocator));
for (p.id..(p.id + p.overflow + 1)) |id| {
// Add to the freelist and cache.
try self.cache.putNoClobber(id, true);
Expand All @@ -161,7 +161,7 @@ pub const FreeList = struct {
if (!@import("builtin").is_test) {
assert(self.pending.count() <= 1, "pending count should be less than 1", .{});
}
var arrayIDs = std.ArrayList(PgidType).init(self.allocator);
var arrayIDs = std.array_list.Managed(PgidType).init(self.allocator);
defer arrayIDs.deinit();
var itr = self.pending.iterator();
while (itr.next()) |entry| {
Expand All @@ -176,7 +176,7 @@ pub const FreeList = struct {
}
// Sort the array
std.mem.sort(PgidType, arrayIDs.items, {}, std.sort.asc(PgidType));
var array = try std.ArrayList(PgidType).initCapacity(self.allocator, arrayIDs.items.len + self.ids.items.len);
var array = try std.array_list.Managed(PgidType).initCapacity(self.allocator, arrayIDs.items.len + self.ids.items.len);
defer array.deinit();
try array.appendNTimes(0, arrayIDs.items.len + self.ids.items.len);
assert(array.items.len == (arrayIDs.items.len + self.ids.items.len), "array.items.len == (arrayIDs.items.len + self.ids.items.len)", .{});
Expand Down Expand Up @@ -273,7 +273,7 @@ pub const FreeList = struct {

// Check each page in the freelist and build a new available freelist.
// with any pages not in the pending lists.
var a = std.ArrayList(PgidType).init(self.allocator);
var a = std.array_list.Managed(PgidType).init(self.allocator);
defer a.deinit();
for (self.ids.items) |id| {
if (!pagaeCahe.contains(id)) {
Expand Down Expand Up @@ -331,7 +331,7 @@ pub const FreeList = struct {

/// Format freelist to string with _allocator.
pub fn string(self: *Self, _allocator: std.mem.Allocator) []u8 {
var buf = std.ArrayList(u8).init(_allocator);
var buf = std.array_list.Managed(u8).init(_allocator);
defer buf.deinit();
const writer = buf.writer();

Expand Down Expand Up @@ -444,9 +444,9 @@ pub const FreeList = struct {
// defer freelist.deinit();
// try freelist.ids.appendSlice(&.{ 12, 39 });

// var c100 = std.ArrayList(PgidType).init(std.testing.allocator);
// var c100 = std.array_list.Managed(PgidType).init(std.testing.allocator);
// c100.appendSlice(&.{ 28, 11 }) catch unreachable;
// var c101 = std.ArrayList(PgidType).init(std.testing.allocator);
// var c101 = std.array_list.Managed(PgidType).init(std.testing.allocator);
// c101.appendSlice(&.{3}) catch unreachable;
// try freelist.pending.put(100, c100);
// try freelist.pending.put(101, c101);
Expand Down Expand Up @@ -491,15 +491,15 @@ pub const FreeList = struct {
// // std.debug.print("{},", .{n});
// // }
// // std.debug.print("\n", .{});
// // var arr = try std.ArrayList(page.PgidType).initCapacity(std.heap.page_allocator, 100);
// // var arr = try std.array_list.Managed(page.PgidType).initCapacity(std.heap.page_allocator, 100);
// // defer arr.deinit();
// // }

// // test "freelist" {
// // var flist = FreeList.init(std.testing.allocator);
// // defer flist.deinit();

// // var ids = std.ArrayList(page.PgidType).initCapacity(std.testing.allocator, 0) catch unreachable;
// // var ids = std.array_list.Managed(page.PgidType).initCapacity(std.testing.allocator, 0) catch unreachable;
// // for (0..29) |i| {
// // const pid = @as(u64, i);
// // ids.append(pid) catch unreachable;
Expand Down
Loading
Loading