| ... | ... | @@ -0,0 +1,240 @@ |
| 1 | const MultiReader = @This(); |
| 2 | |
| 3 | const std = @import("../../std.zig"); |
| 4 | const Io = std.Io; |
| 5 | const File = Io.File; |
| 6 | const Allocator = std.mem.Allocator; |
| 7 | const assert = std.debug.assert; |
| 8 | |
| 9 | gpa: Allocator, |
| 10 | streams: *Streams, |
| 11 | batch: Io.Batch, |
| 12 | |
| 13 | pub const Context = struct { |
| 14 | mr: *MultiReader, |
| 15 | fr: File.Reader, |
| 16 | vec: [1][]u8, |
| 17 | err: ?Error, |
| 18 | eos: bool, |
| 19 | }; |
| 20 | |
| 21 | pub const Error = Allocator.Error || File.Reader.Error || Io.ConcurrentError; |
| 22 | |
| 23 | /// Trailing: |
| 24 | /// * `contexts: [len]Context` |
| 25 | /// * `ring: [len]u32` |
| 26 | /// * `operations: [len]Io.Operation` |
| 27 | pub const Streams = extern struct { |
| 28 | len: u32, |
| 29 | |
| 30 | pub fn contexts(s: *Streams) []Context { |
| 31 | _ = s; |
| 32 | @panic("TODO"); |
| 33 | } |
| 34 | |
| 35 | pub fn ring(s: *Streams) []u32 { |
| 36 | _ = s; |
| 37 | @panic("TODO"); |
| 38 | } |
| 39 | |
| 40 | pub fn operations(s: *Streams) []Io.Operation { |
| 41 | _ = s; |
| 42 | @panic("TODO"); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | pub fn Buffer(comptime n: usize) type { |
| 47 | return extern struct { |
| 48 | len: u32, |
| 49 | contexts: [n][@sizeOf(Context)]u8 align(@alignOf(Context)), |
| 50 | ring: [n]u32, |
| 51 | operations: [n][@sizeOf(Io.Operation)]u8 align(@alignOf(Io.Operation)), |
| 52 | |
| 53 | pub fn toStreams(b: *@This()) *Streams { |
| 54 | return @ptrCast(b); |
| 55 | } |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | /// See `Streams.Buffer` for convenience API to obtain the `streams` parameter. |
| 60 | pub fn init(mr: *MultiReader, gpa: Allocator, io: Io, streams: *Streams, files: []const File) void { |
| 61 | const contexts = streams.contexts(); |
| 62 | for (contexts, files) |*context, file| context.* = .{ |
| 63 | .mr = mr, |
| 64 | .fr = .{ |
| 65 | .io = io, |
| 66 | .file = file, |
| 67 | .mode = .streaming, |
| 68 | .interface = .{ |
| 69 | .vtable = &.{ |
| 70 | .stream = stream, |
| 71 | .discard = discard, |
| 72 | .readVec = readVec, |
| 73 | .rebase = rebase, |
| 74 | }, |
| 75 | .buffer = &.{}, |
| 76 | .seek = 0, |
| 77 | .end = 0, |
| 78 | }, |
| 79 | }, |
| 80 | .vec = .{&.{}}, |
| 81 | .err = null, |
| 82 | .eos = false, |
| 83 | }; |
| 84 | const operations = streams.operations(); |
| 85 | const ring = streams.ring(); |
| 86 | mr.* = .{ |
| 87 | .gpa = gpa, |
| 88 | .streams = streams, |
| 89 | .batch = .init(operations, ring), |
| 90 | }; |
| 91 | for (operations, contexts, files, 0..) |*op, *context, file, i| { |
| 92 | const r = &context.fr.interface; |
| 93 | op.* = .{ .file_read_streaming = .{ |
| 94 | .file = file, |
| 95 | .data = &context.vec, |
| 96 | } }; |
| 97 | rebaseGrowing(mr, context, 1) catch |err| { |
| 98 | context.err = err; |
| 99 | continue; |
| 100 | }; |
| 101 | context.vec[0] = r.buffer; |
| 102 | mr.batch.add(i); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pub fn deinit(mr: *MultiReader) void { |
| 107 | const gpa = mr.gpa; |
| 108 | const contexts = mr.streams.contexts(); |
| 109 | const io = contexts[0].fr.io; |
| 110 | mr.batch.cancel(io); |
| 111 | for (contexts) |*context| { |
| 112 | gpa.free(context.fr.interface.buffer); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pub fn reader(mr: *MultiReader, index: usize) *Io.Reader { |
| 117 | return &mr.streams.contexts()[index].fr.interface; |
| 118 | } |
| 119 | |
| 120 | pub fn toOwnedSlice(mr: *MultiReader, index: usize) Allocator.Error![]u8 { |
| 121 | const gpa = mr.gpa; |
| 122 | const r: *Io.Reader = reader(mr, index); |
| 123 | if (r.seek == 0) { |
| 124 | const new = try gpa.realloc(r.buffer, r.end); |
| 125 | r.buffer = &.{}; |
| 126 | r.end = 0; |
| 127 | return new; |
| 128 | } |
| 129 | const new = try gpa.dupe(u8, r.buffered()); |
| 130 | gpa.free(r.buffer); |
| 131 | r.buffer = &.{}; |
| 132 | r.seek = 0; |
| 133 | r.end = 0; |
| 134 | return new; |
| 135 | } |
| 136 | |
| 137 | fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize { |
| 138 | _ = limit; |
| 139 | _ = w; |
| 140 | const fr: *File.Reader = @alignCast(@fieldParentPtr("interface", r)); |
| 141 | const context: *Context = @fieldParentPtr("fr", fr); |
| 142 | const mr = context.mr; |
| 143 | return fill(mr, context); |
| 144 | } |
| 145 | |
| 146 | fn discard(r: *Io.Reader, limit: Io.Limit) Io.Reader.Error!usize { |
| 147 | _ = limit; |
| 148 | const fr: *File.Reader = @alignCast(@fieldParentPtr("interface", r)); |
| 149 | const context: *Context = @fieldParentPtr("fr", fr); |
| 150 | const mr = context.mr; |
| 151 | return fill(mr, context); |
| 152 | } |
| 153 | |
| 154 | fn readVec(r: *Io.Reader, data: [][]u8) Io.Reader.Error!usize { |
| 155 | _ = data; |
| 156 | const fr: *File.Reader = @alignCast(@fieldParentPtr("interface", r)); |
| 157 | const context: *Context = @fieldParentPtr("fr", fr); |
| 158 | const mr = context.mr; |
| 159 | return fill(mr, context); |
| 160 | } |
| 161 | |
| 162 | fn rebase(r: *Io.Reader, capacity: usize) Io.Reader.RebaseError!void { |
| 163 | const fr: *File.Reader = @alignCast(@fieldParentPtr("interface", r)); |
| 164 | const context: *Context = @fieldParentPtr("fr", fr); |
| 165 | const mr = context.mr; |
| 166 | |
| 167 | return rebaseGrowing(mr, context, capacity) catch |err| { |
| 168 | context.err = err; |
| 169 | return error.ReadFailed; |
| 170 | }; |
| 171 | } |
| 172 | |
| 173 | fn rebaseGrowing(mr: *MultiReader, context: *Context, capacity: usize) Allocator.Error!void { |
| 174 | const gpa = mr.gpa; |
| 175 | const r = &context.fr.interface; |
| 176 | if (r.buffer.len >= capacity) { |
| 177 | const data = r.buffer[r.seek..r.end]; |
| 178 | @memmove(r.buffer[0..data.len], data); |
| 179 | r.seek = 0; |
| 180 | r.end = data.len; |
| 181 | } else { |
| 182 | const adjusted_capacity = std.ArrayList(u8).growCapacity(capacity); |
| 183 | |
| 184 | if (r.seek == 0) { |
| 185 | if (gpa.remap(r.buffer, adjusted_capacity)) |new_memory| { |
| 186 | r.buffer = new_memory; |
| 187 | return; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | const data = r.buffer[r.seek..r.end]; |
| 192 | const new = try gpa.alloc(u8, adjusted_capacity); |
| 193 | @memcpy(new[0..data.len], data); |
| 194 | r.seek = 0; |
| 195 | r.end = data.len; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | fn fill(mr: *MultiReader, original_context: *Context) Io.Reader.Error!usize { |
| 200 | const contexts = mr.streams.contexts(); |
| 201 | const operations = mr.streams.operations(); |
| 202 | const io = contexts[0].fr.io; |
| 203 | |
| 204 | mr.batch.wait(io, .none) catch |err| switch (err) { |
| 205 | error.Timeout, error.UnsupportedClock => unreachable, |
| 206 | else => |e| { |
| 207 | original_context.err = e; |
| 208 | return error.ReadFailed; |
| 209 | }, |
| 210 | }; |
| 211 | |
| 212 | while (mr.batch.next()) |i| { |
| 213 | const context = &contexts[i]; |
| 214 | const operation = &operations[i]; |
| 215 | const n = operation.file_read_streaming.status.result catch |err| { |
| 216 | context.err = err; |
| 217 | continue; |
| 218 | }; |
| 219 | if (n == 0) { |
| 220 | context.eos = true; |
| 221 | continue; |
| 222 | } |
| 223 | const r = &context.fr.interface; |
| 224 | r.end += n; |
| 225 | if (r.buffer.len - r.end == 0) { |
| 226 | rebaseGrowing(mr, context, r.bufferedLen() + 1) catch |err| { |
| 227 | context.err = err; |
| 228 | continue; |
| 229 | }; |
| 230 | assert(r.seek == 0); |
| 231 | context.vec[0] = r.buffer; |
| 232 | } |
| 233 | operation.file_read_streaming.status = .{ .unstarted = {} }; |
| 234 | mr.batch.add(i); |
| 235 | } |
| 236 | |
| 237 | if (original_context.err != null) return error.ReadFailed; |
| 238 | if (original_context.eos) return error.EndOfStream; |
| 239 | return 0; |
| 240 | } |