Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create collection index #299

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public MongoDbContext(BotSharpDatabaseSettings dbSettings)
_mongoClient = new MongoClient(mongoDbConnectionString);
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString);
_collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp");
//CreateIndex();
}

private string GetDatabaseName(string mongoDbConnectionString)
Expand All @@ -28,14 +29,52 @@ private string GetDatabaseName(string mongoDbConnectionString)

private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } }

private IMongoCollection<ConversationDocument> CreateConversationIndex()
{
var collection = Database.GetCollection<ConversationDocument>($"{_collectionPrefix}_Conversations");
var indexes = collection.Indexes.List().ToList();
var createTimeIndex = indexes.FirstOrDefault(x => x.GetElement("name").ToString().StartsWith("CreatedTime"));
if (createTimeIndex == null)
{
var indexDef = Builders<ConversationDocument>.IndexKeys.Descending(x => x.CreatedTime);
collection.Indexes.CreateOne(new CreateIndexModel<ConversationDocument>(indexDef));
}

return collection;
}

private IMongoCollection<AgentTaskDocument> CreateAgentTaskIndex()
{
var collection = Database.GetCollection<AgentTaskDocument>($"{_collectionPrefix}_AgentTasks");
var indexes = collection.Indexes.List().ToList();
var createTimeIndex = indexes.FirstOrDefault(x => x.GetElement("name").ToString().StartsWith("CreatedTime"));
if (createTimeIndex == null)
{
var indexDef = Builders<AgentTaskDocument>.IndexKeys.Descending(x => x.CreatedTime);
collection.Indexes.CreateOne(new CreateIndexModel<AgentTaskDocument>(indexDef));
}

return collection;
}

public IMongoCollection<AgentDocument> Agents
=> Database.GetCollection<AgentDocument>($"{_collectionPrefix}_Agents");

public IMongoCollection<AgentTaskDocument> AgentTasks
=> Database.GetCollection<AgentTaskDocument>($"{_collectionPrefix}_AgentTasks");
{
get
{
return CreateAgentTaskIndex();
}
}

public IMongoCollection<ConversationDocument> Conversations
=> Database.GetCollection<ConversationDocument>($"{_collectionPrefix}_Conversations");
{
get
{
return CreateConversationIndex();
}
}

public IMongoCollection<ConversationDialogDocument> ConversationDialogs
=> Database.GetCollection<ConversationDialogDocument>($"{_collectionPrefix}_ConversationDialogs");
Expand Down