Class: Oculus::Storage::FileStore

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

Defined Under Namespace

Classes: File

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FileStore

Returns a new instance of FileStore.



8
9
10
# File 'lib/oculus/storage/file_store.rb', line 8

def initialize(options)
  @root = options[:cache_path] || 'tmp/data'
end

Instance Method Details

#all_queriesObject



12
13
14
15
16
# File 'lib/oculus/storage/file_store.rb', line 12

def all_queries
  Dir["#{root}/*.query"].map do |path|
    Query.new(File.parse(path))
  end.sort { |a,b| b.id <=> a.id }
end

#delete_query(id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/oculus/storage/file_store.rb', line 56

def delete_query(id)
  star_path = starred_filename_for_id(id)
  File.unlink(star_path) if File.exist?(star_path)

  path = filename_for_id(id)

  if File.exist?(path)
    File.unlink(path)
  else
    raise QueryNotFound, id
  end
end

#load_query(id) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/oculus/storage/file_store.rb', line 46

def load_query(id)
  path = filename_for_id(id)

  if File.exist?(path)
    File.parse(path)
  else
    raise QueryNotFound, id
  end
end

#save_query(query) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/oculus/storage/file_store.rb', line 24

def save_query(query)
  ensure_root_path

  query.id = next_id if query.id.nil?

  File.open(filename_for_id(query.id), 'w') do |file|
    file.flock(File::LOCK_EX)
    file.write_prelude(query.attributes)
    file.write_results(query.results) if query.results && query.results.length > 0
    file.flock(File::LOCK_UN)
  end

  FileUtils.mkdir_p(File.join(root, "starred")) unless Dir.exist?(File.join(root, "starred"))
  star_path = starred_filename_for_id(query.id)

  if query.starred
    File.symlink(File.join('..', "#{query.id}.query"), star_path) unless File.exist?(star_path)
  elsif File.exist?(star_path)
    File.unlink(star_path)
  end
end

#starred_queriesObject



18
19
20
21
22
# File 'lib/oculus/storage/file_store.rb', line 18

def starred_queries
  Dir["#{root}/starred/*.query"].map do |path|
    Query.new(File.parse(path))
  end.sort { |a,b| b.id <=> a.id }
end