Skip to content

Commit 8cb248a

Browse files
committed
RDBC-767 Time Series AppendOperation perf
1 parent 844ba7a commit 8cb248a

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

ravendb/documents/operations/time_series.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import datetime
44
import json
5-
from typing import Dict, Optional, List, Any, TYPE_CHECKING, Callable
5+
from typing import Dict, Optional, List, Any, TYPE_CHECKING, Callable, Set
66
import requests
77

88
from 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:

ravendb/tests/operations_tests/test_time_series_operation.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import unittest
1010

1111
from ravendb.tests.test_base import TestBase, User
12+
from ravendb.tools.raven_test_helper import RavenTestHelper
1213

1314

1415
class TestTimeSeriesOperations(TestBase):
@@ -83,6 +84,23 @@ def test_get_time_series_without_range(self):
8384
ts_range_result = self.store.operations.send(GetTimeSeriesOperation("users/1-A", self.ts_name))
8485
self.assertEqual(len(ts_range_result.entries), 3)
8586

87+
def test_appending_second_time_series_value_at_the_same_timestamp_will_replace_the_previous_value(self):
88+
base_line = RavenTestHelper.utc_this_month()
89+
with self.store.open_session() as session:
90+
session.store(User("Gracjan"), "users/gracjan")
91+
tsf = session.time_series_for("users/gracjan", "stonks")
92+
tsf.append_single(base_line, 1)
93+
tsf.append_single(base_line, 2)
94+
tsf.append_single(base_line, 3)
95+
session.save_changes()
96+
97+
with self.store.open_session() as session:
98+
gracjan = session.load("users/gracjan", User)
99+
ts = session.time_series_for_entity(gracjan, "stonks").get()
100+
self.assertEqual(1, len(ts))
101+
self.assertEqual(1, len(ts[0].values))
102+
self.assertEqual(3, ts[0].values[0])
103+
86104

87105
if __name__ == "__main__":
88106
unittest.main()

0 commit comments

Comments
 (0)