Class: Gitgo::Index::IdxFile

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

Overview

IdxFile is a wrapper providing access to a file of L packed integers.

Constant Summary collapse

PACK =

The pack format

"L*"
UNPACK =

The unpack format

"L*"
PACKED_ENTRY_SIZE =

The size of a packed integer

4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ IdxFile

Initializes a new ShaFile with the specified file. The file will be set to binary mode.



70
71
72
73
# File 'lib/gitgo/index/idx_file.rb', line 70

def initialize(file)
  file.binmode
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

The file being wrapped



66
67
68
# File 'lib/gitgo/index/idx_file.rb', line 66

def file
  @file
end

Class Method Details

.append(path, int) ⇒ Object

Opens the file and appends the int. Provide an array of integers to append multiple integers at once.



41
42
43
44
45
# File 'lib/gitgo/index/idx_file.rb', line 41

def append(path, int)
  dir = File.dirname(path)
  FileUtils.mkdir_p(dir) unless File.exists?(dir)
  open(path, "a") {|idx_file| idx_file.write(int) }
end

.open(path, mode = "r") ⇒ Object

Opens and returns an idx file in the specified mode. If a block is given the file is yielded to it and closed afterwards; in this case the return of open is the block result.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitgo/index/idx_file.rb', line 13

def open(path, mode="r")
  idx_file = new(File.open(path, mode))
  
  return idx_file unless block_given?

  begin
    yield(idx_file)
  ensure
    idx_file.close
  end
end

.read(path) ⇒ Object

Reads the file and returns an array of integers.



26
27
28
# File 'lib/gitgo/index/idx_file.rb', line 26

def read(path)
  open(path) {|idx_file| idx_file.read(nil) }
end

.rm(path, *ints) ⇒ Object

Opens the file and removes the integers.



48
49
50
51
52
53
# File 'lib/gitgo/index/idx_file.rb', line 48

def rm(path, *ints)
  return unless File.exists?(path)
  
  current = read(path)
  write(path, (current-ints))
end

.write(path, int) ⇒ Object

Opens the file and writes the integer; previous contents are replaced. Provide an array of integers to write multiple integers at once.



33
34
35
36
37
# File 'lib/gitgo/index/idx_file.rb', line 33

def write(path, int)
  dir = File.dirname(path)
  FileUtils.mkdir_p(dir) unless File.exists?(dir)
  open(path, "w") {|idx_file| idx_file.write(int) }
end

Instance Method Details

#append(int) ⇒ Object

Appends the integers to the file. Provide an array of integers to append multiple integers at once.



112
113
114
115
116
# File 'lib/gitgo/index/idx_file.rb', line 112

def append(int)
  file.pos = file.size
  write(int)
  self
end

#closeObject

Closes file



76
77
78
# File 'lib/gitgo/index/idx_file.rb', line 76

def close
  file.close
end

#currentObject

The index of the current entry.



81
82
83
# File 'lib/gitgo/index/idx_file.rb', line 81

def current
  file.pos / PACKED_ENTRY_SIZE
end

#read(n = 10, start = 0) ⇒ Object

Reads n entries from the start index and returns them as an array. Nil n will read all remaining entries and nil start will read from the current index.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitgo/index/idx_file.rb', line 88

def read(n=10, start=0)
  if start
    start_pos = start * PACKED_ENTRY_SIZE 
    file.pos = start_pos
  end
  
  str = file.read(n.nil? ? nil : n * PACKED_ENTRY_SIZE).to_s
  unless str.length % PACKED_ENTRY_SIZE == 0
    raise "invalid packed int length: #{str.length}"
  end
  
  str.unpack(UNPACK)
end

#write(int) ⇒ Object

Writes the integers to the file at the current index. Provide an array of integers to write multiple integers at once.



104
105
106
107
108
# File 'lib/gitgo/index/idx_file.rb', line 104

def write(int)
  int = [int] unless int.respond_to?(:pack)
  file.write int.pack(PACK)
  self
end