forked from erlang/otp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdets.erl
More file actions
3302 lines (3016 loc) · 103 KB
/
Copy pathdets.erl
File metadata and controls
3302 lines (3016 loc) · 103 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights 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 parts specific for formats up to and including 8(c) are
%%% implemented in dets_v8.erl, parts specific for format 9 are
%%% implemented in dets_v9.erl.
%% 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, % object | bindings | select | bchunk
no_objs, % requested number of objects: default | integer() > 0
bin, % small chunk not consumed, or 'eof' at end-of-file
alloc, % the part of the file not yet scanned, mostly a binary
tab,
proc, % the pid of the Dets process
match_program % true | compiled_match_spec() | undefined
}).
-record(open_args, {
file,
type,
keypos,
repair,
min_no_slots,
max_no_slots,
ram_file,
delayed_write,
auto_save,
access,
version,
debug
}).
-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).
-type access() :: 'read' | 'read_write'.
-type auto_save() :: 'infinity' | non_neg_integer().
-opaque bindings_cont() :: #dets_cont{}.
-opaque cont() :: #dets_cont{}.
-type keypos() :: pos_integer().
-type match_spec() :: ets:match_spec().
-type object() :: tuple().
-type no_slots() :: non_neg_integer() | 'default'.
-opaque object_cont() :: #dets_cont{}.
-type pattern() :: atom() | tuple().
-opaque select_cont() :: #dets_cont{}.
-type tab_name() :: term().
-type type() :: 'bag' | 'duplicate_bag' | 'set'.
-type version() :: 8 | 9 | 'default'.
%%% 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]).
%% Given a filename, fsck it. Debug.
fsck(Fname) ->
fsck(Fname, default).
fsck(Fname, Version) ->
catch begin
{ok, Fd, FH} = read_file_header(Fname, read, false),
?DEBUGF("FileHeader: ~p~n", [FH]),
case (FH#fileheader.mod):check_file_header(FH, Fd) of
{error, not_closed} ->
fsck(Fd, make_ref(), Fname, FH, default, default, Version);
{ok, _Head, _Extra} ->
fsck(Fd, make_ref(), Fname, FH, default, default, Version);
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:tab(),
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' | 'size' | 'type' | 'version',
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() when
Name :: tab_name(),
Objects :: object() | [object()].
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(File) ->
case dets_server:open_file(to_list(File)) of
badarg -> % Should not happen.
erlang:error(dets_process_died, [File]);
Reply ->
einval(Reply, [File])
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()}
| {'version', version()},
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(Continuation, MatchSpec) -> Continuation2 when
Continuation :: select_cont(),
Continuation2 :: select_cont(),
MatchSpec :: match_spec().
repair_continuation(#dets_cont{match_program = B}=Cont, MS)
when is_binary(B) ->
case ets:is_compiled_ms(B) of
true ->
Cont;
false ->
Cont#dets_cont{match_program = 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:tab(),
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(#dets_cont{bin = eof}, _Proc, Acc, _Fun) ->
Acc;
do_trav(State, Proc, Acc, Fun) ->
case req(Proc, {match_init, State, safe}) of
{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
{done, L} ->
{L, #dets_cont{tab = Tab, proc = Proc,
what = What, bin = eof}};