Module: Fossilize

Extended by:
FFI::Library
Defined in:
lib/fossilize.rb,
lib/fossilize/delta.rb,
lib/fossilize/version.rb,
lib/fossilize/ring_buffer.rb

Overview

Accesss

Public Module

Summary

Provides an interface through Ruby-FFI to the delta encoding algorithm
written for the Fossil SCM project by D. Richard Hipp. All methods are module methods.

Examples

delta = Fossilize.create(file1, file2)
output = Fossilize.apply(file1, delta)

Defined Under Namespace

Modules: Delta Classes: RingBuffer

Constant Summary collapse

VERSION =
"1.1.1"

Class Method Summary collapse

Class Method Details

.apply(source, delta) ⇒ Object

Access

Public Module Method

Summary

Applies a delta string to another string.

Parameters

source - The old string to apply the delta string to.
delta - The delta string created using *create*.

Returns a new unified string created by applying the delta to the source if successful. The algorithm returns -1 as the output_size if the delta was not created from the given source or is malformed. In this case, this method returns nil.

Examples

# original is a String or File object
result = Fossilize.apply(original, delta)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fossilize.rb', line 86

def self.apply(source, delta)
  # Check the input types first
  source_string = check_input(source)
  delta_string = check_input(delta)

  # Get the eventual size of the deltaed file and create a string to hold it
  expected_output_size = delta_output_size(delta_string, delta_string.size)

  # The algorithm will return -1 as the output size if there was an error
  if expected_output_size == -1
    raise MalformedDeltaError, "Was this delta intended for this string/file?"
    return nil
  end

  # Create an empty string that is at-least the output_size given by *delta_output_size*
  output = "\0" * expected_output_size

  # Apply the delta to the old file to produce the merged result
  output_size = delta_apply(source_string, source_string.size, delta_string, delta_string.size, output)

  if output_size != expected_output_size
    raise DeltaApplicationError,
    "Output was #{output_size}, but I expected #{expected_output_size}!"
    return nil
  end

  return output
end

.create(source, target) ⇒ Object

Access

Public Module Method

Summary

Creates a delta of two strings using the Fossil delta encoding algorithm.

Parameters

old - The old string.
new - The new string to create the delta from.

Returns a String that represents the deltaed differences between the two Strings.

Examples

# Create the delta between two strings
Fossilize.create("Hello World!", "Hello Everyone!")

# Create the delta between two files (note the passing of a File object)
source = File.new("README.md", "r")
target = File.new("README_new.md", "r")
Fossilize.create(source, target)

# You can also create a delta between a file and a string (the arguments are interchangeable)
Fossilize(source, "This is the new README for Fossilize!")


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fossilize.rb', line 51

def self.create(source, target)
  # Because this method can accept three different types of parameter (path, String or File)
  # we need to do a sanity check on the input parameters.
  source_string = check_input(source)
  target_string = check_input(target)

  # Create a bare string to hold to returning delta from the C function that's the
  # size of the target + 60 (according to the Fossil source docs).
  delta = ("\0" * (target_string.size + 60))

  # create the delta, retaining the size of the delta output and return the delta,
  # stripping out any excess left over (needs refinement...).
  delta_size = delta_create(source_string, source_string.size, target_string, target_string.size, delta)
  return delta.rstrip!
end