COMB GUID, what is it and why should I use it

GUIDs (Globally Unique Identifier) can be a good choice for the ID or Key value in a database table, but it’s randomness is not good for the database when it comes to indexing the data and sorting the rows of data based on that GUID ID.

COMB-GUID-what-is-it-and-why-should-I-use-itBut a COMB GUID, basically a combination GUID with embedded data and time stamp is much better.  The COMB GUID (with embed date and time) becomes a sequential GUID, with each GUID being sequentially after the previous GUID.  This works great for indexing and sorting.  But you cant just replace or insert this time stamp anywhere in the GUID,  any part of the GUID with the date/time, it depends on the Database Server.  For this post, and where I work, I will refer to MS SQL Server as the database server.  It is important to replace the portion of the GUID that MSSQL sorts.    MSSQL sorts first by the last 6 Data4 bytes, left to right, then the first two bytes of Data4(again, left to right), then Data3, Data2, and Data1 right to left. This means for COMB purposes, we want to overwrite the the last 6 bytes of Data4 (byte index 10), left to right.

In the C# method below, we get a GUID.  Then we get the current date time.  From the date time, we generate a byte array for the total Days value and then a byte array for the total milliseconds value.  We then reverse the bytes to match MSSQL sorting, and then replace the last 6 bytes of the GUID with the total Days and total Milliseconds values.

 

   public class GuidCombGenerator  
   {  
     private static readonly long BaseDateTicks = new DateTime(1900, 1, 1).Ticks;  
   
     /// <summary>  
     ///   Generate a new <see cref="Guid" /> using the comb algorithm.  
     /// </summary>  
     /// <returns>New <see cref="Guid" />.</returns>  
     public Guid Generate()  
     {  
       byte[] guidArray = Guid.NewGuid().ToByteArray();  
   
       DateTime now = DateTime.UtcNow;  
   
       // Get the days and milliseconds which will be used to build the byte string   
       TimeSpan days = new TimeSpan(now.Ticks - BaseDateTicks);  
       TimeSpan msecs = now.TimeOfDay;  
   
       // Convert to a byte array   
       byte[] daysArray = BitConverter.GetBytes(days.Days);  
       byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds));  
   
       // Reverse the bytes to match SQL Servers ordering   
       Array.Reverse(daysArray);  
       Array.Reverse(msecsArray);  
   
       // Copy the bytes into the guid   
       Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);  
       Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);  
   
       return new Guid(guidArray);  
     }  
   }  
   

 

But what do I do with all the GUIDS already in the database that are not this new COMB GUID.  Assuming you have some date/time field in your table for date created or last date/time updated… you can use that date and generate a new COMB GUID and then update your GUID ID column.  This will update the columnName with a system GUID with the last 6 bytes replaced with the date time value pulled from the current row.

UPDATE [tableName] set [columnName] = (CAST(CAST(NEWID() AS binary(10)) + CAST(CAST([date/time columnName] AS datetime) AS binary(6)) AS uniqueidentifier))

I hope you were able to follow all of that and it becomes of some help to you.

References:

https://github.com/richardtallent/RT.Comb

https://github.com/pjvds/ncqrs/blob/master/Framework/src/Ncqrs/GuidCombGenerator.cs

Photography-Swimming

I recently had the opportunity to photograph the Southwest High School Swim Team at a few swim meets

I recently had the opportunity to photograph the Southwest High School Swim Team at a few swim meets.  Photographing swimmers is quite challenging… well, for me it is.  Often it is a dimly lit indoor pool with not a lot of space to move around to “get the shot”.  There are parents watching and not interested in me getting in their view of their swimmer.  There are other swimmers from the swim teams spread out and gathered into groups often cheering on their teammates.  Then there is there is the risk of pool water splashed up onto your camera or lens… or worse yet, getting bumped into the pool.  Now lets consider the fact once the swimmers are in the pool… most of their body (the subject) is concealed by the water. 

Photography-SwimmingLets revisit the fact that the area is dimly lit and it is a sporting event with fast swimmers, thus requiring a fast shutter speed to freeze the action.  This is a bad combination, in order to freeze the action with a fast shutter speed, you need lots of light or a lens with a small aperture like F/2.8.  Being that I am not a professional at this and have a family with 3 kids and one in college, my budget does not allow for the great zoom lens with low aperture of F/2.8.  So… I am forced to use a really high ISO number (as high as 5000 some times) and the camera’s ability to digitally overexpose the image to get enough light.  This works, it does the job, but you pay for it in a grainy or pixelated images that is not always bright, crisp and clear.

But some of the fun or challenge is capturing something more then a hand or arm out of the water.  Some swim strokes are easier or better to photograph then others, like Butterfly or Breast stroke.  Good timing is needed to capture the image without the arm blocking the view of the face, or the splash of water in the way.  Then there is the post shoot processing, adjust the light, saturation, crop the image, etc.  Enough of me rambling, here are some highlights from this year, click on an image to see it larger.

 

DSC_3469   DSC_3471

Photography-Swimming   DSC_3515

DSC_3522   DSC_3526

DSC_3531   DSC_3539

DSC_3546   DSC_3547

DSC_3582   DSC_3646

DSC_3649   DSC_3653

DSC_3667   DSC_3674

DSC_3677   DSC_3717

DSC_3731   DSC_3760

DSC_3794   DSC_3808

DSC_3819   DSC_3821

DSC_3848   image090

image093   image098

image103   image112

image120   image129

image131   image150

image162   image178

image206   image211

image214   image228

image234   image238

Git instead of TFS

My recent job change brought with it many new systems, processes, and technologies changes… including changing from Team Foundation Server (TFS) as a Version Control System (VCS), to using GIT for version control.  GIT is, as advertised, a distributed revision control system that was created for speed, data integrity and to support distributed or non-linear workflows.

As a VCS it is similar… but terminology, commands and user interface is quite different.  In Visual Studio I no longer connect to a TFS server but instead a GIT server.  The GIT server could be hosted internally to the company or it can be hosed externally by a service provider.  We use Visual Studio Online (VSO) and host our source code, Agile / SCRUM Features, PBI’s, Tasks, etc… can all be accessed via VS or the web browser UI.  With all this hosted with VSO, working from home becomes much easier… no need for VPN connection to access your sprint board, tasks, source code etc.  Code reviews are also done online through the browser remotely with other developers through what GIT calls a PULL request.  Reviewers can comment on concerns or issues with the source code changes and the developer can make and publish additional changes for the reviewer to sign off on.

 

Choosing the right version control for your project

 

Here is a comparison of GIT commands to TFS commands.

TFS Version Control GIT
Workspace Repository (aka “Repo”
Get Latest (First Time) Clone
Get Latest (After first time) Pull
Check In Commit + Push
Check Out (just start typing)
Branch Branch
Merge Merge
Code Review Pull Request
Label Tag