alpine

package module
v0.0.0-...-8b3d7ad Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 2 Imported by: 0

README

alpine

High-performance compression for time-series data in Go. Optimized for float64 and int64 sequences.

Features

  • Lossless float compression using ALP (Adaptive Lossless floating-Point)
  • Integer support for timestamps, counters, and sequential data
  • Predictive Delta encoding for optimal time-series compression
  • Auto-optimization - automatic rice parameter and precision detection
  • Zero dependencies - pure Go implementation
  • Builder pattern - fluent API for configuration
  • Fast - optimized for time-series workloads

Installation

go get github.com/ach968/alpine

Quick Start

Float Data (Time-Series)
package main

import (
    "fmt"
    "log"
    
    "github.com/ach968/alpine"
)

func main() {
    // Sample time-series data
    data := []float64{10.5, 11.2, 12.8, 13.1, 14.5}
    
    // Encode with auto-detected parameters (default behavior)
    encoded := alpine.NewFloatEncoder(data).Encode()
    if encoded == nil {
        log.Fatal("encode failed")
    }
    
    fmt.Printf("Original: %d bytes, Encoded: %d bytes\n", 
        len(data)*8, len(encoded))
    
    // Decode
    decoded := alpine.NewDecoder(encoded).DecodeFloat()
    if decoded == nil {
        log.Fatal("decode failed")
    }
    
    fmt.Printf("Round-trip successful: %v\n", decoded)
}
Integer Data (Timestamps)
// Timestamps
timestamps := []int64{1700000000, 1700000060, 1700000120}
encoded := alpine.NewIntEncoder(timestamps).Encode()
decoded := alpine.NewDecoder(encoded).DecodeInt()
Builder Pattern
// Custom configuration
encoded := alpine.NewFloatEncoder(data).
    WithRiceParam(8).           // Set specific Rice parameter
    WithPrecision(2).            // 2 decimal places
    WithAutoRiceParam().        // Override: auto-detect Rice param
    Encode()

// All auto-detected
encoded := alpine.NewFloatEncoder(data).
    WithAutoPrecision().
    WithAutoRiceParam().
    Encode()

API Reference

Builder Types
// FloatEncoder - encodes float64 data
alpine.NewFloatEncoder(data []float64) *FloatEncoder

// IntEncoder - encodes int64 data  
alpine.NewIntEncoder(data []int64) *IntEncoder

// Decoder - decodes both float and int data
alpine.NewDecoder(encoded []byte) *Decoder
FloatEncoder Methods
func (e *FloatEncoder) WithRiceParam(param int) *FloatEncoder
func (e *FloatEncoder) WithPrecision(precision int) *FloatEncoder
func (e *FloatEncoder) WithAutoRiceParam() *FloatEncoder
func (e *FloatEncoder) WithAutoPrecision() *FloatEncoder
func (e *FloatEncoder) Encode() ([]byte, error)
IntEncoder Methods
func (e *IntEncoder) WithRiceParam(param int) *IntEncoder
func (e *IntEncoder) WithAutoRiceParam() *IntEncoder
func (e *IntEncoder) Encode() ([]byte, error)
Decoder Methods
func (d *Decoder) DecodeFloat() ([]float64, error)
func (d *Decoder) DecodeInt() ([]int64, error)
Backwards Compatibility

The legacy Options API is still supported:

opts := alpine.Options{
    RiceParam:   0,  // Auto-detect
    ALPExponent: -1, // Auto-detect precision
}
encoded, _ := alpine.Encode(data, opts)
decoded, _ := alpine.Decode(encoded)

How It Works

Encoding Pipeline
[]float64 -> ALP Scale (detect precision, multiply by 10^p)
          -> Predictive Delta Encode
          -> ZigZag (signed -> unsigned)
          -> Golomb-Rice Encode
          -> []byte (with header)

[]int64 -> Predictive Delta Encode
        -> ZigZag (signed -> unsigned)
        -> Golomb-Rice Encode
        -> []byte (with header)
Auto-Parameter Selection

The library automatically detects optimal parameters:

  • Rice parameter: Calculated from the median of absolute delta values, rounded to nearest power of 2. This heuristic is efficient because Golomb-Rice encoding is optimal when m ≈ median(|deltas|), and powers of 2 enable bit-shift optimization.
  • Precision: Detected by testing round-trip accuracy (1-17 decimal places)

References

License

MIT License - see LICENSE file

Documentation

Overview

Package alpine provides high-performance compression for sequential numeric data. It supports both float64 (with ALP lossless compression) and int64 data, with multiple encoding modes optimized for different data patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoRiceParam

func AutoRiceParam(deltas []int64) int

AutoRiceParam calculates the optimal Rice parameter for given deltas. This is a convenience function for advanced users who want to pre-calculate.

func Decode

func Decode(encoded []byte) ([]float64, error)

Decode decompresses data produced by Encode (float64).

func Encode

func Encode(input []float64, opts Options) ([]byte, error)

Encode compresses float64 data using the specified mode. For ModeFloat, uses ALP + Predictive Delta encoding (lossless).

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder is a builder for decoding compressed data

func NewDecoder

func NewDecoder(encoded []byte) *Decoder

NewDecoder creates a new Decoder with the given encoded data

func (*Decoder) DecodeFloat

func (d *Decoder) DecodeFloat() ([]float64, error)

DecodeFloat decodes the encoded data as float64 values

func (*Decoder) DecodeInt

func (d *Decoder) DecodeInt() ([]int64, error)

DecodeInt decodes the encoded data as int64 values

type FloatEncoder

type FloatEncoder struct {
	// contains filtered or unexported fields
}

FloatEncoder is a builder for encoding float64 data

func NewFloatEncoder

func NewFloatEncoder(data []float64) *FloatEncoder

NewFloatEncoder creates a new FloatEncoder with the given data

func (*FloatEncoder) Encode

func (e *FloatEncoder) Encode() ([]byte, error)

Encode compresses the float64 data and returns the encoded bytes

func (*FloatEncoder) WithAutoPrecision

func (e *FloatEncoder) WithAutoPrecision() *FloatEncoder

WithAutoPrecision enables automatic precision detection

func (*FloatEncoder) WithAutoRiceParam

func (e *FloatEncoder) WithAutoRiceParam() *FloatEncoder

WithAutoRiceParam enables automatic Rice parameter detection

func (*FloatEncoder) WithPrecision

func (e *FloatEncoder) WithPrecision(precision int) *FloatEncoder

WithPrecision sets the ALP precision exponent (0 = auto, -1 = auto-detect) Valid values are 0-17

func (*FloatEncoder) WithRiceParam

func (e *FloatEncoder) WithRiceParam(param int) *FloatEncoder

WithRiceParam sets the Golomb-Rice parameter for encoding

type IntEncoder

type IntEncoder struct {
	// contains filtered or unexported fields
}

IntEncoder is a builder for encoding int64 data

func NewIntEncoder

func NewIntEncoder(data []int64) *IntEncoder

NewIntEncoder creates a new IntEncoder with the given data

func (*IntEncoder) Encode

func (e *IntEncoder) Encode() ([]byte, error)

Encode compresses the int64 data using predictive delta encoding and returns the encoded bytes

func (*IntEncoder) WithAutoRiceParam

func (e *IntEncoder) WithAutoRiceParam() *IntEncoder

WithAutoRiceParam enables automatic Rice parameter detection

func (*IntEncoder) WithRiceParam

func (e *IntEncoder) WithRiceParam(param int) *IntEncoder

WithRiceParam sets the Golomb-Rice parameter for encoding

type Mode

type Mode int

Mode represents the encoding strategy

const (
	// ModeFloat uses ALP + Predictive Delta for float64 data (lossless)
	ModeFloat Mode = 0

	// ModeInt uses simple delta: value[i] - value[i-1]
	// Best for: Monotonically increasing/decreasing integers (timestamps, counters)
	ModeInt Mode = 1
)

type Options

type Options struct {
	Mode        Mode // Encoding mode (default: ModeFloat for floats)
	RiceParam   int  // Golomb-Rice parameter (0 = auto-detect)
	ALPExponent int  // For ModeFloat: precision (-1 = auto-detect, 0 = integers)
}

Options configures the encoding process

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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