Class: Oculus::Storage::FileStore
- Inherits:
-
Object
- Object
- Oculus::Storage::FileStore
- Defined in:
- lib/oculus/storage/file_store.rb
Defined Under Namespace
Classes: File
Instance Method Summary collapse
- #all_queries ⇒ Object
- #delete_query(id) ⇒ Object
-
#initialize(options) ⇒ FileStore
constructor
A new instance of FileStore.
- #load_query(id) ⇒ Object
- #save_query(query) ⇒ Object
- #starred_queries ⇒ Object
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() @root = [:cache_path] || 'tmp/data' end |
Instance Method Details
#all_queries ⇒ Object
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 |