def __init__(self, host, username, password, bucketName): if bucketName is None: connectionType = LCB_TYPE_CLUSTER bucketName = "" else: connectionType = LCB_TYPE_BUCKET self.instance = pylcb.create(host, username, password, bucketName, connectionType) pylcb.set_arithmetic_callback(self.instance, self.arithmetic_callback) pylcb.set_error_callback(self.instance, self.error_callback) pylcb.set_flush_callback(self.instance, self.flush_callback) pylcb.set_get_callback(self.instance, self.get_callback) pylcb.set_http_complete_callback(self.instance, self.http_complete_callback) pylcb.set_remove_callback(self.instance, self.remove_callback) pylcb.set_stat_callback(self.instance, self.stat_callback) pylcb.set_store_callback(self.instance, self.store_callback) self.errorResults = [] pylcb.connect(self.instance) pylcb.wait(self.instance) if len(self.errorResults) == 0: return # special case error 22 lastResult = self.errorResults[len(self.errorResults) - 1] if lastResult['error'] in [LCB_SUCCESS, 22]: return raise PycbException(lastResult['error'], lastResult['errinfo'])
def view(self, view, **params): for param in params: if param in ["key", "keys", "startkey", "endkey"]: value = json.dumps(params[param]) params[param] = value path = view if len(params) > 0: path += "?%s" % urllib.urlencode(params) pylcb.make_http_request(self.instance, None, LCB_HTTP_TYPE_VIEW, path, "", LCB_HTTP_METHOD_GET, 0, "application/json") pylcb.wait(self.instance) # raise exception if http request failed if self.httpResult['error'] != LCB_SUCCESS: errMsg = "get view, error:%s" % \ pylcb.pylcb_strerror(self.httpResult['error']) raise PycbException(self.httpResult['error'], errMsg) # raise exception if http request was successful but status # is not 200 or 201 if self.httpResult['status'] not in [200, 201]: errMsg = "get view, status:%s, response:%s" % \ (self.httpResult['status'], self.httpResult['bytes']) raise PycbException(self.httpResult['error'], errMsg) response = json.loads(self.httpResult['bytes']) if 'rows' in response: return response['rows']
def delete_bucket(self, name): pylcb.make_http_request( self.instance, None, LCB_HTTP_TYPE_MANAGEMENT, "pools/default/buckets/%s" % name, "", LCB_HTTP_METHOD_DELETE, 0, "application/x-www-form-urlencoded" ) pylcb.wait(self.instance) # raise exception if http request failed if self.httpResult['error'] != LCB_SUCCESS: errMsg = "delete bucket, error:%s" % \ pylcb.pylcb_strerror(self.httpResult['error']) raise PycbException(self.httpResult['error'], errMsg) # raise exception if http request was successful but status # is not 200 (OK) if self.httpResult['status'] != 200: errMsg = "delete bucket, status:%s, response:%s" % \ (self.httpResult['status'], self.httpResult['bytes']) raise PycbException(self.httpResult['error'], errMsg)
def flush(self): self.flushResults = [] pylcb.flush(self.instance, self) pylcb.wait(self.instance) return self.flushResults
def create_bucket(self, name, **payload): payload.update(dict(name=name)) body = urllib.urlencode(payload) pylcb.make_http_request( self.instance, None, LCB_HTTP_TYPE_MANAGEMENT, "pools/default/buckets", body, LCB_HTTP_METHOD_POST, 0, "application/x-www-form-urlencoded" ) pylcb.wait(self.instance) # raise exception if http request failed if self.httpResult['error'] != LCB_SUCCESS: errMsg = "create bucket, error:%s" % \ pylcb.pylcb_strerror(self.httpResult['error']) raise PycbException(self.httpResult['error'], errMsg) # raise exception if http request was successful but status # is not 202 (Accepted) if self.httpResult['status'] != 202: errMsg = "create bucket, status:%s, response:%s" % \ (self.httpResult['status'], self.httpResult['bytes']) raise PycbException(self.httpResult['error'], errMsg)
def delete(self, key, cas=0): self.removeResult = None pylcb.remove(self.instance, self, key) pylcb.wait(self.instance) result = self.removeResult if result is None: errMsg = "did not get remove_callback" raise PycbException(LCB_ERROR, errMsg) if result['error'] == LCB_SUCCESS: return True errMsg = "error deleting key, %s" % pylcb.strerror(result['error']) raise PycbException(result['error'], errMsg)
def _store(self, key, expiration, flags, value, operation): self.storeResult = None pylcb.store(self.instance, self, key, expiration, flags, value, operation) pylcb.wait(self.instance) result = self.storeResult if result is None: errMsg = "did not get store_callback" raise PycbException(LCB_ERROR, errMsg) if result['error'] == LCB_SUCCESS: return True errMsg = "error storing key, %s" % pylcb.strerror(result['error']) raise PycbException(result['error'], errMsg)
def _arithmetic(self, key, delta, initial, expiration): self.arithmeticResult = None pylcb.arithmetic(self.instance, self, key, delta, initial, expiration) pylcb.wait(self.instance) result = self.arithmeticResult if result is None: errMsg = "did not get arithmetic_callback" raise PycbException(LCB_ERROR, errMsg) if result['error'] == LCB_SUCCESS: return result['value'] errMsg = "error incrementing/decrementing key, %s" \ % pylcb.strerror(result['error']) raise PycbException(result['error'], errMsg)
def delete_bucket(self, name): pylcb.make_http_request(self.instance, None, LCB_HTTP_TYPE_MANAGEMENT, "pools/default/buckets/%s" % name, "", LCB_HTTP_METHOD_DELETE, 0, "application/x-www-form-urlencoded") pylcb.wait(self.instance) # raise exception if http request failed if self.httpResult['error'] != LCB_SUCCESS: errMsg = "delete bucket, error:%s" % \ pylcb.pylcb_strerror(self.httpResult['error']) raise PycbException(self.httpResult['error'], errMsg) # raise exception if http request was successful but status # is not 200 (OK) if self.httpResult['status'] != 200: errMsg = "delete bucket, status:%s, response:%s" % \ (self.httpResult['status'], self.httpResult['bytes']) raise PycbException(self.httpResult['error'], errMsg)
def get(self, key): self.getResult = None pylcb.get(self.instance, self, key) pylcb.wait(self.instance) result = self.getResult if result is None: errMsg = "did not get get_callback" raise PycbException(LCB_ERROR, errMsg) if result['error'] == LCB_SUCCESS: # for compatibility with old couchbase python client, # do an integer conversion to strings made up only of numeric # digits bytes = result['bytes'] if bytes.isdigit(): bytes = int(bytes) return result['flags'], 0, bytes errMsg = "error retrieving key, %s" % pylcb.strerror(result['error']) raise PycbException(result['error'], errMsg)
def create_bucket(self, name, **payload): payload.update(dict(name=name)) body = urllib.urlencode(payload) pylcb.make_http_request(self.instance, None, LCB_HTTP_TYPE_MANAGEMENT, "pools/default/buckets", body, LCB_HTTP_METHOD_POST, 0, "application/x-www-form-urlencoded") pylcb.wait(self.instance) # raise exception if http request failed if self.httpResult['error'] != LCB_SUCCESS: errMsg = "create bucket, error:%s" % \ pylcb.pylcb_strerror(self.httpResult['error']) raise PycbException(self.httpResult['error'], errMsg) # raise exception if http request was successful but status # is not 202 (Accepted) if self.httpResult['status'] != 202: errMsg = "create bucket, status:%s, response:%s" % \ (self.httpResult['status'], self.httpResult['bytes']) raise PycbException(self.httpResult['error'], errMsg)
def view(self, view, **params): for param in params: if param in ["key", "keys", "startkey", "endkey"]: value = json.dumps(params[param]) params[param] = value path = view if len(params) > 0: path += "?%s" % urllib.urlencode(params) pylcb.make_http_request( self.instance, None, LCB_HTTP_TYPE_VIEW, path, "", LCB_HTTP_METHOD_GET, 0, "application/json" ) pylcb.wait(self.instance) # raise exception if http request failed if self.httpResult['error'] != LCB_SUCCESS: errMsg = "get view, error:%s" % \ pylcb.pylcb_strerror(self.httpResult['error']) raise PycbException(self.httpResult['error'], errMsg) # raise exception if http request was successful but status # is not 200 or 201 if self.httpResult['status'] not in [200, 201]: errMsg = "get view, status:%s, response:%s" % \ (self.httpResult['status'], self.httpResult['bytes']) raise PycbException(self.httpResult['error'], errMsg) response = json.loads(self.httpResult['bytes']) if 'rows' in response: return response['rows']
def stats(self, name=""): self.statsResults = [] pylcb.stats(self.instance, self, name) pylcb.wait(self.instance) return self.statsResults