add Group Members Iterator
This commit is contained in:
parent
ef4062edb7
commit
4962c7286b
50
src/DB.zig
50
src/DB.zig
@ -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.
|
||||
// Leaving this for better times.
|
||||
//const GroupMembersIter = struct {
|
||||
// vit: compress.VarintSliceIterator,
|
||||
// it: compress.DeltaDecompressionIterator,
|
||||
// total: usize,
|
||||
// arr: []const u8,
|
||||
//
|
||||
// pub fn nextMust(self: *GroupMembersIter) ?u64 {
|
||||
// return self.it.nextMust();
|
||||
// }
|
||||
//};
|
||||
//
|
||||
//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 it = compress.deltaDecompressionIterator(&vit);
|
||||
// return GroupMembersIter{
|
||||
// .arr = members_slice,
|
||||
// .vit = vit,
|
||||
// .it = it,
|
||||
// .total = vit.remaining,
|
||||
// };
|
||||
//}
|
||||
const GroupMembersIter = struct {
|
||||
vit: compress.VarintSliceIterator,
|
||||
it: compress.DeltaDecompressionIterator,
|
||||
total: usize,
|
||||
arr: []const u8,
|
||||
|
||||
pub fn nextMust(self: *GroupMembersIter) ?u64 {
|
||||
return self.it.nextMust();
|
||||
}
|
||||
};
|
||||
|
||||
pub fn groupMembersIter(members_slice: []const u8) GroupMembersIter {
|
||||
var vit = compress.varintSliceIteratorMust(members_slice);
|
||||
var it = compress.deltaDecompressionIterator(&vit);
|
||||
return GroupMembersIter{
|
||||
.arr = members_slice,
|
||||
.vit = vit,
|
||||
.it = it,
|
||||
.total = vit.remaining,
|
||||
};
|
||||
}
|
||||
|
||||
// dumps PackedGroup to []u8 and returns a CGroup.
|
||||
pub fn packCGroup(self: *const DB, group: *const PackedGroup, buf: []u8) error{BufferTooSmall}!CGroup {
|
||||
const members_slice = self.groupmembers[group.members_offset..];
|
||||
var vit = compress.varintSliceIteratorMust(members_slice);
|
||||
const num_members = vit.remaining;
|
||||
var it = groupMembersIter(members_slice);
|
||||
|
||||
const num_members = it.total;
|
||||
|
||||
const ptr_end = @sizeOf(?[*:0]const u8) * (num_members + 1);
|
||||
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;
|
||||
var buf_offset: usize = ptr_end;
|
||||
|
||||
var it = compress.deltaDecompressionIterator(&vit);
|
||||
|
||||
var i: usize = 0;
|
||||
while (it.nextMust()) |member_offset| : (i += 1) {
|
||||
const entry = PackedUser.fromBytes(@alignCast(8, self.users[member_offset << 3 ..]));
|
||||
|
@ -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.
|
||||
stdout.print("{s}:x:{d}:", .{ g.name(), g.gid() }) catch return 3;
|
||||
|
||||
// TODO: move member iteration from here and DB.packCGroup
|
||||
// to a common place.
|
||||
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 it = DB.groupMembersIter(db.groupmembers[g.members_offset..]);
|
||||
// lines will be buffered, flushed on every EOL.
|
||||
var line_writer = io.bufferedWriter(stdout);
|
||||
var i: usize = 0;
|
||||
while (it.nextMust()) |member_offset| : (i += 1) {
|
||||
|
Loading…
Reference in New Issue
Block a user