The Transcoder interface may be used to provide custom value and key formats. Among the uses of the transcoder class, one may:
Implement compatibility with other client libraries
formatoption in the operations correspond to a flag value which is stored in the server as meta data along with the key. These flags are utilized by client libraries to determine how to interpret the value on the server (which is just a set of bytes) into a more complex and user-friendly type in the native client language. Some clients may have different ideas about which flags mean which value format, and some clients may use formats which are specific to that platform (for example,
FMT_PICKLEwhich is typically only native to Python objects). One may implement a custom transcoder class which understands a wider variety of types and flags
If storing large values, it may be handy to compress them before storing them on the server. This provides for lower network overhead and storage space on the server, at the expense of the computational overhead of compressing and decompressing objects. One may add extra flags to indicate a value has been compressed, and with which format.
Automatic conversion of different value types into custom classes
The built-in transcoder only uses native Python types. One may wish to interpret values as belong to user-defined classes.
Typically one would subclass the
Transcoder class, and
implement the needed methods, allowing for high efficient built-in
methods to perform the un-needed operations.
Note that the
Client does not
Transcoder object by default (however it internally uses
the same routines that the C-implemented
Transcoder does). Thus,
if no custom transcoding is needed, it is more efficient to set the
is the default.
- class couchbase_core.transcoder.Transcoder¶
Encode the key as a bytes object.
key – This is an object passed as a string key. There is no restriction on this type
a bytes object The default implementation encodes the key as UTF-8. On Python 2.x,
bytesis a synonym for
str. On Python 3.x,
strare distinct objects, in which one must first encode a string to a specific encoding
- encode_value(value, flags)¶
Encode the value into something meaningful
value (any) – A value. This may be a string or a complex python object.
format (any) – The format argument as passed to the mutator
A tuple of
valuemust be a
flagsmust be an integer type whose value does not exceed 32 bits
Convert the key from bytes into something else.
key (bytes) – The key, in the form of a bytearray
a string or other object your application will use The returned key must be hashable
The default implementation decodes the keys from UTF-8.
- decode_value(value, flags)¶
Decode the value from the raw bytes representation into something meaningful
value (bytes) – Raw bytes, as stored on the server
flags (int) – The flags for the value
Something meaningful to be used as a value within the application
- Guess the suitable format for the object specified.
New in version 1.1.0.
Used primarily if received a
FMT_AUTOfor the format parameter in one of the encode methods
value (object) – The value whose format should be guessed
An integer representing the guessed format.
Note that this function is provided as a convenience. It is not called by the Connection object
This function always succeeds