Skip to content

Commit 682ed95

Browse files
author
Mike Dirolf
committed
API CHANGE: deprecating passing direction to ensure_index, create_index for single key - just creates an ascending index since direction doesn't matter
1 parent bf2b906 commit 682ed95

File tree

8 files changed

+76
-71
lines changed

8 files changed

+76
-71
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ ObjectId('\t6\xc6\x07\xb3\xfc\x87\xc4\x82\x04\x0f\\')
5151
10
5252
8
5353
11
54-
>>> from pymongo import ASCENDING
55-
>>> db.my_collection.create_index("x", ASCENDING)
54+
>>> db.my_collection.create_index("x")
5655
u'x_1'
56+
>>> from pymongo import ASCENDING
5757
>>> for item in db.my_collection.find().sort("x", ASCENDING):
5858
... print item["x"]
5959
...

examples/quick_tour.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@
8282
for d in collection.find({"i": {"$gt": 20, "$lte": 30}}):
8383
print d
8484

85-
# Finally, we can create an ascending index on i, and print out the collection's
85+
# Finally, we can create an index on i, and print out the collection's
8686
# dictionary of indexes.
87-
from pymongo import ASCENDING
88-
collection.create_index("i", ASCENDING)
87+
collection.create_index("i")
8988
print collection.index_information()

pymongo/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@
4848
_SEEK_CUR = 1
4949
_SEEK_END = 2
5050

51-
def _index_list(key_or_list, direction):
51+
def _index_list(key_or_list, direction=None):
5252
"""Helper to generate a list of (key, direction) pairs.
5353
54-
Takes such a list, or a single key and direction.
54+
Takes such a list, or a single key, or a single key and direction.
5555
"""
5656
if direction is not None:
5757
return [(key_or_list, direction)]
5858
else:
5959
if isinstance(key_or_list, types.StringTypes):
60-
raise TypeError("must specify a direction if using a string key")
60+
return [(key_or_list, ASCENDING)]
6161
return key_or_list
6262

6363

@@ -67,7 +67,7 @@ def _index_document(index_list):
6767
Takes a list of (key, direction) pairs.
6868
"""
6969
if not isinstance(index_list, types.ListType):
70-
raise TypeError("if no direction is specified, key_or_list must be an"
70+
raise TypeError("if no direction is specified, key_or_list must be an "
7171
"instance of list")
7272
if not len(index_list):
7373
raise ValueError("key_or_list must not be the empty list")
@@ -77,7 +77,7 @@ def _index_document(index_list):
7777
if not isinstance(key, types.StringTypes):
7878
raise TypeError("first item in each key pair must be a string")
7979
if not isinstance(value, types.IntType):
80-
raise TypeError("second item in each key pair must be ASCENDING or"
80+
raise TypeError("second item in each key pair must be ASCENDING or "
8181
"DESCENDING")
8282
index[key] = value
8383
return index

pymongo/collection.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Collection level utilities for Mongo."""
1616

1717
import types
18+
import warnings
1819

1920
import pymongo
2021
import bson
@@ -389,16 +390,15 @@ def _gen_index_name(self, keys):
389390
def create_index(self, key_or_list, direction=None, unique=False, ttl=300):
390391
"""Creates an index on this collection.
391392
392-
Takes either a single key and a direction, or a list of (key,
393-
direction) pairs. The key(s) must be an instance of (str, unicode),
394-
and the direction(s) must be one of (`pymongo.ASCENDING`,
393+
Takes either a single key or a list of (key, direction) pairs.
394+
The key(s) must be an instance of (str, unicode),
395+
and the directions must be one of (`pymongo.ASCENDING`,
395396
`pymongo.DESCENDING`). Returns the name of the created index.
396397
397398
:Parameters:
398399
- `key_or_list`: a single key or a list of (key, direction) pairs
399400
specifying the index to create
400-
- `direction` (optional): must be included if key_or_list is a single
401-
key, otherwise must be None
401+
- `direction` (optional): DEPRECATED this option will be removed
402402
- `unique` (optional): should this index guarantee uniqueness?
403403
- `ttl` (optional): time window (in seconds) during which this index
404404
will be recognized by subsequent calls to `ensure_index` - see
@@ -407,8 +407,14 @@ def create_index(self, key_or_list, direction=None, unique=False, ttl=300):
407407
if not isinstance(key_or_list, (str, unicode, list)):
408408
raise TypeError("key_or_list must either be a single key or a list of (key, direction) pairs")
409409

410+
if direction is not None:
411+
warnings.warn("specifying a direction for a single key index is "
412+
"deprecated and will be removed. there is no need "
413+
"for a direction on a single key index",
414+
DeprecationWarning)
415+
410416
to_save = SON()
411-
keys = pymongo._index_list(key_or_list, direction)
417+
keys = pymongo._index_list(key_or_list)
412418
name = self._gen_index_name(keys)
413419
to_save["name"] = name
414420
to_save["ns"] = self.full_name()
@@ -426,8 +432,8 @@ def create_index(self, key_or_list, direction=None, unique=False, ttl=300):
426432
def ensure_index(self, key_or_list, direction=None, unique=False, ttl=300):
427433
"""Ensures that an index exists on this collection.
428434
429-
Takes either a single key and a direction, or a list of (key,
430-
direction) pairs. The key(s) must be an instance of (str, unicode),
435+
Takes either a single key or a list of (key, direction) pairs.
436+
The key(s) must be an instance of (str, unicode),
431437
and the direction(s) must be one of (`pymongo.ASCENDING`,
432438
`pymongo.DESCENDING`).
433439
@@ -450,21 +456,26 @@ def ensure_index(self, key_or_list, direction=None, unique=False, ttl=300):
450456
:Parameters:
451457
- `key_or_list`: a single key or a list of (key, direction) pairs
452458
specifying the index to ensure
453-
- `direction` (optional): must be included if key_or_list is a single
454-
key, otherwise must be None
459+
- `direction` (optional): DEPRECATED this option will be removed
455460
- `unique` (optional): should this index guarantee uniqueness?
456461
- `ttl` (optional): time window (in seconds) during which this index
457462
will be recognized by subsequent calls to `ensure_index`
458463
"""
459464
if not isinstance(key_or_list, (str, unicode, list)):
460465
raise TypeError("key_or_list must either be a single key or a list of (key, direction) pairs")
461466

462-
keys = pymongo._index_list(key_or_list, direction)
467+
if direction is not None:
468+
warnings.warn("specifying a direction for a single key index is "
469+
"deprecated and will be removed. there is no need "
470+
"for a direction on a single key index",
471+
DeprecationWarning)
472+
473+
keys = pymongo._index_list(key_or_list)
463474
name = self._gen_index_name(keys)
464475
if self.database().connection()._cache_index(self.__database.name(),
465476
self.name(),
466477
name, ttl):
467-
return self.create_index(key_or_list, direction, unique, ttl)
478+
return self.create_index(key_or_list, unique=unique, ttl=ttl)
468479
return None
469480

470481
def drop_indexes(self):
@@ -483,8 +494,9 @@ def drop_index(self, index_or_name):
483494
Can be used on non-existant collections or collections with no indexes.
484495
Raises OperationFailure on an error. `index_or_name` can be either an
485496
index name (as returned by `create_index`), or an index specifier (as
486-
passed to `create_index`). Raises TypeError if index is not an
487-
instance of (str, unicode, list).
497+
passed to `create_index`). An index specifier should be a list of (key,
498+
direction) pairs. Raises TypeError if index is not an instance of (str,
499+
unicode, list).
488500
489501
:Parameters:
490502
- `index_or_name`: index (or name of index) to drop

pymongo/cursor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ def sort(self, key_or_list, direction=None):
224224
:Parameters:
225225
- `key_or_list`: a single key or a list of (key, direction) pairs
226226
specifying the keys to sort on
227-
- `direction` (optional): must be included if key_or_list is a single
228-
key, otherwise must be None
227+
- `direction` (optional): only used if key_or_list is a single
228+
key, if not given ASCENDING is assumed
229229
"""
230230
self.__check_okay_to_chain()
231231
keys = pymongo._index_list(key_or_list, direction)

test/test_collection.py

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,13 @@ def test_create_index(self):
6464

6565
self.assertRaises(TypeError, db.test.create_index, 5)
6666
self.assertRaises(TypeError, db.test.create_index, {"hello": 1})
67-
self.assertRaises(TypeError, db.test.create_index, "hello")
6867
self.assertRaises(ValueError, db.test.create_index, [])
69-
self.assertRaises(TypeError, db.test.create_index, [], ASCENDING)
70-
self.assertRaises(TypeError, db.test.create_index,
71-
[("hello", DESCENDING)], DESCENDING)
72-
self.assertRaises(TypeError, db.test.create_index, "hello", "world")
7368

7469
db.test.drop_indexes()
7570
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
7671
.count(), 1)
7772

78-
db.test.create_index("hello", ASCENDING)
73+
db.test.create_index("hello")
7974
db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)])
8075

8176
count = 0
@@ -86,7 +81,7 @@ def test_create_index(self):
8681
db.test.drop_indexes()
8782
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
8883
.count(), 1)
89-
db.test.create_index("hello", ASCENDING)
84+
db.test.create_index("hello")
9085
self.assert_(SON([(u"name", u"hello_1"),
9186
(u"unique", False),
9287
(u"ns", u"pymongo_test.test"),
@@ -112,54 +107,52 @@ def test_ensure_index(self):
112107
self.assertRaises(TypeError, db.test.ensure_index, {"hello": 1})
113108

114109
db.test.drop_indexes()
115-
self.assertEqual("hello_1", db.test.create_index("hello", ASCENDING))
116-
self.assertEqual("hello_1", db.test.create_index("hello", ASCENDING))
110+
self.assertEqual("hello_1", db.test.create_index("hello"))
111+
self.assertEqual("hello_1", db.test.create_index("hello"))
117112

118113
self.assertEqual("goodbye_1",
119-
db.test.ensure_index("goodbye", ASCENDING))
120-
self.assertEqual(None, db.test.ensure_index("goodbye", ASCENDING))
114+
db.test.ensure_index("goodbye"))
115+
self.assertEqual(None, db.test.ensure_index("goodbye"))
121116

122117
db.test.drop_indexes()
123118
self.assertEqual("goodbye_1",
124-
db.test.ensure_index("goodbye", ASCENDING))
125-
self.assertEqual(None, db.test.ensure_index("goodbye", ASCENDING))
119+
db.test.ensure_index("goodbye"))
120+
self.assertEqual(None, db.test.ensure_index("goodbye"))
126121

127122
db.test.drop_index("goodbye_1")
128123
self.assertEqual("goodbye_1",
129-
db.test.ensure_index("goodbye", ASCENDING))
130-
self.assertEqual(None, db.test.ensure_index("goodbye", ASCENDING))
124+
db.test.ensure_index("goodbye"))
125+
self.assertEqual(None, db.test.ensure_index("goodbye"))
131126

132127
db.drop_collection("test")
133128
self.assertEqual("goodbye_1",
134-
db.test.ensure_index("goodbye", ASCENDING))
135-
self.assertEqual(None, db.test.ensure_index("goodbye", ASCENDING))
129+
db.test.ensure_index("goodbye"))
130+
self.assertEqual(None, db.test.ensure_index("goodbye"))
136131

137132
db_name = self.db.name()
138133
self.connection.drop_database(self.db.name())
139134
self.assertEqual("goodbye_1",
140-
db.test.ensure_index("goodbye", ASCENDING))
141-
self.assertEqual(None, db.test.ensure_index("goodbye", ASCENDING))
135+
db.test.ensure_index("goodbye"))
136+
self.assertEqual(None, db.test.ensure_index("goodbye"))
142137

143138
db.test.drop_index("goodbye_1")
144139
self.assertEqual("goodbye_1",
145-
db.test.create_index("goodbye", ASCENDING))
146-
self.assertEqual(None, db.test.ensure_index("goodbye", ASCENDING))
140+
db.test.create_index("goodbye"))
141+
self.assertEqual(None, db.test.ensure_index("goodbye"))
147142

148143
db.test.drop_index("goodbye_1")
149144
self.assertEqual("goodbye_1",
150-
db.test.ensure_index("goodbye", ASCENDING,
151-
ttl=1))
145+
db.test.ensure_index("goodbye", ttl=1))
152146
time.sleep(1.1)
153147
self.assertEqual("goodbye_1",
154-
db.test.ensure_index("goodbye", ASCENDING))
148+
db.test.ensure_index("goodbye"))
155149

156150
db.test.drop_index("goodbye_1")
157151
self.assertEqual("goodbye_1",
158-
db.test.create_index("goodbye", ASCENDING,
159-
ttl=1))
152+
db.test.create_index("goodbye", ttl=1))
160153
time.sleep(1.1)
161154
self.assertEqual("goodbye_1",
162-
db.test.ensure_index("goodbye", ASCENDING))
155+
db.test.ensure_index("goodbye"))
163156

164157
def test_index_on_binary(self):
165158
db = self.db
@@ -171,19 +164,19 @@ def test_index_on_binary(self):
171164
self.assertEqual(db.test.find({"bin": Binary("abc")})
172165
.explain()["nscanned"], 3)
173166

174-
db.test.create_index("bin", ASCENDING)
167+
db.test.create_index("bin")
175168
self.assertEqual(db.test.find({"bin": Binary("abc")})
176169
.explain()["nscanned"], 1)
177170

178171
def test_drop_index(self):
179172
db = self.db
180173
db.test.drop_indexes()
181-
db.test.create_index("hello", ASCENDING)
182-
name = db.test.create_index("goodbye", DESCENDING)
174+
db.test.create_index("hello")
175+
name = db.test.create_index("goodbye")
183176

184177
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
185178
.count(), 3)
186-
self.assertEqual(name, "goodbye_-1")
179+
self.assertEqual(name, "goodbye_1")
187180
db.test.drop_index(name)
188181
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
189182
.count(), 2)
@@ -195,13 +188,13 @@ def test_drop_index(self):
195188
.find({"ns": u"pymongo_test.test"})))
196189

197190
db.test.drop_indexes()
198-
db.test.create_index("hello", ASCENDING)
199-
name = db.test.create_index("goodbye", DESCENDING)
191+
db.test.create_index("hello")
192+
name = db.test.create_index("goodbye")
200193

201194
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
202195
.count(), 3)
203-
self.assertEqual(name, "goodbye_-1")
204-
db.test.drop_index([("goodbye", DESCENDING)])
196+
self.assertEqual(name, "goodbye_1")
197+
db.test.drop_index([("goodbye", ASCENDING)])
205198
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
206199
.count(), 2)
207200
self.assert_(SON([(u"name", u"hello_1"),
@@ -217,7 +210,7 @@ def test_index_info(self):
217210
self.assertEqual(len(db.test.index_information()), 1)
218211
self.assert_("_id_" in db.test.index_information())
219212

220-
db.test.create_index("hello", ASCENDING)
213+
db.test.create_index("hello")
221214
self.assertEqual(len(db.test.index_information()), 2)
222215
self.assertEqual(db.test.index_information()["hello_1"],
223216
[("hello", ASCENDING)])
@@ -421,15 +414,15 @@ def test_unique_index(self):
421414
db = self.db
422415

423416
db.drop_collection("test")
424-
db.test.create_index("hello", ASCENDING)
417+
db.test.create_index("hello")
425418

426419
db.test.save({"hello": "world"})
427420
db.test.save({"hello": "mike"})
428421
db.test.save({"hello": "world"})
429422
self.failIf(db.error())
430423

431424
db.drop_collection("test")
432-
db.test.create_index("hello", ASCENDING, unique=True)
425+
db.test.create_index("hello", unique=True)
433426

434427
db.test.save({"hello": "world"})
435428
db.test.save({"hello": "mike"})
@@ -446,7 +439,7 @@ def test_index_on_subfield(self):
446439
self.failIf(db.error())
447440

448441
db.drop_collection("test")
449-
db.test.create_index("hello.a", ASCENDING, unique=True)
442+
db.test.create_index("hello.a", unique=True)
450443

451444
db.test.insert({"hello": {"a": 4, "b": 5}})
452445
db.test.insert({"hello": {"a": 7, "b": 2}})
@@ -490,7 +483,7 @@ def test_upsert(self):
490483
def test_safe_update(self):
491484
db = self.db
492485
db.drop_collection("test")
493-
db.test.create_index("x", ASCENDING)
486+
db.test.create_index("x")
494487

495488
a = {"x": 5}
496489
db.test.insert(a)
@@ -504,7 +497,7 @@ def test_safe_update(self):
504497
def test_safe_save(self):
505498
db = self.db
506499
db.drop_collection("test")
507-
db.test.create_index("hello", ASCENDING, unique=True)
500+
db.test.create_index("hello", unique=True)
508501

509502
db.test.save({"hello": "world"})
510503
db.test.save({"hello": "world"})

test/test_cursor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_hint(self):
6060
db.test.find({"num": 17, "foo": 17})
6161
.hint([("foo", ASCENDING)]).explain)
6262

63-
index = db.test.create_index("num", ASCENDING)
63+
index = db.test.create_index("num")
6464

6565
spec = [("num", ASCENDING)]
6666
self.assertEqual(db.test.find({}).explain()["cursor"], "BasicCursor")
@@ -192,7 +192,6 @@ def test_sort(self):
192192
db = self.db
193193

194194
self.assertRaises(TypeError, db.test.find().sort, 5)
195-
self.assertRaises(TypeError, db.test.find().sort, "hello")
196195
self.assertRaises(ValueError, db.test.find().sort, [])
197196
self.assertRaises(TypeError, db.test.find().sort, [], ASCENDING)
198197
self.assertRaises(TypeError, db.test.find().sort,
@@ -209,6 +208,8 @@ def test_sort(self):
209208

210209
asc = [i["x"] for i in db.test.find().sort("x", ASCENDING)]
211210
self.assertEqual(asc, range(10))
211+
asc = [i["x"] for i in db.test.find().sort("x")]
212+
self.assertEqual(asc, range(10))
212213
asc = [i["x"] for i in db.test.find().sort([("x", ASCENDING)])]
213214
self.assertEqual(asc, range(10))
214215

0 commit comments

Comments
 (0)