HiLo id generation implementation

32 views
Skip to first unread message

Adam Schroder

unread,
Jun 10, 2010, 12:32:47 AM6/10/10
to NoRM mongodb
Hi All,

I have just checked in changes to my master branch that will enable the basic scenarios of id generation using the HiLo algorithm.

Here is how it currently works.

    public class TestIntGeneration
    {
        public int? _id { get; set; }
        public string Name { get; set; }
    }

If you have a property that matches the criteria of an Id in the following order
  1. Configured in the mapping
  2. Uses the MongoIdentifier Attribute
  3. Prop name is _id or Id
then when you try and save the instance and the property is null it will generate the id for you, insert it into the database and place the value into the Id field.
eg.

        var testint = new TestIntGeneration { _id = null };
        mongo.GetCollection<TestIntGeneration>("Fake").Insert(testint);

        Assert.NotNull(testint._id);
        Assert.NotEqual(0, testint._id.Value);

You can also pre populate the id field using the GenerateId() method on the collection you are inserting.
eg.

    var collection = mongo.GetCollection<TestIntGeneration>("Fake");

    var identity = (int)collection.GenerateId();
    var testint = new TestIntGeneration { _id = identity, Name = "TestMe" };
    collection.Insert(testint);

    var result = collection.FindOne(new { _id = testint._id });

    Assert.NotNull(testint._id);
    Assert.Equal(result.Name, "TestMe");

Here are some predefined conventions now that we need some feedback on.

  1. Norm will go to the db once every 20 id generations per collection at the moment. Is this number too high? low? ok?
  2. The number should be configurable?
  3. Currently the GenerateId returns a long. Should this be an Int or a long is fine?
  4. Currently it will only auto generate an identity if the value is null. Should this also apply if the value is 0?

If you have an other questions or scenarios please don't hesitate to let us know.

Regards,
Adam



Andrew Theken

unread,
Jun 10, 2010, 7:09:30 AM6/10/10
to Adam Schroder, NoRM mongodb
Thanks Adam, this is awesome!

//Andrew Theken


--
You received this message because you are subscribed to the Google Groups "NoRM mongodb" group.
To post to this group, send email to norm-m...@googlegroups.com.
To unsubscribe from this group, send email to norm-mongodb...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/norm-mongodb?hl=en.

Diego F.

unread,
Jun 15, 2010, 10:34:47 AM6/15/10
to NoRM mongodb
What about comparing to default(T) instead of using nullable types.
I've implemented this logic on my repository class and works very
nicely for int, uint, long and ulong.

Zeroed id's are standard for non-saved entities across the vast
majority of ORM frameworks. It would be easier for people to migrate
and adapt.

Anyway we could have both.

On 10 jun, 08:09, Andrew Theken <athe...@gmail.com> wrote:
> Thanks Adam, this is awesome!
>
> //Andrew Theken
>
> On Thu, Jun 10, 2010 at 12:32 AM, Adam Schroder <adamschro...@gmail.com>wrote:
>
>
>
> > Hi All,
>
> > I have just checked in changes to my master branch that will enable the
> > basic scenarios of id generation using the HiLo algorithm.
> >http://github.com/schotime/NoRM/
> >  <http://github.com/schotime/NoRM/>
> > Here is how it currently works.
>
> >     public class TestIntGeneration
> >     {
> >         public int? _id { get; set; }
> >         public string Name { get; set; }
> >     }
>
> > If you have a property that matches the criteria of an Id in the following
> > order
>
> >    1. Configured in the mapping
> >    2. Uses the MongoIdentifier Attribute
> >    3. Prop name is _id or Id
>
> > then when you try and save the instance and the property is null it will
> > generate the id for you, insert it into the database and place the value
> > into the Id field.
> > eg.
>
> >         var testint = new TestIntGeneration { _id = null };
> >         mongo.GetCollection<TestIntGeneration>("Fake").Insert(testint);
>
> >         Assert.NotNull(testint._id);
> >         Assert.NotEqual(0, testint._id.Value);
>
> > You can also pre populate the id field using the GenerateId() method on the
> > collection you are inserting.
> > eg.
>
> >     var collection = mongo.GetCollection<TestIntGeneration>("Fake");
>
> >     var identity = (int)collection.GenerateId();
> >     var testint = new TestIntGeneration { _id = identity, Name = "TestMe"
> > };
> >     collection.Insert(testint);
>
> >     var result = collection.FindOne(new { _id = testint._id });
>
> >     Assert.NotNull(testint._id);
> >     Assert.Equal(result.Name, "TestMe");
>
> > Here are some predefined conventions now that we need some feedback on.
>
> >    1. Norm will go to the db once every 20 id generations per collection
> >    at the moment. Is this number too high? low? ok?
> >    2. The number should be configurable?
> >    3. Currently the GenerateId returns a long. Should this be an Int or a
> >    long is fine?
> >    4. Currently it will only auto generate an identity if the value is
> >    null. Should this also apply if the value is 0?
>
> > If you have an other questions or scenarios please don't hesitate to let us
> > know.
>
> > Regards,
> > Adam
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "NoRM mongodb" group.
> > To post to this group, send email to norm-m...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > norm-mongodb...@googlegroups.com<norm-mongodb%2Bunsubscribe@google­groups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/norm-mongodb?hl=en.- Ocultar texto das mensagens anteriores -
>
> - Mostrar texto das mensagens anteriores -
Reply all
Reply to author
Forward
0 new messages