Class: NoSE::Backend::FileBackend
- Includes:
- Subtype
- Defined in:
- lib/nose/backend/file.rb
Overview
Simple backend which persists data to a file
Defined Under Namespace
Modules: RowMatcher Classes: DeleteStatementStep, IndexLookupStatementStep, InsertStatementStep
Class Method Summary collapse
-
.finalize(index_data, file) ⇒ Object
Save data when the object is destroyed.
Instance Method Summary collapse
-
#client ⇒ Hash
We just produce the data here which can be manipulated as needed.
-
#generate_id ⇒ Object
Generate a simple UUID.
-
#index_empty?(index) ⇒ Boolean
Check for an empty array for the data.
-
#index_exists?(index) ⇒ Boolean
Check if we have prepared space for this index.
- #index_insert_chunk(index, chunk) ⇒ Object abstract
-
#index_sample(index, count) ⇒ Object
Sample a number of values from the given index.
-
#indexes_ddl(execute = false, skip_existing = false, drop_existing = false) ⇒ Object
Allocate space for data on the new indexes.
-
#initialize(model, indexes, plans, update_plans, config) ⇒ FileBackend
constructor
A new instance of FileBackend.
Methods included from Subtype
Methods inherited from Backend
#by_id_graph, #drop_index, #indexes_sample, #prepare, #prepare_query, #prepare_update, #query, #update
Methods included from Supertype
Methods included from Listing
Constructor Details
#initialize(model, indexes, plans, update_plans, config) ⇒ FileBackend
Returns a new instance of FileBackend.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/nose/backend/file.rb', line 9 def initialize(model, indexes, plans, update_plans, config) super # Try to load data from file or start fresh @index_data = if !config[:file].nil? && File.file?(config[:file]) Marshal.load File.open(config[:file]) else {} end # Ensure the data is saved when we exit ObjectSpace.define_finalizer self, self.class.finalize(@index_data, config[:file]) end |
Class Method Details
.finalize(index_data, file) ⇒ Object
Save data when the object is destroyed
25 26 27 28 29 |
# File 'lib/nose/backend/file.rb', line 25 def self.finalize(index_data, file) proc do Marshal.dump(index_data, File.open(file, 'w')) end end |
Instance Method Details
#client ⇒ Hash
We just produce the data here which can be manipulated as needed
76 77 78 |
# File 'lib/nose/backend/file.rb', line 76 def client @index_data end |
#generate_id ⇒ Object
Generate a simple UUID
47 48 49 |
# File 'lib/nose/backend/file.rb', line 47 def generate_id SecureRandom.uuid end |
#index_empty?(index) ⇒ Boolean
Check for an empty array for the data
32 33 34 |
# File 'lib/nose/backend/file.rb', line 32 def index_empty?(index) !index_exists?(index) || @index_data[index.key].empty? end |
#index_exists?(index) ⇒ Boolean
Check if we have prepared space for this index
37 38 39 |
# File 'lib/nose/backend/file.rb', line 37 def index_exists?(index) @index_data.key? index.key end |
#index_insert_chunk(index, chunk) ⇒ Object
Subclasses implement to allow inserting
42 43 44 |
# File 'lib/nose/backend/file.rb', line 42 def index_insert_chunk(index, chunk) @index_data[index.key].concat chunk end |
#index_sample(index, count) ⇒ Object
Sample a number of values from the given index
69 70 71 72 |
# File 'lib/nose/backend/file.rb', line 69 def index_sample(index, count) data = @index_data[index.key] data.nil? ? [] : data.sample(count) end |
#indexes_ddl(execute = false, skip_existing = false, drop_existing = false) ⇒ Object
Allocate space for data on the new indexes
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/nose/backend/file.rb', line 52 def indexes_ddl(execute = false, skip_existing = false, drop_existing = false) @indexes.each do |index| # Do the appropriate behaviour based on the flags passed in if index_exists?(index) next if skip_existing fail unless drop_existing end @index_data[index.key] = [] end if execute # We just use the original index definition as DDL @indexes.map(&:inspect) end |