Deprecate ttl kwarg for index creation - PYTHON-394
This commit is contained in:
parent
340efb9c01
commit
71841113fa
@ -639,7 +639,7 @@ class Collection(common.BaseObject):
|
||||
"""
|
||||
return self.find().count()
|
||||
|
||||
def create_index(self, key_or_list, ttl=300, **kwargs):
|
||||
def create_index(self, key_or_list, cache_for=300, **kwargs):
|
||||
"""Creates an index on this collection.
|
||||
|
||||
Takes either a single key or a list of (key, direction) pairs.
|
||||
@ -676,13 +676,18 @@ class Collection(common.BaseObject):
|
||||
:Parameters:
|
||||
- `key_or_list`: a single key or a list of (key, direction)
|
||||
pairs specifying the index to create
|
||||
- `ttl` (optional): time window (in seconds) during which
|
||||
- `cache_for` (optional): time window (in seconds) during which
|
||||
this index will be recognized by subsequent calls to
|
||||
:meth:`ensure_index` - see documentation for
|
||||
:meth:`ensure_index` for details
|
||||
- `**kwargs` (optional): any additional index creation
|
||||
options (see the above list) should be passed as keyword
|
||||
arguments
|
||||
- `ttl` (deprecated): Use `cache_for` instead.
|
||||
|
||||
.. versionchanged:: 2.2.1+
|
||||
The `ttl` parameter has been deprecated to avoid confusion with
|
||||
TTL collections. Use `cache_for` instead.
|
||||
|
||||
.. versionchanged:: 2.2
|
||||
Removed deprecated argument: deprecated_unique
|
||||
@ -697,6 +702,12 @@ class Collection(common.BaseObject):
|
||||
|
||||
.. mongodoc:: indexes
|
||||
"""
|
||||
|
||||
if 'ttl' in kwargs:
|
||||
cache_for = kwargs.pop('ttl')
|
||||
warnings.warn("ttl is deprecated. Please use cache_for instead.",
|
||||
DeprecationWarning)
|
||||
|
||||
keys = helpers._index_list(key_or_list)
|
||||
index_doc = helpers._index_document(keys)
|
||||
|
||||
@ -718,11 +729,11 @@ class Collection(common.BaseObject):
|
||||
safe=True)
|
||||
|
||||
self.__database.connection._cache_index(self.__database.name,
|
||||
self.__name, name, ttl)
|
||||
self.__name, name, cache_for)
|
||||
|
||||
return name
|
||||
|
||||
def ensure_index(self, key_or_list, ttl=300, **kwargs):
|
||||
def ensure_index(self, key_or_list, cache_for=300, **kwargs):
|
||||
"""Ensures that an index exists on this collection.
|
||||
|
||||
Takes either a single key or a list of (key, direction) pairs.
|
||||
@ -768,12 +779,17 @@ class Collection(common.BaseObject):
|
||||
:Parameters:
|
||||
- `key_or_list`: a single key or a list of (key, direction)
|
||||
pairs specifying the index to create
|
||||
- `ttl` (optional): time window (in seconds) during which
|
||||
- `cache_for` (optional): time window (in seconds) during which
|
||||
this index will be recognized by subsequent calls to
|
||||
:meth:`ensure_index`
|
||||
- `**kwargs` (optional): any additional index creation
|
||||
options (see the above list) should be passed as keyword
|
||||
arguments
|
||||
- `ttl` (deprecated): Use `cache_for` instead.
|
||||
|
||||
.. versionchanged:: 2.2.1+
|
||||
The `ttl` parameter has been deprecated to avoid confusion with
|
||||
TTL collections. Use `cache_for` instead.
|
||||
|
||||
.. versionchanged:: 2.2
|
||||
Removed deprecated argument: deprecated_unique
|
||||
@ -794,7 +810,7 @@ class Collection(common.BaseObject):
|
||||
|
||||
if not self.__database.connection._cached(self.__database.name,
|
||||
self.__name, name):
|
||||
return self.create_index(key_or_list, ttl, **kwargs)
|
||||
return self.create_index(key_or_list, cache_for, **kwargs)
|
||||
return None
|
||||
|
||||
def drop_indexes(self):
|
||||
|
||||
@ -320,11 +320,11 @@ class Connection(common.BaseObject):
|
||||
index in cache[dbname][coll] and
|
||||
now < cache[dbname][coll][index])
|
||||
|
||||
def _cache_index(self, database, collection, index, ttl):
|
||||
def _cache_index(self, database, collection, index, cache_for):
|
||||
"""Add an index to the index cache for ensure_index operations.
|
||||
"""
|
||||
now = datetime.datetime.utcnow()
|
||||
expire = datetime.timedelta(seconds=ttl) + now
|
||||
expire = datetime.timedelta(seconds=cache_for) + now
|
||||
|
||||
if database not in self.__index_cache:
|
||||
self.__index_cache[database] = {}
|
||||
|
||||
@ -281,9 +281,10 @@ class MasterSlaveConnection(BaseObject):
|
||||
return self.__master._cached(database_name,
|
||||
collection_name, index_name)
|
||||
|
||||
def _cache_index(self, database_name, collection_name, index_name, ttl):
|
||||
def _cache_index(self, database_name, collection_name,
|
||||
index_name, cache_for):
|
||||
return self.__master._cache_index(database_name, collection_name,
|
||||
index_name, ttl)
|
||||
index_name, cache_for)
|
||||
|
||||
def _purge_index(self, database_name,
|
||||
collection_name=None, index_name=None):
|
||||
|
||||
@ -454,11 +454,11 @@ class ReplicaSetConnection(common.BaseObject):
|
||||
index in cache[dbname][coll] and
|
||||
now < cache[dbname][coll][index])
|
||||
|
||||
def _cache_index(self, dbase, collection, index, ttl):
|
||||
def _cache_index(self, dbase, collection, index, cache_for):
|
||||
"""Add an index to the index cache for ensure_index operations.
|
||||
"""
|
||||
now = datetime.datetime.utcnow()
|
||||
expire = datetime.timedelta(seconds=ttl) + now
|
||||
expire = datetime.timedelta(seconds=cache_for) + now
|
||||
|
||||
if dbase not in self.__index_cache:
|
||||
self.__index_cache[dbase] = {}
|
||||
@ -815,7 +815,7 @@ class ReplicaSetConnection(common.BaseObject):
|
||||
if member:
|
||||
member.pool.discard_socket(sock_info)
|
||||
raise ConnectionFailure("%s:%d: %s" % (host[0], host[1], str(why)))
|
||||
|
||||
|
||||
if member and sock_info:
|
||||
member.pool.maybe_return_socket(sock_info)
|
||||
|
||||
|
||||
@ -186,23 +186,40 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
db.test.drop_index("goodbye_1")
|
||||
self.assertEqual("goodbye_1",
|
||||
db.test.ensure_index("goodbye", ttl=1))
|
||||
db.test.ensure_index("goodbye", cache_for=1))
|
||||
time.sleep(1.1)
|
||||
self.assertEqual("goodbye_1",
|
||||
db.test.ensure_index("goodbye"))
|
||||
|
||||
db.test.drop_index("goodbye_1")
|
||||
self.assertEqual("goodbye_1",
|
||||
db.test.create_index("goodbye", ttl=1))
|
||||
db.test.create_index("goodbye", cache_for=1))
|
||||
time.sleep(1.1)
|
||||
self.assertEqual("goodbye_1",
|
||||
db.test.ensure_index("goodbye"))
|
||||
# Make sure the expiration time is updated.
|
||||
self.assertEqual(None,
|
||||
db.test.ensure_index("goodbye"))
|
||||
|
||||
# Clean up indexes for later tests
|
||||
db.test.drop_indexes()
|
||||
|
||||
def test_deprecated_ttl_index_kwarg(self):
|
||||
db = self.db
|
||||
|
||||
# In Python 2.6+ we could use the catch_warnings context
|
||||
# manager to test this warning nicely. As we can't do that
|
||||
# we must test raising errors before the ignore filter is applied.
|
||||
warnings.simplefilter("error", DeprecationWarning)
|
||||
self.assertRaises(DeprecationWarning, lambda:
|
||||
db.test.ensure_index("goodbye", ttl=10))
|
||||
warnings.resetwarnings()
|
||||
warnings.simplefilter("ignore")
|
||||
|
||||
self.assertEqual("goodbye_1",
|
||||
db.test.ensure_index("goodbye", ttl=10))
|
||||
self.assertEqual(None, db.test.ensure_index("goodbye"))
|
||||
|
||||
def test_ensure_unique_index_threaded(self):
|
||||
coll = self.db.test_unique_threaded
|
||||
coll.drop()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user