MaksymSkorupskyi commented on a change in pull request #14460:
URL: https://github.com/apache/beam/pull/14460#discussion_r637580248
##########
File path: sdks/python/apache_beam/io/mongodbio.py
##########
@@ -391,61 +532,55 @@ def int_to_id(cls, number):
Returns: The ObjectId that has the 12 bytes binary converted from the
integer value.
-
"""
# converts integer value to object id. Int value should be less than
# (2 ^ 96) so it can be convert to 12 bytes required by object id.
if number < 0 or number >= (1 << 96):
- raise ValueError('number value must be within [0, %s)' % (1 << 96))
- ints = [(number & 0xffffffff0000000000000000) >> 64,
- (number & 0x00000000ffffffff00000000) >> 32,
- number & 0x0000000000000000ffffffff]
+ raise ValueError("number value must be within [0, %s)" % (1 << 96))
+ ints = [
+ (number & 0xFFFFFFFF0000000000000000) >> 64,
+ (number & 0x00000000FFFFFFFF00000000) >> 32,
+ number & 0x0000000000000000FFFFFFFF,
+ ]
- bytes = struct.pack('>III', *ints)
- return objectid.ObjectId(bytes)
+ number_bytes = struct.pack(">III", *ints)
+ return ObjectId(number_bytes)
@classmethod
- def increment_id(cls, object_id, inc):
+ def increment_id(
+ cls,
+ object_id: Union[int, str, ObjectId],
+ inc: int,
+ ) -> Union[int, str, ObjectId]:
"""
Args:
- object_id: The ObjectId to change.
- inc(int): The incremental int value to be added to ObjectId.
+ object_id: The `_id` to change.
+ inc(int): The incremental int value to be added to `_id`.
Returns:
-
+ `_id` incremented by `inc` value
"""
+ # Handle integer `_id`
+ if isinstance(object_id, int):
+ return object_id + inc
+
+ # Handle string `_id`
+ if isinstance(object_id, str):
+ object_id = object_id or chr(31) # handle empty string ('')
+ # incrementing the latest symbol of the string
+ return object_id[:-1] + chr(ord(object_id[-1:]) + inc)
Review comment:
@y1chi: 600807ff8a75a331362875118bb5e728fb5fbc86
* improve and simplify `_id` logic depended on it's type,
* remove redundant code
* fix and cleanup tests
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]