Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of 'get_version' #189

Merged
merged 18 commits into from
Oct 4, 2016
Merged

Implementation of 'get_version' #189

merged 18 commits into from
Oct 4, 2016

Conversation

oddjobz
Copy link
Contributor

@oddjobz oddjobz commented Oct 4, 2016

This is based on the PyMongo call available in the v3.4 API and is pretty critical for anything that makes use of Mongo's versioning feature. I've added an additional index to ensure you can scan both-ways in the version list, and I've added a test-case to exercise the new code. The PyMongo implementation also allows random (potentially/probably non-indexed) attributes to be included in the search, I have deliberately omitted this feature as it's "likely" to introduce unpredictable performance issues if it's ever used. (although it anyone needs it, adding is relatively trivial) I don't necessarily "like" the indexing scheme, but it mirrors the PyMongo implementation for compatibility.

Copy link
Contributor

@IlyaSkriblovsky IlyaSkriblovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I've added a couple of comments. They are mostly minor except StopIteration one.

@@ -1,6 +1,18 @@
Changelog
=========

Unreleased
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are changed NEWS.rst today by adding unreleased 16.3.0 version. Could you please merge current master branch of txmongo into your branch and resolve the merge conflict?

index = 9
while True:
try:
doc = yield self.gfs.get_last_version('world')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, don't mix tabs and spaces in indents :)

@@ -138,6 +141,45 @@ def get(self, file_id):
defer.returnValue(GridOut(self.__collection, doc))

@defer.inlineCallbacks
def get_version(self, filename=None, version=-1, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have decided to omit custom filtering feature, lets drop **kwargs here too in order to not make an illusion that it will work as in PyMongo


try:
defer.returnValue(GridOut(self.__collection, cursor[0]))
except StopIteration:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yield find() will return plain list, not cursor object like in PyMongo. So IndexError should be cought here, not StopIteration. Or better just checking:

if cursor:
    defer.returnValue(...)
raise NoFile(...)

Gareth Bult and others added 7 commits October 4, 2016 12:08
@IlyaSkriblovsky
Copy link
Contributor

Sorry, I've misguided you about the way to drop database. Currently, there is no any drop_database method in TxMongo. You may drop your database with self.db.command("dropDatabase").

@oddjobz
Copy link
Contributor Author

oddjobz commented Oct 4, 2016

Ok, I was a little confused by the drop issue, looks ok now. Indeed the new version of the test code exercises the StopIteration fix .. :) .. how's it look?

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.001%) to 95.553% when pulling 7dd0555 on oddjobz:master into eee4bb3 on twisted:master.

Copy link
Contributor

@IlyaSkriblovsky IlyaSkriblovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a couple more comments, please take a look :)

yield self.gfs.delete(doc._id)
except NoFile:
break
try:
Copy link
Contributor

@IlyaSkriblovsky IlyaSkriblovsky Oct 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this try block? If we got out of while True loop, it certainly mean that yield self.gfs.get_last_version('world') raises the NoFile. Why re-check?

I guess you have added this block to check StopIteration fix, but it doesn't actually test it because it calls get_last_version, not get_version :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that was from me trying to figure out why my "drop" was failing (silently), but you're right, it's not needed. (removed)

return

@defer.inlineCallbacks
def test_GFSVersion_last(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems to be calling get_last_version instead of get_version(..., -1) too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do have 1 last "last" call left, call it "belt and braces", not strictly needed but nice to make sure get_last and get_version(-1) give the same result .. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok, I just didn't noticed that this test is actually duplicated: one calls get_version and another calls get_last_version

@IlyaSkriblovsky
Copy link
Contributor

I've updated my review. Just a couple of minor comments about testing code :)

@oddjobz
Copy link
Contributor Author

oddjobz commented Oct 4, 2016

Ok, comment should read ".. remove get_last_version" .. should look better now ...

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling 4711394 on oddjobz:master into eee4bb3 on twisted:master.

@@ -62,6 +62,9 @@ def __init__(self, database, collection="fs"):
self.__indexes_created_defer = defer.DeferredList([
self.__files.create_index(
filter.sort(ASCENDING("filesname") + ASCENDING("uploadDate"))),
self.__files.create_index(
filter.sort(ASCENDING("filesname") + DESCENDING("uploadDate"))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After careful reading I've noticed that second index is redundant. {filename: 1, uploadDate: 1} index will cover both find({filename: ...}).sort({uploadDate: 1}) and find({filename: ...}).sort({uploadDate: -1}).

Copy link
Contributor Author

@oddjobz oddjobz Oct 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, in my defence I was just copying other people's code with regards to index creation, and if one of those indexes is obsolete, it would imply the current method for specifying index creation is also rather redundant. I've done a little reading too now ( :) ) so here's another shot with one index, with a slightly more direct specification, and also I modified the test so it runs under both Python2 "and" Python3.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No-no, I didn't meant you are specifying index incorrectly. Specification was good, just ASC+DESC index was redundant. Please see my comment below. Sorry for confusion, I just used mongoshell's notion in my previous comment. It doesn't work well in Python.

@IlyaSkriblovsky
Copy link
Contributor

The last comment, I beleive :)

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling 739bee3 on oddjobz:master into eee4bb3 on twisted:master.

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling 739bee3 on oddjobz:master into eee4bb3 on twisted:master.

@IlyaSkriblovsky
Copy link
Contributor

Nice catch about Py3 compatibility!

But please, revert back index creation code:

self.__files.create_index(
    filter.sort(ASCENDING("filesname") + ASCENDING("uploadDate")))

create_index({'filesname':1, 'uploadDate':1} is not correct because python's dicts do not guarantee key order, so you may get {uploadData:1, filesname:1} index instead. filter.sort(ASCENDING(...) + ASCENDING(...)) is the correct way to specify indexes, though a little bit verbose.

Still you are correctly removed ASCENDING(...) + DESCENDING(...) index, thanks.

@@ -60,7 +60,7 @@ def __init__(self, database, collection="fs"):
self.__files = self.__collection.files
self.__chunks = self.__collection.chunks
self.__indexes_created_defer = defer.DeferredList([
self.__files.create_index(
self.__files.create_index(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are probably already tired of my grumling, but could you please fix indenting of this line? ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm getting tired, I should have paid more attention to the indent before committing .. ;)

@oddjobz
Copy link
Contributor Author

oddjobz commented Oct 4, 2016

Good point, reverted .. that's still a horribly clunky way to define an index .. would be nice to support something a little more intuitive .. maybe [{'field1':1},{'field2':-1}] .. the way the Mongo guys implemented index spec's is a little unfortunate with regards to Python dict's ... :(

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.1%) to 95.701% when pulling 739bee3 on oddjobz:master into eee4bb3 on twisted:master.

@oddjobz
Copy link
Contributor Author

oddjobz commented Oct 4, 2016

Ok, turns out this would be a valid index spec;

create_index([('filesname':1),('uploadDate':1)], name='version_index')

I would submit this is 'clearer' in terms of what it's doing ... (?)

@IlyaSkriblovsky
Copy link
Contributor

You can specify indexes like this: [("field1", 1), ("field2", 1)] and it will work. filter.sort and filter.ASCENDING are internally converted into exactly this. That verbose notation is just a traditional way of specifying indexes.

I suggest you to leave it as it was, with all these filter.sort and ASCENDING, because it turns out that this PR doesn't really need to change anything about indexes. It may probably be redone in clearer way by another pull request.

Sorry, but it seems that your last commit actually broke indenting of another line instead of fixing :) Both indexes are created inside DeferredList constructor, please make them indented one level more than line with DeferredList call. Thanks!

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling e89d93c on oddjobz:master into eee4bb3 on twisted:master.

@oddjobz
Copy link
Contributor Author

oddjobz commented Oct 4, 2016

Ok, how's that?

Copy link
Contributor

@IlyaSkriblovsky IlyaSkriblovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Now I'm happy with this

@oddjobz
Copy link
Contributor Author

oddjobz commented Oct 4, 2016

:)

@IlyaSkriblovsky
Copy link
Contributor

Let's wait for results of automated testing by Travis

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling 47132dc on oddjobz:master into eee4bb3 on twisted:master.

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling 47132dc on oddjobz:master into eee4bb3 on twisted:master.

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling 47132dc on oddjobz:master into eee4bb3 on twisted:master.

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling 47132dc on oddjobz:master into eee4bb3 on twisted:master.

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.03%) to 95.582% when pulling 47132dc on oddjobz:master into eee4bb3 on twisted:master.

@coveralls
Copy link

coveralls commented Oct 4, 2016

Coverage Status

Coverage increased (+0.02%) to 95.576% when pulling 47132dc on oddjobz:master into eee4bb3 on twisted:master.

@IlyaSkriblovsky IlyaSkriblovsky merged commit 6225f42 into twisted:master Oct 4, 2016
@IlyaSkriblovsky
Copy link
Contributor

Thanks for your contribution!

@psi29a
Copy link
Contributor

psi29a commented Oct 5, 2016

Yes, thank you very much and congratulations! :D

IlyaSkriblovsky added a commit that referenced this pull request Oct 22, 2016
additional index was removed after discussion in #189
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants