Show:

Db_Query Class

Extends Db_Expression
Module: Db

This class lets you create and use Db queries.

Methods

applyHash

(
  • $value
  • [$hash='normalize']
  • [$len=self::HASH_LEN]
)
String private

Calculates hash of the value

Parameters:

  • $value String
  • [$hash='normalize'] String optional
  • [$len=self::HASH_LEN] Integer optional

Returns:

String:

hashed

(
  • $value
  • [$hash=null]
)
String

Calculate hash of the value

Parameters:

  • $value String
  • [$hash=null] String optional

    Hash is one of 'md5' or 'normalize' optionally followed by '%' and number

Returns:

String:

map_shard

(
  • $a
)
String private

Make partition from array of points

Parameters:

  • $a Array

Returns:

String:

shard

(
  • [$upcoming=null]
  • [$criteria=null]
)
Array

Analyzes the query's criteria and decides where to execute the query. Here is sample shards config:

NOTE: "fields" shall be an object with keys as fields names and values containing hash definition in the format "type%length" where type is one of 'md5' or 'normalize' and length is hash length hash definition can be empty string or false. In such case 'md5%7' is used

NOTE: "partition" can be an array. In such case shards shall be named after partition points

"Streams": {
    "prefix": "streams_",
    "dsn": "mysql:host=127.0.0.1;dbname=DBNAME",
    "username": "USER",
    "password": "PASSWORD",
    "driver_options": {
        "3": 2
    },
    "shards": {
        "alpha": {
            "prefix": "alpha_",
            "dsn": "mysql:host=127.0.0.1;dbname=SHARDDBNAME",
            "username": "USER",
            "password": "PASSWORD",
            "driver_options": {
                "3": 2
            }
        },
        "betta": {
            "prefix": "betta_",
            "dsn": "mysql:host=127.0.0.1;dbname=SHARDDBNAME",
            "username": "USER",
            "password": "PASSWORD",
            "driver_options": {
                "3": 2
            }
        },
        "gamma": {
            "prefix": "gamma_",
            "dsn": "mysql:host=127.0.0.1;dbname=SHARDDBNAME",
            "username": "USER",
            "password": "PASSWORD",
            "driver_options": {
                "3": 2
            }
        },
        "delta": {
            "prefix": "delta_",
            "dsn": "mysql:host=127.0.0.1;dbname=SHARDDBNAME",
            "username": "USER",
            "password": "PASSWORD",
            "driver_options": {
                "3": 2
            }
        }
    },
    "indexes": {
        "Stream": {
            "fields": {"publisherId": "md5", "name": "normalize"},
            "partition": {
                "0000000.       ": "alpha",
                "0000000.sample_": "betta",
                "4000000.       ": "gamma",
                "4000000.sample_": "delta",
                "8000000.       ": "alpha",
                "8000000.sample_": "betta",
                "c000000.       ": "gamma",
                "c000000.sample_": "delta"
            }
        }
    }
}

Parameters:

  • [$upcoming=null] Array optional

    Temporary config to use in sharding. Used during shard split process only

  • [$criteria=null] Array optional

    Overrides the sharding criteria for the query. Rarely used unless testing what shards the query would be executed on.

Returns:

Array:

Returns an array of ($shardName => $query) pairs, where $shardName can be the name of a shard, '' for just the main shard, or "*" to have the query run on all the shards.

shard_internal

(
  • $index
  • $hashed
)
Array private

does a depth first search and returns the array of shardname => $query pairs corresponding to which shards are affected

Parameters:

  • $index Array
  • $hashed String

Returns:

Array:

slice_partitions

(
  • $partition
  • $j
  • $hashed
  • [$adjust=false]
)
Array private

Narrows the partition list according to hashes

Parameters:

  • $partition Array
  • $j Integer

    Currently processed hashed array member

  • $hashed Array
  • [$adjust=false] Boolean optional

Returns:

Array:

Properties

$cache

Array

Class cache

$chain

Db_Query

The query that was run to produce this result

$expression

String

The expression as a string

$mapping

Array private

Actual points mapping depending if partition is plain or associative array

$parameters

Array protected

The parameters to bind with the expression

HASH_LEN

Integer final

Default length of the hash used for sharding

Default: 7

TYPE_DELETE

Integer final

Delete query

TYPE_INSERT

Integer final

Insert query

TYPE_RAW

Integer final

Raw query

TYPE_ROLLBACK

Integer final

Rollback query

TYPE_SELECT

Integer final

Select query

TYPE_UPDATE

Integer final

Update query