add Group Members Iterator

This commit is contained in:
Motiejus Jakštys 2023-06-06 19:05:14 +03:00
parent ef4062edb7
commit 4962c7286b
2 changed files with 26 additions and 33 deletions

View File

@ -301,34 +301,34 @@ pub fn packCGroupNoMembers(group: *const PackedGroup, buf: []u8) error{BufferToo
// The pointer to `arr` gets changed on second iteration, and I don't know why. // The pointer to `arr` gets changed on second iteration, and I don't know why.
// Leaving this for better times. // Leaving this for better times.
//const GroupMembersIter = struct { const GroupMembersIter = struct {
// vit: compress.VarintSliceIterator, vit: compress.VarintSliceIterator,
// it: compress.DeltaDecompressionIterator, it: compress.DeltaDecompressionIterator,
// total: usize, total: usize,
// arr: []const u8, arr: []const u8,
//
// pub fn nextMust(self: *GroupMembersIter) ?u64 { pub fn nextMust(self: *GroupMembersIter) ?u64 {
// return self.it.nextMust(); return self.it.nextMust();
// } }
//}; };
//
//pub fn groupMembersIter(members_slice: []const u8) GroupMembersIter { pub fn groupMembersIter(members_slice: []const u8) GroupMembersIter {
// std.debug.print("members_slice.ptr={*}\n", .{members_slice.ptr}); var vit = compress.varintSliceIteratorMust(members_slice);
// var vit = compress.varintSliceIteratorMust(members_slice); var it = compress.deltaDecompressionIterator(&vit);
// var it = compress.deltaDecompressionIterator(&vit); return GroupMembersIter{
// return GroupMembersIter{ .arr = members_slice,
// .arr = members_slice, .vit = vit,
// .vit = vit, .it = it,
// .it = it, .total = vit.remaining,
// .total = vit.remaining, };
// }; }
//}
// dumps PackedGroup to []u8 and returns a CGroup. // dumps PackedGroup to []u8 and returns a CGroup.
pub fn packCGroup(self: *const DB, group: *const PackedGroup, buf: []u8) error{BufferTooSmall}!CGroup { pub fn packCGroup(self: *const DB, group: *const PackedGroup, buf: []u8) error{BufferTooSmall}!CGroup {
const members_slice = self.groupmembers[group.members_offset..]; const members_slice = self.groupmembers[group.members_offset..];
var vit = compress.varintSliceIteratorMust(members_slice); var it = groupMembersIter(members_slice);
const num_members = vit.remaining;
const num_members = it.total;
const ptr_end = @sizeOf(?[*:0]const u8) * (num_members + 1); const ptr_end = @sizeOf(?[*:0]const u8) * (num_members + 1);
if (ptr_end > buf.len) if (ptr_end > buf.len)
@ -338,8 +338,6 @@ pub fn packCGroup(self: *const DB, group: *const PackedGroup, buf: []u8) error{B
member_ptrs[member_ptrs.len - 1] = null; member_ptrs[member_ptrs.len - 1] = null;
var buf_offset: usize = ptr_end; var buf_offset: usize = ptr_end;
var it = compress.deltaDecompressionIterator(&vit);
var i: usize = 0; var i: usize = 0;
while (it.nextMust()) |member_offset| : (i += 1) { while (it.nextMust()) |member_offset| : (i += 1) {
const entry = PackedUser.fromBytes(@alignCast(8, self.users[member_offset << 3 ..])); const entry = PackedUser.fromBytes(@alignCast(8, self.users[member_offset << 3 ..]));

View File

@ -171,13 +171,8 @@ fn printGroup(stdout: anytype, db: *const DB, g: *const PackedGroup) ?u8 {
// not converting to Group to save a few memory allocations. // not converting to Group to save a few memory allocations.
stdout.print("{s}:x:{d}:", .{ g.name(), g.gid() }) catch return 3; stdout.print("{s}:x:{d}:", .{ g.name(), g.gid() }) catch return 3;
// TODO: move member iteration from here and DB.packCGroup var it = DB.groupMembersIter(db.groupmembers[g.members_offset..]);
// to a common place. // lines will be buffered, flushed on every EOL.
const members_slice = db.groupmembers[g.members_offset..];
var vit = compress.varintSliceIteratorMust(members_slice);
var it = compress.deltaDecompressionIterator(&vit);
// lines will be buffered, but flushed on every EOL.
var line_writer = io.bufferedWriter(stdout); var line_writer = io.bufferedWriter(stdout);
var i: usize = 0; var i: usize = 0;
while (it.nextMust()) |member_offset| : (i += 1) { while (it.nextMust()) |member_offset| : (i += 1) {