use only normal iterator function
This commit is contained in:
parent
43b861215a
commit
dd23a80e27
@ -605,12 +605,12 @@ delete(Alias, Tab, Key) ->
|
|||||||
|
|
||||||
first(Alias, Tab) ->
|
first(Alias, Tab) ->
|
||||||
{Ref, _Type} = get_ref(Alias, Tab),
|
{Ref, _Type} = get_ref(Alias, Tab),
|
||||||
with_keys_only_iterator(Ref, fun i_first/1).
|
with_iterator(Ref, fun i_first/1).
|
||||||
|
|
||||||
%% PRIVATE ITERATOR
|
%% PRIVATE ITERATOR
|
||||||
i_first(I) ->
|
i_first(I) ->
|
||||||
case ?rocksdb:iterator_move(I, <<?DATA_START>>) of
|
case ?rocksdb:iterator_move(I, <<?DATA_START>>) of
|
||||||
{ok, First} ->
|
{ok, First, _} ->
|
||||||
decode_key(First);
|
decode_key(First);
|
||||||
_ ->
|
_ ->
|
||||||
'$end_of_table'
|
'$end_of_table'
|
||||||
@ -630,14 +630,14 @@ insert(Alias, Tab, Obj) ->
|
|||||||
|
|
||||||
last(Alias, Tab) ->
|
last(Alias, Tab) ->
|
||||||
{Ref, _Type} = get_ref(Alias, Tab),
|
{Ref, _Type} = get_ref(Alias, Tab),
|
||||||
with_keys_only_iterator(Ref, fun i_last/1).
|
with_iterator(Ref, fun i_last/1).
|
||||||
|
|
||||||
%% PRIVATE ITERATOR
|
%% PRIVATE ITERATOR
|
||||||
i_last(I) ->
|
i_last(I) ->
|
||||||
case ?rocksdb:iterator_move(I, last) of
|
case ?rocksdb:iterator_move(I, last) of
|
||||||
{ok, << ?INFO_TAG, _/binary >>} ->
|
{ok, << ?INFO_TAG, _/binary >>, _} ->
|
||||||
'$end_of_table';
|
'$end_of_table';
|
||||||
{ok, Last} ->
|
{ok, Last, _} ->
|
||||||
decode_key(Last);
|
decode_key(Last);
|
||||||
_ ->
|
_ ->
|
||||||
'$end_of_table'
|
'$end_of_table'
|
||||||
@ -703,18 +703,18 @@ match_delete(Alias, Tab, Pat) when is_tuple(Pat) ->
|
|||||||
next(Alias, Tab, Key) ->
|
next(Alias, Tab, Key) ->
|
||||||
{Ref, _Type} = get_ref(Alias, Tab),
|
{Ref, _Type} = get_ref(Alias, Tab),
|
||||||
EncKey = encode_key(Key),
|
EncKey = encode_key(Key),
|
||||||
with_keys_only_iterator(Ref, fun(I) -> i_next(I, EncKey, Key) end).
|
with_iterator(Ref, fun(I) -> i_next(I, EncKey, Key) end).
|
||||||
|
|
||||||
%% PRIVATE ITERATOR
|
%% PRIVATE ITERATOR
|
||||||
i_next(I, EncKey, Key) ->
|
i_next(I, EncKey, Key) ->
|
||||||
case ?rocksdb:iterator_move(I, EncKey) of
|
case ?rocksdb:iterator_move(I, EncKey) of
|
||||||
{ok, EncKey} ->
|
{ok, EncKey, _} ->
|
||||||
i_next_loop(?rocksdb:iterator_move(I, next), I, Key);
|
i_next_loop(?rocksdb:iterator_move(I, next), I, Key);
|
||||||
Other ->
|
Other ->
|
||||||
i_next_loop(Other, I, Key)
|
i_next_loop(Other, I, Key)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
i_next_loop({ok, EncKey}, I, Key) ->
|
i_next_loop({ok, EncKey, _}, I, Key) ->
|
||||||
case decode_key(EncKey) of
|
case decode_key(EncKey) of
|
||||||
Key ->
|
Key ->
|
||||||
i_next_loop(?rocksdb:iterator_move(I, next), I, Key);
|
i_next_loop(?rocksdb:iterator_move(I, next), I, Key);
|
||||||
@ -727,12 +727,12 @@ i_next_loop(_, _I, _Key) ->
|
|||||||
prev(Alias, Tab, Key0) ->
|
prev(Alias, Tab, Key0) ->
|
||||||
{Ref, _Type} = call(Alias, Tab, get_ref),
|
{Ref, _Type} = call(Alias, Tab, get_ref),
|
||||||
Key = encode_key(Key0),
|
Key = encode_key(Key0),
|
||||||
with_keys_only_iterator(Ref, fun(I) -> i_prev(I, Key) end).
|
with_iterator(Ref, fun(I) -> i_prev(I, Key) end).
|
||||||
|
|
||||||
%% PRIVATE ITERATOR
|
%% PRIVATE ITERATOR
|
||||||
i_prev(I, Key) ->
|
i_prev(I, Key) ->
|
||||||
case ?rocksdb:iterator_move(I, Key) of
|
case ?rocksdb:iterator_move(I, Key) of
|
||||||
{ok, _} ->
|
{ok, _, _} ->
|
||||||
i_move_to_prev(I, Key);
|
i_move_to_prev(I, Key);
|
||||||
{error, invalid_iterator} ->
|
{error, invalid_iterator} ->
|
||||||
i_last(I)
|
i_last(I)
|
||||||
@ -741,11 +741,11 @@ i_prev(I, Key) ->
|
|||||||
%% PRIVATE ITERATOR
|
%% PRIVATE ITERATOR
|
||||||
i_move_to_prev(I, Key) ->
|
i_move_to_prev(I, Key) ->
|
||||||
case ?rocksdb:iterator_move(I, prev) of
|
case ?rocksdb:iterator_move(I, prev) of
|
||||||
{ok, << ?INFO_TAG, _/binary >>} ->
|
{ok, << ?INFO_TAG, _/binary >>, _} ->
|
||||||
'$end_of_table';
|
'$end_of_table';
|
||||||
{ok, Prev} when Prev < Key ->
|
{ok, Prev, _} when Prev < Key ->
|
||||||
decode_key(Prev);
|
decode_key(Prev);
|
||||||
{ok, _} ->
|
{ok, _, _} ->
|
||||||
i_move_to_prev(I, Key);
|
i_move_to_prev(I, Key);
|
||||||
_ ->
|
_ ->
|
||||||
'$end_of_table'
|
'$end_of_table'
|
||||||
@ -836,12 +836,12 @@ with_iterator(Ref, F) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
%% keys_only iterator: iterator_move/2 returns {ok, EncKey}
|
%% keys_only iterator: iterator_move/2 returns {ok, EncKey}
|
||||||
with_keys_only_iterator(Ref, F) ->
|
%% with_keys_only_iterator(Ref, F) ->
|
||||||
{ok, I} = ?rocksdb:iterator(Ref, [], keys_only),
|
%% {ok, I} = ?rocksdb:iterator(Ref, [], keys_only),
|
||||||
try F(I)
|
%% try F(I)
|
||||||
after
|
%% after
|
||||||
?rocksdb:iterator_close(I)
|
%% ?rocksdb:iterator_close(I)
|
||||||
end.
|
%% end.
|
||||||
|
|
||||||
%% TODO - use with_keys_only_iterator for match_delete
|
%% TODO - use with_keys_only_iterator for match_delete
|
||||||
|
|
||||||
@ -1246,7 +1246,7 @@ do_delete(Key, #st{ets = Ets, ref = Ref, maintain_size = true}) ->
|
|||||||
|
|
||||||
do_delete_bag(Sz, Key, Ref, TSz) ->
|
do_delete_bag(Sz, Key, Ref, TSz) ->
|
||||||
Found =
|
Found =
|
||||||
with_keys_only_iterator(
|
with_iterator(
|
||||||
Ref, fun(I) ->
|
Ref, fun(I) ->
|
||||||
do_delete_bag_(Sz, Key, ?rocksdb:iterator_move(I, Key),
|
do_delete_bag_(Sz, Key, ?rocksdb:iterator_move(I, Key),
|
||||||
Ref, I)
|
Ref, I)
|
||||||
@ -1267,10 +1267,10 @@ do_delete_bag(Sz, Key, Ref, TSz) ->
|
|||||||
|
|
||||||
do_delete_bag_(Sz, K, Res, Ref, I) ->
|
do_delete_bag_(Sz, K, Res, Ref, I) ->
|
||||||
case Res of
|
case Res of
|
||||||
{ok, K} ->
|
{ok, K, _} ->
|
||||||
do_delete_bag_(Sz, K, ?rocksdb:iterator_move(I, next),
|
do_delete_bag_(Sz, K, ?rocksdb:iterator_move(I, next),
|
||||||
Ref, I);
|
Ref, I);
|
||||||
{ok, <<K:Sz/binary, _:?BAG_CNT>> = Key} ->
|
{ok, <<K:Sz/binary, _:?BAG_CNT>> = Key, _} ->
|
||||||
[Key |
|
[Key |
|
||||||
do_delete_bag_(Sz, K, ?rocksdb:iterator_move(I, next),
|
do_delete_bag_(Sz, K, ?rocksdb:iterator_move(I, next),
|
||||||
Ref, I)];
|
Ref, I)];
|
||||||
|
@ -82,8 +82,10 @@ setup_mnesia() ->
|
|||||||
|
|
||||||
setup() ->
|
setup() ->
|
||||||
{atomic,ok} = mnesia:create_table(d, [{disc_copies, [node()]},
|
{atomic,ok} = mnesia:create_table(d, [{disc_copies, [node()]},
|
||||||
|
{type, ordered_set},
|
||||||
{record_name, x}]),
|
{record_name, x}]),
|
||||||
{atomic,ok} = mnesia:create_table(l, [{rocksdb_copies, [node()]},
|
{atomic,ok} = mnesia:create_table(l, [{rocksdb_copies, [node()]},
|
||||||
|
{type, ordered_set},
|
||||||
{record_name, x}]),
|
{record_name, x}]),
|
||||||
ok = mnesia:wait_for_tables([d, l], 30000),
|
ok = mnesia:wait_for_tables([d, l], 30000),
|
||||||
ok.
|
ok.
|
||||||
@ -121,7 +123,11 @@ db_cmd() ->
|
|||||||
?LET(Type, type(),
|
?LET(Type, type(),
|
||||||
proper_types:oneof([{Type, read, key()},
|
proper_types:oneof([{Type, read, key()},
|
||||||
{Type, write, key(), value()},
|
{Type, write, key(), value()},
|
||||||
{Type, delete, key()}])).
|
{Type, delete, key()},
|
||||||
|
{Type, first},
|
||||||
|
{Type, next, key()},
|
||||||
|
{Type, prev, key()},
|
||||||
|
{Type, last}])).
|
||||||
|
|
||||||
key() ->
|
key() ->
|
||||||
proper_types:oneof([a,b,c]).
|
proper_types:oneof([a,b,c]).
|
||||||
@ -147,14 +153,26 @@ apply_seq(transaction=X, Tab, [H|T], Acc) ->
|
|||||||
{X,write,K,V} -> mnesia:write(Tab, {x, K, V}, write);
|
{X,write,K,V} -> mnesia:write(Tab, {x, K, V}, write);
|
||||||
{_,write,K,V} -> mnesia:dirty_write(Tab, {x,K,V});
|
{_,write,K,V} -> mnesia:dirty_write(Tab, {x,K,V});
|
||||||
{X,delete,K} -> mnesia:delete(Tab, K, write);
|
{X,delete,K} -> mnesia:delete(Tab, K, write);
|
||||||
{_,delete,K} -> mnesia:dirty_delete(Tab,K)
|
{_,delete,K} -> mnesia:dirty_delete(Tab,K);
|
||||||
|
{X,first} -> mnesia:first(Tab);
|
||||||
|
{_,first} -> mnesia:dirty_first(Tab);
|
||||||
|
{X,next,K} -> mnesia:next(Tab, K);
|
||||||
|
{_,next,K} -> mnesia:dirty_next(Tab, K);
|
||||||
|
{X,prev,K} -> mnesia:prev(Tab, K);
|
||||||
|
{_,prev,K} -> mnesia:dirty_prev(Tab, K);
|
||||||
|
{X,last} -> mnesia:last(Tab);
|
||||||
|
{_,last} -> mnesia:dirty_last(Tab)
|
||||||
end,
|
end,
|
||||||
apply_seq(X, Tab, T, [Res|Acc]);
|
apply_seq(X, Tab, T, [Res|Acc]);
|
||||||
apply_seq(X, Tab, [H|T], Acc) ->
|
apply_seq(X, Tab, [H|T], Acc) ->
|
||||||
Res = case H of
|
Res = case H of
|
||||||
{_,read, K} -> mnesia:read(Tab, K, read);
|
{_,read, K} -> mnesia:read(Tab, K, read);
|
||||||
{_,write,K,V} -> mnesia:write(Tab, {x, K, V}, write);
|
{_,write,K,V} -> mnesia:write(Tab, {x, K, V}, write);
|
||||||
{_,delete,K} -> mnesia:delete(Tab, K, write)
|
{_,delete,K} -> mnesia:delete(Tab, K, write);
|
||||||
|
{_,first} -> mnesia:first(Tab);
|
||||||
|
{_,next,K} -> mnesia:next(Tab, K);
|
||||||
|
{_,prev,K} -> mnesia:prev(Tab, K);
|
||||||
|
{_,last} -> mnesia:last(Tab)
|
||||||
end,
|
end,
|
||||||
apply_seq(X, Tab, T, [Res|Acc]);
|
apply_seq(X, Tab, T, [Res|Acc]);
|
||||||
apply_seq(_, _, [], Acc) ->
|
apply_seq(_, _, [], Acc) ->
|
||||||
|
Loading…
x
Reference in New Issue
Block a user