Class: Hammerspace::Backend::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HashMethods
Defined in:
lib/hammerspace/backend.rb

Overview

“Backend” class from which concrete backends extend

Mixes in Enumerable and HashMethods to provide default implementations of most methods that Ruby’s hash supports. Also provides some basic file and lock handling methods common to backends.

Direct Known Subclasses

Sparkey

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashMethods

#==, #[], #[]=, #assoc, #clear, #delete, #delete_if, #each, #each_key, #each_value, #empty?, #eql?, #fetch, #flatten, #has_key?, #has_value?, #include?, #keep_if, #key, #keys, #member?, #merge!, #rassoc, #reject!, #replace, #select!, #shift, #size, #to_hash, #values, #values_at

Constructor Details

#initialize(frontend, path, options = {}) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
26
# File 'lib/hammerspace/backend.rb', line 20

def initialize(frontend, path, options={})
  @frontend = frontend
  @path     = path
  @options  = options

  check_fs unless File.exist?(lockfile_path)
end

Instance Attribute Details

#frontendObject (readonly)

Returns the value of attribute frontend.



16
17
18
# File 'lib/hammerspace/backend.rb', line 16

def frontend
  @frontend
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/hammerspace/backend.rb', line 18

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/hammerspace/backend.rb', line 17

def path
  @path
end

Instance Method Details

#check_fsObject



40
41
42
# File 'lib/hammerspace/backend.rb', line 40

def check_fs
  warn_flock unless flock_works?
end

#closeObject

HashMethods (mixed in above) defines four methods that must be overridden. The default implementations simply raise NotImplementedError. The four methods are: [], []=, delete, and each.



32
33
34
# File 'lib/hammerspace/backend.rb', line 32

def close
  # No-op, should probably be overridden
end

#flock_works?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hammerspace/backend.rb', line 44

def flock_works?
  flock_works = false
  ensure_path_exists(path)
  lockfile = Tempfile.new(['flock_works.', '.lock'], path)
  begin
    lockfile.close
    File.open(lockfile.path) do |outer|
      outer.flock(File::LOCK_EX)
      File.open(lockfile.path) do |inner|
        flock_works = inner.flock(File::LOCK_EX | File::LOCK_NB) == false
      end
    end
  rescue
  ensure
    lockfile.unlink
  end
  flock_works
end

#uidObject



36
37
38
# File 'lib/hammerspace/backend.rb', line 36

def uid
  # No-op, should probably be overridden
end