Class: ActsAsIndexed::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_indexed/storage.rb

Defined Under Namespace

Classes: OldIndexVersion

Constant Summary collapse

INDEX_FILE_EXTENSION =
'.ind'
TEMP_FILE_EXTENSION =
'.tmp'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Storage

Returns a new instance of Storage.



11
12
13
14
15
16
17
# File 'lib/acts_as_indexed/storage.rb', line 11

def initialize(config)
  @path = Pathname.new(config.index_file.to_s)
  @size_path = @path.join('size')
  @prefix_size = config.index_file_depth
  @is_windows_filesystem = config.is_windows_filesystem?
  prepare
end

Instance Method Details

#add(atoms, count = 1) ⇒ Object

Takes a hash of atoms and adds these to storage.



20
21
22
23
24
# File 'lib/acts_as_indexed/storage.rb', line 20

def add(atoms, count=1)
  operate(:+, atoms)

  update_record_count(count)
end

#fetch(atom_names, start = false) ⇒ Object

Takes a string array of atoms names return a hash of the relevant atoms.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/acts_as_indexed/storage.rb', line 35

def fetch(atom_names, start=false)
  atoms = ActiveSupport::OrderedHash.new

  atom_names.uniq.collect{|a| encoded_prefix(a) }.uniq.each do |prefix|
    pattern = @path.join(prefix.to_s).to_s
    pattern += '*' if start
    pattern += INDEX_FILE_EXTENSION

    Pathname.glob(pattern).each do |atom_file|
      atom_file.open do |f|
        atoms.merge!(Marshal.load(f))
      end
    end # Pathname.glob

  end # atom_names.uniq
  atoms
end

#record_countObject

Returns the number of records currently stored in this index.



54
55
56
57
58
59
60
61
62
# File 'lib/acts_as_indexed/storage.rb', line 54

def record_count
  @size_path.read.to_i

# This is a bit horrible.
rescue Errno::ENOENT
  0
rescue EOFError
  0
end

#remove(atoms) ⇒ Object

Takes a hash of atoms and removes these from storage.



27
28
29
30
31
# File 'lib/acts_as_indexed/storage.rb', line 27

def remove(atoms)
  operate(:-, atoms)

  update_record_count(-1)
end