class NoSE::Backend::FileBackend

Simple backend which persists data to a file

Public Class Methods

finalize(index_data, file) click to toggle source

Save data when the object is destroyed

# 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
new(model, indexes, plans, update_plans, config) click to toggle source
Calls superclass method NoSE::Backend::Backend::new
# 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

Public Instance Methods

client() click to toggle source

We just produce the data here which can be manipulated as needed @return [Hash]

# File lib/nose/backend/file.rb, line 76
def client
  @index_data
end
generate_id() click to toggle source

Generate a simple UUID

# File lib/nose/backend/file.rb, line 47
def generate_id
  SecureRandom.uuid
end
index_empty?(index) click to toggle source

Check for an empty array for the data

# File lib/nose/backend/file.rb, line 32
def index_empty?(index)
  !index_exists?(index) || @index_data[index.key].empty?
end
index_exists?(index) click to toggle source

Check if we have prepared space for this index

# File lib/nose/backend/file.rb, line 37
def index_exists?(index)
  @index_data.key? index.key
end
index_insert_chunk(index, chunk) click to toggle source

@abstract Subclasses implement to allow inserting

# 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) click to toggle source

Sample a number of values from the given index

# 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) click to toggle source

Allocate space for data on the new indexes

# 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