IDs management
Monday, 17 November 2014 · 4 messages
IF
Ioan Fericel17 Nov 2014 13:51 #
Hello all,
I would like to ask your advice on the best practice to use the IDs in BaseX, somewhat in the classic style of relationships between records in different nodes or documents.
I have not found a simple way to create autoincrement IDs, and indexes generated by BaseX I understand that it's possible to suffer changes in case of optimization actions or database recovery. So I can not count on them.
I try to do it using attributes as unique identifiers in repetitive elements, like:
<products>
. . .
<product id="123" name="abc" description="xyz"/>
<product id="124" name="bbbb" description="xxxxxxx"/>
</products>
<product-template>
<product id="" name="" description="" ... />
</product-template>When adding a new product, I need to calculate the max from
products/product/@id, incremented by 1 and replaced with this value (125) the id of the product template. After that, the updated product template should be inserted in the node products. I try to do all this in a single restxq function, in many ways, but fail.
Please give me, first, an general idea of how the problem should be approached in working with BaseX.
Many thanks,
Ioan
PS
Paul Swennenhuis17 Nov 2014 13:55 #
Hi Ioan,
I'd use a GUID-generation algorithm, with a near to 100% certainty of generating unique id's.
Or do you need a sequential order?
Paul
Quoted text · Ioan Fericel · 38 lines
> Hello all, > > I would like to ask your advice on the best practice to use the IDs in > BaseX, somewhat in the classic style of relationships between records > in different nodes or documents. > > I have not found a simple way to create autoincrement IDs, and indexes > generated by BaseX I understand that it's possible to suffer changes > in case of optimization actions or database recovery. So I can not > count on them. > > I try to do it using attributes as unique identifiers in repetitive > elements, like: > > <products> > . . . > <product id="123" name="abc" description="xyz"/> > <product id="124" name="bbbb" description="xxxxxxx"/> > > </products> > > <product-template> > <product id="" name="" description="" ... /> > </product-template> > > When adding a new product, I need to calculate the max from > products/product/@id, incremented by 1 and replaced with this value > (125) the id of the product template. After that, the updated product > template should be inserted in the node products. I try to do all this > in a single restxq function, in many ways, but fail. > > Please give me, first, an general idea of how the problem should be > approached in working with BaseX. > > Many thanks, > Ioan > >
IF
Ioan Fericel17 Nov 2014 17:19 #
Quoted text · Paul Swennenhuis · 7 lines
On 11/17/2014 02:55 PM, Paul Swennenhuis wrote: > Hi Ioan, > > I'd use a GUID-generation algorithm, with a near to 100% certainty of > generating unique id's. > Or do you need a sequential order? >
Hi Paul,
Thanks for the prompt response. For my case is neither a requirement to be sequentially. I would be glad to hear opinions also about the performance differences between the two options, which is faster.
In BaseX exist
random:uuid() function. Do you use it, or another way to generate GUID? Is there any way to generate such unique identifiers automatically when inserting a new record?
How do you solved, by using a such method, or by storing ID thus generated in an attribute within each record, manual added, as in the example?
Thank you,
IoanCG
Christian Grün17 Nov 2014 17:52 #
Hi Ioan,
Quoted text · Ioan Fericel · 2 lines
> In BaseX exist random:uuid() function. Do you use it, or another way to > generate GUID?
In our own use case, we sometimes use UUIDs and sometimes create our own IDs.
If you don't need an easily readable ID, and none that is incremental, I would recommend the use of
random:uuid() because it's the easiest solution.Quoted text · Ioan Fericel · 2 lines
> Is there any way to generate such unique identifiers > automatically when inserting a new record?
What do you call a "record" in your terminology?
By looking at your example, you could e.g. add a UUID as follows before storing it the XML fragment in the database:
<product id="{ random:uuid() } " ...Hope this helps,
Christian
Written for BaseX 7.9. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery