Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
HybridUserLookupDomain.cs
/*
The MIT License (MIT)
Copyright (c) 2015 Agile Sports Technologies, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace Hudl.Users.Domain
{
public class HybridUserLookupDomain : IHybridUserLookupDomain
{
private static readonly ILog Log = LogManager.GetLogger(typeof (HybridUserLookupDomain));
private readonly ISqlUserLookupDomain _sql;
private readonly IMongoUserLookupDomain _mongo;
public HybridUserLookupDomain(ISqlUserLookupDomain sql, IMongoUserLookupDomain mongo)
{
_sql = sql;
_mongo = mongo;
}
// Queries MongoDB first, falling back to SQL if not found in MongoDB.
public async Task<UserDto> GetUserByEmail(string email, CancellationToken? token = null)
{
var user = await _mongo.GetUserByEmail(email, token);
if (user != null)
{
return user;
}
try
{
return await _sql.GetUserByEmail(email, token);
}
catch (MigratedUserOldDatabaseException e)
{
Log.Warn("Fallback email query found a result in SQL, but for a migrated user. It's likely that they changed their email address after we migrated them. Returning null because this user technically doesn't exist anymore.", e);
return null;
}
}
// Splits IDs by datasource, querying both and combining the results.
public async Task<List<UserDto>> GetUsersByIds(List<string> userIds, CancellationToken? token = null)
{
if (userIds == null || userIds.Count == 0)
{
return new List<UserDto>();
}
var userIdsSet = userIds.Where(id => !string.IsNullOrWhiteSpace(id)).ToSet();
if (userIdsSet.Count > MongoUserDomain.GetUsersQueryLimit)
{
throw new ArgumentException(string.Format("Query Limit={0} exceeded for GetUsers, Requested={1}", MongoUserDomain.GetUsersQueryLimit, userIdsSet.Count));
}
var idgroup = userIdsSet.GroupBy(UserMigrationState.Instance.IsUserMigrated).ToDictionary(grouping => grouping.Key);
var mongoIds = idgroup.ContainsKey(true) ? idgroup[true].ToList() : new List<string>();
var sqlIds = idgroup.ContainsKey(false) ? idgroup[false].ToList() : new List<string>();
var mongoUsers = mongoIds.Any() ? await _mongo.GetUsers(mongoIds, token) : new List<UserDto>();
var sqlUsers = sqlIds.Any() ? await _sql.GetUsers(sqlIds, token) : new List<UserDto>();
return mongoUsers.Union(sqlUsers).OrderBy(user => user.UserId).ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment