<commit_before># -*- encoding: utf-8 -*-

__author__ = 'kotaimen'
__date__ = '4/3/14'

import time

import couchbase
import couchbase.exceptions

from ..geo import Feature, build_feature, feature2literal, literial2feature
from .base import is_key_valid, SimpleGeoStore, Capability
from .exception import InvalidKey, FeatureAlreadyExists, FeatureDoesNotExist, \
    PropertyDoesNotExist, CASConflict


class SimpleCouchbaseGeoStore(SimpleGeoStore):
    """ Uses couchbase as a key-value storage for features

    Feature are stored as geojson.  Thread safety provided by underlying
    couchbase driver.
    """

    NAME = 'couchbase'

    CAPABILITY = Capability(
        efficient_key_lookup=True,
        in_memory_cache=True,
        presistence=True,

    )

    COUNTER = '__feature_counter'

    def __init__(self,
                 bucket=None,
                 host='localhost',
                 port=8091,
                 password=<PASSWORD>,
                 timeout=2.5,
                 retries=3):
        self._conn = couchbase.Couchbase.connect(bucket=bucket,
                                                 host=host,
                                                 port=port,
                                                 password=password,
                                                 timeout=timeout, )
        assert self._conn.connected
        assert retries > 0
        # Make a (backoff, retry) list
        self._retry = zip([0.01 * 2 ** n for n in range(retries)],
                          range(retries - 1, -1, -1))

    def describe(self):
        description = super(SimpleCouchbaseGeoStore, self).describe()
        if self._conn.connected:
            description['stats'] = self._conn.stats()
            description['libcouchbase'] = self._conn.lcb_version()
        return description

    def put_feature(self, feature, key=None, prefix=None):
        key = self._make_key(key, prefix)
        feature.key = key
        try:
            self._conn.add(key, feature2literal(feature))
        except couchbase.exceptions.KeyExistsError as e:
            ret = self._conn.get(key)
            # HACK: add() raises KeyExistsError when server is busy even if op is successful
            if ret.value['_etag'] != feature.etag:
                raise FeatureAlreadyExists(
                    message='Feature already exists: "%s"' % key, e=e)
        return feature

    def get_feature(self, key, prefix=None):
        key = self._make_key(key, prefix)
        try:
            ret = self._conn.get(key)
        except couchbase.exceptions.NotFoundError as e:
            raise FeatureDoesNotExist(
                message='Feature does not exist: "%s"' % key, e=e)
        else:
            return literial2feature(ret.value)

    def delete_feature(self, key, prefix=None):
        key = self._make_key(key, prefix)
        try:
            ret = self._conn.delete(key)
        except couchbase.exceptions.NotFoundError as e:
            raise FeatureDoesNotExist(
                message='Feature does not exist: "%s"' % key, e=e)

    def update_geometry(self, geometry, key, prefix=None):

        def updater(feature):
            feature.update_geometry(geometry)
            return feature
        try:
            return self._update_document(key, prefix, updater)
        except FeatureDoesNotExist:
            feature = build_feature(geometry)
            feature.key = key
            self._conn.set(key, feature2literal(feature))
            return feature

    def update_properties(self, properties, key, prefix=None):
        def updater(feature):
            feature.update_properties(properties)
            return feature

        return self._update_document(key, prefix, updater)

    def delete_properties(self, key, prefix=None):
        def updater(feature):
            feature.properties.clear()
            feature.recalculate()
            return feature

        return self._update_document(key, prefix, updater)

    def get_property(self, name, key, prefix=None):
        # XXX: Update property without converting to Feature object?

        key = self._make_key(key, prefix)

        ret = self._conn.get(key, quiet=True)
        if ret.value is None:
            raise FeatureDoesNotExist(<commit_msg>Fix 500 error when post new geometry<commit_after># -*- encoding: utf-8 -*-

__author__ = 'kotaimen'
__date__ = '4/3/14'

import time

import couchbase
import couchbase.exceptions

from ..geo import Feature, build_feature, feature2literal, literial2feature
from .base import is_key_valid, SimpleGeoStore, Capability
from .exception import InvalidKey, FeatureAlreadyExists, FeatureDoesNotExist, \
    PropertyDoesNotExist, CASConflict


class SimpleCouchbaseGeoStore(SimpleGeoStore):
    """ Uses couchbase as a key-value storage for features

    Feature are stored as geojson.  Thread safety provided by underlying
    couchbase driver.
    """

    NAME = 'couchbase'

    CAPABILITY = Capability(
        efficient_key_lookup=True,
        in_memory_cache=True,
        presistence=True,

    )

    COUNTER = '__feature_counter'

    def __init__(self,
                 bucket=None,
                 host='localhost',
                 port=8091,
                 password=<PASSWORD>,
                 timeout=2.5,
                 retries=3):
        self._conn = couchbase.Couchbase.connect(bucket=bucket,
                                                 host=host,
                                                 port=port,
                                                 password=password,
                                                 timeout=timeout, )
        assert self._conn.connected
        assert retries > 0
        # Make a (backoff, retry) list
        self._retry = zip([0.01 * 2 ** n for n in range(retries)],
                          range(retries - 1, -1, -1))

    def describe(self):
        description = super(SimpleCouchbaseGeoStore, self).describe()
        if self._conn.connected:
            description['stats'] = self._conn.stats()
            description['libcouchbase'] = self._conn.lcb_version()
        return description

    def put_feature(self, feature, key=None, prefix=None):
        key = self._make_key(key, prefix)
        feature.key = key
        try:
            self._conn.add(key, feature2literal(feature))
        except couchbase.exceptions.KeyExistsError as e:
            ret = self._conn.get(key)
            # HACK: add() raises KeyExistsError when server is busy even if op is successful
            if ret.value['_etag'] != feature.etag:
                raise FeatureAlreadyExists(
                    message='Feature already exists: "%s"' % key, e=e)
        return feature

    def get_feature(self, key, prefix=None):
        key = self._make_key(key, prefix)
        try:
            ret = self._conn.get(key)
        except couchbase.exceptions.NotFoundError as e:
            raise FeatureDoesNotExist(
                message='Feature does not exist: "%s"' % key, e=e)
        else:
            return literial2feature(ret.value)

    def delete_feature(self, key, prefix=None):
        key = self._make_key(key, prefix)
        try:
            ret = self._conn.delete(key)
        except couchbase.exceptions.NotFoundError as e:
            raise FeatureDoesNotExist(
                message='Feature does not exist: "%s"' % key, e=e)

    def update_geometry(self, geometry, key, prefix=None):

        def updater(feature):
            feature.update_geometry(geometry)
            return feature
        try:
            return self._update_document(key, prefix, updater)
        except FeatureDoesNotExist:
            key = self._make_key(key, prefix)
            feature = build_feature(geometry)
            feature.key = key
            self._conn.set(key, feature2literal(feature))
            return feature

    def update_properties(self, properties, key, prefix=None):
        def updater(feature):
            feature.update_properties(properties)
            return feature

        return self._update_document(key, prefix, updater)

    def delete_properties(self, key, prefix=None):
        def updater(feature):
            feature.properties.clear()
            feature.recalculate()
            return feature

        return self._update_document(key, prefix, updater)

    def get_property(self, name, key, prefix=None):
        # XXX: Update property without converting to Feature object?

        key = self._make_key(key, prefix)

        ret = self._conn.get(key, quiet=True)
        if ret.value is None:
            raise FeatureDoesNotExist(
{'fraction_non_alphanumeric': 0.08776811594202899, 'fraction_numerical': 0.006144927536231884, 'mean_word_length': 1.9622252747252746, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1166746', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2227, 'n_words': 580}