[Opa] Db3 -> Mongo || Db3.fresh_key -> ?

Cédric Soulas cedric.soulas at mlstate.com
Mon Apr 30 19:47:09 UTC 2012


You can add a timestamp prefix to the random int to dratically reduce the collision probability. Of course don't use a type int for this key, or not a max_int random ;)

Note that mongo doesn't use an auto increment for _id
Read http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-BSONObjectIDSpecification  

Le 30 avr. 2012 à 19:57, Rudy Sicard <rudy.sicard at mlstate.com> a écrit :

> Hello,
> 
> If collision or compactness of the key range are a serious concern, you have to maintain a counter somewhere (with both intmap and set)
> (see http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb).
> You can use findAndModify to increment and return a counter atomically from the db.
> Or assuming only one server, you can use a cell (to ensure atomicity) that is saved in db (for server reload).
> 
> I am going to push code for both memory atomic operation, and findAndModify interface.
> So if you go for seconds solutions, let me know, if you are lost, I can push it sooner.
> 
> If for you are using a integer key for bad reason and want to stick to it for good reason, 
> and you don't care about compactness of the key range,
> and you don't intend to insert more than billions of entries (or thousands for 32bits machines)
> and you are not writing code for a nuclear power plant (i.e. collisions are not lethal).
> Just go with a proven solution, random key (=> e.g. freshkey() = Random.int(max_int)).
> You can seriously reduce collision to a probability of almost zero just by checking the key is not already used,
> so it is not as bad as you may think.
> freshkey(check) =
>     k = Random.int(max_int)
>     if check(k) then k
>     else freshkey(check)
> 
> Rudy
> 
> On 30/04/2012 18:38, Matthieu Guffroy wrote:
>> 
>> Hello,
>> 
>> How we get a fresh_key with mongo backend ?
>> Is there a different way of using it ?
>> 
>> I think the best would be to insert in a set instead of inserting in a intmap with a freshkey. And let mongo autoincrement the primary key...
>> Is it possible ? If yes how ?
>> 
>> 
>> Thanks,
>> 
>> --
>> Matthieu GUFFROY
>> 
>> 
>> _______________________________________________
>> Opa mailing list
>> Opa at lists.owasp.org
>> https://lists.owasp.org/mailman/listinfo/opa
> 
> _______________________________________________
> Opa mailing list
> Opa at lists.owasp.org
> https://lists.owasp.org/mailman/listinfo/opa

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.owasp.org/pipermail/opa/attachments/20120430/10c3f37c/attachment-0001.html>


More information about the Opa mailing list