Class: RubyFFDB::StorageEngines::YamlEngine

Inherits:
RubyFFDB::StorageEngine show all
Defined in:
lib/rffdb/storage_engines/yaml_engine.rb

Class Method Summary collapse

Methods inherited from RubyFFDB::StorageEngine

cache, cache_lookup, cache_provider, cache_size, cache_store, flush, index, index_lookup, index_update, next_id, read_lock, write_lock

Class Method Details

.all(type) ⇒ Object

Lazily grab all document ids in use



39
40
41
42
43
44
45
46
47
48
# File 'lib/rffdb/storage_engines/yaml_engine.rb', line 39

def self.all(type)
  directory_glob = read_lock(type) do
    Dir.glob(File.join(File.dirname(file_path(type, 0)), '*.yml'))
  end
  if directory_glob && !directory_glob.empty?
    directory_glob.map { |doc| Integer(File.basename(doc, '.yml')) }.sort
  else
    []
  end
end

.file_path(type, object_id) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/rffdb/storage_engines/yaml_engine.rb', line 50

def self.file_path(type, object_id)
  File.join(
    DB_DATA,
    type.to_s.gsub('::', '__'),
    'documents',
    object_id.to_s + '.yml'
  )
end

.retrieve(type, object_id, use_caching = true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rffdb/storage_engines/yaml_engine.rb', line 22

def self.retrieve(type, object_id, use_caching = true)
  result = nil
  begin
    result = cache_lookup(type, object_id) if use_caching
    read_lock(type) do
      result ||= YAML.load_file(file_path(type, object_id))
    end
    write_lock(type) do
      cache_store(type, object_id, result)
    end
  rescue => e
    puts e.message
  end
  result.dup # Return a duplicate to support caching
end

.store(type, object_id, data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rffdb/storage_engines/yaml_engine.rb', line 7

def self.store(type, object_id, data)
  path = file_path(type, object_id)
  write_lock(type) do
    FileUtils.mkdir_p(File.dirname(path))
    File.open(path, 'w') do |file|
      file.puts YAML.dump(data)
    end
    type.structure.collect {|k,v| k if v[:index] }.compact.each do |col|
      index_update(type, col, object_id, data[col.to_s])
    end
    cache_store(type, object_id, data)
  end
  true
end