You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Ole Ersoy <ol...@gmail.com> on 2007/04/19 07:18:47 UTC

[DAS] Unique IDs for Model Instances (Was Initial Context?)

I think I got it.

Here's the challenge:

We have a DN like this:

DN: cn=accounts, cn=users, cn=example, ou=com

That corresponds to a model namespace like this:

http://example.com/users/accounts

So under the RDN cn=accounts there are several model
instances stored.

What should the RDN for each of these instances be?

Here's what I'm thinking the process is:

For each model instance we generate a surrogate key.

We do that by getting the total number of children that
the context cn=accounts, cn=users, cn=example, ou=com
has, and then add 1.  So if there are 14 children, the
new child gets a surrogate key assigned to it which is
15.

Then we write the surrogate key, along with a descriptive human
readable/identifiable string (Like "Alex Karasulu") taken from one of 
the root object's attributes a file designated
by the DAS's configuration.  This is so that if the server
crashes (Hardware - never ADS), we'll know because we did
not write a completion flag into the file, and next time we run
the DAS it can recover.


Then we create the subcontext like this (dirContext is
cn=accounts, cn=users, cn=example, ou=com):

dirContext.createSubcontext("cn=15", attributes);

Originally I was was thinking generate a surrogate key using a hash,
but I think this is better.

So I'm going to go for it, unless anyone has a better idea.

Cheers,
- Ole




Re: [DAS] Unique IDs for Model Instances (Was Initial Context?)

Posted by Ole Ersoy <ol...@gmail.com>.
Stefan, Emmanuel,

Excellent thoughts.

OK - I'll scrap that idea.
I saw something about generating a unique
id based on current time on onjava.com
I'll see if I can find that article.

Thanks,
- Ole



Emmanuel Lecharny wrote:
> Ole Ersoy a écrit :
> 
>> What should the RDN for each of these instances be?
> 
> Try to associate an *unique* key to each object, and used it
> 
>>
>>
>> We do that by getting the total number of children that
>> the context cn=accounts, cn=users, cn=example, ou=com
>> has, and then add 1.  So if there are 14 children, the
>> new child gets a surrogate key assigned to it which is
>> 15.
> 
> There is no way to get the number of entries in the server, but getting 
> all the entries and count them. Not really fast.
> Forget about your idea, it does not work.
> 
>>
>> Then we write the surrogate key, along with a descriptive human
>> readable/identifiable string (Like "Alex Karasulu") taken from one of 
>> the root object's attributes
> 
> Just use this object's attribute as the RDN.
> 
>> <snip>
> 
>> Originally I was was thinking generate a surrogate key using a hash,
> 
> Don't. Hash are not guaranteed to be unique. Java HashCode is just a 
> speedup for comparisons.
> o1.hashcode != o2.hashcode() => o1 != o2
> but
> o1.hashcode == o2.hashcode() does not implies that o1 == o2
> 
> Emmanuel
> 

Re: [DAS] Unique IDs for Model Instances (Was Initial Context?)

Posted by Emmanuel Lecharny <el...@gmail.com>.
Ole Ersoy a écrit :

> What should the RDN for each of these instances be?

Try to associate an *unique* key to each object, and used it

>
>
> We do that by getting the total number of children that
> the context cn=accounts, cn=users, cn=example, ou=com
> has, and then add 1.  So if there are 14 children, the
> new child gets a surrogate key assigned to it which is
> 15.

There is no way to get the number of entries in the server, but getting 
all the entries and count them. Not really fast.
Forget about your idea, it does not work.

>
> Then we write the surrogate key, along with a descriptive human
> readable/identifiable string (Like "Alex Karasulu") taken from one of 
> the root object's attributes

Just use this object's attribute as the RDN.

> <snip>

> Originally I was was thinking generate a surrogate key using a hash,

Don't. Hash are not guaranteed to be unique. Java HashCode is just a 
speedup for comparisons.
o1.hashcode != o2.hashcode() => o1 != o2
but
o1.hashcode == o2.hashcode() does not implies that o1 == o2

Emmanuel

Re: [DAS] Unique IDs for Model Instances (Was Initial Context?)

Posted by Stefan Seelmann <se...@apache.org>.
Hi Ole,

Ole Ersoy wrote:

> Here's what I'm thinking the process is:
>
> For each model instance we generate a surrogate key.
>
> We do that by getting the total number of children that
> the context cn=accounts, cn=users, cn=example, ou=com
> has, and then add 1.  So if there are 14 children, the
> new child gets a surrogate key assigned to it which is
> 15.
>
> Then we write the surrogate key, along with a descriptive human
> readable/identifiable string (Like "Alex Karasulu") taken from one of 
> the root object's attributes a file designated
> by the DAS's configuration.  This is so that if the server
> crashes (Hardware - never ADS), we'll know because we did
> not write a completion flag into the file, and next time we run
> the DAS it can recover.
>
> Then we create the subcontext like this (dirContext is
> cn=accounts, cn=users, cn=example, ou=com):
>
> dirContext.createSubcontext("cn=15", attributes);
>
The are some (LDAP-related) problems with this approach:
- This operation isn't atomic. There is some time between you calculate 
the key and the real write operation. If there are many concurrent 
threads writing model instances you will get an 
NameAlreadyBoundException. Then you have to try again. I think with 
random keys there are less collisions.
- There is no count(*) in LDAP. So to determine the number of children 
you have to retrieve all children and count them within the DAS. Imagine 
you have thousands or millions of children all of the DNs must be 
transfered over the wire. This can take some time and will slow down the 
system and doesn't scale well.

Regards,
Stefan