Return-Path: Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: (qmail 79070 invoked from network); 9 Sep 2009 20:24:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 9 Sep 2009 20:24:19 -0000 Received: (qmail 14373 invoked by uid 500); 9 Sep 2009 20:24:19 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 14290 invoked by uid 500); 9 Sep 2009 20:24:19 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 14281 invoked by uid 99); 9 Sep 2009 20:24:19 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Sep 2009 20:24:19 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Sep 2009 20:24:17 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C284223888D2; Wed, 9 Sep 2009 20:23:56 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r813102 - /couchdb/trunk/src/couchdb/couch_uuids.erl Date: Wed, 09 Sep 2009 20:23:56 -0000 To: commits@couchdb.apache.org From: kocolosk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090909202356.C284223888D2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kocolosk Date: Wed Sep 9 20:23:56 2009 New Revision: 813102 URL: http://svn.apache.org/viewvc?rev=813102&view=rev Log: oopsie Added: couchdb/trunk/src/couchdb/couch_uuids.erl Added: couchdb/trunk/src/couchdb/couch_uuids.erl URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_uuids.erl?rev=813102&view=auto ============================================================================== --- couchdb/trunk/src/couchdb/couch_uuids.erl (added) +++ couchdb/trunk/src/couchdb/couch_uuids.erl Wed Sep 9 20:23:56 2009 @@ -0,0 +1,88 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. +-module(couch_uuids). +-include("couch_db.hrl"). + +-behaviour(gen_server). + +-export([start/0, stop/0]). +-export([new/0, random/0, utc_random/0]). + +-export([init/1, terminate/2, code_change/3]). +-export([handle_call/3, handle_cast/2, handle_info/2]). + +start() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +stop() -> + gen_server:cast(?MODULE, stop). + +new() -> + gen_server:call(?MODULE, create). + +random() -> + list_to_binary(couch_util:to_hex(crypto:rand_bytes(16))). + +utc_random() -> + Now = {_, _, Micro} = now(), + Nowish = calendar:now_to_universal_time(Now), + Nowsecs = calendar:datetime_to_gregorian_seconds(Nowish), + Then = calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}), + Prefix = io_lib:format("~14.16.0b", [(Nowsecs - Then) * 1000000 + Micro]), + list_to_binary(Prefix ++ couch_util:to_hex(crypto:rand_bytes(9))). + +init([]) -> + ok = couch_config:register(fun("uuids", _) -> ?MODULE:stop() end), + AlgoStr = couch_config:get("uuids", "algorithm", "random"), + case couch_util:to_existing_atom(AlgoStr) of + random -> + {ok, random}; + utc_random -> + {ok, utc_random}; + sequential -> + {ok, {sequential, new_prefix(), inc()}}; + Unknown -> + throw({unknown_uuid_algorithm, Unknown}) + end. + +terminate(_Reason, _State) -> + ok. + +handle_call(create, _From, random) -> + {reply, random(), random}; +handle_call(create, _From, utc_random) -> + {reply, utc_random(), utc_random}; +handle_call(create, _From, {sequential, Pref, Seq}) -> + Result = ?l2b(Pref ++ io_lib:format("~6.16.0b", [Seq])), + case Seq >= 16#fff000 of + true -> + {reply, Result, {sequential, new_prefix(), inc()}}; + _ -> + {reply, Result, {sequential, Pref, Seq + inc()}} + end. + +handle_cast(stop, State) -> + {stop, normal, State}; +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_info(_Info, State) -> + {noreply, State}. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +new_prefix() -> + couch_util:to_hex((crypto:rand_bytes(13))). + +inc() -> + crypto:rand_uniform(1, 16#ffe).