Class: Ferret::Store::RAMDirectory
- 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
Instance Method Summary collapse
-
#close ⇒ Object
Closes the store.
-
#create_output(name) ⇒ Object
Creates a new, empty file in the directory with the given name.
-
#delete(name) ⇒ Object
Removes an existing file in the directory.
-
#each ⇒ Object
returns an array of strings, one for each file in the directory.
-
#exists?(name) ⇒ Boolean
Returns true if a file with the given name exists.
-
#initialize(dir = nil, close_dir = false) ⇒ RAMDirectory
constructor
A new instance of RAMDirectory.
-
#length(name) ⇒ Object
Returns the length of a file in the directory.
-
#make_lock(name) ⇒ Object
Construct a Lock.
-
#modified(name) ⇒ Object
Returns the time the named file was last modified.
-
#open_input(name) ⇒ Object
Returns a stream reading an existing file.
- #print_file(name) ⇒ Object
-
#rename(from, to) ⇒ Object
Renames an existing file in the directory.
- #to_s ⇒ Object
-
#touch(name) ⇒ Object
Set the modified time of an existing file to now.
Methods inherited from Directory
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
#close ⇒ Object
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 |
#each ⇒ Object
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.
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.
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 |
#print_file(name) ⇒ Object
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_s ⇒ Object
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 |