2727import io .reactivex .rxjava3 .core .Single ;
2828import java .time .Instant ;
2929import java .util .ArrayList ;
30+ import java .util .Collection ;
3031import java .util .List ;
3132import java .util .Map ;
3233import java .util .Objects ;
3536import java .util .concurrent .ConcurrentHashMap ;
3637import java .util .concurrent .ConcurrentMap ;
3738import org .jspecify .annotations .Nullable ;
38- import org .slf4j .Logger ;
39- import org .slf4j .LoggerFactory ;
4039
4140/**
4241 * An in-memory implementation of {@link BaseSessionService} assuming {@link Session} objects are
5049 * during retrieval operations ({@code getSession}, {@code createSession}).
5150 */
5251public final class InMemorySessionService implements BaseSessionService {
53-
54- private static final Logger logger = LoggerFactory .getLogger (InMemorySessionService .class );
55-
5652 // Structure: appName -> userId -> sessionId -> Session
5753 private final ConcurrentMap <String , ConcurrentMap <String , ConcurrentMap <String , Session >>>
5854 sessions ;
@@ -178,9 +174,7 @@ public Single<ListSessionsResponse> listSessions(String appName, String userId)
178174
179175 // Create copies with empty events and state for the response
180176 List <Session > sessionCopies =
181- userSessionsMap .values ().stream ()
182- .map (this ::copySessionMetadata )
183- .collect (toCollection (ArrayList ::new ));
177+ prepareSessionsForListResponse (appName , userId , userSessionsMap .values ());
184178
185179 return Single .just (ListSessionsResponse .builder ().sessions (sessionCopies ).build ());
186180 }
@@ -298,21 +292,6 @@ private Session copySession(Session original) {
298292 .build ();
299293 }
300294
301- /**
302- * Creates a copy of the session containing only metadata fields (ID, appName, userId, timestamp).
303- * State and Events are explicitly *not* copied.
304- *
305- * @param original The session whose metadata to copy.
306- * @return A new Session instance with only metadata fields populated.
307- */
308- private Session copySessionMetadata (Session original ) {
309- return Session .builder (original .id ())
310- .appName (original .appName ())
311- .userId (original .userId ())
312- .lastUpdateTime (original .lastUpdateTime ())
313- .build ();
314- }
315-
316295 /**
317296 * Merges the app-specific and user-specific state into the provided *mutable* session's state
318297 * map.
@@ -338,4 +317,24 @@ private Session mergeWithGlobalState(String appName, String userId, Session sess
338317
339318 return session ;
340319 }
320+
321+ /**
322+ * Prepares copies of sessions for use in a {@code listSessions} response.
323+ *
324+ * <p>For each session provided, this method creates a deep copy, clears the copy's event list,
325+ * and merges app-level and user-level state into the copy's state map.
326+ *
327+ * @param appName The application name.
328+ * @param userId The user ID.
329+ * @param sessions The collection of sessions to process.
330+ * @return A list of processed {@link Session} copies.
331+ */
332+ private List <Session > prepareSessionsForListResponse (
333+ String appName , String userId , Collection <Session > sessions ) {
334+ return sessions .stream ()
335+ .map (this ::copySession )
336+ .peek (s -> s .events ().clear ())
337+ .map (s -> mergeWithGlobalState (appName , userId , s ))
338+ .collect (toCollection (ArrayList ::new ));
339+ }
341340}
0 commit comments