Reduce

Overview

The reduce system takes key:value pairs as input from the Map operation. When the input meets defined conditions, the reduce produces a new set of key:value pairs using a defined operation.

The reduce operation can be min, max, count or sum. The output of the reduce operation is a summary of all processed inputs transformed into an array of all the reduce operations.

Reduce definition example

{
  "version": "1.7.0",
  "event": {
    "game": {
      "reductions": [
        {
          "id": "changeColor",
          "where": {"key": ["changeColor", "<name>", "<color>"]},
          "key": ["<name>", "<color>"],
          "value": ["$count"],
          "period": 250
        },
        {
          "id": "cheer",
          "where": {"key": ["cheer", "<name>"]},
          "key": ["<name>"],
          "value": ["$sum"],
          "period": 250
        }
      ]
    }
  }
}

Reduce Schema

id

A unique string that refers to the reduce.

where

A schema defining the conditions that trigger producing a new key:value pair. See Where clause content for more information.

period

The frequency of the reduction in milliseconds (ms). For example, a 250ms period means that the game would receive the reduce output 4 times per second.

key

An array of strings representing the new key. Each string can be:

  • Alphanumeric string: A plain literal value.
  • <anyText>: A named value taken from the input key.

value

An array of operations which merges the inputs and generates the outputs. Note that even though an operation is specified, all operations are currently returned into each reduce.

Possible operations are:

  • $min: Returns the minimum value among all inputs.
  • $max: Returns the maximum value among all inputs.
  • $count: Returns the number of times the key was encountered.
  • $sum: Returns the sum of all input values added together.

Reduce Example

The Reduce operation takes key:value pairs as input and transforms them into another set of key:value pairs, but merged together to have fewer of them.

Like the mapping step, Genvid uses JSON for the reduction step.

{
  "id": "playerlike",
  "where": {
    "key": ["like", "<playerName>"]
  },
  "key": ["like", "<playerName>"],
  "value": ["$count"],
  "period": 1000
}

During the reduction step, the operation looks for any key starting with "like"`while ignoring the second parameter. (The second parameter matched will be referable as the token ``playerName` in later steps.)

The operation merges the matching key:value will then be merged, and the resulting valuepairs and assigns the number of times it encountered the original key to the value. Genvid uses this result to transmit the event data.

For example, we receive the following results from the Map function:

[
   { "key": ["like", "player1"], "value": 1 },
   { "key": ["like", "player1"], "value": 1 },
   { "key": ["like", "player2"], "value": 1 },
   { "key": ["like", "player1"], "value": 1 }
]

The Reduce function merges them into the following:

[
  {"key": ["like", "player1"], "value": "3"},
  {"key": ["like", "player2"], "value": "1"}
]