def __EncodeIndexPB(pb):
    if isinstance(pb, entity_pb.PropertyValue) and pb.has_uservalue():

      userval = entity_pb.PropertyValue()
      userval.mutable_uservalue().set_email(pb.uservalue().email())
      userval.mutable_uservalue().set_auth_domain(pb.uservalue().auth_domain())
      userval.mutable_uservalue().set_gaiaid(0)
      pb = userval
    encoder = sortable_pb_encoder.Encoder()
    pb.Output(encoder)
    return buffer(encoder.buffer().tostring())
Beispiel #2
0
def encode_index_pb(pb):
  """ Returns an encoded protocol buffer.

  Args:
      pb: The protocol buffer to encode.
  Returns:
      An encoded protocol buffer.
  """

  def _encode_path(pb):
    """ Takes a protocol buffer and returns the encoded path. """
    path = []
    for e in pb.element_list():
      if e.has_name():
        key_id = e.name()
      elif e.has_id():
        key_id = str(e.id()).zfill(ID_KEY_LENGTH)
      else:
        raise BadRequest('Entity path must contain name or ID')

      if ID_SEPARATOR in e.type():
        raise BadRequest('Kind names must not include ":"')

      path.append(ID_SEPARATOR.join([e.type(), key_id]))
    val = dbconstants.KIND_SEPARATOR.join(path)
    val += dbconstants.KIND_SEPARATOR
    return val

  if isinstance(pb, entity_pb.PropertyValue) and pb.has_uservalue():
    userval = entity_pb.PropertyValue()
    userval.mutable_uservalue().set_email(pb.uservalue().email())
    userval.mutable_uservalue().set_auth_domain("")
    userval.mutable_uservalue().set_gaiaid(0)
    pb = userval

  def remove_nulls(value):
    """ Remove null values from a given string and byte stuff encode. """
    return buffer(
      str(value).replace('\x01', '\x01\x02').replace('\x00', '\x01\x01'))

  encoder = sortable_pb_encoder.Encoder()
  pb.Output(encoder)

  if isinstance(pb, entity_pb.PropertyValue):
    value = encoder.buffer().tostring()
    # We strip off null strings because it is our delimiter.
    value = remove_nulls(value)
    return buffer(value)
  elif isinstance(pb, entity_pb.Path):
    return buffer(_encode_path(pb))
Beispiel #3
0
  def __EncodeIndexPB(pb):
    """Encodes a protobuf using sortable_pb_encoder to preserve entity order.

    Using sortable_pb_encoder, encodes the protobuf, while using
    the ordering semantics for the datastore, and validating for the special
    case of uservalues ordering.

    Args:
      pb: An Entity protobuf.

    Returns:
      A buffer holding the encoded protobuf.
    """
    if isinstance(pb, entity_pb.PropertyValue) and pb.has_uservalue():

      userval = entity_pb.PropertyValue()
      userval.mutable_uservalue().set_email(pb.uservalue().email())
      userval.mutable_uservalue().set_auth_domain(pb.uservalue().auth_domain())
      userval.mutable_uservalue().set_gaiaid(0)
      pb = userval
    encoder = sortable_pb_encoder.Encoder()
    pb.Output(encoder)
    return buffer(encoder.buffer().tostring())