Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
ef6ff45f40 | |||
3d5b7db864 | |||
9f29202764 | |||
47abd5b838 | |||
430e1d8617 |
28
.github/workflows/on-pr.yml
vendored
28
.github/workflows/on-pr.yml
vendored
@ -1,7 +1,7 @@
|
||||
name: build-packages
|
||||
on: pull_request
|
||||
jobs:
|
||||
connectors-packages:
|
||||
build-check:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
DOTNET_INSTALL_DIR: "/home/runner"
|
||||
@ -24,3 +24,29 @@ jobs:
|
||||
fetch-depth: 0
|
||||
|
||||
- run: dotnet build --configuration Release
|
||||
run-tests:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
DOTNET_INSTALL_DIR: "/home/runner"
|
||||
permissions:
|
||||
contents: write
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Cleanup build folder
|
||||
run: |
|
||||
rm -rf ./* ./.??* || true
|
||||
|
||||
- name: Set up .NET
|
||||
uses: actions/setup-dotnet@v3
|
||||
with:
|
||||
dotnet-version: 8.0.x
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Run Tests
|
||||
run: |
|
||||
dotnet test ./meilisearch.NET.Tests/meilisearch.NET.Tests.csproj
|
30
.github/workflows/on-push.yml
vendored
30
.github/workflows/on-push.yml
vendored
@ -6,7 +6,7 @@ on:
|
||||
- 'master'
|
||||
|
||||
jobs:
|
||||
meilisearch-dotnet-packages:
|
||||
nuget-package:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
DOTNET_INSTALL_DIR: "/home/runner"
|
||||
@ -70,4 +70,30 @@ jobs:
|
||||
Automated release of MeiliSearch.NET version ${{ steps.gitversion.outputs.SemVer }}."
|
||||
files: nuget-packages/*.nupkg
|
||||
draft: false
|
||||
prerelease: false
|
||||
prerelease: true
|
||||
run-tests:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
DOTNET_INSTALL_DIR: "/home/runner"
|
||||
permissions:
|
||||
contents: write
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Cleanup build folder
|
||||
run: |
|
||||
rm -rf ./* ./.??* || true
|
||||
|
||||
- name: Set up .NET
|
||||
uses: actions/setup-dotnet@v3
|
||||
with:
|
||||
dotnet-version: 8.0.x
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Run Tests
|
||||
run: |
|
||||
dotnet test ./meilisearch.NET.Tests/meilisearch.NET.Tests.csproj
|
34
.github/workflows/on-release.yml
vendored
Normal file
34
.github/workflows/on-release.yml
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
name: update-release-status
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
update-release:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
steps:
|
||||
- name: Get latest release
|
||||
id: latest_release
|
||||
uses: actions/github-script@v6
|
||||
with:
|
||||
script: |
|
||||
const release = await github.rest.repos.getLatestRelease({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo
|
||||
});
|
||||
return release.data;
|
||||
|
||||
- name: Update release
|
||||
uses: actions/github-script@v6
|
||||
with:
|
||||
script: |
|
||||
const release = ${{ steps.latest_release.outputs.result }};
|
||||
await github.rest.repos.updateRelease({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
release_id: release.id,
|
||||
prerelease: false
|
||||
});
|
@ -102,6 +102,8 @@ public class SearchService
|
||||
};
|
||||
|
||||
_searchService.AddDocument("products", product);
|
||||
|
||||
//_searchService.AddDocument("products", product, true); if you set the third parameter, which is autocommit, to true, it will ignore batching.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
211
meilisearch.NET.Tests/MeilisearchServiceTests.cs
Normal file
211
meilisearch.NET.Tests/MeilisearchServiceTests.cs
Normal file
@ -0,0 +1,211 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Meilisearch;
|
||||
using meilisearch.NET.Interfaces;
|
||||
using meilisearch.NET.Models;
|
||||
using meilisearch.NET.Services.DocumentManagement;
|
||||
using meilisearch.NET.Services.IndexManagement;
|
||||
using meilisearch.NET.Services.ProcessManagement;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace meilisearch.NET.Tests
|
||||
{
|
||||
public class MeiliSearchServiceTests
|
||||
{
|
||||
private readonly Mock<IProcessManager> _mockProcessManager;
|
||||
private readonly Mock<IIndexManager> _mockIndexManager;
|
||||
private readonly Mock<IDocumentManager> _mockDocumentManager;
|
||||
private readonly Mock<MeilisearchClient> _mockClient;
|
||||
private readonly MeiliSearchService _service;
|
||||
|
||||
public MeiliSearchServiceTests()
|
||||
{
|
||||
_mockProcessManager = new Mock<IProcessManager>();
|
||||
_mockIndexManager = new Mock<IIndexManager>();
|
||||
_mockDocumentManager = new Mock<IDocumentManager>();
|
||||
|
||||
_service = new MeiliSearchService(
|
||||
_mockProcessManager.Object,
|
||||
_mockIndexManager.Object,
|
||||
_mockDocumentManager.Object
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Start_CallsProcessManagerStartProcess()
|
||||
{
|
||||
// Arrange
|
||||
_mockProcessManager.Setup(x => x.StartProcess()).Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _service.Start();
|
||||
|
||||
// Assert
|
||||
_mockProcessManager.Verify(x => x.StartProcess(), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetTotalStorageUsage_CallsIndexManagerWithCorrectParameters()
|
||||
{
|
||||
// Arrange
|
||||
const bool useCompressedSize = true;
|
||||
const long expectedUsage = 1000L;
|
||||
_mockIndexManager.Setup(x => x.GetTotalStorageUsage(useCompressedSize)).Returns(expectedUsage);
|
||||
|
||||
// Act
|
||||
var result = _service.GetTotalStorageUsage(useCompressedSize);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedUsage, result);
|
||||
_mockIndexManager.Verify(x => x.GetTotalStorageUsage(useCompressedSize), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetIndexStorageUsage_CallsIndexManagerWithCorrectParameters()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "testIndex";
|
||||
const bool useCompressedSize = true;
|
||||
const long expectedUsage = 500L;
|
||||
_mockIndexManager.Setup(x => x.GetIndexStorageUsage(indexName, useCompressedSize)).Returns(expectedUsage);
|
||||
|
||||
// Act
|
||||
var result = _service.GetIndexStorageUsage(indexName, useCompressedSize);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedUsage, result);
|
||||
_mockIndexManager.Verify(x => x.GetIndexStorageUsage(indexName, useCompressedSize), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Stop_CallsProcessManagerStopProcess()
|
||||
{
|
||||
// Act
|
||||
_service.Stop();
|
||||
|
||||
// Assert
|
||||
_mockProcessManager.Verify(x => x.StopProcess(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsRunning_ReturnsProcessManagerStatus()
|
||||
{
|
||||
// Arrange
|
||||
const bool expectedStatus = true;
|
||||
_mockProcessManager.Setup(x => x.IsProcessRunning()).Returns(expectedStatus);
|
||||
|
||||
// Act
|
||||
var result = _service.IsRunning();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedStatus, result);
|
||||
_mockProcessManager.Verify(x => x.IsProcessRunning(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetIndexEnabled_CallsIndexManagerWithCorrectParameters()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "testIndex";
|
||||
const bool enabled = true;
|
||||
_mockIndexManager.Setup(x => x.SetIndexEnabledAsync(indexName, enabled)).Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _service.SetIndexEnabled(indexName, enabled);
|
||||
|
||||
// Assert
|
||||
_mockIndexManager.Verify(x => x.SetIndexEnabledAsync(indexName, enabled), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetResourceUsage_ReturnsProcessManagerStats()
|
||||
{
|
||||
// Arrange
|
||||
var expectedStats = new ProcessResourceStats();
|
||||
_mockProcessManager.Setup(x => x.GetResourceUsage()).Returns(expectedStats);
|
||||
|
||||
// Act
|
||||
var result = _service.GetResourceUsage();
|
||||
|
||||
// Assert
|
||||
Assert.Same(expectedStats, result);
|
||||
_mockProcessManager.Verify(x => x.GetResourceUsage(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllIndexes_ReturnsIndexManagerResults()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexes = new List<string> { "index1", "index2" };
|
||||
_mockIndexManager.Setup(x => x.GetAllIndexes()).ReturnsAsync(expectedIndexes);
|
||||
|
||||
// Act
|
||||
var result = await _service.GetAllIndexes();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedIndexes, result);
|
||||
_mockIndexManager.Verify(x => x.GetAllIndexes(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateIndex_CallsIndexManagerWithCorrectParameters()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "testIndex";
|
||||
_mockIndexManager.Setup(x => x.CreateIndexAsync<TestDocument>(indexName)).Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _service.CreateIndex<TestDocument>(indexName);
|
||||
|
||||
// Assert
|
||||
_mockIndexManager.Verify(x => x.CreateIndexAsync<TestDocument>(indexName), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteIndex_CallsIndexManagerWithCorrectParameters()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "testIndex";
|
||||
_mockIndexManager.Setup(x => x.DeleteIndexAsync(indexName)).Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _service.DeleteIndex(indexName);
|
||||
|
||||
// Assert
|
||||
_mockIndexManager.Verify(x => x.DeleteIndexAsync(indexName), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddDocument_CallsDocumentManagerWithCorrectParameters()
|
||||
{
|
||||
// Arrange
|
||||
const string repositoryId = "testRepo";
|
||||
var document = new TestDocument();
|
||||
const bool autoCommit = true;
|
||||
|
||||
// Act
|
||||
_service.AddDocument(repositoryId, document, autoCommit);
|
||||
|
||||
// Assert
|
||||
_mockDocumentManager.Verify(x => x.AddDocument(repositoryId, document, autoCommit), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_CallsProcessManagerDispose()
|
||||
{
|
||||
// Act
|
||||
_service.Dispose();
|
||||
|
||||
// Assert
|
||||
_mockProcessManager.Verify(x => x.Dispose(), Times.Once);
|
||||
}
|
||||
|
||||
// Helper class for testing
|
||||
private class TestDocument : IDocument
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
28
meilisearch.NET.Tests/meilisearch.NET.Tests.csproj
Normal file
28
meilisearch.NET.Tests/meilisearch.NET.Tests.csproj
Normal file
@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<RootNamespace>TestProject1</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.extensibility.core" Version="2.9.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\meilisearch.NET\meilisearch.NET.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "meilisearch.NET", "meilisea
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "meilisearch.NET.example", "meilisearch.NET.example\meilisearch.NET.example.csproj", "{E753BBD6-6ADF-4DD4-8822-7279CD55DF58}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "meilisearch.NET.Tests", "meilisearch.NET.Tests\meilisearch.NET.Tests.csproj", "{FDCF4472-79F8-4AF2-AB02-00522C399E22}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -18,5 +20,9 @@ Global
|
||||
{E753BBD6-6ADF-4DD4-8822-7279CD55DF58}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E753BBD6-6ADF-4DD4-8822-7279CD55DF58}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E753BBD6-6ADF-4DD4-8822-7279CD55DF58}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{FDCF4472-79F8-4AF2-AB02-00522C399E22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FDCF4472-79F8-4AF2-AB02-00522C399E22}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FDCF4472-79F8-4AF2-AB02-00522C399E22}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FDCF4472-79F8-4AF2-AB02-00522C399E22}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
@ -1,10 +0,0 @@
|
||||
namespace meilisearch.NET.Enums;
|
||||
|
||||
public enum MeiliSearchStatus
|
||||
{
|
||||
Stopped,
|
||||
Starting,
|
||||
Running,
|
||||
Stopping,
|
||||
Crashed
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
namespace meilisearch.NET.Exceptions;
|
||||
|
||||
public class DocumentBatchException : DocumentManagementException
|
||||
{
|
||||
public int BatchSize { get; }
|
||||
|
||||
public DocumentBatchException(int batchSize, Exception innerException)
|
||||
: base($"Failed to process batch of {batchSize} documents", innerException)
|
||||
{
|
||||
BatchSize = batchSize;
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
namespace meilisearch.NET.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// Exception thrown when there are issues with document management
|
||||
/// </summary>
|
||||
public class DocumentManagementException : MeiliSearchException
|
||||
{
|
||||
public DocumentManagementException(string message) : base(message) { }
|
||||
public DocumentManagementException(string message, Exception innerException) : base(message, innerException) { }
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
namespace meilisearch.NET.Exceptions;
|
||||
|
||||
public class DocumentSyncException : DocumentManagementException
|
||||
{
|
||||
public DocumentSyncException(string message, Exception innerException)
|
||||
: base($"Failed to sync documents: {message}", innerException) { }
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
namespace meilisearch.NET.Exceptions;
|
||||
|
||||
public class DocumentValidationException : DocumentManagementException
|
||||
{
|
||||
public DocumentValidationException(string message)
|
||||
: base($"Document validation failed: {message}") { }
|
||||
}
|
12
meilisearch.NET/Exceptions/IndexAlreadyExistsException.cs
Normal file
12
meilisearch.NET/Exceptions/IndexAlreadyExistsException.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace meilisearch.NET.Exceptions;
|
||||
|
||||
public class IndexAlreadyExistsException : IndexManagementException
|
||||
{
|
||||
public string IndexName { get; }
|
||||
|
||||
public IndexAlreadyExistsException(string indexName)
|
||||
: base($"Index '{indexName}' already exists")
|
||||
{
|
||||
IndexName = indexName;
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
namespace meilisearch.NET.Exceptions;
|
||||
|
||||
public class IndexLimitExceededException : IndexManagementException
|
||||
public class IndexLimitReachedException : IndexManagementException
|
||||
{
|
||||
public IndexLimitExceededException()
|
||||
public IndexLimitReachedException()
|
||||
: base("Maximum number of indexes (1000) has been reached") { }
|
||||
}
|
@ -2,5 +2,5 @@
|
||||
|
||||
public class ProcessStartException : ProcessManagementException
|
||||
{
|
||||
public ProcessStartException(string message) : base($"Failed to start Meilisearch process: {message}") { }
|
||||
public ProcessStartException(Exception innerException, string message) : base($"Failed to start Meilisearch process: {message}", innerException) { }
|
||||
}
|
@ -2,5 +2,5 @@
|
||||
|
||||
public class ProcessStopException : ProcessManagementException
|
||||
{
|
||||
public ProcessStopException(string message) : base($"Failed to stop Meilisearch process: {message}") { }
|
||||
public ProcessStopException(Exception innerException, string message) : base($"Failed to stop Meilisearch process: {message}", innerException) { }
|
||||
}
|
@ -2,8 +2,20 @@
|
||||
|
||||
namespace meilisearch.NET.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for executing operations on MeiliSearch client.
|
||||
/// </summary>
|
||||
public static class MeilisearchClientExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes an action against MeiliSearch client that returns a value, automatically handling compressed index states.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value returned by the action</typeparam>
|
||||
/// <param name="service">The MeiliSearch service instance</param>
|
||||
/// <param name="indexName">Name of the index to operate on</param>
|
||||
/// <param name="action">The action to execute against the MeiliSearch client</param>
|
||||
/// <returns>The result of the executed action</returns>
|
||||
/// <exception cref="InvalidOperationException">Thrown when MeiliSearch client is not initialized</exception>
|
||||
public static async Task<T> SDK<T>(this MeiliSearchService service, string indexName, Func<MeilisearchClient, Task<T>> action)
|
||||
{
|
||||
var client = service.Client;
|
||||
@ -26,6 +38,13 @@ public static class MeilisearchClientExtensions
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes an action against MeiliSearch client that doesn't return a value, automatically handling compressed index states.
|
||||
/// </summary>
|
||||
/// <param name="service">The MeiliSearch service instance</param>
|
||||
/// <param name="indexName">Name of the index to operate on</param>
|
||||
/// <param name="action">The action to execute against the MeiliSearch client</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown when MeiliSearch client is not initialized</exception>
|
||||
public static async Task SDK(this MeiliSearchService service, string indexName, Func<MeilisearchClient, Task> action)
|
||||
{
|
||||
var client = service.Client;
|
||||
|
@ -7,8 +7,25 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace meilisearch.NET.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods for IServiceCollection to configure MeiliSearch services
|
||||
/// </summary>
|
||||
public static class ServiceCollectionExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds MeiliSearch services to the dependency injection container
|
||||
/// </summary>
|
||||
/// <param name="services">The IServiceCollection to add services to</param>
|
||||
/// <returns>The IServiceCollection for chaining</returns>
|
||||
/// <remarks>
|
||||
/// Registers the following services as singletons:
|
||||
/// - MeilisearchClient (configured for localhost:7700)
|
||||
/// - MeiliSearchService (with HttpClient)
|
||||
/// - MeiliSearchConfiguration
|
||||
/// - MeiliSearchProcessManager
|
||||
/// - IIndexManager (implemented by IndexManager)
|
||||
/// - IDocumentManager (implemented by DocumentManager)
|
||||
/// </remarks>
|
||||
public static IServiceCollection AddMeiliSearchService(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<MeilisearchClient>(sp =>
|
||||
@ -17,7 +34,7 @@ public static class ServiceCollectionExtension
|
||||
});
|
||||
services.AddHttpClient<MeiliSearchService>();
|
||||
services.AddSingleton<MeiliSearchConfiguration>();
|
||||
services.AddSingleton< MeiliSearchProcessManager>();
|
||||
services.AddSingleton<MeiliSearchProcessManager>();
|
||||
services.AddSingleton<IIndexManager, IndexManager>();
|
||||
services.AddSingleton<IDocumentManager, DocumentManager>();
|
||||
services.AddSingleton<MeiliSearchService>();
|
||||
|
@ -1,55 +1,96 @@
|
||||
using Meilisearch;
|
||||
using meilisearch.NET.Interfaces;
|
||||
using meilisearch.NET.Models;
|
||||
using meilisearch.NET.Services.DocumentManagement;
|
||||
using meilisearch.NET.Services.IndexManagement;
|
||||
using meilisearch.NET.Services.ProcessManagement;
|
||||
|
||||
namespace meilisearch.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Main service class for interacting with Meilisearch. Manages process, indexes and documents.
|
||||
/// </summary>
|
||||
public class MeiliSearchService : IDisposable
|
||||
{
|
||||
internal readonly MeiliSearchProcessManager ProcessManager;
|
||||
internal readonly IProcessManager ProcessManager;
|
||||
internal readonly IIndexManager IndexManager;
|
||||
internal readonly IDocumentManager DocumentManager;
|
||||
internal readonly MeilisearchClient Client;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of MeiliSearchService
|
||||
/// </summary>
|
||||
/// <param name="processManager">Manager for the Meilisearch process</param>
|
||||
/// <param name="indexManager">Manager for index operations</param>
|
||||
/// <param name="documentManager">Manager for document operations</param>
|
||||
public MeiliSearchService(
|
||||
MeiliSearchProcessManager processManager,
|
||||
IProcessManager processManager,
|
||||
IIndexManager indexManager,
|
||||
IDocumentManager documentManager,
|
||||
MeilisearchClient client)
|
||||
IDocumentManager documentManager)
|
||||
{
|
||||
Client = client;
|
||||
ProcessManager = processManager;
|
||||
IndexManager = indexManager;
|
||||
DocumentManager = documentManager;
|
||||
ProcessManager.StartProcess().Wait();
|
||||
}
|
||||
|
||||
/// <summary>Starts the Meilisearch process</summary>
|
||||
public async Task Start() => await ProcessManager.StartProcess();
|
||||
|
||||
/// <summary>Gets the total storage usage across all indexes</summary>
|
||||
/// <param name="useCompressedSize">If true, returns compressed size for compressed indexes</param>
|
||||
/// <returns>Total storage usage in bytes</returns>
|
||||
public long GetTotalStorageUsage(bool useCompressedSize = true)
|
||||
=> IndexManager.GetTotalStorageUsage(useCompressedSize);
|
||||
|
||||
/// <summary>Gets the storage usage for a specific index</summary>
|
||||
/// <param name="indexName">Name of the index</param>
|
||||
/// <param name="useCompressedSize">If true, returns compressed size for compressed indexes</param>
|
||||
/// <returns>Index storage usage in bytes</returns>
|
||||
public long GetIndexStorageUsage(string indexName, bool useCompressedSize = true)
|
||||
=> IndexManager.GetIndexStorageUsage(indexName, useCompressedSize);
|
||||
|
||||
/// <summary>Stops the Meilisearch process</summary>
|
||||
public void Stop() => ProcessManager.StopProcess();
|
||||
|
||||
/// <summary>Checks if the Meilisearch process is running</summary>
|
||||
/// <returns>True if process is running, false otherwise</returns>
|
||||
public bool IsRunning() => ProcessManager.IsProcessRunning();
|
||||
|
||||
/// <summary>Enables or disables an index</summary>
|
||||
/// <param name="indexName">Name of the index</param>
|
||||
/// <param name="enabled">True to enable, false to disable</param>
|
||||
public Task SetIndexEnabled(string indexName, bool enabled)
|
||||
=> IndexManager.SetIndexEnabledAsync(indexName, enabled);
|
||||
|
||||
/// <summary>Gets current resource usage statistics for the Meilisearch process</summary>
|
||||
/// <returns>Process resource statistics</returns>
|
||||
public ProcessResourceStats GetResourceUsage() => ProcessManager.GetResourceUsage();
|
||||
|
||||
/// <summary>Gets a list of all index names</summary>
|
||||
/// <returns>List of index names</returns>
|
||||
public Task<List<string>> GetAllIndexes() => IndexManager.GetAllIndexes();
|
||||
|
||||
/// <summary>Creates a new index</summary>
|
||||
/// <typeparam name="T">Document type that implements IDocument</typeparam>
|
||||
/// <param name="indexName">Name for the new index</param>
|
||||
public Task CreateIndex<T>(string indexName) where T : IDocument
|
||||
=> IndexManager.CreateIndexAsync<T>(indexName);
|
||||
|
||||
/// <summary>Deletes an existing index</summary>
|
||||
/// <param name="indexName">Name of the index to delete</param>
|
||||
public Task DeleteIndex(string indexName) => IndexManager.DeleteIndexAsync(indexName);
|
||||
|
||||
/// <summary>Adds a document to an index</summary>
|
||||
/// <param name="repositoryId">ID of the target repository/index</param>
|
||||
/// <param name="document">Document to add</param>
|
||||
/// <param name="autoCommit">If true, immediately syncs to server</param>
|
||||
public void AddDocument(string repositoryId, IDocument document, bool autoCommit = false)
|
||||
=> DocumentManager.AddDocument(repositoryId, document, autoCommit);
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the MeiliSearchService instance and its associated process manager
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
ProcessManager.Dispose();
|
||||
|
@ -1,16 +1,51 @@
|
||||
namespace meilisearch.NET.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a Meilisearch index with its metadata and compression state.
|
||||
/// </summary>
|
||||
public class Index
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the index.
|
||||
/// </summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the creation timestamp of the index in UTC.
|
||||
/// </summary>
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the unique folder identifier where the index data is stored.
|
||||
/// </summary>
|
||||
public string FolderId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the index is currently compressed.
|
||||
/// </summary>
|
||||
public bool IsCompressed { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the timestamp when the index was last compressed.
|
||||
/// </summary>
|
||||
public DateTime? LastCompressedAt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the size of the index in bytes before compression.
|
||||
/// </summary>
|
||||
public long? SizeBeforeCompression { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the Index class.
|
||||
/// </summary>
|
||||
public Index() { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the Index class with specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the index.</param>
|
||||
/// <param name="folderId">The folder identifier where index data is stored.</param>
|
||||
/// <param name="createdAt">The creation timestamp of the index.</param>
|
||||
public Index(string name, string folderId, DateTime createdAt)
|
||||
{
|
||||
Name = name;
|
||||
|
@ -1,11 +1,37 @@
|
||||
namespace meilisearch.NET.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents resource usage statistics for a Meilisearch process
|
||||
/// </summary>
|
||||
public class MeilisearchUsageStats
|
||||
{
|
||||
/// <summary>
|
||||
/// CPU usage as a percentage (0-100)
|
||||
/// </summary>
|
||||
public double CpuPercentage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Memory usage in bytes
|
||||
/// </summary>
|
||||
public long MemoryUsageBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Total bytes read from disk
|
||||
/// </summary>
|
||||
public long DiskReadBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Total bytes written to disk
|
||||
/// </summary>
|
||||
public long DiskWriteBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Number of threads used by the process
|
||||
/// </summary>
|
||||
public int ThreadCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Process identifier
|
||||
/// </summary>
|
||||
public int ProcessId { get; set; }
|
||||
}
|
37
meilisearch.NET/Models/ProcessResourceStats.cs
Normal file
37
meilisearch.NET/Models/ProcessResourceStats.cs
Normal file
@ -0,0 +1,37 @@
|
||||
namespace meilisearch.NET.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents resource usage statistics for a process at a point in time.
|
||||
/// </summary>
|
||||
public record ProcessResourceStats
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the CPU usage percentage of the process.
|
||||
/// </summary>
|
||||
public double CpuPercentage { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the memory usage in bytes of the process.
|
||||
/// </summary>
|
||||
public long MemoryUsageBytes { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of bytes read from disk by the process.
|
||||
/// </summary>
|
||||
public long DiskReadBytes { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of bytes written to disk by the process.
|
||||
/// </summary>
|
||||
public long DiskWriteBytes { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the process identifier.
|
||||
/// </summary>
|
||||
public int ProcessId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of threads currently running in the process.
|
||||
/// </summary>
|
||||
public int ThreadCount { get; init; }
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using Meilisearch;
|
||||
using meilisearch.NET.Exceptions;
|
||||
using meilisearch.NET.Interfaces;
|
||||
using meilisearch.NET.Services.ProcessManagement;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace meilisearch.NET.Services.DocumentManagement;
|
||||
@ -9,13 +11,15 @@ namespace meilisearch.NET.Services.DocumentManagement;
|
||||
public class DocumentManager:IDocumentManager
|
||||
{
|
||||
private readonly ILogger<DocumentManager> _logger;
|
||||
private readonly MeiliSearchProcessManager _meiliSearchProcessManager;
|
||||
private readonly MeilisearchClient _client;
|
||||
|
||||
private const int THRESHOLD = 100;
|
||||
private ObservableCollection<KeyValuePair<string,IDocument>> _documentCollection;
|
||||
|
||||
public DocumentManager(MeilisearchClient client, ILogger<DocumentManager> logger)
|
||||
public DocumentManager(MeilisearchClient client, ILogger<DocumentManager> logger, MeiliSearchProcessManager meiliSearchProcessManager)
|
||||
{
|
||||
_meiliSearchProcessManager = meiliSearchProcessManager;
|
||||
_logger = logger;
|
||||
_client = client;
|
||||
_documentCollection = new ObservableCollection<KeyValuePair<string,IDocument>>();
|
||||
@ -24,6 +28,9 @@ public class DocumentManager:IDocumentManager
|
||||
|
||||
public async Task AddDocumentAsync(string repositoryId, IDocument document, bool autoCommit = false)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
_logger.LogTrace($"Adding document '{document.Id}' to repository '{repositoryId}'...");
|
||||
_documentCollection.Add(new KeyValuePair<string, IDocument>(repositoryId, document));
|
||||
_logger.LogInformation($"Document {document.Id} added to collection.");
|
||||
@ -34,6 +41,9 @@ public class DocumentManager:IDocumentManager
|
||||
}
|
||||
public void AddDocument(string repositoryId, IDocument document, bool autoCommit = false)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
_logger.LogTrace($"Adding document '{document.Id}' to repository '{repositoryId}'...");
|
||||
_documentCollection.Add(new KeyValuePair<string, IDocument>(repositoryId, document));
|
||||
_logger.LogInformation($"Document {document.Id} added to collection.");
|
||||
@ -44,6 +54,9 @@ public class DocumentManager:IDocumentManager
|
||||
}
|
||||
public void SyncDocumentsToServer()
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var grouped = _documentCollection.GroupBy(pair => pair.Key)
|
||||
.ToDictionary(group => group.Key, group => group.Select(pair => pair.Value).ToList());
|
||||
foreach (var repository in grouped)
|
||||
@ -53,9 +66,12 @@ public class DocumentManager:IDocumentManager
|
||||
_documentCollection.Clear();
|
||||
var result = RetryAsync(() => repositoryIndex.AddDocumentsAsync(repository.Value, "id")).Result;
|
||||
}
|
||||
}
|
||||
}
|
||||
public async Task SyncDocumentsToServerAsync()
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var grouped = _documentCollection.GroupBy(pair => pair.Key)
|
||||
.ToDictionary(group => group.Key, group => group.Select(pair => pair.Value).ToList());
|
||||
foreach (var repository in grouped)
|
||||
|
@ -2,10 +2,34 @@ using meilisearch.NET.Interfaces;
|
||||
|
||||
namespace meilisearch.NET.Services.DocumentManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Manages document operations for Meilisearch indexes.
|
||||
/// </summary>
|
||||
public interface IDocumentManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a document to the specified repository.
|
||||
/// </summary>
|
||||
/// <param name="repositoryId">The ID of the repository to add the document to.</param>
|
||||
/// <param name="document">The document to add.</param>
|
||||
/// <param name="autoCommit">If true, automatically syncs the document to the server.</param>
|
||||
void AddDocument(string repositoryId, IDocument document, bool autoCommit = false);
|
||||
|
||||
/// <summary>
|
||||
/// Synchronizes all pending documents to the Meilisearch server.
|
||||
/// </summary>
|
||||
void SyncDocumentsToServer();
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a document to the specified repository.
|
||||
/// </summary>
|
||||
/// <param name="repositoryId">The ID of the repository to add the document to.</param>
|
||||
/// <param name="document">The document to add.</param>
|
||||
/// <param name="autoCommit">If true, automatically syncs the document to the server.</param>
|
||||
Task AddDocumentAsync(string repositoryId, IDocument document, bool autoCommit = false);
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously synchronizes all pending documents to the Meilisearch server.
|
||||
/// </summary>
|
||||
Task SyncDocumentsToServerAsync();
|
||||
}
|
@ -2,17 +2,84 @@ using meilisearch.NET.Interfaces;
|
||||
|
||||
namespace meilisearch.NET.Services.IndexManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Manages Meilisearch index operations including creation, deletion, and storage management.
|
||||
/// </summary>
|
||||
public interface IIndexManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves a list of all index names from the Meilisearch server.
|
||||
/// </summary>
|
||||
/// <returns>A list of index names.</returns>
|
||||
Task<List<string>> GetAllIndexes();
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously creates a new index with the specified name for the given document type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The document type that implements IDocument.</typeparam>
|
||||
/// <param name="indexName">The name of the index to create.</param>
|
||||
Task CreateIndexAsync<T>(string indexName) where T : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously creates a new index with the specified name for the given document type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The document type that implements IDocument.</typeparam>
|
||||
/// <param name="indexName">The name of the index to create.</param>
|
||||
void CreateIndex<T>(string indexName) where T : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes the specified index.
|
||||
/// </summary>
|
||||
/// <param name="indexName">The name of the index to delete.</param>
|
||||
Task DeleteIndexAsync(string indexName);
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously deletes the specified index.
|
||||
/// </summary>
|
||||
/// <param name="indexName">The name of the index to delete.</param>
|
||||
void DeleteIndex(string indexName);
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously enables or disables an index by compressing or decompressing it.
|
||||
/// </summary>
|
||||
/// <param name="indexName">The name of the index to modify.</param>
|
||||
/// <param name="enabled">True to enable (decompress), false to disable (compress).</param>
|
||||
Task SetIndexEnabledAsync(string indexName, bool enabled);
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously enables or disables an index by compressing or decompressing it.
|
||||
/// </summary>
|
||||
/// <param name="indexName">The name of the index to modify.</param>
|
||||
/// <param name="enabled">True to enable (decompress), false to disable (compress).</param>
|
||||
void SetIndexEnabled(string indexName, bool enabled);
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously gets the storage usage of a specific index.
|
||||
/// </summary>
|
||||
/// <param name="indexName">The name of the index.</param>
|
||||
/// <param name="useCompressedSize">If true, returns compressed size for compressed indexes.</param>
|
||||
/// <returns>The storage usage in bytes.</returns>
|
||||
Task<long> GetIndexStorageUsageAsync(string indexName, bool useCompressedSize = true);
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously gets the total storage usage of all indexes.
|
||||
/// </summary>
|
||||
/// <param name="useCompressedSize">If true, uses compressed sizes for compressed indexes.</param>
|
||||
/// <returns>The total storage usage in bytes.</returns>
|
||||
Task<long> GetTotalStorageUsageAsync(bool useCompressedSize = true);
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously gets the storage usage of a specific index.
|
||||
/// </summary>
|
||||
/// <param name="indexName">The name of the index.</param>
|
||||
/// <param name="useCompressedSize">If true, returns compressed size for compressed indexes.</param>
|
||||
/// <returns>The storage usage in bytes.</returns>
|
||||
long GetIndexStorageUsage(string indexName, bool useCompressedSize = true);
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously gets the total storage usage of all indexes.
|
||||
/// </summary>
|
||||
/// <param name="useCompressedSize">If true, uses compressed sizes for compressed indexes.</param>
|
||||
/// <returns>The total storage usage in bytes.</returns>
|
||||
long GetTotalStorageUsage(bool useCompressedSize = true);
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using System.IO.Compression;
|
||||
using System.Reflection;
|
||||
using Meilisearch;
|
||||
using meilisearch.NET.Exceptions;
|
||||
using meilisearch.NET.Interfaces;
|
||||
using meilisearch.NET.Services.ProcessManagement;
|
||||
using Meilisearch.QueryParameters;
|
||||
@ -16,35 +17,36 @@ public class IndexManager:IIndexManager
|
||||
private readonly string _indexBasePath = Path.Combine(AppContext.BaseDirectory, "db", "indexes" );
|
||||
private readonly ILogger<IndexManager> _logger;
|
||||
private readonly MeilisearchClient _client;
|
||||
private readonly MeiliSearchProcessManager _processManager;
|
||||
private readonly MeiliSearchProcessManager _meiliSearchProcessManager;
|
||||
|
||||
public IndexManager(ILogger<IndexManager> logger, MeilisearchClient client, MeiliSearchProcessManager processManager)
|
||||
public IndexManager(ILogger<IndexManager> logger, MeilisearchClient client, MeiliSearchProcessManager meiliSearchProcessManager)
|
||||
{
|
||||
_processManager = processManager;
|
||||
_meiliSearchProcessManager = meiliSearchProcessManager;
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public Task<List<string>> GetAllIndexes()
|
||||
public async Task<List<string>> GetAllIndexes()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_logger.LogTrace("Fetching all indexes from Meilisearch server created with the SDK...");
|
||||
var result = _client.GetAllIndexesAsync().Result.Results.Select(x => x.Uid).Where(x=>x!="index_bindings").ToList();
|
||||
_logger.LogInformation($"Fetched {result.Count} indexes from Meilisearch server.");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public void CreateIndex<T>(string indexName) where T : IDocument
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var indexes = GetAllIndexes().Result;
|
||||
if(indexes.Count>=1000)
|
||||
{
|
||||
_logger.LogWarning("Maximum number of indexes reached, cannot create new index.");
|
||||
return;
|
||||
}
|
||||
throw new IndexLimitReachedException();
|
||||
|
||||
if (indexes.Any(x => x == indexName))
|
||||
{
|
||||
_logger.LogWarning($"Index {indexName} already exists, skipping creation of index.");
|
||||
return;
|
||||
}
|
||||
throw new IndexAlreadyExistsException(indexName);
|
||||
|
||||
var foldersBefore = Directory.GetDirectories(_indexBasePath);
|
||||
_logger.LogTrace($"Creating index '{indexName}'...");
|
||||
@ -74,18 +76,16 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
public async Task CreateIndexAsync<T>(string indexName) where T : IDocument
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var indexes = await GetAllIndexes();
|
||||
|
||||
if(indexes.Count>=1000)
|
||||
{
|
||||
_logger.LogWarning("Maximum number of indexes reached, cannot create new index.");
|
||||
return;
|
||||
}
|
||||
throw new IndexLimitReachedException();
|
||||
|
||||
if (indexes.Any(x => x == indexName))
|
||||
{
|
||||
_logger.LogWarning($"Index {indexName} already exists, skipping creation of index.");
|
||||
return;
|
||||
}
|
||||
throw new IndexAlreadyExistsException(indexName);
|
||||
|
||||
var foldersBefore = Directory.GetDirectories(_indexBasePath);
|
||||
_logger.LogTrace($"Creating index '{indexName}'...");
|
||||
@ -114,11 +114,13 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
public void DeleteIndex(string indexName)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var indexes = _client.GetAllIndexesAsync().Result;
|
||||
if (indexes.Results.Any(x => x.Uid == indexName)==false)
|
||||
{
|
||||
_logger.LogWarning($"Index '{indexName}' does not exist, skipping deletion of index.");
|
||||
return;
|
||||
throw new IndexNotFoundException(indexName);
|
||||
}
|
||||
_logger.LogTrace($"Deleting index '{indexName}'...");
|
||||
_client.DeleteIndexAsync(indexName).Wait();
|
||||
@ -127,11 +129,13 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
public async Task DeleteIndexAsync(string indexName)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var indexes = _client.GetAllIndexesAsync().Result;
|
||||
if (indexes.Results.Any(x => x.Uid == indexName)==false)
|
||||
{
|
||||
_logger.LogWarning($"Index '{indexName}' does not exist, skipping deletion of index.");
|
||||
return;
|
||||
throw new IndexNotFoundException(indexName);
|
||||
}
|
||||
_logger.LogTrace($"Deleting index '{indexName}'...");
|
||||
await _client.DeleteIndexAsync(indexName);
|
||||
@ -140,6 +144,9 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
public void SetIndexEnabled(string indexName, bool enabled)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
_logger.LogTrace($"Updating index '{indexName}' status to {enabled}...");
|
||||
if(enabled)
|
||||
{
|
||||
@ -153,6 +160,9 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
public async Task SetIndexEnabledAsync(string indexName, bool enabled)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
_logger.LogTrace($"Updating index '{indexName}' status to {enabled}...");
|
||||
if(enabled)
|
||||
{
|
||||
@ -166,6 +176,9 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
public long GetIndexStorageUsage(string indexName, bool useCompressedSize = true)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var doc = _client.GetIndexAsync("index_bindings").Result.GetDocumentAsync<Models.Index>(indexName).Result;
|
||||
|
||||
if (doc.IsCompressed)
|
||||
@ -176,21 +189,22 @@ public class IndexManager:IIndexManager
|
||||
var indexPath = GetIndexFilePath(indexName).Result+".zip";
|
||||
if (!File.Exists(indexPath))
|
||||
{
|
||||
_logger.LogWarning($"Compressed index not found at: {indexPath}");
|
||||
return 0;
|
||||
throw new FileNotFoundException($"Compressed index not found at: {indexPath}");
|
||||
}
|
||||
return new FileInfo(indexPath).Length;
|
||||
}
|
||||
var path = Path.Combine(_indexBasePath, doc.FolderId);
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
_logger.LogWarning($"Index directory not found at: {path}");
|
||||
return 0;
|
||||
throw new DirectoryNotFoundException($"Index directory not found at: {path}");
|
||||
}
|
||||
return new DirectoryInfo(path).GetFiles().Sum(f => f.Length);
|
||||
}
|
||||
public long GetTotalStorageUsage(bool useCompressedSize = true)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var result = _client.GetIndexAsync("index_bindings").Result.GetDocumentsAsync<Models.Index>(new DocumentsQuery(){Limit = 1000}).Result;
|
||||
var total = 0L;
|
||||
foreach (var index in result.Results)
|
||||
@ -208,6 +222,9 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
public async Task<long> GetIndexStorageUsageAsync(string indexName, bool useCompressedSize = true)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var doc = _client.GetIndexAsync("index_bindings").Result.GetDocumentAsync<Models.Index>(indexName).Result;
|
||||
|
||||
if (doc.IsCompressed)
|
||||
@ -218,21 +235,22 @@ public class IndexManager:IIndexManager
|
||||
var indexPath = await GetIndexFilePath(indexName)+".zip";
|
||||
if (!File.Exists(indexPath))
|
||||
{
|
||||
_logger.LogWarning($"Compressed index not found at: {indexPath}");
|
||||
return 0;
|
||||
throw new FileNotFoundException($"Compressed index not found at: {indexPath}");
|
||||
}
|
||||
return new FileInfo(indexPath).Length;
|
||||
}
|
||||
var path = Path.Combine(_indexBasePath, doc.FolderId);
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
_logger.LogWarning($"Index directory not found at: {path}");
|
||||
return 0;
|
||||
throw new DirectoryNotFoundException($"Index directory not found at: {path}");
|
||||
}
|
||||
return new DirectoryInfo(path).GetFiles().Sum(f => f.Length);
|
||||
}
|
||||
public async Task<long> GetTotalStorageUsageAsync(bool useCompressedSize = true)
|
||||
{
|
||||
if (!_meiliSearchProcessManager.IsProcessRunning())
|
||||
throw new ProcessNotRunningException();
|
||||
|
||||
var result = _client.GetIndexAsync("index_bindings").Result.GetDocumentsAsync<Models.Index>(new DocumentsQuery(){Limit = 1000}).Result;
|
||||
var total = 0L;
|
||||
foreach (var index in result.Results)
|
||||
@ -278,8 +296,7 @@ public class IndexManager:IIndexManager
|
||||
var indexPath = await GetIndexFilePath(indexName);
|
||||
if (!Directory.Exists(indexPath))
|
||||
{
|
||||
_logger.LogWarning($"Index directory not found at: {indexPath}");
|
||||
return;
|
||||
throw new DirectoryNotFoundException($"Index directory not found at: {indexPath}");
|
||||
}
|
||||
|
||||
var compressedPath = indexPath + ".zip";
|
||||
@ -322,10 +339,9 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to compress index '{indexName}': {ex.Message}");
|
||||
throw;
|
||||
throw new IndexCompressionException(indexName, "compress", ex);
|
||||
}
|
||||
_processManager.StopProcess();
|
||||
_meiliSearchProcessManager.StopProcess();
|
||||
}
|
||||
|
||||
private async Task DecompressIndex(string indexName)
|
||||
@ -336,8 +352,7 @@ public class IndexManager:IIndexManager
|
||||
|
||||
if (!File.Exists(compressedPath))
|
||||
{
|
||||
_logger.LogWarning($"Compressed index not found at: {compressedPath}");
|
||||
return;
|
||||
throw new FileNotFoundException($"Compressed index not found at: {compressedPath}");
|
||||
}
|
||||
|
||||
_logger.LogTrace($"Decompressing index '{indexName}' to {extractPath}...");
|
||||
@ -379,8 +394,7 @@ public class IndexManager:IIndexManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to decompress index '{indexName}': {ex.Message}");
|
||||
throw;
|
||||
throw new IndexCompressionException(indexName, "decompress", ex);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
@ -1,4 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using meilisearch.NET.Exceptions;
|
||||
using meilisearch.NET.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace meilisearch.NET.Services.ProcessManagement;
|
||||
@ -35,14 +37,20 @@ public abstract class BaseProcessManager : IProcessManager
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError($"Failed to start {GetProcessName()}: {ex.Message}");
|
||||
throw;
|
||||
throw new ProcessStartException(ex,ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void StopProcess()
|
||||
{
|
||||
Process?.Kill();
|
||||
try
|
||||
{
|
||||
Process?.Kill();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError($"Error stopping {GetProcessName()} process: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsProcessRunning()
|
||||
|
@ -1,21 +1,33 @@
|
||||
using System.Diagnostics;
|
||||
using meilisearch.NET.Models;
|
||||
|
||||
namespace meilisearch.NET.Services.ProcessManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the contract for managing a process lifecycle and monitoring its resources.
|
||||
/// </summary>
|
||||
public interface IProcessManager : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Starts the managed process asynchronously.
|
||||
/// </summary>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
Task StartProcess();
|
||||
|
||||
/// <summary>
|
||||
/// Stops the managed process.
|
||||
/// </summary>
|
||||
void StopProcess();
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the managed process is currently running.
|
||||
/// </summary>
|
||||
/// <returns>True if the process is running, false otherwise.</returns>
|
||||
bool IsProcessRunning();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves current resource usage statistics for the managed process.
|
||||
/// </summary>
|
||||
/// <returns>A ProcessResourceStats object containing resource usage information.</returns>
|
||||
ProcessResourceStats GetResourceUsage();
|
||||
}
|
||||
|
||||
public record ProcessResourceStats
|
||||
{
|
||||
public double CpuPercentage { get; init; }
|
||||
public long MemoryUsageBytes { get; init; }
|
||||
public long DiskReadBytes { get; init; }
|
||||
public long DiskWriteBytes { get; init; }
|
||||
public int ProcessId { get; init; }
|
||||
public int ThreadCount { get; init; }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user