1
1
import os
2
+ from functools import lru_cache
2
3
from typing import Any , Dict , List , Optional , Protocol
3
4
4
5
from stac_fastapi .types .stac import Item
@@ -14,6 +15,7 @@ class Geometry(Protocol): # noqa
14
15
15
16
COLLECTIONS_INDEX = os .getenv ("STAC_COLLECTIONS_INDEX" , "collections" )
16
17
ITEMS_INDEX_PREFIX = os .getenv ("STAC_ITEMS_INDEX_PREFIX" , "items_" )
18
+
17
19
ES_INDEX_NAME_UNSUPPORTED_CHARS = {
18
20
"\\ " ,
19
21
"/" ,
@@ -29,6 +31,10 @@ class Geometry(Protocol): # noqa
29
31
":" ,
30
32
}
31
33
34
+ _ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE = str .maketrans (
35
+ "" , "" , "" .join (ES_INDEX_NAME_UNSUPPORTED_CHARS )
36
+ )
37
+
32
38
ITEM_INDICES = f"{ ITEMS_INDEX_PREFIX } *,-*kibana*,-{ COLLECTIONS_INDEX } *"
33
39
34
40
DEFAULT_SORT = {
@@ -131,6 +137,7 @@ class Geometry(Protocol): # noqa
131
137
}
132
138
133
139
140
+ @lru_cache (256 )
134
141
def index_by_collection_id (collection_id : str ) -> str :
135
142
"""
136
143
Translate a collection id into an Elasticsearch index name.
@@ -141,9 +148,13 @@ def index_by_collection_id(collection_id: str) -> str:
141
148
Returns:
142
149
str: The index name derived from the collection id.
143
150
"""
144
- return f"{ ITEMS_INDEX_PREFIX } { '' .join (c for c in collection_id .lower () if c not in ES_INDEX_NAME_UNSUPPORTED_CHARS )} _{ collection_id .encode ('utf-8' ).hex ()} "
151
+ cleaned = collection_id .translate (_ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE )
152
+ return (
153
+ f"{ ITEMS_INDEX_PREFIX } { cleaned .lower ()} _{ collection_id .encode ('utf-8' ).hex ()} "
154
+ )
145
155
146
156
157
+ @lru_cache (256 )
147
158
def index_alias_by_collection_id (collection_id : str ) -> str :
148
159
"""
149
160
Translate a collection id into an Elasticsearch index alias.
@@ -154,7 +165,8 @@ def index_alias_by_collection_id(collection_id: str) -> str:
154
165
Returns:
155
166
str: The index alias derived from the collection id.
156
167
"""
157
- return f"{ ITEMS_INDEX_PREFIX } { '' .join (c for c in collection_id if c not in ES_INDEX_NAME_UNSUPPORTED_CHARS )} "
168
+ cleaned = collection_id .translate (_ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE )
169
+ return f"{ ITEMS_INDEX_PREFIX } { cleaned } "
158
170
159
171
160
172
def indices (collection_ids : Optional [List [str ]]) -> str :
@@ -165,12 +177,13 @@ def indices(collection_ids: Optional[List[str]]) -> str:
165
177
collection_ids: A list of collection ids.
166
178
167
179
Returns:
168
- A string of comma-separated index names. If `collection_ids` is None , returns the default indices.
180
+ A string of comma-separated index names. If `collection_ids` is empty , returns the default indices.
169
181
"""
170
- if not collection_ids :
171
- return ITEM_INDICES
172
- else :
173
- return "," .join ([index_alias_by_collection_id (c ) for c in collection_ids ])
182
+ return (
183
+ "," .join (map (index_alias_by_collection_id , collection_ids ))
184
+ if collection_ids
185
+ else ITEM_INDICES
186
+ )
174
187
175
188
176
189
def mk_item_id (item_id : str , collection_id : str ) -> str :
0 commit comments