Skip to content

Commit

Permalink
rebuild create collection if not existing
Browse files Browse the repository at this point in the history
  • Loading branch information
samihafidiDLW committed Aug 13, 2024
1 parent 4a0bd58 commit efa3bd2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>$(Company)</Authors>
<Copyright>Copyright © $(Company) $([System.DateTime]::Now.Year)</Copyright>
<Trademark>$(Company)™</Trademark>
<VersionPrefix>1.0.6-beta-1</VersionPrefix>
<VersionPrefix>1.0.7-beta-1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<PackageLicenseExpression>MIT</PackageLicenseExpression>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text;
using System.Text.Json;

using CMS.ContentEngine;
using CMS.Core;
using CMS.DataEngine;
using CMS.Helpers;
using CMS.Websites;

using Kentico.Xperience.Typesense.QueueWorker;
using Kentico.Xperience.Typesense.Search;
using Kentico.Xperience.Typesense.Xperience;

using Microsoft.Extensions.DependencyInjection;

using Typesense;
using Kentico.Xperience.Typesense.QueueWorker;
using Kentico.Xperience.Typesense.Xperience;

namespace Kentico.Xperience.Typesense.Collection;

Expand Down Expand Up @@ -77,6 +72,7 @@ public Task<int> DeleteRecords(IEnumerable<string> itemGuids, string collectionN

return DeleteRecordsInternal(itemGuids, collectionName);
}

/// <inheritdoc/>
public async Task<ICollection<TypesenseCollectionStatisticsViewModel>> GetStatistics(CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -119,16 +115,34 @@ public async Task<ICollection<TypesenseCollectionAliasViewModel>> GetAliases(Can
}

/// <inheritdoc />
public Task Rebuild(string collectionName, CancellationToken cancellationToken)
public async Task Rebuild(string collectionName, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(collectionName))
{
throw new ArgumentNullException(nameof(collectionName));
}

var typesenseCollection = TypesenseCollectionStore.Instance.GetRequiredCollection(collectionName);
TypesenseCollection? typesenseCollection = null;
try
{
typesenseCollection = TypesenseCollectionStore.Instance.GetRequiredCollection(collectionName);
}
//index does not exist
catch (InvalidOperationException)
{
bool isCreated = await TryCreateCollectionInternal(collectionName);
if (isCreated)
{
typesenseCollection = TypesenseCollectionStore.Instance.GetRequiredCollection(collectionName);
}
}

return RebuildInternal(typesenseCollection, cancellationToken);
if (typesenseCollection is null)
{
throw new Exception($"typesenseCollection is null. Could not get {collectionName}");
}

await RebuildInternal(typesenseCollection, cancellationToken);
}

/// <inheritdoc />
Expand All @@ -153,7 +167,6 @@ public async Task<bool> TryDeleteCollection(ITypesenseConfigurationModel? config
return primary != null || secondary != null;
}
return false;

}

/// <inheritdoc />
Expand Down Expand Up @@ -219,7 +232,6 @@ private async Task RebuildInternal(TypesenseCollection typesenseCollection, Canc

await EnsureNewCollection(newCollectionName, typesenseCollection);


indexedItems.ForEach(async node => await queue.EnqueueTypesenseQueueItem(new TypesenseQueueItem(node, TypesenseTaskType.PUBLISH_INDEX, newCollectionName)));

queue.EnqueueTypesenseQueueItem(new TypesenseQueueItem(new EndOfRebuildItemModel(activeCollectionName, newCollectionName, typesenseCollection.CollectionName), TypesenseTaskType.END_OF_REBUILD, typesenseCollection.CollectionName));

Check warning on line 237 in src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseClient.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 237 in src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseClient.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
Expand All @@ -241,7 +253,7 @@ private async Task<CollectionEventWebPageItemModel> MapToEventItem(IWebPageConte
languageName,
content.ContentTypeName,
content.WebPageItemName,
content.ContentItemIsSecured,
content.ContentItemIsSecured,
content.ContentItemContentTypeID,
content.ContentItemCommonDataContentLanguageID,
channelName,
Expand Down Expand Up @@ -297,7 +309,6 @@ private Task<IEnumerable<ContentLanguageInfo>> GetAllLanguages() =>
private Task<IEnumerable<(int WebsiteChannelID, string ChannelName)>> GetAllWebsiteChannels() =>
cache.LoadAsync(async cs =>
{

var results = await channelProvider.Get()
.Source(s => s.Join<WebsiteChannelInfo>(nameof(ChannelInfo.ChannelID), nameof(WebsiteChannelInfo.WebsiteChannelChannelID)))
.Columns(nameof(WebsiteChannelInfo.WebsiteChannelID), nameof(ChannelInfo.ChannelName))
Expand Down Expand Up @@ -325,24 +336,31 @@ public async Task<bool> TryCreateCollection(ITypesenseConfigurationModel configu
return false;
}

return await TryCreateCollectionInternal(configuration.CollectionName);
}

private async Task<bool> TryCreateCollectionInternal(string collectionName)
{
//Create the collection in Typesense
var typesenseCollection = TypesenseCollectionStore.Instance.GetCollection(configuration.CollectionName) ?? throw new InvalidOperationException($"Registered index with name '{configuration.CollectionName}' doesn't exist.");
var typesenseCollection = TypesenseCollectionStore.Instance.GetCollection(collectionName) ??
throw new InvalidOperationException(
$"Registered index with name '{collectionName}' doesn't exist.");

var typesenseStrategy = serviceProvider.GetRequiredStrategy(typesenseCollection);
var indexSettings = await typesenseStrategy.GetTypesenseCollectionSettings();

var createdCollection = await searchClient.CreateCollection(indexSettings.ToSchema($"{configuration.CollectionName}-primary"));
var createdCollection =
await searchClient.CreateCollection(indexSettings.ToSchema($"{collectionName}-primary"));
if (createdCollection == null)
{
return false;
}

await searchClient.UpsertCollectionAlias(configuration.CollectionName, new CollectionAlias($"{configuration.CollectionName}-primary"));
await searchClient.UpsertCollectionAlias(collectionName,
new CollectionAlias($"{collectionName}-primary"));
return true;
}



public async Task<bool> TryEditCollection(ITypesenseConfigurationModel configuration, Func<string, Task> rebuildAction)
{
if (configuration is null)
Expand Down Expand Up @@ -441,6 +459,7 @@ public async Task<int> SwapAliasWhenRebuildIsDone(IEnumerable<TypesenseQueueItem
}
return processed;
}

public async Task SwapAliasWhenRebuildIsDone(string alias, string newCollectionRebuilded)
{
if (string.IsNullOrEmpty(alias) || string.IsNullOrEmpty(newCollectionRebuilded))
Expand Down Expand Up @@ -468,5 +487,4 @@ public async Task SwapAliasWhenRebuildIsDone(string alias, string newCollectionR

return (currentCollection.Name, newCollectionName);
}

}

0 comments on commit efa3bd2

Please sign in to comment.