Merge branch 'master' of gitorious.org:mediagoblin/mediagoblin
[mediagoblin.git] / mediagoblin / db / mongo / indexes.py
CommitLineData
3cd6ea5b 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3cd6ea5b
CAW
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"""
18Indexes for the local database.
19
1f774999
CAW
20To add new indexes
21------------------
22
3cd6ea5b
CAW
23Indexes are recorded in the following format:
24
1f774999 25ACTIVE_INDEXES = {
ca5d2c51
CAW
26 'collection_name': {
27 'identifier': { # key identifier used for possibly deprecating later
28 'index': [index_foo_goes_here]}}
3cd6ea5b
CAW
29
30... and anything else being parameters to the create_index function
31(including unique=True, etc)
32
33Current indexes must be registered in ACTIVE_INDEXES... deprecated
34indexes should be marked in DEPRECATED_INDEXES.
35
36Remember, ordering of compound indexes MATTERS. Read below for more.
37
38REQUIRED READING:
39 - http://kylebanker.com/blog/2010/09/21/the-joy-of-mongodb-indexes/
ca5d2c51 40
3cd6ea5b
CAW
41 - http://www.mongodb.org/display/DOCS/Indexes
42 - http://www.mongodb.org/display/DOCS/Indexing+Advice+and+FAQ
b1db6f20
CAW
43
44
1f774999
CAW
45To remove deprecated indexes
46----------------------------
47
7ecb1b07
CAW
48Removing deprecated indexes is the same, just move the index into the
49deprecated indexes mapping.
1f774999 50
7ecb1b07
CAW
51DEPRECATED_INDEXES = {
52 'collection_name': {
53 'deprecated_index_identifier1': {
54 'index': [index_foo_goes_here]}}
1f774999
CAW
55
56... etc.
57
58If an index has been deprecated that identifier should NEVER BE USED
59AGAIN. Eg, if you previously had 'awesomepants_unique', you shouldn't
60use 'awesomepants_unique' again, you should create a totally new name
61or at worst use 'awesomepants_unique2'.
3cd6ea5b
CAW
62"""
63
64from pymongo import ASCENDING, DESCENDING
65
66
67################
68# Active indexes
69################
70ACTIVE_INDEXES = {}
71
72# MediaEntry indexes
73# ------------------
74
75MEDIAENTRY_INDEXES = {
b1db6f20 76 'uploader_slug_unique': {
3cd6ea5b
CAW
77 # Matching an object to an uploader + slug.
78 # MediaEntries are unique on these two combined, eg:
79 # /u/${myuser}/m/${myslugname}/
3cd6ea5b
CAW
80 'index': [('uploader', ASCENDING),
81 ('slug', ASCENDING)],
82 'unique': True},
83
b1db6f20 84 'created': {
3cd6ea5b
CAW
85 # A global index for all media entries created, in descending
86 # order. This is used for the site's frontpage.
3cd6ea5b
CAW
87 'index': [('created', DESCENDING)]},
88
b1db6f20 89 'uploader_created': {
3cd6ea5b
CAW
90 # Indexing on uploaders and when media entries are created.
91 # Used for showing a user gallery, etc.
3cd6ea5b 92 'index': [('uploader', ASCENDING),
272469da
CFD
93 ('created', DESCENDING)]},
94
cea8f2b6 95 'state_uploader_tags_created': {
243c3843
NY
96 # Indexing on processed?, media uploader, associated tags, and
97 # timestamp Used for showing media items matching a tag
98 # search, most recent first.
cea8f2b6
CFD
99 'index': [('state', ASCENDING),
100 ('uploader', ASCENDING),
37be7b6d 101 ('tags.slug', DESCENDING),
1580c7c5
CFD
102 ('created', DESCENDING)]},
103
cea8f2b6
CFD
104 'state_tags_created': {
105 # Indexing on processed?, media tags, and timestamp (across all users)
1580c7c5 106 # This is used for a front page tag search.
cea8f2b6 107 'index': [('state', ASCENDING),
37be7b6d 108 ('tags.slug', DESCENDING),
3cd6ea5b
CAW
109 ('created', DESCENDING)]}}
110
111
ca5d2c51 112ACTIVE_INDEXES['media_entries'] = MEDIAENTRY_INDEXES
3cd6ea5b
CAW
113
114
115# User indexes
116# ------------
117
118USER_INDEXES = {
b1db6f20 119 'username_unique': {
3cd6ea5b
CAW
120 # Index usernames, and make sure they're unique.
121 # ... I guess we might need to adjust this once we're federated :)
ca5d2c51
CAW
122 'index': 'username',
123 'unique': True},
b1db6f20 124 'created': {
3cd6ea5b 125 # All most recently created users
3cd6ea5b 126 'index': 'created'}}
b1db6f20 127
3cd6ea5b 128
ca5d2c51 129ACTIVE_INDEXES['users'] = USER_INDEXES
3cd6ea5b
CAW
130
131
a98104eb
CAW
132# MediaComment indexes
133
134MEDIA_COMMENT_INDEXES = {
135 'mediaentry_created': {
136 'index': [('media_entry', ASCENDING),
137 ('created', DESCENDING)]}}
138
139ACTIVE_INDEXES['media_comments'] = MEDIA_COMMENT_INDEXES
140
141
3cd6ea5b
CAW
142####################
143# Deprecated indexes
144####################
145
ca5d2c51 146DEPRECATED_INDEXES = {}