Skip to content

May I ask if you accept partial code style optimization. #882

Description

@zfchai

MongoBase

  • add default non empty constraint.
namespaceBotSharp.Plugin.MongoStorage;[BsonIgnoreExtraElements(Inherited=true)]publicabstractclassMongoBase{[BsonId(IdGenerator=typeof(StringGuidIdGenerator))]publicstringId{get;set;}=default!;}

MongoDbContext.cs

  • add the SelectionExists & GetCollectionOrCreate method;
namespaceBotSharp.Plugin.MongoStorage;publicclassMongoDbContext{privatereadonlyMongoClient_mongoClient;privatereadonlystring_mongoDbDatabaseName;privatereadonlystring_collectionPrefix;privateconststringDB_NAME_INDEX="authSource";publicMongoDbContext(BotSharpDatabaseSettingsdbSettings){varmongoDbConnectionString=dbSettings.BotSharpMongoDb;_mongoClient=newMongoClient(mongoDbConnectionString);_mongoDbDatabaseName=GetDatabaseName(mongoDbConnectionString);_collectionPrefix=dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp");}privatestringGetDatabaseName(stringmongoDbConnectionString){vardatabaseName=mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/",StringComparison.InvariantCultureIgnoreCase)+1);varsymbol="?";if(databaseName.Contains(symbol)){varmarkIdx=databaseName.IndexOf(symbol,StringComparison.InvariantCultureIgnoreCase);vardb=databaseName.Substring(0,markIdx);if(!string.IsNullOrWhiteSpace(db)){returndb;}varqueryStr=databaseName.Substring(markIdx+1);varqueries=queryStr.Split("&",StringSplitOptions.RemoveEmptyEntries).Select(x =>new{Key=x.Split("=")[0],Value=x.Split("=")[1]}).ToList();varsource=queries.FirstOrDefault(x =>x.Key.IsEqualTo(DB_NAME_INDEX));if(source!=null){databaseName=source.Value;}}returndatabaseName;}privateIMongoDatabaseDatabase=>_mongoClient.GetDatabase(_mongoDbDatabaseName);privateboolCollectionExists(IMongoDatabasedatabase,stringcollectionName){varfilter=Builders<BsonDocument>.Filter.Eq("name",collectionName);varcollections=database.ListCollections(newListCollectionsOptions{Filter=filter});returncollections.Any();}privateIMongoCollection<TDocument>GetCollectionOrCreate<TDocument>(stringname){if(string.IsNullOrWhiteSpace(name))thrownewArgumentException($"The collection {name} cannot be empty.");stringcollectionName=$"{_collectionPrefix}_{name}";if(!CollectionExists(Database,collectionName))Database.CreateCollection(collectionName);varcollection=Database.GetCollection<TDocument>(collectionName);returncollection;}
#region Indexes
privateIMongoCollection<ConversationDocument>CreateConversationIndex(){varcollection=GetCollectionOrCreate<ConversationDocument>("Conversations");varindexes=collection.Indexes.List().ToList();varcreateTimeIndex=indexes.FirstOrDefault(x =>x.GetElement("name").ToString().StartsWith("CreatedTime"));if(createTimeIndex==null){varindexDef=Builders<ConversationDocument>.IndexKeys.Descending(x =>x.CreatedTime);collection.Indexes.CreateOne(newCreateIndexModel<ConversationDocument>(indexDef));}returncollection;}privateIMongoCollection<ConversationStateDocument>CreateConversationStateIndex(){varcollection=GetCollectionOrCreate<ConversationStateDocument>($"ConversationStates");varindexes=collection.Indexes.List().ToList();varstateIndex=indexes.FirstOrDefault(x =>x.GetElement("name").ToString().StartsWith("States.Key"));if(stateIndex==null){varindexDef=Builders<ConversationStateDocument>.IndexKeys.Ascending("States.Key");collection.Indexes.CreateOne(newCreateIndexModel<ConversationStateDocument>(indexDef));}returncollection;}privateIMongoCollection<AgentTaskDocument>CreateAgentTaskIndex(){varcollection=GetCollectionOrCreate<AgentTaskDocument>($"AgentTasks");varindexes=collection.Indexes.List().ToList();varcreateTimeIndex=indexes.FirstOrDefault(x =>x.GetElement("name").ToString().StartsWith("CreatedTime"));if(createTimeIndex==null){varindexDef=Builders<AgentTaskDocument>.IndexKeys.Descending(x =>x.CreatedTime);collection.Indexes.CreateOne(newCreateIndexModel<AgentTaskDocument>(indexDef));}returncollection;}privateIMongoCollection<ConversationContentLogDocument>CreateContentLogIndex(){varcollection=GetCollectionOrCreate<ConversationContentLogDocument>($"ConversationContentLogs");varindexes=collection.Indexes.List().ToList();varcreateTimeIndex=indexes.FirstOrDefault(x =>x.GetElement("name").ToString().StartsWith("CreateTime"));if(createTimeIndex==null){varindexDef=Builders<ConversationContentLogDocument>.IndexKeys.Ascending(x =>x.CreateTime);collection.Indexes.CreateOne(newCreateIndexModel<ConversationContentLogDocument>(indexDef));}returncollection;}privateIMongoCollection<ConversationStateLogDocument>CreateStateLogIndex(){varcollection=GetCollectionOrCreate<ConversationStateLogDocument>($"ConversationStateLogs");varindexes=collection.Indexes.List().ToList();varcreateTimeIndex=indexes.FirstOrDefault(x =>x.GetElement("name").ToString().StartsWith("CreateTime"));if(createTimeIndex==null){varindexDef=Builders<ConversationStateLogDocument>.IndexKeys.Ascending(x =>x.CreateTime);collection.Indexes.CreateOne(newCreateIndexModel<ConversationStateLogDocument>(indexDef));}returncollection;}
#endregion
publicIMongoCollection<AgentDocument>Agents=>GetCollectionOrCreate<AgentDocument>("Agents");publicIMongoCollection<AgentTaskDocument>AgentTasks=>CreateAgentTaskIndex();publicIMongoCollection<ConversationDocument>Conversations=>CreateConversationIndex();publicIMongoCollection<ConversationDialogDocument>ConversationDialogs=>GetCollectionOrCreate<ConversationDialogDocument>("ConversationDialogs");publicIMongoCollection<ConversationStateDocument>ConversationStates=>CreateConversationStateIndex();publicIMongoCollection<ExecutionLogDocument>ExectionLogs=>GetCollectionOrCreate<ExecutionLogDocument>("ExecutionLogs");publicIMongoCollection<LlmCompletionLogDocument>LlmCompletionLogs=>GetCollectionOrCreate<LlmCompletionLogDocument>("LlmCompletionLogs");publicIMongoCollection<ConversationContentLogDocument>ContentLogs=>CreateContentLogIndex();publicIMongoCollection<ConversationStateLogDocument>StateLogs=>CreateStateLogIndex();publicIMongoCollection<UserDocument>Users=>GetCollectionOrCreate<UserDocument>("Users");publicIMongoCollection<UserAgentDocument>UserAgents=>GetCollectionOrCreate<UserAgentDocument>("UserAgents");publicIMongoCollection<PluginDocument>Plugins=>GetCollectionOrCreate<PluginDocument>("Plugins");publicIMongoCollection<TranslationMemoryDocument>TranslationMemories=>GetCollectionOrCreate<TranslationMemoryDocument>("TranslationMemories");publicIMongoCollection<KnowledgeCollectionConfigDocument>KnowledgeCollectionConfigs=>GetCollectionOrCreate<KnowledgeCollectionConfigDocument>("KnowledgeCollectionConfigs");publicIMongoCollection<KnowledgeCollectionFileMetaDocument>KnowledgeCollectionFileMeta=>GetCollectionOrCreate<KnowledgeCollectionFileMetaDocument>("KnowledgeCollectionFileMeta");publicIMongoCollection<RoleDocument>Roles=>GetCollectionOrCreate<RoleDocument>("Roles");publicIMongoCollection<RoleAgentDocument>RoleAgents=>GetCollectionOrCreate<RoleAgentDocument>("RoleAgents");publicIMongoCollection<CrontabItemDocument>CrontabItems=>GetCollectionOrCreate<CrontabItemDocument>("CronTabItems");publicIMongoCollection<GlobalStatisticsDocument>GlobalStatistics=>GetCollectionOrCreate<GlobalStatisticsDocument>("GlobalStatistics");}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions