|
| 1 | +import msgpack |
| 2 | +from enum import Enum |
| 3 | + |
| 4 | +from tarantool.error import MsgpackError |
| 5 | + |
| 6 | +# https://www.tarantool.io/en/doc/latest/dev_guide/internals/msgpack_extensions/#the-interval-type |
| 7 | +# |
| 8 | +# The interval MessagePack representation looks like this: |
| 9 | +# +--------+-------------------------+-------------+----------------+ |
| 10 | +# | MP_EXT | Size of packed interval | MP_INTERVAL | PackedInterval | |
| 11 | +# +--------+-------------------------+-------------+----------------+ |
| 12 | +# Packed interval consists of: |
| 13 | +# - Packed number of non-zero fields. |
| 14 | +# - Packed non-null fields. |
| 15 | +# |
| 16 | +# Each packed field has the following structure: |
| 17 | +# +----------+=====================+ |
| 18 | +# | field ID | field value | |
| 19 | +# +----------+=====================+ |
| 20 | +# |
| 21 | +# The number of defined (non-null) fields can be zero. In this case, |
| 22 | +# the packed interval will be encoded as integer 0. |
| 23 | +# |
| 24 | +# List of the field IDs: |
| 25 | +# - 0 – year |
| 26 | +# - 1 – month |
| 27 | +# - 2 – week |
| 28 | +# - 3 – day |
| 29 | +# - 4 – hour |
| 30 | +# - 5 – minute |
| 31 | +# - 6 – second |
| 32 | +# - 7 – nanosecond |
| 33 | +# - 8 – adjust |
| 34 | + |
| 35 | +id_map = { |
| 36 | + 0: 'year', |
| 37 | + 1: 'month', |
| 38 | + 2: 'week', |
| 39 | + 3: 'day', |
| 40 | + 4: 'hour', |
| 41 | + 5: 'minute', |
| 42 | + 6: 'sec', |
| 43 | + 7: 'nsec', |
| 44 | + 8: 'adjust', |
| 45 | +} |
| 46 | + |
| 47 | +# https://github.com/tarantool/c-dt/blob/cec6acebb54d9e73ea0b99c63898732abd7683a6/dt_arithmetic.h#L34 |
| 48 | +class Adjust(Enum): |
| 49 | + EXCESS = 0 # DT_EXCESS in c-dt, "excess" in Tarantool |
| 50 | + NONE = 1 # DT_LIMIT in c-dt, "none" in Tarantool |
| 51 | + LAST = 2 # DT_SNAP in c-dt, "last" in Tarantool |
| 52 | + |
| 53 | +class Interval(): |
| 54 | + def __init__(self, data=None, *, year=0, month=0, week=0, |
| 55 | + day=0, hour=0, minute=0, sec=0, |
| 56 | + nsec=0, adjust=Adjust.NONE): |
| 57 | + # If msgpack data does not contain a field value, it is zero. |
| 58 | + # If built not from msgpack data, set argument values later. |
| 59 | + self.year = 0 |
| 60 | + self.month = 0 |
| 61 | + self.week = 0 |
| 62 | + self.day = 0 |
| 63 | + self.hour = 0 |
| 64 | + self.minute = 0 |
| 65 | + self.sec = 0 |
| 66 | + self.nsec = 0 |
| 67 | + self.adjust = Adjust(0) |
| 68 | + |
| 69 | + if data is not None: |
| 70 | + if not isinstance(data, bytes): |
| 71 | + raise ValueError('data argument (first positional argument) ' + |
| 72 | + 'expected to be a "bytes" instance') |
| 73 | + |
| 74 | + if len(data) == 0: |
| 75 | + return |
| 76 | + |
| 77 | + # To create an unpacker is the only way to parse |
| 78 | + # a sequence of values in Python msgpack module. |
| 79 | + unpacker = msgpack.Unpacker() |
| 80 | + unpacker.feed(data) |
| 81 | + field_count = unpacker.unpack() |
| 82 | + for _ in range(field_count): |
| 83 | + field_id = unpacker.unpack() |
| 84 | + value = unpacker.unpack() |
| 85 | + |
| 86 | + if field_id not in id_map: |
| 87 | + raise MsgpackError(f'Unknown interval field id {field_id}') |
| 88 | + |
| 89 | + field_name = id_map[field_id] |
| 90 | + |
| 91 | + if field_name == 'adjust': |
| 92 | + try: |
| 93 | + value = Adjust(value) |
| 94 | + except ValueError as e: |
| 95 | + raise MsgpackError(e) |
| 96 | + |
| 97 | + setattr(self, id_map[field_id], value) |
| 98 | + else: |
| 99 | + self.year = year |
| 100 | + self.month = month |
| 101 | + self.week = week |
| 102 | + self.day = day |
| 103 | + self.hour = hour |
| 104 | + self.minute = minute |
| 105 | + self.sec = sec |
| 106 | + self.nsec = nsec |
| 107 | + self.adjust = adjust |
| 108 | + |
| 109 | + def __eq__(self, other): |
| 110 | + if not isinstance(other, Interval): |
| 111 | + return False |
| 112 | + |
| 113 | + # Tarantool interval compare is naive too |
| 114 | + # |
| 115 | + # Tarantool 2.10.1-0-g482d91c66 |
| 116 | + # |
| 117 | + # tarantool> datetime.interval.new{hour=1} == datetime.interval.new{min=60} |
| 118 | + # --- |
| 119 | + # - false |
| 120 | + # ... |
| 121 | + |
| 122 | + for field_id in id_map.keys(): |
| 123 | + field_name = id_map[field_id] |
| 124 | + if getattr(self, field_name) != getattr(other, field_name): |
| 125 | + return False |
| 126 | + |
| 127 | + return True |
| 128 | + |
| 129 | + def __repr__(self): |
| 130 | + return f'tarantool.Interval(year={self.year}, month={self.month}, day={self.day}, ' + \ |
| 131 | + f'hour={self.hour}, minute={self.minute}, sec={self.sec}, ' + \ |
| 132 | + f'nsec={self.nsec}, adjust={self.adjust})' |
| 133 | + |
| 134 | + __str__ = __repr__ |
| 135 | + |
| 136 | + def msgpack_encode(self): |
| 137 | + buf = bytes() |
| 138 | + |
| 139 | + count = 0 |
| 140 | + for field_id in id_map.keys(): |
| 141 | + field_name = id_map[field_id] |
| 142 | + value = getattr(self, field_name) |
| 143 | + |
| 144 | + if field_name == 'adjust': |
| 145 | + value = value.value |
| 146 | + |
| 147 | + if value != 0: |
| 148 | + buf = buf + msgpack.packb(field_id) + msgpack.packb(value) |
| 149 | + count = count + 1 |
| 150 | + |
| 151 | + buf = msgpack.packb(count) + buf |
| 152 | + |
| 153 | + return buf |
0 commit comments