Skip to content

Commit 50dfabc

Browse files
committed
More work on attributes
1 parent 4efbe21 commit 50dfabc

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

build.zig.zon

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
.hash = "12201776681f1e5ec6df7df30786e90771c5de564c941a309c73c4299c7864ddb4c3",
1717
},
1818
.msgpack = .{
19-
.url = "git+https://github.com/lalinsky/msgpack.zig?ref=main#7c0a9846b33063199e56e50d683b4ca8785c773e",
20-
.hash = "12207a2d5cff5690049e70a0ce65c8a7b67bf385abc3acf86caa42db2a921c83a269",
19+
.url = "git+https://github.com/lalinsky/msgpack.zig?ref=v0.1.0#d141ef4e1f585fecbbcdac9a9f85e41b5759182c",
20+
.hash = "1220cb5fbd418638a830cb3c8a47d95d766d5ec1904631a14cde18cad89047165404",
2121
},
2222
},
2323
.paths = .{

src/filefmt.zig

+42-6
Original file line numberDiff line numberDiff line change
@@ -238,36 +238,57 @@ const segment_file_header_magic_v1: u32 = 0x53474D31; // "SGM1" in big endian
238238
const segment_file_footer_magic_v1: u32 = @byteSwap(segment_file_header_magic_v1);
239239

240240
pub const SegmentFileHeader = struct {
241-
magic: u32 = segment_file_header_magic_v1,
242-
block_size: u32,
241+
magic: u32,
243242
info: SegmentInfo,
243+
has_attributes: bool,
244+
has_docs: bool,
245+
block_size: u32,
244246

245247
pub fn msgpackFormat() msgpack.StructFormat {
246248
return .{
247249
.as_map = .{
248-
.key = .field_index,
250+
.key = .field_index, // FIXME
249251
.omit_defaults = false,
250252
.omit_nulls = true,
251253
},
252254
};
253255
}
256+
257+
pub fn msgpackFieldKey(field: std.meta.FieldEnum(@This())) u8 {
258+
return switch (field) {
259+
.magic => 0x00,
260+
.info => 0x01,
261+
.has_attributes => 0x02,
262+
.has_docs => 0x03,
263+
.block_size => 0x04,
264+
};
265+
}
254266
};
255267

256268
pub const SegmentFileFooter = struct {
257-
magic: u32 = segment_file_footer_magic_v1,
269+
magic: u32,
258270
num_items: u32,
259271
num_blocks: u32,
260272
checksum: u64,
261273

262274
pub fn msgpackFormat() msgpack.StructFormat {
263275
return .{
264276
.as_map = .{
265-
.key = .field_index,
277+
.key = .field_index, // FIXME
266278
.omit_defaults = false,
267279
.omit_nulls = true,
268280
},
269281
};
270282
}
283+
284+
pub fn msgpackFieldKey(field: std.meta.FieldEnum(@This())) u8 {
285+
return switch (field) {
286+
.magic => 0x00,
287+
.num_items => 0x01,
288+
.num_blocks => 0x02,
289+
.checksum => 0x03,
290+
};
291+
}
271292
};
272293

273294
pub fn deleteSegmentFile(dir: std.fs.Dir, info: SegmentInfo) !void {
@@ -299,11 +320,15 @@ pub fn writeSegmentFile(dir: std.fs.Dir, reader: anytype) !void {
299320
const packer = msgpack.packer(writer);
300321

301322
const header = SegmentFileHeader{
323+
.magic = segment_file_header_magic_v1,
302324
.block_size = block_size,
303325
.info = segment.info,
326+
.has_attributes = true,
327+
.has_docs = true,
304328
};
305329
try packer.write(SegmentFileHeader, header);
306330

331+
try packer.writeMap(segment.attributes);
307332
try packer.writeMap(segment.docs);
308333

309334
try buffered_writer.flush();
@@ -393,7 +418,18 @@ pub fn readSegmentFile(dir: fs.Dir, info: SegmentInfo, segment: *FileSegment) !v
393418
segment.info = header.info;
394419
segment.block_size = header.block_size;
395420

396-
try unpacker.readMapInto(&segment.docs);
421+
if (header.has_attributes) {
422+
// FIXME nicer api in msgpack.zig
423+
var attributes = std.AutoHashMap(u64, u64).init(segment.allocator);
424+
defer attributes.deinit();
425+
try unpacker.readMapInto(&attributes);
426+
segment.attributes.deinit(segment.allocator);
427+
segment.attributes = attributes.unmanaged.move();
428+
}
429+
430+
if (header.has_docs) {
431+
try unpacker.readMapInto(&segment.docs);
432+
}
397433

398434
const block_size = header.block_size;
399435
const padding_size = block_size - fixed_buffer_stream.pos % block_size;

src/utils/shared_ptr.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn RefCounter(comptime T: type) type {
1010

1111
pub fn init() Self {
1212
return .{
13-
.refs = std.atomic.Value(u32).init(1),
13+
.refs = std.atomic.Value(T).init(1),
1414
};
1515
}
1616

0 commit comments

Comments
 (0)