Created
December 19, 2012 14:43
-
-
Save diyan/4337132 to your computer and use it in GitHub Desktop.
Monkey patch Couchbase Python client in order to work with data stored by .NET couchbase client.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Monkey patch Couchbase Python client in order to work with data stored by .NET couchbase client. | |
# http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-datastore-fields.html | |
# Both Python and .NET couchbase clients using fields in document metadata | |
# for storing type information which does not compatible with each other. | |
def get_patched_couchbase(): | |
from couchbase.constants import MemcachedConstants | |
import struct as original_struct | |
unpack_original = original_struct.unpack | |
def unpack_workaround(fmt, string): | |
if fmt == MemcachedConstants.GET_RES_FMT and string.encode('hex') == '00000112': | |
return unpack_original(fmt, '00000000'.decode('hex')) | |
else: | |
return unpack_original(fmt, string) | |
original_struct.unpack = unpack_workaround | |
from couchbase.client import Couchbase | |
return Couchbase | |
Couchbase = get_patched_couchbase() | |
couchbase = Couchbase('localhost:8091', 'admin', 'password') | |
bucket = couchbase['dev'] | |
rows = bucket.view('_design/testing/_view/all_by_types') | |
for row in rows: | |
print bucket.get(row['id']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment