mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
perf: make EventStore cur_id a lazy calculated property (#9544)
Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
parent
21a5e3eed5
commit
229f35093d
@ -49,12 +49,23 @@ class EventStore(EventStoreABC):
|
||||
sid: str
|
||||
file_store: FileStore
|
||||
user_id: str | None
|
||||
cur_id: int = -1 # We fix this in post init if it is not specified
|
||||
cache_size: int = 25
|
||||
_cur_id: int | None = None # Private field to cache the calculated value
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if self.cur_id >= 0:
|
||||
return
|
||||
@property
|
||||
def cur_id(self) -> int:
|
||||
"""Lazy calculated property for the current event ID."""
|
||||
if self._cur_id is None:
|
||||
self._cur_id = self._calculate_cur_id()
|
||||
return self._cur_id
|
||||
|
||||
@cur_id.setter
|
||||
def cur_id(self, value: int) -> None:
|
||||
"""Setter for cur_id to allow updates."""
|
||||
self._cur_id = value
|
||||
|
||||
def _calculate_cur_id(self) -> int:
|
||||
"""Calculate the current event ID based on file system content."""
|
||||
events = []
|
||||
try:
|
||||
events_dir = get_conversation_events_dir(self.sid, self.user_id)
|
||||
@ -63,14 +74,15 @@ class EventStore(EventStoreABC):
|
||||
logger.debug(f'No events found for session {self.sid} at {events_dir}')
|
||||
|
||||
if not events:
|
||||
self.cur_id = 0
|
||||
return
|
||||
return 0
|
||||
|
||||
# if we have events, we need to find the highest id to prepare for new events
|
||||
max_id = -1
|
||||
for event_str in events:
|
||||
id = self._get_id_from_filename(event_str)
|
||||
if id >= self.cur_id:
|
||||
self.cur_id = id + 1
|
||||
if id >= max_id:
|
||||
max_id = id
|
||||
return max_id + 1
|
||||
|
||||
def search_events(
|
||||
self,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user