ublog

erlang : робота з ets

programming [35]erlang [33]

приклади роботи з ets ( Erlang Term Storage )
ets:new(table_test, [ordered_set, public, named_table]).
ets:insert(table_test, {1, 777}).
ets:insert(table_test, {3, 779}).
ets:insert(table_test, {2, 778}).

ets:match_object(table_test, {'$1', '$2'}).
[{1,777},
 {2,778},
 {3,779}]

ets:fun2ms(fun({A,B}) -> {A,B} end).
[{{'$1','$2'},[],[{{'$1','$2'}}]}]

ets:select(table_test, [{{'$1','$2'},[],[{{'$1','$2'}}]}], 1).
{[{1,777}],
 {table_test,1,[],1,#Ref<0.2209292443.1747320834.54125>,[],0,
            0}}

ets:select(table_test, [{{'$1','$2'},[],[{{'$1','$2'}}]}], 5).
{[{1,777},{2,779},{3,778}],
  '$end_of_table'}

ets:delete(table_test, 2).
ets:match_object(table_test, {'$1', '$2'}).
[{1,777},{3,779}]


ets:new(table_test, [ordered_set, public, named_table]).
ets:insert(table_test, {{1, 777}, 888}).

ets:match_object(table_test, {'$1', '$2'}).
[{{1,777},888}]

ets:fun2ms(fun({A,B}) -> {A,B} end).
[{{'$1','$2'},[],[{{'$1','$2'}}]}]

ets:select(table_test, [{{'$1','$2'},[],[{{'$1','$2'}}]}], 1).
{[{{1,777},888}],
 {table_test,{1,777},
            [],1,#Ref<0.469085729.436600833.148380>,[],0,0}}

ets:insert(table_test, {{3, 772}, 880}).
ets:insert(table_test, {{2, 771}, 88760}).
ets:insert(table_test, {{3, 770}, 8890}).
ets:insert(table_test, {{4, 771}, 8760}).

ets:select(table_test, [{{'$1','$2'},[],[{{'$1','$2'}}]}], 10).
{[{{1,777},888},
  {{2,771},88760},
  {{3,770},8890},
  {{3,772},880},
  {{4,771},8760}],
 '$end_of_table'}

ets:delete(table_test, {2,771}).

ets:select(table_test, [{{'$1','$2'},[],[{{'$1','$2'}}]}], 10).
{[{{1,777},888},
  {{3,770},8890},
  {{3,772},880},
  {{4,771},8760}],
 '$end_of_table'}


гіперпосилання по темі:

https://elixirschool.com/ru/lessons/specifics/ets/
http://learnyousomeerlang.com/ets
https://www.proctor-it.com/erlang-thursday-using-ets-select-with-a-limit/
http://erlang.org/doc/man/ets.html#fun2ms-1
http://erlang.org/doc/apps/erts/match_spec.html

далі буде :)