Skip to content

Commit cdd3b20

Browse files
committed
Cleanup
1 parent 9053120 commit cdd3b20

File tree

2 files changed

+20
-64
lines changed

2 files changed

+20
-64
lines changed

src/Index2.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ fn loadSegments(self: *Self) !void {
266266
}
267267

268268
fn doCheckpoint(self: *Self) !bool {
269-
var segments = self.acquireSegments();
270-
defer self.releaseSegments(&segments);
269+
var snapshot = self.acquireSegments();
270+
defer self.releaseSegments(&snapshot);
271271

272-
const source = segments.memory_segments.value.getFirst() orelse return false;
272+
const source = snapshot.memory_segments.value.getFirst() orelse return false;
273273
if (source.value.getSize() < self.options.min_segment_size) {
274274
return false;
275275
}

src/segment_list2.zig

+17-61
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,14 @@ pub fn SegmentListManager(Segment: type) type {
189189
};
190190
}
191191

192-
pub fn deinit(self: *Self, allocator: Allocator) void {
193-
self.segments.release(allocator, .{allocator});
192+
pub fn deinit(self: *Self) void {
193+
releaseSegments(&self.segments);
194194
}
195195

196196
pub fn count(self: Self) usize {
197197
return self.segments.value.nodes.items.len;
198198
}
199199

200-
pub fn swap(self: *Self, segments_list: List) !void {
201-
var segments = try SharedPtr(List).create(self.allocator, segments_list);
202-
defer segments.release(self.allocator, .{self.allocator});
203-
204-
self.segments.swap(&segments);
205-
}
206-
207200
fn acquireSegments(self: Self, lock: *std.Thread.RwLock) SharedPtr(List) {
208201
lock.lockShared();
209202
defer lock.unlockShared();
@@ -282,7 +275,8 @@ pub fn SegmentListManager(Segment: type) type {
282275
var segments = try SharedPtr(List).create(self.allocator, List.initEmpty());
283276
errdefer self.releaseSegments(&segments);
284277

285-
try segments.value.nodes.ensureTotalCapacity(self.allocator, self.segments.value.nodes.items.len + 1);
278+
// allocate memory for one extra segment, if it's going to be unused, it's going to be unused, but we need to have it ready
279+
try segments.value.nodes.ensureTotalCapacity(self.allocator, self.count() + 1);
286280

287281
return .{
288282
.manager = self,
@@ -300,63 +294,25 @@ pub fn SegmentListManager(Segment: type) type {
300294
if (!update.committed) {
301295
self.update_lock.unlock();
302296
}
303-
update.segments.releaseWithCleanup(self.allocator, destroySegmentList, .{self});
304-
}
305-
306-
fn destroySegmentList(segments: *List, self: *Self) void {
307-
for (segments.nodes.items) |node| {
308-
node.value.cleanup();
309-
}
310-
segments.deinit(self.allocator);
297+
self.destroySegments(&update.segments);
311298
}
312-
};
313-
}
314-
315-
test "SegmentList" {
316-
const MockSegment = struct {
317-
pub const Options = struct {};
318299

319-
pub fn init(allocator: Allocator, options: Options) @This() {
320-
_ = allocator;
321-
_ = options;
322-
return .{};
300+
pub fn destroySegments(self: *Self, segments: *SharedPtr(List)) void {
301+
// we also call cleanup on these segments, to ensure that unused segments will get deleted from disk
302+
segments.releaseWithCleanup(self.allocator, destroySegmentList, .{self.allocator});
323303
}
324304

325-
pub fn deinit(self: *@This()) void {
326-
_ = self;
305+
fn destroySegmentList(segments: *List, allocator: Allocator) void {
306+
while (segments.nodes.items.len > 0) {
307+
var node = segments.nodes.pop();
308+
node.releaseWithCleanup(allocator, destroySegment, .{});
309+
}
310+
segments.deinit(allocator);
327311
}
328312

329-
pub fn search(self: @This(), hashes: []const u32, results: *SearchResults) !void {
330-
_ = self;
331-
_ = hashes;
332-
_ = results;
313+
fn destroySegment(segment: *Segment) void {
314+
segment.cleanup();
315+
segment.deinit();
333316
}
334317
};
335-
336-
const MockSegmentList = SegmentList(MockSegment);
337-
338-
const allocator = std.testing.allocator;
339-
340-
var segments1 = MockSegmentList.initEmpty();
341-
defer segments1.deinit(allocator);
342-
343-
var node = try MockSegmentList.createSegment(allocator, .{});
344-
defer MockSegmentList.destroySegment(allocator, &node);
345-
346-
var segments2 = try segments1.appendSegment(allocator, node);
347-
defer segments2.deinit(allocator);
348-
349-
var segments3 = try segments2.removeSegment(allocator, 0);
350-
defer segments3.deinit(allocator);
351-
352-
var segments4 = try segments2.replaceSegments(allocator, node, 0, 1);
353-
defer segments4.deinit(allocator);
354-
355-
var results = SearchResults.init(allocator);
356-
defer results.deinit();
357-
358-
try segments1.search(&[_]u32{ 1, 2, 3 }, &results, .{});
359-
try segments2.search(&[_]u32{ 1, 2, 3 }, &results, .{});
360-
try segments3.search(&[_]u32{ 1, 2, 3 }, &results, .{});
361-
try segments4.search(&[_]u32{ 1, 2, 3 }, &results, .{});
362318
}

0 commit comments

Comments
 (0)