# HG changeset patch # User Sascha L. Teichmann # Date 1536594536 -7200 # Node ID d127856e689d1d0ba0f64bac63f8ccb5826bbd56 # Parent effd22c0ae5acde85a0854e29c04bde9f9c8b6a0 Sounding results: Sort the data sets by name and date to make the output deterministic. diff -r effd22c0ae5a -r d127856e689d cmd/soundingresults/main.go --- a/cmd/soundingresults/main.go Mon Sep 10 17:32:51 2018 +0200 +++ b/cmd/soundingresults/main.go Mon Sep 10 17:48:56 2018 +0200 @@ -11,6 +11,7 @@ "os" "path/filepath" "runtime" + "sort" "strings" "sync" "time" @@ -194,29 +195,40 @@ flush := func() {} if *dump { - out := bufio.NewWriter(os.Stdout) - fmt.Fprintln(out, "BEGIN;") - flush = func() { - fmt.Fprintln(out, "COMMIT;") - out.Flush() - } + var results []result handler = func(r result) { if r.m.depthReference != "ADR" { return } - fmt.Fprintln(out, "INSERT INTO waterway.sounding_results (") - fmt.Fprintln(out, " bottleneck_id,") - fmt.Fprintln(out, " date_info,") - fmt.Fprintln(out, " depth_reference,") - fmt.Fprintln(out, " point_cloud") - fmt.Fprintln(out, ") VALUES (") - fmt.Fprintf(out, " %s,\n", quote(r.m.name)) - fmt.Fprintf(out, " '%s'::date,\n", r.m.date.Format("2006-01-02")) - fmt.Fprintf(out, " %s,\n", quote(r.m.depthReference)) - fmt.Fprintf(out, " '") - io.Copy(hex.NewEncoder(out), strings.NewReader(r.wkb)) - fmt.Fprintln(out, "'") - fmt.Fprintln(out, ");") + results = append(results, r) + } + flush = func() { + sort.Slice(results, func(i, j int) bool { + if a, b := results[i].m.name, results[j].m.name; a != b { + return a < b + } + return results[i].m.date.Before(results[j].m.date) + }) + out := bufio.NewWriter(os.Stdout) + fmt.Fprintln(out, "BEGIN;") + for i := range results { + r := &results[i] + fmt.Fprintln(out, "INSERT INTO waterway.sounding_results (") + fmt.Fprintln(out, " bottleneck_id,") + fmt.Fprintln(out, " date_info,") + fmt.Fprintln(out, " depth_reference,") + fmt.Fprintln(out, " point_cloud") + fmt.Fprintln(out, ") VALUES (") + fmt.Fprintf(out, " %s,\n", quote(r.m.name)) + fmt.Fprintf(out, " '%s'::date,\n", r.m.date.Format("2006-01-02")) + fmt.Fprintf(out, " %s,\n", quote(r.m.depthReference)) + fmt.Fprintf(out, " '") + io.Copy(hex.NewEncoder(out), strings.NewReader(r.wkb)) + fmt.Fprintln(out, "'") + fmt.Fprintln(out, ");") + } + fmt.Fprintln(out, "COMMIT;") + out.Flush() } } else { // TODO: Implement database stuff. @@ -225,12 +237,10 @@ fin := make(chan struct{}) go func() { - defer flush() - defer close(fin) + defer func() { flush(); close(fin) }() for { select { case <-done: - log.Println("xxx") return case r := <-results: handler(r)