Karta. Messages
(Karta v1.0.0)
Stored messages: metadata rows (Karta.Messages.Message) and their
encrypted bodies (Karta.Storage).
The storage protocol keeps an acknowledged message from ever being lost and a blob from ever being left without a row:
create_pending/1inserts the row as:pending(committed);- the body is written to the blob store;
mark_accepted/1moves the row from:pendingto:acceptedwith a conditional update; only then is the message acknowledged.
discard/3 removes messages: it moves rows to :discarding with a
conditional update, deletes their blobs, and deletes the rows whose blob
is gone. A failed blob delete leaves the row :discarding for the next
sweep. Because both transitions out of :pending are conditional, a
message is either acknowledged with its blob intact, or discarded and
never acknowledged.
Reads are scoped by server: a message is found only through its own server.
Accepting a message announces it on its server's topic
(:message_accepted), and removing accepted messages announces them on
their servers' and their own topics (:messages_removed), see
Karta.Changes.
Summary
Functions
How many of the server's accepted messages that match search were
created after since.
Inserts a :pending message row.
How many messages the server accepted on each UTC day from the day
days - 1 days before now's through it: [%{date: Date, incoming: n, outgoing: n}], oldest first, with the days without messages (count 0).
Removes the messages selected by ids (a list or an Ecto.Query
selecting ids) that are in one of the from statuses. Returns how many
were removed completely. opts go to Karta.Storage (e.g. blob_store:).
Accepted messages that were removed are announced (:messages_removed).
A message of this server, or nil.
How many messages the server accepted in each hour from the hour of
since to the hour of now: [{hour, count}], oldest first, with the
hours without messages (count 0). An hour is the DateTime it starts at.
The server's accepted messages, newest first (limit: default 50): the
first page of search/3 with no search, through Karta.Pager.
Moves a message from :pending to :accepted and announces it on its
server's topic (after the commit, inside Karta.Changes.transaction/2);
{:error, :not_pending} when it is no longer pending.
The server's accepted messages among ids that match search, in no
order: what a page that shows them may show of them now.
The stored body of an accepted message, decrypted.
A page of the server's accepted messages that match search, newest
first. Options: after, before (a message id) and limit (default 50),
as for Karta.Pager.page/2.
Folds the stored body of an accepted message, decrypted, chunk by chunk
(Karta.Storage.stream/4): see there for the folder's contract and the
errors. A body is read this way when it is sent or checked, so that a
message of any size costs the same memory.
Functions
@spec count_since(Karta.Servers.Server.t(), Karta.Messages.Search.t(), DateTime.t()) :: non_neg_integer()
How many of the server's accepted messages that match search were
created after since.
@spec create_pending(map()) :: {:ok, Karta.Messages.Message.t()}
Inserts a :pending message row.
@spec daily_counts(Karta.Servers.Server.t(), pos_integer(), DateTime.t()) :: [ %{date: Date.t(), incoming: non_neg_integer(), outgoing: non_neg_integer()} ]
How many messages the server accepted on each UTC day from the day
days - 1 days before now's through it: [%{date: Date, incoming: n, outgoing: n}], oldest first, with the days without messages (count 0).
@spec discard( [Ecto.UUID.t()] | Ecto.Query.t(), [Karta.Messages.Message.t()], keyword() ) :: non_neg_integer()
Removes the messages selected by ids (a list or an Ecto.Query
selecting ids) that are in one of the from statuses. Returns how many
were removed completely. opts go to Karta.Storage (e.g. blob_store:).
Accepted messages that were removed are announced (:messages_removed).
@spec get(Karta.Servers.Server.t(), Ecto.UUID.t()) :: Karta.Messages.Message.t() | nil
A message of this server, or nil.
@spec hourly_counts(Karta.Servers.Server.t(), DateTime.t(), DateTime.t()) :: [ {DateTime.t(), non_neg_integer()} ]
How many messages the server accepted in each hour from the hour of
since to the hour of now: [{hour, count}], oldest first, with the
hours without messages (count 0). An hour is the DateTime it starts at.
@spec list(Karta.Servers.Server.t(), keyword()) :: [Karta.Messages.Message.t()]
The server's accepted messages, newest first (limit: default 50): the
first page of search/3 with no search, through Karta.Pager.
@spec mark_accepted(Ecto.UUID.t()) :: :ok | {:error, :not_pending}
Moves a message from :pending to :accepted and announces it on its
server's topic (after the commit, inside Karta.Changes.transaction/2);
{:error, :not_pending} when it is no longer pending.
@spec matching(Karta.Servers.Server.t(), Karta.Messages.Search.t(), [Ecto.UUID.t()]) :: [ Karta.Messages.Message.t() ]
The server's accepted messages among ids that match search, in no
order: what a page that shows them may show of them now.
@spec raw(Karta.Messages.Message.t(), keyword()) :: {:ok, binary()} | {:error, term()}
The stored body of an accepted message, decrypted.
@spec search(Karta.Servers.Server.t(), Karta.Messages.Search.t(), keyword()) :: Karta.Pager.Page.t()
A page of the server's accepted messages that match search, newest
first. Options: after, before (a message id) and limit (default 50),
as for Karta.Pager.page/2.
@spec stream( Karta.Messages.Message.t(), acc, (binary(), acc -> {:ok, acc} | {:error, term()}), keyword() ) :: {:ok, acc} | {:error, term()} when acc: var
Folds the stored body of an accepted message, decrypted, chunk by chunk
(Karta.Storage.stream/4): see there for the folder's contract and the
errors. A body is read this way when it is sent or checked, so that a
message of any size costs the same memory.