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, get/2, insert/3, delete/3, delete_key/2, delete_value/2, drop_table/1]).
-export_type([bag_table/2]).
-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(DTZ, DUA) :: any() | {gleam_phantom, DTZ, DUA}.
-file("src/mala.gleam", 17).
?DOC(
" Create a new bag. The protection mode is `public`, so it can be accessed\n"
" from any process.\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"
).
-spec new() -> bag_table(any(), any()).
new() ->
mala_ffi:bag_new().
-file("src/mala.gleam", 25).
?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(DUF, DUG), DUF) -> {ok, list(DUG)} | {error, nil}.
get(Table, Key) ->
mala_ffi:bag_get(Table, Key).
-file("src/mala.gleam", 34).
?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(DUM, DUN), DUM, DUN) -> {ok, nil} | {error, nil}.
insert(Table, Key, Value) ->
mala_ffi:bag_insert(Table, Key, Value).
-file("src/mala.gleam", 42).
?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(DUS, DUT), DUS, DUT) -> {ok, nil} | {error, nil}.
delete(Table, Key, Value) ->
mala_ffi:bag_delete(Table, Key, Value).
-file("src/mala.gleam", 50).
?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(DUY, any()), DUY) -> {ok, nil} | {error, nil}.
delete_key(Table, Key) ->
mala_ffi:bag_delete_key(Table, Key).
-file("src/mala.gleam", 62).
?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(), DVF), DVF) -> {ok, integer()} | {error, nil}.
delete_value(Table, Value) ->
mala_ffi:bag_delete_value(Table, Value).
-file("src/mala.gleam", 69).
?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).