Class: Ferret::Store::RAMDirectory::RAMLock

Inherits:
Lock
  • Object
show all
Defined in:
lib/ferret/store/ram_store.rb

Overview

A Lock is used to lock a data source (in this case a file) so that not more than one output stream can access a data source at one time.

Constant Summary

Constants inherited from Lock

Lock::MAX_ATTEMPTS

Instance Method Summary collapse

Methods inherited from Lock

#while_locked

Constructor Details

#initialize(lock_file, dir) ⇒ RAMLock

pass the name of the file that we are going to lock



247
248
249
250
# File 'lib/ferret/store/ram_store.rb', line 247

def initialize(lock_file, dir)
  @lock_file = lock_file
  @dir = dir
end

Instance Method Details

#locked?Boolean

returns true if there is a lock on the data source

Returns:

  • (Boolean)


277
278
279
# File 'lib/ferret/store/ram_store.rb', line 277

def locked?
  @dir.exists?(@lock_file)
end

#obtain(lock_timeout = 1) ⇒ Object

obtain the lock on the data source



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/ferret/store/ram_store.rb', line 253

def obtain(lock_timeout = 1) 
  MAX_ATTEMPTS.times do
    @dir.synchronize do
      # create a file if none exists. If one already exists
      # then someone beat us to the lock so return false
      if (! locked?) then
        @dir.create_output(@lock_file)
        return true
      end
    end
    # lock was not obtained so sleep for timeout then try again.
    sleep(lock_timeout)
  end
  # lock could not be obtained so raise an exception
  raise "could not obtain lock: " + @lock_file.to_s
end

#releaseObject

Release the lock on the data source. Returns true if successful.



271
272
273
274
# File 'lib/ferret/store/ram_store.rb', line 271

def release 
  @dir.delete(@lock_file)
  return true
end