Class InMemorySessionService

java.lang.Object
com.google.adk.sessions.InMemorySessionService
All Implemented Interfaces:
BaseSessionService

public final class InMemorySessionService extends Object implements BaseSessionService
An in-memory implementation of BaseSessionService assuming Session objects are mutable regarding their state map, events list, and last update time.

This implementation stores sessions, user state, and app state directly in memory using concurrent maps for basic thread safety. It is suitable for testing or single-node deployments where persistence is not required.

Note: State merging (app/user state prefixed with _app_ / _user_) occurs during retrieval operations (getSession, createSession).

  • Constructor Details

    • InMemorySessionService

      public InMemorySessionService()
      Creates a new instance of the in-memory session service with empty storage.
  • Method Details

    • createSession

      public io.reactivex.rxjava3.core.Single<Session> createSession(String appName, String userId, @Nullable ConcurrentMap<String,Object> state, @Nullable String sessionId)
      Description copied from interface: BaseSessionService
      Creates a new session with the specified parameters.
      Specified by:
      createSession in interface BaseSessionService
      Parameters:
      appName - The name of the application associated with the session.
      userId - The identifier for the user associated with the session.
      state - An optional map representing the initial state of the session. Can be null or empty.
      sessionId - An optional client-provided identifier for the session. If empty or null, the service should generate a unique ID.
      Returns:
      The newly created Session instance.
    • getSession

      public io.reactivex.rxjava3.core.Maybe<Session> getSession(String appName, String userId, String sessionId, Optional<GetSessionConfig> configOpt)
      Description copied from interface: BaseSessionService
      Retrieves a specific session, optionally filtering the events included.
      Specified by:
      getSession in interface BaseSessionService
      Parameters:
      appName - The name of the application.
      userId - The identifier of the user.
      sessionId - The unique identifier of the session to retrieve.
      configOpt - Optional configuration to filter the events returned within the session (e.g., limit number of recent events, filter by timestamp). If empty, default retrieval behavior is used (potentially all events or a service-defined limit).
      Returns:
      An Optional containing the Session if found, otherwise Optional.empty().
    • listSessions

      public io.reactivex.rxjava3.core.Single<ListSessionsResponse> listSessions(String appName, String userId)
      Description copied from interface: BaseSessionService
      Lists sessions associated with a specific application and user.

      The Session objects in the response typically contain only metadata (like ID, creation time) and not the full event list or state to optimize performance.

      Specified by:
      listSessions in interface BaseSessionService
      Parameters:
      appName - The name of the application.
      userId - The identifier of the user whose sessions are to be listed.
      Returns:
      A ListSessionsResponse containing a list of matching sessions.
    • deleteSession

      public io.reactivex.rxjava3.core.Completable deleteSession(String appName, String userId, String sessionId)
      Description copied from interface: BaseSessionService
      Deletes a specific session.
      Specified by:
      deleteSession in interface BaseSessionService
      Parameters:
      appName - The name of the application.
      userId - The identifier of the user.
      sessionId - The unique identifier of the session to delete.
    • listEvents

      public io.reactivex.rxjava3.core.Single<ListEventsResponse> listEvents(String appName, String userId, String sessionId)
      Description copied from interface: BaseSessionService
      Lists the events within a specific session. Supports pagination via the response object.
      Specified by:
      listEvents in interface BaseSessionService
      Parameters:
      appName - The name of the application.
      userId - The identifier of the user.
      sessionId - The unique identifier of the session whose events are to be listed.
      Returns:
      A ListEventsResponse containing a list of events and an optional token for retrieving the next page.
    • appendEvent

      @CanIgnoreReturnValue public io.reactivex.rxjava3.core.Single<Event> appendEvent(Session session, Event event)
      Description copied from interface: BaseSessionService
      Appends an event to an in-memory session object and updates the session's state based on the event's state delta, if applicable.

      This method primarily modifies the passed session object in memory. Persisting these changes typically requires a separate call to an update/save method provided by the specific service implementation, or might happen implicitly depending on the implementation's design.

      If the event is marked as partial (e.g., event.isPartial() == true), it is returned directly without modifying the session state or event list. State delta keys starting with State.TEMP_PREFIX are ignored during state updates.

      Specified by:
      appendEvent in interface BaseSessionService
      Parameters:
      session - The Session object to which the event should be appended (will be mutated).
      event - The Event to append.
      Returns:
      The appended Event instance (or the original event if it was partial).