Example #1
0
    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")
Example #2
0
# 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:
    # An exception will be raised if the CAS doesn't match
    wrong_cas = cas + 123
    cb.upsert('first', {'hello': 'world', 'additional': True}, cas=wrong_cas)
except KeyExistsError:
    # Get the correct current CAS value
    rv = cb.get('first')
Example #3
0
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")
upsert_result = collection.upsert(
Example #4
0
    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
        with open(fullfn, 'r') as f:
            jn = json.load(f)
            retries = 0
            while retries < 10:
                try:
                    thing_bucket.upsert(str(jn['id']), jn)
                    break
                except TempFailException:
                    print("failed")
                    time.sleep(3)
                    retries += 1
                    if 100 == retries:
                        sys.exit(2)
    # Leave a marker in the dir
    with open(donefile, "w") as f:
        f.write("1")
    del jns
    print(f"Done")
Example #5
0
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_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)