|
| 1 | +const std = @import("std"); |
| 2 | +const msgpack = @import("msgpack"); |
| 3 | + |
| 4 | +pub const SegmentInfo = struct { |
| 5 | + version: u64 = 0, |
| 6 | + merges: u64 = 0, |
| 7 | + |
| 8 | + pub fn contains(self: SegmentInfo, other: SegmentInfo) bool { |
| 9 | + const start = self.version; |
| 10 | + const end = self.version + self.merges; |
| 11 | + |
| 12 | + const other_start = other.version; |
| 13 | + const other_end = other.version + other.merges; |
| 14 | + |
| 15 | + return other_start >= start and other_end <= end; |
| 16 | + } |
| 17 | + |
| 18 | + pub fn merge(self: SegmentInfo, other: SegmentInfo) SegmentInfo { |
| 19 | + std.debug.assert(self.version + self.merges + 1 == other.version); |
| 20 | + return .{ |
| 21 | + .version = @min(self.version, other.version), |
| 22 | + .merges = self.merges + other.merges + 1, |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + pub fn getLastCommitId(self: SegmentInfo) u64 { |
| 27 | + return self.version + self.merges; |
| 28 | + } |
| 29 | + |
| 30 | + pub fn msgpackFormat() msgpack.StructFormat { |
| 31 | + return .{ .as_array = .{} }; |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +test "SegmentInfo.contains" { |
| 36 | + const a = SegmentInfo{ .version = 1, .merges = 0 }; |
| 37 | + const b = SegmentInfo{ .version = 2, .merges = 0 }; |
| 38 | + const c = SegmentInfo{ .version = 1, .merges = 1 }; |
| 39 | + |
| 40 | + try std.testing.expect(a.contains(a)); |
| 41 | + try std.testing.expect(!a.contains(b)); |
| 42 | + try std.testing.expect(!a.contains(c)); |
| 43 | + |
| 44 | + try std.testing.expect(!b.contains(a)); |
| 45 | + try std.testing.expect(b.contains(b)); |
| 46 | + try std.testing.expect(!b.contains(c)); |
| 47 | + |
| 48 | + try std.testing.expect(c.contains(a)); |
| 49 | + try std.testing.expect(c.contains(b)); |
| 50 | + try std.testing.expect(c.contains(c)); |
| 51 | +} |
0 commit comments