Mercurial > gemma
annotate pkg/octree/cache.go @ 4488:bff6c5c1db4f
client: pdf-gen: improve adding bottleneck info to pdf
* Check if the bottleneck is in the current view to add its info to the exported pdf and the pdf filename, this avoid wrong filename and wrong info in pdf in case view has been changed to another location.
* Set the bottleneck to print after moving to it in map.
author | Fadi Abbud <fadi.abbud@intevation.de> |
---|---|
date | Fri, 27 Sep 2019 11:15:02 +0200 |
parents | 49564382ffff |
children | 4bbfe3dd2ab5 |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
727
diff
changeset
|
13 |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
14 package octree |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 "context" |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "database/sql" |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "sync" |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "time" |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 ) |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 type ( |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
24 cacheKey struct { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 date time.Time |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 bottleneck string |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
29 cacheEntry struct { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 checksum string |
727
41c8dc61f38f
Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
725
diff
changeset
|
31 tree *Tree |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 access time.Time |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 } |
1692
f4dcbe8941a1
Octree: Resolved the remaing golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1333
diff
changeset
|
34 |
f4dcbe8941a1
Octree: Resolved the remaing golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1333
diff
changeset
|
35 // Cache holds Octrees for a defined amount of time in memory |
f4dcbe8941a1
Octree: Resolved the remaing golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1333
diff
changeset
|
36 // before they are released. |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
37 Cache struct { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 sync.Mutex |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
39 entries map[cacheKey]*cacheEntry |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 ) |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 const ( |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
44 cleanupCacheSleep = 6 * time.Minute |
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
45 maxCacheAge = 5 * time.Minute |
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
46 maxCacheEntries = 4 |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 ) |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 const ( |
4214
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
50 directFetchOctreeSQL = ` |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
51 SELECT octree_index FROM waterway.sounding_results |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
52 WHERE id = $1 |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
53 ` |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 fetchOctreeSQL = ` |
1131
2e6b47cdb2ca
Store octrees along with sounding results
Tom Gottfried <tom@intevation.de>
parents:
1017
diff
changeset
|
55 SELECT octree_checksum, octree_index |
2e6b47cdb2ca
Store octrees along with sounding results
Tom Gottfried <tom@intevation.de>
parents:
1017
diff
changeset
|
56 FROM waterway.sounding_results |
2e6b47cdb2ca
Store octrees along with sounding results
Tom Gottfried <tom@intevation.de>
parents:
1017
diff
changeset
|
57 WHERE bottleneck_id = $1 AND date_info = $2::date |
1135
19a04b150b6c
Fixed SQL syntax errors introduced with 1153:2e6b47cdb2ca.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1131
diff
changeset
|
58 AND octree_checksum IS NOT NULL AND octree_index IS NOT NULL |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 ` |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 checkOctreeSQL = ` |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 SELECT CASE |
1131
2e6b47cdb2ca
Store octrees along with sounding results
Tom Gottfried <tom@intevation.de>
parents:
1017
diff
changeset
|
62 WHEN octree_checksum = $3 THEN NULL |
2e6b47cdb2ca
Store octrees along with sounding results
Tom Gottfried <tom@intevation.de>
parents:
1017
diff
changeset
|
63 ELSE octree_index |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 END |
1131
2e6b47cdb2ca
Store octrees along with sounding results
Tom Gottfried <tom@intevation.de>
parents:
1017
diff
changeset
|
65 FROM waterway.sounding_results |
2e6b47cdb2ca
Store octrees along with sounding results
Tom Gottfried <tom@intevation.de>
parents:
1017
diff
changeset
|
66 WHERE bottleneck_id = $1 AND date_info = $2::date |
1135
19a04b150b6c
Fixed SQL syntax errors introduced with 1153:2e6b47cdb2ca.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1131
diff
changeset
|
67 AND octree_checksum IS NOT NULL AND octree_index IS NOT NULL |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 ` |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 ) |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
71 var cache = Cache{ |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
72 entries: map[cacheKey]*cacheEntry{}, |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 func init() { |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
76 go cache.background() |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
79 func (c *Cache) background() { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 for { |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
81 time.Sleep(cleanupCacheSleep) |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
82 c.cleanup() |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
86 func (c *Cache) cleanup() { |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
87 c.Lock() |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
88 defer c.Unlock() |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
89 good := time.Now().Add(-maxCacheAge) |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
90 for k, v := range c.entries { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 if v.access.Before(good) { |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
92 delete(c.entries, k) |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 |
1692
f4dcbe8941a1
Octree: Resolved the remaing golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1333
diff
changeset
|
97 // FromCache fetches an Octree from the global Octree cache. |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
98 func FromCache( |
1328
d753ce6cf588
To make golint happier made context.Context to be the first argument in all calls.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1135
diff
changeset
|
99 ctx context.Context, |
d753ce6cf588
To make golint happier made context.Context to be the first argument in all calls.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1135
diff
changeset
|
100 conn *sql.Conn, |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 bottleneck string, date time.Time, |
727
41c8dc61f38f
Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
725
diff
changeset
|
102 ) (*Tree, error) { |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
103 return cache.get(ctx, conn, bottleneck, date) |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
104 } |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
105 |
4214
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
106 // FetchOctreeDirectly loads an octree directly from the database. |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
107 func FetchOctreeDirectly( |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
108 ctx context.Context, |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
109 tx *sql.Tx, |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
110 id int64, |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
111 ) (*Tree, error) { |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
112 var data []byte |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
113 err := tx.QueryRowContext(ctx, directFetchOctreeSQL, id).Scan(&data) |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
114 if err != nil { |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
115 return nil, err |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
116 } |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
117 return Deserialize(data) |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
118 } |
49564382ffff
Added a import queue job to recalculate the contour lines of the sounding results if the heights have changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2465
diff
changeset
|
119 |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
120 func (c *Cache) get( |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
121 ctx context.Context, |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
122 conn *sql.Conn, |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
123 bottleneck string, date time.Time, |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
124 ) (*Tree, error) { |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
125 c.Lock() |
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
126 defer c.Unlock() |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
128 key := cacheKey{date, bottleneck} |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
129 entry := c.entries[key] |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
130 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
131 var data []byte |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
132 var checksum string |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
133 |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
134 if entry == nil { |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
135 // fetch from database |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
136 err := conn.QueryRowContext( |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
137 ctx, fetchOctreeSQL, bottleneck, date).Scan(&checksum, &data) |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
138 switch { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
139 case err == sql.ErrNoRows: |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
140 return nil, nil |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
141 case err != nil: |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
142 return nil, err |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
143 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
144 } else { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
145 // check if we are not outdated. |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
146 err := conn.QueryRowContext( |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
147 ctx, checkOctreeSQL, bottleneck, date, entry.checksum).Scan(&data) |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 switch { |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 case err == sql.ErrNoRows: |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 return nil, nil |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 case err != nil: |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 return nil, err |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 } |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
154 if data == nil { // we are still current |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
155 entry.access = time.Now() |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
156 return entry.tree, nil |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
157 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
158 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
159 |
2465
86c7a023400e
Started experimental octree diff branch.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1692
diff
changeset
|
160 tree, err := Deserialize(data) |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
161 if err != nil { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
162 return nil, err |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
163 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
164 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
165 now := time.Now() |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
166 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
167 if entry != nil { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
168 entry.tree = tree |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
169 entry.access = now |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
170 return tree, nil |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
171 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
173 for len(c.entries) >= maxCacheEntries { |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
174 // Evict the entry that is accessed the longest time ago. |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
175 var oldestKey cacheKey |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
176 oldest := now |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
177 |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
178 for k, v := range c.entries { |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
179 if v.access.Before(oldest) { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
180 oldest = v.access |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
181 oldestKey = k |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
182 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
183 } |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
184 delete(c.entries, oldestKey) |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
185 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
186 |
1333
8c6b5d47a7ff
Renamed octree.OctreeCache to octree.Cache to reduce stuttering.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1328
diff
changeset
|
187 c.entries[key] = &cacheEntry{ |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
188 checksum: checksum, |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
189 tree: tree, |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
190 access: now, |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
191 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
192 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
193 return tree, nil |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
194 } |