Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Commit

Permalink
Add DatabaseOwnerFetcher to query for current owner information
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Apr 6, 2019
1 parent 1b19717 commit d8b9fbd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public static IServiceCollection AddAzureSearch(this IServiceCollection services
services.AddTransient<ICatalogLeafFetcher, CatalogLeafFetcher>();
services.AddTransient<ICollector, AzureSearchCollector>();
services.AddTransient<ICommitCollectorLogic, AzureSearchCollectorLogic>();
services.AddTransient<IDatabaseOwnerFetcher, DatabaseOwnerFetcher>();
services.AddTransient<IDiagnosticsService, LoggerDiagnosticsService>();
services.AddTransient<IEntitiesContextFactory, EntitiesContextFactory>();
services.AddTransient<IHijackDocumentBuilder, HijackDocumentBuilder>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<Compile Include="Analysis\IdentifierCustomTokenFilter.cs" />
<Compile Include="Analysis\PackageIdCustomAnalyzer.cs" />
<Compile Include="Analysis\PackageIdCustomTokenizer.cs" />
<Compile Include="Owners2AzureSearch\DatabaseOwnerFetcher.cs" />
<Compile Include="Owners2AzureSearch\IDatabaseOwnerFetcher.cs" />
<Compile Include="Owners2AzureSearch\IOwnerSetComparer.cs" />
<Compile Include="Owners2AzureSearch\OwnerSetComparer.cs" />
<Compile Include="Owners2AzureSearch\PackageIdToOwnersBuilder.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NuGet.Jobs;
using NuGet.Jobs.Configuration;

namespace NuGet.Services.AzureSearch.Owners2AzureSearch
{
public class DatabaseOwnerFetcher : IDatabaseOwnerFetcher
{
private readonly ISqlConnectionFactory<GalleryDbConfiguration> _connectionFactory;
private readonly ILogger<DatabaseOwnerFetcher> _logger;

private const string Sql = @"
SELECT
pr.Id,
u.Username
FROM PackageRegistrations pr (NOLOCK)
INNER JOIN PackageRegistrationOwners pro (NOLOCK) ON pro.PackageRegistrationKey = pr.[Key]
INNER JOIN Users u (NOLOCK) ON pro.UserKey = u.[Key]
";

public DatabaseOwnerFetcher(
ISqlConnectionFactory<GalleryDbConfiguration> connectionFactory,
ILogger<DatabaseOwnerFetcher> logger)
{
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
_logger = logger;
}

public async Task<SortedDictionary<string, SortedSet<string>>> GetPackageIdToOwnersAsync()
{
using (var connection = await _connectionFactory.OpenAsync())
using (var command = connection.CreateCommand())
{
command.CommandText = Sql;

using (var reader = await command.ExecuteReaderAsync())
{
var builder = new PackageIdToOwnersBuilder(_logger);
while (await reader.ReadAsync())
{
var id = reader.GetString(0);
var username = reader.GetString(1);

builder.Add(id, username);
}

return builder.GetResult();
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Threading.Tasks;

namespace NuGet.Services.AzureSearch.Owners2AzureSearch
{
public interface IDatabaseOwnerFetcher
{
Task<SortedDictionary<string, SortedSet<string>>> GetPackageIdToOwnersAsync();
}
}

0 comments on commit d8b9fbd

Please sign in to comment.