8000 otp/lib/stdlib/src/dets.erl at master · googol-lab/otp · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
3257 lines (2974 loc) · 101 KB

dets.erl

File metadata and controls

3257 lines (2974 loc) · 101 KB
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2022. All Rights Reserved.
%% 8BD0
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(dets).
%% Disk based linear hashing lookup dictionary.
%% Public.
-export([all/0,
bchunk/2,
close/1,
delete/2,
delete_all_objects/1,
delete_object/2,
first/1,
foldl/3,
foldr/3,
from_ets/2,
info/1,
info/2,
init_table/2,
init_table/3,
insert/2,
insert_new/2,
is_compatible_bchunk_format/2,
is_dets_file/1,
lookup/2,
match/1,
match/2,
match/3,
match_delete/2,
match_object/1,
match_object/2,
match_object/3,
member/2,
next/2,
open_file/1,
open_file/2,
pid2name/1,
repair_continuation/2,
safe_fixtable/2,
select/1,
select/2,
select/3,
select_delete/2,
slot/2,
sync/1,
table/1,
table/2,
to_ets/2,
traverse/2,
update_counter/3]).
%% Server export.
-export([start/0, stop/0]).
%% Internal exports.
-export([istart_link/1, init/2, internal_open/3, add_user/3,
internal_close/1, remove_user/2,
system_continue/3, system_terminate/4, system_code_change/4]).
%% Debug.
-export([file_info/1,
fsck/1,
fsck/2,
get_head_field/2,
view/1,
where/2,
verbose/0,
verbose/1
]).
%% Not documented, or not ready for publication.
-export([lookup_keys/2]).
-export_type([bindings_cont/0, cont/0, object_cont/0, select_cont/0,
tab_name/0]).
-compile({inline, [{einval,2},{badarg,2},{undefined,1},
{badarg_exit,2},{lookup_reply,2}]}).
-include_lib("kernel/include/file.hrl").
-include("dets.hrl").
%%% This is the implementation of the mnesia file storage. Each (non
%%% ram-copy) table is maintained in a corresponding .DAT file. The
%%% dat file is organized as a segmented linear hashlist. The head of
%%% the file with the split indicator, size etc is held in ram by the
%%% server at all times.
%%%
%% The method of hashing is the so called linear hashing algorithm
%% with segments.
%%
%% Linear hashing:
%%
%% - n indicates next bucket to split (initially zero);
%% - m is the size of the hash table
%% - initially next = m and n = 0
%%
%% - to insert:
%% - hash = key mod m
%% - if hash < n then hash = key mod 2m
%% - when the number of objects exceeds the initial size
%% of the hash table, each insertion of an object
%% causes bucket n to be split:
%% - add a new bucket to the end of the table
%% - redistribute the contents of bucket n
%% using hash = key mod 2m
%% - increment n
%% - if n = m then m = 2m, n = 0
%% - to search:
%% hash = key mod m
%% if hash < n then hash = key mod 2m
%% do linear scan of the bucket
%%
%%% If a file error occurs on a working dets file, update_mode is set
%%% to the error tuple. When in 'error' mode, the free lists are not
%%% written, and a repair is forced next time the file is opened.
-record(dets_cont, {
what :: 'undefined' | 'bchunk' | 'bindings' | 'object' | 'select',
no_objs :: 'default' | pos_integer(), % requested number of objects
bin :: 'eof' | binary(), % small chunk not consumed,
% or 'eof' at end-of-file
alloc :: binary() % the part of the file not yet scanned
| {From :: non_neg_integer(),
To :: non_neg_integer,
10000 binary()},
tab :: tab_name(),
proc :: 'undefined' | pid(), % the pid of the Dets process
match_program :: 'true'
| 'undefined'
| {'match_spec', ets:compiled_match_spec()}
}).
-record(open_args, {
file :: list(),
type :: type(),
keypos :: keypos(),
repair :: 'force' | boolean(),
min_no_slots :: no_slots(),
max_no_slots :: no_slots(),
ram_file :: boolean(),
delayed_write :: cache_parms(),
auto_save :: auto_save(),
access :: access(),
debug :: boolean()
}).
-define(PATTERN_TO_OBJECT_MATCH_SPEC(Pat), [{Pat,[],['$_']}]).
-define(PATTERN_TO_BINDINGS_MATCH_SPEC(Pat), [{Pat,[],['$$']}]).
-define(PATTERN_TO_TRUE_MATCH_SPEC(Pat), [{Pat,[],[true]}]).
%%-define(DEBUGM(X, Y), io:format(X, Y)).
-define(DEBUGM(X, Y), true).
%%-define(DEBUGF(X,Y), io:format(X, Y)).
-define(DEBUGF(X,Y), void).
%%-define(PROFILE(C), C).
-define(PROFILE(C), void).
-opaque bindings_cont() :: #dets_cont{}.
-opaque cont() :: #dets_cont{}.
-type match_spec() :: ets:match_spec().
-type object() :: tuple().
-opaque object_cont() :: #dets_cont{}.
-type pattern() :: atom() | tuple().
-opaque select_cont() :: #dets_cont{}.
%%% Some further debug code was added in R12B-1 (stdlib-1.15.1):
%%% - there is a new open_file() option 'debug';
%%% - there is a new OS environment variable 'DETS_DEBUG';
%%% - verbose(true) implies that info messages are written onto
%%% the error log whenever an unsafe traversal is started.
%%% The 'debug' mode (set by the open_file() option 'debug' or
%%% by os:putenv("DETS_DEBUG", "true")) implies that the results of
%%% calling pwrite() and pread() are tested to some extent. It also
%%% means a considerable overhead when it comes to RAM usage. The
%%% operation of Dets is also slowed down a bit. Note that in debug
%%% mode terms will be output on the error logger.
%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
add_user(Pid, Tab, Args) ->
req(Pid, {add_user, Tab, Args}).
-spec all() -> [tab_name()].
all() ->
dets_server:all().
-spec bchunk(Name, Continuation) ->
{Continuation2, Data} | '$end_of_table' | {'error', Reason} when
Name :: tab_name(),
Continuation :: 'start' | cont(),
Continuation2 :: cont(),
Data :: binary() | tuple(),
Reason :: term().
bchunk(Tab, start) ->
badarg(treq(Tab, {bchunk_init, Tab}), [Tab, start]);
bchunk(Tab, #dets_cont{what = bchunk, tab = Tab} = State) ->
badarg(treq(Tab, {bchunk, State}), [Tab, State]);
bchunk(Tab, Term) ->
erlang:error(badarg, [Tab, Term]).
-spec close(Name) -> 'ok' | {'error', Reason} when
Name :: tab_name(),
Reason :: term().
close(Tab) ->
case dets_server:close(Tab) of
badarg -> % Should not happen.
{error, not_owner}; % Backwards compatibility...
Reply ->
Reply
end.
-spec delete(Name, Key) -> 'ok' | {'error', Reason} when
Name :: tab_name(),
Key :: term(),
Reason :: term().
delete(Tab, Key) ->
badarg(treq(Tab, {delete_key, [Key]}), [Tab, Key]).
-spec delete_all_objects(Name) -> 'ok' | {'error', Reason} when
Name :: tab_name(),
Reason :: term().
delete_all_objects(Tab) ->
case treq(Tab, delete_all_objects) of
badarg ->
erlang:error(badarg, [Tab]);
fixed ->
match_delete(Tab, '_');
Reply ->
Reply
end.
-spec delete_object(Name, Object) -> 'ok' | {'error', Reason} when
Name :: tab_name(),
Object :: object(),
Reason :: term().
delete_object(Tab, O) ->
badarg(treq(Tab, {delete_object, [O]}), [Tab, O]).
%% Backwards compatibility.
fsck(Fname, _Version) ->
fsck(Fname).
%% Given a filename, fsck it. Debug.
fsck(Fname) ->
catch begin
{ok, Fd, FH} = read_file_header(Fname, read, false),
?DEBUGF("FileHeader: ~p~n", [FH]),
case dets_v9:check_file_header(FH, Fd) of
{error, not_closed} ->
fsck(Fd, make_ref(), Fname, FH, default, default);
{ok, _Head} ->
fsck(Fd, make_ref(), Fname, FH, default, default);
Error ->
Error
end
end.
-spec first(Name) -> Key | '$end_of_table' when
Name :: tab_name(),
Key :: term().
first(Tab) ->
badarg_exit(treq(Tab, first), [Tab]).
-spec foldr(Function, Acc0, Name) -> Acc | {'error', Reason} when
Name :: tab_name(),
Function :: fun((Object :: object(), AccIn) -> AccOut),
Acc0 :: term(),
Acc :: term(),
AccIn :: term(),
AccOut :: term(),
Reason :: term().
foldr(Fun, Acc, Tab) ->
foldl(Fun, Acc, Tab).
-spec foldl(Function, Acc0, Name) -> Acc | {'error', Reason} when
Name :: tab_name(),
Function :: fun((Object :: object(), AccIn) -> AccOut),
Acc0 :: term(),
Acc :: term(),
AccIn :: term(),
AccOut :: term(),
Reason :: term().
foldl(Fun, Acc, Tab) ->
Ref = make_ref(),
badarg(do_traverse(Fun, Acc, Tab, Ref), [Fun, Acc, Tab]).
-spec from_ets(Name, EtsTab) -> 'ok' | {'error', Reason} when
Name :: tab_name(),
EtsTab :: ets:table(),
Reason :: term().
from_ets(DTab, ETab) ->
ets:safe_fixtable(ETab, true),
Spec = ?PATTERN_TO_OBJECT_MATCH_SPEC('_'),
LC = ets:select(ETab, Spec, 100),
InitFun = from_ets_fun(LC, ETab),
Reply = treq(DTab, {initialize, InitFun, term, default}),
ets:safe_fixtable(ETab, false),
case Reply of
{thrown, Thrown} -> throw(Thrown);
Else -> badarg(Else, [DTab, ETab])
end.
from_ets_fun(LC, ETab) ->
fun(close) ->
ok;
(read) when LC =:= '$end_of_table' ->
end_of_input;
(read) ->
{L, C} = LC,
{L, from_ets_fun(ets:select(C), ETab)}
end.
-spec info(Name) -> InfoList | 'undefined' when
Name :: tab_name(),
InfoList :: [InfoTuple],
InfoTuple :: {'file_size', non_neg_integer()}
| {'filename', file:name()}
| {'keypos', keypos()}
| {'size', non_neg_integer()}
| {'type', type()}.
info(Tab) ->
case catch dets_server:get_pid(Tab) of
{'EXIT', _Reason} ->
undefined;
Pid ->
undefined(req(Pid, info))
end.
-spec info(Name, Item) -> Value | 'undefined' when
Name :: tab_name(),
Item :: 'access' | 'auto_save' | 'bchunk_format'
| 'hash' | 'file_size' | 'filename' | 'keypos' | 'memory'
| 'no_keys' | 'no_objects' | 'no_slots' | 'owner' | 'ram_file'
| 'safe_fixed' | 'safe_fixed_monotonic_time' | 'size' | 'type',
Value :: term().
info(Tab, owner) ->
case catch dets_server:get_pid(Tab) of
Pid when is_pid(Pid) ->
Pid;
_ ->
undefined
end;
info(Tab, users) -> % undocumented
case dets_server:users(Tab) of
[] ->
undefined;
Users ->
Users
end;
info(Tab, Tag) ->
case catch dets_server:get_pid(Tab) of
{'EXIT', _Reason} ->
undefined;
Pid ->
undefined(req(Pid, {info, Tag}))
end.
-spec init_table(Name, InitFun) -> ok | {'error', Reason} when
Name :: tab_name(),
InitFun :: fun((Arg) -> Res),
Arg :: read | close,
Res :: end_of_input | {[object()], InitFun} | {Data, InitFun} | term(),
Reason :: term(),
Data :: binary() | tuple().
init_table(Tab, InitFun) ->
init_table(Tab, InitFun, []).
-spec init_table(Name, InitFun, Options) -> ok | {'error', Reason} when
Name :: tab_name(),
InitFun :: fun((Arg) -> Res),
Arg :: read | close,
Res :: end_of_input | {[object()], InitFun} | {Data, InitFun} | term(),
Options :: Option | [Option],
Option :: {min_no_slots,no_slots()} | {format,term | bchunk},
Reason :: term(),
Data :: binary() | tuple().
init_table(Tab, InitFun, Options) when is_function(InitFun) ->
case options(Options, [format, min_no_slots]) of
{badarg,_} ->
erlang:error(badarg, [Tab, InitFun, Options]);
[Format, MinNoSlots] ->
case treq(Tab, {initialize, InitFun, Format, MinNoSlots}) of
{thrown, Thrown} -> throw(Thrown);
Else -> badarg(Else, [Tab, InitFun, Options])
end
end;
init_table(Tab, InitFun, Options) ->
erlang:error(badarg, [Tab, InitFun, Options]).
-spec insert(Name, Objects) -> 'ok' | {'error', Reason} when
Name :: tab_name(),
Objects :: object() | [object()],
Reason :: term().
insert(Tab, Objs) when is_list(Objs) ->
badarg(treq(Tab, {insert, Objs}), [Tab, Objs]);
insert(Tab, Obj) ->
badarg(treq(Tab, {insert, [Obj]}), [Tab, Obj]).
-spec insert_new(Name, Objects) -> boolean() | {'error', Reason} when
Name :: tab_name(),
Objects :: object() | [object()],
Reason :: term().
insert_new(Tab, Objs) when is_list(Objs) ->
badarg(treq(Tab, {insert_new, Objs}), [Tab, Objs]);
insert_new(Tab, Obj) ->
badarg(treq(Tab, {insert_new, [Obj]}), [Tab, Obj]).
internal_close(Pid) ->
req(Pid, close).
internal_open(Pid, Ref, Args) ->
req(Pid, {internal_open, Ref, Args}).
-spec is_compatible_bchunk_format(Name, BchunkFormat) -> boolean() when
Name :: tab_name(),
BchunkFormat :: binary().
is_compatible_bchunk_format(Tab, Term) ->
badarg(treq(Tab, {is_compatible_bchunk_format, Term}), [Tab, Term]).
-spec is_dets_file(Filename) -> boolean() | {'error', Reason} when
Filename :: file:name(),
Reason :: term().
is_dets_file(FileName) ->
case catch read_file_header(FileName, read, false) of
{ok, Fd, FH} ->
_ = file:close(Fd),
FH#fileheader.cookie =:= ?MAGIC;
{error, {tooshort, _}} ->
false;
{error, {not_a_dets_file, _}} ->
false;
Other ->
Other
end.
-spec lookup(Name, Key) -> Objects | {'error', Reason} when
Name :: tab_name(),
Key :: term(),
Objects :: [object()],
Reason :: term().
lookup(Tab, Key) ->
badarg(treq(Tab, {lookup_keys, [Key]}), [Tab, Key]).
%% Not public.
lookup_keys(Tab, Keys) ->
case catch lists:usort(Keys) of
UKeys when is_list(UKeys), UKeys =/= [] ->
badarg(treq(Tab, {lookup_keys, UKeys}), [Tab, Keys]);
_Else ->
erlang:error(badarg, [Tab, Keys])
end.
-spec match(Name, Pattern) -> [Match] | {'error', Reason} when
Name :: tab_name(),
Pattern :: pattern(),
Match :: [term()],
Reason :: term().
match(Tab, Pat) ->
badarg(safe_match(Tab, Pat, bindings), [Tab, Pat]).
-spec match(Name, Pattern, N) ->
{[Match], Continuation} | '$end_of_table' | {'error', Reason} when
Name :: tab_name(),
Pattern :: pattern(),
N :: 'default' | non_neg_integer(),
Continuation :: bindings_cont(),
Match :: [term()],
Reason :: term().
match(Tab, Pat, N) ->
badarg(init_chunk_match(Tab, Pat, bindings, N, no_safe), [Tab, Pat, N]).
-spec match(Continuation) ->
{[Match], Continuation2} | '$end_of_table' | {'error', Reason} when
Continuation :: bindings_cont(),
Continuation2 :: bindings_cont(),
Match :: [term()],
Reason :: term().
match(State) when State#dets_cont.what =:= bindings ->
badarg(chunk_match(State, no_safe), [State]);
match(Term) ->
erlang:error(badarg, [Term]).
-spec match_delete(Name, Pattern) -> 'ok' | {'error', Reason} when
Name :: tab_name(),
Pattern :: pattern(),
Reason :: term().
match_delete(Tab, Pat) ->
badarg(match_delete(Tab, Pat, delete), [Tab, Pat]).
match_delete(Tab, Pat, What) ->
case compile_match_spec(What, Pat) of
{Spec, MP} ->
case catch dets_server:get_pid(Tab) of
{'EXIT', _Reason} ->
badarg;
Proc ->
R = req(Proc, {match_delete_init, MP, Spec}),
do_match_delete(Proc, R, What, 0)
end;
badarg ->
badarg
end.
do_match_delete(_Proc, {done, N1}, select, N) ->
N + N1;
do_match_delete(_Proc, {done, _N1}, _What, _N) ->
ok;
do_match_delete(Proc, {cont, State, N1}, What, N) ->
do_match_delete(Proc, req(Proc, {match_delete, State}), What, N+N1);
do_match_delete(_Proc, Error, _What, _N) ->
Error.
-spec match_object(Name, Pattern) -> Objects | {'error', Reason} when
Name :: tab_name(),
Pattern :: pattern(),
Objects :: [object()],
Reason :: term().
match_object(Tab, Pat) ->
badarg(safe_match(Tab, Pat, object), [Tab, Pat]).
-spec match_object(Name, Pattern, N) ->
{Objects, Continuation} | '$end_of_table' | {'error', Reason} when
Name :: tab_name(),
Pattern :: pattern(),
N :: 'default' | non_neg_integer(),
Continuation :: object_cont(),
Objects :: [object()],
Reason :: term().
match_object(Tab, Pat, N) ->
badarg(init_chunk_match(Tab, Pat, object, N, no_safe), [Tab, Pat, N]).
-spec match_object(Continuation) ->
{Objects, Continuation2} | '$end_of_table' | {'error', Reason} when
Continuation :: object_cont(),
Continuation2 :: object_cont(),
Objects :: [object()],
Reason :: term().
match_object(State) when State#dets_cont.what =:= object ->
badarg(chunk_match(State, no_safe), [State]);
match_object(Term) ->
erlang:error(badarg, [Term]).
-spec member(Name, Key) -> boolean() | {'error', Reason} when
Name :: tab_name(),
Key :: term(),
Reason :: term().
member(Tab, Key) ->
badarg(treq(Tab, {member, Key}), [Tab, Key]).
-spec next(Name, Key1) -> Key2 | '$end_of_table' when
Name :: tab_name(),
Key1 :: term(),
Key2 :: term().
next(Tab, Key) ->
badarg_exit(treq(Tab, {next, Key}), [Tab, Key]).
-spec open_file(Filename) -> {'ok', Reference} | {'error', Reason} when
Filename :: file:name(),
Reference :: reference(),
Reason :: term().
%% Assuming that a file already exists, open it with the
%% parameters as already specified in the file itself.
%% Return a ref leading to the file.
open_file(File0) ->
File = to_list(File0),
case is_list(File) of
true ->
case dets_server:open_file(File) of
badarg -> % Should not happen.
erlang:error(dets_process_died, [File]);
Reply ->
einval(Reply, [File])
end;
false ->
erlang:error(badarg, [File0])
end.
-spec open_file(Name, Args) -> {'ok', Name} | {'error', Reason} when
Name :: tab_name(),
Args :: [OpenArg],
OpenArg :: {'access', access()}
| {'auto_save', auto_save()}
| {'estimated_no_objects', non_neg_integer()}
| {'file', file:name()}
| {'max_no_slots', no_slots()}
| {'min_no_slots', no_slots()}
| {'keypos', keypos()}
| {'ram_file', boolean()}
| {'repair', boolean() | 'force'}
| {'type', type()},
Reason :: term().
open_file(Tab, Args) when is_list(Args) ->
case catch defaults(Tab, Args) of
OpenArgs when is_record(OpenArgs, open_args) ->
case dets_server:open_file(Tab, OpenArgs) of
badarg -> % Should not happen.
erlang:error(dets_process_died, [Tab, Args]);
Reply ->
einval(Reply, [Tab, Args])
end;
_ ->
erlang:error(badarg, [Tab, Args])
end;
open_file(Tab, Arg) ->
open_file(Tab, [Arg]).
-spec pid2name(Pid) -> {'ok', Name} | 'undefined' when
Pid :: pid(),
Name :: tab_name().
pid2name(Pid) ->
dets_server:pid2name(Pid).
remove_user(Pid, From) ->
req(Pid, {close, From}).
-spec repair_continuation(Cont 10BC8 inuation, MatchSpec) -> Continuation2 when
Continuation :: select_cont(),
Continuation2 :: select_cont(),
MatchSpec :: match_spec().
repair_continuation(#dets_cont{match_program = {match_spec, B}}=Cont, MS) ->
case ets:is_compiled_ms(B) of
true ->
Cont;
false ->
Cont#dets_cont{match_program = {match_spec,
ets:match_spec_compile(MS)}}
end;
repair_continuation(#dets_cont{}=Cont, _MS) ->
Cont;
repair_continuation(T, MS) ->
erlang:error(badarg, [T, MS]).
-spec safe_fixtable(Name, Fix) -> 'ok' when
Name :: tab_name(),
Fix :: boolean().
safe_fixtable(Tab, Bool) when Bool; not Bool ->
badarg(treq(Tab, {safe_fixtable, Bool}), [Tab, Bool]);
safe_fixtable(Tab, Term) ->
erlang:error(badarg, [Tab, Term]).
-spec select(Name, MatchSpec) -> Selection | {'error', Reason} when
Name :: tab_name(),
MatchSpec :: match_spec(),
Selection :: [term()],
Reason :: term().
select(Tab, Pat) ->
badarg(safe_match(Tab, Pat, select), [Tab, Pat]).
-spec select(Name, MatchSpec, N) ->
{Selection, Continuation} | '$end_of_table' | {'error', Reason} when
Name :: tab_name(),
MatchSpec :: match_spec(),
N :: 'default' | non_neg_integer(),
Continuation :: select_cont(),
Selection :: [term()],
Reason :: term().
select(Tab, Pat, N) ->
badarg(init_chunk_match(Tab, Pat, select, N, no_safe), [Tab, Pat, N]).
-spec select(Continuation) ->
{Selection, Continuation2} | '$end_of_table' | {'error', Reason} when
Continuation :: select_cont(),
Continuation2 :: select_cont(),
Selection :: [term()],
Reason :: term().
select(State) when State#dets_cont.what =:= select ->
badarg(chunk_match(State, no_safe), [State]);
select(Term) ->
erlang:error(badarg, [Term]).
-spec select_delete(Name, MatchSpec) -> N | {'error', Reason} when
Name :: tab_name(),
MatchSpec :: match_spec(),
N :: non_neg_integer(),
Reason :: term().
select_delete(Tab, Pat) ->
badarg(match_delete(Tab, Pat, select), [Tab, Pat]).
-spec slot(Name, I) -> '$end_of_table' | Objects | {'error', Reason} when
Name :: tab_name(),
I :: non_neg_integer(),
Objects :: [object()],
Reason :: term().
slot(Tab, Slot) when is_integer(Slot), Slot >= 0 ->
badarg(treq(Tab, {slot, Slot}), [Tab, Slot]);
slot(Tab, Term) ->
erlang:error(badarg, [Tab, Term]).
start() ->
dets_server:start().
stop() ->
dets_server:stop().
istart_link(Server) ->
{ok, proc_lib:spawn_link(dets, init, [self(), Server])}.
-spec sync(Name) -> 'ok' | {'error', Reason} when
Name :: tab_name(),
Reason :: term().
sync(Tab) ->
badarg(treq(Tab, sync), [Tab]).
-spec table(Name) -> QueryHandle when
Name :: tab_name(),
QueryHandle :: qlc:query_handle().
table(Tab) ->
table(Tab, []).
-spec table(Name, Options) -> QueryHandle when
Name :: tab_name(),
Options :: Option | [Option],
Option :: {'n_objects', Limit}
| {'traverse', TraverseMethod},
Limit :: 'default' | pos_integer(),
TraverseMethod :: 'first_next' | 'select' | {'select', match_spec()},
QueryHandle :: qlc:query_handle().
table(Tab, Opts) ->
case options(Opts, [traverse, n_objects]) of
{badarg,_} ->
erlang:error(badarg, [Tab, Opts]);
[Traverse, NObjs] ->
TF = case Traverse of
first_next ->
fun() -> qlc_next(Tab, first(Tab)) end;
select ->
fun(MS) -> qlc_select(select(Tab, MS, NObjs)) end;
{select, MS} ->
fun() -> qlc_select(select(Tab, MS, NObjs)) end
end,
PreFun = fun(_) -> safe_fixtable(Tab, true) end,
PostFun = fun() -> safe_fixtable(Tab, false) end,
InfoFun = fun(Tag) -> table_info(Tab, Tag) end,
%% lookup_keys is not public, but convenient
LookupFun =
case Traverse of
{select, _MS} ->
undefined;
_ ->
fun(_KeyPos, [K]) -> lookup(Tab, K);
(_KeyPos, Ks) -> lookup_keys(Tab, Ks)
end
end,
FormatFun =
fun({all, _NElements, _ElementFun}) ->
As = [Tab | [Opts || _ <- [[]], Opts =/= []]],
{?MODULE, table, As};
({match_spec, MS}) ->
{?MODULE, table, [Tab, [{traverse, {select, MS}} |
listify(Opts)]]};
({lookup, _KeyPos, [Value], _NElements, ElementFun}) ->
io_lib:format("~w:lookup(~w, ~w)",
[?MODULE, Tab, ElementFun(Value)]);
({lookup, _KeyPos, Values, _NElements, ElementFun}) ->
Vals = [ElementFun(V) || V <- Values],
io_lib:format("lists:flatmap(fun(V) -> "
"~w:lookup(~w, V) end, ~w)",
[?MODULE, Tab, Vals])
end,
qlc:table(TF, [{pre_fun, PreFun}, {post_fun, PostFun},
{info_fun, InfoFun}, {format_fun, FormatFun},
{key_equality, '=:='},
{lookup_fun, LookupFun}])
end.
qlc_next(_Tab, '$end_of_table') ->
[];
qlc_next(Tab, Key) ->
case lookup(Tab, Key) of
Objects when is_list(Objects) ->
Objects ++ fun() -> qlc_next(Tab, next(Tab, Key)) end;
Error ->
%% Do what first and next do.
exit(Error)
end.
qlc_select('$end_of_table') ->
[];
qlc_select({Objects, Cont}) when is_list(Objects) ->
Objects ++ fun() -> qlc_select(select(Cont)) end;
qlc_select(Error) ->
Error.
table_info(Tab, num_of_objects) ->
info(Tab, size);
table_info(Tab, keypos) ->
info(Tab, keypos);
table_info(Tab, is_unique_objects) ->
info(Tab, type) =/= duplicate_bag;
table_info(_Tab, _) ->
undefined.
%% End of table/2.
-spec to_ets(Name, EtsTab) -> EtsTab | {'error', Reason} when
Name :: tab_name(),
EtsTab :: ets:table(),
Reason :: term().
to_ets(DTab, ETab) ->
case ets:info(ETab, protection) of
undefined ->
erlang:error(badarg, [DTab, ETab]);
_ ->
Fun = fun(X, T) -> true = ets:insert(T, X), T end,
foldl(Fun, ETab, DTab)
end.
-spec traverse(Name, Fun) -> Return | {'error', Reason} when
Name :: tab_name(),
Fun :: fun((Object) -> FunReturn),
Object :: object(),
FunReturn :: 'continue'
| {'continue', Val}
| {'done', Value}
| OtherValue,
Return :: [term()] | OtherValue,
Val :: term(),
Value :: term(),
OtherValue :: term(),
Reason :: term().
traverse(Tab, Fun) ->
Ref = make_ref(),
TFun =
fun(O, Acc) ->
case Fun(O) of
continue ->
Acc;
{continue, Val} ->
[Val | Acc];
{done, Value} ->
throw({Ref, [Value | Acc]});
Other ->
throw({Ref, Other})
end
end,
badarg(do_traverse(TFun, [], Tab, Ref), [Tab, Fun]).
-spec update_counter(Name, Key, Increment) -> Result when
Name :: tab_name(),
Key :: term(),
Increment :: {Pos, Incr} | Incr,
Pos :: integer(),
Incr :: integer(),
Result :: integer().
update_counter(Tab, Key, C) ->
badarg(treq(Tab, {update_counter, Key, C}), [Tab, Key, C]).
verbose() ->
verbose(true).
verbose(What) ->
ok = dets_server:verbose(What),
All = dets_server:all(),
Fun = fun(Tab) -> treq(Tab, {set_verbose, What}) end,
lists:foreach(Fun, All),
All.
%% Where in the (open) table is Object located?
%% The address of the first matching object is returned.
%% Format 9 returns the address of the object collection.
%% -> {ok, Address} | false
where(Tab, Object) ->
badarg(treq(Tab, {where, Object}), [Tab, Object]).
do_traverse(Fun, Acc, Tab, Ref) ->
case catch dets_server:get_pid(Tab) of
{'EXIT', _Reason} ->
badarg;
Proc ->
try
do_trav(Proc, Acc, Fun)
catch {Ref, Result} ->
Result
end
end.
do_trav(Proc, Acc, Fun) ->
{Spec, MP} = compile_match_spec(object, '_'),
%% MP not used
case req(Proc, {match, MP, Spec, default, safe}) of
{cont, State} ->
do_trav(State, Proc, Acc, Fun);
Error ->
Error
end.
do_trav(State, Proc, Acc, Fun) ->
case req(Proc, {match_init, State, safe}) of
'$end_of_table'->
Acc;
{cont, {Bins, NewState}} ->
do_trav_bins(NewState, Proc, Acc, Fun, lists:reverse(Bins));
Error ->
Error
end.
do_trav_bins(State, Proc, Acc, Fun, []) ->
do_trav(State, Proc, Acc, Fun);
do_trav_bins(State, Proc, Acc, Fun, [Bin | Bins]) ->
%% Unpack one binary at a time, using the client's heap.
case catch binary_to_term(Bin) of
{'EXIT', _} ->
req(Proc, {corrupt, dets_utils:bad_object(do_trav_bins, Bin)});
Term ->
NewAcc = Fun(Term, Acc),
do_trav_bins(State, Proc, NewAcc, Fun, Bins)
end.
safe_match(Tab, Pat, What) ->
do_safe_match(init_chunk_match(Tab, Pat, What, default, safe), []).
do_safe_match({error, Error}, _L) ->
{error, Error};
do_safe_match({L, C}, LL) ->
do_safe_match(chunk_match(C, safe), L++LL);
do_safe_match('$end_of_table', L) ->
L;
do_safe_match(badarg, _L) ->
badarg.
%% What = object | bindings | select
init_chunk_match(Tab, Pat, What, N, Safe) when is_integer(N), N >= 0;
N =:= default ->
case compile_match_spec(What, Pat) of
{Spec, MP} ->
case catch dets_server:get_pid(Tab) of
{'EXIT', _Reason} ->
badarg;
Proc ->
case req(Proc, {match, MP, Spec, N, Safe}) of
0