Merge branch 'remotes/gullydwarf-cfdv/f360_tagging' (early part) into mergetags
[mediagoblin.git] / mediagoblin / db / indexes.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """
18 Indexes for the local database.
19
20 To add new indexes
21 ------------------
22
23 Indexes are recorded in the following format:
24
25 ACTIVE_INDEXES = {
26 'collection_name': {
27 'identifier': { # key identifier used for possibly deprecating later
28 'index': [index_foo_goes_here]}}
29
30 ... and anything else being parameters to the create_index function
31 (including unique=True, etc)
32
33 Current indexes must be registered in ACTIVE_INDEXES... deprecated
34 indexes should be marked in DEPRECATED_INDEXES.
35
36 Remember, ordering of compound indexes MATTERS. Read below for more.
37
38 REQUIRED READING:
39 - http://kylebanker.com/blog/2010/09/21/the-joy-of-mongodb-indexes/
40
41 - http://www.mongodb.org/display/DOCS/Indexes
42 - http://www.mongodb.org/display/DOCS/Indexing+Advice+and+FAQ
43
44
45 To remove deprecated indexes
46 ----------------------------
47
48 Removing deprecated indexes is the same, just move the index into the
49 deprecated indexes mapping.
50
51 DEPRECATED_INDEXES = {
52 'collection_name': {
53 'deprecated_index_identifier1': {
54 'index': [index_foo_goes_here]}}
55
56 ... etc.
57
58 If an index has been deprecated that identifier should NEVER BE USED
59 AGAIN. Eg, if you previously had 'awesomepants_unique', you shouldn't
60 use 'awesomepants_unique' again, you should create a totally new name
61 or at worst use 'awesomepants_unique2'.
62 """
63
64 from pymongo import ASCENDING, DESCENDING
65
66
67 ################
68 # Active indexes
69 ################
70 ACTIVE_INDEXES = {}
71
72 # MediaEntry indexes
73 # ------------------
74
75 MEDIAENTRY_INDEXES = {
76 'uploader_slug_unique': {
77 # Matching an object to an uploader + slug.
78 # MediaEntries are unique on these two combined, eg:
79 # /u/${myuser}/m/${myslugname}/
80 'index': [('uploader', ASCENDING),
81 ('slug', ASCENDING)],
82 'unique': True},
83
84 'created': {
85 # A global index for all media entries created, in descending
86 # order. This is used for the site's frontpage.
87 'index': [('created', DESCENDING)]},
88
89 'uploader_created': {
90 # Indexing on uploaders and when media entries are created.
91 # Used for showing a user gallery, etc.
92 'index': [('uploader', ASCENDING),
93 ('created', DESCENDING)]},
94
95 'state_uploader_tags_created': {
96 # Indexing on processed?, media uploader, associated tags, and timestamp
97 # Used for showing media items matching a tag search, most recent first.
98 'index': [('state', ASCENDING),
99 ('uploader', ASCENDING),
100 ('tags.slug', DESCENDING),
101 ('created', DESCENDING)]},
102
103 'state_tags_created': {
104 # Indexing on processed?, media tags, and timestamp (across all users)
105 # This is used for a front page tag search.
106 'index': [('state', ASCENDING),
107 ('tags.slug', DESCENDING),
108 ('created', DESCENDING)]}}
109
110
111 ACTIVE_INDEXES['media_entries'] = MEDIAENTRY_INDEXES
112
113
114 # User indexes
115 # ------------
116
117 USER_INDEXES = {
118 'username_unique': {
119 # Index usernames, and make sure they're unique.
120 # ... I guess we might need to adjust this once we're federated :)
121 'index': 'username',
122 'unique': True},
123 'created': {
124 # All most recently created users
125 'index': 'created'}}
126
127
128 ACTIVE_INDEXES['users'] = USER_INDEXES
129
130
131 # MediaComment indexes
132
133 MEDIA_COMMENT_INDEXES = {
134 'mediaentry_created': {
135 'index': [('media_entry', ASCENDING),
136 ('created', DESCENDING)]}}
137
138 ACTIVE_INDEXES['media_comments'] = MEDIA_COMMENT_INDEXES
139
140
141 ####################
142 # Deprecated indexes
143 ####################
144
145 DEPRECATED_INDEXES = {}