Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequential GUIDs #260

Merged
merged 2 commits into from
Apr 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Security.Cryptography;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage.Internal;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.ValueGeneration.Internal
{
public class MySqlSequentialGuidValueGenerator : ValueGenerator<Guid>
{

private readonly MySqlScopedTypeMapper _mySqlTypeMapper;

public MySqlSequentialGuidValueGenerator(MySqlScopedTypeMapper mySqlTypeMapper)
{
_mySqlTypeMapper = mySqlTypeMapper;
}

private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();

/// <summary>
/// Gets a value to be assigned to a property.
/// Creates a GUID where the first 8 bytes are the current UTC date/time (in ticks)
/// and the last 8 bytes are cryptographically random. This allows for better performance
/// in clustered index scenarios.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <returns> The value to be assigned to a property. </returns>
public override Guid Next(EntityEntry entry)
{
var randomBytes = new byte[8];
Rng.GetBytes(randomBytes);
var ticks = (ulong) DateTime.UtcNow.Ticks;

if (_mySqlTypeMapper.ConnectionSettings.OldGuids)
{
var guidBytes = new byte[16];
var tickBytes = BitConverter.GetBytes(ticks);
if (BitConverter.IsLittleEndian)
Array.Reverse(tickBytes);

Buffer.BlockCopy(tickBytes, 0, guidBytes, 0, 8);
Buffer.BlockCopy(randomBytes, 0, guidBytes, 8, 8);

return new Guid(guidBytes);
}

var guid = new Guid((uint) (ticks >> 32), (ushort) (ticks << 32 >> 48), (ushort) (ticks << 48 >> 48),
randomBytes[0],
randomBytes[1],
randomBytes[2],
randomBytes[3],
randomBytes[4],
randomBytes[5],
randomBytes[6],
randomBytes[7]);

return guid;
}

/// <summary>
/// Gets a value indicating whether the values generated are temporary or permanent. This implementation
/// always returns false, meaning the generated values will be saved to the database.
/// </summary>
public override bool GeneratesTemporaryValues => false;

}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.ValueGeneration.Internal
{
public class MySqlValueGeneratorSelector : RelationalValueGeneratorSelector
{
private readonly MySqlScopedTypeMapper _mySqlTypeMapper;

public MySqlValueGeneratorSelector(
[NotNull] IValueGeneratorCache cache,
[NotNull] IRelationalAnnotationProvider relationalExtensions)
[NotNull] IRelationalAnnotationProvider relationalExtensions,
[NotNull] IRelationalTypeMapper typeMapper)
: base(cache, relationalExtensions)
{
_mySqlTypeMapper = typeMapper as MySqlScopedTypeMapper;
}

public override ValueGenerator Create(IProperty property, IEntityType entityType)
Expand All @@ -24,9 +30,9 @@ public override ValueGenerator Create(IProperty property, IEntityType entityType
? property.ValueGenerated == ValueGenerated.Never
|| property.MySql().DefaultValueSql != null
? (ValueGenerator)new TemporaryGuidValueGenerator()
: new SequentialGuidValueGenerator()
: new MySqlSequentialGuidValueGenerator(_mySqlTypeMapper)
: base.Create(property, entityType);
return ret;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void OnModelCreating(ModelBuilder modelBuilder)

public class GeneratedContact
{
public int Id { get; set; }
public Guid Id { get; set; }

public string Name { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using Pomelo.EntityFrameworkCore.MySql.PerfTests.Models;
using Xunit;

Expand All @@ -19,14 +21,22 @@ public async Task TestGeneratedContact()
const string zip = "99999";
var addressFormatted = string.Join(", ", address, city, state, zip);

Action<GeneratedContact> testContact = contact =>
using (var db = new AppDb())
{
Assert.Equal(email, contact.Email);
Assert.Equal(addressFormatted, contact.Address);
};
void TestContact(GeneratedContact contact)
{
var csb = new MySqlConnectionStringBuilder(db.Database.GetDbConnection().ConnectionString);
var guidHexStr = csb.OldGuids
? BitConverter.ToString(contact.Id.ToByteArray().Take(8).ToArray()).Replace("-", "")
: contact.Id.ToString().Replace("-", "").Substring(0, 16);
var guidTicks = Convert.ToInt64("0x" + guidHexStr, 16);
var guidDateTime = new DateTime(guidTicks);

Assert.InRange(guidDateTime - DateTime.UtcNow, TimeSpan.FromSeconds(-5), TimeSpan.FromSeconds(5));
Assert.Equal(email, contact.Email);
Assert.Equal(addressFormatted, contact.Address);
}

using (var db = new AppDb())
{
var gen = new GeneratedContact
{
Names = new JsonObject<List<string>>(new List<string> {"Bob", "Bobby"}),
Expand All @@ -43,11 +53,11 @@ public async Task TestGeneratedContact()
// test the entity after saving to the db
db.GeneratedContacts.Add(gen);
await db.SaveChangesAsync();
testContact(gen);
TestContact(gen);

// test the entity after fresh retreival from the database
var genDb = await db.GeneratedContacts.FirstOrDefaultAsync(m => m.Id == gen.Id);
testContact(genDb);
TestContact(genDb);
}
}

Expand Down