This commit is contained in:
James Bowes 2012-07-26 17:04:52 -03:00
parent a5b7fd02ac
commit ddf7d89408

View file

@ -12,12 +12,52 @@ end
# usage: ./content_from_pem.rb 5286016419950084643.pem # usage: ./content_from_pem.rb 5286016419950084643.pem
class Children
attr_accessor :children
def initialize()
@children = []
end
def each()
@children.each do |child|
yield child
end
end
def collect()
@children.each do |child|
yield child
end
end
def length()
@children.length
end
def [](i)
@children[i]
end
def []=(i, val)
@children[i] = val
end
def <<(other)
@children << other
end
def join(str)
@children.join(str)
end
end
class Node class Node
attr_accessor :path, :children, :de_duped, :written attr_accessor :path, :children, :de_duped, :written
def initialize(path) def initialize(path)
@path = path @path = path
@children = [] @children = Children.new
@sig = nil @sig = nil
@de_duped = false @de_duped = false
@written = false @written = false
@ -42,6 +82,8 @@ class Node
end end
def signature() def signature()
@sig = @path + "[" +
@children.collect { |x| x.signature }.join("|") + "]"
if @sig.nil? if @sig.nil?
@sig = @path + "[" + @sig = @path + "[" +
@children.collect { |x| x.signature }.join("|") + "]" @children.collect { |x| x.signature }.join("|") + "]"
@ -95,6 +137,7 @@ def compress_prefix(parent)
compress_prefix(child) compress_prefix(child)
end end
if parent.children.length == 1 if parent.children.length == 1
puts "compressing #{parent.path} and #{parent.children[0].path}"
parent.path += "/" + parent.children[0].path parent.path += "/" + parent.children[0].path
parent.children = parent.children[0].children parent.children = parent.children[0].children
end end
@ -104,7 +147,7 @@ end
# given a tree of nodes, try and find branches that match the children of node. # given a tree of nodes, try and find branches that match the children of node.
# if found, replace those branches with node's children # if found, replace those branches with node's children
def de_dupe(tree, node) def de_dupe(tree, node)
for i in 0..tree.children.count - 1 for i in 0..tree.children.length - 1
if tree.children[i] == node if tree.children[i] == node
# nothing # nothing
elsif node.signature == tree.children[i].signature elsif node.signature == tree.children[i].signature
@ -125,10 +168,10 @@ end
def binary_write(file, parent) def binary_write(file, parent)
file.write(parent.path) file.write(parent.path)
file.write("\0AAA") file.write("\0\0\0\0")
parent.children.each do |child| parent.children.each do |child|
# file.write(child.path) # file.write(child.path)
file.write("AAA") file.write("\0\0\0")
end end
parent.children.each do |child| parent.children.each do |child|
unless child.written unless child.written
@ -179,9 +222,9 @@ if $0 == __FILE__
parent = mk_hash(chunks, parent) parent = mk_hash(chunks, parent)
end end
# prime the signatures # prime the signatures
parent.signature
de_dupe_driver(parent, parent.flatten) de_dupe_driver(parent, parent.flatten)
parent = compress_prefix(parent) parent = compress_prefix(parent)
de_dupe_driver(parent, parent.flatten)
binary_write(binary, parent) binary_write(binary, parent)
file.write(parent.to_json) file.write(parent.to_json)
end end