Skip to content

Create a JSON decoder subclass with a hook for pseudo types. - #4585

Merged
Tryneus merged 9 commits into
rethinkdb:nextfrom
asakatida:next
Aug 31, 2015
Merged

Tryneus merged 9 commits into
rethinkdb:nextfrom
asakatida:next

Conversation

@asakatida

Copy link
Copy Markdown

This is my proposed solution for #3506 and #4569

@danielmewes

Copy link
Copy Markdown
Member

Very cool. Thanks for the pull request @grandquista!
We're going to take a look at this soon.

@mlucy

mlucy commented Jul 27, 2015

Copy link
Copy Markdown
Member

@danielmewes --should this be in a milestone?

@danielmewes

Copy link
Copy Markdown
Member

2.1.x I think once we have that. Putting in 2.1-polish for now.

@danielmewes danielmewes added this to the 2.1-polish milestone Jul 27, 2015
@deontologician

Copy link
Copy Markdown
Contributor

Tangentially related: #1016

@asakatida

Copy link
Copy Markdown
Author

#1016 looks like a duplicate of #3506. The proposal in #1016 seems to ask for #3506 to be done on the server.

@danielmewes

Copy link
Copy Markdown
Member

They are similar, but #1016 would provide a more convenient interface for specifying individual additional types, that would also work across drivers (not just on Python). I think the main work for #1016 would be on the client, but the server might need some minor additions as well to detect the fact that a certain object is actually a user-defined pseudo type.

@danielmewes

Copy link
Copy Markdown
Member

I'm getting "Unexpected response received" on many (if not all) queries:

Python 2.7.3 (default, Jun 22 2015, 19:33:41) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rethinkdb as r
>>> r.connect().repl()
<rethinkdb.net.DefaultConnection object at 0x7f12a1b75a10>
>>> r.expr(1).run()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "rethinkdb/ast.py", line 118, in run
    return c._start(self, **global_optargs)
  File "rethinkdb/net.py", line 565, in _start
    return self._instance.run_query(q, global_optargs.get('noreply', False))
  File "rethinkdb/net.py", line 426, in run_query
    res = self._read_response(query.token)
  File "rethinkdb/net.py", line 475, in _read_response
    raise RqlDriverError("Unexpected response received.")
rethinkdb.errors.RqlDriverError: Unexpected response received.
>>>

Any ideas @grandquista ?

(Edit: Replaced example by a simpler query)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This code needs to handle the case where the result is not for a cursor (e.g. r.expr(1)).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have a patch to handle the first call to _read_response.

@danielmewes

Copy link
Copy Markdown
Member

Never mind, found the problem I think (I added a comment in the code).

@danielmewes

Copy link
Copy Markdown
Member

I hacked in a quick fix, and ran the benchmark from #4569 (comment) .
Even with the default JSON deserializer, these changes make loading 300,000 documents more than twice as fast (7.1 seconds vs. 14.4 seconds)
This is really awesome @grandquista :-)

@Tryneus I think this might be next after migration?

Comment thread drivers/python/rethinkdb/ast.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be convert_time right? At least it's called by that name in line 728

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, that would be consistent with how I was naming the other convert methods.

@danielmewes

Copy link
Copy Markdown
Member

Actually my measurements were with a slightly modified version of the benchmark from #4569 (comment) that used numeric timestamps and not ReQL time objects in the table. With real time objects, your patch is still much faster though: ~18 seconds instead of ~25 seconds.

@deontologician

Copy link
Copy Markdown
Contributor

I'm excited, this is potentially fixes several longstanding issues. Nice work! (I've looked over the patch as well, and the only things I noticed Daniel already mentioned)

@danielmewes danielmewes modified the milestones: 2.1-polish, 2.1.x Aug 11, 2015
@Tryneus

Tryneus commented Aug 31, 2015

Copy link
Copy Markdown
Member

Ok, finally got around to this. Looks good for the most part, although it doesn't change the other net.py implementations - I can take care of that when this is merged, though.

Tryneus added a commit that referenced this pull request Aug 31, 2015
Create a JSON decoder subclass with a hook for pseudo types.
@Tryneus
Tryneus merged commit a91f5e4 into rethinkdb:next Aug 31, 2015
@Tryneus

Tryneus commented Aug 31, 2015

Copy link
Copy Markdown
Member

Thanks for all the help, @grandquista, merged!

@danielmewes

Copy link
Copy Markdown
Member

@Tryneus once you are done transferring this to the other connection backends, please also backport this to v2.1.x.

@Tryneus

Tryneus commented Sep 1, 2015

Copy link
Copy Markdown
Member

The changes were applied to the v2.1.x branch in commit 097b05b.

@danielmewes

Copy link
Copy Markdown
Member

For reference, here's an example for using UltraJSON in the driver (https://github.com/esnme/ultrajson):

import rethinkdb as r
import ujson

class UjsonDecoder(object):
    def __init__(self, reql_format_opts={}):
        pass
    def decode(self, s):
        return ujson.loads(s)

def create_decoder(format_opts):
    return UjsonDecoder(format_opts)

conn = r.connect()
conn._get_json_decoder = create_decoder

# `conn` now uses UltraJSON to parse server responses

Note that UltraJSON doesn't currently allow registering a hook for processing pseudo types. So RethinkDB pseudo types (binary data, date/time values) will not be converted into native Python types in this case.

@danielmewes

Copy link
Copy Markdown
Member

The change was included in the latest Python driver release, "2.1.0.post1"

@sontek

sontek commented Sep 10, 2015

Copy link
Copy Markdown
Contributor

@danielmewes Seems like an odd API to override a private method on the connection, could it be exposed as a public API?

@coffeemug

Copy link
Copy Markdown
Contributor

@sontek -- I think we should use the API you proposed originally:

# Specify default encoder/decoder on the connection
r.connect(..., json_encoder=ujon.loads, json_decoder=ujson.dumps)

# Override for a particular query
query.run(..., json_encoder=jon.loads, json_decoder=json.dumps)

We didn't add it yet because the existing version is pretty simple, and we wanted to get it out fast. I can open a new issue for this if people think the API proposed above is reasonable.

@danielmewes

Copy link
Copy Markdown
Member

For reference: @grandquista put an implementation of the simplified API up as a pull request: #4825

We're planning to include that with RethinkDB 2.2.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is making a mutable global object at import time. This should be something like this instead:

def __init__(self, reql_format_opts=None):
    self.reql_form_opts = reql_format_opts or {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cc @danielmewes @grandquista

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

>>> from rethinkdb.ast import ReQLDecoder
>>> one = ReQLDecoder()
>>> two = ReQLDecoder()
>>> one.reql_format_opts['boom'] = 'shaka'
>>> one.reql_format_opts
{'boom': 'shaka'}
>>> two.reql_format_opts
{'boom': 'shaka'}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh good catch! Do you want to open a pull request? (otherwise I'm just going to copy over your patch, if that's ok).

@sontek

sontek commented Sep 14, 2015

Copy link
Copy Markdown
Contributor

I was able to shave off 50% of my time using this API:

# standard json
$ python foo_rethinkdb.py 
count is 164717
int query took 2213ms

# using rapidjson
$ python foo_rethinkdb.py 
count is 164717
int query took 1332ms

totalwindupflightsystems pushed a commit to totalwindupflightsystems/rethinkdb that referenced this pull request Aug 7, 2026
Create a JSON decoder subclass with a hook for pseudo types.
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.

7 participants