Can’t we all just get a Long?

Creating a database. I wish to be able to create linked data offline.

I initially went for a Guid, which is mostly safe to generate in a client. But Guids present issues with indexing, unless you use the SequentialId but that needs to be generated in the database.

I considered the CombGuid which improves indexing by byte shuffling, but is specific to the ordering used by Sql Server.

Given that my client is a web app, running in a browser, I wasn’t able to generate proper Guids anyway – only the mostly random type.

And then there are database engines that don’t natively support a Guid datatype anyway, such as Sqlite which stores it as a Blob.

By this time I was getting a bit hacked off with the whole Guid thing and wondered if there was another option. I came up with the Huid – a “Hopefully Unique IDentifier”.

The base datatype is Integer64 (or Long) and the value is a Unix timestamp, with milliseconds, and with an extra digit to support dates beyond 2038 (when the 32 bit timestamps will run out). A timestamp is currently 10 digits, +1 for added range, + 3 for milliseconds. That left me with 5 digits to play with which I am randomizing (although an app may specify these if creating Huids in a loop).

My resulting Integer64 is universally supported in database engines, and I trust that the numerical ID is also more efficient than a string or Guid. Plus it is easy to extract the date part.

I then ran into an issue that I haven’t encountered in so long that I had completely forgotten about – that JavaScript’s float datatype doesn’t support the range of a Long! So I have to translate between Int64 and string when passing to/from the client. But that’s ok with me as the client won’t benefit from a numerical type anyway (no need for indexing), and the translation is simple in C# with a custom Json.NET.JsonConverter.

So far it seems to be working, and I have since found that others have taken a similar path. But I wonder why this is not a more widely applied solution? Hopefully I won’t discover terrible pitfalls down the line.

In the meantime I urge my fellow devs to consider a similar approach. Can’t we all just get a Long?

Leave a Reply

Your email address will not be published. Required fields are marked *