@@ -24,15 +24,14 @@ class TrySwapResponse:
2424 ttl : Optional [int ] = None
2525
2626class SwapStore :
27- def __init__ (self ):
27+ def __init__ (self ) -> None :
2828 # key -> UnmatchedOffer
2929 self .unmatched_offer_map : Dict [str , UnmatchedOffer ] = {}
3030 # key -> sid -> SwappedAnswer
3131 self .swapped_answer_multimap : Dict [str , Dict [str , SwappedAnswer ]] = {}
3232 self .ttl = MAX_TTL_SECONDS
3333
3434 def expire_swapped_answers (self , key : str , now_ms : float ) -> bool :
35- """Presumably, data was exchanged."""
3635 answer_map = self .swapped_answer_multimap .get (key )
3736 if not answer_map :
3837 return True
@@ -52,7 +51,6 @@ def expire_swapped_answers(self, key: str, now_ms: float) -> bool:
5251 return False
5352
5453 def expire_unmatched_offers (self , now_ms : float ) -> None :
55- """No takers."""
5654 expiring_offers = []
5755 # Python dicts maintain insertion order, allowing this logic to work like JS Map
5856 for key , v in self .unmatched_offer_map .items ():
@@ -68,16 +66,11 @@ def expire_unmatched_offers(self, now_ms: float) -> None:
6866 @staticmethod
6967 def matches_original (original_values : List [str ], offset : int , values : List [str ]) -> bool :
7068 if len (original_values ) < offset :
71- return False # Too far ahead. Out of place.
69+ return False
7270 if len (original_values ) > offset + len (values ):
73- return False # Too short. Out of place.
71+ return False
7472
7573 original_slice = original_values [offset :]
76- # Compare element by element
77- # JS logic: original_slice.every((v, i) => values[i] == v)
78- # This implies values must match original_slice where they overlap.
79- # Since we already checked that original_values isn't longer than offset+values,
80- # we know values covers original_slice entirely (or extends it).
8174
8275 for i , v in enumerate (original_slice ):
8376 if values [i ] != v :
@@ -111,10 +104,9 @@ def tryswap(self, key: str, sid: str, offset: int, values: List[str], now_ms: fl
111104 offer = self .unmatched_offer_map .get (key )
112105 if offer and offer .expiry_ms == 0 :
113106 del self .unmatched_offer_map [key ]
114- offer = None # Fall through to next case.
115- self .expire_swapped_answers (key , now_ms ) # Ensure stuff would expire.
107+ offer = None
108+ self .expire_swapped_answers (key , now_ms )
116109
117- # We'll need to make an offer.
118110 if not offer :
119111 if offset != 0 :
120112 return 404
@@ -131,15 +123,9 @@ def tryswap(self, key: str, sid: str, offset: int, values: List[str], now_ms: fl
131123 ttl = ttl ,
132124 )
133125
134- # Still no match? Might as well reset expiry.
135126 if offer .sid == sid :
136127 original_values = offer .values or []
137128 if SwapStore .matches_original (original_values , offset , values ):
138- # Update offer with new values appended
139- # Note: original code slices then concats.
140- # original_values.slice(0, offset).concat(values)
141- # But match checks that values matches suffix of original_values starting at offset.
142- # So if matched, we are extending the offer.
143129 new_values = original_values [:offset ] + values
144130
145131 self .unmatched_offer_map [key ] = UnmatchedOffer (
@@ -156,7 +142,6 @@ def tryswap(self, key: str, sid: str, offset: int, values: List[str], now_ms: fl
156142 return 404
157143
158144 if offset != 0 :
159- # Invalid offset. We had no existing data!
160145 return 404
161146
162147 if answer_map is None :
@@ -169,20 +154,13 @@ def tryswap(self, key: str, sid: str, offset: int, values: List[str], now_ms: fl
169154 expiry_ms = now_ms + ttl * 1000 ,
170155 )
171156
172- # offer.sid is guaranteed to exist and offer.sid != sid here.
173157 if offer .sid :
174158 answer_map [offer .sid ] = SwappedAnswer (
175159 original_values = offer .values or [],
176160 values = values ,
177161 expiry_ms = now_ms + ttl * 1000 ,
178162 )
179163
180- # Remove the unmatched offer as it is now matched
181- # But instead of deleting, we mark it as expired (expiry_ms=0)
182- # so it can be cleaned up later or overwritten?
183- # JS code:
184- # this.unmatched_offer_map.delete(key);
185- # this.unmatched_offer_map.set(key, { expiry_ms: 0 });
186164 del self .unmatched_offer_map [key ]
187165 self .unmatched_offer_map [key ] = UnmatchedOffer (expiry_ms = 0 )
188166
0 commit comments