async def put(self, robj, return_body=True): bucket = robj.bucket req = riak_pb.RpbPutReq() if return_body: req.return_body = 1 req.bucket = str_to_bytes(bucket.name) self._add_bucket_type(req, bucket.bucket_type) if robj.key: req.key = str_to_bytes(robj.key) if robj.vclock: req.vclock = robj.vclock.encode('binary') self._encode_content(robj, req.content) msg_code, resp = await self._request(messages.MSG_CODE_PUT_REQ, req, messages.MSG_CODE_PUT_RESP) if resp is not None: if resp.HasField('key'): robj.key = bytes_to_str(resp.key) if resp.HasField('vclock'): robj.vclock = VClock(resp.vclock, 'binary') if resp.content: self._decode_contents(resp.content, robj) elif not robj.key: raise RiakError("missing response object") return robj
def _parse_body(self, robj, response, expected_statuses): """ Parse the body of an object response and populate the object. """ # If no response given, then return. if response is None: return None status, headers, data = response # Check if the server is down(status==0) if not status: m = 'Could not contact Riak Server: http://{0}:{1}!'.format( self._node.host, self._node.http_port) raise RiakError(m) # Make sure expected code came back self.check_http_code(status, expected_statuses) if 'x-riak-vclock' in headers: robj.vclock = VClock(headers['x-riak-vclock'], 'base64') # If 404(Not Found), then clear the object. if status == 404: robj.siblings = [] return None # If 201 Created, we need to extract the location and set the # key on the object. elif status == 201: robj.key = headers['location'].strip().split('/')[-1] # If 300(Siblings), apply the siblings to the object elif status == 300: ctype, params = parse_header(headers['content-type']) if ctype == 'multipart/mixed': if six.PY3: data = bytes_to_str(data) boundary = re.compile('\r?\n--%s(?:--)?\r?\n' % re.escape(params['boundary'])) parts = [message_from_string(p) for p in re.split(boundary, data)[1:-1]] robj.siblings = [self._parse_sibling(RiakContent(robj), part.items(), part.get_payload()) for part in parts] # Invoke sibling-resolution logic if robj.resolver is not None: robj.resolver(robj) return robj else: raise Exception('unexpected sibling response format: {0}'. format(ctype)) robj.siblings = [self._parse_sibling(RiakContent(robj), headers.items(), data)] return robj
def decode_put(self, robj, resp): if resp is not None: if resp.HasField('key'): robj.key = bytes_to_str(resp.key) if resp.HasField("vclock"): robj.vclock = VClock(resp.vclock, 'binary') if resp.content: self.decode_contents(resp.content, robj) elif not robj.key: raise RiakError("missing response object") return robj
def decode_get(self, robj, resp): if resp is not None: if resp.HasField('vclock'): robj.vclock = VClock(resp.vclock, 'binary') # We should do this even if there are no contents, i.e. # the object is tombstoned self.decode_contents(resp.content, robj) else: # "not found" returns an empty message, # so let's make sure to clear the siblings robj.siblings = [] return robj
def put(self, robj, w=None, dw=None, pw=None, return_body=True, if_none_match=False, timeout=None): """ Serialize get request and deserialize response """ bucket = robj.bucket req = riak_pb.RpbPutReq() if w: req.w = self._encode_quorum(w) if dw: req.dw = self._encode_quorum(dw) if self.quorum_controls() and pw: req.pw = self._encode_quorum(pw) if return_body: req.return_body = 1 if if_none_match: req.if_none_match = 1 if self.client_timeouts() and timeout: req.timeout = timeout req.bucket = bucket.name if robj.key: req.key = robj.key if robj.vclock: req.vclock = robj.vclock.encode('binary') self._encode_content(robj, req.content) msg_code, resp = self._request(MSG_CODE_PUT_REQ, req, MSG_CODE_PUT_RESP) if resp is not None: if resp.HasField('key'): robj.key = resp.key if resp.HasField("vclock"): robj.vclock = VClock(resp.vclock, 'binary') if resp.content: self._decode_contents(resp.content, robj) elif not robj.key: raise RiakError("missing response object") return robj
async def put(self, robj, w=None, dw=None, pw=None, return_body=True, if_none_match=False, timeout=None): bucket = robj.bucket req = riak_kv_pb2.RpbPutReq() if w: req.w = self._encode_quorum(w) if dw: req.dw = self._encode_quorum(dw) if pw: req.pw = self._encode_quorum(pw) if return_body: req.return_body = 1 if if_none_match: req.if_none_match = 1 if timeout: req.timeout = timeout req.bucket = str_to_bytes(bucket.name) self._add_bucket_type(req, bucket.bucket_type) if robj.key: req.key = str_to_bytes(robj.key) if robj.vclock: req.vclock = robj.vclock.encode('binary') self._encode_content(robj, req.content) msg_code, resp = await self._request(messages.MSG_CODE_PUT_REQ, req, messages.MSG_CODE_PUT_RESP) if resp is not None: if resp.HasField('key'): robj.key = bytes_to_str(resp.key) if resp.HasField('vclock'): robj.vclock = VClock(resp.vclock, 'binary') if resp.content: self._decode_contents(resp.content, robj) elif not robj.key: raise RiakError("missing response object") return robj
def get(self, robj, r=None, pr=None, timeout=None, basic_quorum=None, notfound_ok=None): """ Serialize get request and deserialize response """ bucket = robj.bucket req = riak_pb.RpbGetReq() if r: req.r = self._encode_quorum(r) if self.quorum_controls(): if pr: req.pr = self._encode_quorum(pr) if basic_quorum is not None: req.basic_quorum = basic_quorum if notfound_ok is not None: req.notfound_ok = notfound_ok if self.client_timeouts() and timeout: req.timeout = timeout if self.tombstone_vclocks(): req.deletedvclock = True req.bucket = str_to_bytes(bucket.name) self._add_bucket_type(req, bucket.bucket_type) req.key = str_to_bytes(robj.key) msg_code, resp = self._request(MSG_CODE_GET_REQ, req, MSG_CODE_GET_RESP) if resp is not None: if resp.HasField('vclock'): robj.vclock = VClock(resp.vclock, 'binary') # We should do this even if there are no contents, i.e. # the object is tombstoned self._decode_contents(resp.content, robj) else: # "not found" returns an empty message, # so let's make sure to clear the siblings robj.siblings = [] return robj
async def get(self, robj, r=None, pr=None, timeout=None, basic_quorum=None, notfound_ok=None): ''' Serialize get request and deserialize response ''' bucket = robj.bucket req = riak_kv_pb2.RpbGetReq() if r: req.r = self._encode_quorum(r) if pr: req.pr = self._encode_quorum(pr) if basic_quorum is not None: req.basic_quorum = basic_quorum if notfound_ok is not None: req.notfound_ok = notfound_ok if timeout: req.timeout = timeout req.deletedvclock = True req.bucket = bucket.name.encode() self._add_bucket_type(req, bucket.bucket_type) req.key = robj.key.encode() msg_code, resp = await self._request(messages.MSG_CODE_GET_REQ, req, messages.MSG_CODE_GET_RESP) if resp is not None: if resp.HasField('vclock'): robj.vclock = VClock(resp.vclock, 'binary') # We should do this even if there are no contents, i.e. # the object is tombstoned self._decode_contents(resp.content, robj) else: # "not found" returns an empty message, # so let's make sure to clear the siblings robj.siblings = [] return robj
def get(self, robj, r=None, pr=None, timeout=None): """ Serialize get request and deserialize response """ bucket = robj.bucket req = riak_pb.RpbGetReq() if r: req.r = self._encode_quorum(r) if self.quorum_controls() and pr: req.pr = self._encode_quorum(pr) if self.client_timeouts() and timeout: req.timeout = timeout if self.tombstone_vclocks(): req.deletedvclock = 1 req.bucket = bucket.name req.key = robj.key msg_code, resp = self._request(MSG_CODE_GET_REQ, req, MSG_CODE_GET_RESP) # TODO: support if_modified flag if resp is not None: if resp.HasField('vclock'): robj.vclock = VClock(resp.vclock, 'binary') # We should do this even if there are no contents, i.e. # the object is tombstoned self._decode_contents(resp.content, robj) else: # "not found" returns an empty message, # so let's make sure to clear the siblings robj.siblings = [] return robj
async def get(self, robj): ''' Serialize get request and deserialize response ''' bucket = robj.bucket req = riak_pb.RpbGetReq() req.bucket = bucket.name.encode() self._add_bucket_type(req, bucket.bucket_type) req.key = robj.key.encode() msg_code, resp = await self._request(messages.MSG_CODE_GET_REQ, req, messages.MSG_CODE_GET_RESP) if resp is not None: if resp.HasField('vclock'): robj.vclock = VClock(resp.vclock, 'binary') # We should do this even if there are no contents, i.e. # the object is tombstoned self._decode_contents(resp.content, robj) else: # "not found" returns an empty message, # so let's make sure to clear the siblings robj.siblings = [] return robj