rankex v0.1.3 Rankex View Source

A ranking/leaderboard library for Elixir based on ETS.

It's leveraged on ETS ordered set once it can also order some kinds of records with a tuple as key.

This way all operations regarding sorting is given for granted within a bulletproof cache.

For performance reasons, update and delete operations require the previous score which should be already stored in users cache.

Supports:

  • Fast insertion
  • Fast update
  • Fast delete
  • Position by score (thousand reads in 300ms) or id
  • Detail field that might be used for rank names or other info
  • All with operation for tied score
  • Multiple tables
  • Top N results in different formats:

    • :tuples : [{{score, id}, detail}, ...]
    • :map_list : [%{id: id, detail: detail, score: score}, ...]
    • :position_map : %{1: %{id: id, detail: detail, score: score}, 2: ...}
    • :score_position_map : %{score_value => %{id: id, detail: detail, position: position}, ...}

Benchmarking

insert/3 for 10000 items: 30ms

After inserting one million records:

delete/2 for 10000 items: 7ms update/4 for 10000 items: 37ms position_in/1 for 1000 items: 310ms

Running on Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz and DDR4 2400 MT/s

Link to this section Summary

Functions

All items with certain score.

All items with certain score (in a named table).

Deletes item from ranking.

Deletes item from ranking (of named table).

Creates default table.

Creates named table.

Inserts and sorts item on ranking.

Inserts and sorts item on ranking (of named table).

Inserts and sorts many item on ranking.

Inserts and sorts item on ranking (of named table).

Returns a map with leader data or nil.

Returns a map with leader data or nil (of named table).

Gives the position/rank for an item with given id.

Gives the position/rank for an item with given id (in named table).

Gives the position/rank for an item with given score.

Gives the position/rank for an item with given score (in named table).

Number of items in the ranking.

Number of items in the ranking (of named table).

Returns the top N items of the ranking.

Returns the top N items of the ranking (for a named table).

Updates score and position of an item on ranking.

Updates score and position of an item on ranking (of named table).

Link to this section Functions

All items with certain score.

All items with certain score (in a named table).

Link to this function

delete(id, prev_stored_score)

View Source

Deletes item from ranking.

Link to this function

delete(table, id, prev_stored_score)

View Source

Deletes item from ranking (of named table).

Creates default table.

Creates named table.

Link to this function

insert(id, new_score, detail)

View Source

Inserts and sorts item on ranking.

Params: id: integer, UUID, etc. new_score: integer or float detail: might be the name of a person on the raking or any other detail related to the id.

Link to this function

insert(table, id, new_score, detail)

View Source

Inserts and sorts item on ranking (of named table).

Link to this function

insert_many(record_list)

View Source

Inserts and sorts many item on ranking.

Params: list: [{{score, id}, detail}, ...]

Link to this function

insert_many(table, record_list)

View Source

Inserts and sorts item on ranking (of named table).

Returns a map with leader data or nil.

Returns a map with leader data or nil (of named table).

Gives the position/rank for an item with given id.

This is much slower than position/1 once the table is sorted by score.

Link to this function

position_by_id(table, id)

View Source

Gives the position/rank for an item with given id (in named table).

This is much slower than position/2 once the table is sorted by score.

Link to this function

position_in(score, rank_size)

View Source

Gives the position/rank for an item with given score.

Link to this function

position_in(table, score, rank_size)

View Source

Gives the position/rank for an item with given score (in named table).

Number of items in the ranking.

Number of items in the ranking (of named table).

Link to this function

top(num, mode \\ :tuples)

View Source

Returns the top N items of the ranking.

format for the modes: :tuples : [{{score, id}, detail}, ...] :map_list : [%{id: id, detail: detail, score: score}, ...] :position_map : %{1: %{id: id, detail: detail, score: score}, 2: ...} :score_position_map : %{score: %{id: id, detail: detail, position: position}, 2: ...}

Returns the top N items of the ranking (for a named table).

Link to this function

update(id, prev_stored_score, new_score, detail)

View Source

Updates score and position of an item on ranking.

Link to this function

update(table, id, prev_stored_score, new_score, detail)

View Source

Updates score and position of an item on ranking (of named table).