Class: Database::Loose

Inherits:
Object
  • Object
show all
Defined in:
lib/database/loose.rb

Instance Method Summary collapse

Constructor Details

#initialize(pathname) ⇒ Loose

Returns a new instance of Loose.



9
10
11
# File 'lib/database/loose.rb', line 9

def initialize(pathname)
  @pathname = pathname
end

Instance Method Details

#has?(oid) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/database/loose.rb', line 13

def has?(oid)
  File.file?(object_path(oid))
end

#load_info(oid) ⇒ Object



17
18
19
20
21
22
# File 'lib/database/loose.rb', line 17

def load_info(oid)
  type, size, _ = read_object_header(oid, 128)
  Raw.new(type, size)
rescue Errno::ENOENT
  nil
end

#load_raw(oid) ⇒ Object



24
25
26
27
28
29
# File 'lib/database/loose.rb', line 24

def load_raw(oid)
  type, size, scanner = read_object_header(oid)
  Raw.new(type, size, scanner.rest)
rescue Errno::ENOENT
  nil
end

#prefix_match(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/database/loose.rb', line 31

def prefix_match(name)
  dirname = object_path(name).dirname

  oids = Dir.entries(dirname).map do |filename|
    "#{ dirname.basename }#{ filename }"
  end

  oids.select { |oid| oid.start_with?(name) }
rescue Errno::ENOENT
  []
end

#write_object(oid, content) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/database/loose.rb', line 43

def write_object(oid, content)
  path = object_path(oid)
  return if File.exist?(path)

  file = TempFile.new(path.dirname, "tmp_obj")
  file.write(Zlib::Deflate.deflate(content, Zlib::BEST_SPEED))
  file.move(path.basename)
end