[Opa] Db3 -> Mongo || Db3.fresh_key -> ?
Rudy Sicard
rudy.sicard at mlstate.com
Mon Apr 30 17:57:04 UTC 2012
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.owasp.org/pipermail/opa/attachments/20120430/93c32350/attachment.html>
More information about the Opa
mailing list