Skip to content

Commit

Permalink
fix: use concurrent dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranayub committed Jun 13, 2024
1 parent 2635352 commit c8ae4e6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 42 deletions.
1 change: 0 additions & 1 deletion IGDB.Tests/Games.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Threading.Tasks;
using IGDB.Models;
using RestEase;
using Xunit;

namespace IGDB.Tests
Expand Down
37 changes: 37 additions & 0 deletions IGDB.Tests/Platforms.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using IGDB.Models;
using Xunit;

namespace IGDB.Tests
{
public class Platforms
{
IGDBClient _api;

public Platforms()
{
_api = new IGDB.IGDBClient(
Environment.GetEnvironmentVariable("IGDB_CLIENT_ID"),
Environment.GetEnvironmentVariable("IGDB_CLIENT_SECRET")
);
}

[Fact]
public async Task ShouldReturnResponseWithLogsVersionsAndReleaseDates()
{
// Xbox 360
var plaforms = await _api.QueryAsync<Platform>(IGDBClient.Endpoints.Platforms, "fields *,platform_logo.*,versions.*,versions.platform_version_release_dates.*; where id = 12;");

Assert.NotNull(plaforms);

var platform = plaforms[0];

Assert.NotNull(platform.PlatformLogo.Value);
Assert.NotNull(platform.Versions.Values);
Assert.True(platform.Versions.Values.Length > 0, "No versions found");
Assert.NotNull(platform.Versions.Values[1].PlatformVersionReleaseDates.Values);
}
}
}
75 changes: 34 additions & 41 deletions IGDB/Serialization/IdentityConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -115,65 +116,57 @@ public static bool IsIdentitiesOrValues(Type givenType)
return givenType.Name.Contains(IdentitiesOrValuesName);
}

private static readonly IDictionary<Type, ObjectActivator> identitiesActivators
= new Dictionary<Type, ObjectActivator>();
private static readonly IDictionary<Type, ObjectActivator> valuesActivators
= new Dictionary<Type, ObjectActivator>();
private static readonly IDictionary<Type, ObjectActivator> identityActivators
= new Dictionary<Type, ObjectActivator>();
private static readonly IDictionary<Type, ObjectActivator> valueActivators
= new Dictionary<Type, ObjectActivator>();
private static readonly ConcurrentDictionary<Type, ObjectActivator> identitiesActivators
= new ConcurrentDictionary<Type, ObjectActivator>();
private static readonly ConcurrentDictionary<Type, ObjectActivator> valuesActivators
= new ConcurrentDictionary<Type, ObjectActivator>();
private static readonly ConcurrentDictionary<Type, ObjectActivator> identityActivators
= new ConcurrentDictionary<Type, ObjectActivator>();
private static readonly ConcurrentDictionary<Type, ObjectActivator> valueActivators
= new ConcurrentDictionary<Type, ObjectActivator>();

public static ObjectActivator GetIdentitiesActivator(Type objectType)
{
if (identitiesActivators.ContainsKey(objectType))
{
return identitiesActivators[objectType];
}

ConstructorInfo ctor = objectType.GetConstructors().Skip(1).First();
var activator = GetActivator(ctor);
identitiesActivators[objectType] = activator;
return activator;
return identitiesActivators.GetOrAdd(objectType, CreateIdentitiesActivator);
}

public static ObjectActivator GetValuesActivator(Type objectType)
{
if (valuesActivators.ContainsKey(objectType))
{
return valuesActivators[objectType];
}

ConstructorInfo ctor = objectType.GetConstructors().Skip(2).First();
var activator = GetActivator(ctor);
valuesActivators[objectType] = activator;
return activator;
return valuesActivators.GetOrAdd(objectType, CreateValuesActivator);
}

public static ObjectActivator GetIdentityActivator(Type objectType)
{
if (identityActivators.ContainsKey(objectType))
{
return identityActivators[objectType];
}
return identityActivators.GetOrAdd(objectType, CreateIdentityActivator);
}

public static ObjectActivator GetValueActivator(Type objectType)
{
return valueActivators.GetOrAdd(objectType, CreateValueActivator);
}

private static ObjectActivator CreateIdentitiesActivator(Type objectType)
{
ConstructorInfo ctor = objectType.GetConstructors().Skip(1).First();
var activator = GetActivator(ctor);
identityActivators[objectType] = activator;
return activator;
return GetActivator(ctor);
}

public static ObjectActivator GetValueActivator(Type objectType)
private static ObjectActivator CreateValuesActivator(Type objectType)
{
if (valueActivators.ContainsKey(objectType))
{
return valueActivators[objectType];
}
ConstructorInfo ctor = objectType.GetConstructors().Skip(2).First();
return GetActivator(ctor);
}

private static ObjectActivator CreateIdentityActivator(Type objectType)
{
ConstructorInfo ctor = objectType.GetConstructors().Skip(1).First();
return GetActivator(ctor);
}

private static ObjectActivator CreateValueActivator(Type objectType)
{
ConstructorInfo ctor = objectType.GetConstructors().Skip(2).First();
var activator = GetActivator(ctor);
valueActivators[objectType] = activator;
return activator;
return GetActivator(ctor);
}
}

Expand Down

0 comments on commit c8ae4e6

Please sign in to comment.