Class: Pack::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/pack/index.rb

Constant Summary collapse

HEADER_SIZE =
8
FANOUT_SIZE =
1024
OID_LAYER =
2
CRC_LAYER =
3
OFS_LAYER =
4
EXT_LAYER =
5
SIZES =
{
  OID_LAYER => 20,
  CRC_LAYER => 4,
  OFS_LAYER => 4,
  EXT_LAYER => 8
}

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Index

Returns a new instance of Index.



19
20
21
22
# File 'lib/pack/index.rb', line 19

def initialize(input)
  @input = input
  load_fanout_table
end

Instance Method Details

#oid_offset(oid) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pack/index.rb', line 24

def oid_offset(oid)
  pos = oid_position(oid)
  return nil if pos < 0

  offset = read_int32(OFS_LAYER, pos)

  return offset if offset < IDX_MAX_OFFSET

  pos = offset & (IDX_MAX_OFFSET - 1)
  @input.seek(offset_for(EXT_LAYER, pos))
  @input.read(8).unpack("Q>").first
end

#prefix_match(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pack/index.rb', line 37

def prefix_match(name)
  pos = oid_position(name)
  return [name] unless pos < 0

  @input.seek(offset_for(OID_LAYER, -1 - pos))
  oids = []

  loop do
    oid = @input.read(20).unpack("H40").first
    return oids unless oid.start_with?(name)
    oids << oid
  end
end