wio

package module
v0.0.0-...-b8da6e2 Latest Latest
Warning

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

Go to latest
Published: May 2, 2018 License: MIT Imports: 7 Imported by: 0

README

go-window-io

Build Status GoDoc Go Report Card

A Sliding window over arbitrary io.Readers in go.

wio or windowed-io wraps an io.Reader and implements io.Read but reads data chunked over a Sliding Window.

wio provides two controls: window size and the step size.

For a canonical Rolling window, the step size is 1.

Examples
wSize := 3 // window size of 3

rollingBuf := []byte("\x00\x01\x02\x03\x04\x05\x06\x07\x08")
reader := bytes.NewBuffer(rollingBuf)
rollingReader := wio.NewRolling(reader, wSize)

// For a window that rolls by more than 1:
// sSize := 2 // window of 3 slide by 2
// steppingReader := wio.NewRolling(reader, wSize, sSize)

for {
  // buf is some buffer at-least wSize in size
  n, err := rollingReader.Read(buf)
  if err != nil {
    if err == io.EOF {
      // handle EOF
    }
    if err == io.ErrShortBuffer {
      // len of `buf` was shorter than wSize - make sure the given buf has correct size
    }
  }

  // n can be less than wSize for the last window
  consumeData(buf[:n])
}

Benchmarks:

• [MEASUREMENT]
Test window io
/Users/UrjitSingh/AdvancedApps/gocode/src/github.com/urjitbhatia/go-window-io/wio_test.go:19
  benchmark rolling window
  /Users/UrjitSingh/AdvancedApps/gocode/src/github.com/urjitbhatia/go-window-io/wio_test.go:199

  Ran 1000 samples:
  runtime:
    Fastest Time: 0.002s
    Slowest Time: 0.004s
    Average Time: 0.003s ± 0.000s
------------------------------

Ran 12 of 12 Specs in 2.566 seconds
SUCCESS! -- 12 Passed | 0 Failed | 0 Pending | 0 Skipped --- PASS: TestGoWindowIo (2.57s)
goos: darwin
goarch: amd64
pkg: github.com/urjitbhatia/go-window-io
BenchmarkRollingWindow/BufSize:_10240_wSize:_1-8         	50000000	        31.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_103-8       	50000000	        33.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_205-8       	50000000	        31.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_307-8       	50000000	        32.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_409-8       	50000000	        31.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_511-8       	50000000	        33.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_613-8       	50000000	        32.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_715-8       	50000000	        33.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_817-8       	50000000	        30.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_919-8       	50000000	        33.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1021-8      	50000000	        32.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1123-8      	30000000	        33.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1225-8      	50000000	        32.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1327-8      	50000000	        32.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1429-8      	50000000	        31.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1531-8      	50000000	        33.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1633-8      	50000000	        31.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1735-8      	50000000	        31.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1837-8      	50000000	        32.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_1939-8      	50000000	        33.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkRollingWindow/BufSize:_10240_wSize:_2041-8      	50000000	        32.1 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/urjitbhatia/go-window-io	44.603s

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDisjointWindow = errors.New("Step size cannot be larger than window size")

ErrDisjointWindow - Step size has to be >= 1 && <= window size

View Source
var ErrZeroStepSize = errors.New("Step size cannot be zero. Should be 1 or more")

ErrZeroStepSize - Step size has to be >= 1

View Source
var ErrZeroWindowSize = errors.New("Window size cannot be zero. Should be 1 or more")

ErrZeroWindowSize - Window size has to be >= 1

Functions

func PrintRing

func PrintRing(r *ring.Ring, msg string)

PrintRing is a helpful debug function that prints a ring with the given message

Types

type WindowReader

type WindowReader interface {
	io.Reader
}

WindowReader wraps io.Reader and reads in a window of data The window size and the window rolling step size can be configured

func NewRolling

func NewRolling(r io.Reader, wSize int) (WindowReader, error)

NewRolling converts an io.Reader into a rolling windowed io.Reader The size of a Rolling Window is 1. This is same as calling: NewStepping(reader, w, 1)

func NewStepping

func NewStepping(r io.Reader, wSize, sSize int) (WindowReader, error)

NewStepping converts an io.Reader into a windowed io.Reader with a window size of wSize, and a step size of sSize

Jump to

Keyboard shortcuts

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