Skip to content

Commit

Permalink
Merge 484c47a into 6225f42
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSkriblovsky authored Oct 4, 2016
2 parents 6225f42 + 484c47a commit 6e0d1af
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
10 changes: 10 additions & 0 deletions docs/source/NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ Features
as it may create *very* variable conditions in terms of loading and timing. An additional index is
also added to facilitate bi-directional movement between versions. Additional Unit test for GFS also added.

API Changes
^^^^^^^^^^^

- ``Database.command()`` now takes ``codec_options`` argument.

Bugfixes
^^^^^^^^

- ``GridFS.get_last_version()`` was creating redundant index

Release 16.2.0 (2016-10-02)
---------------------------

Expand Down
10 changes: 9 additions & 1 deletion tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from io import BytesIO as StringIO

import os
from bson import objectid, timestamp
from bson import objectid, timestamp, SON
from pymongo.write_concern import WriteConcern
import txmongo
from txmongo import database, collection, filter as qf
Expand Down Expand Up @@ -371,4 +371,12 @@ def test_GridFsIndexesCreation(self):

yield gfs.indexes_created()

indexes = yield db.fs.files.index_information()
self.assertTrue(any(key["key"] == SON([("filename", 1), ("uploadDate", 1)])
for key in indexes.values()))

indexes = yield db.fs.chunks.index_information()
self.assertTrue(any(key["key"] == SON([("files_id", 1), ("n", 1)])
for key in indexes.values()))

yield conn.disconnect()
4 changes: 1 addition & 3 deletions txmongo/_gridfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, database, collection="fs"):
self.__chunks = self.__collection.chunks
self.__indexes_created_defer = defer.DeferredList([
self.__files.create_index(
filter.sort(ASCENDING("filesname") + ASCENDING("uploadDate"))),
filter.sort(ASCENDING("filename") + ASCENDING("uploadDate"))),
self.__chunks.create_index(
filter.sort(ASCENDING("files_id") + ASCENDING("n")), unique=True)
])
Expand Down Expand Up @@ -193,8 +193,6 @@ def get_last_version(self, filename):
.. versionadded:: 1.6
"""
self.__files.ensure_index(filter.sort(ASCENDING("filename") + DESCENDING("uploadDate")))

doc = yield self.__files.find_one({"filename": filename},
filter=filter.sort(DESCENDING("uploadDate")))
if doc is None:
Expand Down
8 changes: 6 additions & 2 deletions txmongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from bson import BSON, ObjectId
from bson.code import Code
from bson.son import SON
from bson.codec_options import CodecOptions
from pymongo.bulk import _Bulk, _COMMANDS, _merge_command
from pymongo.errors import InvalidName, BulkWriteError, InvalidOperation, OperationFailure
from pymongo.helpers import _check_write_command_response
Expand Down Expand Up @@ -971,7 +972,9 @@ def drop_indexes(self, **kwargs):

@defer.inlineCallbacks
def __index_information_3_0(self):
indexes_info = yield self._database.command("listIndexes", self.name)
codec = CodecOptions(document_class=SON)
indexes_info = yield self._database.command("listIndexes", self.name,
codec_options=codec)
assert indexes_info["cursor"]["id"] == 0
defer.returnValue(indexes_info["cursor"]["firstBatch"])

Expand All @@ -981,7 +984,8 @@ def index_information(self, **kwargs):
try:
raw = yield self.__index_information_3_0()
except OperationFailure:
raw = yield self._database.system.indexes.find({"ns": str(self)}, **kwargs)
raw = yield self._database.system.indexes.find({"ns": str(self)},
as_class=SON, **kwargs)

info = {}
for idx in raw:
Expand Down
6 changes: 4 additions & 2 deletions txmongo/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# found in the LICENSE file.

from bson.son import SON
from bson.codec_options import DEFAULT_CODEC_OPTIONS
from pymongo.helpers import _check_command_response
from twisted.internet import defer
from twisted.python.compat import unicode
Expand Down Expand Up @@ -54,14 +55,15 @@ def codec_options(self):

@timeout
@defer.inlineCallbacks
def command(self, command, value=1, check=True, allowable_errors=None, **kwargs):
def command(self, command, value=1, check=True, allowable_errors=None,
codec_options=DEFAULT_CODEC_OPTIONS, **kwargs):
if isinstance(command, (bytes, unicode)):
command = SON([(command, value)])
options = kwargs.copy()
options.pop("_deadline", None)
command.update(options)

ns = self["$cmd"]
ns = self["$cmd"].with_options(codec_options=codec_options)
response = yield ns.find_one(command, **kwargs)

if check:
Expand Down

0 comments on commit 6e0d1af

Please sign in to comment.