Packages

ETS bags, an in-memory table where one key can have multiple values

Current section

Files

Jump to
mala src mala.erl
Raw

src/mala.erl

-module(mala).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/mala.gleam").
-export([new/0, new_protected/0, new_private/0, get/2, has_key/2, insert/3, delete/3, delete_key/2, delete_value/2, drop_table/1]).
-export_type([bag_table/2, non_owner_access/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type bag_table(DUS, DUT) :: any() | {gleam_phantom, DUS, DUT}.
-type non_owner_access() :: public | protected | private.
-file("src/mala.gleam", 20).
?DOC(
" Create a new bag.\n"
"\n"
" The process that calls this function is the owner of the ETS table, and the\n"
" table will be dropped when the owner process terminates.\n"
"\n"
" The access control is set to allow all processes to read and write to the\n"
" table.\n"
"\n"
" See `new_protected` and `new_private` for other access control options.\n"
).
-spec new() -> bag_table(any(), any()).
new() ->
mala_ffi:bag_new(public).
-file("src/mala.gleam", 34).
?DOC(
" Create a new bag.\n"
"\n"
" The process that calls this function is the owner of the ETS table, and the\n"
" table will be dropped when the owner process terminates.\n"
"\n"
" The access control is set so only the process that created the table can\n"
" write to it. All other processes can read from it.\n"
"\n"
" See `new` and `new_private` for other access control options.\n"
).
-spec new_protected() -> bag_table(any(), any()).
new_protected() ->
mala_ffi:bag_new(protected).
-file("src/mala.gleam", 48).
?DOC(
" Create a new bag.\n"
"\n"
" The process that calls this function is the owner of the ETS table, and the\n"
" table will be dropped when the owner process terminates.\n"
"\n"
" The access control is set so only the process that created the table can\n"
" read from and write to it. All other processes cannot access the table.\n"
"\n"
" See `new` and `new_protected` for other access control options.\n"
).
-spec new_private() -> bag_table(any(), any()).
new_private() ->
mala_ffi:bag_new(private).
-file("src/mala.gleam", 70).
?DOC(
" Key all values for a given key in the table.\n"
"\n"
" This function will return an error if the bag has been dropped, either explicitly\n"
" with `drop_table` or implicitly by the owner process terminating.\n"
).
-spec get(bag_table(DVK, DVL), DVK) -> {ok, list(DVL)} | {error, nil}.
get(Table, Key) ->
mala_ffi:bag_get(Table, Key).
-file("src/mala.gleam", 78).
?DOC(
" Check if the table contains at least one value for the given key.\n"
"\n"
" This function will return an error if the bag has been dropped, either explicitly\n"
" with `drop_table` or implicitly by the owner process terminating.\n"
).
-spec has_key(bag_table(DVR, any()), DVR) -> {ok, boolean()} | {error, nil}.
has_key(Bag, Key) ->
mala_ffi:bag_has_key(Bag, Key).
-file("src/mala.gleam", 87).
?DOC(
" Insert a value for the given key in the table. If the key already has this\n"
" value then there will be no change.\n"
"\n"
" This function will return an error if the bag has been dropped, either explicitly\n"
" with `drop_table` or implicitly by the owner process terminating.\n"
).
-spec insert(bag_table(DVX, DVY), DVX, DVY) -> {ok, nil} | {error, nil}.
insert(Table, Key, Value) ->
mala_ffi:bag_insert(Table, Key, Value).
-file("src/mala.gleam", 95).
?DOC(
" Delete a key-value pair from the table.\n"
"\n"
" This function will return an error if the bag has been dropped, either explicitly\n"
" with `drop_table` or implicitly by the owner process terminating.\n"
).
-spec delete(bag_table(DWD, DWE), DWD, DWE) -> {ok, nil} | {error, nil}.
delete(Table, Key, Value) ->
mala_ffi:bag_delete(Table, Key, Value).
-file("src/mala.gleam", 103).
?DOC(
" Delete all values for the given key in the table.\n"
"\n"
" This function will return an error if the bag has been dropped, either explicitly\n"
" with `drop_table` or implicitly by the owner process terminating.\n"
).
-spec delete_key(bag_table(DWJ, any()), DWJ) -> {ok, nil} | {error, nil}.
delete_key(Table, Key) ->
mala_ffi:bag_delete_key(Table, Key).
-file("src/mala.gleam", 115).
?DOC(
" Delete all instances of a given value from the table, no matter what key\n"
" they are stored under.\n"
"\n"
" This performs a full table scan so is slower than the other operations in\n"
" this module.\n"
"\n"
" This function will return an error if the bag has been dropped, either explicitly\n"
" with `drop_table` or implicitly by the owner process terminating.\n"
).
-spec delete_value(bag_table(any(), DWQ), DWQ) -> {ok, integer()} | {error, nil}.
delete_value(Table, Value) ->
mala_ffi:bag_delete_value(Table, Value).
-file("src/mala.gleam", 122).
?DOC(
" Drop a table, freeing the memory it used.\n"
"\n"
" Any attempt to use a table after it was dropped will result in an error.\n"
).
-spec drop_table(bag_table(any(), any())) -> nil.
drop_table(Table) ->
mala_ffi:drop(Table).