def __init__(self, bucket_name, server): self.server = server self.bucket_name = bucket_name rest = server._rest() self.bucket_password = rest.get_bucket(bucket_name).saslPassword ip, port, rest_username, rest_password = server._rest_info() formatter_uri = "http://%s:%s/pools/default" self.mc_client = CouchbaseClient(formatter_uri % (ip, port), self.bucket_name, self.bucket_password)
class Bucket(object): def __init__(self, bucket_name, server): self.server = server self.bucket_name = bucket_name rest = server._rest() self.bucket_password = rest.get_bucket(bucket_name).saslPassword ip, port, rest_username, rest_password = server._rest_info() formatter_uri = "http://%s:%s/pools/default" self.mc_client = CouchbaseClient(formatter_uri % (ip, port), self.bucket_name, self.bucket_password) def append(self, key, value, cas=0): return self.mc_client.append(key, value, cas) def prepend(self, key, value, cas=0): return self.mc_client.prepend(key, value, cas) def incr(self, key, amt=1, init=0, exp=0): return self.mc_client.incr(key, amt, init, exp) def decr(self, key, amt=1, init=0, exp=0): return self.mc_client.decr(key, amt, init, exp) def set(self, key, expiration, flags, value): self.mc_client.set(key, expiration, flags, value) def add(self, key, exp, flags, val): return self.mc_client.add(key, exp, flags, val) def replace(self, key, exp, flags, val): return self.mc_client.replace(key, exp, flags, val) def get(self, key): return self.mc_client.get(key) def send_get(self, key): return self.mc_client.send_get(key) def getl(self, key, exp=15): return self.mc_client.getl(key, exp) def cas(self, key, exp, flags, oldVal, val): return self.mc_client.cas(key, exp, flags, oldVal, val) def touch(self, key, exp): return self.mc_client.touch(key, exp) def gat(self, key, exp): return self.mc_client.gat(key, exp) def getMulti(self, keys): return self.mc_client.getMulti(keys) def stats(self, sub=''): return self.mc_client.stats(sub) def delete(self, key, cas=0): if key.startswith('_design/'): # this is a design doc, we need to handle it differently view = key.split('/')[1] rest = self.server._rest() rest.delete_view(self.bucket_name, view) else: return self.mc_client.delete(key, cas) def save(self, document): value = deepcopy(document) if '_id' in value: key = value['_id'] del value['_id'] else: key = str(uuid.uuid4()) if '$flags' in value: flags = value['$flags'] del value['$flags'] else: flags = 0 if '$expiration' in value: expiration = value['$expiration'] del value['$expiration'] else: expiration = 0 if key.startswith('_design/'): # this is a design doc, we need to handle it differently view = key.split('/')[1] rest = self.server._rest() rest.create_view(self.bucket_name, view, json.dumps(value)) else: if '_rev' in value: # couchbase works in clobber mode so for "set" _rev is useless del value['_rev'] self.set(key, expiration, flags, json.dumps(value)) return key def __setitem__(self, key, value): if isinstance(value, dict): self.set(key, value['expiration'], value['flags'], value['value']) else: self.set(key, 0, 0, value) def __getitem__(self, key): return self.get(key) def view(self, view, **options): params = deepcopy(options) limit = None if 'limit' in params: limit = params['limit'] del params['limit'] if view.startswith("_design/"): view_s = view.split('/') view_doc = view_s[1] view_map = view_s[3] else: view_doc = view view_map = None rest = self.server._rest() results = rest.view_results(self.bucket_name, view_doc, view_map, params, limit) if 'rows' in results: return results['rows'] else: return None