Exemplo n.º 1
0
def get_from_cache_by_key(key):
    """
        Given a datastore.Key (which should already have the namespace applied to it), return an
        entity from the context cache, falling back to memcache when possible.
    """
    if not CACHE_ENABLED:
        return None

    context = get_context()
    namespace = key.namespace() or None
    ret = None
    if context.context_enabled:
        # It's safe to hit the context cache, because a new one was pushed on the stack at the start of the transaction
        ret = context.stack.top.get_entity_by_key(key)
        if ret is None and not datastore.IsInTransaction():
            if context.memcache_enabled:
                ret = _get_entity_from_memcache_by_key(key)
                if ret:
                    # Add back into the context cache
                    add_entities_to_cache(
                        utils.get_model_from_db_table(ret.key().kind()),
                        [ret],
                        CachingSituation.DATASTORE_GET,
                        namespace,
                        skip_memcache=
                        True  # Don't put in memcache, we just got it from there!
                    )
    elif context.memcache_enabled and not datastore.IsInTransaction():
        ret = _get_entity_from_memcache_by_key(key)

    return ret
Exemplo n.º 2
0
def get_from_cache(unique_identifier, namespace):
    """
        Return an entity from the context cache, falling back to memcache when possible
    """
    context = get_context()

    if not CACHE_ENABLED:
        return None

    cache_key = _apply_namespace(unique_identifier, namespace)
    ret = None
    if context.context_enabled:
        # It's safe to hit the context cache, because a new one was pushed on the stack at the start of the transaction
        ret = context.stack.top.get_entity(cache_key)
        if ret is None and not datastore.IsInTransaction():
            if context.memcache_enabled:
                ret = _get_entity_from_memcache(cache_key)
                if ret:
                    # Add back into the context cache
                    add_entities_to_cache(
                        utils.get_model_from_db_table(ret.key().kind()),
                        [ret],
                        CachingSituation.DATASTORE_GET,
                        namespace,
                        skip_memcache=
                        True  # Don't put in memcache, we just got it from there!
                    )

    elif context.memcache_enabled and not datastore.IsInTransaction():
        ret = _get_entity_from_memcache(cache_key)

    return ret
Exemplo n.º 3
0
def get_from_cache(unique_identifier, namespace):
    """
        Return an entity from the context cache, falling back to memcache when possible
    """
    context = get_context()

    if not CACHE_ENABLED:
        return None

    cache_key = _apply_namespace(unique_identifier, namespace)
    ret = None
    if context.context_enabled:
        # It's safe to hit the context cache, because a new one was pushed on the stack at the start of the transaction
        ret = context.stack.top.get_entity(cache_key)
        if ret is None and not datastore.IsInTransaction():
            if context.memcache_enabled:
                ret = _get_entity_from_memcache(cache_key)
                if ret:
                    # Add back into the context cache
                    add_entities_to_cache(
                        utils.get_model_from_db_table(ret.key().kind()),
                        [ret],
                        CachingSituation.DATASTORE_GET,
                        namespace,
                        skip_memcache=True # Don't put in memcache, we just got it from there!
                    )

    elif context.memcache_enabled and not datastore.IsInTransaction():
        ret = _get_entity_from_memcache(cache_key)

    return ret
Exemplo n.º 4
0
def _get_cache_key_and_model_from_datastore_key(key):
    from django.db.migrations.recorder import MigrationRecorder

    # The django migration model isn't registered with the app registry so this
    # is special cased here
    MODELS_WHICH_ARENT_REGISTERED_WITH_DJANGO = {
        MigrationRecorder.Migration._meta.db_table: MigrationRecorder.Migration
    }

    kind = key.kind()
    model = utils.get_model_from_db_table(kind)

    if not model:
        if kind in MODELS_WHICH_ARENT_REGISTERED_WITH_DJANGO:
            model = MODELS_WHICH_ARENT_REGISTERED_WITH_DJANGO[kind]
        else:
            # This should never happen.. if it does then we can edit get_model_from_db_table to pass
            # include_deferred=True/included_swapped=True to get_models, whichever makes it better
            raise ImproperlyConfigured(
                "Unable to locate model for db_table '{}' - are you missing an INSTALLED_APP?"
                .format(key.kind()))

    # We build the cache key for the ID of the instance
    cache_key = "|".join([
        key.kind(),
        "{}:{}".format(model._meta.pk.column,
                       _format_value_for_identifier(key.id_or_name()))
    ])

    return (cache_key, model)
Exemplo n.º 5
0
def _get_cache_key_and_model_from_datastore_key(key):
    from django.db.migrations.recorder import MigrationRecorder

    # The django migration model isn't registered with the app registry so this
    # is special cased here
    MODELS_WHICH_ARENT_REGISTERED_WITH_DJANGO = {
        MigrationRecorder.Migration._meta.db_table: MigrationRecorder.Migration
    }

    kind = key.kind()
    model = utils.get_model_from_db_table(kind)

    if not model:
        if kind in MODELS_WHICH_ARENT_REGISTERED_WITH_DJANGO:
            model = MODELS_WHICH_ARENT_REGISTERED_WITH_DJANGO[kind]
        else:
            # This should never happen.. if it does then we can edit get_model_from_db_table to pass
            # include_deferred=True/included_swapped=True to get_models, whichever makes it better
            raise ImproperlyConfigured("Unable to locate model for db_table '{}' - are you missing an INSTALLED_APP?".format(key.kind()))

    # We build the cache key for the ID of the instance
    cache_key = "|".join(
        [key.kind(), "{}:{}".format(model._meta.pk.column, _format_value_for_identifier(key.id_or_name()))]
    )

    return (cache_key, model)
Exemplo n.º 6
0
def get_from_cache_by_key(key):
    """
        Given a datastore.Key (which should already have the namespace applied to it), return an
        entity from the context cache, falling back to memcache when possible.
    """
    if not CACHE_ENABLED:
        return None

    context = get_context()
    namespace = key.namespace() or None
    ret = None
    if context.context_enabled:
        # It's safe to hit the context cache, because a new one was pushed on the stack at the start of the transaction
        ret = context.stack.top.get_entity_by_key(key)
        if ret is None and not datastore.IsInTransaction():
            if context.memcache_enabled:
                ret = _get_entity_from_memcache_by_key(key)
                if ret:
                    # Add back into the context cache
                    add_entities_to_cache(
                        utils.get_model_from_db_table(ret.key().kind()),
                        [ret],
                        CachingSituation.DATASTORE_GET,
                        namespace,
                        skip_memcache=True # Don't put in memcache, we just got it from there!
                    )
    elif context.memcache_enabled and not datastore.IsInTransaction():
        ret = _get_entity_from_memcache_by_key(key)

    return ret
Exemplo n.º 7
0
def get_from_cache_by_key(key):
    """
        Return an entity from the context cache, falling back to memcache when possible
    """

    ensure_context()

    if not CACHE_ENABLED:
        return None

    ret = None
    if _context.context_enabled:
        # It's safe to hit the context cache, because a new one was pushed on the stack at the start of the transaction
        ret = _context.stack.top.get_entity_by_key(key)
        if ret is None and not datastore.IsInTransaction():
            if _context.memcache_enabled:
                ret = _get_entity_from_memcache_by_key(key)
                if ret:
                    # Add back into the context cache
                    add_entity_to_cache(
                        utils.get_model_from_db_table(ret.key().kind()),
                        ret,
                        CachingSituation.DATASTORE_GET,
                        skip_memcache=True,  # Don't put in memcache, we just got it from there!
                    )
    elif _context.memcache_enabled and not datastore.IsInTransaction():
        ret = _get_entity_from_memcache_by_key(key)

    return ret
Exemplo n.º 8
0
def _extract_projected_columns_from_query_17(query):
    result = []

    if query.select:
        for x in query.select:
            if x.field is None:
                model = get_model_from_db_table(x.col.col[0])
                if get_top_concrete_parent(model) != get_top_concrete_parent(query.model):
                    raise NotSupportedError("Attempted a cross-join select which is not supported on the datastore")

                column = x.col.col[1]  # This is the column we are getting
            else:
                column = x.field.column

            result.append(column)
        return result
    else:
        # If the query uses defer()/only() then we need to process deferred. We have to get all deferred columns
        # for all (concrete) inherited models and then only include columns if they appear in that list
        only_load = query.get_loaded_field_names()
        if only_load:
            for field, model in query.model._meta.get_concrete_fields_with_model():
                model = model or query.model
                try:
                    if field.name in only_load[model]:
                        # Add a field that has been explicitly included
                        result.append(field.column)
                except KeyError:
                    # Model wasn't explicitly listed in the only_load table
                    # Therefore, we need to load all fields from this model
                    result.append(field.column)
            return result
        else:
            return []
def _extract_projected_columns_from_query_17(query):
    result = []

    if query.select:
        for x in query.select:
            if x.field is None:
                model = get_model_from_db_table(x.col.col[0])
                if get_top_concrete_parent(model) != get_top_concrete_parent(query.model):
                    raise NotSupportedError("Attempted a cross-join select which is not supported on the datastore")

                column = x.col.col[1]  # This is the column we are getting
            else:
                column = x.field.column

            result.append(column)
        return result
    else:
        # If the query uses defer()/only() then we need to process deferred. We have to get all deferred columns
        # for all (concrete) inherited models and then only include columns if they appear in that list
        only_load = query.get_loaded_field_names()
        if only_load:
            for field, model in query.model._meta.get_concrete_fields_with_model():
                model = model or query.model
                try:
                    if field.name in only_load[model]:
                        # Add a field that has been explicitly included
                        result.append(field.column)
                except KeyError:
                    # Model wasn't explicitly listed in the only_load table
                    # Therefore, we need to load all fields from this model
                    result.append(field.column)
            return result
        else:
            return []
Exemplo n.º 10
0
def _get_cache_key_and_model_from_datastore_key(key):
    model = utils.get_model_from_db_table(key.kind())

    if not model:
        # This should never happen.. if it does then we can edit get_model_from_db_table to pass
        # include_deferred=True/included_swapped=True to get_models, whichever makes it better
        raise AssertionError("Unable to locate model for db_table '{}' - item won't be evicted from the cache".format(key.kind()))

    # We build the cache key for the ID of the instance
    cache_key =  "|".join([key.kind(), "{}:{}".format(model._meta.pk.column,  _format_value_for_identifier(key.id_or_name()))])

    return (cache_key, model)
Exemplo n.º 11
0
    def __get__(self, instance):
        if instance is None:
            return self

        value = getattr(instance, self.attname)

        from djangae.forms.fields import decode_pk
        from djangae.db.utils import get_model_from_db_table

        if value is None:
            return None

        model_ref, pk = decode_pk(value)
        try:
            return get_model_from_db_table(model_ref).objects.get(pk=pk)
        except AttributeError:
            raise ImproperlyConfigured("Unable to find model with db_table: {}".format(model_ref))
Exemplo n.º 12
0
        def wipe_polymodel_from_entity(entity, db_table):
            """
                Wipes out the fields associated with the specified polymodel table
            """
            polymodel_value = entity.get('class', [])
            if polymodel_value and self.table_to_delete in polymodel_value:
                # Remove any local fields from this model from the entity
                model = utils.get_model_from_db_table(self.table_to_delete)
                for field in model._meta.local_fields:
                    col = field.column
                    if col in entity:
                        del entity[col]

                # Then remove this model from the polymodel heirarchy
                polymodel_value.remove(self.table_to_delete)
                if polymodel_value:
                    entity['class'] = polymodel_value
Exemplo n.º 13
0
    def __get__(self, instance):
        if instance is None:
            return self

        value = getattr(instance, self.attname)

        from djangae.forms.fields import decode_pk
        from djangae.db.utils import get_model_from_db_table

        if value is None:
            return None

        model_ref, pk = decode_pk(value)
        try:
            return get_model_from_db_table(model_ref).objects.get(pk=pk)
        except AttributeError:
            raise ImproperlyConfigured("Unable to find model with db_table: {}".format(model_ref))
Exemplo n.º 14
0
        def wipe_polymodel_from_entity(entity, db_table):
            """
                Wipes out the fields associated with the specified polymodel table
            """
            polymodel_value = entity.get('class', [])
            if polymodel_value and self.table_to_delete in polymodel_value:
                # Remove any local fields from this model from the entity
                model = utils.get_model_from_db_table(self.table_to_delete)
                for field in model._meta.local_fields:
                    col = field.column
                    if col in entity:
                        del entity[col]

                # Then remove this model from the polymodel heirarchy
                polymodel_value.remove(self.table_to_delete)
                if polymodel_value:
                    entity['class'] = polymodel_value