forked from pynamodb/PynamoDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes.py
234 lines (206 loc) · 6.95 KB
/
indexes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
PynamoDB Indexes
"""
from inspect import getmembers
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import TYPE_CHECKING
from pynamodb._compat import GenericMeta
from pynamodb.constants import (
INCLUDE, ALL, KEYS_ONLY, ATTR_NAME, ATTR_TYPE, KEY_TYPE, ATTR_TYPE_MAP, KEY_SCHEMA,
ATTR_DEFINITIONS, META_CLASS_NAME
)
from pynamodb.attributes import Attribute
from pynamodb.expressions.condition import Condition
from pynamodb.pagination import ResultIterator
from pynamodb.types import HASH, RANGE
from pynamodb.util import snake_to_camel_case
if TYPE_CHECKING:
from pynamodb.models import Model
_KeyType = Any
_M = TypeVar('_M', bound='Model')
class IndexMeta(GenericMeta):
"""
Index meta class
This class is here to allow for an index `Meta` class
that contains the index settings
"""
def __init__(self, name, bases, attrs, *args, **kwargs):
super().__init__(name, bases, attrs, *args, **kwargs) # type: ignore
if isinstance(attrs, dict):
for attr_name, attr_obj in attrs.items():
if attr_name == META_CLASS_NAME:
meta_cls = attrs.get(META_CLASS_NAME)
if meta_cls is not None:
meta_cls.attributes = None
elif isinstance(attr_obj, Attribute):
if attr_obj.attr_name is None:
attr_obj.attr_name = attr_name
class Index(Generic[_M], metaclass=IndexMeta):
"""
Base class for secondary indexes
"""
Meta: Any = None
def __init__(self) -> None:
if self.Meta is None:
raise ValueError("Indexes require a Meta class for settings")
if not hasattr(self.Meta, "projection"):
raise ValueError("No projection defined, define a projection for this class")
@classmethod
def count(
cls,
hash_key: _KeyType,
range_key_condition: Optional[Condition] = None,
filter_condition: Optional[Condition] = None,
consistent_read: bool = False,
limit: Optional[int] = None,
rate_limit: Optional[float] = None,
) -> int:
"""
Count on an index
"""
return cls.Meta.model.count(
hash_key,
range_key_condition=range_key_condition,
filter_condition=filter_condition,
index_name=cls.Meta.index_name,
consistent_read=consistent_read,
limit=limit,
rate_limit=rate_limit,
)
@classmethod
def query(
cls,
hash_key: _KeyType,
range_key_condition: Optional[Condition] = None,
filter_condition: Optional[Condition] = None,
consistent_read: Optional[bool] = False,
scan_index_forward: Optional[bool] = None,
limit: Optional[int] = None,
last_evaluated_key: Optional[Dict[str, Dict[str, Any]]] = None,
attributes_to_get: Optional[List[str]] = None,
page_size: Optional[int] = None,
rate_limit: Optional[float] = None,
) -> ResultIterator[_M]:
"""
Queries an index
"""
return cls.Meta.model.query(
hash_key,
range_key_condition=range_key_condition,
filter_condition=filter_condition,
consistent_read=consistent_read,
index_name=cls.Meta.index_name,
scan_index_forward=scan_index_forward,
limit=limit,
last_evaluated_key=last_evaluated_key,
attributes_to_get=attributes_to_get,
page_size=page_size,
rate_limit=rate_limit,
)
@classmethod
def scan(
cls,
filter_condition: Optional[Condition] = None,
segment: Optional[int] = None,
total_segments: Optional[int] = None,
limit: Optional[int] = None,
last_evaluated_key: Optional[Dict[str, Dict[str, Any]]] = None,
page_size: Optional[int] = None,
consistent_read: Optional[bool] = None,
rate_limit: Optional[float] = None,
attributes_to_get: Optional[List[str]] = None,
):
"""
Scans an index
"""
return cls.Meta.model.scan(
filter_condition=filter_condition,
segment=segment,
total_segments=total_segments,
limit=limit,
last_evaluated_key=last_evaluated_key,
page_size=page_size,
consistent_read=consistent_read,
index_name=cls.Meta.index_name,
rate_limit=rate_limit,
attributes_to_get=attributes_to_get,
)
@classmethod
def _hash_key_attribute(cls):
"""
Returns the attribute class for the hash key
"""
for attr_cls in cls._get_attributes().values():
if attr_cls.is_hash_key:
return attr_cls
@classmethod
def _get_schema(cls) -> Dict:
"""
Returns the schema for this index
"""
attr_definitions = []
schema = []
for attr_name, attr_cls in cls._get_attributes().items():
attr_definitions.append({
snake_to_camel_case(ATTR_NAME): attr_cls.attr_name,
snake_to_camel_case(ATTR_TYPE): ATTR_TYPE_MAP[attr_cls.attr_type]
})
if attr_cls.is_hash_key:
schema.append({
ATTR_NAME: attr_cls.attr_name,
KEY_TYPE: HASH
})
elif attr_cls.is_range_key:
schema.append({
ATTR_NAME: attr_cls.attr_name,
KEY_TYPE: RANGE
})
return {
snake_to_camel_case(KEY_SCHEMA): schema,
snake_to_camel_case(ATTR_DEFINITIONS): attr_definitions
}
@classmethod
def _get_attributes(cls):
"""
Returns the list of attributes for this class
"""
if cls.Meta.attributes is None:
cls.Meta.attributes = {}
for name, attribute in getmembers(cls, lambda o: isinstance(o, Attribute)):
cls.Meta.attributes[name] = attribute
return cls.Meta.attributes
class GlobalSecondaryIndex(Index[_M]):
"""
A global secondary index
"""
pass
class LocalSecondaryIndex(Index[_M]):
"""
A local secondary index
"""
pass
class Projection(object):
"""
A class for presenting projections
"""
projection_type: Any = None
non_key_attributes: Any = None
class KeysOnlyProjection(Projection):
"""
Keys only projection
"""
projection_type = KEYS_ONLY
class IncludeProjection(Projection):
"""
An INCLUDE projection
"""
projection_type = INCLUDE
def __init__(self, non_attr_keys: Optional[List[str]] = None) -> None:
if not non_attr_keys:
raise ValueError("The INCLUDE type projection requires a list of string attribute names")
self.non_key_attributes = non_attr_keys
class AllProjection(Projection):
"""
An ALL projection
"""
projection_type = ALL