1

Possible Duplicate:
Sequential Guid Generator C#

I got documents which might be stored in raven. I'll like to generate a sequential GUID for them (I saw at RavenDB.net that raven uses them).

Are there a ravendb API that I can use to generate the id?

Notice that the question is RavenDB specific

8
  • 2
    GUID isn't sequential by definition. Commented Sep 3, 2012 at 18:26
  • 1
    @ChrisGessler: Not duplicate. I asked about RavenDb:s implementation Commented Sep 3, 2012 at 18:31
  • @jgauffin: do you want to generate on DB side, cause according to the Ayende post, it's enough to set idColumn=null ? Commented Sep 3, 2012 at 18:36
  • @Tigran: The documents might be stored. I want to generate the keys up front Commented Sep 3, 2012 at 18:38
  • 1
    @jgauffin: entities, described in that article, are not GUIDs. They are looking like GUIDS, that's all. Commented Sep 3, 2012 at 19:06

3 Answers 3

3

The code below shows how RavenDB achieves this on the server for eTags, you can see it in context here. The other piece of the code is here

public Guid CreateSequentialUuid() { var ticksAsBytes = BitConverter.GetBytes(currentEtagBase); Array.Reverse(ticksAsBytes); var increment = Interlocked.Increment(ref sequentialUuidCounter); var currentAsBytes = BitConverter.GetBytes(increment); Array.Reverse(currentAsBytes); var bytes = new byte[16]; Array.Copy(ticksAsBytes, 0, bytes, 0, ticksAsBytes.Length); Array.Copy(currentAsBytes, 0, bytes, 8, currentAsBytes.Length); return bytes.TransfromToGuidWithProperSorting(); } 

I don't think this is accessible via the API as it's an internal detail, but you could implement something similar yourself. Basically it relies on having a global counter (currentEtagBase and sequentialUuidCounter).

Sign up to request clarification or add additional context in comments.

Comments

0

Typically, if you're using GUID values, you wouldn't worry about them being sequential.

Guid.NewGuid will provide you with unique values you can use directly.

If you truly need sequential values, you can use the Guid constructor which accepts integral types, and build sequentially increasing values. This would be highly unusual, however.

7 Comments

Globally unique identifiers with a sequential part is nothing new. They exists for instance in SqlServer and speedup the lookup siginifically
@jgauffin Yes, but then they're typically auto-generated at insert. Making new, sequential values manually isn't that common.
That's why I asked. I want to be able to use ravendbs implementation
Then check the code at github. Isn't it open source?
@jgauffin My understanding with Raven is that you just leave it as Guid.Empty, and when you call Store, Raven will give you a proper Guid. You don't typically provide your own...
|
0

It's not usual request: create a sequential GUID, as by definition, GUID is unique (as much as it possible) and presence of some mathematical, predictable sequence will introduce additional problems in its uniqueness support.

But, by the way, as usual, there is always someone that think about this already.

Seems that guy on this link had the same problem:

Generate Sequential GUID

Note: the code provided is for C# 2005, but not much changed since that, in regard of this question at least.

EDIT

According to documentation, in regard of the sequential GUID generation:

Numeric or Guid Id properties are supported and will work seamlessly. In this case, RavenDB will automatically make the translation between the inner string ID to the numeric or Guid value shown in the entity and back.

Using this approach, IDs are available immediately after calling Store on the object - the RavenDB session will set a unique ID in memory without asking one from the serve

That means even if it's generated by "server", its also stored in the memory, so ask for it will cost almost nothing, if this is your warry...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.