rfc: rdbms - new type system

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Mon Feb 27 18:05:53 CET 2006


Ulf Wiger wrote:

> I'm toying with the idea of allowing a match specification
> as a table-level 'type' specification:

> Ms = [{{1,2,'$1'}, [{is_integer,'$1'}], [{{1,2,{float,'$1'}}}]}].
> [{{1,2,'$1'},[{is_integer,'$1'}],[{{1,2,{float,'$1'}}}]}]
> 10> ets:match_spec_run([{1,2,3}],ets:match_spec_compile(Ms)).

> [{1,2,3.00000}]

> It sure is ugly, but it's there, and it's fast. It 
> is also able to do the type coercion you're asking for.

Ok, I added this to rdbms. It gives you your float
coercion:

rdbms_write_filter(Config) when is_list(Config) ->
    ?line {atomic, ok} = rdbms:create_table(
			   rdbms_w_filter,
			   [{attributes, [key, value]},
			    {rdbms,
			     [{write_filter, 
			       [{{rdbms_w_filter,'$1','$2'},
				 [],
[{{rdbms_w_filter,'$1',{float,'$2'}}}]}]},
			      {{attr,value,type}, float}
			     ]}
			    ]),
     ?line {atomic, ok} = rdbms:create_table(
			   rdbms_no_w_filter,
			   [{attributes, [key, value]},
			    {rdbms,
			     [
			      {{attr,value,type}, float}
			     ]}
			    ]),
   
    ?line trans(fun() ->
			mnesia:write({rdbms_w_filter, 17, 1})
		end),
    ?line trans(fun() ->
			mnesia:write({rdbms_w_filter, 17, 1.0})
		end),
    ?line fail_trans(fun() ->
			mnesia:write({rdbms_no_w_filter, 17, 1})
		end),
    ?line trans(fun() ->
			mnesia:write({rdbms_no_w_filter, 17, 1.0})
		end).

(The type check on #rdbms_no_w_filter{value = 1} fails
since it's not a float, and there is no filter.)

Any type specifications are run on the output from the 
write_filter. If it doesn't produce a valid record, the
transaction is aborted.

There is also a read_filter, but I haven't added code 
for that yet. There is no overhead if the filters aren't
used (the code generator doesn't lay out code if the 
filters are undefined).

Applying the actual filter seems to take a few tens of
microseconds. I have to compile the match spec each
time. I don't think it can be avoided.

/Uffe



More information about the erlang-questions mailing list