Erlang/OTP Forums

Author Message

<  Erlang questions mailing list  ~  mnesia primary key creation

Guest
Posted: Tue Jul 04, 2006 7:37 pm Reply with quote
Guest
I liked the cheap solution:

oid() -> {node(), erlang:now()}.

but how can I turn erlang:now() into a String ?? (which probablly
won't look very URL-friendly)

I thouhgt of turing it into a list, then into a binary, then base64
encoding it, but
list_to_binary(tuple_to_list(erlang:now()))
does not work.

regards
Roberto

On 6/30/06, Ulf Wiger (AL/EAB) <ulf.wiger@ericsson.com> wrote:
>
> I sketched the attached code for maintaining a node-specific sequence
> number in a cluster of nodes. The code is untested - probably doesn't
> even compile, and upon reflection, I'd do away with all the dirty ops.
> Basically, you should never use dirty ops on replicated tables.
>
> The thing I was after was a system-unique object identifier which was
> compact enough to be human- and URL-friendly.
>
> For cheap(in terms of CPU cost) automatic primary keys, I tend to use
>
> oid() -> {node(), erlang:now()}.
>
> Given a reasonably well-behaved system clock(*), this is certain to give
> unique values - increasing values, even - each time.
>
> (*) AFAIK, even though it's not documented, erlang:now() is derived from
> sys_gethrtime(), except on Win32, where it's derived from GetTickCount()
> - I gave up before understanding from the source how erlang:now()
> relates to "zero hour" on Windows. A cursory inspection of the code made
> me doubt that my statement holds true for Windows. Correct me, please,
> anyone.
>
> BR,
> Ulf W
>
> > -----Original Message-----
> > From: owner-erlang-questions@erlang.org
> > [mailto:owner-erlang-questions@erlang.org] On Behalf Of Roberto Saccon
> > Sent: den 30 juni 2006 03:59
> > To: erlang-questions@erlang.org
> > Subject: mnesia primary key creation
> >
> > I am wondering whether experienced erlang developers have
> > something better in their best-practises-treasure-box than
> > what I have come up with to create mnesia primary keys:
> >
> > 1. use "ordered_set" tables
> >
> > 2. use "mnesia:dirty_last(myTable)" to get the last inserted
> > key and increment it for a new key before inserting a new record.
> >
> >
> > regards
> > --
> > Roberto Saccon
> >
>
>
>


--
Roberto Saccon
Post generated from www.trapexit.org
uffe
Posted: Tue Jul 04, 2006 9:23 pm Reply with quote
User Joined: 02 Mar 2005 Posts: 365 Location: Sweden
Den 2006-07-04 21:31:26 skrev Roberto Saccon <rsaccon@gmail.com>:

> I liked the cheap solution:
>
> oid() -> {node(), erlang:now()}.
>
> but how can I turn erlang:now() into a String ?? (which probablly
> won't look very URL-friendly)
>
> I thouhgt of turing it into a list, then into a binary, then base64
> encoding it, but
> list_to_binary(tuple_to_list(erlang:now()))
> does not work.

Well, for example:

1> {Node,{MS,S,US}} = {node(), erlang:now()}.
{nonode@nohost,{1152,47770,297000}}
2>
list_to_binary([atom_to_list(Node),"-",integer_to_list(MS),"-",integer_to_list(S),"-",integer_to_list(US)]).
<<110,111,110,111,100,101,64,110,111,104,111,115,116,45,49,49,53,50,45,52,55,55,55,48,45,50,57,55,48,...>>
3> binary_to_list(v(2)).
"nonode@nohost-1152-47770-297000"

BR,
Ulf W

--
Ulf Wiger
Post generated from www.trapexit.org
View user's profile Send private message Visit poster's website
Guest
Posted: Tue Jul 04, 2006 9:42 pm Reply with quote
Guest
Thanks, after seeing the example it looks soooo simple and obvious !

On 7/4/06, Ulf Wiger <ulf@wiger.net> wrote:
> Den 2006-07-04 21:31:26 skrev Roberto Saccon <rsaccon@gmail.com>:
>
> > I liked the cheap solution:
> >
> > oid() -> {node(), erlang:now()}.
> >
> > but how can I turn erlang:now() into a String ?? (which probablly
> > won't look very URL-friendly)
> >
> > I thouhgt of turing it into a list, then into a binary, then base64
> > encoding it, but
> > list_to_binary(tuple_to_list(erlang:now()))
> > does not work.
>
> Well, for example:
>
> 1> {Node,{MS,S,US}} = {node(), erlang:now()}.
> {nonode@nohost,{1152,47770,297000}}
> 2>
> list_to_binary([atom_to_list(Node),"-",integer_to_list(MS),"-",integer_to_list(S),"-",integer_to_list(US)]).
> <<110,111,110,111,100,101,64,110,111,104,111,115,116,45,49,49,53,50,45,52,55,55,55,48,45,50,57,55,48,...>>
> 3> binary_to_list(v(2)).
> "nonode@nohost-1152-47770-297000"
>
> BR,
> Ulf W
>
> --
> Ulf Wiger
>


--
Roberto Saccon
Post generated from www.trapexit.org
Thomas Lindgren
Posted: Tue Jul 04, 2006 10:03 pm Reply with quote
User Joined: 09 Mar 2005 Posts: 284
--- Ulf Wiger <ulf@wiger.net> wrote:

> Den 2006-07-04 21:31:26 skrev Roberto Saccon
> <rsaccon@gmail.com>:
>
> > I liked the cheap solution:
> >
> > oid() -> {node(), erlang:now()}.
> >
> > but how can I turn erlang:now() into a String ??
> (which probablly
> > won't look very URL-friendly)
> >
> > I thouhgt of turing it into a list, then into a
> binary, then base64
> > encoding it, but
> > list_to_binary(tuple_to_list(erlang:now()))
> > does not work.
>
> Well, for example:
>
> 1> {Node,{MS,S,US}} = {node(), erlang:now()}.
> {nonode@nohost,{1152,47770,297000}}
> 2>
>
list_to_binary([atom_to_list(Node),"-",integer_to_list(MS),"-",integer_to_list(S),"-",integer_to_list(US)]).
>
<<110,111,110,111,100,101,64,110,111,104,111,115,116,45,49,49,53,50,45,52,55,55,55,48,45,50,57,55,48,...>>
> 3> binary_to_list(v(2)).
> "nonode@nohost-1152-47770-297000"

Or, a bit easier on the eyes (though perhaps a bit
more expensive):

fmt_oid({Node, {MS, S, US}}) ->
list_to_binary(
io_lib:format("~w-~w-~w-~w", [Node, MS, S, US])).

Best,
Thomas


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Post generated from www.trapexit.org
View user's profile Send private message
Guest
Posted: Tue Jul 04, 2006 10:27 pm Reply with quote
Guest
yeah, you are right, why not. Thanks for the suggestion.

regards
Roberto

On 7/4/06, RCB <rcbeerman@gmail.com> wrote:
> {T1, T2, T3} = now(),
> <<T1:16, T2:20, T3:20>>.
>
> Why not just generate a unique string?
>
> -module(randstr).
>
> -export([randstr/1, seedrand/0, seedrandstr/1]).
>
> chartab() ->
> { $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m,
> $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z,
> $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M,
> $N, $O, $P, $Q, $R, $S, $T, $U, $V, $W, $X, $Y, $Z,
> $0, $1, $2, $3, $4, $5, $6, $7, $8, $9 }.
>
> seedrand() ->
> {S1, S2, S3} = now(),
> random:seed(S1,S2,S3),
>
> seedrandstr(N) ->
> seedrand(),
> randstr(N).
>
> randstr(N) ->
> C = chartab(),
> randstr(N,C,size(C),[]).
> randstr(0,_,_,Acc) ->
> Acc;
> randstr(N,C,Csz,Acc) ->
> randstr(N - 1, C, Csz, [element(random:uniform(Csz),C) | Acc]).
>
>
>
>
>
> On 7/4/06, Roberto Saccon <rsaccon@gmail.com> wrote:
> > I liked the cheap solution:
> >
> > oid() -> {node(), erlang:now()}.
> >
> > but how can I turn erlang:now() into a String ?? (which probablly
> > won't look very URL-friendly)
> >
> > I thouhgt of turing it into a list, then into a binary, then base64
> > encoding it, but
> > list_to_binary(tuple_to_list(erlang:now()))
> > does not work.
> >
> > regards
> > Roberto
> --
> Rich Beerman
> Cupertino, California
> mobile: 408/221-2444
> fax: 408/255-7944
>


--
Roberto Saccon
Post generated from www.trapexit.org

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum