Mercurial > gemma
changeset 2580:5125db802b79
Morphological differences: Check if we already have a diff before we start to calculate it.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 11 Mar 2019 15:58:26 +0100 |
parents | 5295a182b4a4 |
children | 5466800da970 |
files | pkg/controllers/diff.go |
diffstat | 1 files changed, 45 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/diff.go Mon Mar 11 15:53:31 2019 +0100 +++ b/pkg/controllers/diff.go Mon Mar 11 15:58:26 2019 +0100 @@ -28,6 +28,20 @@ "gemma.intevation.de/gemma/pkg/octree" ) +const ( + diffIDSQL = ` +SELECT sd.id FROM + caching.sounding_differences sd JOIN + waterway.sounding_results srm ON sd.minuend = srm.id JOIN + waterway.sounding_results srs ON sd.subtrahend = srs.id JOIN + waterway.bottlenecks bns ON srm.bottleneck_id = bns.id +WHERE srm.bottleneck_id = srs.bottleneck_id AND + bns.objnam = $1 AND + srs.date_info = $2::date AND + srm.date_info = $3::date AND +` +) + // Only allow three diffence calculation at once. // TODO: Make this configurable? var diffCalculationSemaphore = semaphore.NewWeighted(int64(3)) @@ -40,16 +54,38 @@ start := time.Now() + dci := input.(models.DiffCalculationInput) + ctx := req.Context() + var id int64 + err = conn.QueryRowContext( + ctx, + diffIDSQL, + dci.Bottleneck, + dci.Minuend.Time, + dci.Subtrahend.Time, + ).Scan(&id) + + switch { + case err == sql.ErrNoRows: + // We need to calculate it. + case err != nil: + return + default: + // We already have this diff + jr = JSONResult{ + Result: map[string]int64{"id": id}, + } + return + } + // DoS counter measure. if err = diffCalculationSemaphore.Acquire(ctx, 1); err != nil { return } defer diffCalculationSemaphore.Release(1) - dci := input.(models.DiffCalculationInput) - minuendTree, err := octree.FromCache( ctx, conn, dci.Bottleneck, dci.Minuend.Time) @@ -176,6 +212,13 @@ heights := octree.SampleDiffHeights(tree.Min.Z, tree.Max.Z, 0.1) log.Printf("info: num heights: %d\n", len(heights)) + // XXX: Maybe we should start this transaction earlier!? + var tx *sql.Tx + if tx, err = conn.BeginTx(ctx, nil); err != nil { + return + } + defer tx.Rollback() + // TODO: Implement me! return