mnesia:first/1 and mnesia:next/2

Chandrashekhar Mullaparthi <>
Tue Aug 5 18:20:41 CEST 2003


Hello everyone,

I need functions like mnesia:first/1 and mnesia:next/2 (which can be safely
run in the context of a transaction) similar to mnesia:dirty_first/1 and
mnesia:dirty_next/2 - I've implemented them by copying the read/2 function
and modifying it as follows :-) It seems to work alright.

Has anyone felt the need for such functions...? Any comments on the
implementation? Wont work very well for fragmented tables but I guess I can
live with that.

cheers
Chandru

%% ----------------------------------
%% Following code added to mnesia.erl
%% ----------------------------------

first(Tab) ->
    first(Tab, read).

first(Tab, LockKind) ->
    case get(mnesia_activity_state) of
	{?DEFAULT_ACCESS, Tid, Ts} ->
	    first(Tid, Ts, Tab, LockKind);
	{Mod, Tid, Ts} ->
	    Mod:first(Tid, Ts, Tab, LockKind);
	_ ->
	    abort(no_transaction)
    end.

first(Tid, Ts, Tab, LockKind)
  when atom(Tab), Tab /= schema ->
      case element(1, Tid) of
	  ets ->
	      ?ets_first(Tab);
	  tid ->
	      Store = Ts#tidstore.store,
	      Key = ?ets_first(Tab),
	      LockItem = {record, Tab, Key},
	      case LockKind of
		  read ->
		      lock(LockItem, read);
		  write ->
		      lock(LockItem, write);
		  sticky_write ->
		      lock(LockItem, sticky);
		  _ ->
		      abort({bad_type, Tab, LockItem})
	      end,
	      Key;
	  Protocol ->
	      dirty_first(Tab)
      end; 
first(Tid, Ts, Tab, LockKind) ->
    abort({bad_type, Tab}).


next(Tab, Key) ->
    next(Tab, Key, read).

next(Tab, Key, LockKind) ->
    case get(mnesia_activity_state) of
	{?DEFAULT_ACCESS, Tid, Ts} ->
	    next(Tid, Ts, Tab, Key, LockKind);
	{Mod, Tid, Ts} ->
	    Mod:next(Tid, Ts, Tab, Key, LockKind);
	_ ->
	    abort(no_transaction)
    end.

next(Tid, Ts, Tab, Key, LockKind)
  when atom(Tab), Tab /= schema ->
      case element(1, Tid) of
	  ets ->
	      ?ets_next(Tab, Key);
	  tid ->
	      Store = Ts#tidstore.store,
	      Key_1 = ?ets_next(Tab, Key),
	      LockItem = {record, Tab, Key_1},
	      case LockKind of
		  read ->
		      lock(LockItem, read);
		  write ->
		      lock(LockItem, write);
		  sticky_write ->
		      lock(LockItem, sticky);
		  _ ->
		      abort({bad_type, Tab, LockItem})
	      end,
	      Key_1;
	  Protocol ->
	      dirty_next(Tab, Key)
      end; 
next(Tid, Ts, Tab, Key, LockKind) ->
    abort({bad_type, Tab}).



 NOTICE AND DISCLAIMER:
This email (including attachments) is confidential.  If you have received
this email in error please notify the sender immediately and delete this
email from your system without copying or disseminating it or placing any
reliance upon its contents.  We cannot accept liability for any breaches of
confidence arising through use of email.  Any opinions expressed in this
email (including attachments) are those of the author and do not necessarily
reflect our opinions.  We will not accept responsibility for any commitments
made by our employees outside the scope of our business.  We do not warrant
the accuracy or completeness of such information.




More information about the erlang-questions mailing list