lanto03 / couchdb-python

Automatically exported from code.google.com/p/couchdb-python
Other
0 stars 0 forks source link

couchdb.design.ViewDefinition.sync_many doesn't sort views before group_by #218

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Make several ViewDefinitions, with at least two of them in the same design 
document.
2. Call ViewDefinition.sync_many (this is called by Flask-CouchDB's 
CouchDBManager's sync method)

What is the expected output? What do you see instead?

Expected Output:
- I would expect that views that belong to the same design document should be 
merged together, with one update call for each design document.

Actual Output:
- It makes a doc for each view (even if they're in the same design document), 
which ultimately leads to conflicts.

What version of the product are you using? On what operating system?
CouchDB 0.8

Please provide any additional information below.
This is happening because in sync_many, there is a itertools.groupby call that 
is supposed to combine these together, However, as mentioned here:
http://docs.python.org/2/library/itertools.html#itertools.groupby

"The operation of groupby() is similar to the uniq filter in Unix. It generates 
a break or new group every time the value of the key function changes (which is 
why it is usually necessary to have sorted the data using the same key 
function). That behavior differs from SQL’s GROUP BY which aggregates common 
elements regardless of their input order."

So, because there is no sort call beforehand, it does not group as you would 
expect it to.

The fix is to place this line:
views = sorted(views, key=attrgetter('design'))

right before:
for design, views in groupby(views, key=attrgetter('design')):

Diff:
--- venv/lib/python2.7/site-packages/couchdb/design.py  2013-01-29 
00:03:30.000000000 -0800
+++ 
/otherdirectory/venv/lib/python2.7/site-packages/couchdb/design.py  2013-01-28 
23:51:26.000000000 -0800
@@ -160,6 +160,7 @@
         """
         docs = []

+        views = sorted(views, key=attrgetter('design'))
         for design, views in groupby(views, key=attrgetter('design')):
             doc_id = '_design/%s' % design
             doc = db.get(doc_id, {'_id': doc_id})

I would have gladly have submitted a pull request, but it's sadly not github. I 
did however confirm that this works locally. If you need my actual views, let 
me know.

Original issue reported on code.google.com by j.goodno...@gmail.com on 29 Jan 2013 at 8:05

GoogleCodeExporter commented 8 years ago
Also, since this is showing up as a bug in an application I'm building and the 
fix is quick, getting this into pypi soon would be pretty sweet.

Thanks,

Johnny

Original comment by j.goodno...@gmail.com on 29 Jan 2013 at 8:08

GoogleCodeExporter commented 8 years ago
Sorry, I don't think your application is enough motivation for us to do quick 
release (particularly since we haven't done a release in quite a while). It 
would be useful if you can provide your patch with Mercurial metadata (via hg 
export). It seems you might be reluctant to use Mercurial, but it's really not 
hard.

Original comment by djc.ochtman on 29 Jan 2013 at 5:29

GoogleCodeExporter commented 8 years ago
Not a problem; I'll attach it later this evening.

On Tuesday, January 29, 2013, wrote:

Original comment by j.goodno...@gmail.com on 29 Jan 2013 at 6:01

GoogleCodeExporter commented 8 years ago
I've attached my patch. And I don't dislike Mercurial (I like git better, but I 
started with Mercurial); I just wish Google Code had a "pull request"-like 
mechanism such as what Github and Bitbucket has...and you can always host 
Mercurial projects at Bitbucket + Docs at Read The Docs :D.

Thanks!

Original comment by j.goodno...@gmail.com on 30 Jan 2013 at 7:35

Attachments:

GoogleCodeExporter commented 8 years ago
This issue was closed by revision c8a11a9a2a59.

Original comment by djc.ochtman on 18 Feb 2013 at 10:12

GoogleCodeExporter commented 8 years ago
Sorry, that took slightly longer than planned.

It would be really cool if you could contribute a test for this behavior, to 
make sure we don't regress it.

Original comment by djc.ochtman on 18 Feb 2013 at 10:13

GoogleCodeExporter commented 8 years ago
Sure; I'll look at the test code you already have and will try and submit
something this week.

On Monday, February 18, 2013, wrote:

Original comment by j.goodno...@gmail.com on 18 Feb 2013 at 10:15

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Attached is the test; thanks!

Original comment by j.goodno...@gmail.com on 4 Mar 2013 at 6:16

Attachments:

GoogleCodeExporter commented 8 years ago
Very nice, thank you! This will (finally) be released shortly.

Original comment by djc.ochtman on 25 Apr 2013 at 10:26