hll

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Copyright (c) 2015, RetailNext, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

hyperloglog.c - Redis HyperLogLog probabilistic cardinality approximation.
* This file implements the algorithm and the exported Redis commands.
*
* Copyright (c) 2014, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*   * Redistributions of source code must retain the above copyright notice,
*     this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in the
*     documentation and/or other materials provided with the distribution.
*   * Neither the name of Redis nor the names of its contributors may be used
*     to endorse or promote products derived from this software without
*     specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.

Index

Constants

View Source
const (
	PipelineDenseDirty  = 'd'
	PipelineDenseClean  = 'D'
	PipelineSparseClean = 'S'
	PipelineSparseDirty = 's'
)
View Source
const (
	P         = 14     // The greater P is, the smaller the error.
	Registers = 1 << P // With P=14, 16384 registers.

	CountBits = 6 // Enough to count up to 63 leading zeros.
	CountMax  = (1 << CountBits) - 1

	HeaderSize         = 16
	DenseRegistersSize = (Registers*CountBits+7)/8 + 1 + HeaderSize
)
View Source
const (
	HeaderSizeRetailNext = 15
)

Variables

View Source
var PE [64]float64

Functions

func Add

func Add(p, tmp []byte, reg Register, count uint8) (pout, tmpout []byte, added bool, err error)

Add folds |reg| and |count| into the HLL encoded by |p|. |tmp| may be used to reduce required allocation.

func Count

func Count(p []byte) (int, error)

Count returns the cardinality of an HLL.

func DecodeRetailNextHeader

func DecodeRetailNextHeader(p []byte) (sparse bool, bpr int, err error)

DecodeRetailNextHeader verifies that |p| is a serialized RetailNext HLL, and extracts key properties of the representation.

func DenseSum

func DenseSum(p []byte) (E float64, ez int)

Compute SUM(2^-reg) in the dense representation. * PE is an array with a pre-computer table of values 2^-reg indexed by reg. * As a side effect the integer pointed by 'ezp' is set to the number * of zero registers.

func EncodePipelineFormat

func EncodePipelineFormat(b, p []byte) ([]byte, error)

EncodePipelineFormat appends the HLL |p|, in Redis format, to |b| as a PipelineDB-formatted HLL. The representations share the same register wire format, but have slightly differing headers.

func Init

func Init(p []byte) []byte

Init returns an empty sparse HLL.

func InitDense

func InitDense(p []byte) []byte

InitDense returns an empty dense HLL.

func IsRedisFormat

func IsRedisFormat(p []byte) bool

IsRedisFormat returns whether the HLL is in the standard Redis wire format.

func MurmurSum64

func MurmurSum64(data []byte) uint64

This is a port of MurmurHash3_x64_128 from MurmurHash3.cpp

func Reduce

func Reduce(p, q, tmp []byte) (pout, tmpout []byte, err error)

Reduce reduces HLL |q| into HLL |p|. |tmp| may be used to reduce memory allocation. |p| and |tmp| may be modified and are returned. |q| is never modified.

func SparseSum

func SparseSum(p []byte) (E float64, ez int, err error)

func SparseToDense

func SparseToDense(p, tmp []byte) (pout, tmpout []byte, err error)

Types

type Register

type Register uint

func RegisterRhoRedis

func RegisterRhoRedis(hash uint64) (Register, uint8)

RegisterRhoRedis returns the Register and rho (count) for |hash| with an identical implementation to Redis.

func RegisterRhoRetailNext

func RegisterRhoRetailNext(hash uint64) (Register, uint8)

RegisterRhoRedis returns the Register and rho (count) for |hash| with an identical implementation to the RetailNext HLLPP implementation.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL