|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.model.chat.memory.mongo.autoconfigure; |
| 18 | + |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.springframework.ai.chat.memory.mongo.Conversation; |
| 22 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 23 | +import org.springframework.context.event.ContextRefreshedEvent; |
| 24 | +import org.springframework.context.event.EventListener; |
| 25 | +import org.springframework.data.domain.Sort; |
| 26 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 27 | +import org.springframework.data.mongodb.core.index.Index; |
| 28 | +import org.springframework.stereotype.Component; |
| 29 | + |
| 30 | +/** |
| 31 | + * Class responsible for creating MongoDB proper indices for the ChatMemory. Creates a |
| 32 | + * main index on the conversationId and timestamp fields, and a TTL index on the timestamp |
| 33 | + * field if the TTL is set in properties. |
| 34 | + * |
| 35 | + * @author Łukasz Jernaś |
| 36 | + * @see MongoChatMemoryProperties |
| 37 | + * @since 1.0.0 |
| 38 | + */ |
| 39 | +@Component |
| 40 | +@ConditionalOnProperty(value = "spring.ai.chat.memory.mongodb.create-indices", havingValue = "true") |
| 41 | +public class MongoChatMemoryIndexCreator { |
| 42 | + |
| 43 | + private static final Logger logger = LoggerFactory.getLogger(MongoChatMemoryIndexCreator.class); |
| 44 | + |
| 45 | + private final MongoTemplate mongoTemplate; |
| 46 | + |
| 47 | + private final MongoChatMemoryProperties mongoChatMemoryProperties; |
| 48 | + |
| 49 | + public MongoChatMemoryIndexCreator(MongoTemplate mongoTemplate, |
| 50 | + MongoChatMemoryProperties mongoChatMemoryProperties) { |
| 51 | + this.mongoTemplate = mongoTemplate; |
| 52 | + this.mongoChatMemoryProperties = mongoChatMemoryProperties; |
| 53 | + } |
| 54 | + |
| 55 | + @EventListener(ContextRefreshedEvent.class) |
| 56 | + public void initIndicesAfterStartup() { |
| 57 | + logger.info("Creating MongoDB indices for ChatMemory"); |
| 58 | + // Create a main index |
| 59 | + mongoTemplate.indexOps(Conversation.class) |
| 60 | + .ensureIndex(new Index().on("conversationId", Sort.Direction.ASC).on("timestamp", Sort.Direction.DESC)); |
| 61 | + |
| 62 | + createOrUpdateTtlIndex(); |
| 63 | + } |
| 64 | + |
| 65 | + private void createOrUpdateTtlIndex() { |
| 66 | + if (!this.mongoChatMemoryProperties.getTtl().isZero()) { |
| 67 | + // Check for existing TTL index |
| 68 | + mongoTemplate.indexOps(Conversation.class).getIndexInfo().forEach(idx -> { |
| 69 | + if (idx.getExpireAfter().isPresent() |
| 70 | + && !idx.getExpireAfter().get().equals(this.mongoChatMemoryProperties.getTtl())) { |
| 71 | + logger.warn("Dropping existing TTL index, because TTL is different"); |
| 72 | + mongoTemplate.indexOps(Conversation.class).dropIndex(idx.getName()); |
| 73 | + } |
| 74 | + }); |
| 75 | + mongoTemplate.indexOps(Conversation.class) |
| 76 | + .ensureIndex(new Index().on("timestamp", Sort.Direction.ASC) |
| 77 | + .expire(this.mongoChatMemoryProperties.getTtl())); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments