def get_new_entity_proto(self,
                             app_id,
                             kind,
                             entity_name,
                             prop_name,
                             prop_value,
                             ns=""):
        entity_proto = datastore_pb.EntityProto()

        reference = entity_proto.mutable_key()
        reference.set_app(app_id)
        reference.set_name_space(ns)

        path = reference.mutable_path()
        element = path.add_element()
        element.set_type(kind)
        element.set_name(entity_name)

        ent_group = entity_proto.mutable_entity_group()
        eg_element = ent_group.add_element()
        eg_element.set_type(kind)
        eg_element.set_name(entity_name)

        prop = entity_proto.add_property()
        prop.set_meaning(datastore_pb.Property.BYTESTRING)
        prop.set_name(prop_name)
        prop.set_multiple(1)
        val = prop.mutable_value()
        val.set_stringvalue(prop_value)
        return entity_proto
Exemple #2
0
    def _DecodeCompiledCursor(self, compiled_cursor):
        """Converts a compiled_cursor into a cursor_entity.

        Args:
          compiled_cursor: The datastore_pb.CompiledCursor to decode.

        Returns:
          (cursor_entity, inclusive): a datastore_pb.EntityProto and if it should
          be included in the result set.
        """
        assert len(compiled_cursor.position_list()) == 1

        position = compiled_cursor.position(0)

        remaining_properties = self.__order_property_names.copy()
        cursor_entity = datastore_pb.EntityProto()
        cursor_entity.mutable_key().CopyFrom(position.key())
        for indexvalue in position.indexvalue_list():
            property = cursor_entity.add_property()
            property.set_name(indexvalue.property())
            property.mutable_value().CopyFrom(indexvalue.value())
            remaining_properties.remove(indexvalue.property())

        Check(
            not remaining_properties,
            'Cursor does not match query: missing values for %r' %
            remaining_properties)

        return (cursor_entity, position.start_inclusive())
Exemple #3
0
    def _DecodeCompiledCursor(self, query, compiled_cursor):
        """Converts a compiled_cursor into a cursor_entity.

    Returns:
      (cursor_entity, inclusive): a datastore_pb.EntityProto and if it should
      be included in the result set.
    """
        assert len(compiled_cursor.position_list()) == 1

        position = compiled_cursor.position(0)
        entity_as_pb = datastore_pb.EntityProto()
        if position.start_key():
            (query_info_encoded, entity_encoded) = \
                       position.start_key().split(_CURSOR_CONCAT_STR, 1)
            query_info_pb = datastore_pb.Query()
            query_info_pb.ParseFromString(query_info_encoded)
            self._ValidateQuery(query, query_info_pb)

            entity_as_pb.ParseFromString(entity_encoded)
        else:
            """Java doesn't include a start_key() so we will create the last entity
         from the position variable. 
      """
            entity_as_pb.key().MergeFrom(position.key_)
            entity_as_pb.entity_group().MergeFrom(position.key_.path_)
        return (entity_as_pb, position.start_inclusive())
Exemple #4
0
    def _DecodeCompiledCursor(self, query, compiled_cursor):
        """Converts a compiled_cursor into a cursor_entity.

    Returns:
      (cursor_entity, inclusive): a datastore_pb.EntityProto and if it should
      be included in the result set.
    """
        assert len(compiled_cursor.position_list()) == 1

        position = compiled_cursor.position(0)
        entity_as_pb = datastore_pb.EntityProto()
        (query_info_encoded,
         entity_encoded) = position.start_key().split(_CURSOR_CONCAT_STR, 1)
        query_info_pb = datastore_pb.Query()
        query_info_pb.ParseFromString(query_info_encoded)
        self._ValidateQuery(query, query_info_pb)

        entity_as_pb.ParseFromString(entity_encoded)
        return (entity_as_pb, position.start_inclusive())
Exemple #5
0
    def _MinimalEntityInfo(self, entity_proto, query):
        """Extract the minimal set of information that preserves entity order.

    Args:
      entity_proto: datastore_pb.EntityProto instance from which to extract
      information
      query: datastore_pb.Query instance for which ordering must be preserved.

    Returns:
      datastore_pb.EntityProto instance suitable for matching against a list of
      results when finding cursor positions.
    """
        entity_info = datastore_pb.EntityProto()
        order_names = [o.property() for o in query.order_list()]
        entity_info.mutable_key().MergeFrom(entity_proto.key())
        entity_info.mutable_entity_group().MergeFrom(
            entity_proto.entity_group())
        for prop in entity_proto.property_list():
            if prop.name() in order_names:
                entity_info.add_property().MergeFrom(prop)
        return entity_info
Exemple #6
0
    def _DecodeCompiledCursor(self, compiled_cursor):
        """Converts a compiled_cursor into a cursor_entity.

    Args:
      compiled_cursor: Cursor instance to decode.

    Returns:
      (offset, query_pb, cursor_entity, inclusive)
    """
        assert len(compiled_cursor.position_list()) == 1

        position = compiled_cursor.position(0)
        entity_pb = datastore_pb.EntityProto()
        (count, query_info_encoded,
         entity_encoded) = position.start_key().split(_CURSOR_CONCAT_STR)
        query_info_pb = datastore_pb.Query()
        query_info_pb.ParseFromString(query_info_encoded)
        entity_pb.ParseFromString(entity_encoded)
        offset = int(count) + query_info_pb.offset()
        return (offset, query_info_pb,
                datastore.Entity._FromPb(entity_pb,
                                         True), position.start_inclusive())