Class: Ferret::Store::RAMDirectory

Inherits:
Directory
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/ferret/store/ram_store.rb,
ext/ram_directory.c

Defined Under Namespace

Classes: RAMFile, RAMIndexInput, RAMIndexOutput, RAMLock

Constant Summary

Constants inherited from Directory

Directory::LOCK_PREFIX

Instance Method Summary collapse

Methods inherited from Directory

#file_count

Constructor Details

#initialize(dir = nil, close_dir = false) ⇒ RAMDirectory

Returns a new instance of RAMDirectory.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ferret/store/ram_store.rb', line 7

def initialize(dir = nil, close_dir = false)
  super()
  @files = Hash.new
  unless dir.nil?
    dir.each do |file|
      os = create_output(file)    # make a place on ram disk
      is = dir.open_input(file)   # read the current file
      len = is.length             # and copy the file to ram disk
      buf = Array.new(len)
      is.read_bytes(buf, 0, len)
      os.write_bytes(buf, len)
      is.close()
      os.close()
    end
    dir.close() if close_dir
  end
end

Instance Method Details

#closeObject

Closes the store.



97
98
# File 'lib/ferret/store/ram_store.rb', line 97

def close()
end

#create_output(name) ⇒ Object

Creates a new, empty file in the directory with the given name. Returns a stream writing this file.



71
72
73
74
75
# File 'lib/ferret/store/ram_store.rb', line 71

def create_output(name)
  file = RAMFile.new(name)
  @files[name] = file
  RAMIndexOutput.new(file)
end

#delete(name) ⇒ Object

Removes an existing file in the directory.



52
53
54
# File 'lib/ferret/store/ram_store.rb', line 52

def delete(name)
  @files.delete(name)
end

#eachObject

returns an array of strings, one for each file in the directory



26
27
28
29
30
31
# File 'lib/ferret/store/ram_store.rb', line 26

def each()
  @files.each do |path, file|
    next if file =~ /#{LOCK_PREFIX}/
    yield file
  end
end

#exists?(name) ⇒ Boolean

Returns true if a file with the given name exists.

Returns:

  • (Boolean)


34
35
36
# File 'lib/ferret/store/ram_store.rb', line 34

def exists?(name)
  @files.has_key?(name)
end

#length(name) ⇒ Object

Returns the length of a file in the directory.



65
66
67
# File 'lib/ferret/store/ram_store.rb', line 65

def length(name)
  @files[name].length
end

#make_lock(name) ⇒ Object

Construct a Lock.



91
92
93
# File 'lib/ferret/store/ram_store.rb', line 91

def make_lock(name) 
  RAMLock.new(LOCK_PREFIX + name, self)
end

#modified(name) ⇒ Object

Returns the time the named file was last modified.



39
40
41
# File 'lib/ferret/store/ram_store.rb', line 39

def modified(name)
  @files[name].mtime
end

#open_input(name) ⇒ Object

Returns a stream reading an existing file.

Raises:

  • (IOError)


78
79
80
81
# File 'lib/ferret/store/ram_store.rb', line 78

def open_input(name)
  raise IOError, "No file #{name}" if @files[name].nil?
  RAMIndexInput.new(@files[name])
end


83
84
85
86
87
88
# File 'lib/ferret/store/ram_store.rb', line 83

def print_file(name)
  input = RAMIndexInput.new(@files[name])
  buf = " " * input.length
  input.read_internal(buf, 0, input.length)
  puts buf
end

#rename(from, to) ⇒ Object

Renames an existing file in the directory. If a file already exists with the new name, then it is replaced. This replacement should be atomic.



59
60
61
62
# File 'lib/ferret/store/ram_store.rb', line 59

def rename(from, to)
  @files[to] = @files[from]
  @files.delete(from)
end

#to_sObject



100
101
102
103
104
105
106
# File 'lib/ferret/store/ram_store.rb', line 100

def to_s
  str = "The files in this directory are: \n"
  @files.each do |path, file|
    str << path + " - " + file.size.to_s + "\n"
  end
  str
end

#touch(name) ⇒ Object

Set the modified time of an existing file to now.



44
45
46
47
48
49
# File 'lib/ferret/store/ram_store.rb', line 44

def touch(name)
  if @files[name].nil?
    @files[name] = RAMFile.new(name)
  end
  @files[name].mtime = Time.now
end