def decode_path(encoded_path): """ Parse a Cassandra-encoded reference path. Args: encoded_path: A string specifying the encoded path. Returns: An entity_pb.Path object. """ path = entity_pb.Path() for element in encoded_path.split(dbconstants.KIND_SEPARATOR): # For some reason, encoded keys have a trailing separator, so ignore the # last empty element. if not element: continue kind, identifier = element.split(dbconstants.ID_SEPARATOR) new_element = path.add_element() new_element.set_type(kind) # Encoded paths do not differentiate between IDs and names, so we can only # guess which one it is. IDs often exceed the ID_KEY_LENGTH. if len(identifier) >= ID_KEY_LENGTH and identifier.isdigit(): new_element.set_id(int(identifier)) else: new_element.set_name(identifier) return path
def encode_path_from_filter(query_filter): """ Encode a reference path from a query filter. Args: query_filter: A datastore_pb.Query_Filter. Returns: A string containing an encoded reference path. """ path = entity_pb.Path() ref_value = query_filter.property(0).value().referencevalue() for element in ref_value.pathelement_list(): path.add_element().MergeFrom(element) return str(encode_index_pb(path))
def decode(flat_path, reference_value=False): """ Converts a tuple to a key path protobuf object. """ if reference_value: path = entity_pb.PropertyValue_ReferenceValue() else: path = entity_pb.Path() for index in range(0, len(flat_path), 2): if reference_value: element = path.add_pathelement() else: element = path.add_element() element.set_type(flat_path[index]) id_or_name = flat_path[index + 1] if isinstance(id_or_name, int): element.set_id(id_or_name) else: element.set_name(id_or_name.encode('utf-8')) return path
def group(self): group = entity_pb.Path() group.add_element().MergeFrom(Path.decode_element(self.path[:2])) return group