Skip to content

Commit 9a68168

Browse files
committed
organization
1 parent 149be4e commit 9a68168

File tree

4 files changed

+163
-30
lines changed

4 files changed

+163
-30
lines changed

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database_logic_helpers.py

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
"""Shared code for elasticsearch/ opensearch database logic."""
1+
"""Shared code for elasticsearch/ opensearch database logic.
2+
3+
This module contains shared functions used by both the Elasticsearch and OpenSearch
4+
implementations of STAC FastAPI for database operations. It helps reduce code duplication
5+
and ensures consistent behavior between the two implementations.
6+
7+
The sfeos_helpers package is organized as follows:
8+
- database_logic_helpers.py: Shared database operations (this file)
9+
- filter.py: Shared filter extension implementation
10+
- mappings.py: Shared constants and mapping definitions
11+
- utilities.py: Shared utility functions
12+
13+
When adding new functionality to this package, consider:
14+
1. Will this code be used by both Elasticsearch and OpenSearch implementations?
15+
2. Is the functionality stable and unlikely to diverge between implementations?
16+
3. Is the function well-documented with clear input/output contracts?
17+
18+
Function Naming Conventions:
19+
- All shared functions should end with `_shared` to clearly indicate they're meant to be used by both implementations
20+
- Function names should be descriptive and indicate their purpose
21+
- Parameter names should be consistent across similar functions
22+
"""
223

324
from typing import Any, Dict, List, Optional
425

@@ -12,6 +33,10 @@
1233
)
1334
from stac_fastapi.sfeos_helpers.utilities import index_alias_by_collection_id
1435

36+
# ============================================================================
37+
# Index Management Functions
38+
# ============================================================================
39+
1540

1641
async def create_index_templates_shared(settings: Any) -> None:
1742
"""Create index templates for Elasticsearch/OpenSearch Collection and Item indices.
@@ -49,6 +74,39 @@ async def create_index_templates_shared(settings: Any) -> None:
4974
await client.close()
5075

5176

77+
async def delete_item_index_shared(settings: Any, collection_id: str) -> None:
78+
"""Delete the index for items in a collection.
79+
80+
Args:
81+
settings (Any): The settings object containing the client configuration.
82+
Must have a create_client attribute that returns an Elasticsearch/OpenSearch client.
83+
collection_id (str): The ID of the collection whose items index will be deleted.
84+
85+
Returns:
86+
None: This function doesn't return any value but deletes an item index in the database.
87+
88+
Notes:
89+
This function deletes an item index and its alias. It first resolves the alias to find
90+
the actual index name, then deletes both the alias and the index.
91+
"""
92+
client = settings.create_client
93+
94+
name = index_alias_by_collection_id(collection_id)
95+
resolved = await client.indices.resolve_index(name=name)
96+
if "aliases" in resolved and resolved["aliases"]:
97+
[alias] = resolved["aliases"]
98+
await client.indices.delete_alias(index=alias["indices"], name=alias["name"])
99+
await client.indices.delete(index=alias["indices"])
100+
else:
101+
await client.indices.delete(index=name)
102+
await client.close()
103+
104+
105+
# ============================================================================
106+
# Query Building Functions
107+
# ============================================================================
108+
109+
52110
def apply_free_text_filter_shared(
53111
search: Any, free_text_queries: Optional[List[str]]
54112
) -> Any:
@@ -126,32 +184,9 @@ def populate_sort_shared(sortby: List) -> Optional[Dict[str, Dict[str, str]]]:
126184
return None
127185

128186

129-
async def delete_item_index_shared(settings: Any, collection_id: str) -> None:
130-
"""Delete the index for items in a collection.
131-
132-
Args:
133-
settings (Any): The settings object containing the client configuration.
134-
Must have a create_client attribute that returns an Elasticsearch/OpenSearch client.
135-
collection_id (str): The ID of the collection whose items index will be deleted.
136-
137-
Returns:
138-
None: This function doesn't return any value but deletes an item index in the database.
139-
140-
Notes:
141-
This function deletes an item index and its alias. It first resolves the alias to find
142-
the actual index name, then deletes both the alias and the index.
143-
"""
144-
client = settings.create_client
145-
146-
name = index_alias_by_collection_id(collection_id)
147-
resolved = await client.indices.resolve_index(name=name)
148-
if "aliases" in resolved and resolved["aliases"]:
149-
[alias] = resolved["aliases"]
150-
await client.indices.delete_alias(index=alias["indices"], name=alias["name"])
151-
await client.indices.delete(index=alias["indices"])
152-
else:
153-
await client.indices.delete(index=name)
154-
await client.close()
187+
# ============================================================================
188+
# Mapping Functions
189+
# ============================================================================
155190

156191

157192
async def get_queryables_mapping_shared(

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/filter.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
"""Shared filter extension methods for stac-fastapi elasticsearch and opensearch backends."""
1+
"""Shared filter extension methods for stac-fastapi elasticsearch and opensearch backends.
2+
3+
This module provides shared functionality for implementing the STAC API Filter Extension
4+
with Elasticsearch and OpenSearch. It includes:
5+
6+
1. Functions for converting CQL2 queries to Elasticsearch/OpenSearch query DSL
7+
2. Helper functions for field mapping and query transformation
8+
3. Base implementation of the AsyncBaseFiltersClient for Elasticsearch/OpenSearch
9+
10+
The sfeos_helpers package is organized as follows:
11+
- database_logic_helpers.py: Shared database operations
12+
- filter.py: Shared filter extension implementation (this file)
13+
- mappings.py: Shared constants and mapping definitions
14+
- utilities.py: Shared utility functions
15+
16+
When adding new functionality to this package, consider:
17+
1. Will this code be used by both Elasticsearch and OpenSearch implementations?
18+
2. Is the functionality stable and unlikely to diverge between implementations?
19+
3. Is the function well-documented with clear input/output contracts?
20+
21+
Function Naming Conventions:
22+
- All shared functions should end with `_shared` to clearly indicate they're meant to be used by both implementations
23+
- Function names should be descriptive and indicate their purpose
24+
- Parameter names should be consistent across similar functions
25+
"""
226

327
import re
428
from collections import deque
@@ -20,6 +44,10 @@
2044

2145
from .mappings import ES_MAPPING_TYPE_TO_JSON
2246

47+
# ============================================================================
48+
# CQL2 Pattern Conversion Helpers
49+
# ============================================================================
50+
2351

2452
def _replace_like_patterns(match: re.Match) -> str:
2553
pattern = match.group()
@@ -48,6 +76,11 @@ def cql2_like_to_es(string: str) -> str:
4876
)
4977

5078

79+
# ============================================================================
80+
# Query Transformation Functions
81+
# ============================================================================
82+
83+
5184
def to_es_field(queryables_mapping: Dict[str, Any], field: str) -> str:
5285
"""
5386
Map a given field to its corresponding Elasticsearch field according to a predefined mapping.
@@ -169,6 +202,11 @@ def to_es(queryables_mapping: Dict[str, Any], query: Dict[str, Any]) -> Dict[str
169202
return {}
170203

171204

205+
# ============================================================================
206+
# Filter Client Implementation
207+
# ============================================================================
208+
209+
172210
@attr.s
173211
class EsAsyncBaseFiltersClient(AsyncBaseFiltersClient):
174212
"""Defines a pattern for implementing the STAC filter extension."""

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/mappings.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
"""Shared mappings for stac-fastapi elasticsearch and opensearch backends."""
1+
"""Shared mappings for stac-fastapi elasticsearch and opensearch backends.
2+
3+
This module contains shared constants, mappings, and type definitions used by both
4+
the Elasticsearch and OpenSearch implementations of STAC FastAPI. It includes:
5+
6+
1. Index name constants and character translation tables
7+
2. Mapping definitions for Collections and Items
8+
3. Aggregation mappings for search queries
9+
4. Type conversion mappings between Elasticsearch/OpenSearch and JSON Schema types
10+
11+
The sfeos_helpers package is organized as follows:
12+
- database_logic_helpers.py: Shared database operations
13+
- filter.py: Shared filter extension implementation
14+
- mappings.py: Shared constants and mapping definitions (this file)
15+
- utilities.py: Shared utility functions
16+
17+
When adding new functionality to this package, consider:
18+
1. Will this code be used by both Elasticsearch and OpenSearch implementations?
19+
2. Is the functionality stable and unlikely to diverge between implementations?
20+
3. Is the function well-documented with clear input/output contracts?
21+
22+
Function Naming Conventions:
23+
- All shared functions should end with `_shared` to clearly indicate they're meant to be used by both implementations
24+
- Function names should be descriptive and indicate their purpose
25+
- Parameter names should be consistent across similar functions
26+
"""
227

328
import os
429
from typing import Any, Dict, Literal, Protocol

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/utilities.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
"""Shared utilities functions for stac-fastapi elasticsearch and opensearch backends."""
1+
"""Shared utilities functions for stac-fastapi elasticsearch and opensearch backends.
2+
3+
This module contains general utility functions used by both the Elasticsearch and OpenSearch
4+
implementations of STAC FastAPI. These functions handle common tasks like parameter validation,
5+
index naming, and document ID generation.
6+
7+
The sfeos_helpers package is organized as follows:
8+
- database_logic_helpers.py: Shared database operations
9+
- filter.py: Shared filter extension implementation
10+
- mappings.py: Shared constants and mapping definitions
11+
- utilities.py: Shared utility functions (this file)
12+
13+
When adding new functionality to this package, consider:
14+
1. Will this code be used by both Elasticsearch and OpenSearch implementations?
15+
2. Is the functionality stable and unlikely to diverge between implementations?
16+
3. Is the function well-documented with clear input/output contracts?
17+
18+
Function Naming Conventions:
19+
- All shared functions should end with `_shared` to clearly indicate they're meant to be used by both implementations
20+
- Function names should be descriptive and indicate their purpose
21+
- Parameter names should be consistent across similar functions
22+
"""
223
import logging
324
from functools import lru_cache
425
from typing import Any, Dict, List, Optional, Union
@@ -13,6 +34,10 @@
1334
)
1435
from stac_fastapi.types.stac import Item
1536

37+
# ============================================================================
38+
# Parameter Validation
39+
# ============================================================================
40+
1641

1742
def validate_refresh(value: Union[str, bool]) -> str:
1843
"""
@@ -54,6 +79,11 @@ def validate_refresh(value: Union[str, bool]) -> str:
5479
return "false"
5580

5681

82+
# ============================================================================
83+
# Index and Document ID Utilities
84+
# ============================================================================
85+
86+
5787
@lru_cache(256)
5888
def index_by_collection_id(collection_id: str) -> str:
5989
"""
@@ -103,6 +133,11 @@ def indices(collection_ids: Optional[List[str]]) -> str:
103133
)
104134

105135

136+
# ============================================================================
137+
# Document ID and Action Generation
138+
# ============================================================================
139+
140+
106141
def mk_item_id(item_id: str, collection_id: str) -> str:
107142
"""Create the document id for an Item in Elasticsearch.
108143

0 commit comments

Comments
 (0)