Skip to content

Commit

Permalink
[Core] database add indexed (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkRRb authored Aug 21, 2024
1 parent 8b56d09 commit 82374b9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 17 additions & 2 deletions Lagrange.OneBot/Core/Notify/NotifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
using Lagrange.OneBot.Core.Entity.Notify;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Database;
using LiteDB;
using Microsoft.Extensions.Logging;

namespace Lagrange.OneBot.Core.Notify;

public sealed class NotifyService(BotContext bot, ILogger<NotifyService> logger, LagrangeWebSvcCollection service)
public sealed class NotifyService(BotContext bot, ILogger<NotifyService> logger, LagrangeWebSvcCollection service, LiteDatabase database)
{
public void RegisterEvents()
{
Expand Down Expand Up @@ -195,10 +196,24 @@ await service.SendJsonAsync(new OneBotGroupEssence(bot.BotUin)
{
logger.LogInformation(@event.ToString());

var record = database.GetCollection<MessageRecord>().FindOne(Query.And(
Query.EQ("GroupUin", new BsonValue(@event.TargetGroupUin)),
Query.EQ("Sequence", new BsonValue(@event.TargetSequence))
));

if (record == null)
{
logger.LogInformation(
"Unable to find the corresponding message using GroupUin: {} and Sequence: {}",
@event.TargetGroupUin,
@event.TargetSequence
);
}

await service.SendJsonAsync(new OneBotGroupReaction(
bot.BotUin,
@event.TargetGroupUin,
0,
record?.MessageHash ?? 0,
@event.OperatorUin,
@event.IsAdd ? "add" : "remove",
@event.Code,
Expand Down
12 changes: 11 additions & 1 deletion Lagrange.OneBot/LagrangeAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
using Lagrange.OneBot.Core.Network.Service;
using Lagrange.OneBot.Core.Notify;
using Lagrange.OneBot.Core.Operation;
using Lagrange.OneBot.Database;
using Lagrange.OneBot.Message;
using Lagrange.OneBot.Utility;
using LiteDB;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Lagrange.OneBot;
Expand Down Expand Up @@ -108,14 +110,22 @@ public LagrangeAppBuilder ConfigureOneBot()
return services.GetRequiredService<ILagrangeWebServiceFactory>().Create() ?? throw new Exception("Invalid conf detected");
});

Services.AddSingleton(x =>
Services.AddSingleton(provider =>
{
var logger = provider.GetRequiredService<ILogger<LagrangeAppBuilder>>();

string path = Configuration["ConfigPath:Database"] ?? $"lagrange-{Configuration["Account:Uin"]}.db";

var db = new LiteDatabase(path)
{
CheckpointSize = 50
};
logger.LogInformation("Indexing in the database...");
logger.LogInformation("The first indexing of the old database will load the entire database into memory.");
logger.LogInformation("If this is the first time creating an index for the old database, please restart the application");
var collection = db.GetCollection<MessageRecord>();
collection.EnsureIndex(record => record.MessageId);
collection.EnsureIndex(record => record.Sequence);
return db;
});
Services.AddSingleton<SignProvider, OneBotSigner>();
Expand Down

0 comments on commit 82374b9

Please sign in to comment.