Mercurial > gemma
annotate pkg/octree/cache.go @ 1017:a244b18cb916
Added GNU Affero General Public License.
Adjusted the headers of the Go files to be AGPLv3.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 23 Oct 2018 18:15:14 +0200 |
parents | 41c8dc61f38f |
children | 2e6b47cdb2ca |
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 } |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
34 OctreeCache struct { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 sync.Mutex |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
36 entries map[cacheKey]*cacheEntry |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 ) |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 const ( |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
41 cleanupCacheSleep = 6 * time.Minute |
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
42 maxCacheAge = 5 * time.Minute |
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
43 maxCacheEntries = 4 |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 ) |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 const ( |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 fetchOctreeSQL = ` |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 SELECT checksum, octree_index |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 FROM waterway.octrees ot |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 JOIN waterway.sounding_results sr ON ot.sounding_result_id = sr.id |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 WHERE sr.bottleneck_id = $1 AND sr.date_info = $2::date |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 ` |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 checkOctreeSQL = ` |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 SELECT CASE |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 WHEN checksum = $3 THEN NULL |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 ELSE ot.octree_index |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 END |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 FROM waterway.octrees ot |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 JOIN waterway.sounding_results sr ON ot.sounding_result_id = sr.id |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 WHERE sr.bottleneck_id = $1 AND sr.date_info = $2::date |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 ` |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 ) |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
64 var Cache = OctreeCache{ |
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
65 entries: map[cacheKey]*cacheEntry{}, |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 func init() { |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
69 go Cache.background() |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
72 func (oc *OctreeCache) background() { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 for { |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
74 time.Sleep(cleanupCacheSleep) |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 oc.cleanup() |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 } |
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 |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
79 func (oc *OctreeCache) cleanup() { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 oc.Lock() |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 defer oc.Unlock() |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
82 good := time.Now().Add(-maxCacheAge) |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 for k, v := range oc.entries { |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 if v.access.Before(good) { |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 delete(oc.entries, k) |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
90 func (oc *OctreeCache) Get( |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 bottleneck string, date time.Time, |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 conn *sql.Conn, ctx context.Context, |
727
41c8dc61f38f
Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
725
diff
changeset
|
93 ) (*Tree, error) { |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 oc.Lock() |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 defer oc.Unlock() |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 |
724
6ab0c170e5b8
Moved octree stuff to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
723
diff
changeset
|
97 key := cacheKey{date, bottleneck} |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 entry := oc.entries[key] |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
99 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
100 var data []byte |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
101 var checksum string |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
102 |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 if entry == nil { |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 // fetch from database |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
105 err := conn.QueryRowContext( |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
106 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
|
107 switch { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
108 case err == sql.ErrNoRows: |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
109 return nil, nil |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
110 case err != nil: |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
111 return nil, err |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
112 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
113 } else { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
114 // 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
|
115 err := conn.QueryRowContext( |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
116 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
|
117 switch { |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 case err == sql.ErrNoRows: |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 return nil, nil |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 case err != nil: |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 return nil, err |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 } |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
123 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
|
124 entry.access = time.Now() |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
125 return entry.tree, nil |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
126 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
127 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
128 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
129 tree, err := Deserialize(data) |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
130 if err != nil { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
131 return nil, err |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
132 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
133 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
134 now := time.Now() |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
135 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
136 if entry != nil { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
137 entry.tree = tree |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
138 entry.access = now |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
139 return tree, nil |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 } |
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 |
725
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
142 for len(oc.entries) >= maxCacheEntries { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
143 // 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
|
144 var oldestKey cacheKey |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
145 oldest := now |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
146 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
147 for k, v := range oc.entries { |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
148 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
|
149 oldest = v.access |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
150 oldestKey = k |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
151 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
152 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
153 delete(oc.entries, oldestKey) |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
154 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
155 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
156 oc.entries[key] = &cacheEntry{ |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
157 checksum: checksum, |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
158 tree: tree, |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
159 access: now, |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
160 } |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
161 |
e0437ec46798
Finished the octree cache. TODO: implement the deserialization.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
724
diff
changeset
|
162 return tree, nil |
723
7eed7ff3142d
Started with model to load octrees from database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
163 } |