Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 489 lines (401 sloc) 15.194 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
%%% The MIT License
%%%
%%% Copyright (C) 2011-2014 by Joseph Wayne Norton <norton@alum.mit.edu>
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.

-module(gen_ets).

-include("gen_ets.hrl").

%% External exports
-export([all/0
         , tid/1
         , new/2
         , destroy/2
         , repair/2
         , delete/1
         , delete/2
         , delete_all_objects/1
         , first/1
         , foldl/3
         , foldr/3
         , info/1
         , info/2
         , insert/2
         , insert_new/2
         , last/1
         , lookup/2
         , lookup_element/3
         , match/2
         , match/3
         , match/1
         , match_delete/2
         , match_object/2
         , match_object/3
         , match_object/1
         , member/2
         , next/2
         , prev/2
         , select/2
         , select/3
         , select/1
         , select_count/2
         , select_delete/2
         , select_reverse/2
         , select_reverse/3
         , select_reverse/1
         , tab2list/1
        ]).

%% DEBUG -compile(export_all).

-export_type([gen_tab/0, gen_tid/0, name/0, key/0, object/0, match_pattern/0, match_spec/0, cont/0]).

%%%----------------------------------------------------------------------
%%% Types/Specs/Records
%%%----------------------------------------------------------------------

-type gen_tid() :: gen_ets_ns:gen_tid().
-type gen_tab() :: gen_ets_ns:gen_tab().

-type opts() :: [opt() | {impl, {module(), impl_opts()}}].
-type opt() :: set | ordered_set | named_table | {keypos,pos_integer()} | public | protected | private | compressed | async.
-type impl_opts() :: [impl_opt()].
-type impl_opt() :: term().

-type key() :: gen_ets_ns:key().
-type object() :: gen_ets_ns:object().

-type name() :: gen_ets_ns:name().
-type item() :: owner | name | named_table | type | keypos | protection | compressed | async | memory | size.
-type pos() :: pos_integer().
-type match_pattern() :: gen_ets:match_pattern().
-type match_spec() :: gen_ets:match_spec().
-type match() :: term().
-type limit() :: pos_integer().
-opaque cont() :: {cont, gen_tid(), term()}.

-define(NS, gen_ets_reg).

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------

%% @doc Returns a list of all tables at the node.
%% @end
%% @see ets:all/0

-spec all() -> [gen_tab()].
all() ->
    gen_ets_ns:all(?NS).

%% @doc Returns a table\'s identifier.
%% @end

-spec tid(gen_tab()) -> gen_tid().
tid(Tab) ->
    gen_ets_ns:tid(?NS, Tab).

%% @doc Creates a new table and returns a table identifier which can
%% be used in subsequent operations. The table identifier can be sent
%% to other processes so that a table can be shared between different
%% processes within a node.
%%
%% Valid GEN_ETS properties for +Options+ are:
%%
%% - +set+ The table is a set table - one key, one object, no order
%% among objects. This is the default table type.
%%
%% - +ordered_set+ The table is an ordered_set table - one key, one
%% object, ordered in Erlang term order, which is the order implied
%% by the +<+ and +>+ operators.
%%
%% - +named_table+ If this option is present, the name +Name+ is
%% associated with the table identifier.
%%
%% - +{keypos,pos_integer()}+ Specfies which element in the stored
%% tuples should be used as key. By default, it is the first
%% element, i.e. +Pos=1+.
%%
%% - +public+ Any process may read or write to the table.
%%
%% - +protected+ The owner process can read and write to the table.
%% Other processes can only read the table. This is the default
%% setting for the access rights.
%%
%% - +private+ Only the owner process can read or write to the table.
%%
%% - +compressed+ If this option is present, the table data will be
%% stored in a compressed format.
%%
%% - +async+ If this option is present and supported by the
%% implementation, the emulator\'s async thread pool will be used
%% when accessing the table data.
%%
%% - +{impl, module(), [impl_option()]}+ The module that implements
%% GEN_ETS callback functions. Implementation specific options can
%% be given. The default is +{impl, gen_ets_impl_ets, []}+.
%%
%% @end
%% @see ets:new/2

-spec new(name(), opts()) -> gen_tab().
new(Name, Opts) ->
    create(new, Name, Opts).

%% @doc Destroy the contents of the specified table.
%% @end

-spec destroy(name(), opts()) -> true.
destroy(Name, Opts) ->
    create(destroy, Name, Opts).

%% @doc If a table cannot be opened, you may attempt to call this
%% method to resurrect as much of the contents of the table as
%% possible. Some data may be lost, so be careful when calling this
%% function on a table that contains important information.
%% @end

-spec repair(name(), opts()) -> true.
repair(Name, Opts) ->
    create(repair, Name, Opts).

%% @doc Deletes the entire table +Tab+.
%% @end
%% @see ets:delete/1

-spec delete(gen_tab()) -> true.
delete(Tab) ->
    gen_ets_ns:delete(?NS, Tab).

%% @doc Deletes all objects with the key +Key+ from the table +Tab+.
%% @end
%% @see ets:delete/2

-spec delete(gen_tab(), key()) -> true.
delete(Tab, Key) ->
    gen_ets_ns:delete(?NS, Tab, Key).

%% @doc Delete all objects in the table +Tab+. The operation is
%% guaranteed to be atomic and isolated. This function only applies
%% to the +ets+ implementation.
%% @end
%% @see ets:delete_all_objects/1

-spec delete_all_objects(gen_tab()) -> true.
delete_all_objects(Tab) ->
    gen_ets_ns:delete_all_objects(?NS, Tab).

%% @doc Returns the first key +Key+ in the table +Tab+. If the table
%% is empty, +'$end_of_table'+ will be returned.
%% @end
%% @see ets:first/1

-spec first(gen_tab()) -> key() | '$end_of_table'.
first(Tab) ->
    gen_ets_ns:first(?NS, Tab).

%% @doc Fold from left to right over the elements of the table.
%% @end
%% @see ets:foldl/3

-spec foldl(Fun, Acc0::term(), gen_tab()) -> Acc1::term() when
      Fun :: fun((Element::term(), AccIn::term()) -> AccOut::term()).
foldl(Function, Acc0, Tab) ->
    gen_ets_ns:foldl(?NS, Function, Acc0, Tab).

%% @doc Fold from right to left over the elements of the table.
%% @end
%% @see ets:foldr/3

-spec foldr(Fun, Acc0::term(), gen_tab()) -> Acc1::term() when
      Fun :: fun((Element::term(), AccIn::term()) -> AccOut::term()).
foldr(Function, Acc0, Tab) ->
    gen_ets_ns:foldr(?NS, Function, Acc0, Tab).

%% @doc Returns information about the table +Tab+ as a list of +{Item,
%% Value}+ tuples.
%%
%% @end
%% @see info/2

-spec info(gen_tab()) -> [{item(), term()}].
info(Tab) ->
    gen_ets_ns:info(?NS, Tab).

%% @doc Returns the information associated with +Item+ for the table +Tab+.
%%
%% Valid +Item+ options are:
%%
%% - +owner+
%% - +name+
%% - +named_table+ _only the ets implementation_
%% - +type+
%% - +keypos+
%% - +protection+
%% - +compressed+
%% - +async+ _only the drv implementation_
%% - +memory+ _only the ets implementation_
%% - +size+ _only the ets implementation_
%%
%% @end
%% @see ets:info/2

-spec info(gen_tab(), item()) -> term().
info(Tab, Item) ->
    gen_ets_ns:info(?NS, Tab, Item).

%% @doc Inserts the object or all of the objects in the list
%% +ObjOrObjs+ into the table +Tab+.
%% @end
%% @see ets:insert/2

-spec insert(gen_tab(), object() | [object()]) -> true.
insert(Tab, ObjOrObjs) ->
    gen_ets_ns:insert(?NS, Tab, ObjOrObjs).

%% @doc This function works exactly like +insert/2+, with the
%% exception that instead of overwriting objects with the same key, it
%% simply returns false. This function only applies to the +ets+
%% implementation.
%% @end
%% @see ets:insert_new/2

-spec insert_new(gen_tab(), object() | [object()]) -> true.
insert_new(Tab, ObjOrObjs) ->
    gen_ets_ns:insert_new(?NS, Tab, ObjOrObjs).

%% @doc Returns the last key +Key+ in the table +Tab+. If the table
%% is empty, +'$end_of_table'+ will be returned.
%% @end
%% @see ets:last/1

-spec last(gen_tab()) -> key() | '$end_of_table'.
last(Tab) ->
    gen_ets_ns:last(?NS, Tab).

%% @doc Returns a list of all objects with the key +Key+ in the table
%% +Tab+.
%% @end
%% @see ets:lookup/2

-spec lookup(gen_tab(), key()) -> [object()].
lookup(Tab, Key) ->
    gen_ets_ns:lookup(?NS, Tab, Key).

%% @doc Returns the +Pos+:th element of the object with the key +Key+
%% in the table +Tab+.
%% @end
%% @see ets:lookup_element/3

-spec lookup_element(gen_tab(), key(), pos()) -> term().
lookup_element(Tab, Key, Pos) ->
    gen_ets_ns:lookup_element(?NS, Tab, Key, Pos).

%% @doc Matches the objects in the table +Tab+ against the pattern
%% +Pattern+.
%% @end
%% @see ets:match/2

-spec match(gen_tab(), match_pattern()) -> [match()].
match(Tab, Pattern) ->
    gen_ets_ns:match(?NS, Tab, Pattern).

%% @doc Matches the objects in the table +Tab+ against the pattern
%% +Pattern+ and returns a limited (+Limit+) number of matching
%% objects.
%% @end
%% @see ets:match/3

-spec match(gen_tab(), match_pattern(), limit()) -> {[match()], cont() | '$end_of_table'} | '$end_of_table'.
match(Tab, Pattern, Limit) ->
    gen_ets_ns:match(?NS, Tab, Pattern, Limit).

%% @doc Continues a match started with +match/3+.
%% @end
%% @see ets:match/1

-spec match(cont() | '$end_of_table') -> {[match()], cont() | '$end_of_table'} | '$end_of_table'.
match(Cont) ->
    gen_ets_ns:match(?NS, Cont).

%% @doc Deletes all objects which match the pattern +Pattern+ from the
%% table +Tab+.
%% @end
%% @see ets:match_delete/2

-spec match_delete(gen_tab(), match_pattern()) -> true.
match_delete(Tab, Pattern) ->
    gen_ets_ns:match_delete(?NS, Tab, Pattern).

%% @doc Matches the objects in the table +Tab+ against the pattern
%% +Pattern+.
%% @end
%% @see ets:match_object/2

-spec match_object(gen_tab(), match_pattern()) -> [match()].
match_object(Tab, Pattern) ->
    gen_ets_ns:match_object(?NS, Tab, Pattern).

%% @doc Matches the objects in the table +Tab+ against the pattern
%% +Pattern+ and returns a limited (+Limit+) number of matching
%% objects.
%% @end
%% @see ets:match_object/3

-spec match_object(gen_tab(), match_pattern(), limit()) -> {[match()], cont() | '$end_of_table'} | '$end_of_table'.
match_object(Tab, Pattern, Limit) ->
    gen_ets_ns:match_object(?NS, Tab, Pattern, Limit).

%% @doc Continues a match started with +match_object/3+.
%% @end
%% @see ets:match_object/1

-spec match_object(cont() | '$end_of_table') -> {[match()], cont() | '$end_of_table'} | '$end_of_table'.
match_object(Cont) ->
    gen_ets_ns:match_object(?NS, Cont).

%% @doc Returns +true+ if one or more elements in the table +Tab+ has
%% the key +Key+, +false+ otherwise.
%% @end
%% @see ets:member/2

-spec member(gen_tab(), key()) -> true | false.
member(Tab, Key) ->
    gen_ets_ns:member(?NS, Tab, Key).

%% @doc Returns the next key +Key2+, following the key +Key1+ in the
%% table +Tab+. If there is no next key, +'$end_of_table'+ is
%% returned.
%% @end
%% @see ets:next/2

-spec next(gen_tab(), key()) -> key() | '$end_of_table'.
next(Tab, Key) ->
    gen_ets_ns:next(?NS, Tab, Key).

%% @doc Returns the previous key +Key2+, following the key +Key1+ in
%% the table +Tab+. If there is no previous key, +'$end_of_table'+ is
%% returned.
%% @end
%% @see ets:prev/2

-spec prev(gen_tab(), key()) -> key() | '$end_of_table'.
prev(Tab, Key) ->
    gen_ets_ns:prev(?NS, Tab, Key).

%% @doc Matches the objects in the table +Tab+ against the spec
%% +Spec+.
%% @end
%% @see ets:select/2

-spec select(gen_tab(), match_spec()) -> [match()].
select(Tab, Spec) ->
    gen_ets_ns:select(?NS, Tab, Spec).

%% @doc Matches the objects in the table +Tab+ against the spec +Spec+
%% and returns a limited (+Limit+) number of matching objects.
%% @end
%% @see ets:select/3

-spec select(gen_tab(), match_spec(), limit()) -> {[match()], cont() | '$end_of_table'} | '$end_of_table'.
select(Tab, Spec, Limit) ->
    gen_ets_ns:select(?NS, Tab, Spec, Limit).

%% @doc Continues a select started with +select/3+.
%% @end
%% @see ets:select/1

-spec select(cont() | '$end_of_table') -> {[match()], cont() | '$end_of_table'} | '$end_of_table'.
select(Cont) ->
    gen_ets_ns:select(?NS, Cont).

%% @doc Counts all objects which match the spec +Spec+ from the
%% table +Tab+ and returns the number matched.
%% @end
%% @see ets:select_count/2

-spec select_count(gen_tab(), match_spec()) -> pos_integer().
select_count(Tab, Spec) ->
    gen_ets_ns:select_count(?NS, Tab, Spec).

%% @doc Deletes all objects which match the spec +Spec+ from the
%% table +Tab+ and returns the number deleted.
%% @end
%% @see ets:select_delete/2

-spec select_delete(gen_tab(), match_spec()) -> pos_integer().
select_delete(Tab, Spec) ->
    gen_ets_ns:select_delete(?NS, Tab, Spec).

%% @doc Matches in reverse the objects in the table +Tab+ against the
%% spec +Spec+.
%% @end
%% @see ets:select_reverse/2

-spec select_reverse(gen_tab(), match_spec()) -> [match()].
select_reverse(Tab, Spec) ->
    gen_ets_ns:select_reverse(?NS, Tab, Spec).

%% @doc Matches in reverse the objects in the table +Tab+ against the
%% spec +Spec+ and returns a limited (+Limit+) number of matching
%% objects.
%% @end
%% @see ets:select_reverse/3

-spec select_reverse(gen_tab(), match_spec(), limit()) -> {[match()], cont() | '$end_of_table'} | '$end_of_table'.
select_reverse(Tab, Spec, Limit) ->
    gen_ets_ns:select_reverse(?NS, Tab, Spec, Limit).

%% @doc Continues a select reverse started with +select_reverse/3+.
%% @end
%% @see ets:select_reverse/1

-spec select_reverse(cont() | '$end_of_table') -> {[match()], cont() | '$end_of_table'} | '$end_of_table'.
select_reverse(Cont) ->
    gen_ets_ns:select_reverse(?NS, Cont).

%% @doc Returns a list of all objects in the table +Tab+. The
%% operation is *not* guaranteed to be atomic and isolated.
%% @end
%% @see ets:tab2list/1

-spec tab2list(gen_tab()) -> [object()].
tab2list(Tab) ->
    gen_ets_ns:tab2list(?NS, Tab).

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------

create(Op, Name, Opts) ->
    gen_ets_ns:Op(?NS, Name, Opts).
Something went wrong with that request. Please try again.