z_pivot_rsc (zotonic_core v1.0.0-rc.17)

Pivoting server for the rsc table. Takes care of full text indices. Polls the pivot queue for any changed resources.

Summary

Functions

Convert process state when code is changed

Let a module define a custom pivot. The custom pivot table is created or changed to reflect the given columns. Per default an index is created for each column. Be careful when adding columns that are not nullable, if the table contains data then adding those columns will fail.

Handling all non call/cast messages

Initiates the server.

Insert a rsc_id in the pivot queue

Insert a rsc_id in the pivot queue for a certain date

Insert a slow running pivot task. For example syncing category numbers after an category update.

Insert a slow running pivot task. Use the UniqueKey to prevent double queued tasks for the same module:function.

Insert a slow running pivot task with unique key and arguments. The key is unique for the module:function.

Insert a slow running pivot task with unique key and arguments that should start after Seconds seconds. Always delete any existing transaction, to prevent race conditions when the task is running during this insert. The UniqueKey is used to have multiple entries per module/function. If only a single module/function should be queued, then set the UniqueKey to <<>>.

Lookup a custom pivot; give back the Id based on a column. Will always return the first Id found.

An immediate pivot request for a resource

Delay the next pivot, useful when performing big updates

Signal from pivot job that processing is done.

Ping from pivot process to keep alive and report progress

Return a modified property list with fields that need immediate pivoting on an update.

Poll the pivot queue for the database in the context

Rebuild the search index by queueing all resources for pivot.

Return the length of the pivot queue.

Return the number of pivot queue items scheduled for direct pivot.

Starts the server

Return the language used for stemming the full text index. We use a single stemming to prevent having seperate indexes per language.

Signal from task process that job is finished.

Ping from task process to keep alive and report progress

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

Types

task_delay/0

-type task_delay() :: non_neg_integer() | calendar:datetime() | undefined.

task_key/0

-type task_key() :: undefined | binary() | string() | atom() | integer().

task_return/0

-type task_return() :: ok | {delay, task_delay()} | {delay, task_delay(), NewArgs :: list()} | any().

Functions

cleanup_tsv_text(Text)

-spec cleanup_tsv_text(binary()) -> binary().

code_change(OldVsn, State, Extra)

Convert process state when code is changed

count_tasks(Context)

-spec count_tasks(z:context()) -> {ok, [map()]} | {error, term()}.

define_custom_pivot(Module, Columns, Context)

-spec define_custom_pivot(Module, Columns, Context) -> ok
                             when
                                 Module :: atom(),
                                 Columns :: [Column],
                                 Column ::
                                     #column_def{name :: atom(),
                                                 type :: string() | binary(),
                                                 length :: non_neg_integer() | undefined,
                                                 is_nullable :: boolean(),
                                                 is_array :: boolean(),
                                                 default :: term(),
                                                 primary_key :: boolean(),
                                                 unique :: boolean()} |
                                     {#column_def{name :: atom(),
                                                  type :: string() | binary(),
                                                  length :: non_neg_integer() | undefined,
                                                  is_nullable :: boolean(),
                                                  is_array :: boolean(),
                                                  default :: term(),
                                                  primary_key :: boolean(),
                                                  unique :: boolean()},
                                      Options} |
                                     {Colname, Colspec} |
                                     {Colname, Colspec, Options},
                                 Colname :: string() | binary() | atom(),
                                 Colspec :: string() | binary(),
                                 Options :: [Option],
                                 Option :: noindex,
                                 Context :: z:context().

Let a module define a custom pivot. The custom pivot table is created or changed to reflect the given columns. Per default an index is created for each column. Be careful when adding columns that are not nullable, if the table contains data then adding those columns will fail.

delete_task(Module, Function, Context)

-spec delete_task(module(), atom(), z:context()) -> non_neg_integer().

delete_task(Module, Function, UniqueKey, Context)

-spec delete_task(module(), atom(), task_key(), z:context()) -> non_neg_integer().

delete_tasks(Context)

-spec delete_tasks(z:context()) -> non_neg_integer().

get_pivot_title(Props)

-spec get_pivot_title(map()) -> binary().

get_pivot_title(Id, Context)

-spec get_pivot_title(m_rsc:resource_id(), z:context()) -> binary().

get_task(Context)

-spec get_task(z:context()) -> {ok, [map()]} | {error, term()}.

get_task(Module, Context)

-spec get_task(module(), z:context()) -> {ok, [map()]} | {error, term()}.

get_task(Module, Function, Context)

-spec get_task(module(), atom(), z:context()) -> {ok, [map()]} | {error, term()}.

get_task(Module, Function, UniqueKey, Context)

-spec get_task(module(), atom(), task_key(), z:context()) -> {ok, map()} | {error, term()}.

handle_call(Message, From, State)

handle_cast(Message, State)

handle_info(Info, State)

Handling all non call/cast messages

init(Site)

-spec init(term()) -> {ok, term()} | {ok, term(), timeout() | hibernate} | ignore | {stop, term()}.

Initiates the server.

insert_queue(IdOrIds, Context)

-spec insert_queue(IdOrIds, Context) -> ok
                      when
                          IdOrIds :: m_rsc:resource_id() | [m_rsc:resource_id()], Context :: z:context().

Insert a rsc_id in the pivot queue

insert_queue(IdOrIds, DueDate, Context)

-spec insert_queue(IdOrIds, DueDate, Context) -> ok
                      when
                          IdOrIds :: m_rsc:resource_id() | [m_rsc:resource_id()],
                          DueDate :: calendar:datetime(),
                          Context :: z:context().

Insert a rsc_id in the pivot queue for a certain date

insert_task(Module, Function, Context)

-spec insert_task(Module, Function, Context) -> {ok, TaskId} | {error, term()}
                     when
                         Module :: atom(),
                         Function :: atom(),
                         Context :: z:context(),
                         TaskId :: integer().

Insert a slow running pivot task. For example syncing category numbers after an category update.

insert_task(Module, Function, UniqueKey, Context)

-spec insert_task(Module, Function, UniqueKey, Context) -> {ok, TaskId} | {error, term()}
                     when
                         Module :: atom(),
                         Function :: atom(),
                         UniqueKey :: task_key(),
                         Context :: z:context(),
                         TaskId :: integer().

Insert a slow running pivot task. Use the UniqueKey to prevent double queued tasks for the same module:function.

insert_task(Module, Function, UniqueKey, Args, Context)

-spec insert_task(Module, Function, UniqueKey, Args, Context) -> {ok, TaskId} | {error, term()}
                     when
                         Module :: atom(),
                         Function :: atom(),
                         UniqueKey :: task_key(),
                         Args ::
                             list() |
                             fun((OldDue :: undefined | calendar:datetime(),
                                  OldArgs :: undefined | list(),
                                  NewDue :: calendar:datetime(),
                                  z:context()) ->
                                     {ok, {calendar:datetime(), list()}} | {error, term()}),
                         Context :: z:context(),
                         TaskId :: integer().

Insert a slow running pivot task with unique key and arguments. The key is unique for the module:function.

insert_task_after(Delay, Module, Function, UniqueKey, Args, Context)

-spec insert_task_after(Delay, Module, Function, UniqueKey, Args, Context) ->
                           {ok, TaskId} | {error, term()}
                           when
                               Delay :: task_delay(),
                               Module :: atom(),
                               Function :: atom(),
                               UniqueKey :: task_key(),
                               Args ::
                                   list() |
                                   fun((OldDue :: undefined | calendar:datetime(),
                                        OldArgs :: undefined | list(),
                                        NewDue :: calendar:datetime(),
                                        z:context()) ->
                                           {ok, {calendar:datetime(), list()}} | {error, term()}),
                               Context :: z:context(),
                               TaskId :: integer().

Insert a slow running pivot task with unique key and arguments that should start after Seconds seconds. Always delete any existing transaction, to prevent race conditions when the task is running during this insert. The UniqueKey is used to have multiple entries per module/function. If only a single module/function should be queued, then set the UniqueKey to <<>>.

list_tasks(Context)

-spec list_tasks(z:context()) -> {ok, [map()]} | {error, term()}.

lookup_custom_pivot(Module, Column, Value, Context)

Lookup a custom pivot; give back the Id based on a column. Will always return the first Id found.

pg_lang(LangCode)

-spec pg_lang(atom()) -> string().

pg_lang_extra(LangCode)

-spec pg_lang_extra(atom()) -> string().

pivot(Id, Context)

-spec pivot(Id, Context) -> ok when Id :: m_rsc:resource_id(), Context :: z:context().

An immediate pivot request for a resource

pivot_delay(Context)

-spec pivot_delay(z:context()) -> ok.

Delay the next pivot, useful when performing big updates

pivot_job_done(IdsOrError, Context)

-spec pivot_job_done(IdsOrError, Context) -> ok
                        when IdsOrError :: [m_rsc:resource_id()] | error, Context :: z:context().

Signal from pivot job that processing is done.

pivot_job_ping(Id, Context)

-spec pivot_job_ping(m_rsc:resource_id(), z:context()) -> ok.

Ping from pivot process to keep alive and report progress

pivot_resource_update(Id, UpdateProps, RawProps, Context)

-spec pivot_resource_update(Id, UpdateProps, RawProps, Context) -> UpdateProps1
                               when
                                   Id :: m_rsc:resource_id(),
                                   UpdateProps :: m_rsc:props(),
                                   RawProps :: m_rsc:props(),
                                   Context :: z:context(),
                                   UpdateProps1 :: m_rsc:props().

Return a modified property list with fields that need immediate pivoting on an update.

poll(Context)

-spec poll(z:context()) -> ok.

Poll the pivot queue for the database in the context

publish_task_event(What, Module, Function, Due, Context)

queue_all(Context)

-spec queue_all(Context) -> ok when Context :: z:context().

Rebuild the search index by queueing all resources for pivot.

queue_count(Context)

-spec queue_count(Context) -> QueueLength when Context :: z:context(), QueueLength :: non_neg_integer().

Return the length of the pivot queue.

queue_count_backlog(Context)

-spec queue_count_backlog(Context) -> BacklogLength
                             when Context :: z:context(), BacklogLength :: non_neg_integer().

Return the number of pivot queue items scheduled for direct pivot.

start_link(Site)

-spec start_link(term()) -> {ok, pid()} | ignore | {error, term()}.

Starts the server

status(Context)

-spec status(z:context()) -> {ok, map()} | {error, term()}.

stemmer_language(Context)

-spec stemmer_language(z:context()) -> string().

Return the language used for stemming the full text index. We use a single stemming to prevent having seperate indexes per language.

stemmer_language_config(Context)

-spec stemmer_language_config(z:context()) -> atom().

task_job_done(TaskId, Context)

-spec task_job_done(TaskId :: integer(), z:context()) -> ok.

Signal from task process that job is finished.

task_job_ping(TaskId, Percentage, Context)

-spec task_job_ping(TaskId :: integer(), Percentage :: 0..100, z:context()) -> ok.

Ping from task process to keep alive and report progress

terminate(Reason, State)

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.