delta

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2021 License: MIT Imports: 8 Imported by: 2

README

go-delta - A Go package and utility to generate and apply binary delta updates.

Go Report Card Build Status Test Coverage Gitter chat godoc

Suggestions:

  • Works best on text files, database dumps and any other files with lots of repeating patterns and few changes between updates.

  • Generating deltas of compressed files is not recommended because a small change in the source data can lead to lots of changes in the compressed result, so generating a delta update may give you only minimal size reduction.

  • Don't compress bytes returned by Delta.Bytes() because they are already compressed using ZLib compression.

  • Every delta update adds about 156 bytes for the source and target hashes and various lengths, so it is not recommended for very miniscule updates.

Demonstration:

package main

import (
    "fmt"
    "github.com/balacode/go-delta"
)

func main() {
    fmt.Print("Binary delta update demo:\n\n")

    // The original data (20 bytes):
    var source = []byte("quick brown fox, lazy dog, and five boxing wizards")
    fmt.Print("The original is:", "\n", string(source), "\n\n")

    // The updated data containing the original and new content (82 bytes):
    var target = []byte(
        "The quick brown fox jumps over the lazy dog. " +
        "The five boxing wizards jump quickly.",
    )
    fmt.Print("The update is:", "\n", string(target), "\n\n")

    var dbytes []byte
    {
    	// Use Make() to generate a compressed patch from source and target
    	var d = delta.Make(source, target)
    	
    	// Convert the delta to a slice of bytes (e.g. for writing to a file)
    	dbytes = d.Bytes()
    }

    // Create a Delta from the byte slice
    var d = delta.Load(dbytes)

    // Apply the patch to source to get the target
    // The size of the patch is much shorter than target.
    var target2, err = d.Apply(source)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Print("Patched:", "\n", string(target2), "\n\n")
} //                                                                        main

Documentation

Index

Constants

View Source
const (
	// MatchLimit specifies the maximum number of positions tracked
	// for each unique key in the map of source data. See makeMap().
	MatchLimit = 50

	// MatchSize specifies the size of unique
	// chunks being searched for, in bytes.
	MatchSize = 9
)
View Source
const DebugIndex = false

Variables

View Source
var (
	// PL is fmt.Println() but is used only for debugging.
	PL = fmt.Println

	// TempBufferSize sets the size of memory buffers for reading files and other
	// streams. This memory is not fixed but allocated/released transiently.
	TempBufferSize = 32 * 1024 * 1024 // 32 MB

)
View Source
var (
	// DebugInfo when set, causes printing of messages helpful for debugging.
	DebugInfo = false

	// DebugTiming controls timing (benchmarking) of time spent in each function.
	DebugTiming = true

	// DebugWriteArgs when set, prints the arguments passed to write()
	DebugWriteArgs = false
)

Functions

func SetErrorFunc

func SetErrorFunc(fn func(args ...interface{}) error)

SetErrorFunc changes the error-handling function, so that all errors in this package will be sent to this handler, which is useful for custom logging and mocking during unit tests. To restore the default error handler use SetErrorFunc(nil).

Types

type Delta

type Delta struct {
	// contains filtered or unexported fields

} //                                                                       Delta

Delta stores the binary delta difference between two byte arrays

func Load

func Load(data []byte) (Delta, error)

Load fills a new Delta structure from a byte array previously returned by Delta.Bytes().

func Make

func Make(a, b []byte) Delta

Make given two byte arrays 'a' and 'b', calculates the binary delta difference between the two arrays and returns it as a Delta. You can then use Delta.Apply() to generate 'b' from 'a' the Delta.

func (*Delta) Apply

func (ob *Delta) Apply(source []byte) ([]byte, error)

Apply uses the 'source' byte array, applies this Delta to it and returns the updated byte array. If this delta was not generated from source, returns an error.

func (*Delta) Bytes

func (ob *Delta) Bytes() []byte

Bytes converts the Delta structure to a byte array (for serializing to a file, etc.)

func (*Delta) Dump

func (ob *Delta) Dump()

Dump prints this object to the console in a human-friendly format.

func (Delta) GoString

func (ob Delta) GoString() string

GoString returns a Go-syntax representation of the Delta structure. It implements the fmt.GoStringer interface.

func (*Delta) NewCount

func (ob *Delta) NewCount() int

NewCount returns the number of chunks not matched in source array.

func (*Delta) OldCount

func (ob *Delta) OldCount() int

OldCount returns the number of matched chunks in source array.

func (*Delta) SourceSize

func (ob *Delta) SourceSize() int

SourceSize returns the size of the source byte array, in bytes.

func (*Delta) TargetSize

func (ob *Delta) TargetSize() int

TargetSize returns the size of the target byte array, in bytes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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