Skip to content

Commit

Permalink
.NET 8 upgrade and convert to primary ctors (#9)
Browse files Browse the repository at this point in the history
* .NET 8 upgrade and convert to primary ctors

* Use .NET 8 in build pipeline
  • Loading branch information
paulirwin authored Jan 11, 2024
1 parent 324b7e5 commit 64ac460
Show file tree
Hide file tree
Showing 30 changed files with 144 additions and 269 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
8 changes: 3 additions & 5 deletions AzureSearchEmulator/AzureSearchEmulator.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>6751dc28-24d4-4572-8f44-e652593dd678</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>0.0.1</Version>
Expand All @@ -16,8 +15,7 @@
<PackageReference Include="Lucene.Net.Highlighter" Version="4.8.0-beta00016" />
<PackageReference Include="Lucene.Net.Queries" Version="4.8.0-beta00016" />
<PackageReference Include="Lucene.Net.QueryParser" Version="4.8.0-beta00016" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.3" />
</ItemGroup>

<ItemGroup>
Expand All @@ -30,4 +28,4 @@
</None>
</ItemGroup>

</Project>
</Project>
27 changes: 9 additions & 18 deletions AzureSearchEmulator/Controllers/DocumentIndexingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@

namespace AzureSearchEmulator.Controllers;

public class DocumentIndexingController : ODataController
public class DocumentIndexingController(
JsonSerializerOptions jsonSerializerOptions,
ISearchIndexRepository searchIndexRepository,
ISearchIndexer searchIndexer)
: ODataController
{
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly ISearchIndexRepository _searchIndexRepository;
private readonly ISearchIndexer _searchIndexer;

public DocumentIndexingController(JsonSerializerOptions jsonSerializerOptions,
ISearchIndexRepository searchIndexRepository,
ISearchIndexer searchIndexer)
{
_jsonSerializerOptions = jsonSerializerOptions;
_searchIndexRepository = searchIndexRepository;
_searchIndexer = searchIndexer;
}

[HttpPost]
[Route("indexes('{indexKey}')/docs/search.index")]
[Route("indexes/{indexKey}/docs/search.index")]
public async Task<IActionResult> IndexDocuments(string indexKey)
{
var index = await _searchIndexRepository.Get(indexKey);
var index = await searchIndexRepository.Get(indexKey);

if (index == null)
{
Expand All @@ -36,7 +27,7 @@ public async Task<IActionResult> IndexDocuments(string indexKey)

using var sr = new StreamReader(Request.Body);
var json = await sr.ReadToEndAsync();
var batch = JsonSerializer.Deserialize<IndexDocumentsBatch>(json, _jsonSerializerOptions);
var batch = JsonSerializer.Deserialize<IndexDocumentsBatch>(json, jsonSerializerOptions);

if (batch == null)
{
Expand Down Expand Up @@ -69,8 +60,8 @@ public async Task<IActionResult> IndexDocuments(string indexKey)
itemIndex++;
}

var result = _searchIndexer.IndexDocuments(index, actions);
var result = searchIndexer.IndexDocuments(index, actions);

return StatusCode(result.Value.Any(i => !i.Status) ? 207 : 200, result);
}
}
}
31 changes: 12 additions & 19 deletions AzureSearchEmulator/Controllers/DocumentSearchingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,25 @@

namespace AzureSearchEmulator.Controllers;

public class DocumentSearchingController : ODataController
public class DocumentSearchingController(
IIndexSearcher indexSearcher,
ISearchIndexRepository searchIndexRepository)
: ODataController
{
private readonly IIndexSearcher _indexSearcher;
private readonly ISearchIndexRepository _searchIndexRepository;

public DocumentSearchingController(IIndexSearcher indexSearcher,
ISearchIndexRepository searchIndexRepository)
{
_indexSearcher = indexSearcher;
_searchIndexRepository = searchIndexRepository;
}

[HttpGet]
[Route("indexes/{indexKey}/docs/$count")]
[Route("indexes/({indexKey})/docs/$count")]
public async Task<IActionResult> GetDocumentCount(string indexKey)
{
var index = await _searchIndexRepository.Get(indexKey);
var index = await searchIndexRepository.Get(indexKey);

if (index == null)
{
return NotFound();
}

var count = await _indexSearcher.GetDocCount(index);
var count = await indexSearcher.GetDocCount(index);

return Ok(count);
}

Expand All @@ -42,14 +35,14 @@ public async Task<IActionResult> GetDocumentCount(string indexKey)
[EnableQuery]
public async Task<IActionResult> GetDocument(string indexKey, string key)
{
var index = await _searchIndexRepository.Get(indexKey);
var index = await searchIndexRepository.Get(indexKey);

if (index == null)
{
return NotFound();
}

var doc = await _indexSearcher.GetDoc(index, key);
var doc = await indexSearcher.GetDoc(index, key);

if (doc == null)
{
Expand Down Expand Up @@ -120,14 +113,14 @@ public async Task<IActionResult> SearchPost(string indexKey, [FromBody] SearchRe
return BadRequest(ModelState);
}

var index = await _searchIndexRepository.Get(indexKey);
var index = await searchIndexRepository.Get(indexKey);

if (index == null)
{
return NotFound();
}

var response = await _indexSearcher.Search(index, request);
var response = await indexSearcher.Search(index, request);

var oDataResponse = new JsonObject();

Expand All @@ -140,4 +133,4 @@ public async Task<IActionResult> SearchPost(string indexKey, [FromBody] SearchRe

return Ok(oDataResponse);
}
}
}
29 changes: 11 additions & 18 deletions AzureSearchEmulator/Controllers/IndexesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,25 @@

namespace AzureSearchEmulator.Controllers;

public class IndexesController : ODataController
public class IndexesController(
JsonSerializerOptions jsonSerializerOptions,
ISearchIndexRepository searchIndexRepository)
: ODataController
{
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly ISearchIndexRepository _searchIndexRepository;

public IndexesController(JsonSerializerOptions jsonSerializerOptions,
ISearchIndexRepository searchIndexRepository)
{
_jsonSerializerOptions = jsonSerializerOptions;
_searchIndexRepository = searchIndexRepository;
}

[HttpGet]
[EnableQuery]
[Route("indexes")]
public IAsyncEnumerable<SearchIndex> Get()
{
return _searchIndexRepository.GetAll();
return searchIndexRepository.GetAll();
}

[HttpGet]
[Route("indexes({key})")]
[Route("indexes/{key}")]
public async Task<IActionResult> Get(string key)
{
var index = await _searchIndexRepository.Get(key);
var index = await searchIndexRepository.Get(key);

if (index == null)
{
Expand All @@ -49,7 +42,7 @@ public async Task<IActionResult> Post() //([FromBody] SearchIndex? index)
// HACK.PI: For some reason, having this as a parameter with [FromBody] fails to deserialize properly.
using var sr = new StreamReader(Request.Body);
var indexJson = await sr.ReadToEndAsync();
var index = JsonSerializer.Deserialize<SearchIndex>(indexJson, _jsonSerializerOptions);
var index = JsonSerializer.Deserialize<SearchIndex>(indexJson, jsonSerializerOptions);

if (index == null || !ModelState.IsValid)
{
Expand All @@ -58,7 +51,7 @@ public async Task<IActionResult> Post() //([FromBody] SearchIndex? index)

try
{
await _searchIndexRepository.Create(index);
await searchIndexRepository.Create(index);
}
catch (SearchIndexExistsException)
{
Expand All @@ -73,15 +66,15 @@ public async Task<IActionResult> Post() //([FromBody] SearchIndex? index)
[Route("indexes/{key}")]
public async Task<IActionResult> Delete(string key)
{
var index = await _searchIndexRepository.Get(key);
var index = await searchIndexRepository.Get(key);

if (index == null)
{
return NotFound();
}

await _searchIndexRepository.Delete(index);
await searchIndexRepository.Delete(index);

return NoContent();
}
}
}
4 changes: 2 additions & 2 deletions AzureSearchEmulator/EmulatorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public class EmulatorOptions
{
public string IndexesDirectory { get; set; } = "";
}
public string IndexesDirectory { get; init; } = "";
}
9 changes: 2 additions & 7 deletions AzureSearchEmulator/Indexing/DeleteIndexDocumentAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace AzureSearchEmulator.Indexing;

public class DeleteIndexDocumentAction : IndexDocumentAction
public class DeleteIndexDocumentAction(JsonObject item) : IndexDocumentAction(item)
{
public DeleteIndexDocumentAction(JsonObject item)
: base(item)
{
}

public override IndexingResult PerformIndexingAsync(IndexingContext context)
{
var keyTerm = GetKeyTerm(context.Key);
Expand All @@ -17,4 +12,4 @@ public override IndexingResult PerformIndexingAsync(IndexingContext context)

return new IndexingResult(keyTerm.Text, true, 200);
}
}
}
8 changes: 1 addition & 7 deletions AzureSearchEmulator/Indexing/DocumentNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
namespace AzureSearchEmulator.Indexing;

public class DocumentNotFoundException : Exception
{
public DocumentNotFoundException()
: base("The specified document was not found")
{
}
}
public class DocumentNotFoundException() : Exception("The specified document was not found");
11 changes: 3 additions & 8 deletions AzureSearchEmulator/Indexing/IndexDocumentAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@

namespace AzureSearchEmulator.Indexing;

public abstract class IndexDocumentAction
public abstract class IndexDocumentAction(JsonObject item)
{
protected IndexDocumentAction(JsonObject item)
{
Item = item;
}

public JsonObject Item { get; }
public JsonObject Item { get; } = item;

protected Term GetKeyTerm(SearchField key)
{
Expand All @@ -26,4 +21,4 @@ protected Term GetKeyTerm(SearchField key)
}

public abstract IndexingResult PerformIndexingAsync(IndexingContext context);
}
}
24 changes: 8 additions & 16 deletions AzureSearchEmulator/Indexing/IndexingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@

namespace AzureSearchEmulator.Indexing;

public class IndexingContext
public class IndexingContext(SearchIndex index, SearchField key, IndexWriter writer, Lazy<IndexReader> reader)
{
public IndexingContext(SearchIndex index, SearchField key, IndexWriter writer, Lazy<IndexReader> reader)
{
Index = index;
Key = key;
Writer = writer;
Reader = reader;
}
public SearchIndex Index { get; } = index;

public SearchIndex Index { get; }

public SearchField Key { get; }

public IndexWriter Writer { get; }

public Lazy<IndexReader> Reader { get; }
}
public SearchField Key { get; } = key;

public IndexWriter Writer { get; } = writer;

public Lazy<IndexReader> Reader { get; } = reader;
}
24 changes: 7 additions & 17 deletions AzureSearchEmulator/Indexing/IndexingResult.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
namespace AzureSearchEmulator.Indexing;

public class IndexingResult
public class IndexingResult(string key, string? errorMessage, bool status, int statusCode)
{
public IndexingResult(string key, bool status, int statusCode)
: this(key, null, status, statusCode)
{
Key = key;
Status = status;
StatusCode = statusCode;
}

public IndexingResult(string key, string? errorMessage, bool status, int statusCode)
{
Key = key;
ErrorMessage = errorMessage;
Status = status;
StatusCode = statusCode;
}

public string Key { get; }
public string Key { get; } = key;

public string? ErrorMessage { get; }
public string? ErrorMessage { get; } = errorMessage;

public bool Status { get; }
public bool Status { get; } = status;

public int StatusCode { get; }
}
public int StatusCode { get; } = statusCode;
}
Loading

0 comments on commit 64ac460

Please sign in to comment.