1Import tạo ctx bằng WithCancel rồi gọi lines := Source(ctx, r). Goroutine Source dùng bufio.NewScanner đọc từng dòng và gửi out <- sc.Text(), thay vì nạp cả file hai triệu dòng vào RAM.Import creates ctx with WithCancel and calls lines := Source(ctx, r). The Source goroutine reads line by line with bufio.NewScanner and sends out <- sc.Text() instead of loading all two million rows into RAM.
2txns := Stage(ctx, lines, parse): goroutine Stage nhận dòng, parse tách 3 trường và ParseInt thành Txn{Ref: FT2401, Amount: 120, Currency: USD}. Dòng sai định dạng trả ok=false và bị bỏ qua bằng continue.txns := Stage(ctx, lines, parse): the Stage goroutine receives the line, parse splits 3 fields and ParseInt builds Txn{Ref: FT2401, Amount: 120, Currency: USD}. A malformed line returns ok=false and is skipped with continue.
3priced := Stage(ctx, txns, fn): công đoạn quy đổi lấy rates["USD"] = 25400 và gán t.VND = 120 * 25400. Tiền tệ không có trong rates trả ok=false nên bị lọc. Trong lúc đó Source và parse đã xử lý các dòng kế tiếp.priced := Stage(ctx, txns, fn): the conversion stage looks up rates["USD"] = 25400 and sets t.VND = 120 * 25400. A currency missing from rates returns ok=false and is filtered out. Meanwhile Source and parse already work on the next lines.
4Sink là vòng for t := range priced trong Import: gom vào batch cap 500, đủ thì gọi repo.SaveAll(ctx, batch). Khi SaveAll đang ghi, các channel không buffer khiến từng công đoạn chờ ở lệnh gửi: backpressure lan ngược về Source.The sink is the for t := range priced loop in Import: it fills a batch of cap 500 and then calls repo.SaveAll(ctx, batch). While SaveAll writes, the unbuffered channels make each stage wait on its send: backpressure flows back to Source.
5Hết file: Source defer close(out), vòng range của Stage(parse) kết thúc và đóng txns, rồi Stage(rates) đóng priced; range trong Import dừng và SaveAll lô cuối nhỏ hơn 500. Nếu SaveAll lỗi, Import return err và defer cancel() làm các goroutine phía trên thoát qua <-ctx.Done().EOF: Source does defer close(out), the range in Stage(parse) ends and closes txns, then Stage(rates) closes priced; the range in Import stops and SaveAll stores the final batch under 500. If SaveAll fails, Import returns err and defer cancel() makes the upstream goroutines exit via <-ctx.Done().
6Java StatementImporter.importFile dựng cùng dây chuyền bằng Stream lười: Files.lines(file) → map(Txn::parse).flatMap(Optional::stream) → filter(fx.supports(...)) → map(t.withVnd(fx.rate(...))) → forEach(repo::save). Mỗi dòng đi hết ống rồi mới đọc dòng sau.Java StatementImporter.importFile builds the same line with a lazy Stream: Files.lines(file) → map(Txn::parse).flatMap(Optional::stream) → filter(fx.supports(...)) → map(t.withVnd(fx.rate(...))) → forEach(repo::save). Each line goes through the whole pipe before the next is read.
7ReactiveImporter dùng Reactor: Flux.using(Files.lines) → mapNotNull(Txn.parse) → flatMap(fx.rate(..).map(t::withVnd), 8) gọi FxClient tối đa 8 lời song song → buffer(500) → concatMap(repo::saveAll) → count(). Backpressure đi qua request(n) thay vì channel.ReactiveImporter uses Reactor: Flux.using(Files.lines) → mapNotNull(Txn.parse) → flatMap(fx.rate(..).map(t::withVnd), 8) calls FxClient with at most 8 in flight → buffer(500) → concatMap(repo::saveAll) → count(). Backpressure travels through request(n) instead of channels.