Class: Ferret::Store::Directory

A Directory is an object which is used to access the index storage. Ruby‘s IO API is not used so that we can use different storage mechanisms to store the index. Some examples are;

  • File system based storage (currently implemented as FSDirectory)
  • RAM based storage (currently implemented as RAMDirectory)
  • Database based storage

NOTE: Once a file has been written and closed, it can no longer be modified. To make any changes to the file it must be deleted and rewritten. For this reason, the method to open a file for writing is called create_output, while the method to open a file for reading is called open_input If there is a risk of simultaneous modifications of the files then locks should be used. See Lock to find out how.

Constants

NameValue
LOCK_PREFIX rb_str_new2(LOCK_PREFIX)

Public Instance Methods


dir.close() → nil

It is a good idea to close a directory when you have finished using it. Although the garbage collector will currently handle this for you, this behaviour may change in future.

/*
 *  call-seq:
 *     dir.close() -> nil
 *
 *  It is a good idea to close a directory when you have finished using it.
 *  Although the garbage collector will currently handle this for you, this
 *  behaviour may change in future.
 */
static VALUE
frt_dir_close(VALUE self)
{
    Store *store = DATA_PTR(self);
    int ref_cnt = FIX2INT(rb_ivar_get(self, id_ref_cnt)) - 1;
    rb_ivar_set(self, id_ref_cnt, INT2FIX(ref_cnt));
    if (ref_cnt < 0) {
        Frt_Unwrap_Struct(self);
        object_del(store);
        frt_unwrap_locks(store);
        store_deref(store);
    }
    return Qnil;
}

dir.delete(file_name) → nil

Remove file file_name from the directory. Returns true if successful.

/*
 *  call-seq:
 *     dir.delete(file_name) -> nil
 *
 *  Remove file +file_name+ from the directory. Returns true if successful.
 */
static VALUE
frt_dir_delete(VALUE self, VALUE rfname)
{
    Store *store = DATA_PTR(self);
    StringValue(rfname);
    return (store->remove(store, rs2s(rfname)) == 0) ? Qtrue : Qfalse;
}

dir.exists?(file_name) → nil

Return true if a file with the name file_name exists in the directory.

/*
 *  call-seq:
 *     dir.exists?(file_name) -> nil
 *
 *  Return true if a file with the name +file_name+ exists in the directory.
 */
static VALUE
frt_dir_exists(VALUE self, VALUE rfname)
{
    Store *store = DATA_PTR(self);
    StringValue(rfname);
    return store->exists(store, rs2s(rfname)) ? Qtrue : Qfalse;
}

dir.count → integer

Return a count of the number of files in the directory.

/*
 *  call-seq:
 *     dir.count -> integer
 *
 *  Return a count of the number of files in the directory.
 */
static VALUE
frt_dir_file_count(VALUE self)
{
    Store *store = DATA_PTR(self);
    return INT2FIX(store->count(store));
}

dir.make_lock(lock_name) → self

Make a lock with the name lock_name. Note that lockfiles will be stored in the directory with other files but they won‘t be visible to you. You should avoid using files with a .lck extension as this extension is reserved for lock files

/*
 *  call-seq:
 *     dir.make_lock(lock_name) -> self
 *
 *  Make a lock with the name +lock_name+. Note that lockfiles will be stored
 *  in the directory with other files but they won't be visible to you. You
 *  should avoid using files with a .lck extension as this extension is
 *  reserved for lock files
 */
static VALUE
frt_dir_make_lock(VALUE self, VALUE rlock_name)
{
    VALUE rlock;
    Lock *lock;
    Store *store = DATA_PTR(self);
    StringValue(rlock_name);
    lock = open_lock(store, rs2s(rlock_name));
    rlock = Data_Wrap_Struct(cLock, &frt_lock_mark, &frt_lock_free, lock);
    object_add(lock, rlock);
    return rlock;
}

dir.refresh → self

Delete all files in the directory. It gives you a clean slate.

/*
 *  call-seq:
 *     dir.refresh -> self
 *
 *  Delete all files in the directory. It gives you a clean slate.
 */
static VALUE
frt_dir_refresh(VALUE self)
{
    Store *store = DATA_PTR(self);
    store->clear_all(store);
    return self;
}

dir.rename(from, to) → self

Rename a file from from to to. An error will be raised if the file doesn‘t exist or there is some other type of IOError.

/*
 *  call-seq:
 *     dir.rename(from, to) -> self
 *
 *  Rename a file from +from+ to +to+. An error will be raised if the file
 *  doesn't exist or there is some other type of IOError.
 */
static VALUE
frt_dir_rename(VALUE self, VALUE rfrom, VALUE rto)
{
    Store *store = DATA_PTR(self);
    StringValue(rfrom);
    StringValue(rto);
    store->rename(store, rs2s(rfrom), rs2s(rto));
    return self;
}

dir.touch(file_name) → nil

Create an empty file in the directory with the name file_name.

/*
 *  call-seq:
 *     dir.touch(file_name) -> nil
 *
 *  Create an empty file in the directory with the name +file_name+.
 */
static VALUE
frt_dir_touch(VALUE self, VALUE rfname)
{
    Store *store = DATA_PTR(self);
    StringValue(rfname);
    store->touch(store, rs2s(rfname));
    return Qnil;
}