oleiade / py-elevator

py-elevator is a python client for Elevator, a Key-Value store written in Python and based on levelDB, allows high performance on-disk bulk read/write.
https://py-elevator.readthedocs.org/en/latest/
MIT License
13 stars 6 forks source link

Put/Get serializable objects #38

Open k4nar opened 10 years ago

k4nar commented 10 years ago

Both py-elevator and Elevator are using msgpack. One great thing about msgpack is that it allows fast and safe serialization of standard types. For example :

>>> import msgpack
>>> msgpack.dumps(42)
'*'
>>> msgpack.loads(_)
42
>>> msgpack.dumps([{'foo': ('bar', 1), 'bar': 42}, 'toto'])
'\x92\x82\xa3foo\x92\xa3bar\x01\xa3bar*\xa4toto'
>>> msgpack.loads(_)
[{'foo': ['bar', 1], 'bar': 42}, 'toto']
>>> _[0]
{'foo': ['bar', 1], 'bar': 42}

However, py-elevator complains if we put something else than a string :

>>> import pyelevator
>>> E = pyelevator.Elevator()
>>> E.Put('foo', 'bar')
>>> E.Put('key', ['foo', 12])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/yannick/.virtualenvs/onitu/lib/python2.7/site-packages/pyelevator/client.py", line 46, in Put
    self.send(self.db_uid, 'PUT', [key, value], *args, **kwargs)
  File "/home/yannick/.virtualenvs/onitu/lib/python2.7/site-packages/pyelevator/base.py", line 132, in send
    raise ELEVATOR_ERROR[header.err_code](header.err_msg)
TypeError: Unsupported value type : <type 'list'>

I don't see any reason not to handle serialization/deserialization of common types as msgpack is already used.

oleiade commented 10 years ago

I'll try to figure out something generic enough. While we're at implementing native client serialization, let's try to provide an internal interface to messages serialization too. :+1:

k4nar commented 10 years ago

Internal, like in Elevator ? I'm not sure to how that could be possible.

oleiade commented 10 years ago

Nope sorry I meant "privately throught the Client object, py-elevator side".

k4nar commented 10 years ago

Hum, not sure to see what you mean by that.

k4nar commented 10 years ago

I'm not sure how we should do this. I did a quick hack (https://github.com/onitu/py-elevator/commit/ee95784a181f0ed982ec19d9ae64433ff23136d2) in order to get it working, but serializing the message twice is not really elegant (and I'm not handling MGet and batches of course, but it wasn't the point).

k4nar commented 10 years ago

Now I've something which should be working pretty well (cf #48). I didn't find a way to make the serialization global, so I had to handle it in every function concerned.

If you find a more elegant approach, I'm all ears :) .