-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathindexes.py
102 lines (84 loc) · 3.05 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
"""
Examples using DynamoDB indexes
"""
import datetime
from pynamodb.models import Model
from pynamodb.indexes import GlobalSecondaryIndex, AllProjection, LocalSecondaryIndex
from pynamodb.attributes import UnicodeAttribute, NumberAttribute, UTCDateTimeAttribute
class ViewIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index
"""
class Meta:
# You can override the index name by setting it below
index_name = "viewIdx"
read_capacity_units = 1
write_capacity_units = 1
# All attributes are projected
projection = AllProjection()
# This attribute is the hash key for the index
# Note that this attribute must also exist
# in the model
view = NumberAttribute(default=0, hash_key=True)
class TestModel(Model):
"""
A test model that uses a global secondary index
"""
class Meta:
table_name = "TestModel"
# Set host for using DynamoDB Local
host = "http://localhost:8000"
forum = UnicodeAttribute(hash_key=True)
thread = UnicodeAttribute(range_key=True)
view_index = ViewIndex()
view = NumberAttribute(default=0)
if not TestModel.exists():
TestModel.create_table(read_capacity_units=1, write_capacity_units=1, wait=True)
# Create an item
item = TestModel('forum-example', 'thread-example')
item.view = 1
item.save()
# Indexes can be queried easily using the index's hash key
for item in TestModel.view_index.query(1):
print(f"Item queried from index: {item}")
class GamePlayerOpponentIndex(LocalSecondaryIndex):
class Meta:
read_capacity_units = 1
write_capacity_units = 1
table_name = "GamePlayerOpponentIndex"
host = "http://localhost:8000"
projection = AllProjection()
player_id = UnicodeAttribute(hash_key=True)
winner_id = UnicodeAttribute(range_key=True)
class GameOpponentTimeIndex(GlobalSecondaryIndex):
class Meta:
read_capacity_units = 1
write_capacity_units = 1
table_name = "GameOpponentTimeIndex"
host = "http://localhost:8000"
projection = AllProjection()
winner_id = UnicodeAttribute(hash_key=True)
created_time = UnicodeAttribute(range_key=True)
class GameModel(Model):
class Meta:
read_capacity_units = 1
write_capacity_units = 1
table_name = "GameModel"
host = "http://localhost:8000"
player_id = UnicodeAttribute(hash_key=True)
created_time = UTCDateTimeAttribute(range_key=True)
winner_id = UnicodeAttribute()
loser_id = UnicodeAttribute(null=True)
player_opponent_index = GamePlayerOpponentIndex()
opponent_time_index = GameOpponentTimeIndex()
if not GameModel.exists():
GameModel.create_table(wait=True)
# Create an item
item = GameModel('1234', datetime.datetime.utcnow())
item.winner_id = '5678'
item.save()
# Indexes can be queried easily using the index's hash key
for item in GameModel.player_opponent_index.query('1234'):
print(f"Item queried from index: {item}")
# Count on an index
print(GameModel.player_opponent_index.count('1234'))