couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r954451 - in /couchdb/branches/0.11.x/src/couchdb: couch_db.erl couch_server.erl
Date Mon, 14 Jun 2010 13:32:38 GMT
Author: jan
Date: Mon Jun 14 13:32:38 2010
New Revision: 954451

URL: http://svn.apache.org/viewvc?rev=954451&view=rev
Log:
Revert "Merge r923456 from trunk:"

This reverts commit ee9c26c6c7bff1de83ffc3862abc95dd533184e8.

Modified:
    couchdb/branches/0.11.x/src/couchdb/couch_db.erl
    couchdb/branches/0.11.x/src/couchdb/couch_server.erl

Modified: couchdb/branches/0.11.x/src/couchdb/couch_db.erl
URL: http://svn.apache.org/viewvc/couchdb/branches/0.11.x/src/couchdb/couch_db.erl?rev=954451&r1=954450&r2=954451&view=diff
==============================================================================
--- couchdb/branches/0.11.x/src/couchdb/couch_db.erl (original)
+++ couchdb/branches/0.11.x/src/couchdb/couch_db.erl Mon Jun 14 13:32:38 2010
@@ -93,8 +93,9 @@ ensure_full_commit(#db{update_pid=Update
 close(#db{fd_ref_counter=RefCntr}) ->
     couch_ref_counter:drop(RefCntr).
 
-open_ref_counted(MainPid, OpenedPid) ->
-    gen_server:call(MainPid, {open_ref_count, OpenedPid}).
+open_ref_counted(MainPid, UserCtx) ->
+    {ok, Db} = gen_server:call(MainPid, {open_ref_count, self()}),
+    {ok, Db#db{user_ctx=UserCtx}}.
 
 is_idle(MainPid) ->
     gen_server:call(MainPid, is_idle).

Modified: couchdb/branches/0.11.x/src/couchdb/couch_server.erl
URL: http://svn.apache.org/viewvc/couchdb/branches/0.11.x/src/couchdb/couch_server.erl?rev=954451&r1=954450&r2=954451&view=diff
==============================================================================
--- couchdb/branches/0.11.x/src/couchdb/couch_server.erl (original)
+++ couchdb/branches/0.11.x/src/couchdb/couch_server.erl Mon Jun 14 13:32:38 2010
@@ -52,18 +52,18 @@ sup_start_link() ->
 
 open(DbName, Options) ->
     case gen_server:call(couch_server, {open, DbName, Options}) of
-    {ok, Db} ->
+    {ok, MainPid} ->
         Ctx = proplists:get_value(user_ctx, Options, #user_ctx{}),
-        {ok, Db#db{user_ctx=Ctx}};
+        couch_db:open_ref_counted(MainPid, Ctx);
     Error ->
         Error
     end.
 
 create(DbName, Options) ->
     case gen_server:call(couch_server, {create, DbName, Options}) of
-    {ok, Db} ->
+    {ok, MainPid} ->
         Ctx = proplists:get_value(user_ctx, Options, #user_ctx{}),
-        {ok, Db#db{user_ctx=Ctx}};
+        couch_db:open_ref_counted(MainPid, Ctx);
     Error ->
         Error
     end.
@@ -187,7 +187,7 @@ try_close_lru(StartTime) ->
         {error, all_dbs_active};
     true ->
         [{_, DbName}] = ets:lookup(couch_dbs_by_lru, LruTime),
-        [{_, {opened, MainPid, LruTime}}] = ets:lookup(couch_dbs_by_name, DbName),
+        [{_, {MainPid, LruTime}}] = ets:lookup(couch_dbs_by_name, DbName),
         case couch_db:is_idle(MainPid) of
         true ->
             couch_util:shutdown_sync(MainPid),
@@ -199,7 +199,7 @@ try_close_lru(StartTime) ->
             % this still has referrers. Go ahead and give it a current lru time
             % and try the next one in the table.
             NewLruTime = now(),
-            true = ets:insert(couch_dbs_by_name, {DbName, {opened, MainPid, NewLruTime}}),
+            true = ets:insert(couch_dbs_by_name, {DbName, {MainPid, NewLruTime}}),
             true = ets:insert(couch_dbs_by_pid, {MainPid, DbName}),
             true = ets:delete(couch_dbs_by_lru, LruTime),
             true = ets:insert(couch_dbs_by_lru, {NewLruTime, DbName}),
@@ -207,44 +207,11 @@ try_close_lru(StartTime) ->
         end
     end.
 
-open_async(Server, From, DbName, Filepath, Options) ->
-    Parent = self(),
-    Opener = spawn_link(fun() ->
-            Res = couch_db:start_link(DbName, Filepath, Options),
-            gen_server:call(Parent, {open_result, DbName, Res}, infinity),
-            unlink(Parent)
-        end),
-    true = ets:insert(couch_dbs_by_name, {DbName, {opening, Opener, [From]}}),
-    true = ets:insert(couch_dbs_by_pid, {Opener, DbName}),
-    Server#server{dbs_open=Server#server.dbs_open + 1}.
-
 handle_call({set_max_dbs_open, Max}, _From, Server) ->
     {reply, ok, Server#server{max_dbs_open=Max}};
 handle_call(get_server, _From, Server) ->
     {reply, {ok, Server}, Server};
-handle_call({open_result, DbName, {ok, OpenedDbPid}}, _From, Server) ->
-    link(OpenedDbPid),
-    [{DbName, {opening,Opener,Froms}}] = ets:lookup(couch_dbs_by_name, DbName),
-    lists:foreach(fun({FromPid,_}=From) ->
-        gen_server:reply(From, 
-                catch couch_db:open_ref_counted(OpenedDbPid, FromPid))
-    end, Froms),
-    LruTime = now(),
-    true = ets:insert(couch_dbs_by_name,
-            {DbName, {opened, OpenedDbPid, LruTime}}),
-    true = ets:delete(couch_dbs_by_pid, Opener),
-    true = ets:insert(couch_dbs_by_pid, {OpenedDbPid, DbName}),
-    true = ets:insert(couch_dbs_by_lru, {LruTime, DbName}),
-    {reply, ok, Server};
-handle_call({open_result, DbName, Error}, _From, Server) ->    
-    [{DbName, {opening,Opener,Froms}}] = ets:lookup(couch_dbs_by_name, DbName),
-    lists:foreach(fun(From) ->
-        gen_server:reply(From, Error)
-    end, Froms),    
-    true = ets:delete(couch_dbs_by_name, DbName),
-    true = ets:delete(couch_dbs_by_pid, Opener),
-    {reply, ok, Server#server{dbs_open=Server#server.dbs_open - 1}};
-handle_call({open, DbName, Options}, {FromPid,_}=From, Server) ->
+handle_call({open, DbName, Options}, _From, Server) ->
     LruTime = now(),
     case ets:lookup(couch_dbs_by_name, DbName) of
     [] ->
@@ -254,33 +221,53 @@ handle_call({open, DbName, Options}, {Fr
             case maybe_close_lru_db(Server) of
             {ok, Server2} ->
                 Filepath = get_full_filename(Server, DbNameList),
-                {noreply, open_async(Server2, From, DbName, Filepath, Options)};
+                case couch_db:start_link(DbName, Filepath, Options) of
+                {ok, MainPid} ->
+                    true = ets:insert(couch_dbs_by_name, {DbName, {MainPid, LruTime}}),
+                    true = ets:insert(couch_dbs_by_pid, {MainPid, DbName}),
+                    true = ets:insert(couch_dbs_by_lru, {LruTime, DbName}),
+                    DbsOpen = Server2#server.dbs_open + 1,
+                    {reply, {ok, MainPid},
+                            Server2#server{dbs_open=DbsOpen}};
+                Error ->
+                    {reply, Error, Server2}
+                end;
             CloseError ->
                 {reply, CloseError, Server}
             end;
         Error ->
             {reply, Error, Server}
         end;
-    [{_, {opening, Opener, Froms}}] ->
-        true = ets:insert(couch_dbs_by_name, {DbName, {opening, Opener, [From|Froms]}}),
-        {noreply, Server};
-    [{_, {opened, MainPid, PrevLruTime}}] ->
-        true = ets:insert(couch_dbs_by_name, {DbName, {opened, MainPid, LruTime}}),
+    [{_, {MainPid, PrevLruTime}}] ->
+        true = ets:insert(couch_dbs_by_name, {DbName, {MainPid, LruTime}}),
         true = ets:delete(couch_dbs_by_lru, PrevLruTime),
         true = ets:insert(couch_dbs_by_lru, {LruTime, DbName}),
-        {reply, couch_db:open_ref_counted(MainPid, FromPid), Server}
+        {reply, {ok, MainPid}, Server}
     end;
-handle_call({create, DbName, Options}, From, Server) ->
+handle_call({create, DbName, Options}, _From, Server) ->
     DbNameList = binary_to_list(DbName),
     case check_dbname(Server, DbNameList) of
     ok ->
+        Filepath = get_full_filename(Server, DbNameList),
+
         case ets:lookup(couch_dbs_by_name, DbName) of
         [] ->
             case maybe_close_lru_db(Server) of
             {ok, Server2} ->
-                Filepath = get_full_filename(Server, DbNameList),
-                {noreply, open_async(Server2, From, DbName, Filepath,
-                        [create | Options])};
+                case couch_db:start_link(DbName, Filepath, [create|Options]) of
+                {ok, MainPid} ->
+                    LruTime = now(),
+                    true = ets:insert(couch_dbs_by_name,
+                            {DbName, {MainPid, LruTime}}),
+                    true = ets:insert(couch_dbs_by_pid, {MainPid, DbName}),
+                    true = ets:insert(couch_dbs_by_lru, {LruTime, DbName}),
+                    DbsOpen = Server2#server.dbs_open + 1,
+                    couch_db_update_notifier:notify({created, DbName}),
+                    {reply, {ok, MainPid},
+                            Server2#server{dbs_open=DbsOpen}};
+                Error ->
+                    {reply, Error, Server2}
+                end;
             CloseError ->
                 {reply, CloseError, Server}
             end;
@@ -298,16 +285,8 @@ handle_call({delete, DbName, _Options}, 
         Server2 =
         case ets:lookup(couch_dbs_by_name, DbName) of
         [] -> Server;
-        [{_, {opening, Pid, Froms}}] ->
-            exit(Pid, kill),
-            receive {'EXIT', Pid, _Reason} -> ok end,
-            true = ets:delete(couch_dbs_by_name, DbName),
-            true = ets:delete(couch_dbs_by_pid, Pid),
-            [gen_server:send_result(F, not_found) || F <- Froms],
-            Server#server{dbs_open=Server#server.dbs_open - 1};
-        [{_, {opened, Pid, LruTime}}] ->
-            exit(Pid, kill),
-            receive {'EXIT', Pid, _Reason} -> ok end,
+        [{_, {Pid, LruTime}}] ->
+            couch_util:shutdown_sync(Pid),
             true = ets:delete(couch_dbs_by_name, DbName),
             true = ets:delete(couch_dbs_by_pid, Pid),
             true = ets:delete(couch_dbs_by_lru, LruTime),
@@ -337,19 +316,18 @@ handle_cast(Msg, _Server) ->
 code_change(_OldVsn, State, _Extra) ->
     {ok, State}.
 
-handle_info({'EXIT', _Pid, config_change}, _Server) ->
+handle_info({'EXIT', _Pid, config_change}, Server) ->
     {stop, shutdown, Server};
 handle_info({'EXIT', Pid, Reason}, #server{dbs_open=DbsOpen}=Server) ->
-    [{Pid, DbName}] = ets:lookup(couch_dbs_by_pid, Pid),
-    case ets:lookup(couch_dbs_by_name, DbName) of
-    [{DbName, {opened, Pid, LruTime}}] ->
-        true = ets:delete(couch_dbs_by_lru, LruTime);
-    [{DbName, {opening, Pid, Froms}}] ->
-        [gen_server:reply(From, Reason) || From <- Froms]
-    end,
-    true = ets:delete(couch_dbs_by_pid, Pid),
-    true = ets:delete(couch_dbs_by_name, DbName),
-    {noreply, Server#server{dbs_open=DbsOpen - 1}};
-qhandle_info(Info, _Server) ->
+    case ets:lookup(couch_dbs_by_pid, Pid) of
+    [{Pid, DbName}] ->
+        [{DbName, {Pid, LruTime}}] = ets:lookup(couch_dbs_by_name, DbName),
+        true = ets:delete(couch_dbs_by_pid, Pid),
+        true = ets:delete(couch_dbs_by_name, DbName),
+        true = ets:delete(couch_dbs_by_lru, LruTime),
+        {noreply, Server#server{dbs_open=DbsOpen - 1}};
+    _ ->
+        {stop,Reason,Server}
+    end;
+handle_info(Info, _Server) ->
     exit({unknown_message, Info}).
-



Mime
View raw message