Tornex.Scheduler.QueryRegistry (Tornex v0.5.1)

Copy Markdown View Source

A registry for Tornex.SpecQuery backed by a tree held by a globally registered GenServer.

This module will store each Tornex.SpecQuery and map the query to its Tornex.Scheduler.Bucket to allow for the combination of queries. This only applies to Tornex.SpecQuery that are not quarantined.

Summary

Types

The state for the GenServer of the tree representing pending, un-fulfilled API calls.

Functions

Returns a specification to start this module under a supervisor.

Determine if a list of Tornex.SpecQuery is "conflict free".

Get the number of unique paths in a list of queries.

Greedily build subsets of the queries where the subsets of queries' parameters do not conflict.

Insert a Tornex.SpecQuery into the query tree.

Merge any applicable Tornex.SpecQuery for this combintion of a resource and resource ID together.

Merge "similar" queries to the Tornex.SpecQuery provided to create one query containing as many selections as possible. Quarantined queries will not be merged and will be executed as provided.

Traverse the state tree and remove the branches that are being fulfilled by the Tornex.Scheduler.ExecutionUnit.

Types

state()

@type state() :: %{
  required(String.t()) => %{
    required(Tornex.SpecQuery.parameter() | nil) => %{
      required(String.t()) => [Tornex.SpecQuery.t()]
    }
  }
}

The state for the GenServer of the tree representing pending, un-fulfilled API calls.

Example Tree

user
 2383326
   profile
   attacks
 1
faction
 15644
   attacks
torn
 nil
   crimes
   items

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

conflict_free?(queries)

@spec conflict_free?(queries :: [Tornex.SpecQuery.t()]) :: boolean()

Determine if a list of Tornex.SpecQuery is "conflict free".

For the list of queries to be "conflict free", all of the queries must have one unique value for each parameter key-value pair.

count_unique_paths(queries)

@spec count_unique_paths(queries :: [Tornex.SpecQuery.t()]) :: non_neg_integer()

Get the number of unique paths in a list of queries.

greedy_subsets(queries)

@spec greedy_subsets(queries :: [Tornex.SpecQuery.t()]) :: [[Tornex.SpecQuery.t()]]

Greedily build subsets of the queries where the subsets of queries' parameters do not conflict.

insert(query)

@spec insert(query :: Tornex.SpecQuery.t()) :: :ok

Insert a Tornex.SpecQuery into the query tree.

The Tornex.SpecQuery must be registered against an origin (of type Genserver.from()) so that the Tornex.Scheduler.ExecutionUnit generated including the Tornex.SpecQuery will be able to return the portion of the response corresponding to the Tornex.SpecQuery to the origin.

merge(query)

Merge any applicable Tornex.SpecQuery for this combintion of a resource and resource ID together.

merge_similar(query, state)

@spec merge_similar(query :: Tornex.SpecQuery.t(), state :: state()) ::
  Tornex.Scheduler.ExecutionUnit.t() | Tornex.SpecQuery.t()

Merge "similar" queries to the Tornex.SpecQuery provided to create one query containing as many selections as possible. Quarantined queries will not be merged and will be executed as provided.

NOTE: The queries may not necessarily be merged into the query provided if it is more efficient to merge the queries into a different non-public query.

For a query to be similar to another query, the query must:

  1. Be for the same resource and resource ID.
  2. Not have conflicting parameters where the same key has different values.
  3. Not be different responses depending on the invoker. For example, this can occur when the responses is specific to a resource owner (such as a user for user/basic when not providing a user to user/{id}/basic).
  4. Not have an additional permission required such as faction API access.

remove(state, execution_unit)

@spec remove(
  state :: state(),
  execution_unit :: Tornex.Scheduler.ExecutionUnit.t() | Tornex.SpecQuery.t()
) :: state()

Traverse the state tree and remove the branches that are being fulfilled by the Tornex.Scheduler.ExecutionUnit.

After removing the queries being fulfilled from the branches of the state tree, the function will also prune the tree of any branches related to the queries if there are no queries retaining them.

start_link(opts \\ [])

@spec start_link(opts :: keyword()) :: GenServer.on_start()

Start the Tornex.Scheduler.QueryRegistry GenServer.