Return-Path: Delivered-To: apmail-couchdb-dev-archive@www.apache.org Received: (qmail 63933 invoked from network); 27 Jan 2011 11:10:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 27 Jan 2011 11:10:23 -0000 Received: (qmail 48867 invoked by uid 500); 27 Jan 2011 11:10:23 -0000 Delivered-To: apmail-couchdb-dev-archive@couchdb.apache.org Received: (qmail 48634 invoked by uid 500); 27 Jan 2011 11:10:20 -0000 Mailing-List: contact dev-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 dev@couchdb.apache.org Received: (qmail 48620 invoked by uid 99); 27 Jan 2011 11:10:19 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Jan 2011 11:10:19 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=10.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of bchesneau@gmail.com designates 209.85.210.180 as permitted sender) Received: from [209.85.210.180] (HELO mail-iy0-f180.google.com) (209.85.210.180) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Jan 2011 11:10:13 +0000 Received: by iyj21 with SMTP id 21so1420364iyj.11 for ; Thu, 27 Jan 2011 03:09:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=UIRsZPJhskn7Ut0+bs0kmmLKTzVRupPQfT6PMGqptO0=; b=a7UkUwe9qpuySPyiWNe7B0c4i+oZbu5HGtubYeEfUZ1tPEnCp2EIPiexw/DhZHn5D5 GnXhnZbmXyA2nt9JVx+V8PLlp7vFCKAsIY97kW939j5XhkGukAXj6Q4hRCIi4wIfIunU aQgqlAb6lzqrYDNZHZDOwB0Utye2mjJY07WsE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=oc3NgLIo5FAAaqBf1f6ArIFofz/OOUUuPpPd5Nw29TOfOp8A/wNeXkMZaGaMpj4RCt SEOBvOr09fQJ3Ws2/QwI9qGaVInY9cyVjr4gx1FWfukFWCglngyOTViNxBlK2Je6AO50 VOlw3tiHSOxMnfXfF88E8pbHXVChg/zOhEmtk= MIME-Version: 1.0 Received: by 10.231.14.131 with SMTP id g3mr761820iba.41.1296126592025; Thu, 27 Jan 2011 03:09:52 -0800 (PST) Received: by 10.231.36.67 with HTTP; Thu, 27 Jan 2011 03:09:51 -0800 (PST) In-Reply-To: <20110127104643.44D5723889F7@eris.apache.org> References: <20110127104643.44D5723889F7@eris.apache.org> Date: Thu, 27 Jan 2011 12:09:51 +0100 Message-ID: Subject: Re: svn commit: r1064076 - /couchdb/trunk/src/couchdb/couch_auth_cache.erl From: Benoit Chesneau To: dev@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I think we could remove the uses of lists call and just use pattern matching here. I attached a patch that is doing that. Also I added a guard in case there is no member items in the cache. - beno=EEt --- src/couchdb/couch_auth_cache.erl | 25 +++++++++++++++---------- 1 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/couchdb/couch_auth_cache.erl b/src/couchdb/couch_auth_cach= e.erl index 033e160..9bd3450 100644 --- a/src/couchdb/couch_auth_cache.erl +++ b/src/couchdb/couch_auth_cache.erl @@ -169,15 +169,7 @@ handle_call({new_max_cache_size, NewSize}, {reply, ok, State#state{max_cache_size =3D NewSize}}; handle_call({new_max_cache_size, NewSize}, _From, State) -> - lists:foreach( - fun(_) -> - LruTime =3D ets:last(?BY_ATIME), - [{LruTime, UserName}] =3D ets:lookup(?BY_ATIME, LruTime), - true =3D ets:delete(?BY_ATIME, LruTime), - true =3D ets:delete(?BY_USER, UserName) - end, - lists:seq(1, State#state.cache_size - NewSize) - ), + free_mru_cache_entries(State#state.cache_size - NewSize), {reply, ok, State#state{max_cache_size =3D NewSize, cache_size =3D New= Size}}; handle_call({fetch, UserName}, _From, State) -> @@ -223,7 +215,7 @@ terminate(_Reason, #state{db_notifier =3D Notifier}) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. - + clear_cache(State) -> exec_if_auth_db(fun(AuthDb) -> catch couch_db:close(AuthDb) end), @@ -243,6 +235,19 @@ add_cache_entry(UserName, Credentials, ATime, State) -= > true =3D ets:insert(?BY_USER, {UserName, {Credentials, ATime}}), State#state{cache_size =3D couch_util:get_value(size, ets:info(?BY_USE= R))}. +free_mru_cache_entries(1) -> + ok; +free_mru_cache_entries(Count) -> + case ets:last(?BY_ATIME) of + '$end_of_table' -> + ok; + LruTime -> + [{LruTime, UserName}] =3D ets:lookup(?BY_ATIME, LruTime), + true =3D ets:delete(?BY_ATIME, LruTime), + true =3D ets:delete(?BY_USER, UserName), + free_mru_cache_entries(Count-1) + end. + free_mru_cache_entry() -> case ets:last(?BY_ATIME) of --=20 1.7.3.4 On Thu, Jan 27, 2011 at 11:46 AM, wrote: > Author: fdmanana > Date: Thu Jan 27 10:46:42 2011 > New Revision: 1064076 > > URL: http://svn.apache.org/viewvc?rev=3D1064076&view=3Drev > Log: > Trivial small refactoring > > Shortening the code by using multiple function clauses. > > Modified: > =A0 =A0couchdb/trunk/src/couchdb/couch_auth_cache.erl > > Modified: couchdb/trunk/src/couchdb/couch_auth_cache.erl > URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_auth_ca= che.erl?rev=3D1064076&r1=3D1064075&r2=3D1064076&view=3Ddiff > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D > --- couchdb/trunk/src/couchdb/couch_auth_cache.erl (original) > +++ couchdb/trunk/src/couchdb/couch_auth_cache.erl Thu Jan 27 10:46:42 20= 11 > @@ -110,11 +110,8 @@ init(_) -> > =A0 =A0 ok =3D couch_config:register( > =A0 =A0 =A0 =A0 fun("couch_httpd_auth", "auth_cache_size", SizeList) -> > =A0 =A0 =A0 =A0 =A0 =A0 Size =3D list_to_integer(SizeList), > - =A0 =A0 =A0 =A0 =A0 =A0ok =3D gen_server:call(?MODULE, {new_max_cache_s= ize, Size}, infinity) > - =A0 =A0 =A0 =A0end > - =A0 =A0), > - =A0 =A0ok =3D couch_config:register( > - =A0 =A0 =A0 =A0fun("couch_httpd_auth", "authentication_db", DbName) -> > + =A0 =A0 =A0 =A0 =A0 =A0ok =3D gen_server:call(?MODULE, {new_max_cache_s= ize, Size}, infinity); > + =A0 =A0 =A0 =A0("couch_httpd_auth", "authentication_db", DbName) -> > =A0 =A0 =A0 =A0 =A0 =A0 ok =3D gen_server:call(?MODULE, {new_auth_db, ?l2= b(DbName)}, infinity) > =A0 =A0 =A0 =A0 end > =A0 =A0 ), > @@ -167,26 +164,21 @@ handle_call(auth_db_compacted, _From, St > =A0 =A0 ), > =A0 =A0 {reply, ok, State}; > > +handle_call({new_max_cache_size, NewSize}, > + =A0 =A0 =A0 =A0_From, #state{cache_size =3D Size} =3D State) when NewSi= ze >=3D Size -> > + =A0 =A0{reply, ok, State#state{max_cache_size =3D NewSize}}; > + > =A0handle_call({new_max_cache_size, NewSize}, _From, State) -> > - =A0 =A0case NewSize >=3D State#state.cache_size of > - =A0 =A0true -> > - =A0 =A0 =A0 =A0ok; > - =A0 =A0false -> > - =A0 =A0 =A0 =A0lists:foreach( > - =A0 =A0 =A0 =A0 =A0 =A0fun(_) -> > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0LruTime =3D ets:last(?BY_ATIME), > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0[{LruTime, UserName}] =3D ets:lookup(?BY= _ATIME, LruTime), > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0true =3D ets:delete(?BY_ATIME, LruTime), > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0true =3D ets:delete(?BY_USER, UserName) > - =A0 =A0 =A0 =A0 =A0 =A0end, > - =A0 =A0 =A0 =A0 =A0 =A0lists:seq(1, State#state.cache_size - NewSize) > - =A0 =A0 =A0 =A0) > - =A0 =A0end, > - =A0 =A0NewState =3D State#state{ > - =A0 =A0 =A0 =A0max_cache_size =3D NewSize, > - =A0 =A0 =A0 =A0cache_size =3D lists:min([NewSize, State#state.cache_siz= e]) > - =A0 =A0}, > - =A0 =A0{reply, ok, NewState}; > + =A0 =A0lists:foreach( > + =A0 =A0 =A0 =A0fun(_) -> > + =A0 =A0 =A0 =A0 =A0 =A0LruTime =3D ets:last(?BY_ATIME), > + =A0 =A0 =A0 =A0 =A0 =A0[{LruTime, UserName}] =3D ets:lookup(?BY_ATIME, = LruTime), > + =A0 =A0 =A0 =A0 =A0 =A0true =3D ets:delete(?BY_ATIME, LruTime), > + =A0 =A0 =A0 =A0 =A0 =A0true =3D ets:delete(?BY_USER, UserName) > + =A0 =A0 =A0 =A0end, > + =A0 =A0 =A0 =A0lists:seq(1, State#state.cache_size - NewSize) > + =A0 =A0), > + =A0 =A0{reply, ok, State#state{max_cache_size =3D NewSize, cache_size = =3D NewSize}}; > > =A0handle_call({fetch, UserName}, _From, State) -> > =A0 =A0 {Credentials, NewState} =3D case ets:lookup(?BY_USER, UserName) o= f > > >