Combine semantic ID and HiLo

71 views
Skip to first unread message

Ivan Montilla

unread,
Dec 12, 2019, 8:02:46 AM12/12/19
to RavenDB - 2nd generation document database
Hello,

Can be used HiLo combined with semantic ID?

My idea is use the ID to perform a relationship, in my example, an order is part of a contract, so my idea is generate ID for Order like this:

contracts/{contractID}/orders/{HiLoOrder}

Example:

contracts/1241-A/orders/12-A

The idea is mark on the order ID that the order 12-A is part of the contract 1241-A.

How can I do it? I'm using C# client.

Oren Eini (Ayende Rahien)

unread,
Dec 13, 2019, 6:36:26 AM12/13/19
to ravendb
Hi,
Yes, you can do that in one of two ways.
The simplest is to save the document using the key: "contracts/{contractID}/orders/"
That will cause the server to generate the rest of the value, appropriately

Alternatively, you can use the hi lo directly. 
AsyncHiLoIdGenerator is the class you want. Make sure that it has the same lifecyle as the document store (and you dispose it before you dispose the doc store)
You can use that to generate ids by calling GenerateDocumentIdAsync and overriding GetDocumentIdFromId.
Just be sure to also use the ServerTag as well


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/b35dc992-8497-4e39-ace2-c1a898020f35%40googlegroups.com.


--
Oren Eini
CEO   /   Hibernating Rhinos LTD
Skype:  ayenderahien
Support:  sup...@ravendb.net

Ivan Montilla

unread,
Dec 16, 2019, 12:40:46 PM12/16/19
to RavenDB - 2nd generation document database
Thanks Ayende. The first approach is using server-side ID generation, not HiLo, so it is not the I'm looking.

I will try the second approach and I will add a reply here. Thank you!

Can you consider add a character to the end of ID to indicate using HiLo? As the same form that / indicate to use server-side and | indicate to use consecutive number.


El viernes, 13 de diciembre de 2019, 12:36:26 (UTC+1), Oren Eini escribió:
Hi,
Yes, you can do that in one of two ways.
The simplest is to save the document using the key: "contracts/{contractID}/orders/"
That will cause the server to generate the rest of the value, appropriately

Alternatively, you can use the hi lo directly. 
AsyncHiLoIdGenerator is the class you want. Make sure that it has the same lifecyle as the document store (and you dispose it before you dispose the doc store)
You can use that to generate ids by calling GenerateDocumentIdAsync and overriding GetDocumentIdFromId.
Just be sure to also use the ServerTag as well


On Thu, Dec 12, 2019 at 3:02 PM Ivan Montilla <goo...@ivanmontilla.es> wrote:
Hello,

Can be used HiLo combined with semantic ID?

My idea is use the ID to perform a relationship, in my example, an order is part of a contract, so my idea is generate ID for Order like this:

contracts/{contractID}/orders/{HiLoOrder}

Example:

contracts/1241-A/orders/12-A

The idea is mark on the order ID that the order 12-A is part of the contract 1241-A.

How can I do it? I'm using C# client.

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Dec 18, 2019, 3:32:16 AM12/18/19
to ravendb
The problem we have is that there isn't really any more special chars we can really use.
That said, please note that you have DocumentConventions.AsyncDocumentIdGenerator available to inject your logic exactly for this kind of usages

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/5fda112d-b312-487c-9ec0-85078a7a2942%40googlegroups.com.

Ivan Montilla

unread,
Jan 13, 2020, 12:14:08 PM1/13/20
to RavenDB - 2nd generation document database
Hello,

My DocumentStore is singleton in ASP.NET Core application, As I know, the singleton instances will be disposed when the application shutdown, but I'm not sure how I can ensure that HiLoGenerator will be disposed before DocumentStore (maybe registering it first?)

There are three HiLo Id generator classes, what is the purpose of these differents classes?

Captura.PNG



El viernes, 13 de diciembre de 2019, 12:36:26 (UTC+1), Oren Eini escribió:
Hi,
Yes, you can do that in one of two ways.
The simplest is to save the document using the key: "contracts/{contractID}/orders/"
That will cause the server to generate the rest of the value, appropriately

Alternatively, you can use the hi lo directly. 
AsyncHiLoIdGenerator is the class you want. Make sure that it has the same lifecyle as the document store (and you dispose it before you dispose the doc store)
You can use that to generate ids by calling GenerateDocumentIdAsync and overriding GetDocumentIdFromId.
Just be sure to also use the ServerTag as well


On Thu, Dec 12, 2019 at 3:02 PM Ivan Montilla <goo...@ivanmontilla.es> wrote:
Hello,

Can be used HiLo combined with semantic ID?

My idea is use the ID to perform a relationship, in my example, an order is part of a contract, so my idea is generate ID for Order like this:

contracts/{contractID}/orders/{HiLoOrder}

Example:

contracts/1241-A/orders/12-A

The idea is mark on the order ID that the order 12-A is part of the contract 1241-A.

How can I do it? I'm using C# client.

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.

Ivan Montilla

unread,
Jan 13, 2020, 12:21:41 PM1/13/20
to RavenDB - 2nd generation document database
What about the idea of adding a overload to 'Store(object entity)' method with a new parameter that indicate what should be appened to ID?

Can be an enum like this:

enum IdGeneratorStrategy
{
 
None, Server-Side, HiLo, Identity
}

Can be used like this:

await session.StoreAsync(entity, $"orders/{customerId}/", IdGeneratorStrategy.HiLo, cancellationToken);

This allow us to end ours ids with "/" and "|" characters when indicating "None" and don't need to use more characters in future ID generations strategies.

Egor Shamanaev

unread,
Jan 13, 2020, 12:56:42 PM1/13/20
to rav...@googlegroups.com
Hi,

I think you can use the OnBeforeDelete event handler for that:

        private static async Task DoWork()
        {
            using (var store = new DocumentStore
            {
                Database = "test",
                Urls = new[] { "http://127.0.0.1:8080/" },
            })
            {
                store.OnBeforeDelete += OnBeforeDelete;
                store.Initialize();

                // client code
            }
        }

        private static void OnBeforeDelete(object sender, BeforeDeleteEventArgs e)
        {
            // do your stuff before dispose
        }


To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/6857fbe4-bb82-4d29-af04-fe59db2cd6d3%40googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Jan 14, 2020, 4:49:26 AM1/14/20
to ravendb
DocumentStore has a BeforeDispose event that you can register to, specifically for these kinds of purposes.

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/5d5a4c7b-ba51-4cce-82c3-28b67a28a612%40googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Jan 14, 2020, 4:49:56 AM1/14/20
to ravendb
OnBeforeDelete here is called whenever we delete a document, not when we dispose the store. I think you mixed the C++ client here

Egor Shamanaev

unread,
Jan 14, 2020, 5:21:39 AM1/14/20
to rav...@googlegroups.com
Hi, 

Sorry I mixed up those events, so the code should be :


        private static async Task DoWork()
        {
            using (var store = new DocumentStore
            {
                Database = "test",
                Urls = new[] { "http://127.0.0.1:8080/" },
            })
            {
                store.BeforeDispose += OnBeforeDispose;
                store.Initialize();

                // client code
            }
        }

        private static void OnBeforeDispose(object sender, EventArgs e)
Reply all
Reply to author
Forward
0 new messages