Skip to content

Commit 6cc9d2f

Browse files
committed
optimize mongodb init
1 parent a42b058 commit 6cc9d2f

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Repositories.Settings;
2+
using System.Threading;
23

34
namespace BotSharp.Plugin.MongoStorage;
45

@@ -7,6 +8,7 @@ public class MongoDbContext
78
private readonly MongoClient _mongoClient;
89
private readonly string _mongoDbDatabaseName;
910
private readonly string _collectionPrefix;
11+
private static int _indexesInitialized = 0;
1012

1113
private const string DB_NAME_INDEX = "authSource";
1214

@@ -16,6 +18,7 @@ public MongoDbContext(BotSharpDatabaseSettings dbSettings)
1618
_mongoClient = new MongoClient(mongoDbConnectionString);
1719
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString);
1820
_collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp")!;
21+
CreateIndexes();
1922
}
2023

2124
private string GetDatabaseName(string mongoDbConnectionString)
@@ -58,6 +61,19 @@ private bool CollectionExists(IMongoDatabase database, string collectionName)
5861
return collections.Any();
5962
}
6063

64+
private IMongoCollection<TDocument> GetCollection<TDocument>(string name)
65+
{
66+
if (string.IsNullOrWhiteSpace(name))
67+
{
68+
throw new ArgumentException($"The collection {name} cannot be empty.");
69+
}
70+
71+
var collectionName = $"{_collectionPrefix}_{name}";
72+
73+
var collection = Database.GetCollection<TDocument>(collectionName);
74+
return collection;
75+
}
76+
6177
private IMongoCollection<TDocument> GetCollectionOrCreate<TDocument>(string name)
6278
{
6379
if (string.IsNullOrWhiteSpace(name))
@@ -76,6 +92,25 @@ private IMongoCollection<TDocument> GetCollectionOrCreate<TDocument>(string name
7692
}
7793

7894
#region Indexes
95+
96+
public void CreateIndexes()
97+
{
98+
// Use Interlocked.CompareExchange to ensure the index is initialized only once, ensuring thread safety
99+
// 0 indicates uninitialized, 1 indicates initialized
100+
if (Interlocked.CompareExchange(ref _indexesInitialized, 1, 0) != 0)
101+
{
102+
return;
103+
}
104+
105+
// Perform index creation (only executed on the first call).)
106+
CreateConversationIndex();
107+
CreateConversationStateIndex();
108+
CreateContentLogIndex();
109+
CreateStateLogIndex();
110+
CreateInstructionLogIndex();
111+
CreateAgentCodeScriptIndex();
112+
CreateAgentTaskIndex();
113+
}
79114
private IMongoCollection<AgentCodeScriptDocument> CreateAgentCodeScriptIndex()
80115
{
81116
var collection = GetCollectionOrCreate<AgentCodeScriptDocument>("AgentCodeScripts");
@@ -179,62 +214,62 @@ private void CreateIndex<T>(IMongoCollection<T> collection, IndexKeysDefinition<
179214
#endregion
180215

181216
public IMongoCollection<AgentDocument> Agents
182-
=> GetCollectionOrCreate<AgentDocument>("Agents");
217+
=> GetCollection<AgentDocument>("Agents");
183218

184219
public IMongoCollection<AgentTaskDocument> AgentTasks
185-
=> CreateAgentTaskIndex();
220+
=> GetCollection<AgentTaskDocument>("AgentTasks");
186221

187222
public IMongoCollection<AgentCodeScriptDocument> AgentCodeScripts
188-
=> CreateAgentCodeScriptIndex();
223+
=> GetCollection<AgentCodeScriptDocument>("AgentCodeScripts");
189224

190225
public IMongoCollection<ConversationDocument> Conversations
191-
=> CreateConversationIndex();
226+
=> GetCollection<ConversationDocument>("Conversations");
192227

193228
public IMongoCollection<ConversationDialogDocument> ConversationDialogs
194-
=> GetCollectionOrCreate<ConversationDialogDocument>("ConversationDialogs");
229+
=> GetCollection<ConversationDialogDocument>("ConversationDialogs");
195230

196231
public IMongoCollection<ConversationStateDocument> ConversationStates
197-
=> CreateConversationStateIndex();
232+
=> GetCollection<ConversationStateDocument>("ConversationStates");
198233

199234
public IMongoCollection<LlmCompletionLogDocument> LlmCompletionLogs
200-
=> GetCollectionOrCreate<LlmCompletionLogDocument>("LlmCompletionLogs");
235+
=> GetCollection<LlmCompletionLogDocument>("LlmCompletionLogs");
201236

202237
public IMongoCollection<ConversationContentLogDocument> ContentLogs
203-
=> CreateContentLogIndex();
238+
=> GetCollection<ConversationContentLogDocument>("ConversationContentLogs");
204239

205240
public IMongoCollection<ConversationStateLogDocument> StateLogs
206-
=> CreateStateLogIndex();
241+
=> GetCollection<ConversationStateLogDocument>("ConversationStateLogs");
207242

208243
public IMongoCollection<UserDocument> Users
209-
=> GetCollectionOrCreate<UserDocument>("Users");
244+
=> GetCollection<UserDocument>("Users");
210245

211246
public IMongoCollection<UserAgentDocument> UserAgents
212-
=> GetCollectionOrCreate<UserAgentDocument>("UserAgents");
247+
=> GetCollection<UserAgentDocument>("UserAgents");
213248

214249
public IMongoCollection<PluginDocument> Plugins
215-
=> GetCollectionOrCreate<PluginDocument>("Plugins");
250+
=> GetCollection<PluginDocument>("Plugins");
216251

217252
public IMongoCollection<TranslationMemoryDocument> TranslationMemories
218-
=> GetCollectionOrCreate<TranslationMemoryDocument>("TranslationMemories");
253+
=> GetCollection<TranslationMemoryDocument>("TranslationMemories");
219254

220255
public IMongoCollection<KnowledgeCollectionConfigDocument> KnowledgeCollectionConfigs
221-
=> GetCollectionOrCreate<KnowledgeCollectionConfigDocument>("KnowledgeCollectionConfigs");
256+
=> GetCollection<KnowledgeCollectionConfigDocument>("KnowledgeCollectionConfigs");
222257

223258
public IMongoCollection<KnowledgeCollectionFileMetaDocument> KnowledgeCollectionFileMeta
224-
=> GetCollectionOrCreate<KnowledgeCollectionFileMetaDocument>("KnowledgeCollectionFileMeta");
259+
=> GetCollection<KnowledgeCollectionFileMetaDocument>("KnowledgeCollectionFileMeta");
225260

226261
public IMongoCollection<RoleDocument> Roles
227-
=> GetCollectionOrCreate<RoleDocument>("Roles");
262+
=> GetCollection<RoleDocument>("Roles");
228263

229264
public IMongoCollection<RoleAgentDocument> RoleAgents
230-
=> GetCollectionOrCreate<RoleAgentDocument>("RoleAgents");
265+
=> GetCollection<RoleAgentDocument>("RoleAgents");
231266

232267
public IMongoCollection<CrontabItemDocument> CrontabItems
233-
=> GetCollectionOrCreate<CrontabItemDocument>("CronTabItems");
268+
=> GetCollection<CrontabItemDocument>("CronTabItems");
234269

235270
public IMongoCollection<GlobalStatisticsDocument> GlobalStats
236-
=> GetCollectionOrCreate<GlobalStatisticsDocument>("GlobalStats");
271+
=> GetCollection<GlobalStatisticsDocument>("GlobalStats");
237272

238273
public IMongoCollection<InstructionLogDocument> InstructionLogs
239-
=> CreateInstructionLogIndex();
274+
=> GetCollection<InstructionLogDocument>("InstructionLogs");
240275
}

0 commit comments

Comments
 (0)