Merge lp://staging/~thisfred/u1db/iterate-over-list-of-dicts into lp://staging/u1db

Proposed by Eric Casteleijn
Status: Merged
Approved by: Eric Casteleijn
Approved revision: 372
Merged at revision: 366
Proposed branch: lp://staging/~thisfred/u1db/iterate-over-list-of-dicts
Merge into: lp://staging/u1db
Diff against target: 349 lines (+130/-69)
5 files modified
src/u1db_query.c (+74/-46)
u1db/query_parser.py (+25/-17)
u1db/tests/test_backends.py (+20/-0)
u1db/tests/test_query_parser.py (+10/-5)
u1db/tests/test_sqlite_backend.py (+1/-1)
To merge this branch: bzr merge lp://staging/~thisfred/u1db/iterate-over-list-of-dicts
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+117120@code.staging.launchpad.net

Commit message

Added support for indexing dictionaries in lists, so this becomes possible:

        doc = self.db.create_doc_from_json(
            '{"foo": [{"zap": [{"qux": "fnord"}, {"qux": "zombo"}]}]}')
        self.db.create_index('test-idx', 'foo.zap.qux')
        self.assertEqual([doc], self.db.get_from_index('test-idx', 'fnord'))
        self.assertEqual([doc], self.db.get_from_index('test-idx', 'zombo'))

Description of the change

Added support for indexing dictionaries in lists, so this becomes possible:

        doc = self.db.create_doc_from_json(
            '{"foo": [{"zap": [{"qux": "fnord"}, {"qux": "zombo"}]}]}')
        self.db.create_index('test-idx', 'foo.zap.qux')
        self.assertEqual([doc], self.db.get_from_index('test-idx', 'fnord'))
        self.assertEqual([doc], self.db.get_from_index('test-idx', 'zombo'))

To post a comment you must log in.
Revision history for this message
Samuele Pedroni (pedronis) wrote :

 + subfields = self.field.split('.')

I would move this to the constructor

255 + return extract_field(raw_doc, subfields)

+def extract_field(raw_doc, subfields):
206 + if not isinstance(raw_doc, dict):
207 + return []
208 + val = raw_doc.get(subfields.pop(0))

I would use a index argument and avoid the pop (especially pop(0)) and copy

216 + subfields = []
this line is not needed

same for these:
223 + if val is None:
224 + return []

line 209-210 cover that

368. By Eric Casteleijn

split in __init__

369. By Eric Casteleijn

split in __init__

370. By Eric Casteleijn

don't mutate list

Revision history for this message
Eric Casteleijn (thisfred) wrote :

Fixes pushed

371. By Eric Casteleijn

don't copy list around

372. By Eric Casteleijn

don't copy list around

Revision history for this message
Samuele Pedroni (pedronis) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
The diff is not available at this time. You can reload the page or download it.

Subscribers

People subscribed via source and target branches