def test_reject_ephemeral_attempt(self): if not self._realserver_info: raise SkipTest("Need real server") admin=self.make_admin_connection() bucket_name = 'ephemeral' users=[('writer',('s3cr3t',[('data_reader', 'ephemeral'), ('data_writer', 'ephemeral')])), ('reader',('s3cr3t',[('data_reader', 'ephemeral')])), ('viewer',('s3cr3t',[('views_reader', 'ephemeral'), ('views_admin', 'ephemeral')]))] user=users[2] (userid, password, roles) = user[0],user[1][0],user[1][1] # add user try: admin.bucket_delete(bucket_name) except: pass try: admin.bucket_create(name=bucket_name, bucket_type='ephemeral', ram_quota=100) except HTTPException: raise SkipTest("Unable to provision ephemeral bucket") try: admin.user_upsert(AuthDomain.Local, userid, password, roles) admin.wait_ready(bucket_name, timeout=10) conn_str = "couchbase://{0}/{1}".format(self.cluster_info.host, bucket_name) bucket = Bucket(connection_string=conn_str,username=userid,password=password) self.assertIsNotNone(bucket) self.assertRaisesRegex(NotSupportedException, "Ephemeral", lambda: bucket.query("beer", "brewery_beers", streaming=True, limit=100)) finally: admin.bucket_delete(bucket_name) admin.user_remove(AuthDomain.Local, userid)
def test_transcoder_class(self): # Test whether we can pass a class for a transcoder key = self.gen_key("transcoder_class") c = Bucket(**self.make_connargs(transcoder=TranscoderPP)) c.upsert(key, "value") c = Bucket(**self.make_connargs(transcoder=TranscoderPP)) c.upsert(key, "value")
def test_connection_defaults(self): # This will only work on the basic Connection class from couchbase_v2.bucket import Bucket ctor_params = self.make_connargs() # XXX: Change these if any of the defaults change defaults = { 'quiet' : False, 'default_format' : FMT_JSON, 'unlock_gil' : True, 'transcoder' : None } cb_ctor = Bucket(**ctor_params) for option, value in defaults.items(): actual = getattr(cb_ctor, option) self.assertEqual(actual, value)
def act_on_special_bucket(self, bucket_name, password, action, perm_generator=None): try: if self.is_realserver: self.admin.bucket_remove("default") time.sleep(10) self.admin.bucket_create(name=bucket_name, bucket_type='ephemeral', ram_quota=100, bucket_password=password) self.admin.wait_ready(bucket_name, timeout=100) if perm_generator: roles = perm_generator(bucket_name) else: roles = [('data_reader', bucket_name), ('data_writer', bucket_name)] self.admin.user_upsert(AuthDomain.Local, bucket_name, password, roles) # connect to bucket to ensure we can use it conn_str = "http://{0}:{1}/{2}".format( self.cluster_info.host, self.cluster_info.port, bucket_name) + "?ipv6=" + self.cluster_info.ipv6 bucket = Bucket(connection_string=conn_str, password=password) self.assertIsNotNone(bucket) action(bucket) finally: try: self.admin.bucket_delete(bucket_name) finally: if self.is_realserver: time.sleep(10) self.admin.bucket_create(name="default", bucket_type='couchbase', ram_quota=100, bucket_password=password) self.admin.wait_ready("default", timeout=100)
def make_client(self): cb = None while not cb: # Try to connect via HTTP try: cb = Bucket(self.bucket_spec) cb.transcoder = AlwaysJsonTranscoder() cb.stats() cb.timeout = 7.5 except CouchbaseNetworkError as e: self.logger.exception( 'Got error while connecting. Sleeping for a bit') sleep(1) self._client = cb
def __init__(self): self.delay = options.delay self.key = 'K' * options.ksize self.value = b'V' * options.vsize self.kv = {} for x in range(options.batch): self.kv[self.key + str(x)] = self.value self.wait_time = 0 self.opcount = 0 connopts = { "connstr": options.connstr, "unlock_gil": DO_UNLOCK_GIL, "password": options.password } if options.iops: connopts["experimental_gevent_support"] = True self.cb = Bucket(**connopts) if options.transcoder: self.cb.transcoder = TC self.end_time = time() + options.duration super(Worker, self).__init__()
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from couchbase_v2.bucket import Bucket from couchbase_core.exceptions import KeyExistsError # Connect to the default bucket on local host cb = Bucket('couchbase://127.0.0.1/default') # If you want to store the Python objects pickled and not as JSON #cb.default_format = FMT_PICKLE # Store a document rv = cb.upsert('first', {'hello': 'world'}) cas = rv.cas print(rv) # Get the document item = cb.get('first') print(item) # Overwrite the existing document only if the CAS value matched try:
#end::simpleget[] cluster = Cluster.connect("couchbases://127.0.0.1", ClusterOptions(PasswordAuthenticator("user", "pass"))) bucket = cluster.bucket("travel-sample") collection = bucket.default_collection() # tag::upsertandget[] upsert_result = collection.upsert("mydoc-id", {}) get_result = collection.get("mydoc-id") # end::upsertandget[] from couchbase_v2.bucket import Bucket # tag::upsertandget_sdk2[] # SDK 2 upsert and get bucket = Bucket("couchbases://127.0.0.1/default") upsert_result = bucket.upsert("mydoc-id", {}) get_result = bucket.get("mydoc-id") # end::upsertandget_sdk2[] import couchbase_tests.base #natag::rawjson[] # TODO: update when implemented from couchbase.collection import UpsertOptions from couchbase_core.transcoder import Transcoder class RawJSONTranscoder(Transcoder): pass content = "{}".encode("UTF_8")
@score.setter def score(self, value): self.value['score'] = value @property def games(self): return self.value['games'] @property def email(self): return self.value['email'] @email.setter def email(self, value): self.value['email'] = value cb = Bucket('couchbase://localhost/default') single_player = Player.create("bob", "*****@*****.**", cb) single_player.score += 100 single_player.save(cb) # Let's try multiple players players = ItemSequence([Player(x, create_structure=True) for x in ("joe", "jim", "bill", "larry")]) # Save them all cb.upsert_multi(players) # Give them all some points for p, options in players: p.score += randint(20, 2000) # also set the email?
dest="thing_json_dir", default=THING_JSON_DIR) args = parser.parse_args() try: with open(args.c, 'r') as f: COUCHBASE_CREDS = json.load(f) except IOError: print( f"Copy {COUCHBASE_CREDS_F}.example to {args.c}.json and add creds first" ) sys.exit(2) # Authenticate with cluster thing_bucket = Bucket(f"{args.cluster}/{args.thing_bucket}", **COUCHBASE_CREDS) n = 0 for root, dirs, files in os.walk(args.thing_json_dir): print(f"Processing {root}") donefile = os.path.join(root, ".done") if os.path.exists(donefile): print(f"Already done") continue jns = {} # Load up the entire root for fn in files: fullfn = os.path.join(root, fn) if n % 100 == 0: print(f"Loading {fullfn}\r") n += 1
def test(x): c = Bucket('couchbase://localhost/default', experimental_gevent_support=True) c.upsert("tmp-" + str(x), 1) sys.stdout.write(str(x) + " ") sys.stdout.flush()
from couchbase_core.transcoder import Transcoder from couchbase_v2.bucket import Bucket class ReverseTranscoder(Transcoder): def encode_key(self, key): return super(ReverseTranscoder, self).encode_key(key[::-1]) def decode_key(self, key): key = super(ReverseTranscoder, self).decode_key(key) return key[::-1] c_reversed = Bucket('couchbase://localhost/default', transcoder=ReverseTranscoder()) c_plain = Bucket('couchbase://localhost/default') c_plain.remove_multi(('ABC', 'CBA', 'XYZ', 'ZYX'), quiet=True) c_reversed.upsert("ABC", "This is a reversed key") rv = c_plain.get("CBA") print("Got value for reversed key '{0}'".format(rv.value)) rv = c_reversed.get("ABC") print("Got value for reversed key '{0}' again".format(rv.value)) c_plain.upsert("ZYX", "This is really ZYX") rv = c_reversed.get("XYZ") print("Got value for '{0}': '{1}'".format(rv.key, rv.value))
help="Thing Bucket Name", dest="thing_bucket", default="thingiverse_things") parser.add_argument("-t", help="Things JSON directory", dest="thing_json_dir", default=THING_JSON_DIR) args = parser.parse_args() try: with open(args.c, 'r') as f: COUCHBASE_CREDS = json.load(f) except IOError: print( f"Copy {COUCHBASE_CREDS_F}.example to {args.c}.json and add creds first" ) sys.exit(2) # Authenticate with cluster thing_bucket = Bucket(f"{args.cluster}/{args.thing_bucket}", **COUCHBASE_CREDS) for root, dirs, files in os.walk(args.thing_json_dir): for fn in files: fullfn = os.path.join(root, fn) print(f"Loading {fullfn}") with open(fullfn, 'r') as f: jn = json.load(f) fnid = fn.split(".")[0] thing_bucket.upsert(fnid, jn)
import pprint from couchbase_v2.bucket import Bucket ap = ArgumentParser() ap.add_argument('-D', '--create-design', default=False, action='store_true', help='whether to create the design') ap.add_argument('-n', '--number-of-terms', default=10, type=int, help="How many terms to generate") options = ap.parse_args() c = Bucket('couchbase://localhost/default') DESIGN = { '_id': '_design/search_keywords', 'language': 'javascript', 'views': { 'top_keywords': { 'map': """ function(doc) { if (typeof doc === 'number') { emit(doc, null); } } """ }