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

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 2
    GUID isn't sequential by definition. – Dennis Sep 03 '12 at 18:26
  • 1
    @ChrisGessler: Not duplicate. I asked about ***RavenDb:s*** implementation – jgauffin Sep 03 '12 at 18:31
  • @jgauffin: do you want to generate on DB side, cause according to the [Ayende](https://groups.google.com/forum/?fromgroups#!topic/ravendb/5awuv842u38) post, it's enough to set `idColumn=null` ? – Tigran Sep 03 '12 at 18:36
  • @Tigran: The documents might be stored. I want to generate the keys up front – jgauffin Sep 03 '12 at 18:38
  • @Dennis: http://www.codeproject.com/Articles/388157/GUIDs-as-fast-primary-keys-under-multiple-database – jgauffin Sep 03 '12 at 18:38
  • @jgauffin: don't believe, at this point, that you have more choices then: 1. Simply look in the code 2. Write to Ayende and ask about this. – Tigran Sep 03 '12 at 18:41
  • @Tigran: He or Matt Warren usually answers questions here too.. – jgauffin Sep 03 '12 at 18:45
  • 1
    @jgauffin: entities, described in that article, are not **GUIDs**. They are looking **like** GUIDS, that's all. – Dennis Sep 03 '12 at 19:06

3 Answers3

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).

Matt Warren
  • 10,279
  • 7
  • 48
  • 63
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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
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...

Tigran
  • 61,654
  • 8
  • 86
  • 123