2
2
from collections import OrderedDict
3
3
from datetime import datetime , timedelta , timezone
4
4
from os import scandir
5
+ from hashlib import sha256
5
6
import os
6
7
from contextlib import suppress
7
8
from pathlib import Path
@@ -23,14 +24,37 @@ def path_age(now: datetime, path: Path) -> timedelta:
23
24
24
25
return now - path_dt
25
26
27
+ index_hash_map = {}
28
+
29
+ def get_index_hash (idx : str ) -> str :
30
+ '''
31
+ Calculates a hash value for the specific index set
32
+ On some OS's the pathname becomes too long and causes errors when
33
+ creating files if multiple CCS indexes have been explicitly defined
34
+ *:my-data-* listed as
35
+ mysite-1:my-data-*,mysite-2:my-data-*,mysite-3:my-data-*,mysite-4:my-data-*,mysite-5:my-data-*
36
+ '''
37
+ idx_hash = index_hash_map .get (idx ,None )
38
+ if idx_hash is not None :
39
+ return idx_hash
40
+ idx_hash = sha256 ()
41
+ idx_hash .update (str (idx ).encode ("utf-8" ))
42
+ idx_hash = idx_hash .hexdigest ()[0 :20 ]
43
+ index_hash_map [idx ] = idx_hash
44
+ return idx_hash
45
+
26
46
def tile_name (idx , x , y , z , parameter_hash ) -> str :
27
- return f"{ idx } /{ parameter_hash } /{ z } /{ x } /{ y } .png"
47
+ idx_hash = get_index_hash (idx )
48
+ return f"{ idx_hash } /{ parameter_hash } /{ z } /{ x } /{ y } .png"
28
49
29
50
def rendering_tile_name (idx , x , y , z , parameter_hash ) -> str :
30
- return f"{ idx } /{ parameter_hash } /{ z } /{ x } /{ y } .rendering"
51
+ idx_hash = get_index_hash (idx )
52
+
53
+ return f"{ idx_hash } /{ parameter_hash } /{ z } /{ x } /{ y } .rendering"
31
54
32
55
def tile_id (idx , x , y , z , parameter_hash ) -> str :
33
- return f"{ idx } _{ parameter_hash } _{ z } _{ x } _{ y } "
56
+ idx_hash = get_index_hash (idx )
57
+ return f"{ idx_hash } _{ parameter_hash } _{ z } _{ x } _{ y } "
34
58
35
59
def directory_size (path : Path ) -> int :
36
60
'''
@@ -134,14 +158,14 @@ def release_cache_placeholder(cache_path: Path, tile: str) -> None:
134
158
if tile_path .exists ():
135
159
tile_path .unlink (missing_ok = True )
136
160
137
- def check_cache_dir (cache_path : Path , layer_name : str ) -> None :
161
+ def check_cache_dir (cache_path : Path , idx : str ) -> None :
138
162
"""
139
163
Ensure the folder ``cache_path``/``layer_name`` exists
140
164
141
165
:param cache_path: Top level directory
142
166
:param layer_name: Specific layer in cache
143
167
"""
144
- tile_cache_path = cache_path / layer_name
168
+ tile_cache_path = cache_path / get_index_hash ( idx )
145
169
tile_cache_path .mkdir (parents = True , exist_ok = True )
146
170
147
171
def clear_hash_cache (cache_path : Path , idx_name : str , param_hash : Optional [str ]) -> None :
0 commit comments