Mercurial > gemma
changeset 4128:14706384a464 request_hist_bns
Cleanup by Sascha L. Teichmann applied.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Thu, 01 Aug 2019 17:27:40 +0200 |
parents | 8c62809ea87e |
children | 24c5eec22c2d |
files | pkg/imports/bn.go |
diffstat | 1 files changed, 74 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/imports/bn.go Thu Aug 01 17:07:01 2019 +0200 +++ b/pkg/imports/bn.go Thu Aug 01 17:27:40 2019 +0200 @@ -242,6 +242,48 @@ return storeBottlenecks(ctx, fetch, importID, conn, feedback, bn.Tolerance) } +type bnStmts struct { + insert *sql.Stmt + findExactMatch *sql.Stmt + findIntersecting *sql.Stmt + insertMaterial *sql.Stmt + track *sql.Stmt +} + +func (bs *bnStmts) close() { + for _, s := range []**sql.Stmt{ + &bs.insert, + &bs.findExactMatch, + &bs.findIntersecting, + &bs.insertMaterial, + &bs.track, + } { + if *s != nil { + (*s).Close() + *s = nil + } + } +} + +func (bs *bnStmts) prepare(ctx context.Context, conn *sql.Conn) error { + for _, x := range []struct { + sql string + stmt **sql.Stmt + }{ + {insertBottleneckSQL, &bs.insert}, + {findExactMatchBottleneckSQL, &bs.findExactMatch}, + {findIntersectingBottleneckSQL, &bs.findIntersecting}, + {insertBottleneckMaterialSQL, &bs.insertMaterial}, + {trackImportDeletionSQL, &bs.track}, + } { + var err error + if *x.stmt, err = conn.PrepareContext(ctx, x.sql); err != nil { + return err + } + } + return nil +} + func storeBottlenecks( ctx context.Context, fetch func() ([]*ifbn.BottleNeckType, error), @@ -259,24 +301,11 @@ feedback.Info("Found %d bottlenecks for import", len(bns)) - var insertStmt, findExactMatchingBNStmt, findIntersectingBNStmt, - insertMaterialStmt, trackStmt *sql.Stmt + var bs bnStmts + defer bs.close() - for _, x := range []struct { - sql string - stmt **sql.Stmt - }{ - {insertBottleneckSQL, &insertStmt}, - {findExactMatchBottleneckSQL, &findExactMatchingBNStmt}, - {findIntersectingBottleneckSQL, &findIntersectingBNStmt}, - {insertBottleneckMaterialSQL, &insertMaterialStmt}, - {trackImportDeletionSQL, &trackStmt}, - } { - var err error - if *x.stmt, err = conn.PrepareContext(ctx, x.sql); err != nil { - return nil, err - } - defer (*x.stmt).Close() + if err := bs.prepare(ctx, conn); err != nil { + return nil, err } var nids []string @@ -293,12 +322,9 @@ for _, bn := range bns { if err := storeBottleneck( tx, ctx, importID, feedback, bn, - &nids, &seenOldBnIds, tolerance, - insertStmt, - findExactMatchingBNStmt, - findIntersectingBNStmt, - insertMaterialStmt, - trackStmt); err != nil { + &nids, seenOldBnIds, tolerance, + &bs, + ); err != nil { return nil, err } } @@ -327,12 +353,9 @@ feedback Feedback, bn *ifbn.BottleNeckType, nids *[]string, - seenOldBnIds *map[int64]bool, + seenOldBnIds map[int64]bool, tolerance float64, - insertStmt, - findExactMatchingBNStmt, findIntersectingBNStmt, - insertMaterialStmt, - trackStmt *sql.Stmt, + bs *bnStmts, ) error { feedback.Info("Processing %s (%s)", bn.OBJNAM, bn.Bottleneck_id) @@ -436,7 +459,8 @@ // Check if an bottleneck identical to the one we would insert already // exists: - bns, err := tx.StmtContext(ctx, findExactMatchingBNStmt).QueryContext( + var old int64 + err := tx.StmtContext(ctx, bs.findExactMatch).QueryRowContext( ctx, bn.Bottleneck_id, &validity, @@ -451,16 +475,17 @@ limiting, bn.Date_Info, bn.Source, - ) - if err != nil { + ).Scan(&old) + switch { + case err == sql.ErrNoRows: + // We dont have a matching old. + case err != nil: return err - } - defer bns.Close() - // We could check if the materials are also matching -- but per - // specification the Date_Info would hvae to change on that kind of - // change anyway. So actualy we ar alreayd checking more in dpth than - // required. - if bns.Next() { + default: + // We could check if the materials are also matching -- but per + // specification the Date_Info would hvae to change on that kind of + // change anyway. So actualy we ar alreayd checking more in dpth than + // required. feedback.Info("unchanged") return nil } @@ -472,7 +497,7 @@ // Check if the new bottleneck intersects with the validity of existing // for the same bottleneck_id we consider this an update and mark the // old data for deletion. - bns, err = tx.StmtContext(ctx, findIntersectingBNStmt).QueryContext( + bns, err := tx.StmtContext(ctx, bs.findIntersecting).QueryContext( ctx, bn.Bottleneck_id, &validity, ) if err != nil { @@ -492,6 +517,10 @@ oldBnIds = append(oldBnIds, oldID) } + if err := bns.Err(); err != nil { + return err + } + switch { case len(oldBnIds) == 1: feedback.Info("Bottelneck '%s' "+ @@ -510,13 +539,13 @@ // It is possible, that two new bottlenecks intersect with the // same old noe, therefor we have to handle duplicates in // oldBnIds. - if !(*seenOldBnIds)[oldID] { - if _, err := tx.StmtContext(ctx, trackStmt).ExecContext( + if !seenOldBnIds[oldID] { + if _, err := tx.StmtContext(ctx, bs.track).ExecContext( ctx, importID, "waterway.bottlenecks", oldID, true, ); err != nil { return err } - (*seenOldBnIds)[oldID] = true + seenOldBnIds[oldID] = true } } @@ -525,7 +554,7 @@ savepoint := Savepoint(ctx, tx, "insert_bottlenck") err = savepoint(func() error { - bns, err := tx.StmtContext(ctx, insertStmt).QueryContext(ctx, + bns, err := tx.StmtContext(ctx, bs.insert).QueryContext(ctx, bn.Bottleneck_id, &validity, bn.Fk_g_fid, @@ -565,7 +594,7 @@ pgMaterials.Set(materials) // Insert riverbed materials - if _, err := tx.StmtContext(ctx, insertMaterialStmt).ExecContext(ctx, + if _, err := tx.StmtContext(ctx, bs.insertMaterial).ExecContext(ctx, &pgBnIds, &pgMaterials, @@ -582,7 +611,7 @@ // Only add new BN data to tracking for staging review. for _, nid := range bnIds { - if _, err := tx.StmtContext(ctx, trackStmt).ExecContext( + if _, err := tx.StmtContext(ctx, bs.track).ExecContext( ctx, importID, "waterway.bottlenecks", nid, false, ); err != nil { return err