github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/utils/sharded_key.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 # pytype: skip-file 19 20 21 class ShardedKey(object): 22 """ 23 A sharded key consisting of a user key and an opaque shard id represented by 24 bytes. 25 26 Attributes: 27 key: The user key. 28 shard_id: An opaque byte string that uniquely represents a shard of the key. 29 """ 30 def __init__( 31 self, 32 key, 33 shard_id, # type: bytes 34 ): 35 # type: (...) -> None 36 assert shard_id is not None 37 self._key = key 38 self._shard_id = shard_id 39 40 @property 41 def key(self): 42 return self._key 43 44 def __repr__(self): 45 return '(%s, %s)' % (repr(self.key), self._shard_id) 46 47 def __eq__(self, other): 48 return ( 49 type(self) == type(other) and self.key == other.key and 50 self._shard_id == other._shard_id) 51 52 def __hash__(self): 53 return hash((self.key, self._shard_id)) 54 55 def __reduce__(self): 56 return ShardedKey, (self.key, self._shard_id)