### Home Classes Methods

### In Files

- ext/r_store.c

### Parent

Object

### Namespace

- CLASS Ferret::Store::Lock::LockError

### Methods

- #locked?
- #obtain
- #release
- #while_locked

### Files

- MIT-LICENSE
- README
- TODO
- TUTORIAL

### Class/Module Index

Quicksearch

- Ferret
- Ferret::Analysis
- Ferret::Analysis::Analyzer
- Ferret::Analysis::AsciiLetterAnalyzer
- Ferret::Analysis::AsciiLetterTokenizer
- Ferret::Analysis::AsciiLowerCaseFilter
- Ferret::Analysis::AsciiStandardAnalyzer
- Ferret::Analysis::AsciiStandardTokenizer
- Ferret::Analysis::AsciiWhiteSpaceAnalyzer
- Ferret::Analysis::AsciiWhiteSpaceTokenizer
- Ferret::Analysis::HyphenFilter
- Ferret::Analysis::LetterAnalyzer
- Ferret::Analysis::LetterTokenizer
- Ferret::Analysis::LowerCaseFilter
- Ferret::Analysis::MappingFilter
- Ferret::Analysis::PerFieldAnalyzer
- Ferret::Analysis::RegExpAnalyzer
- Ferret::Analysis::RegExpTokenizer
- Ferret::Analysis::StandardAnalyzer
- Ferret::Analysis::StandardTokenizer
- Ferret::Analysis::StemFilter
- Ferret::Analysis::StopFilter
- Ferret::Analysis::Token
- Ferret::Analysis::TokenStream
- Ferret::Analysis::WhiteSpaceAnalyzer
- Ferret::Analysis::WhiteSpaceTokenizer
- Ferret::BlankSlate
- Ferret::BoostMixin
- Ferret::Browser
- Ferret::Browser::Controller
- Ferret::Browser::Delegator
- Ferret::Browser::DocumentController
- Ferret::Browser::ErrorController
- Ferret::Browser::HelpController
- Ferret::Browser::HomeController
- Ferret::Browser::TermController
- Ferret::Browser::TermVectorController
- Ferret::Browser::ViewHelper
- Ferret::Document
- Ferret::Field
- Ferret::FieldSymbol
- Ferret::FieldSymbolMethods
- Ferret::FileNotFoundError
- Ferret::Index
- Ferret::Index::FieldInfo
- Ferret::Index::FieldInfos
- Ferret::Index::Index
- Ferret::Index::IndexReader
- Ferret::Index::IndexWriter
- Ferret::Index::LazyDoc
- Ferret::Index::LazyDoc::LazyDocData
- Ferret::Index::TermDocEnum
- Ferret::Index::TermEnum
- Ferret::Index::TermVector
- Ferret::Index::TermVector::TVOffsets
- Ferret::Index::TermVector::TVTerm
- Ferret::ParseError
- Ferret::QueryParser
- Ferret::QueryParser::QueryParseException
- Ferret::Search
- Ferret::Search::BooleanQuery
- Ferret::Search::BooleanQuery::BooleanClause
- Ferret::Search::ConstantScoreQuery
- Ferret::Search::Explanation
- Ferret::Search::Filter
- Ferret::Search::FilteredQuery
- Ferret::Search::FuzzyQuery
- Ferret::Search::Hit
- Ferret::Search::MatchAllQuery
- Ferret::Search::MultiSearcher
- Ferret::Search::MultiTermQuery
- Ferret::Search::PhraseQuery
- Ferret::Search::PrefixQuery
- Ferret::Search::Query
- Ferret::Search::QueryFilter
- Ferret::Search::RangeFilter
- Ferret::Search::RangeQuery
- Ferret::Search::Searcher
- Ferret::Search::Sort
- Ferret::Search::SortField
- Ferret::Search::Spans
- Ferret::Search::Spans::SpanFirstQuery
- Ferret::Search::Spans::SpanMultiTermQuery
- Ferret::Search::Spans::SpanNearQuery
- Ferret::Search::Spans::SpanNotQuery
- Ferret::Search::Spans::SpanOrQuery
- Ferret::Search::Spans::SpanPrefixQuery
- Ferret::Search::Spans::SpanTermQuery
- Ferret::Search::TermQuery
- Ferret::Search::TopDocs
- Ferret::Search::TypedRangeFilter
- Ferret::Search::TypedRangeQuery
- Ferret::Search::WildcardQuery
- Ferret::StateError
- Ferret::Store
- Ferret::Store::Directory
- Ferret::Store::FSDirectory
- Ferret::Store::Lock
- Ferret::Store::Lock::LockError
- Ferret::Store::RAMDirectory
- Ferret::Utils
- Ferret::Utils::BitVector
- Ferret::Utils::MultiMapper
- Ferret::Utils::PriorityQueue
- WEBrick
- WEBrick::FerretBrowserHandler
- Date
- DateTime
- FileUtils
- Float
- Integer
- String
- Symbol
- Time

No matching classes.

# Ferret::Store::Lock

A Lock is used to lock a data source so that not more than one output stream can access a data source at one time. It is possible that locks could be disabled. For example a read only index stored on a CDROM would have no need for a lock.

You can use a lock in two ways. Firstly:

    write_lock = @directory.make_lock(LOCK_NAME)
    write_lock.obtain(WRITE_LOCK_TIME_OUT)
      ... # Do your file modifications # ...
    write_lock.release()

Alternatively you could use the while locked method. This ensures that the lock will be released once processing has finished.

    write_lock = @directory.make_lock(LOCK_NAME)
    write_lock.while_locked(WRITE_LOCK_TIME_OUT) do
      ... # Do your file modifications # ...
    end

### Public Instance Methods

locked? → bool click to toggle source

Returns true if the lock has been obtained.

    static VALUE
    frb_lock_is_locked(VALUE self)
    {
        Lock *lock;
        GET_LOCK(lock, self);
        return lock->is_locked(lock) ? Qtrue : Qfalse;
    }

obtain(timeout = 1) → bool click to toggle source

Obtain a lock. Returns true if lock was successfully obtained. Make sure the lock is released using Lock#release. Otherwise you'll be left with a stale lock file.

The timeout defaults to 1 second and 5 attempts are made to obtain the lock. If you're doing large batch updates on the index with multiple processes you may need to increase the lock timeout but 1 second will be substantial in most cases.

timeout | seconds to wait to obtain lock before timing out and returning false
return | true if lock was successfully obtained. Raises a Lock::LockError otherwise.

    static VALUE
    frb_lock_obtain(int argc, VALUE *argv, VALUE self)
    {
        VALUE rtimeout;
        int timeout = 1;
        Lock *lock;
        GET_LOCK(lock, self);

        if (rb_scan_args(argc, argv, "01", &rtimeout) > 0) {
            timeout = FIX2INT(rtimeout);
        }
        /* TODO: use the lock timeout */
        if (!lock->obtain(lock)) {
            rb_raise(cLockError, "could not obtain lock: #%s", lock->name);
        }
        return Qtrue;
    }

release() → self click to toggle source

Release the lock. This should only be called by the process which obtains the lock.

    static VALUE
    frb_lock_release(VALUE self)
    {
        Lock *lock;
        GET_LOCK(lock, self);
        lock->release(lock);
        return self;
    }

while_locked(timeout = 1) { do_something() } → bool click to toggle source

Run the code in a block while a lock is obtained, automatically releasing the lock when the block returns.

See Lock#obtain for more information on lock timeout.

timeout | seconds to wait to obtain lock before timing out and returning false
return | true if lock was successfully obtained. Raises a Lock::LockError otherwise.

    static VALUE
    frb_lock_while_locked(int argc, VALUE *argv, VALUE self)
    {
        VALUE rtimeout;
        int timeout = 1;
        Lock *lock;
        GET_LOCK(lock, self);
        if (rb_scan_args(argc, argv, "01", &rtimeout) > 0) {
            timeout = FIX2INT(rtimeout);
        }
        if (!lock->obtain(lock)) {
            rb_raise(cLockError, "could not obtain lock: #%s", lock->name);
        }
        rb_yield(Qnil);
        lock->release(lock);
        return Qtrue;
    }

[Validate]

Generated with the Darkfish Rdoc Generator 2.