22
33import datetime
44import json
5- from typing import Dict , Optional , List , Any , TYPE_CHECKING , Callable
5+ from typing import Dict , Optional , List , Any , TYPE_CHECKING , Callable , Set
66import requests
77
88from ravendb .primitives .constants import int_max
@@ -357,6 +357,12 @@ def __init__(self, timestamp: datetime.datetime, values: List[float], tag: Optio
357357 self .values = values
358358 self .tag = tag
359359
360+ def __eq__ (self , other ):
361+ return isinstance (other , TimeSeriesOperation .AppendOperation ) and other .timestamp == self .timestamp
362+
363+ def __hash__ (self ):
364+ return hash (self .timestamp )
365+
360366 def to_json (self ) -> Dict [str , Any ]:
361367 json_dict = {
362368 "Timestamp" : Utils .datetime_to_string (self .timestamp ),
@@ -382,7 +388,7 @@ def to_json(self) -> Dict[str, Any]:
382388
383389 def __init__ (self , name : Optional [str ] = None ):
384390 self .name = name
385- self ._appends : List [TimeSeriesOperation .AppendOperation ] = []
391+ self ._appends : Set [TimeSeriesOperation .AppendOperation ] = set ()
386392 self ._deletes : List [TimeSeriesOperation .DeleteOperation ] = []
387393
388394 def to_json (self ) -> Dict [str , Any ]:
@@ -395,14 +401,13 @@ def to_json(self) -> Dict[str, Any]:
395401
396402 def append (self , append_operation : AppendOperation ) -> None :
397403 if self ._appends is None :
398- self ._appends = [] # todo: perf
399- filtered = self ._appends
404+ self ._appends = set ()
400405
401- # if len(filtered) != 0 :
402- # # element with given timestamp already exists - remove and retry add operation
403- # self._appends.remove(filtered.pop() )
406+ if append_operation in self . _appends :
407+ # __eq__ override lets us discard old one by passing new one due to the same timestamps
408+ self ._appends .discard ( append_operation )
404409
405- self ._appends .append (append_operation )
410+ self ._appends .add (append_operation )
406411
407412 def delete (self , delete_operation : DeleteOperation ) -> None :
408413 if self ._deletes is None :
0 commit comments