Inserting without overwriting with mnesia or ets

Zoltan Peter Toth Zoltan.Toth@REDACTED
Fri May 7 14:49:22 CEST 2004

Einar Karttunen wrote:
> Hello
> I need a way to insert into ets/mnesia and fail if the
> key already exists. These failures should be very rare
> and exceptional, but not checking for them could corrupt 
> data... Is there an efficient solution to this?
> The naive approach is not very good:
> insert(K,V) ->
> 	fun() ->
> 		case mnesia:wread(K) of
> 		[Exist] -> {abort, Exist};
> 		_ -> mnesia:write(V)
> 		end
> 	end.


If you have a RAM copy of your table on this host, you can check
the existence of the key in the ETS table holding the RAM copy.
(It has the same name as the mnesia table).
That should be very fast.
Of course it's more decent to access that table via mnesia:ets(...) :)
Would that help ?

More information about the erlang-questions mailing list