Interface SessionRepository
- All Known Implementing Classes:
InMemorySessionRepository,JdbcSessionRepository
appendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long)).- Since:
- 2.1.0
- Author:
- Christian Tzolov
-
Method Summary
Modifier and TypeMethodDescriptionvoidappendEvent(SessionEvent event) Appends one event to the log of the session named bySessionEvent.getSessionId().booleanappendEvents(String sessionId, List<SessionEvent> events, long expectedVersion) Appends several events in order, atomically, only if the log's version equalsexpectedVersion; otherwise nothing is written andfalseis returned.voidDeletes the session and its events.intdeleteExpiredSessions(Instant before) Deletes every session that expired before the given instant, with their events, in one atomic step per session, so a session whose expiry was extended concurrently (save(org.springframework.ai.session.Session)) is kept.@Nullable SessionfindByMetadata(String name, String value) findEvents(String sessionId, EventFilter filter) The events of the session the filter selects, oldest first; an empty list for a session that does not exist.longgetEventVersion(String sessionId) The version of the session's log, advanced on everyappendEvent(org.springframework.ai.session.SessionEvent)that appends a new event and on every successfulappendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long);0for a session that does not exist or has no events.Persists session metadata (create or update).booleansaveIfAbsent(Session session) Inserts the session unless one with the same id exists, atomically: a primary-key-guardedINSERT, or a compare-and-set put.
-
Method Details
-
save
Persists session metadata (create or update). If the session already exists its event log and its originalSession.createdAt()are preserved; the other metadata fields are replaced.- Returns:
- the saved session
-
saveIfAbsent
Inserts the session unless one with the same id exists, atomically: a primary-key-guardedINSERT, or a compare-and-set put.SessionService.create(org.springframework.ai.session.CreateSessionRequest)relies on it, so two concurrent creates of one id cannot both succeed.- Returns:
trueif the session was inserted,falseif one with the same id exists; the existing session is left untouched
-
findById
-
findByMetadata
The sessions whosemetadataholdsvalueundername, oldest first (creation time, then id). The one scoped lookup of the store, in the shape of Spring Session'sfindByIndexNameAndIndexValue: the store indexes attributes and has no notion of ownership. Every store indexesSessionAttributes.PRINCIPAL_NAME; a store that cannot index every key documents which others it answers for.- Parameters:
name- the metadata keyvalue- the value, compared as a string
-
delete
Deletes the session and its events. -
deleteExpiredSessions
Deletes every session that expired before the given instant, with their events, in one atomic step per session, so a session whose expiry was extended concurrently (save(org.springframework.ai.session.Session)) is kept.- Parameters:
before- the expiry cut-off- Returns:
- the number of sessions deleted
-
appendEvent
Appends one event to the log of the session named bySessionEvent.getSessionId().Idempotent by id: an event whose id is already in the session's log is a retried append and a no-op, neither a duplicate nor an error, and the version is not advanced. A caller that wants to retry safely supplies its own id rather than the builder's random one. Event ids are unique across sessions: a persistent store keys events by id alone and rejects an id used by another session rather than dropping the event.
- Throws:
IllegalArgumentException- if the session does not existIllegalStateException- if the id is used by an event of another session
-
appendEvents
Appends several events in order, atomically, only if the log's version equalsexpectedVersion; otherwise nothing is written andfalseis returned. On success the version advances once, whatever the number of events. This is how a compaction is recorded: oneEventType.COMPACTIONboundary and its summary events, with the version check keeping two concurrent compactions from both landing. ReadgetEventVersion(java.lang.String)beforefindEvents(java.lang.String, org.springframework.ai.session.EventFilter), then pass that version here.- Parameters:
sessionId- the session whose log is appended to; every event must belong to itevents- the events, in order; at least one, with ids that are new to the logexpectedVersion- the version the caller read- Returns:
truewhen the events were appended,falsewhen the version did not match- Throws:
IllegalArgumentException- if the session does not exist, the batch is empty, an event belongs to another session, or an id is already in the log or appears twice
-
getEventVersion
The version of the session's log, advanced on everyappendEvent(org.springframework.ai.session.SessionEvent)that appends a new event and on every successfulappendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long);0for a session that does not exist or has no events. Read it beforefindEvents(java.lang.String, org.springframework.ai.session.EventFilter), so that the version passed toappendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long)is not newer than the events read. -
findEvents
The events of the session the filter selects, oldest first; an empty list for a session that does not exist.A
EventFilter.Window.Lastwindow returns the newest N matching events, extended back to the start of the turn it lands in (the nearest precedingturn startamong the matches), so a tool call is never returned without its results; the extension is skipped when no turn start precedes the window. WithEventFilter.activeView()the filter applies to the active view, the events the newestEventType.COMPACTIONboundary names and everything appended after it, rather than to the whole log.EventFilter.apply(List)is the reference implementation of both. A store pushes the criteria and the window down to its query, so that a read does not cost the whole log, and may fetch only the rows a view can contain and hand them toapply, asJdbcSessionRepositorydoes.
-